001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package gov.nist.microedition.sip;
028:
029: import javax.microedition.sip.*;
030: import javax.microedition.io.Connector;
031: import gov.nist.microedition.sip.SipDialogImpl;
032: import gov.nist.siplite.header.ContactHeader;
033: import gov.nist.core.ParseException;
034: import java.io.OutputStream;
035: import com.sun.midp.i3test.TestCase;
036:
037: /**
038: * Tests for SipDialog(server side) class.
039: *
040: */
041: public class TestSipDialogServer extends TestCase {
042: /** SIP dialog */
043: SipDialog dialog;
044: /** Client connection */
045: SipClientConnection scc;
046: /** Connection Notifier */
047: SipConnectionNotifier scn;
048:
049: /**
050: * Test SipDialog for INVITE
051: */
052: public void TestInvite() {
053: try {
054: scn = (SipConnectionNotifier) Connector.open("sip:5060");
055: scc = (SipClientConnection) Connector
056: .open("sip:alice@localhost:5060");
057: scc.initRequest("INVITE", scn);
058:
059: scc.setHeader("From", "sip:sippy.tester@localhost");
060: scc.setHeader("Subject", "Hello...");
061: scc.setHeader("Contact", "sip:user@"
062: + scn.getLocalAddress() + ":" + scn.getLocalPort());
063:
064: // write message body
065: scc.setHeader("Content-Type", "text/plain");
066: String clientMsg = "Hello, how are you?";
067: scc.setHeader("Content-Length", Integer.toString(clientMsg
068: .length()));
069: OutputStream os = scc.openContentOutputStream();
070: os.write(clientMsg.getBytes());
071: os.close(); // close stream and send the message
072:
073: notifyInviteRequest(scn);
074: boolean resp = scc.receive(10000); // wait 10 secs for response
075: if (resp) {
076: if (scc.getStatusCode() == 200) {
077: // System.out.println("Received OK for INVITE");
078: }
079: }
080: scn.close();
081: scc.close();
082: } catch (Exception ex) {
083: // handle Exceptions
084: System.out.println("Exception received");
085: ex.printStackTrace();
086: }
087: assertTrue("Test1 done", true);
088: }
089:
090: /**
091: * Process INVITE request at UAS
092: * @param scn SipConnectionNotifier at UAS
093: */
094: public void notifyInviteRequest(SipConnectionNotifier scn) {
095: try {
096: SipServerConnection ssc;
097: // retrieve the request received
098: ssc = scn.acceptAndOpen();
099: if (ssc.getMethod().equals("INVITE")) {
100: SipDialog dialog = ssc.getDialog();
101: assertTrue("Dialog is not null", dialog == null);
102: ssc.initResponse(100);
103: ssc.send();
104: dialog = ssc.getDialog();
105: assertTrue("Dialog is null after sending 100 TRYING",
106: dialog != null);
107: assertTrue("Dialog is not in EARLY state", dialog
108: .getState() == SipDialog.EARLY);
109: /*
110: ssc.initResponse(200);
111: ssc.send();
112: assertTrue("Dialog is null after sending 200 OK",
113: dialog != null);
114: assertTrue("Dialog is not in CONFIRMED state",
115: dialog.getState() == SipDialog.CONFIRMED);
116: */
117: }
118: } catch (Exception ex) {
119: // handle Exceptions
120: }
121: }
122:
123: /**
124: * Test SipDialog for SUBSCRIBE
125: */
126: public void TestSubscribe() {
127: try {
128: scn = (SipConnectionNotifier) Connector.open("sip:5060");
129: scc = (SipClientConnection) Connector
130: .open("sip:alice@localhost:5060");
131: scc.initRequest("SUBSCRIBE", scn);
132: scc.setHeader("From", "sip:UserA@host.com");
133: scc.setHeader("Accept", "application/pidf+xml");
134: scc.setHeader("Event", "presence");
135: scc.setHeader("Expires", "950");
136: String contact = new String("sip:user@"
137: + scn.getLocalAddress() + ":" + scn.getLocalPort());
138: scc.setHeader("Contact", contact);
139: scc.send();
140: notifySubscribeRequest(scn);
141: boolean resp = scc.receive(10000); // wait 10 secs for response
142: if (resp) {
143: if (scc.getStatusCode() == 200) {
144: // System.out.println("Received OK for subscribe");
145: /*
146: dialog = scc.getDialog();
147: // initialize new SipClientConnection
148: scc = dialog.getNewClientConnection("SUBSCRIBE");
149: // read dialog Call-ID
150: callID = scc.getHeader("Call-ID");
151: // read remote tag
152: SipHeader sh = new SipHeader("To", scc.getHeader("To"));
153: remoteTag = sh.getParameter("tag");
154: // unSUBSCRIBE
155: scc.setHeader("Expires", "0");
156: scc.send();
157: */
158: }
159: } else {
160: // didn't receive any response in given time
161: }
162: scn.close();
163: scc.close();
164: } catch (Exception ex) {
165: // handle Exceptions
166: System.out.println("Exception received");
167: ex.printStackTrace();
168: }
169: assertTrue("Test1 done", true);
170: }
171:
172: /**
173: * Process SUBSCRIBE request at UAS
174: * @param scn SipConnectionNotifier at UAS
175: */
176: public void notifySubscribeRequest(SipConnectionNotifier scn) {
177: try {
178: SipServerConnection ssc;
179: // retrieve the request received
180: ssc = scn.acceptAndOpen();
181: if (ssc.getMethod().equals("SUBSCRIBE")) {
182: SipDialog dialog = ssc.getDialog();
183: assertTrue("Dialog is not null", dialog == null);
184: ssc.initResponse(200);
185: ssc.send();
186: dialog = ssc.getDialog();
187: assertTrue("Dialog is null after sending 200 OK",
188: dialog != null);
189: assertTrue("Dialog is not in CONFIRMED state", dialog
190: .getState() == SipDialog.CONFIRMED);
191: }
192: } catch (Exception ex) {
193: // handle Exceptions
194: }
195: }
196:
197: /**
198: * Tests execute
199: *
200: */
201: public void runTests() {
202: declare("INVITE request");
203: TestInvite();
204: declare("SUBSCRIBE request");
205: TestSubscribe();
206: }
207:
208: }
|