-
Notifications
You must be signed in to change notification settings - Fork 36
/
s3g_test.py
executable file
·251 lines (200 loc) · 7.72 KB
/
s3g_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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import unittest
import sys
import io
import s3g
class CRCTests(unittest.TestCase):
def test_cases(self):
# Calculated using the processing tool 'ibutton_crc'
cases = [
[b'', 0],
[b'abcdefghijk', 0xb4],
[b'\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f', 0x3c],
]
for case in cases:
assert s3g.CalculateCRC(case[0]) == case[1]
class EncodeTests(unittest.TestCase):
def test_encode_int32(self):
cases = [
[0, '\x00\x00\x00\x00'],
[-2147483648, '\x00\x00\x00\x80'],
[2147483647, '\xFF\xFF\xFF\x7F'],
]
for case in cases:
assert s3g.EncodeInt32(case[0]) == case[1]
def test_encode_uint32(self):
cases = [
[0, '\x00\x00\x00\x00'],
[2147483647, '\xFF\xFF\xFF\x7F'],
[4294967295, '\xFF\xFF\xFF\xFF'],
]
for case in cases:
assert s3g.EncodeUint32(case[0]) == case[1]
class PacketEncodeTests(unittest.TestCase):
def test_reject_oversize_payload(self):
payload = bytearray()
for i in range (0, s3g.maximum_payload_length + 1):
payload.append(i)
self.assertRaises(s3g.PacketLengthError,s3g.EncodePayload,payload)
def test_packet_length(self):
payload = 'abcd'
packet = s3g.EncodePayload(payload)
assert len(packet) == len(payload) + 3
def test_packet_header(self):
payload = 'abcd'
packet = s3g.EncodePayload(payload)
assert packet[0] == s3g.header
def test_packet_length_field(self):
payload = 'abcd'
packet = s3g.EncodePayload(payload)
assert packet[1] == len(payload)
def test_packet_crc(self):
payload = 'abcd'
packet = s3g.EncodePayload(payload)
assert packet[6] == s3g.CalculateCRC(payload);
class PacketDecodeTests(unittest.TestCase):
def test_undersize_packet(self):
packet = bytearray('abc')
self.assertRaises(s3g.PacketLengthError,s3g.DecodePacket,packet)
def test_wrong_header(self):
packet = bytearray('abcd')
self.assertRaises(s3g.PacketHeaderError,s3g.DecodePacket,packet)
def test_bad_packet_length_field(self):
packet = bytearray()
packet.append(s3g.header)
packet.append(5)
packet.extend('ab')
self.assertRaises(s3g.PacketLengthFieldError,s3g.DecodePacket,packet)
def test_bad_crc(self):
packet = bytearray()
packet.append(s3g.header)
packet.append(1)
packet.extend('a')
packet.append(s3g.CalculateCRC('a')+1)
self.assertRaises(s3g.PacketCRCError,s3g.DecodePacket,packet)
def test_got_payload(self):
expected_payload = bytearray('abcde')
packet = bytearray()
packet.append(s3g.header)
packet.append(len(expected_payload))
packet.extend(expected_payload)
packet.append(s3g.CalculateCRC(expected_payload))
payload = s3g.DecodePacket(packet)
assert payload == expected_payload
class PacketStreamDecoderTests(unittest.TestCase):
def setUp(self):
self.s = s3g.PacketStreamDecoder()
def tearDown(self):
self.s = None
def test_starts_in_wait_for_header_mode(self):
assert(self.s.state == 'WAIT_FOR_HEADER')
assert(len(self.s.payload) == 0)
assert(self.s.expected_length == 0)
def test_reject_bad_header(self):
self.assertRaises(s3g.PacketHeaderError,self.s.ParseByte,0x00)
assert(self.s.state == 'WAIT_FOR_HEADER')
def test_accept_header(self):
self.s.ParseByte(s3g.header)
assert(self.s.state == 'WAIT_FOR_LENGTH')
def test_reject_bad_size(self):
self.s.ParseByte(s3g.header)
self.assertRaises(s3g.PacketLengthFieldError,self.s.ParseByte,s3g.maximum_payload_length+1)
def test_accept_size(self):
self.s.ParseByte(s3g.header)
self.s.ParseByte(s3g.maximum_payload_length)
assert(self.s.state == 'WAIT_FOR_DATA')
assert(self.s.expected_length == s3g.maximum_payload_length)
def test_accepts_data(self):
self.s.ParseByte(s3g.header)
self.s.ParseByte(s3g.maximum_payload_length)
for i in range (0, s3g.maximum_payload_length):
self.s.ParseByte(i)
assert(self.s.expected_length == s3g.maximum_payload_length)
for i in range (0, s3g.maximum_payload_length):
assert(self.s.payload[i] == i)
def test_reject_bad_crc(self):
payload = 'abcde'
self.s.ParseByte(s3g.header)
self.s.ParseByte(len(payload))
for i in range (0, len(payload)):
self.s.ParseByte(payload[i])
self.assertRaises(s3g.PacketCRCError,self.s.ParseByte,s3g.CalculateCRC(payload)+1)
def test_accepts_crc(self):
payload = 'abcde'
self.s.ParseByte(s3g.header)
self.s.ParseByte(len(payload))
for i in range (0, len(payload)):
self.s.ParseByte(payload[i])
self.s.ParseByte(s3g.CalculateCRC(payload))
assert(self.s.state == 'PAYLOAD_READY')
assert(self.s.payload == payload)
class ReplicatorTests(unittest.TestCase):
"""
Emulate a machine
"""
def setUp(self):
self.r = s3g.Replicator()
self.outputstream = io.BytesIO() # Stream that we will send responses on
self.inputstream = io.BytesIO() # Stream that we will receive commands on
self.file = io.BufferedRWPair(self.outputstream, self.inputstream)
self.r.file = self.file
def tearDown(self):
self.r = None
self.outputstream = None
self.inputstream = None
self.file = None
def test_send_command_timeout(self):
"""
Time out when no data is received. The input stream should have max_rety_count copies of the
payload packet in it.
"""
payload = 'abcde'
expected_packet = s3g.EncodePayload(payload)
self.assertRaises(s3g.TransmissionError,self.r.SendCommand,payload)
#TODO: We should use a queue here, it doesn't make sense to shove this in a file buffer?
self.inputstream.seek(0)
for i in range (0, s3g.max_retry_count):
for byte in expected_packet:
assert byte == ord(self.inputstream.read(1))
def test_send_command_many_bad_responses(self):
"""
Passing case: test that the transmission can recover from one less than the alloted
number of errors.
"""
payload = 'abcde'
expected_packet = s3g.EncodePayload(payload)
expected_response_payload = '12345'
for i in range (0, s3g.max_retry_count - 1):
self.outputstream.write('a')
self.outputstream.write(s3g.EncodePayload(expected_response_payload))
#TODO: We should use a queue here, it doesn't make sense to shove this in a file buffer?
self.outputstream.seek(0)
assert (expected_response_payload == self.r.SendCommand(payload))
#TODO: We should use a queue here, it doesn't make sense to shove this in a file buffer?
self.inputstream.seek(0)
for i in range (0, s3g.max_retry_count - 1):
for byte in expected_packet:
assert byte == ord(self.inputstream.read(1))
def test_send_command(self):
"""
Passing case: Preload the buffer with a correctly formatted expected response, and
verify that it works correctly.
"""
payload = 'abcde'
expected_response_payload = '12345'
self.outputstream.write(s3g.EncodePayload(expected_response_payload))
#TODO: We should use a queue here, it doesn't make sense to shove this in a file buffer?
self.outputstream.seek(0)
assert (expected_response_payload == self.r.SendCommand(payload))
assert (s3g.EncodePayload(payload) == self.inputstream.getvalue())
# TODO: Test timing based errors- can we send half a response, get it to re-send, then send a regular response?
def test_queue_extended_point(self):
expected_target = [1,2,3,4,5]
expected_velocity = 6
self.r.Move(expected_target, expected_velocity)
packet = bytearray(self.inputstream.getvalue())
payload = s3g.DecodePacket(packet)
assert payload[0] == s3g.command_map['QUEUE_EXTENDED_POINT']
for i in range(0, 5):
assert s3g.EncodeInt32(expected_target[i]) == payload[(i*4+1):(i*4+5)]
if __name__ == "__main__":
unittest.main()