-
Notifications
You must be signed in to change notification settings - Fork 30
/
test_enet.py
191 lines (157 loc) · 7.47 KB
/
test_enet.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import unittest
import enet
class TestAddress(unittest.TestCase):
def test_host(self):
self.assertEquals(enet.Address("127.0.0.1", 9999).host, "127.0.0.1")
self.assertEquals(enet.Address("localhost", 9999).host, "127.0.0.1")
self.assertEquals(enet.Address(None, 9999).host, "*")
self.assertRaises(IOError, enet.Address, "foo.bar.baz.999", 9999)
def test_port(self):
self.assertEquals(enet.Address("127.0.0.1", 9999).port, 9999)
self.assertRaises(TypeError, enet.Address, "127.0.0.1", "foo")
def test_hostname(self):
import socket
self.assertEquals(enet.Address(socket.gethostname(), 9999).hostname, socket.gethostname())
self.assertEquals(enet.Address(None, 9999).hostname, "*")
def test_str(self):
self.assertEquals(enet.Address("127.0.0.1", 9999).__str__(), "127.0.0.1:9999")
def test_richcmp(self):
self.assertTrue(enet.Address("127.0.0.1", 9999) == enet.Address("127.0.0.1", 9999))
self.assertTrue(enet.Address("127.0.0.1", 9999) != enet.Address("127.0.0.1", 8888))
self.assertFalse(enet.Address("127.1.1.1", 1992) == enet.Address("127.0.0.1", 9999))
class TestPacket(unittest.TestCase):
def test_data(self):
self.assertEquals(enet.Packet(b"foo\0bar").data, b"foo\0bar")
self.assertRaises(MemoryError, getattr, enet.Packet(), "data")
def test_dataLength(self):
self.assertEquals(enet.Packet(b"foobar").dataLength, 6)
self.assertEquals(enet.Packet(b"foo\0bar").dataLength, 7)
self.assertRaises(MemoryError, getattr, enet.Packet(), "dataLength")
def test_flags(self):
self.assertEquals(enet.Packet(b"foobar").flags, 0)
self.assertEquals(enet.Packet(b"foobar", enet.PACKET_FLAG_UNSEQUENCED).flags, enet.PACKET_FLAG_UNSEQUENCED)
self.assertRaises(MemoryError, getattr, enet.Packet(), "flags")
class TestHost(unittest.TestCase):
def setUp(self):
self.client = enet.Host(None, 1, 0, 0, 0)
self.server = enet.Host(enet.Address("localhost", 54301), 1, 0, 0, 0)
self.peer = self.client.connect(enet.Address("localhost", 54301), 1)
self.assertEquals(self.peer.state, enet.PEER_STATE_CONNECTING)
def tearDown(self):
del self.client
del self.server
del self.peer
def test_connect(self):
client_connected = False
server_connected = False
counter = 0
while counter < 100 or not (client_connected and server_connected):
event = self.client.service(0)
if event.type == enet.EVENT_TYPE_CONNECT:
self.assertEquals(event.peer.state, enet.PEER_STATE_CONNECTED)
client_connected = True
event = self.server.service(0)
if event.type == enet.EVENT_TYPE_CONNECT:
self.assertEquals(event.peer.state, enet.PEER_STATE_CONNECTED)
server_connected = True
counter += 1
self.assertEquals(client_connected, True)
self.assertEquals(server_connected, True)
def test_socketsend(self):
self.send_done = False
socketsend_msg = b"\xff\xff\xff\xffgetstatus\x00"
def f(address, data):
if data != socketsend_msg:
# error messages are not propagating
# through cython
print("data != statusResponse")
assert(False)
self.send_done = True
while not self.send_done:
self.client.service(0)
self.client.socket.send(self.server.address, socketsend_msg)
event = self.server.service(0)
if event.type == enet.EVENT_TYPE_CONNECT:
self.server.intercept = f
def test_broadcast(self):
broadcast_done = False
broadcast_msg = b"foo\0bar\n baz!"
while not broadcast_done:
event = self.client.service(0)
if event.type == enet.EVENT_TYPE_RECEIVE:
self.assertEquals(event.peer.state, enet.PEER_STATE_CONNECTED)
self.assertEquals(event.packet.data, broadcast_msg)
broadcast_done = True
event = self.server.service(0)
if event.type == enet.EVENT_TYPE_CONNECT:
self.assertEquals(event.peer.state, enet.PEER_STATE_CONNECTED)
self.server.broadcast(0, enet.Packet(broadcast_msg))
class TestPeer(unittest.TestCase):
def setUp(self):
self.client = enet.Host(None, 1, 0, 0, 0)
self.server = enet.Host(enet.Address("localhost", 54301), 1, 0, 0, 0)
self.peer = self.client.connect(enet.Address("localhost", 54301), 1)
self.assertEquals(self.peer.state, enet.PEER_STATE_CONNECTING)
self.assertTrue(self.peer == self.peer)
self.assertFalse(self.peer != self.peer)
def tearDown(self):
del self.client
del self.server
del self.peer
def test_access(self):
self.assertRaises(MemoryError, enet.Peer().reset)
def test_send(self):
msg = b"foo\0bar"
msg_received = False
while not msg_received:
event = self.server.service(0)
if event.type == enet.EVENT_TYPE_RECEIVE:
msg_received = True
self.assertEquals(event.packet.data, msg)
event = self.client.service(0)
if event.type == enet.EVENT_TYPE_CONNECT:
packet = enet.Packet(msg)
self.assertEquals(packet.sent, False)
ret = self.peer.send(0, packet)
self.assertEquals(ret, 0)
self.assertEquals(packet.sent, True)
def test_reset(self):
reset_done = False
while not reset_done:
event = self.server.service(0)
if event.type == enet.EVENT_TYPE_CONNECT:
self.assertEquals(event.peer.state, enet.PEER_STATE_CONNECTED)
event.peer.reset()
self.assertEquals(event.peer.state, enet.PEER_STATE_DISCONNECTED)
reset_done = True
event = self.client.service(0)
def test_disconnect(self):
connected = True
while connected:
event = self.server.service(0)
if event.type == enet.EVENT_TYPE_DISCONNECT:
self.assertEquals(event.peer.state, enet.PEER_STATE_DISCONNECTED)
connected = False
elif event.type == enet.EVENT_TYPE_CONNECT:
self.assertEquals(self.peer.state, enet.PEER_STATE_CONNECTED)
self.peer.disconnect()
self.assertEquals(self.peer.state, enet.PEER_STATE_DISCONNECTING)
event = self.client.service(0)
if event.type == enet.EVENT_TYPE_CONNECT:
self.assertEquals(event.peer.state, enet.PEER_STATE_CONNECTED)
def test_disconnect_later(self):
connected = True
while connected:
event = self.server.service(0)
if event.type == enet.EVENT_TYPE_DISCONNECT:
self.assertEquals(event.peer.state, enet.PEER_STATE_DISCONNECTED)
connected = False
elif event.type == enet.EVENT_TYPE_CONNECT:
self.assertEquals(self.peer.state, enet.PEER_STATE_CONNECTED)
self.peer.disconnect_later()
self.assertEquals(self.peer.state, enet.PEER_STATE_DISCONNECT_LATER)
event = self.client.service(0)
if event.type == enet.EVENT_TYPE_CONNECT:
self.assertEquals(event.peer.state, enet.PEER_STATE_CONNECTED)
if __name__ == '__main__':
unittest.main()