-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
executable file
·167 lines (139 loc) · 4.78 KB
/
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
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
#!/usr/bin/env python3
import time
import typing
import unittest
import socket
from server import PROTOCOL_VERSION, handshake_magic, MobileRelayCommand, \
MobileRelayCallResult, MobileRelayWaitResult
class MobileRelayClient:
sock: typing.Optional[socket.socket]
def __init__(self, token: typing.Optional[bytes] = None):
self.sock = None
self.token = token
self.connect()
def connect(self) -> None:
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.connect(("127.0.0.1", 31227))
def close(self) -> None:
if self.sock:
self.sock.close()
self.sock = None
def send_handshake(self) -> None:
buffer = bytearray(handshake_magic)
if self.token is not None:
buffer.append(1)
buffer += self.token
else:
buffer.append(0)
self.sock.send(buffer)
def recv_handshake(self) -> typing.Optional[bytes]:
handshake = self.sock.recv(len(handshake_magic))
assert handshake == handshake_magic
new_token, = self.sock.recv(1)
if new_token == 1:
token = self.sock.recv(16)
assert len(token) == 16
self.token = token
return token
assert new_token == 0
return None
def send_call(self, number: str) -> None:
encnum = number.encode()
buffer = bytearray([PROTOCOL_VERSION, MobileRelayCommand.CALL])
buffer.append(len(encnum))
buffer += encnum
self.sock.send(buffer)
def recv_call(self) -> MobileRelayCallResult:
recv = self.sock.recv(3)
assert recv[0] == PROTOCOL_VERSION
assert recv[1] == MobileRelayCommand.CALL
return MobileRelayCallResult(recv[2])
def send_wait(self) -> None:
buffer = bytearray([PROTOCOL_VERSION, MobileRelayCommand.WAIT])
self.sock.send(buffer)
def recv_wait(self) -> tuple[MobileRelayWaitResult, str]:
recv = self.sock.recv(4)
assert recv[0] == PROTOCOL_VERSION
assert recv[1] == MobileRelayCommand.WAIT
assert recv[3] != 0
number = self.sock.recv(recv[3])
assert len(number) == recv[3]
return MobileRelayWaitResult(recv[2]), number.decode()
def send_get_number(self) -> None:
buffer = bytearray([PROTOCOL_VERSION, MobileRelayCommand.GET_NUMBER])
self.sock.send(buffer)
def recv_get_number(self) -> str:
recv = self.sock.recv(3)
assert recv[0] == PROTOCOL_VERSION
assert recv[1] == MobileRelayCommand.GET_NUMBER
assert recv[2] != 0
number = self.sock.recv(recv[2])
assert len(number) == recv[2]
return number.decode()
class Tests(unittest.TestCase):
def test_token(self):
c = MobileRelayClient()
c.send_handshake()
token = c.recv_handshake()
self.assertIsNot(token, None)
c.close()
c = MobileRelayClient(token)
c.send_handshake()
token = c.recv_handshake()
self.assertIs(token, None)
c.close()
def test_conn(self):
c1 = MobileRelayClient()
c1.send_handshake()
self.assertIsNot(c1.recv_handshake(), None)
c2 = MobileRelayClient()
c2.send_handshake()
self.assertIsNot(c2.recv_handshake(), None)
c1.send_get_number()
num = c1.recv_get_number()
c2.send_call(num)
c1.send_wait()
self.assertEqual(c2.recv_call(), MobileRelayCallResult.ACCEPTED)
self.assertEqual(c1.recv_wait()[0], MobileRelayWaitResult.ACCEPTED)
msg = b"hello"
c1.sock.send(msg)
c2.sock.send(msg)
self.assertEqual(c2.sock.recv(16), msg)
self.assertEqual(c1.sock.recv(16), msg)
c1.close()
c2.close()
def test_disconnect_call(self):
c = MobileRelayClient()
c.send_handshake()
self.assertIsNot(c.recv_handshake(), None)
c.send_call("1234")
time.sleep(0.1)
c.close()
def test_disconnect_wait(self):
c = MobileRelayClient()
c.send_handshake()
self.assertIsNot(c.recv_handshake(), None)
c.send_wait()
time.sleep(0.1)
c.close()
def test_connerr(self):
c = MobileRelayClient()
c.send_handshake()
c.close()
def test_connerr_relay(self):
c1 = MobileRelayClient()
c1.send_handshake()
self.assertIsNot(c1.recv_handshake(), None)
c1.send_get_number()
num = c1.recv_get_number()
c2 = MobileRelayClient()
c2.send_handshake()
self.assertIsNot(c2.recv_handshake(), None)
c2.send_call(num)
c1.send_wait()
time.sleep(0.2)
c1.close()
c2.recv_call()
c2.close()
if __name__ == '__main__':
unittest.main(verbosity=2)