-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathsgip_server_test.py
77 lines (70 loc) · 2.88 KB
/
sgip_server_test.py
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
# coding:utf-8
"""
sgip server test
"""
import eventlet
from eventlet.green import socket
from feiying import sgip
import unittest
class TestSGIPServer(unittest.TestCase):
def setUp(self):
self.msg_content = 'DZFY'
self.user_number = '13813005100'
self.socket = socket.socket()
ip = '127.0.0.1'
port = 8801
self.socket.connect((ip, port))
print 'server %s connected' % ip
def test_Business(self):
# send bind msg
bindMsg = sgip.SGIPBind(2, 'star', 'test')
header = sgip.SGIPHeader(sgip.SGIPHeader.size() + bindMsg.size(),
sgip.SGIPBind.ID)
bindMsg.header = header
raw_data = bindMsg.pack()
self.socket.sendall(raw_data)
# recv bind resp msg
resp_header_data = self.socket.recv(sgip.SGIPHeader.size())
resp_body_data = self.socket.recv(sgip.SGIPBindResp.size())
bindRespMsg = sgip.SGIPBindResp()
bindRespMsg.unpackBody(resp_body_data)
respHeader = sgip.SGIPHeader()
respHeader.unpack(resp_header_data)
self.assertEqual(respHeader.CommandID, sgip.SGIPBindResp.ID)
self.assertEqual(bindRespMsg.Result, 0)
# send deliver msg
deliverMsg = sgip.SGIPDeliver(self.user_number, '10010', 0, 0, 0,
len(self.msg_content), self.msg_content,
'')
deliverMsg.contentLength = len(self.msg_content)
header = sgip.SGIPHeader(sgip.SGIPHeader.size() + deliverMsg.mySize(),
sgip.SGIPDeliver.ID)
deliverMsg.header = header
raw_data = deliverMsg.pack()
self.socket.sendall(raw_data)
# recv deliver resp msg
resp_header_data = self.socket.recv(sgip.SGIPHeader.size())
resp_body_data = self.socket.recv(sgip.SGIPDeliverResp.size())
deliverRespMsg = sgip.SGIPDeliverResp()
deliverRespMsg.unpackBody(resp_body_data)
respHeader = sgip.SGIPHeader()
respHeader.unpack(resp_header_data)
self.assertEqual(respHeader.CommandID, sgip.SGIPDeliverResp.ID)
self.assertEqual(deliverRespMsg.Result, 0)
# send unbind msg
unbindMsg = sgip.SGIPUnbind()
header = sgip.SGIPHeader(sgip.SGIPHeader.size() + unbindMsg.size(),
sgip.SGIPUnbind.ID)
unbindMsg.header = header
raw_data = unbindMsg.pack()
self.socket.sendall(raw_data)
# recv unbind resp msg
resp_header_data = self.socket.recv(sgip.SGIPHeader.size())
resp_body_data = self.socket.recv(sgip.SGIPUnbindResp.size())
respHeader = sgip.SGIPHeader()
respHeader.unpack(resp_header_data)
self.assertEqual(respHeader.CommandID, sgip.SGIPUnbindResp.ID)
def tearDown(self):
self.socket.close()
if __name__ == '__main__':
unittest.main()