0001: /*
0002: *
0003: *
0004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
0005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
0006: *
0007: * This program is free software; you can redistribute it and/or
0008: * modify it under the terms of the GNU General Public License version
0009: * 2 only, as published by the Free Software Foundation.
0010: *
0011: * This program is distributed in the hope that it will be useful, but
0012: * WITHOUT ANY WARRANTY; without even the implied warranty of
0013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
0014: * General Public License version 2 for more details (a copy is
0015: * included at /legal/license.txt).
0016: *
0017: * You should have received a copy of the GNU General Public License
0018: * version 2 along with this work; if not, write to the Free Software
0019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
0020: * 02110-1301 USA
0021: *
0022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
0023: * Clara, CA 95054 or visit www.sun.com if you need additional
0024: * information or have any questions.
0025: */
0026:
0027: package gov.nist.microedition.sip;
0028:
0029: import javax.microedition.sip.*;
0030: import java.io.OutputStream;
0031: import gov.nist.microedition.sip.SipConnectionNotifierImpl;
0032: import gov.nist.microedition.sip.SipClientConnectionImpl;
0033: import gov.nist.siplite.SipStack;
0034: import gov.nist.siplite.message.Request;
0035: import gov.nist.siplite.message.Response;
0036: import gov.nist.siplite.header.WWWAuthenticateHeader;
0037: import gov.nist.siplite.header.AuthenticationHeader;
0038: import gov.nist.siplite.stack.ClientTransaction;
0039: import gov.nist.siplite.stack.MessageChannel;
0040: import gov.nist.siplite.stack.Dialog;
0041: import gov.nist.siplite.stack.Transaction;
0042: import gov.nist.siplite.stack.SIPServerResponseInterface;
0043: import gov.nist.siplite.stack.SIPStackMessageFactory;
0044: import gov.nist.siplite.stack.GetSipServerResponse;
0045: import gov.nist.siplite.address.Address;
0046: import gov.nist.siplite.header.ToHeader;
0047: import gov.nist.siplite.header.CallIdHeader;
0048: import gov.nist.siplite.header.FromHeader;
0049: import gov.nist.siplite.header.ContactHeader;
0050: import gov.nist.siplite.header.ExpiresHeader;
0051: import gov.nist.siplite.address.URI;
0052: import gov.nist.siplite.address.SipURI;
0053: import gov.nist.core.ParseException;
0054: import javax.microedition.io.*;
0055: import com.sun.midp.i3test.TestCase;
0056: import java.io.*;
0057:
0058: /**
0059: * Tests for SipConnection class.
0060: *
0061: */
0062: public class TestSipClientConnection extends TestCase {
0063:
0064: /** URI of other side of SIP session. */
0065: private String corrURI = "sip:sippy.tester@localhost:5060";
0066:
0067: /** Server tag. */
0068: private static final String toTag = StackConnector.generateTag();
0069:
0070: /** Flag of creating a dialog. */
0071: private boolean isDialogCreated = false;
0072:
0073: /** Refresh ID for refreshable requests. */
0074: private int refreshID = 0;
0075:
0076: /**
0077: * Body of the test 1.
0078: *
0079: * Walk along states of SipClientConnection object.
0080: * @param method string representation of the requests method
0081: * @param transport the name of transport protocol
0082: */
0083: void Test1(String method, String transport) {
0084: /** Client connection. */
0085: SipClientConnection sc = null;
0086: StubSipRefreshListener refreshListener = null;
0087:
0088: /**
0089: * Dialog is created in case of INVITE method (RFC 3261, 12.1)
0090: * or SUBSCRIBE method (RFC 3265, 3.3.4) after provisional requests.
0091: */
0092: isDialogCreated = method.equals("INVITE")
0093: || method.equals("SUBSCRIBE");
0094: try {
0095: // Open SIP client connection to the local SIP server
0096: sc = (SipClientConnection) Connector.open(corrURI
0097: + ";transport=" + transport);
0098: } catch (Exception ex) {
0099: assertNull("Exception during sc open", sc);
0100: }
0101: assertNotNull("sc is null", sc);
0102:
0103: // State of client connection must be "Created"
0104: assertEquals("State is not CREATED",
0105: ((SipClientConnectionImpl) sc).getState(),
0106: SipClientConnectionImpl.CREATED);
0107:
0108: // Check the CREATED state
0109: checkCreatedState(sc);
0110:
0111: // Move to INITIALIZED state
0112: try {
0113: sc.initRequest(method, null);
0114: refreshListener = new StubSipRefreshListener();
0115: refreshID = sc.enableRefresh(refreshListener);
0116: sc.setHeader("Content-Type", "text/plain");
0117:
0118: // SUBSCRIBE request - Event header is mandatory (RFC 3265, 7.2)
0119: if (method.equals("SUBSCRIBE")) {
0120: sc.setHeader("Event", "12345");
0121: }
0122:
0123: } catch (Throwable e) {
0124: fail("INITIALIZED " + e + " was caused");
0125: }
0126:
0127: assertEquals("State is not INITIALIZED",
0128: ((SipClientConnectionImpl) sc).getState(),
0129: SipClientConnectionImpl.INITIALIZED);
0130:
0131: // Check the INITIALIZED state after CREATED state
0132: checkInitializedState(sc, SipClientConnectionImpl.CREATED);
0133:
0134: // Move to STREAM_OPEN state
0135: String msg = "SipClientConnection class testing.";
0136: OutputStream os = null;
0137: try {
0138: sc.setHeader("Content-Length", Integer.toString(msg
0139: .length()));
0140: os = sc.openContentOutputStream();
0141: } catch (Throwable e) {
0142: fail("STREAM " + e + " was caused");
0143: }
0144:
0145: assertEquals("State is not STREAM_OPEN",
0146: ((SipClientConnectionImpl) sc).getState(),
0147: SipClientConnectionImpl.STREAM_OPEN);
0148:
0149: // Check the STREAM_OPEN state
0150: checkStreamOpenState(sc, false);
0151:
0152: // write a message
0153: try {
0154: os.write(msg.getBytes());
0155: } catch (Throwable e) {
0156: fail("write a message " + e + " was caused");
0157: }
0158:
0159: // Move to PROCEEDING state
0160: try {
0161: os.close(); // close stream and send the message
0162: } catch (Throwable e) {
0163: fail("PROCEEDING " + e + " was caused");
0164: }
0165:
0166: assertEquals("State is not PROCEEDING",
0167: ((SipClientConnectionImpl) sc).getState(),
0168: SipClientConnectionImpl.PROCEEDING);
0169:
0170: // check PROCEEDING state before and after provisional response
0171: checkComplexProceedingState(sc, method);
0172:
0173: // Move to COMPLETED state
0174: sendResponse(sc, Response.OK);
0175:
0176: assertEquals("State is not COMPLETED",
0177: ((SipClientConnectionImpl) sc).getState(),
0178: SipClientConnectionImpl.COMPLETED);
0179:
0180: // Check the COMPLETED state
0181: if (!method.equals("INVITE")) {
0182: // Transaction instanse must be in completed state
0183: // (RFC 3261, 17.1.4)
0184: checkCompletedState(sc, isDialogCreated,
0185: Transaction.TERMINATED_STATE,
0186: Transaction.COMPLETED_STATE);
0187: } else {
0188: // Transaction instanse must be in terminated state
0189: // (RFC 3261, 17.1.1.2)
0190: checkCompletedState(sc, true, Transaction.TERMINATED_STATE,
0191: Transaction.TERMINATED_STATE);
0192: // move to INITIALIZED state by init ACK
0193: Request request = null;
0194: CallIdHeader callIdHeader = null;
0195: FromHeader fromHeader = null;
0196: URI requestURI = null;
0197: ToHeader toHeader = null;
0198: try {
0199:
0200: /**
0201: * RFC 3261, 17.1.1.3:
0202: * The ACK request constructed by the client transaction
0203: * MUST contain values for the Call-ID, From,
0204: * and Request-URI that are equal to the values of
0205: * those header fields in the request
0206: */
0207: // save previous request data
0208: request = ((SipClientConnectionImpl) sc).getRequest();
0209: callIdHeader = request.getCallId();
0210: fromHeader = request.getFromHeader();
0211: requestURI = request.getRequestURI();
0212: sc.initAck();
0213: // get the ACK request
0214: request = ((SipClientConnectionImpl) sc).getRequest();
0215: toHeader = request.getTo();
0216: assertTrue("ACK: callId is wrong", callIdHeader
0217: .toString().equals(
0218: request.getCallId().toString()));
0219: assertTrue("ACK: From header is wrong", fromHeader
0220: .toString().equals(
0221: request.getFromHeader().toString()));
0222: assertTrue("ACK: requestURI is wrong", requestURI
0223: .toString().equals(
0224: request.getRequestURI().toString()));
0225: /**
0226: * RFC 3261, 17.1.1.3:
0227: * The To header field in the ACK MUST equal
0228: * the To header field in the response being acknowledged.
0229: */
0230: Address toAddress = StackConnector.addressFactory
0231: .createAddress(corrURI);
0232: ToHeader toHeader1 = StackConnector.headerFactory
0233: .createToHeader(toAddress, null);
0234: toHeader1.setTag(toTag);
0235: assertTrue("ACK: To header is wrong", toHeader
0236: .toString().equals(toHeader1.toString()));
0237: } catch (Throwable e) {
0238: fail("Exception was caused");
0239: }
0240:
0241: assertEquals("State is not INITIALIZED",
0242: ((SipClientConnectionImpl) sc).getState(),
0243: SipClientConnectionImpl.INITIALIZED);
0244:
0245: // return to COMPLETED state by sending ACK
0246: try {
0247: sc.send();
0248: } catch (Throwable e) {
0249: fail("Exception was caused");
0250: }
0251:
0252: }
0253:
0254: assertEquals("State is not COMPLETED",
0255: ((SipClientConnectionImpl) sc).getState(),
0256: SipClientConnectionImpl.COMPLETED);
0257:
0258: // close connection
0259: try {
0260: sc.close();
0261: } catch (Throwable e) {
0262: fail("" + e + " was caused");
0263: }
0264:
0265: assertEquals("State is not TERMINATED",
0266: ((SipClientConnectionImpl) sc).getState(),
0267: SipClientConnectionImpl.TERMINATED);
0268:
0269: }
0270:
0271: /**
0272: * Check the state CREATED of SipClientConnection object.
0273: *
0274: * @param sc SipClientConnection object in CREATED state
0275: */
0276: private void checkCreatedState(SipClientConnection sc) {
0277: // Try to call methods other that initRequest and setListener
0278:
0279: // enableRefresh:
0280: disMethod_enableRefresh(sc);
0281:
0282: // initAck:
0283: disMethod_initAck(sc);
0284:
0285: // initCancel:
0286: disMethod_initCancel(sc);
0287:
0288: // receive:
0289: disMethod_receive(sc);
0290:
0291: // setCredentials:
0292: disMethod_setCredentials(sc);
0293:
0294: // setRequestURI:
0295: disMethod_setRequestURI(sc);
0296:
0297: // addHeader:
0298: disMethod_addHeader(sc);
0299:
0300: // openContentInputStream:
0301: disMethod_openContentInputStream(sc);
0302:
0303: // openContentOutputStream:
0304: disMethod_openContentOutputStream(sc);
0305:
0306: // removeHeader:
0307: disMethod_removeHeader(sc);
0308:
0309: // send:
0310: disMethod_send(sc);
0311:
0312: // setHeader:
0313: disMethod_setHeader(sc);
0314:
0315: // Dialog must be unaccessable
0316: assertTrue("Dialog object is visible on CREATED state", sc
0317: .getDialog() == null);
0318:
0319: // Client transaction must be null
0320: assertTrue(
0321: "Client transaction is not null on CREATED state",
0322: ((SipClientConnectionImpl) sc).getClientTransaction() == null);
0323: }
0324:
0325: /**
0326: * Check the state INITIALIZED of SipClientConnection object.
0327: *
0328: * @param sc SipClientConnection object in INITIALIZED state
0329: * @param prevState the previous state
0330: */
0331: private void checkInitializedState(SipClientConnection sc,
0332: int prevState) {
0333:
0334: // initAck:
0335: disMethod_initAck(sc);
0336:
0337: // initCancel:
0338: disMethod_initCancel(sc);
0339:
0340: // initRequest:
0341: disMethod_initRequest(sc);
0342:
0343: // receive:
0344: disMethod_receive(sc);
0345:
0346: // openContentInputStream:
0347: disMethod_openContentInputStream(sc);
0348:
0349: // Check dialog and transaction
0350: if (prevState == SipClientConnectionImpl.CREATED) {
0351: // Dialog must be unaccessable
0352: assertTrue("Dialog object is visible on INITIALIZED state",
0353: sc.getDialog() == null);
0354:
0355: // Client transaction must be null
0356: assertTrue(
0357: "Client transaction is not null on INITIALIZED state",
0358: ((SipClientConnectionImpl) sc)
0359: .getClientTransaction() == null);
0360: }
0361: }
0362:
0363: /**
0364: * Check the state STREAM_OPEN of SipClientConnection object.
0365: *
0366: * @param sc SipClientConnection object in STREAM_OPEN state
0367: * @param isDialog true when dialog is created, else - false
0368: */
0369: private void checkStreamOpenState(SipClientConnection sc,
0370: boolean isDialog) {
0371:
0372: // enableRefresh:
0373: disMethod_enableRefresh(sc);
0374:
0375: // initAck:
0376: disMethod_initAck(sc);
0377:
0378: // initCancel:
0379: disMethod_initCancel(sc);
0380:
0381: // initRequest:
0382: disMethod_initRequest(sc);
0383:
0384: // receive:
0385: disMethod_receive(sc);
0386:
0387: // setCredentials:
0388: disMethod_setCredentials(sc);
0389:
0390: // setRequestURI:
0391: disMethod_setRequestURI(sc);
0392:
0393: // addHeader:
0394: disMethod_addHeader(sc);
0395:
0396: // openContentInputStream:
0397: disMethod_openContentInputStream(sc);
0398:
0399: // openContentOutputStream:
0400: disMethod_openContentOutputStream(sc);
0401:
0402: // removeHeader:
0403: disMethod_removeHeader(sc);
0404:
0405: // setHeader:
0406: disMethod_setHeader(sc);
0407:
0408: // Check dialog and transaction
0409: if (!isDialog) {
0410: // Dialog must be unaccessable
0411: assertTrue("Dialog object is visible on STREAM_OPEN state",
0412: sc.getDialog() == null);
0413:
0414: // Client transaction must be null
0415: assertTrue(
0416: "Client transaction is not null on STREAM_OPEN state",
0417: ((SipClientConnectionImpl) sc)
0418: .getClientTransaction() == null);
0419: }
0420:
0421: }
0422:
0423: /**
0424: * Complex check the PROCEEDing state (before and after
0425: * provisional response.
0426: *
0427: * @param sc SipClientConnection object in CREATED state
0428: * @param method the method of request
0429: */
0430: private void checkComplexProceedingState(SipClientConnection sc,
0431: String method) {
0432:
0433: // Check the PROCEEDING state
0434: // no provisional request - dialog must be unvisible
0435: // (RFC 3261, 12.1)
0436: if (method.equals("INVITE")) {
0437: // INVITE: transaction instanse must be in calling state
0438: // (RFC 3261, 17.1.1.2)
0439: checkProceedingState(sc, false, 0,
0440: Transaction.CALLING_STATE);
0441: } else {
0442: // non-INVITE: transaction instanse must be in trying state
0443: // (RFC 3261, 17.1.4)
0444: checkProceedingState(sc, false, 0, Transaction.TRYING_STATE);
0445: }
0446: // Receiving provisional response
0447: sendResponse(sc, Response.TRYING);
0448:
0449: assertEquals("State is not PROCEEDING",
0450: ((SipClientConnectionImpl) sc).getState(),
0451: SipClientConnectionImpl.PROCEEDING);
0452:
0453: // Check the PROCEEDING state
0454: // provisional request - dialog must be in early state
0455: // (JSR180)
0456: // Transaction instanse must be in proceeding state
0457: // (RFC 3261, 17.1.1.2, 17.1.4)
0458: checkProceedingState(sc, isDialogCreated, SipDialog.EARLY,
0459: Transaction.PROCEEDING_STATE);
0460:
0461: // Receiving any provisional response when transaction is in
0462: // proceedin state doesn't change its state (both INVITE
0463: // and non-INVITE, RFC3261, 17.1.1.2, 17.1.4)
0464: sendResponse(sc, Response.TRYING);
0465: assertEquals("State is not PROCEEDING",
0466: ((SipClientConnectionImpl) sc).getState(),
0467: SipClientConnectionImpl.PROCEEDING);
0468: checkProceedingState(sc, isDialogCreated, SipDialog.EARLY,
0469: Transaction.PROCEEDING_STATE);
0470:
0471: // Move to UNAUTHORIZED state
0472: sendResponse(sc, Response.UNAUTHORIZED);
0473:
0474: assertEquals("State is not UNAUTHORIZED",
0475: ((SipClientConnectionImpl) sc).getState(),
0476: SipClientConnectionImpl.UNAUTHORIZED);
0477:
0478: // Check the UNAUTHORIZED state
0479: checkUnautorizedState(sc);
0480:
0481: // Move to back to PROCEEDING state
0482: try {
0483: sc.setCredentials("name", "pass", "realm"); // re-send request
0484: } catch (Throwable e) {
0485: fail("" + e + " was caused");
0486: }
0487:
0488: assertEquals("State is not PROCEEDING",
0489: ((SipClientConnectionImpl) sc).getState(),
0490: SipClientConnectionImpl.PROCEEDING);
0491:
0492: }
0493:
0494: /**
0495: * Check the state PROCEEDING of SipClientConnection object.
0496: *
0497: * @param sc SipClientConnection object in CREATED state
0498: * @param isDialogVisible true when dialog is visible, else - false
0499: * @param dialogState required state of Dialog object
0500: * @param transState required state of Transaction object
0501: */
0502: private void checkProceedingState(SipClientConnection sc,
0503: boolean isDialogVisible, int dialogState, int transState) {
0504:
0505: // enableRefresh:
0506: disMethod_enableRefresh(sc);
0507:
0508: // initAck:
0509: disMethod_initAck(sc);
0510:
0511: // initRequest:
0512: disMethod_initRequest(sc);
0513:
0514: // setCredentials:
0515: disMethod_setCredentials(sc);
0516:
0517: // setRequestURI:
0518: disMethod_setRequestURI(sc);
0519:
0520: // addHeader:
0521: disMethod_addHeader(sc);
0522:
0523: // openContentOutputStream:
0524: disMethod_openContentOutputStream(sc);
0525:
0526: // removeHeader:
0527: disMethod_removeHeader(sc);
0528:
0529: // send:
0530: disMethod_send(sc);
0531:
0532: // setHeader:
0533: disMethod_setHeader(sc);
0534:
0535: // Check dialog state
0536: if (!isDialogVisible) { // dialog must be null
0537: assertTrue("Dialog object is visible on PROCEEDING state",
0538: sc.getDialog() == null);
0539: } else { // check state
0540: SipDialog dialog = sc.getDialog();
0541: assertTrue(
0542: "Dialog object is not visible on PROCEEDING state",
0543: dialog != null);
0544: assertTrue("Proceeding - Dialog has wrong state",
0545: (int) dialog.getState() == dialogState);
0546: }
0547:
0548: // Check client transaction
0549: ClientTransaction clientTransaction = ((SipClientConnectionImpl) sc)
0550: .getClientTransaction();
0551: assertTrue("Client transaction is null on PROCEEDING state",
0552: clientTransaction != null);
0553: assertTrue("Client transaction has wrong state",
0554: clientTransaction.getState() == transState);
0555: }
0556:
0557: /**
0558: * Check the state COMPLETED of SipClientConnection object.
0559: *
0560: * @param sc SipClientConnection object in COMPLETED state
0561: * @param isDialogVisible true when dialog is visible, else - false
0562: * @param transactionStateReliable client tramsaction state
0563: * for reliable protocols
0564: * @param transactionStateUnReliable client tramsaction state
0565: * for unreliable protocols
0566: */
0567: private void checkCompletedState(SipClientConnection sc,
0568: boolean isDialogVisible, int transactionStateReliable,
0569: int transactionStateUnReliable) {
0570:
0571: // enableRefresh:
0572: disMethod_enableRefresh(sc);
0573:
0574: // initCancel:
0575: disMethod_initCancel(sc);
0576:
0577: // initRequest:
0578: disMethod_initRequest(sc);
0579:
0580: // setCredentials:
0581: disMethod_setCredentials(sc);
0582:
0583: // setRequestURI:
0584: disMethod_setRequestURI(sc);
0585:
0586: // addHeader:
0587: disMethod_addHeader(sc);
0588:
0589: // openContentOutputStream:
0590: disMethod_openContentOutputStream(sc);
0591:
0592: // removeHeader:
0593: disMethod_removeHeader(sc);
0594:
0595: // send:
0596: disMethod_send(sc);
0597:
0598: // setHeader:
0599: disMethod_setHeader(sc);
0600:
0601: if (!isDialogVisible) { // dialog must be null
0602: assertTrue("Dialog object is visible on COMPLETED state",
0603: sc.getDialog() == null);
0604: } else { // check state
0605: // Dialog must be visible and be in confirmed state
0606: SipDialog dialog = sc.getDialog();
0607: assertTrue(
0608: "Dialog object is not visible on COMPLETED state",
0609: dialog != null);
0610: assertTrue("Completed - Dialog has wrong state",
0611: (int) dialog.getState() == SipDialog.CONFIRMED);
0612: }
0613:
0614: // Check client transaction
0615: ClientTransaction clientTransaction = ((SipClientConnectionImpl) sc)
0616: .getClientTransaction();
0617: assertTrue("Client transaction is null on COMPLETED state",
0618: clientTransaction != null);
0619: assertTrue(
0620: "COMPLETED: Client transaction has wrong state",
0621: clientTransaction.isReliable() ? clientTransaction
0622: .getState() == transactionStateReliable
0623: : clientTransaction.getState() == transactionStateUnReliable);
0624: }
0625:
0626: /**
0627: * Check the state UNAUTHORIZED of SipClientConnection object.
0628: *
0629: * @param sc SipClientConnection object in UNAUTHORIZED state
0630: */
0631: private void checkUnautorizedState(SipClientConnection sc) {
0632: // Try to call methods other that initRequest and setListener
0633:
0634: // enableRefresh:
0635: disMethod_enableRefresh(sc);
0636:
0637: // initAck:
0638: disMethod_initAck(sc);
0639:
0640: // initCancel:
0641: disMethod_initCancel(sc);
0642:
0643: // initRequest:
0644: disMethod_initRequest(sc);
0645:
0646: // receive:
0647: disMethod_receive(sc);
0648:
0649: // setRequestURI:
0650: disMethod_setRequestURI(sc);
0651:
0652: // addHeader:
0653: disMethod_addHeader(sc);
0654:
0655: // openContentInputStream:
0656: disMethod_openContentInputStream(sc);
0657:
0658: // openContentOutputStream:
0659: disMethod_openContentOutputStream(sc);
0660:
0661: // removeHeader:
0662: disMethod_removeHeader(sc);
0663:
0664: // send:
0665: disMethod_send(sc);
0666:
0667: // setHeader:
0668: disMethod_setHeader(sc);
0669:
0670: // Dialog must be unvisible (terminated state)
0671: assertTrue(
0672: "Dialog object must be unvisible on UNAUTHORIZED state",
0673: sc.getDialog() == null);
0674: }
0675:
0676: /**
0677: * Disabled method enableRefresh.
0678: *
0679: * @param sc input SipClientConnection object
0680: */
0681: private void disMethod_enableRefresh(SipClientConnection sc) {
0682: StubSipRefreshListener refresh = new StubSipRefreshListener();
0683: try {
0684: sc.enableRefresh(refresh);
0685: fail("No SipException was caused");
0686: } catch (SipException e) {
0687: assertEquals("Wrong error code", e.getErrorCode(),
0688: SipException.INVALID_STATE);
0689: } catch (Throwable e) {
0690: fail("Wrong exception was caused");
0691: }
0692: }
0693:
0694: /**
0695: * Disabled method initAck.
0696: *
0697: * @param sc input SipClientConnection object
0698: */
0699: private void disMethod_initAck(SipClientConnection sc) {
0700: try {
0701: sc.initAck();
0702: fail("No SipException was caused");
0703: } catch (SipException e) {
0704: assertEquals("Wrong error code", e.getErrorCode(),
0705: SipException.INVALID_STATE);
0706: } catch (Throwable e) {
0707: fail("Wrong exception was caused");
0708: }
0709: }
0710:
0711: /**
0712: * Disabled method initCancel.
0713: *
0714: * @param sc input SipClientConnection object
0715: */
0716: private void disMethod_initCancel(SipClientConnection sc) {
0717: try {
0718: sc.initCancel();
0719: fail("No SipException was caused");
0720: } catch (SipException e) {
0721: assertEquals("Wrong error code", e.getErrorCode(),
0722: SipException.INVALID_STATE);
0723: } catch (Throwable e) {
0724: fail("Wrong exception was caused");
0725: }
0726: }
0727:
0728: /**
0729: * Disabled method receive.
0730: *
0731: * @param sc input SipClientConnection object
0732: */
0733: private void disMethod_receive(SipClientConnection sc) {
0734: try {
0735: sc.receive(10000);
0736: fail("No SipException was caused");
0737: } catch (SipException e) {
0738: assertEquals("Wrong error code", e.getErrorCode(),
0739: SipException.INVALID_STATE);
0740: } catch (Throwable e) {
0741: fail("Wrong exception was caused");
0742: }
0743: }
0744:
0745: /**
0746: * Disabled method setCredentials.
0747: *
0748: * @param sc input SipClientConnection object
0749: */
0750: private void disMethod_setCredentials(SipClientConnection sc) {
0751: try {
0752: sc.setCredentials("name", "password", "realm");
0753: fail("No SipException was caused");
0754: } catch (SipException e) {
0755: assertEquals("Wrong error code", e.getErrorCode(),
0756: SipException.INVALID_STATE);
0757: } catch (Throwable e) {
0758: fail("Wrong exception was caused");
0759: }
0760: }
0761:
0762: /**
0763: * Disabled method setRequestURI.
0764: *
0765: * @param sc input SipClientConnection object
0766: */
0767: private void disMethod_setRequestURI(SipClientConnection sc) {
0768: try {
0769: sc.setRequestURI("sip:sippy.tester@localhost:5060");
0770: fail("No SipException was caused");
0771: } catch (SipException e) {
0772: assertEquals("Wrong error code", e.getErrorCode(),
0773: SipException.INVALID_STATE);
0774: } catch (Throwable e) {
0775: fail("Wrong exception was caused");
0776: }
0777: }
0778:
0779: /**
0780: * Disabled method addHeader.
0781: *
0782: * @param sc input SipClientConnection object
0783: */
0784: private void disMethod_addHeader(SipClientConnection sc) {
0785: try {
0786: sc.addHeader("name", "value");
0787: fail("No SipException was caused");
0788: } catch (SipException e) {
0789: assertEquals("Wrong error code", e.getErrorCode(),
0790: SipException.INVALID_STATE);
0791: } catch (Throwable e) {
0792: fail("Wrong exception was caused");
0793: }
0794: }
0795:
0796: /**
0797: * Disabled method openContentInputStream.
0798: *
0799: * @param sc input SipClientConnection object
0800: */
0801: private void disMethod_openContentInputStream(SipClientConnection sc) {
0802: try {
0803: sc.openContentInputStream();
0804: fail("No SipException was caused");
0805: } catch (SipException e) {
0806: assertEquals("Wrong error code", e.getErrorCode(),
0807: SipException.INVALID_STATE);
0808: } catch (Throwable e) {
0809: fail("Wrong exception was caused");
0810: }
0811: }
0812:
0813: /**
0814: * Disabled method openContentOutputStream.
0815: *
0816: * @param sc input SipClientConnection object
0817: */
0818: private void disMethod_openContentOutputStream(
0819: SipClientConnection sc) {
0820: try {
0821: sc.openContentOutputStream();
0822: fail("No SipException was caused");
0823: } catch (SipException e) {
0824: assertEquals("Wrong error code", e.getErrorCode(),
0825: SipException.INVALID_STATE);
0826: } catch (Throwable e) {
0827: fail("Wrong exception was caused");
0828: }
0829: }
0830:
0831: /**
0832: * Disabled method removeHeader.
0833: *
0834: * @param sc input SipClientConnection object
0835: */
0836: private void disMethod_removeHeader(SipClientConnection sc) {
0837: try {
0838: sc.removeHeader("Header");
0839: fail("No SipException was caused");
0840: } catch (SipException e) {
0841: assertEquals("Wrong error code", e.getErrorCode(),
0842: SipException.INVALID_STATE);
0843: } catch (Throwable e) {
0844: fail("Wrong exception was caused");
0845: }
0846: }
0847:
0848: /**
0849: * Disabled method send.
0850: *
0851: * @param sc input SipClientConnection object
0852: */
0853: private void disMethod_send(SipClientConnection sc) {
0854: try {
0855: sc.send();
0856: fail("No SipException was caused");
0857: } catch (SipException e) {
0858: assertEquals("Wrong error code", e.getErrorCode(),
0859: SipException.INVALID_STATE);
0860: } catch (Throwable e) {
0861: fail("Wrong exception was caused");
0862: }
0863: }
0864:
0865: /**
0866: * Disabled method setHeader.
0867: *
0868: * @param sc input SipClientConnection object
0869: */
0870: private void disMethod_setHeader(SipClientConnection sc) {
0871: try {
0872: sc.setHeader("Header", "value");
0873: fail("No SipException was caused");
0874: } catch (SipException e) {
0875: assertEquals("Wrong error code", e.getErrorCode(),
0876: SipException.INVALID_STATE);
0877: } catch (Throwable e) {
0878: fail("Wrong exception was caused");
0879: }
0880: }
0881:
0882: /**
0883: * Disabled method initRequest.
0884: *
0885: * @param sc input SipClientConnection object
0886: */
0887: private void disMethod_initRequest(SipClientConnection sc) {
0888: try {
0889: sc.initRequest("MESSAGE", null);
0890: fail("No SipException was caused");
0891: } catch (SipException e) {
0892: assertEquals("Wrong error code", e.getErrorCode(),
0893: SipException.INVALID_STATE);
0894: } catch (Throwable e) {
0895: fail("Wrong exception was caused");
0896: }
0897: }
0898:
0899: /**
0900: * Send a response with given code.
0901: *
0902: * @param sc SipClientConnection object
0903: * @param respCode response code
0904: */
0905: private void sendResponse(SipClientConnection sc, int respCode) {
0906:
0907: // Send a response
0908: Request request = null;
0909: Response response = null;
0910:
0911: try {
0912: request = ((SipClientConnectionImpl) sc).getRequest();
0913: // create a response for request
0914: response = request.createResponse(respCode);
0915:
0916: // When response is 200 OK change "To" header (RFC 3261, 17.1.1.3)
0917: if (respCode == Response.OK) {
0918: Address toAddress = StackConnector.addressFactory
0919: .createAddress(corrURI);
0920: ToHeader toHeader = null;
0921: try {
0922: toHeader = StackConnector.headerFactory
0923: .createToHeader(toAddress, null);
0924: } catch (ParseException ex) {
0925: fail("Problem during the creation"
0926: + " of the ToHeader");
0927: }
0928: response.setTo(toHeader);
0929: }
0930:
0931: // When response is 2xx and request is refreshable,
0932: // response must contain "expires" header (RFC 3265, 7.1)
0933: if ((respCode / 100 == 2) && (refreshID != 0)) {
0934: ExpiresHeader expHeader = new ExpiresHeader();
0935: expHeader.setExpires(1000);
0936: response.setHeader(expHeader);
0937: }
0938:
0939: // RFC 3261 8.2.6.2 Response must have To tag
0940: ToHeader toHeader = response.getTo();
0941: if (!toHeader.hasTag()) {
0942: // To header has no tag - add server tag
0943: toHeader.setTag(toTag);
0944: response.setTo(toHeader);
0945: }
0946:
0947: if (respCode == Response.UNAUTHORIZED) {
0948: WWWAuthenticateHeader authHeader = new WWWAuthenticateHeader();
0949: authHeader.setParameter(AuthenticationHeader.REALM,
0950: "realm");
0951: response.setHeader(authHeader);
0952: }
0953:
0954: // add Contact header when request is not "MESSAGE"
0955: if ((respCode == Response.OK)
0956: && (!request.getMethod().equals("MESSAGE"))) {
0957: SipURI contactURI = StackConnector.addressFactory
0958: .createSipURI("sippy.tester", // name
0959: "localhost:5060");
0960: ContactHeader contactHeader = StackConnector.headerFactory
0961: .createContactHeader(StackConnector.addressFactory
0962: .createAddress(contactURI));
0963: response.addHeader(contactHeader);
0964: }
0965:
0966: ClientTransaction clientTransaction = ((SipClientConnectionImpl) sc)
0967: .getClientTransaction();
0968:
0969: SipStack sipStack = ((SipClientConnectionImpl) sc)
0970: .getSipStack();
0971:
0972: SIPServerResponseInterface ssc = GetSipServerResponse
0973: .newSIPServerResponse(sipStack, response,
0974: clientTransaction.getMessageChannel());
0975:
0976: ssc.processResponse(response, clientTransaction
0977: .getMessageChannel());
0978: // receive a response
0979: assertTrue("Response wasn't received", sc.receive(10000));
0980: } catch (Throwable e) {
0981: fail("sendResponse " + e + " was caused");
0982: }
0983: }
0984:
0985: /**
0986: * Tests execute
0987: *
0988: */
0989: public void runTests() {
0990: String arrProt[] = { "UDP", "TCP" };
0991: String arrMethods[] = { "INVITE", "MESSAGE", "SUBSCRIBE",
0992: "OPTIONS", "REGISTER", "INFO", "PUBLISH" };
0993: for (int i = 0; i < arrMethods.length; i++) {
0994: declare("SipClientConnection - " + arrMethods[i]);
0995: for (int j = 0; j < arrProt.length; j++) {
0996: Test1(arrMethods[i], arrProt[j]);
0997: }
0998: }
0999: }
1000:
1001: }
1002:
1003: /**
1004: * Stub class implements SipRefreshListener
1005: * for testing SipClientConnection.
1006: */
1007: class StubSipRefreshListener implements SipRefreshListener {
1008: /**
1009: * Empty callback function refreshEvent.
1010: *
1011: * @param refreshID ID of refresh action
1012: * @param statusCode status of refresh task
1013: * @param reasonPhrase additional text message
1014: */
1015: public void refreshEvent(int refreshID, int statusCode,
1016: java.lang.String reasonPhrase) {
1017: }
1018: }
|