summaryrefslogtreecommitdiffstats
path: root/callagent/tests/SIPInviteTest.st
blob: 4a770aad31c8d9227fcd47cf81f4d11558733221 (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
"
 (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: 'Shiny remote SDP file'; cr; nl;
            cr; nl;
            contents
    ]

    createAck: aTag [
        ^(WriteStream on: String new)
            nextPutAll: 'ACK sip:127.0.0.1 SIP/2.0'; cr; nl;
            nextPutAll: 'Via: SIP/2.0/UDP 10.23.43.1:5060;rport;branch=z9hG4bK1675813621'; cr; nl;
            nextPutAll: 'CSeq: 3 ACK'; cr; nl;
            nextPutAll: 'Call-ID: 726660594@10.23.43.1'; cr; nl;
            nextPutAll: 'From: "00331234567" <sip:00331234567@10.23.43.1>;tag=807191241'; cr; nl;
            nextPutAll: 'To: <sip:+99915123456@10.23.43.2>;tag='; nextPutAll: aTag; 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.
    ]

    testConnectedCall [
        | msg call tag |

        agent onNewCall: [:invite :dialog | 
            call := (SIPIncomingCall initWith: invite dialog: dialog on: agent)
                trying;
                ringing;
                pickUp: 'a SDP file';
                yourself].

        "Inject the invite"
        transport inject: self createInvite.

        "Check the messages"
        self assert: sent size equals: 3.
        msg := SIPParser parse: sent first data.
        self assert: msg code equals: '100'.
        self assert: msg phrase equals: 'Trying'.
        self assert: agent dialogs size equals: 1.
        self deny: call unregisterDialogIsPending.
        tag := (msg parameter: 'To' ifAbsent: []) tag.

        msg := SIPParser parse: sent second data.
        self assert: msg code equals: '180'.
        self assert: msg phrase equals: 'Ringing'.
        self assert: agent dialogs size equals: 1.
        self deny: call unregisterDialogIsPending.

        msg := SIPParser parse: sent third data.
        self assert: msg code equals: '200'.
        self assert: msg phrase equals: 'OK'.
        self assert: agent dialogs size equals: 1.
        self deny: call unregisterDialogIsPending.
        self assert: (msg parameter: 'Content-Type' ifAbsent: []) equals: 'application/sdp'.
        self assert: (msg parameter: 'Content-Length' ifAbsent: []) equals: '10'.

        "Inject the ACK for the 200"
        self assert: call state equals: call class stateAccepted.
        transport inject: (self createAck: tag).
        self assert: call state equals: call class stateSession.

        self assert: (call remoteSDP startsWith: 'Shiny remote SDP file').

        "Now hangup the call"
        call hangup.
        self assert: call state equals: call class stateHangup.
        msg := SIPParser parse: sent fourth data.
        self assert: msg class equals: SIPByeRequest.
    ]

    testConnectedCallWithRetransmission [
        | msg call tag |

        agent onNewCall: [:invite :dialog | 
            call := (SIPIncomingCall initWith: invite dialog: dialog on: agent)].

        "Inject the invite"
        transport inject: self createInvite.

        "Check the reject"
        self assert: sent size equals: 0.

        "Send a 100 Trying to the other end"
        call trying.
        self assert: sent size equals: 1.
        msg := SIPParser parse: sent first data.
        self assert: msg code equals: '100'.
        self assert: msg phrase equals: 'Trying'.

        "Retransmit the INVITE to forc another trying"
        transport inject: self createInvite.
        self assert: sent size equals: 2.
        msg := SIPParser parse: sent second data.
        self assert: msg code equals: '100'.
        self assert: msg phrase equals: 'Trying'.


        "Now ring and re-transmit"
        call ringing.
        self assert: sent size equals: 3.
        msg := SIPParser parse: sent third data.
        self assert: msg code equals: '180'.
        self assert: msg phrase equals: 'Ringing'.

        transport inject: self createInvite.
        self assert: sent size equals: 4.
        msg := SIPParser parse: (sent at: 4) data.
        self assert: msg code equals: '180'.
        self assert: msg phrase equals: 'Ringing'.


        "Now pick-up..."
        call pickUp: 'file'.
        self assert: sent size equals: 5.
        msg := SIPParser parse: (sent at: 5) data.
        self assert: msg code equals: '200'.
        self assert: msg phrase equals: 'OK'.

        transport inject: self createInvite.
        self assert: sent size equals: 6.
        msg := SIPParser parse: (sent at: 6) data.
        self assert: msg code equals: '200'.
        self assert: msg phrase equals: 'OK'.
    ]
]