summaryrefslogtreecommitdiffstats
path: root/callagent/tests/SIPRegisterTransactionTest.st
blob: cdb1542a500a72aa2505bca55855c0bc4fe17216 (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
"
 (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: SIPRegisterTransactionTest [
    | sent transport agent dialog |
    <category: 'OsmoSIP-Callagent-Tests'>

    createSimple401: 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 displayString; nextPutAll: ' REGISTER'; cr; nl;
            nextPutAll: 'WWW-Authenticate: Digest realm="Yate", nonce="2ea525666844d310cd6d23cd6869b8fd.1393336640", stale=FALSE, algorithm=MD5'; cr; nl;
            nextPutAll: 'Server: YATE/5.1.0'; cr; nl;
            nextPutAll: 'Allow: ACK, INVITE, BYE, CANCEL, REGISTER, REFER, OPTIONS, INFO'; cr; nl;
            nextPutAll: 'Content-Length: 0'; cr; nl;
            cr; nl;
            contents
    ]

    createSimple200: aBranch callId: aCallId tag: aTag cseq: aCseq [
        ^(WriteStream on: String new)
            nextPutAll: 'SIP/2.0 200 OK'; 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 displayString; nextPutAll: ' REGISTER'; cr; nl;
            nextPutAll: 'WWW-Authenticate: Digest realm="Yate", nonce="2ea525666844d310cd6d23cd6869b8fd.1393336640", stale=FALSE, algorithm=MD5'; cr; nl;
            nextPutAll: 'Server: YATE/5.1.0'; cr; nl;
            nextPutAll: 'Allow: ACK, INVITE, BYE, CANCEL, REGISTER, REFER, OPTIONS, INFO'; 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'.

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

    testSimpleRegister [
        | register branch callId fromTag msg |

        register := (SIPRegisterTransaction createWith: dialog on: agent cseq: 1)
                        destination: 'sip:127.0.0.1';
                        yourself.
        register start.
        self assert: sent size equals: 1.
        msg := SIPParser parse: sent first data.
        self assert: (msg parameter: 'CSeq' ifAbsent: [-1]) number equals: 1.

        "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 createSimple401: branch callId: callId tag: fromTag tag cseq: 1).
        self assert: sent size equals: 3.
        msg := SIPParser parse: sent second data.
        self assert: msg class verb equals: 'ACK'.

        msg := SIPParser parse: sent third data.
        self assert: msg class verb equals: 'REGISTER'.
        self assert: (msg parameter: 'CSeq' ifAbsent: [-1]) number equals: 2.
        branch := (msg parameter: 'Via' ifAbsent: [nil]) branch.
        transport inject: (self createSimple200: branch callId: callId tag: fromTag tag cseq: 2).

        self assert: register state equals: SIPTransaction stateCompleted.

        "Cancel the transaction"
        register
            onTimeout: [];
            timedout.
    ]
]