summaryrefslogtreecommitdiffstats
path: root/callagent/tests/SIPInviteTest.st
blob: 0d1c7d19f3980684cdf8bd35a580bf2b397f2452 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
"
 (C) 2011,2014 by Holger Hans Peter Freyther
 All Rights Reserved

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU Affero General Public License as
 published by the Free Software Foundation, either version 3 of the
 License, or (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU Affero General Public License for more details.

 You should have received a copy of the GNU Affero General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
"

TestCase subclass: SIPInviteTest [
    | sent transport agent dialog |

    <category: 'OsmoSIP-Callagent-Tests'>
    <comment: 'Test incoming SIP INVITES. This should create a
    new dialog/transaction and a SIPCall based on that. The call
    can be accepted or rejected.'>

    createInvite [
        ^(WriteStream on: String new)
            nextPutAll: 'INVITE sip:+99915123456@10.23.43.2 SIP/2.0'; cr; nl;
            nextPutAll: 'Max-Forwards: 19'; cr; nl;
            nextPutAll: 'Via: SIP/2.0/UDP 10.23.43.1:5060;rport;branch=z9hG4bK1675813621'; cr; nl;
            nextPutAll: 'From: "00331234567" <sip:00331234567@10.23.43.1>;tag=807191241'; cr; nl;
            nextPutAll: 'To: <sip:+99915123456@10.23.43.2>'; cr; nl;
            nextPutAll: 'Call-ID: 726660594@10.23.43.1'; cr; nl;
            nextPutAll: 'CSeq: 3 INVITE'; cr; nl;
            nextPutAll: 'User-Agent: BLOAT/4.3.0'; cr; nl;
            nextPutAll: 'Contact: <sip:00331234567@10.23.43.1:5060>'; cr; nl;
            nextPutAll: 'Allow: ACK, INVITE, BYE, CANCEL, REGISTER, REFER, OPTIONS, INFO'; cr; nl;
            nextPutAll: 'Content-Type: application/sdp'; cr; nl;
            nextPutAll: 'Content-Length: 189'; cr; nl;
            cr; nl;
            nextPutAll: 'v=0'; cr; nl;
            nextPutAll: 'o=yate 1401116278 1401116278 IN IP4 10.23.42.1'; cr; nl;
            nextPutAll: 's=SIP Call'; cr; nl;
            nextPutAll: 'c=IN IP4 10.23.42.1'; cr; nl;
            nextPutAll: 't=0 0'; cr; nl;
            nextPutAll: 'm=audio 16576 RTP/AVP 8 101'; cr; nl;
            nextPutAll: 'a=rtpmap:8 PCMA/8000'; cr; nl;
            nextPutAll: 'a=rtpmap:101 telephone-event/8000'; cr; nl;
            cr; nl;
            contents
    ]

    setUp [
        sent := OrderedCollection new.
        transport := SIPTransportMock new
                        onData: [:datagram | sent add: datagram];
                        yourself.
        agent := SIPUserAgent createOn: transport.
        agent
            username: 'st';
            password: 'st'.

        dialog := SIPDialog fromUser: 'sip:st@127.0.0.1' host: '127.0.0.1' port: 5060.
        dialog identity: agent mainIdentity.
    ]

    testRejectCallDefault [
        | msg |

        "Inject the invite"
        transport inject: self createInvite.

        "Check the reject"
        self assert: sent size equals: 1.
        msg := SIPParser parse: sent first data.
        self assert: msg code equals: '603'.
        self assert: msg phrase equals: 'Not Found'.
        self assert: agent dialogs isEmpty.
    ]

    testRejectCall [
        | msg call calls firstTag secondTag |

        calls := 0.

        agent onNewCall: [:invite :dialog |
            calls := calls + 1.
            call := (SIPIncomingCall initWith: invite dialog: dialog on: agent)
                reject; yourself].

        "Inject the invite"
        transport inject: self createInvite.

        "Check the reject"
        self assert: sent size equals: 1.
        msg := SIPParser parse: sent first data.
        self assert: msg code equals: '603'.
        self assert: msg phrase equals: 'Not Found'.
        self assert: agent dialogs size equals: 1.
        self assert: call unregisterDialogIsPending.
        firstTag := (msg parameter: 'To' ifAbsent: []) tag.

        "Do a re-transmit and see what happens.."
        transport inject: self createInvite.
        self assert: call unregisterDialogIsPending.
        self assert: sent size equals: 2.
        msg := SIPParser parse: sent second data.
        secondTag := (msg parameter: 'To' ifAbsent: []) tag.
        self assert: firstTag equals: secondTag.
    ]
]