summaryrefslogtreecommitdiffstats
path: root/callagent/tests/SIPCallAgentTest.st
blob: 6e56fd44e475a9053e3e95285f1698ab031f57a6 (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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
"
 (C) 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: SIPCallAgentTest [
    | transport sent agent |
    <category: 'OsmoSIP-Callagent-Tests'>
    <comment: 'I will test some basic call agent high-level interaction'>

    bye: aBranch callId: aCallId toTag: aTag cseq: aCseq [
        ^(WriteStream on: String new)
            nextPutAll: 'BYE sip:127.0.0.1 SIP/2.0'; cr; nl;
            nextPutAll: 'Via: SIP/2.0/UDP 127.0.0.1:5060;branch='; nextPutAll: aBranch; cr; nl;
            nextPutAll: 'Max-Forwards: 70'; cr; nl;
            nextPutAll: 'CSeq: '; nextPutAll: aCseq asString; nextPutAll: ' BYE'; cr; nl;
            nextPutAll: 'Call-Id: '; nextPutAll: aCallId; cr; nl;
            nextPutAll: 'To: <sip:st@127.0.0.1>;tag='; nextPutAll: aTag; cr; nl;
            nextPutAll: 'From: <sip:st@127.0.0.1>;tag=123'; cr; nl;
            cr; nl;
            contents
    ]

    redirect: aBranch callId: aCallId tag: aTag [
        ^(WriteStream on: String new)
            nextPutAll: 'SIP/2.0 302 Moved Temporarily'; cr; nl;
            nextPutAll: 'Allow: INVITE, ACK'; cr; nl;
            nextPutAll: 'Call-Id: '; nextPutAll: aCallId; cr; nl;
            nextPutAll: 'Contact: sip:+12345678@10.8.254.1'; cr; nl;
            nextPutAll: 'Content-Length: 0'; cr; nl;
            nextPutAll: 'CSeq: 1 INVITE'; cr; nl;
            nextPutAll: 'Via: SIP/2.0/UDP 127.0.0.1:5060;branch='; nextPutAll: aBranch; cr; nl;
            nextPutAll: 'From: <sip:st@127.0.0.1>;tag='; nextPutAll: aTag; cr; nl;
            nextPutAll: 'To: <sip:st@127.0.0.1>'; cr; nl;
            nextPutAll: 'Max-Forwards: 70'; cr; nl;
            nextPutAll: 'Supported: replaces'; cr; nl;
            cr; nl;
            contents
    ]

    trying: aBranch callId: aCallId tag: aTag [
        ^(WriteStream on: String new)
            nextPutAll: 'SIP/2.0 100 Trying'; cr; nl;
            nextPutAll: 'Call-ID: '; nextPutAll: aCallId; cr; nl;
            nextPutAll: 'Content-Length: 0'; cr; nl;
            nextPutAll: 'CSeq: 1 INVITE'; cr; nl;
            nextPutAll: 'From: <sip:st@127.0.0.1>;tag='; nextPutAll: aTag; cr; nl;
            nextPutAll: 'To: <sip:st@127.0.0.1>'; cr; nl;
            nextPutAll: 'Via: SIP/2.0/UDP 127.0.0.1:5060;branch='; nextPutAll: aBranch; cr; nl;
            cr; nl;
            contents
    ]

    ok: aBranch callId: aCallId tag: aTag cseq: aCseq [
        ^(WriteStream on: String new)
            nextPutAll: 'SIP/2.0 200 OK'; cr; nl;
            nextPutAll: 'Call-ID: '; nextPutAll: aCallId; cr; nl;
            nextPutAll: 'Content-Length: 0'; cr; nl;
            nextPutAll: 'CSeq: '; nextPutAll: aCseq asString; nextPutAll: ' INVITE'; cr; nl;
            nextPutAll: 'From: <sip:st@127.0.0.1>;tag='; nextPutAll: aTag; cr; nl;
            nextPutAll: 'To: <sip:st@127.0.0.1>;tag=123'; cr; nl;
            nextPutAll: 'Via: SIP/2.0/UDP 127.0.0.1:5060;branch='; nextPutAll: aBranch; cr; nl;
            cr; nl;
            contents
    ]

    invalidAuthorizationRequired: aBranch callId: aCallId tag: aTag [
        "This is missing WWW-Authenticate so it is kind of invalid"
        ^(WriteStream on: String new)
            nextPutAll: 'SIP/2.0 401 Unauthorized'; cr; nl;
            nextPutAll: 'Via: SIP/2.0/UDP 127.0.0.1:5060;branch='; nextPutAll: aBranch; cr; nl;
            nextPutAll: 'From: <sip:st@127.0.0.1>;tag='; nextPutAll: aTag; cr; nl;
            nextPutAll: 'To: <sip:st@127.0.0.1>'; cr; nl;
            nextPutAll: 'Call-ID: '; nextPutAll: aCallId; cr; nl;
            nextPutAll: 'CSeq: 1 INVITE'; cr; nl;
            nextPutAll: 'Server: YATE/5.1.0'; cr; nl;
            nextPutAll: 'Allow: ACK, INVITE, BYE, CANCEL, REGISTER, REFER'; cr; nl;
            nextPutAll: 'Content-Length: 0'; cr; nl;
            cr; nl;
            contents
    ]

    authorizationRequired: aBranch callId: aCallId tag: aTag [
        ^self authorizationRequired: aBranch callId: aCallId tag: aTag cseq: 1
    ]

    authorizationRequired: aBranch callId: aCallId tag: aTag cseq: aCseq [
        ^(WriteStream on: String new)
            nextPutAll: 'SIP/2.0 401 Unauthorized'; cr; nl;
            nextPutAll: 'Via: SIP/2.0/UDP 127.0.0.1:5060;branch='; nextPutAll: aBranch; cr; nl;
            nextPutAll: 'From: <sip:st@127.0.0.1>;tag='; nextPutAll: aTag; cr; nl;
            nextPutAll: 'To: <sip:st@127.0.0.1>'; cr; nl;
            nextPutAll: 'Call-ID: '; nextPutAll: aCallId; cr; nl;
            nextPutAll: 'CSeq: '; nextPutAll: aCseq asString; nextPutAll: ' INVITE'; cr; nl;
            nextPutAll: 'WWW-Authenticate: Digest realm="Yate", nonce="373ef30b297545cbce99fad09f1409cb.1392124197", stale=TRUE, algorithm=MD5'; cr; nl;
            nextPutAll: 'Server: YATE/5.1.0'; cr; nl;
            nextPutAll: 'Allow: ACK, INVITE, BYE, CANCEL, REGISTER, REFER'; cr; nl;
            nextPutAll: 'Content-Length: 0'; cr; nl;
            cr; nl;
            contents
    ]

    proxyAuthRequired: aBranch callId: aCallId tag: aTag cseq: aCseq [
        ^(WriteStream on: String new)
            nextPutAll: 'SIP/2.0 407 Proxy Authentication Required'; cr; nl;
            nextPutAll: 'Via: SIP/2.0/UDP 127.0.0.1:5060;branch='; nextPutAll: aBranch; cr; nl;
            nextPutAll: 'Proxy-Authenticate: Digest realm="07440491",qop="auth",nonce="06fafd01e9aade68c8400db8c9c146a7bfc5bc28C0A4AC1EE6A8",algorithm=MD5'; cr; nl;
            nextPutAll: 'From: <sip:st@127.0.0.1>;tag='; nextPutAll: aTag; cr; nl;
            nextPutAll: 'To: <sip:st@127.0.0.1>'; cr; nl;
            nextPutAll: 'Call-ID: '; nextPutAll: aCallId; cr; nl;
            nextPutAll: 'CSeq: '; nextPutAll: aCseq asString; nextPutAll: ' INVITE'; cr; nl;
            nextPutAll: 'Server: YATE/5.1.0'; cr; nl;
            nextPutAll: 'Allow: ACK, INVITE, BYE, CANCEL, REGISTER, REFER'; cr; nl;
            nextPutAll: 'Content-Length: 0'; 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'.
    ]

    testSimpleInvite [
        | call msg |
        call := SIPCall fromUser: 'sip:sip@test' host: '127.0.0.1' port: 5060 to: 'sip:127.0.0.1' on: agent.
        call createCall: 'dummy-sdp'.
        self assert: call state equals: SIPCall stateInvite.
        self assert: sent size equals: 1.
        msg := agent parser parse: sent first data.
        self assert: msg class verb equals: SIPInviteRequest verb.
        self assert: (msg parameter: 'CSeq' ifAbsent: [-1]) number equals: 1.

        call cancel.
        self assert: call state equals: SIPCall stateCancel.
    ]

    testInviteWithInvalidAuthorization [
        | call msg branch callId fromTag auth |
 
        call := SIPCall fromUser: 'sip:sip@test' host: '127.0.0.1' port: 5060 to: 'sip:127.0.0.1' on: agent.
        call createCall: 'dummy-sdp'.

        "First assertions for the invite"
        self assert: sent size equals: 1.
        msg := agent parser parse: sent first data.
        self assert: (msg parameter: 'CSeq' ifAbsent: [-1]) number equals: 1.
        self assert: call state equals: SIPCall stateInvite.

        "Now inject an auth requirement message"
        branch := (msg parameter: 'Via' ifAbsent: [nil]) branch.
        callId := (msg parameter: 'Call-ID' ifAbsent: [-1]).
        fromTag := (msg parameter: 'From' ifAbsent: [nil]).
        transport inject: (self invalidAuthorizationRequired: branch callId: callId tag: fromTag tag).
        self assert: call state equals: SIPCall stateFailed.
        self assert: sent size equals: 1.
    ]

    testInviteWithAuthorization [
        | call msg branch callId fromTag auth secondBranch |

        call := SIPCall fromUser: 'sip:sip@test' host: '127.0.0.1' port: 5060 to: 'sip:127.0.0.1' on: agent.
        call createCall: 'dummy-sdp'.

        "First assertions for the invite"
        self assert: sent size equals: 1.
        msg := agent parser parse: sent first data.
        self assert: (msg parameter: 'CSeq' ifAbsent: [-1]) number equals: 1.
        self assert: call state equals: SIPCall stateInvite.

        "Now inject an auth requirement message"
        branch := (msg parameter: 'Via' ifAbsent: [nil]) branch.
        callId := (msg parameter: 'Call-ID' ifAbsent: [-1]).
        fromTag := (msg parameter: 'From' ifAbsent: [nil]).
        transport inject: (self authorizationRequired: branch callId: callId tag: fromTag tag).

        "Verify that a second message has been sent and it contains an auth result"
        self assert: sent size equals: 3.
        msg := agent parser parse: sent second data.
        self assert: msg class verb equals: 'ACK'.

        msg := agent parser parse: sent third data.
        self assert: msg class verb equals: 'INVITE'.
        secondBranch := (msg parameter: 'Via' ifAbsent: [nil]) branch.
        self deny: branch = secondBranch.
        self assert: (msg parameter: 'CSeq' ifAbsent: [-1]) number equals: 2.
        self assert: call state equals: SIPCall stateInvite.

        "Verify the auth part of the message"
        auth := msg parameter: 'Authorization' ifAbsent: [nil].
        self deny: auth isNil.
        self assert: auth username equals: 'st'.
        self assert: auth realm equals: 'Yate'.
        self assert: auth uri equals: 'sip:127.0.0.1'.
        self assert: auth nonce equals: '373ef30b297545cbce99fad09f1409cb.1392124197'.
        self assert: auth response equals: 'bc8dfaa413e897863dbab4c622e4b9b4'.

        call cancel.
        self assert: call state equals: SIPCall stateCancel.
    ]

    testInviteWithDoubleAuth [
        | call msg branch callId fromTag auth |

        call := SIPCall fromUser: 'sip:sip@test' host: '127.0.0.1' port: 5060 to: 'sip:127.0.0.1' on: agent.
        call createCall: 'dummy-sdp'.

        "First assertions for the invite"
        self assert: sent size equals: 1.
        msg := agent parser parse: sent first data.
        self assert: (msg parameter: 'CSeq' ifAbsent: [-1]) number equals: 1.
        self assert: call state equals: SIPCall stateInvite.

        "Now inject an auth requirement message"
        branch := (msg parameter: 'Via' ifAbsent: [nil]) branch.
        callId := (msg parameter: 'Call-ID' ifAbsent: [-1]).
        fromTag := (msg parameter: 'From' ifAbsent: [nil]).
        transport inject: (self authorizationRequired: branch callId: callId tag: fromTag tag).

        "Verify that a second message has been sent and it contains an auth result"
        self assert: sent size equals: 3.
        msg := agent parser parse: sent second data.
        self assert: msg class verb equals: 'ACK'.

        msg := agent parser parse: sent third data.
        self assert: msg class verb equals: 'INVITE'.
        branch := (msg parameter: 'Via' ifAbsent: [nil]) branch.
        self assert: (msg parameter: 'CSeq' ifAbsent: [-1]) number equals: 2.
        self assert: call state equals: SIPCall stateInvite.

        "Verify the auth part of the message"
        auth := msg parameter: 'Authorization' ifAbsent: [nil].
        self deny: auth isNil.
        self assert: auth username equals: 'st'.
        self assert: auth realm equals: 'Yate'.
        self assert: auth uri equals: 'sip:127.0.0.1'.
        self assert: auth nonce equals: '373ef30b297545cbce99fad09f1409cb.1392124197'.
        self assert: auth response equals: 'bc8dfaa413e897863dbab4c622e4b9b4'.

        "Inject another auth.."
        transport inject: (self authorizationRequired: branch callId: callId tag: fromTag tag cseq: 2).
        self assert: call state equals: #failed.
    ]

    setUpProxyAuthCall [
        | call msg branch callId fromTag auth secondBranch origCnonce |

        call := SIPCall fromUser: 'sip:sip@test' host: '127.0.0.1' port: 5060 to: 'sip:127.0.0.1' on: agent.
        call createCall: 'dummy-sdp'.

        "First assertions for the invite"
        self assert: sent size equals: 1.
        msg := agent parser parse: sent first data.
        self assert: (msg parameter: 'CSeq' ifAbsent: [-1]) number equals: 1.
        self assert: call state equals: SIPCall stateInvite.

        "Now inject an auth requirement message"
        branch := (msg parameter: 'Via' ifAbsent: [nil]) branch.
        callId := (msg parameter: 'Call-ID' ifAbsent: [-1]).
        fromTag := (msg parameter: 'From' ifAbsent: [nil]).
        transport inject: (self proxyAuthRequired: branch callId: callId tag: fromTag tag cseq: 1).

        "Verify that a second message has been sent and it contains an auth result"
        self assert: sent size equals: 3.
        msg := agent parser parse: sent second data.
        self assert: msg class verb equals: 'ACK'.

        msg := agent parser parse: sent third data.
        self assert: msg class verb equals: 'INVITE'.
        secondBranch := (msg parameter: 'Via' ifAbsent: [nil]) branch.
        self deny: branch = secondBranch.
        self assert: (msg parameter: 'CSeq' ifAbsent: [-1]) number equals: 2.
        self assert: call state equals: SIPCall stateInvite.

        "Verify the auth part of the message"
        auth := msg parameter: 'Proxy-Authorization' ifAbsent: [nil].
        self deny: auth isNil.
        self assert: auth username equals: 'st'.
        self assert: auth realm equals: '07440491'.
        self assert: auth uri equals: 'sip:127.0.0.1'.
        self assert: auth nonce equals: '06fafd01e9aade68c8400db8c9c146a7bfc5bc28C0A4AC1EE6A8'.
        self assert: auth qop equals: 'auth'.
        self assert: auth nonceCount equals: '00000001'.

        "Fields that depend on the random clientNonce"
        self assert: auth response size equals: 32.
        self assert: auth clientNonce size equals: 8.
        origCnonce := auth clientNonce.

        "Inject a 200 and check the ACK"
        transport inject: (self ok: secondBranch callId: callId tag: fromTag tag cseq: 2).
        self assert: sent size equals: 4.
        msg := agent parser parse: (sent at: 4) data.
        self assert: (msg parameter: 'CSeq' ifAbsent: [-1]) number equals: 2.
        self assert: call state equals: SIPCall stateSession.
        auth := msg parameter: 'Proxy-Authorization' ifAbsent: [nil].
        self deny: auth isNil.
        self assert: auth username equals: 'st'.
        self assert: auth realm equals: '07440491'.
        self assert: auth uri equals: 'sip:127.0.0.1'.
        self assert: auth nonce equals: '06fafd01e9aade68c8400db8c9c146a7bfc5bc28C0A4AC1EE6A8'.
        self assert: auth qop equals: 'auth'.
        self assert: auth nonceCount equals: '00000002'.
        self assert: auth clientNonce equals: origCnonce.

        ^call
    ]

    testWithProxyAuth [
        | call |
        call := self setUpProxyAuthCall.

        "And let it timeout.. With better code and the clientNonce
        being stored in the call/initial dialog we could even add
        the Proxy-Authorization to the BYE message.."
        call hangup.
    ]

    testWithProxyAuthRemoteBye [
        | call msg branch callId fromTag sentNr |
        call := self setUpProxyAuthCall.

        msg := agent parser parse: sent second data.
        branch := (msg parameter: 'Via' ifAbsent: [nil]) branch.
        callId := (msg parameter: 'Call-ID' ifAbsent: [-1]).
        fromTag := (msg parameter: 'From' ifAbsent: [nil]).

        sentNr := sent size.
        self assert: call state equals: SIPCall stateSession.

        transport inject: (self bye: branch callId: callId toTag: fromTag tag cseq: 2).
        self assert: sent size equals: sentNr + 1.
        self assert: call state equals: SIPCall stateRemoteHangup.

        msg := agent parser parse: (sent at: sentNr + 1) data.
        self assert: msg class equals: SIPResponse.
        self assert: msg code equals: '200'.
        self assert: msg phrase equals: 'OK'.
        self assert: (msg parameter: 'From' ifAbsent: [nil]) tag equals: '123'.
    ]

    testInviteWithRedirect [
        | call msg branch callId fromTag |

        call := SIPCall fromUser: 'sip:sip@test' host: '127.0.0.1' port: 5060 to: 'sip:127.0.0.1' on: agent.
        call createCall: 'dummy-sdp'.

        "First assertions for the invite"
        self assert: sent size equals: 1.
        msg := agent parser parse: sent first data.
        self assert: (msg parameter: 'CSeq' ifAbsent: [-1]) number equals: 1.
        self assert: call state equals: SIPCall stateInvite.
        branch := (msg parameter: 'Via' ifAbsent: [nil]) branch.
        callId := (msg parameter: 'Call-ID' ifAbsent: [-1]).
        fromTag := (msg parameter: 'From' ifAbsent: [nil]).

        "Inject a 100 trying"
        transport inject: (self trying: branch callId: callId tag: fromTag tag).
        self assert: call state equals: SIPCall stateInvite.

        "We could inject a 100 Trying but Now inject an auth requirement message"
        transport inject: (self redirect: branch callId: callId tag: fromTag tag).
        self assert: call state equals: SIPCall stateRedirect.

        "Check we get the ACK"
        msg := agent parser parse: sent second data.
        self assert: msg class equals: SIPACKRequest.
    ]
]