~divmod-dev/divmod.org/830343-optional-pycrypto

« back to all changes in this revision

Viewing changes to Sine/sign/test/test_terminator.py

  • Committer: glyph
  • Date: 2005-10-29 23:28:40 UTC
  • Revision ID: svn-v4:866e43f7-fbfc-0310-8f2a-ec88d1da2979:trunk:2703
dash: I hope this doesn't screw anything up in your local checkout, but that name doesn't make any sense: Sine, like a Sine wave, related to sound...

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from sign.test.test_sip import FakeClockTestCase, TestRealm, PermissiveChecker
 
2
from sign import sip
 
3
from twisted import cred
 
4
from twisted.internet import reactor
 
5
 
 
6
exampleInvite = """INVITE sip:bob@proxy2.org SIP/2.0\r
 
7
Via: SIP/2.0/UDP client.com:5060;branch=z9hG4bK74bf9\r
 
8
Max-Forwards: 70\r
 
9
From: Alice <sip:alice@proxy1.org>;tag=9fxced76sl\r
 
10
To: Bob <sip:bob@proxy2.org>\r
 
11
Call-ID: 3848276298220188511@client.com\r
 
12
CSeq: 1 INVITE\r
 
13
Contact: <sip:alice@client.com>\r
 
14
\r
 
15
v=0\r
 
16
o=alice 2890844526 2890844526 IN IP4 server.com\r
 
17
s=-\r
 
18
c=IN IP4 10.0.0.1\r
 
19
t=0 0\r
 
20
m=audio 49172 RTP/AVP 0\r
 
21
a=rtpmap:0 PCMU/8000\r
 
22
"""
 
23
 
 
24
 
 
25
response180 = """\
 
26
SIP/2.0 180 Ringing\r
 
27
Via: SIP/2.0/UDP client.com:5060;branch=z9hG4bK74bf9;received=10.0.0.1\r
 
28
From: Alice <sip:alice@proxy1.org>;tag=9fxced76sl\r
 
29
To: Bob <sip:bob@proxy2.org>;tag=314159\r
 
30
Call-ID: 3848276298220188511@client.com\r
 
31
Contact: <sip:bob@server.com>\r
 
32
CSeq: 1 INVITE\r
 
33
\r
 
34
"""
 
35
 
 
36
response200 = """SIP/2.0 200 OK\r
 
37
Via: SIP/2.0/UDP client.com:1234;branch=z9hG4bK74bf9\r
 
38
From: Alice <sip:alice@proxy1.org>;tag=9fxced76sl\r
 
39
To: Bob <sip:bob@proxy2.org>\r
 
40
Call-ID: 3848276298220188511@client.com\r
 
41
CSeq: 1 INVITE\r
 
42
Contact: <sip:alice@client.com>\r
 
43
\r
 
44
v=0\r
 
45
o=bob 2890844527 2890844527 IN IP4 server.com\r
 
46
s=-\r
 
47
c=IN IP4 10.0.0.2\r
 
48
t=0 0\r
 
49
m=audio 3456 RTP/AVP 0\r
 
50
a=rtpmap:0 PCMU/8000\r
 
51
"""
 
52
 
 
53
ackRequest = """\
 
54
ACK sip:bob@proxy2.org SIP/2.0\r
 
55
Via: SIP/2.0/UDP client.com:5060;branch=z9hG4bK74b76\r
 
56
Max-Forwards: 70\r
 
57
Route: sip:proxy2.org:5060;lr\r
 
58
From: Alice <sip:alice@proxy1.org>;tag=9fxced76sl\r
 
59
To: Bob <sip:bob@proxy2.org>;tag=314159\r
 
60
Call-ID: 3848276298220188511@client.com\r
 
61
CSeq: 1 ACK\r
 
62
\r
 
63
"""
 
64
 
 
65
byeRequest = """\
 
66
BYE sip:alice@proxy1.org SIP/2.0\r
 
67
Via: SIP/2.0/UDP server.com:5060;branch=z9hG4bKnashds7\r
 
68
Max-Forwards: 70\r
 
69
Route: sip:proxy2.org:5060;lr\r
 
70
Route: sip:proxy1.org:5060;lr\r
 
71
From: Bob <sip:bob@proxy2.org>;tag=314159\r
 
72
To: Alice <sip:alice@proxy1.org>;tag=9fxced76sl\r
 
73
Call-ID: 3848276298220188511@client.com\r
 
74
CSeq: 1 BYE\r
 
75
\r
 
76
"""
 
77
 
 
78
byeResponse = """\
 
79
SIP/2.0 200 OK\r
 
80
Via: SIP/2.0/UDP server.com:5060;branch=z9hG4bKnashds7;received=10.0.0.2\r
 
81
From: Bob <sip:bob@proxy2.org>;tag=314159\r
 
82
To: Alice <sip:alice@proxy1.org>;tag=9fxced76sl\r
 
83
Call-ID: 3848276298220188511@client.com\r
 
84
CSeq: 1 BYE\r
 
85
\r
 
86
"""
 
87
 
 
88
class CallTerminateTest(FakeClockTestCase):
 
89
    
 
90
    def setUp(self):
 
91
        r = TestRealm("server.com")
 
92
        p = cred.portal.Portal(r)
 
93
        p.registerChecker(PermissiveChecker())
 
94
        self.uas = sip.Terminator()
 
95
        self.sent = []
 
96
        self.sip = sip.SIPTransport(self.uas, ["server.com"], 5060)
 
97
        self.sip.sendMessage = lambda dest, msg: self.sent.append((dest, msg))
 
98
        self.testMessages = []
 
99
        self.parser = sip.MessagesParser(self.testMessages.append)
 
100
    def tearDown(self):
 
101
        self.clock.advance(33)
 
102
        reactor.iterate()
 
103
        self.clock.advance(33)
 
104
        reactor.iterate()
 
105
 
 
106
    def assertMsgEqual(self, first, second):
 
107
        self.testMessages[:] = []
 
108
        self.parser.dataReceived(first)
 
109
        self.parser.dataDone()
 
110
        self.parser.dataReceived(second)
 
111
        self.parser.dataDone()
 
112
        self.assertEqual(self.testMessages[0],  self.testMessages[1])
 
113
 
 
114
    def testCallTermination(self):
 
115
        self.sip.datagramReceived(exampleInvite, ('10.0.0.1', 5060))
 
116
        reactor.iterate()
 
117
        self.assertEquals(len(self.sent), 2)
 
118
        self.assertMsgEqual(self.sent[0], response180)
 
119
        self.assertMsgEqual(self.sent[1], response200)
 
120
        self.sent = []
 
121
        self.sip.datagramReceived(ackRequest, ('10.0.0.1', 5060))
 
122
        self.assertEquals(len(self.sent), 0)
 
123
        self.sip.datagramReceived(byeRequest, ('10.0.0.1', 5060))
 
124
        self.assertEquals(len(self.sent), 1)
 
125
        self.assertMsgEqual(self.sent[1], byeResponse)
 
126
    
 
127