// Issue http://code.google.com/p/mobicents/issues/detail?id=1847
  public void testProxyExtraRouteNoRewrite() throws Exception {
    setupPhones(ListeningPoint.UDP);
    String fromName = "unique-location";
    String fromSipAddress = "sip-servlets.com";
    SipURI fromAddress =
        senderProtocolObjects.addressFactory.createSipURI(fromName, fromSipAddress);

    String toSipAddress = "sip-servlets.com";
    String toUser = "******";
    SipURI toAddress = senderProtocolObjects.addressFactory.createSipURI(toUser, toSipAddress);

    sender.sendSipRequest("INVITE", fromAddress, toAddress, null, null, false);
    Header rh =
        senderProtocolObjects.headerFactory.createHeader(
            "Route",
            "sip:extra-route@"
                + System.getProperty("org.mobicents.testsuite.testhostaddr")
                + ":5057;lr");
    LinkedList<Header> hh = new LinkedList<Header>();
    hh.add(rh);
    Thread.sleep(TIMEOUT / 4);
    sender.sendInDialogSipRequest("INVITE", null, null, null, hh, "udp");
    Thread.sleep(TIMEOUT / 2);

    assertTrue(receiver.getInviteRequest().getRequestURI().toString().contains("extra-route"));
  }
  public void testReInvite()
      throws InterruptedException, SipException, ParseException, InvalidArgumentException {
    String fromName = "reinvite";
    String fromSipAddress = "sip-servlets.com";
    SipURI fromAddress =
        senderProtocolObjects.addressFactory.createSipURI(fromName, fromSipAddress);

    String toUser = "******";
    String toSipAddress = "sip-servlets.com";
    SipURI toAddress = senderProtocolObjects.addressFactory.createSipURI(toUser, toSipAddress);

    sender.sendSipRequest("INVITE", fromAddress, toAddress, null, null, false);
    Thread.sleep(TIMEOUT);
    assertTrue(sender.getByeReceived());
  }
  /*
   * Non regression test for Issue 1792
   */
  public void testProxyCancelTCP() throws Exception {
    tomcat.addSipConnector(serverName, sipIpAddress, 5070, ListeningPoint.TCP);
    setupPhones(ListeningPoint.TCP);
    String fromName = "unique-location";
    String fromSipAddress = "sip-servlets.com";
    SipURI fromAddress =
        senderProtocolObjects.addressFactory.createSipURI(fromName, fromSipAddress);

    String toSipAddress = "sip-servlets.com";
    String toUser = "******";
    SipURI toAddress = senderProtocolObjects.addressFactory.createSipURI(toUser, toSipAddress);

    List<Integer> provisionalResponsesToSend = new ArrayList<Integer>();
    provisionalResponsesToSend.add(Response.TRYING);
    provisionalResponsesToSend.add(Response.RINGING);
    receiver.setProvisionalResponsesToSend(provisionalResponsesToSend);

    receiver.setWaitForCancel(true);
    sender.sendSipRequest("INVITE", fromAddress, toAddress, null, null, false);
    Thread.sleep(1000);
    sender.sendCancel();
    Thread.sleep(TIMEOUT);
    assertTrue(receiver.isCancelReceived());
    assertTrue(sender.isCancelOkReceived());
    assertTrue(sender.isRequestTerminatedReceived());
  }
  /**
   * Non Regression test for http://code.google.com/p/mobicents/issues/detail?id=1837 ACK was
   * received by JAIN-SIP but was not routed to application
   */
  public void testCallForwardingCallerReInviteAckRaceInfo() throws Exception {
    sender = new TestSipListener(5080, 5070, senderProtocolObjects, false);
    SipProvider senderProvider = sender.createProvider();

    receiver = new TestSipListener(5090, 5070, receiverProtocolObjects, false);
    receiver.setDisableSequenceNumberValidation(true);
    SipProvider receiverProvider = receiver.createProvider();

    receiverProvider.addSipListener(receiver);
    senderProvider.addSipListener(sender);

    senderProtocolObjects.start();
    receiverProtocolObjects.start();

    String fromName = "forward-pending-sender";
    String fromSipAddress = "sip-servlets.com";
    SipURI fromAddress =
        senderProtocolObjects.addressFactory.createSipURI(fromName, fromSipAddress);

    String toSipAddress = "sip-servlets.com";
    String toUser = "******";
    SipURI toAddress = senderProtocolObjects.addressFactory.createSipURI(toUser, toSipAddress);

    sender.setTimeToWaitBeforeAck(9000);
    sender.sendSipRequest("INVITE", fromAddress, toAddress, null, null, false);
    Thread.sleep(8000);
    receiver.sendInDialogSipRequest("UPDATE", null, null, null, null, null);
    Thread.sleep(TIMEOUT);
    assertTrue(receiver.isAckReceived());
  }
  public void setupPhones(String transport) throws Exception {
    senderProtocolObjects =
        new ProtocolObjects("proxy-sender", "gov.nist", transport, AUTODIALOG, null, null, null);
    receiverProtocolObjects =
        new ProtocolObjects("proxy-receiver", "gov.nist", transport, AUTODIALOG, null, null, null);
    sender = new TestSipListener(5080, 5070, senderProtocolObjects, false);
    sender.setRecordRoutingProxyTesting(true);
    SipProvider senderProvider = sender.createProvider();

    receiver = new TestSipListener(5057, 5070, receiverProtocolObjects, false);
    receiver.setRecordRoutingProxyTesting(true);
    SipProvider receiverProvider = receiver.createProvider();

    receiverProvider.addSipListener(receiver);
    senderProvider.addSipListener(sender);

    senderProtocolObjects.start();
    receiverProtocolObjects.start();
  }
  public void testNoAppDeployed404() throws Exception {
    tomcat.addSipConnector(serverName, sipIpAddress, 5060, listeningPointTransport);
    super.tomcat.startTomcat();

    sender = new TestSipListener(5080, 5060, senderProtocolObjects, true);
    SipProvider senderProvider = sender.createProvider();

    senderProvider.addSipListener(sender);

    senderProtocolObjects.start();

    String fromName = "sender";
    String fromHost = "sip-servlets.com";
    SipURI fromAddress = senderProtocolObjects.addressFactory.createSipURI(fromName, fromHost);

    String toUser = "******";
    String toHost = "" + System.getProperty("org.mobicents.testsuite.testhostaddr") + "";
    SipURI toAddress = senderProtocolObjects.addressFactory.createSipURI(toUser, toHost);

    sender.sendSipRequest("INVITE", fromAddress, toAddress, null, null, true);
    Thread.sleep(TIMEOUT);
    assertEquals(404, sender.getFinalResponseStatus());
  }
  @Override
  protected void setUp() {
    try {
      super.sipIpAddress = "0.0.0.0";
      super.setUp();

      senderProtocolObjects =
          new ProtocolObjects("reinvite", "gov.nist", TRANSPORT, AUTODIALOG, null);

      sender = new TestSipListener(5080, 5070, senderProtocolObjects, false);
      SipProvider senderProvider = sender.createProvider();

      senderProvider.addSipListener(sender);

      senderProtocolObjects.start();
    } catch (Exception ex) {
      ex.printStackTrace();
      fail("unexpected exception ");
    }
  }
  /**
   * Non Regression test for http://code.google.com/p/mobicents/issues/detail?id=1445 "Bad Request
   * Method. CANCEL" during sending CANCEL to re-INVITE
   */
  public void testCallForwardingCallerReInviteCancel() throws Exception {
    sender = new TestSipListener(5080, 5070, senderProtocolObjects, false);
    SipProvider senderProvider = sender.createProvider();

    receiver = new TestSipListener(5090, 5070, receiverProtocolObjects, false);
    SipProvider receiverProvider = receiver.createProvider();

    receiverProvider.addSipListener(receiver);
    senderProvider.addSipListener(sender);

    senderProtocolObjects.start();
    receiverProtocolObjects.start();

    String fromName = "forward-sender";
    String fromSipAddress = "sip-servlets.com";
    SipURI fromAddress =
        senderProtocolObjects.addressFactory.createSipURI(fromName, fromSipAddress);

    String toSipAddress = "sip-servlets.com";
    String toUser = "******";
    SipURI toAddress = senderProtocolObjects.addressFactory.createSipURI(toUser, toSipAddress);

    sender.sendSipRequest("INVITE", fromAddress, toAddress, null, null, false);
    Thread.sleep(TIMEOUT);
    assertTrue(sender.isAckSent());
    receiver.setWaitForCancel(true);
    sender.sendInDialogSipRequest("INVITE", null, null, null, null, null);
    Thread.sleep(500);
    sender.sendCancel();
    Thread.sleep(TIMEOUT);
    assertTrue(sender.isCancelOkReceived());
    assertTrue(sender.isRequestTerminatedReceived());
    assertTrue(receiver.isCancelReceived());
    sender.setCancelOkReceived(false);
    sender.setRequestTerminatedReceived(false);
    receiver.setCancelReceived(false);
    sender.sendInDialogSipRequest("INVITE", null, null, null, null, null);
    Thread.sleep(500);
    sender.sendCancel();
    Thread.sleep(TIMEOUT);
    assertTrue(sender.isCancelOkReceived());
    assertTrue(sender.isRequestTerminatedReceived());
    assertTrue(receiver.isCancelReceived());
  }
  /**
   * Non Regression test for http://code.google.com/p/mobicents/issues/detail?id=2230 BYE is routed
   * to unexpected IP
   */
  public void testCallForwardingCallerSendBye408onReinviteUPDATENewThread() throws Exception {
    sender = new TestSipListener(5080, 5070, senderProtocolObjects, false);
    SipProvider senderProvider = sender.createProvider();

    receiver = new TestSipListener(5090, 5070, receiverProtocolObjects, false);
    SipProvider receiverProvider = receiver.createProvider();

    receiverProvider.addSipListener(receiver);
    senderProvider.addSipListener(sender);

    senderProtocolObjects.start();
    receiverProtocolObjects.start();

    String fromName = "forward-sender-408-new-thread";
    String fromSipAddress = "sip-servlets.com";
    SipURI fromAddress =
        senderProtocolObjects.addressFactory.createSipURI(fromName, fromSipAddress);

    String toSipAddress = "sip-servlets.com";
    String toUser = "******";
    SipURI toAddress = senderProtocolObjects.addressFactory.createSipURI(toUser, toSipAddress);

    sender.sendSipRequest("INVITE", fromAddress, toAddress, null, null, false);
    Thread.sleep(TIMEOUT);
    assertTrue(receiver.isInviteReceived());
    assertTrue(receiver.isAckReceived());
    MaxForwardsHeader maxForwardsHeader =
        (MaxForwardsHeader) receiver.getInviteRequest().getHeader(MaxForwardsHeader.NAME);
    assertNotNull(maxForwardsHeader);
    receiver.setProvisionalResponsesToSend(new ArrayList<Integer>());
    receiver.setFinalResponseToSend(Response.REQUEST_TIMEOUT);
    Header methodHeader = receiverProtocolObjects.headerFactory.createHeader("Method", "UPDATE");
    List<Header> headers = new ArrayList<Header>();
    headers.add(methodHeader);
    sender.sendInDialogSipRequest("INVITE", null, null, null, headers, null);
    Thread.sleep(TIMEOUT);
    assertTrue(receiver.isInviteReceived());
    assertTrue(receiver.isAckReceived());

    receiver.sendInDialogSipRequest("UPDATE", null, null, null, null, null);
    Thread.sleep(TIMEOUT);
    assertTrue(receiver.isInviteReceived());
    assertTrue(receiver.isAckReceived());

    sender.sendInDialogSipRequest("BYE", null, null, null, null, null);
    Thread.sleep(TIMEOUT);
    assertTrue(sender.getOkToByeReceived());

    Iterator<String> allMessagesIterator = sender.getAllMessagesContent().iterator();
    while (allMessagesIterator.hasNext()) {
      String message = (String) allMessagesIterator.next();
      logger.info(message);
    }
    assertTrue(sender.getAllMessagesContent().contains("IllegalStateException"));
  }
  // Issue 2500 http://code.google.com/p/mobicents/issues/detail?id=2500
  // B2buaHelper.createRequest() throws a NullPointerException if the request contains an empty
  // header
  public void testCallForwardingCallerSendReInviteSendBye() throws Exception {
    sender = new TestSipListener(5080, 5070, senderProtocolObjects, false);
    SipProvider senderProvider = sender.createProvider();

    receiver = new TestSipListener(5090, 5070, receiverProtocolObjects, false);
    SipProvider receiverProvider = receiver.createProvider();

    receiverProvider.addSipListener(receiver);
    senderProvider.addSipListener(sender);

    senderProtocolObjects.start();
    receiverProtocolObjects.start();

    String fromName = "forward-sender";
    String fromSipAddress = "sip-servlets.com";
    SipURI fromAddress =
        senderProtocolObjects.addressFactory.createSipURI(fromName, fromSipAddress);

    String toSipAddress = "sip-servlets.com";
    String toUser = "******";
    SipURI toAddress = senderProtocolObjects.addressFactory.createSipURI(toUser, toSipAddress);

    sender.sendSipRequest(
        "INVITE",
        fromAddress,
        toAddress,
        null,
        null,
        false,
        new String[] {AllowHeader.NAME},
        new String[] {"INVITE, CANCEL, BYE, ACK, OPTIONS"},
        true);
    Thread.sleep(TIMEOUT);
    assertTrue(receiver.isInviteReceived());
    assertTrue(receiver.isAckReceived());
    MaxForwardsHeader maxForwardsHeader =
        (MaxForwardsHeader) receiver.getInviteRequest().getHeader(MaxForwardsHeader.NAME);
    assertNotNull(maxForwardsHeader);
    // Non Regression test for http://code.google.com/p/mobicents/issues/detail?id=1490
    // B2buaHelper.createRequest does not decrement Max-forwards
    assertEquals(69, maxForwardsHeader.getMaxForwards());

    ListIterator<AllowHeader> allowHeaderIt =
        receiver.getInviteRequest().getHeaders(AllowHeader.NAME);
    int i = 0;
    while (allowHeaderIt.hasNext()) {
      allowHeaderIt.next();
      i++;
    }
    assertEquals(5, i);

    Header methodHeader = receiverProtocolObjects.headerFactory.createHeader("Supported", "");
    // Non Regression for Issue 184 http://code.google.com/p/sipservlets/issues/detail?id=184
    Header allowHeader =
        receiverProtocolObjects.headerFactory.createAllowHeader(
            "INVITE, CANCEL, BYE, ACK, OPTIONS");
    List<Header> headers = new ArrayList<Header>();
    headers.add(methodHeader);
    headers.add(allowHeader);

    sender.sendInDialogSipRequest("INVITE", null, null, null, headers, null);
    receiver.setInviteReceived(false);
    receiver.setAckReceived(false);
    Thread.sleep(TIMEOUT);
    assertTrue(receiver.isInviteReceived());
    assertTrue(receiver.isAckReceived());
    allowHeaderIt = receiver.getInviteRequest().getHeaders(AllowHeader.NAME);
    i = 0;
    while (allowHeaderIt.hasNext()) {
      allowHeaderIt.next();
      i++;
    }
    assertEquals(5, i);
    sender.sendInDialogSipRequest("BYE", null, null, null, null, null);
    Thread.sleep(TIMEOUT);
    assertTrue(receiver.getByeReceived());
    assertTrue(sender.getOkToByeReceived());
  }
  public void testProxyCallerSendBye() throws Exception {
    setupPhones(ListeningPoint.UDP);
    String fromName = "unique-location";
    String fromSipAddress = "sip-servlets.com";
    SipURI fromAddress =
        senderProtocolObjects.addressFactory.createSipURI(fromName, fromSipAddress);

    String toSipAddress = "sip-servlets.com";
    String toUser = "******";
    SipURI toAddress = senderProtocolObjects.addressFactory.createSipURI(toUser, toSipAddress);

    // part of non regression test for Issue
    // http://code.google.com/p/mobicents/issues/detail?id=2359
    // allow to check if ACK retrans keep the same different branch id
    sender.setTimeToWaitBeforeAck(2000);
    sender.sendSipRequest("INVITE", fromAddress, toAddress, null, null, false);
    Thread.sleep(TIMEOUT);
    assertTrue(sender.isAckSent());
    assertTrue(receiver.isAckReceived());
    // non regression test for Issue http://code.google.com/p/mobicents/issues/detail?id=2359
    String inviteBranch =
        ((MessageExt) receiver.getInviteRequest()).getTopmostViaHeader().getBranch();
    String ackBranch = ((MessageExt) receiver.getAckRequest()).getTopmostViaHeader().getBranch();
    assertFalse(inviteBranch.equals(ackBranch));
    receiver.setAckReceived(false);
    sender.setAckSent(false);
    sender.sendInDialogSipRequest("INVITE", null, null, null, null, null);
    Thread.sleep(TIMEOUT);
    assertTrue(sender.isAckSent());
    assertTrue(receiver.isAckReceived());
    receiver.setAckReceived(false);
    receiver.setAckSent(false);
    sender.setAckSent(false);
    sender.setAckReceived(false);
    receiver.sendInDialogSipRequest("INVITE", null, null, null, null, null);
    Thread.sleep(TIMEOUT);
    assertTrue(receiver.isAckSent());
    assertTrue(sender.isAckReceived());
    sender.sendBye();
    Thread.sleep(TIMEOUT);
    assertTrue(receiver.getByeReceived());
    assertTrue(sender.getOkToByeReceived());
    logger.info(
        "RequestsProcessedByMethod "
            + tomcat.getSipService().getSipApplicationDispatcher().getRequestsProcessedByMethod());
    logger.info(
        "RequestsSentByMethod "
            + tomcat.getSipService().getSipApplicationDispatcher().getRequestsSentByMethod());
    logger.info(
        "ResponsesProcessedByStatusCode "
            + tomcat
                .getSipService()
                .getSipApplicationDispatcher()
                .getResponsesProcessedByStatusCode());
    logger.info(
        "ResponsesSentByStatusCode "
            + tomcat.getSipService().getSipApplicationDispatcher().getResponsesSentByStatusCode());
    assertTrue(
        tomcat
                .getSipService()
                .getSipApplicationDispatcher()
                .getRequestsProcessedByMethod(Request.INVITE)
            > 1);
    assertTrue(
        tomcat
                .getSipService()
                .getSipApplicationDispatcher()
                .getRequestsProcessedByMethod(Request.ACK)
            > 1);
    assertTrue(
        tomcat
                .getSipService()
                .getSipApplicationDispatcher()
                .getRequestsProcessedByMethod(Request.BYE)
            > 0);
    assertTrue(
        tomcat.getSipService().getSipApplicationDispatcher().getResponsesSentByStatusCode("2XX")
            > 1);
    assertTrue(
        tomcat.getSipService().getSipApplicationDispatcher().getRequestsSentByMethod(Request.INVITE)
            > 1);
    assertTrue(
        tomcat.getSipService().getSipApplicationDispatcher().getRequestsSentByMethod(Request.ACK)
            > 1);
    assertTrue(
        tomcat.getSipService().getSipApplicationDispatcher().getRequestsSentByMethod(Request.BYE)
            > 0);
    assertTrue(
        tomcat
                .getSipService()
                .getSipApplicationDispatcher()
                .getResponsesProcessedByStatusCode("2XX")
            > 1);
  }
  /*
   * Non regression test for https://code.google.com/p/sipservlets/issues/detail?id=202
   */
  public void testProxyModifySDP() throws Exception {
    setupPhones(ListeningPoint.UDP);
    String fromName = "modify-SDP";
    String fromSipAddress = "sip-servlets.com";
    SipURI fromAddress =
        senderProtocolObjects.addressFactory.createSipURI(fromName, fromSipAddress);

    String toSipAddress = "sip-servlets.com";
    String toUser = "******";
    SipURI toAddress = senderProtocolObjects.addressFactory.createSipURI(toUser, toSipAddress);

    // part of non regression test for Issue
    // http://code.google.com/p/mobicents/issues/detail?id=2359
    // allow to check if ACK retrans keep the same different branch id
    sender.setTimeToWaitBeforeAck(2000);
    sender.sendSipRequest("INVITE", fromAddress, toAddress, null, null, false);
    Thread.sleep(TIMEOUT);
    assertTrue(sender.isAckSent());
    assertTrue(receiver.isAckReceived());
    assertEquals(
        "SDP modified successfully",
        new String(sender.getFinalResponse().getRawContent(), "UTF-8"));
    // non regression test for Issue http://code.google.com/p/mobicents/issues/detail?id=2359
    String inviteBranch =
        ((MessageExt) receiver.getInviteRequest()).getTopmostViaHeader().getBranch();
    String ackBranch = ((MessageExt) receiver.getAckRequest()).getTopmostViaHeader().getBranch();
    assertFalse(inviteBranch.equals(ackBranch));
    receiver.setAckReceived(false);
    sender.setAckSent(false);
    sender.sendInDialogSipRequest("INVITE", null, null, null, null, null);
    Thread.sleep(TIMEOUT);
    assertTrue(sender.isAckSent());
    assertTrue(receiver.isAckReceived());
    receiver.setAckReceived(false);
    receiver.setAckSent(false);
    sender.setAckSent(false);
    sender.setAckReceived(false);
    assertEquals(
        "SDP modified successfully",
        new String(sender.getFinalResponse().getRawContent(), "UTF-8"));
    receiver.sendInDialogSipRequest("INVITE", null, null, null, null, null);
    Thread.sleep(TIMEOUT);
    assertTrue(receiver.isAckSent());
    assertTrue(sender.isAckReceived());
    assertEquals(
        "SDP modified successfully",
        new String(receiver.getFinalResponse().getRawContent(), "UTF-8"));
    sender.sendBye();
    Thread.sleep(TIMEOUT);
    assertTrue(receiver.getByeReceived());
    assertTrue(sender.getOkToByeReceived());
  }
  // Check branchId of the ACK in case the reINVITE has an INFO in between to make sure that
  // the ACK to 200 OK reINVITE is different than the INFO branchId
  public void testProxyReinviteINFOCheckACKBranchId() throws Exception {
    setupPhones(ListeningPoint.UDP);
    String fromName = "unique-location";
    String fromSipAddress = "sip-servlets.com";
    SipURI fromAddress =
        senderProtocolObjects.addressFactory.createSipURI(fromName, fromSipAddress);

    String toSipAddress = "sip-servlets.com";
    String toUser = "******";
    SipURI toAddress = senderProtocolObjects.addressFactory.createSipURI(toUser, toSipAddress);

    // part of non regression test for Issue
    // http://code.google.com/p/mobicents/issues/detail?id=2359
    // allow to check if ACK retrans keep the same different branch id
    sender.sendSipRequest("INVITE", fromAddress, toAddress, null, null, false);
    Thread.sleep(TIMEOUT);
    assertTrue(sender.isAckSent());
    assertTrue(receiver.isAckReceived());
    receiver.setAckReceived(false);
    sender.setAckSent(false);
    sender.sendInDialogSipRequest("INFO", null, null, null, null, null);
    Thread.sleep(500);
    sender.sendInDialogSipRequest("INVITE", null, null, null, null, null);
    Thread.sleep(TIMEOUT);
    assertTrue(sender.isAckSent());
    assertTrue(receiver.isAckReceived());
    receiver.setAckReceived(false);
    receiver.setAckSent(false);
    sender.setAckSent(false);
    sender.setAckReceived(false);
    // non regression test for Issue http://code.google.com/p/mobicents/issues/detail?id=2359
    String inviteBranch =
        ((MessageExt) receiver.getInviteRequest()).getTopmostViaHeader().getBranch();
    String infoBranch = ((MessageExt) receiver.getInfoRequest()).getTopmostViaHeader().getBranch();
    String ackBranch = ((MessageExt) receiver.getAckRequest()).getTopmostViaHeader().getBranch();
    assertFalse(inviteBranch.equals(ackBranch));
    assertFalse(infoBranch.equals(ackBranch));
    sender.sendBye();
    Thread.sleep(TIMEOUT);
    assertTrue(receiver.getByeReceived());
    assertTrue(sender.getOkToByeReceived());
  }
  public void testProxyCalleeSendBye() throws Exception {
    setupPhones(ListeningPoint.UDP);
    String fromName = "unique-location";
    String fromSipAddress = "sip-servlets.com";
    SipURI fromAddress =
        senderProtocolObjects.addressFactory.createSipURI(fromName, fromSipAddress);

    String toSipAddress = "sip-servlets.com";
    String toUser = "******";
    SipURI toAddress = senderProtocolObjects.addressFactory.createSipURI(toUser, toSipAddress);

    sender.sendSipRequest("INVITE", fromAddress, toAddress, null, null, false);
    Thread.sleep(TIMEOUT);
    assertTrue(sender.isAckSent());
    assertTrue(receiver.isAckReceived());
    receiver.setAckReceived(false);
    sender.setAckSent(false);
    sender.sendInDialogSipRequest("INVITE", null, null, null, null, null);
    Thread.sleep(TIMEOUT);
    assertTrue(sender.isAckSent());
    assertTrue(receiver.isAckReceived());
    receiver.setAckReceived(false);
    sender.setAckSent(false);
    receiver.sendInDialogSipRequest("INVITE", null, null, null, null, null);
    Thread.sleep(TIMEOUT);
    assertTrue(receiver.isAckSent());
    assertTrue(sender.isAckReceived());
    receiver.sendBye();
    Thread.sleep(TIMEOUT);
    assertTrue(sender.getByeReceived());
    assertTrue(receiver.getOkToByeReceived());
  }
  // Test for http://code.google.com/p/sipservlets/issues/detail?id=44
  public void testProxyReinviteAckSeenByApp() throws Exception {
    setupPhones(ListeningPoint.UDP);
    String fromName = "unique-location-ack-seen-by-app";
    String fromSipAddress = "sip-servlets.com";
    SipURI fromAddress =
        senderProtocolObjects.addressFactory.createSipURI(fromName, fromSipAddress);

    String toSipAddress = "sip-servlets.com";
    String toUser = "******";
    SipURI toAddress = senderProtocolObjects.addressFactory.createSipURI(toUser, toSipAddress);

    sender.sendSipRequest("INVITE", fromAddress, toAddress, null, null, false);
    Thread.sleep(TIMEOUT);
    assertTrue(sender.isAckSent());
    assertTrue(receiver.isAckReceived());
    // non regression test for Issue http://code.google.com/p/mobicents/issues/detail?id=2359
    String inviteBranch =
        ((MessageExt) receiver.getInviteRequest()).getTopmostViaHeader().getBranch();
    String ackBranch = ((MessageExt) receiver.getAckRequest()).getTopmostViaHeader().getBranch();
    assertFalse(inviteBranch.equals(ackBranch));
    receiver.setAckReceived(false);
    sender.setAckSent(false);
    receiver.setFinalResponseToSend(491);
    sender.sendInDialogSipRequest("INVITE", null, null, null, null, null);
    Thread.sleep(TIMEOUT);
    sender.sendBye();
    Thread.sleep(TIMEOUT);
    assertTrue(receiver.getByeReceived());
    assertTrue(sender.getOkToByeReceived());

    Iterator<String> allMessagesIterator = sender.getAllMessagesContent().iterator();
    while (allMessagesIterator.hasNext()) {
      String message = (String) allMessagesIterator.next();
      logger.info(message);
    }
    assertFalse(sender.getAllMessagesContent().contains("ack-seen-by-app"));
  }
  /*
   * https://code.google.com/p/sipservlets/issues/detail?id=21
   */
  public void testProxyCallerFinalResponseOnSubsequentRequest() throws Exception {
    setupPhones(ListeningPoint.UDP);
    String fromName = "unique-location-final-response-subsequent";
    String fromSipAddress = "sip-servlets.com";
    SipURI fromAddress =
        senderProtocolObjects.addressFactory.createSipURI(fromName, fromSipAddress);

    String toSipAddress = "sip-servlets.com";
    String toUser = "******";
    SipURI toAddress = senderProtocolObjects.addressFactory.createSipURI(toUser, toSipAddress);

    sender.sendSipRequest("INVITE", fromAddress, toAddress, null, null, false);
    Thread.sleep(TIMEOUT);
    assertTrue(sender.isAckSent());
    assertTrue(receiver.isAckReceived());
    // non regression test for Issue http://code.google.com/p/mobicents/issues/detail?id=2359
    String inviteBranch =
        ((MessageExt) receiver.getInviteRequest()).getTopmostViaHeader().getBranch();
    String ackBranch = ((MessageExt) receiver.getAckRequest()).getTopmostViaHeader().getBranch();
    assertFalse(inviteBranch.equals(ackBranch));
    receiver.setAckReceived(false);
    sender.setAckSent(false);
    sender.sendInDialogSipRequest("INVITE", null, null, null, null, null);
    Thread.sleep(TIMEOUT);
    assertEquals(491, sender.getFinalResponseStatus());
    assertFalse(receiver.isAckReceived());
    receiver.setAckReceived(false);
    receiver.setAckSent(false);
    sender.setAckSent(false);
    sender.setAckReceived(false);
    sender.sendBye();
    Thread.sleep(TIMEOUT);
    assertTrue(receiver.getByeReceived());
    assertTrue(sender.getOkToByeReceived());
  }
  public void testCallForwardingCallerSendBye() throws Exception {
    sender = new TestSipListener(5080, 5070, senderProtocolObjects, false);
    SipProvider senderProvider = sender.createProvider();

    receiver = new TestSipListener(5090, 5070, receiverProtocolObjects, false);
    receiver.setSendReinvite(true);
    SipProvider receiverProvider = receiver.createProvider();

    receiverProvider.addSipListener(receiver);
    senderProvider.addSipListener(sender);

    senderProtocolObjects.start();
    receiverProtocolObjects.start();

    String fromName = "forward-sender";
    String fromSipAddress = "sip-servlets.com";
    SipURI fromAddress =
        senderProtocolObjects.addressFactory.createSipURI(fromName, fromSipAddress);

    String toSipAddress = "sip-servlets.com";
    String toUser = "******";
    SipURI toAddress = senderProtocolObjects.addressFactory.createSipURI(toUser, toSipAddress);

    sender.sendSipRequest("INVITE", fromAddress, toAddress, null, null, false);
    Thread.sleep(TIMEOUT);
    assertTrue(sender.isInviteReceived());
    assertTrue(sender.isAckReceived());
    assertNotNull(sender.getInviteRequest().getHeader("ReInvite"));
    MaxForwardsHeader maxForwardsHeader =
        (MaxForwardsHeader) receiver.getInviteRequest().getHeader(MaxForwardsHeader.NAME);
    assertNotNull(maxForwardsHeader);
    // Non Regression test for http://code.google.com/p/mobicents/issues/detail?id=1490
    // B2buaHelper.createRequest does not decrement Max-forwards
    assertEquals(69, maxForwardsHeader.getMaxForwards());
    sender.sendInDialogSipRequest("BYE", null, null, null, null, null);
    Thread.sleep(TIMEOUT);
    assertTrue(receiver.getByeReceived());
    assertTrue(sender.getOkToByeReceived());
    assertEquals(1, sender.bindings); // http://code.google.com/p/mobicents/issues/detail?id=2100
    maxForwardsHeader =
        (MaxForwardsHeader) receiver.getByeRequestReceived().getHeader(MaxForwardsHeader.NAME);
    assertNotNull(maxForwardsHeader);
    // Non Regression test for http://code.google.com/p/mobicents/issues/detail?id=1490
    // B2buaHelper.createRequest does not decrement Max-forwards
    assertEquals(69, maxForwardsHeader.getMaxForwards());
  }
  // https://code.google.com/p/sipservlets/issues/detail?id=238
  public void testProxyINFOLeak() throws Exception {
    setupPhones(ListeningPoint.UDP);
    String fromName = "unique-location";
    String fromSipAddress = "sip-servlets.com";
    SipURI fromAddress =
        senderProtocolObjects.addressFactory.createSipURI(fromName, fromSipAddress);

    String toSipAddress = "sip-servlets.com";
    String toUser = "******";
    SipURI toAddress = senderProtocolObjects.addressFactory.createSipURI(toUser, toSipAddress);

    // part of non regression test for Issue
    // http://code.google.com/p/mobicents/issues/detail?id=2359
    // allow to check if ACK retrans keep the same different branch id
    sender.sendSipRequest("INVITE", fromAddress, toAddress, null, null, false);
    Thread.sleep(TIMEOUT);
    assertTrue(sender.isAckSent());
    assertTrue(receiver.isAckReceived());
    receiver.setAckReceived(false);
    sender.setAckSent(false);
    sender.sendInDialogSipRequest("INFO", null, null, null, null, null);
    Thread.sleep(500);
    sender.sendInDialogSipRequest("INFO", null, null, null, null, null);
    Thread.sleep(500);
    sender.sendInDialogSipRequest("INFO", null, null, null, null, null);
    Thread.sleep(500);
    sender.sendInDialogSipRequest("INFO", null, null, null, null, null);
    Thread.sleep(500);
    sender.sendInDialogSipRequest("INFO", null, null, null, null, null);
    Thread.sleep(TIMEOUT * 2);
    sender.sendBye();
    Thread.sleep(TIMEOUT);
    assertTrue(receiver.getByeReceived());
    assertTrue(sender.getOkToByeReceived());
    assertNotNull(sender.getFinalResponse().getHeader("X-Proxy-Transactions"));
    assertEquals(
        "1", ((HeaderExt) sender.getFinalResponse().getHeader("X-Proxy-Transactions")).getValue());
  }