<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE scenario SYSTEM "sipp.dtd">
<scenario name="INVITE-test-session-refresher-uac">

  <!--

    Test UAS when we (UAC) do the refreshing for bug ASTERISK-22551,
    Walter Doekes, May 2014

    Asterisk dialplan:

      exten => wait_a_while,1,Ringing()
       ;; time is now 00:00
       same => n,Wait(30)
       ;; time is now 00:30
       same => n,Answer()
       ;; if time is 00:30 + 90/2, it's refresh time: 01:15 
       ;; however, asterisk thinks it's disconnect time
       ;; at: 00:00 + 90 - min(90/3, 32) = 00:58
       ;; wait a long while.. let the sipp script does its testing
       ;; within these 300 seconds we should send the BYE, but at
       ;; the right time
       same => n,Wait(300)
       same => n,Hangup()

  -->

  <send retrans="500" start_txn="invite">
    <![CDATA[

      INVITE sip:[tel]@[remote_ip]:[remote_port] SIP/2.0
      Via: SIP/2.0/[transport] [local_ip]:[local_port];branch=[branch]
      From: sip:[service]@[local_ip]:[local_port];tag=[pid]SIPpTag00[call_number]
      To: sip:[tel]@[remote_ip]:[remote_port]
      Contact: sip:[service]@[local_ip]:[local_port]
      Call-ID: [call_id]
      CSeq: [cseq] INVITE
      Max-Forwards: 70
      Content-Type: application/sdp
      Content-Length: [len]
      Require: timer
      Session-Expires: 90;refresher=uac
      Supported: timer

      v=0
      o=user1 53655765 2353687637 IN IP[local_ip_type] [local_ip]
      s=-
      c=IN IP[media_ip_type] [media_ip]
      t=0 0
      m=audio [media_port] RTP/AVP 8 0
      a=rtpmap:8 PCMA/8000
      a=rtpmap:0 PCMU/8000

    ]]>
  </send>

  <recv response="100" optional="true" response_txn="invite"/>

  <recv response="401" optional="true" next="invite-with-auth" auth="true" rrs="true" response_txn="invite">
    <action>
      <ereg regexp=";branch=[^;]*" search_in="hdr" header="Via" check_it="false" assign_to="1"/>
    </action>
  </recv>

  <recv response="407" auth="true" rrs="true" response_txn="invite">
    <action>
      <ereg regexp=";branch=[^;]*" search_in="hdr" header="Via" check_it="false" assign_to="1"/>
    </action>
  </recv>

  <label id="invite-with-auth"/>

  <!-- ACK is within transaction because of non-2xx. Reuse Via branch.      -->
  <!-- We don't use last_Via: because it would include rport stuff from     -->
  <!-- remote.                                                              -->
  <send ack_txn="invite">
    <![CDATA[

      ACK sip:[tel]@[remote_ip]:[remote_port] SIP/2.0
      Via: SIP/2.0/[transport] [local_ip]:[local_port][$1]
      [last_From:]
      [last_To:]
      [last_Call-ID:]
      CSeq: [cseq] ACK
      Max-Forwards: 70
      Content-Length: 0

    ]]>
  </send>

  <send retrans="500" start_txn="invite">
    <![CDATA[

      INVITE sip:[tel]@[remote_ip]:[remote_port] SIP/2.0
      Via: SIP/2.0/[transport] [local_ip]:[local_port];branch=[branch]
      From: sip:[service]@[local_ip]:[local_port];tag=[pid]SIPpTag00[call_number]
      To: sip:[tel]@[remote_ip]:[remote_port]
      Contact: sip:[service]@[local_ip]:[local_port]
      [authentication]
      Call-ID: [call_id]
      CSeq: [cseq] INVITE
      Max-Forwards: 70
      Content-Type: application/sdp
      Content-Length: [len]
      Require: timer
      Session-Expires: 90;refresher=uac
      Supported: timer

      v=0
      o=user1 53655765 2353687637 IN IP[local_ip_type] [local_ip]
      s=-
      c=IN IP[media_ip_type] [media_ip]
      t=0 0
      m=audio [media_port] RTP/AVP 8 0
      a=rtpmap:8 PCMA/8000
      a=rtpmap:0 PCMU/8000

    ]]>
  </send>

  <recv response="100" response_txn="invite"/>

  <recv response="180" optional="true" response_txn="invite"/>

  <recv response="183" optional="true" response_txn="invite"/>

  <!--

    We expect the UAS to wait for 30 seconds in ringing state here.
    This triggers the bug as explained below.

  -->
  
  <recv response="200" rrs="true" response_txn="invite"/>

  <send ack_txn="invite">
    <![CDATA[

      ACK [next_url] SIP/2.0
      Via: SIP/2.0/[transport] [local_ip]:[local_port];branch=[branch]
      [routes]
      From: sip:[service]@[local_ip]:[local_port];tag=[pid]SIPpTag00[call_number]
      To: sip:[tel]@[remote_ip]:[remote_port][peer_tag_param]
      Contact: sip:[service]@[local_ip]:[local_port]
      Call-ID: [call_id]
      CSeq: [cseq] ACK
      Max-Forwards: 70
      Content-Type: application/sdp
      Content-Length: [len]

      v=0
      o=user1 53655765 2353687637 IN IP[local_ip_type] [local_ip]
      s=-
      c=IN IP[media_ip_type] [media_ip]
      t=0 0
      m=audio [media_port] RTP/AVP 8 0
      a=rtpmap:8 PCMA/8000
      a=rtpmap:0 PCMU/8000
    ]]>
  </send>

  <pause milliseconds="45000"/>

  <!--

    If we're still alive, we're at t75 (30 ringing + 45 pause).

    If bug ASTERISK-22551 is still in effect, Asterisk will have
    counted from the start of the INVITE t0 to t58 and decided
    that is was kill time.

    If it is fixed, we have time until t88 to send the refresh reINVITE.
    Let's do that.

  -->

  <send retrans="500" start_txn="invite">
    <![CDATA[

      INVITE [next_url] SIP/2.0
      Via: SIP/2.0/[transport] [local_ip]:[local_port];branch=[branch]
      [routes]
      From: sip:[service]@[local_ip]:[local_port];tag=[pid]SIPpTag00[call_number]
      To: sip:[tel]@[remote_ip]:[remote_port][peer_tag_param]
      Contact: sip:[service]@[local_ip]:[local_port]
      Call-ID: [call_id]
      CSeq: [cseq] INVITE
      Max-Forwards: 70
      Content-Type: application/sdp
      Content-Length: [len]
      Require: timer
      Session-Expires: 90;refresher=uac
      Supported: timer
      X-Reason: SIP re-invite (Session-Timers)

      v=0
      o=user1 53655765 2353687637 IN IP[local_ip_type] [local_ip]
      s=-
      c=IN IP[media_ip_type] [media_ip]
      t=0 0
      m=audio [media_port] RTP/AVP 8 0
      a=rtpmap:8 PCMA/8000
      a=rtpmap:0 PCMU/8000

    ]]>
  </send>

  <recv response="200" response_txn="invite"/>

  <send ack_txn="invite">
    <![CDATA[

      ACK [next_url] SIP/2.0
      Via: SIP/2.0/[transport] [local_ip]:[local_port];branch=[branch]
      [routes]
      From: sip:[service]@[local_ip]:[local_port];tag=[pid]SIPpTag00[call_number]
      To: sip:[tel]@[remote_ip]:[remote_port][peer_tag_param]
      Contact: sip:[service]@[local_ip]:[local_port]
      Call-ID: [call_id]
      CSeq: [cseq] ACK
      Max-Forwards: 70
      Content-Type: application/sdp
      Content-Length: [len]

      v=0
      o=user1 53655765 2353687637 IN IP[local_ip_type] [local_ip]
      s=-
      c=IN IP[media_ip_type] [media_ip]
      t=0 0
      m=audio [media_port] RTP/AVP 8 0
      a=rtpmap:8 PCMA/8000
      a=rtpmap:0 PCMU/8000
    ]]>
  </send>

  <!--

    For extra testing, wait another 50 seconds. We don't need to send
    another refresh for 58 seconds.

  -->

  <pause milliseconds="50000"/>

  <!--

    In 8 seconds we should be receiving a BYE.

  -->

  <recv request="BYE" timeout="15000"/>

  <send>
    <![CDATA[

      SIP/2.0 200 OK
      [last_Via:]
      [last_Record-Route:]
      [last_From:]
      [last_To:]
      [last_Call-ID:]
      [last_CSeq:]

    ]]>
  </send>

</scenario>
