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 javax.microedition.sip;
028:
029: import com.sun.midp.i3test.TestCase;
030: import javax.microedition.io.Connector;
031: import java.io.InputStream;
032: import java.io.OutputStream;
033: import java.io.IOException;
034: import gov.nist.microedition.sip.SipServerConnectionImpl;
035:
036: /**
037: * Tests for end-to-end SIP client-server communication
038: *
039: */
040: public class TestOriginatingInvite extends TestCase implements
041: SipClientConnectionListener, SipServerConnectionListener {
042:
043: /** Connection notifier. */
044: SipConnectionNotifier scn = null;
045: /** Server connection. */
046: // SipServerConnection ssc = null;
047: /** Client connection. */
048: SipClientConnection scc = null;
049: /** Boolean flag to indicate that test was successful */
050: boolean done = false;
051: /** SIP request message from client to server */
052: String clientMsg = "Client : How are you?";
053: /** SIP response message from server to client */
054: String serverMsg = "Server : Fine";
055:
056: /**
057: * Open the connection and start the thread to call the run
058: * method. It waits until the InputStream is open before returning.
059: */
060: void setup() {
061: try {
062: // Open SIP server connection and listen to port 5090
063: scn = (SipConnectionNotifier) Connector.open("sip:5090");
064: } catch (Exception ex) {
065: assertNull("Exception during scn open", scn);
066: ex.printStackTrace();
067: }
068:
069: assertNotNull("scn is null", scn);
070:
071: try {
072: scn.setListener(this );
073:
074: // Open SIP client connection to the local SIP server
075: scc = (SipClientConnection) Connector
076: .open("sip:sippy.tester@localhost:5090");
077:
078: scc.setListener(this );
079: } catch (Exception ex) {
080: assertNull("Exception during sc open", scc);
081: ex.printStackTrace();
082: cleanup();
083: }
084:
085: assertNotNull("sc is null", scc);
086:
087: }
088:
089: /**
090: * Send a SIP message from user agent client to user agent server
091: * SipConnectionNotifier queues the incoming requests if the
092: * instance of SipServerConnection is not available yet
093: *
094: */
095: public void testOriginatingInvite() {
096:
097: try {
098: scc.initRequest("INVITE", scn);
099: //scc.setHeader("From", "sip:sippy.tester@localhost");
100: scc.setHeader("From", "sip:alice@some.server");
101: scc.setHeader("To", "sip:bob@some.other.server");
102: scc.setHeader("Subject", "Hello...");
103: scc.setHeader("Contact", "sip:user@"
104: + scn.getLocalAddress() + ":" + scn.getLocalPort());
105:
106: // write message body
107: scc.setHeader("Content-Type", "text/plain");
108: scc.setHeader("Content-Length", Integer.toString(clientMsg
109: .length()));
110: OutputStream os = scc.openContentOutputStream();
111: os.write(clientMsg.getBytes());
112: os.close(); // close stream and send the message
113:
114: // System.out.println("Inviting..." + scc.getHeader("To"));
115: } catch (IOException ioe) {
116: ioe.printStackTrace();
117: cleanup();
118: fail("IOException in testOriginatingInvite");
119: }
120:
121: }
122:
123: /**
124: * Accept a new connection request and process the request from client.
125: * Send "OK" to client
126: *
127: * This method is declared in SipServerConnectionListener
128: *
129: * @param scnLocal Local SIP connection notifier
130: */
131: public void notifyRequest(SipConnectionNotifier scnLocal) {
132: try {
133: // block and wait for incoming request.
134: // SipServerConnection is establised and returned
135: // when new request is received.
136: SipServerConnection ssc = scnLocal.acceptAndOpen();
137: assertNotNull("ssc is null", ssc);
138:
139: assertEquals("State should be REQUEST_RECEIVED",
140: SipServerConnectionImpl.REQUEST_RECEIVED,
141: ((SipServerConnectionImpl) ssc).getState());
142: // System.out.println("Request received : " + ssc.getMethod());
143:
144: if (ssc.getMethod().equals("INVITE")) {
145: ssc.initResponse(200);
146:
147: assertEquals("State should be INITIALIZED",
148: SipServerConnectionImpl.INITIALIZED,
149: ((SipServerConnectionImpl) ssc).getState());
150:
151: // write message body to respond back to client
152: ssc.setHeader("Content-Type", "text/plain");
153: ssc.setHeader("Content-Length", Integer
154: .toString(serverMsg.length()));
155: OutputStream os = ssc.openContentOutputStream();
156: assertEquals("State should be STREAM_OPEN",
157: SipServerConnectionImpl.STREAM_OPEN,
158: ((SipServerConnectionImpl) ssc).getState());
159:
160: os.write(serverMsg.getBytes());
161: os.close(); // close stream and send the message
162:
163: assertEquals("State should be COMPLETED",
164: SipServerConnectionImpl.COMPLETED,
165: ((SipServerConnectionImpl) ssc).getState());
166:
167: // ssc.send();
168: } else if (ssc.getMethod().equals("ACK")) {
169: // System.out.println("ACK received at server");
170: SipDialog dialog = ssc.getDialog();
171: SipClientConnection scm = dialog
172: .getNewClientConnection("MESSAGE");
173: scm.send();
174: // scc.setHeader("Content-Type", "text/plain");
175: } else if (ssc.getMethod().equals("MESSAGE")) {
176: // System.out.println("MESSAGE received at client");
177: SipDialog dialog = ssc.getDialog();
178: assertNotNull("dialog is null", dialog);
179: synchronized (this ) {
180: done = true;
181: notify();
182: }
183: }
184: ssc.close();
185: } catch (SipException sipex) {
186: assertNull("Unexpected SipException", sipex);
187: sipex.printStackTrace();
188: cleanup();
189: } catch (IOException ioe) {
190: assertNull("Unexpected IOException", ioe);
191: ioe.printStackTrace();
192: cleanup();
193: }
194: }
195:
196: /**
197: * Received the response from user agent server
198: *
199: * This method is declared in SipClientConnectionListener
200: *
201: * @param sccLocal Local SIP Client (UAC)
202: *
203: */
204: public void notifyResponse(SipClientConnection sccLocal) {
205: try {
206:
207: boolean ok = sccLocal.receive(0);
208:
209: if (ok) { // response received
210:
211: if (sccLocal.getStatusCode() == 200) {
212: String contentType = sccLocal
213: .getHeader("Content-Type");
214: String strLength = sccLocal
215: .getHeader("Content-Length");
216: strLength.trim();
217: int contentLength = Integer.parseInt(strLength);
218:
219: assertEquals(
220: "Server response message content length incorrect",
221: serverMsg.length(), contentLength);
222:
223: byte[] readBytes = new byte[contentLength];
224:
225: if ((contentType != null)
226: && contentType.equals("text/plain")) {
227: InputStream is = sccLocal
228: .openContentInputStream();
229: is.read(readBytes, 0, contentLength);
230: String receivedMsg = new String(readBytes);
231: assertEquals(
232: "Received response message content length incorrect",
233: receivedMsg.length(), contentLength);
234: }
235:
236: scc.initAck();
237: scc.setHeader("Content-Type", "text/plain");
238: scc.send();
239: }
240: }
241: } catch (Exception ex) {
242: // handle Exceptions
243: ex.printStackTrace();
244: }
245: }
246:
247: /**
248: * Close all connections.
249: *
250: */
251: void cleanup() {
252: // System.out.println("cleanup called");
253: try {
254: if (scc != null) {
255: scc.close();
256: }
257:
258: /*
259: if (ssc != null) {
260: ssc.close();
261: }
262:
263: assertEquals("State should be TERMINATED",
264: SipServerConnectionImpl.TERMINATED,
265: ((SipServerConnectionImpl)ssc).getState());
266: */
267:
268: if (scn != null) {
269: scn.close();
270: }
271: } catch (IOException ioe) {
272: assertNull("Unexpected IOException during cleanup", ioe);
273: ioe.printStackTrace();
274: }
275: }
276:
277: /**
278: * Tests execute.
279: *
280: */
281: public void runTests() {
282: declare("Test TestOriginatingINVITE ");
283:
284: setup();
285: testOriginatingInvite();
286:
287: synchronized (this ) {
288: while (!done) {
289: try {
290: wait();
291: } catch (InterruptedException e) {
292: System.out.println("Catch interrupt");
293: }
294: }
295: }
296:
297: cleanup();
298:
299: assertTrue("Failure in TestOriginatingInvite", done);
300: }
301:
302: }
|