def test_make_request(self): buf = bytearray(range(256)) seq = 2 action = stentura._OPEN p1, p2, p3, p4, p5 = 1, 2, 3, 4, 5 p = stentura._make_request(buf, action, seq, p1, p2, p3, p4, p5) for_crc = [seq, 18, 0, action, 0, p1, 0, p2, 0, p3, 0, p4, 0, p5, 0] crc = stentura._crc(for_crc) expected = bytearray([1] + for_crc + [crc & 0xFF, crc >> 8]) self.assertEqual(p, expected) # Now with data. data = b'Testing Testing 123' p = stentura._make_request(buf, action, seq, p1, p2, p3, p4, p5, data) length = 18 + len(data) + 2 for_crc = [ seq, length & 0xFF, length >> 8, action, 0, p1, 0, p2, 0, p3, 0, p4, 0, p5, 0 ] crc = stentura._crc(for_crc) data_crc = stentura._crc(data) expected = bytearray([1] + for_crc + [crc & 0xFF, crc >> 8]) expected.extend(data) expected.extend([data_crc & 0xFF, data_crc >> 8]) self.assertEqual(p, expected)
def make_response(seq, action, error=0, p1=0, p2=0, data=None, length=None): if not length: length = 14 if data: length += 2 + len(data) response = bytearray(14 + ((2 + len(data)) if data else 0)) struct.pack_into('<2B5H', response, 0, 1, seq, length, action, error, p1, p2) struct.pack_into('<H', response, 12, stentura._crc(response, 1, 11)) if data: response[14:14+len(data)] = data struct.pack_into('<H', response, 14 + len(data), stentura._crc(data)) return response
def make_response(seq, action, error=0, p1=0, p2=0, data=None, length=None): if not length: length = 14 if data: length += 2 + len(data) response = bytearray(14 + ((2 + len(data)) if data else 0)) struct.pack_into('<2B5H', response, 0, 1, seq, length, action, error, p1, p2) struct.pack_into('<H', response, 12, stentura._crc(response, 1, 11)) if data: response[14:14 + len(data)] = data struct.pack_into('<H', response, 14 + len(data), stentura._crc(data)) return response
def test_make_open(self): buf = bytearray(range(32)) # Start with junk in the buffer. seq = 79 drive = b'A' filename = b'REALTIME.000' p = stentura._make_open(buf, seq, drive, filename) for_crc = [seq, 20 + len(filename), 0, stentura._OPEN, 0, ord(drive), 0, 0, 0, 0, 0, 0, 0, 0, 0] crc = stentura._crc(for_crc) data_crc = stentura._crc(filename) expected = bytearray([1] + for_crc + [crc & 0xFF, crc >> 8]) expected.extend(filename) expected.extend([data_crc & 0xFF, data_crc >> 8]) self.assertEqual(p, expected)
def make_response(seq, action, error=0, p1=0, p2=0, data=None, length=None): if not length: length = 14 if data: length += 2 + len(data) response = struct.pack('<2B5H', 1, seq, length, action, error, p1, p2) crc = stentura._crc(buffer(response, 1, 11)) response += struct.pack('<H', crc) if data: crc = stentura._crc(data) if not isinstance(data, str) and not isinstance(data, buffer): data = ''.join([chr(b) for b in data]) response += data + struct.pack('<H', crc) return response
def test_make_open(self): buf = array.array('B', [3] * 18) # Start with junk in the buffer. seq = 79 drive = 'A' filename = 'REALTIME.000' p = stentura._make_open(buf, seq, drive, filename) for_crc = [seq, 20 + len(filename), 0, stentura._OPEN, 0, ord(drive), 0, 0, 0, 0, 0, 0, 0, 0, 0] crc = stentura._crc(for_crc) data_crc = stentura._crc(filename) expected = ([1] + for_crc + [crc & 0xFF, crc >> 8] + [ord(b) for b in filename] + [data_crc & 0xFF, data_crc >> 8]) self.assertSequenceEqual(p, [chr(b) for b in expected])
def test_make_open(self): buf = array.array('B', [3] * 18) # Start with junk in the buffer. seq = 79 drive = 'A' filename = 'REALTIME.000' p = stentura._make_open(buf, seq, drive, filename) for_crc = [ seq, 20 + len(filename), 0, stentura._OPEN, 0, ord(drive), 0, 0, 0, 0, 0, 0, 0, 0, 0 ] crc = stentura._crc(for_crc) data_crc = stentura._crc(filename) expected = ([1] + for_crc + [crc & 0xFF, crc >> 8] + [ord(b) for b in filename] + [data_crc & 0xFF, data_crc >> 8]) self.assertSequenceEqual(p, [chr(b) for b in expected])
def test_make_reset(self): buf = array.array('B', [3] * 20) # Start with junk in the buffer. seq = 67 p = stentura._make_reset(buf, seq) for_crc = [seq, 18, 0, stentura._RESET, 0] + ([0] * 10) crc = stentura._crc(for_crc) expected = [1] + for_crc + [crc & 0xFF, crc >> 8] self.assertSequenceEqual(p, [chr(b) for b in expected])
def test_make_reset(self): buf = bytearray(range(32)) # Start with junk in the buffer. seq = 67 p = stentura._make_reset(buf, seq) for_crc = [seq, 18, 0, stentura._RESET, 0] + ([0] * 10) crc = stentura._crc(for_crc) expected = bytearray([1] + for_crc + [crc & 0xFF, crc >> 8]) self.assertEqual(p, expected)
def test_make_read(self): buf = array.array('B', [3] * 20) # Start with junk in the buffer. seq = 32 block = 1 byte = 8 length = 20 p = stentura._make_read(buf, seq, block, byte, length) for_crc = [seq, 18, 0, stentura._READC, 0, 1, 0, 0, 0, length, 0, block, 0, byte, 0] crc = stentura._crc(for_crc) expected = [1] + for_crc + [crc & 0xFF, crc >> 8] self.assertSequenceEqual(p, [chr(b) for b in expected])
def test_make_read(self): buf = bytearray(range(32)) # Start with junk in the buffer. seq = 32 block = 1 byte = 8 length = 20 p = stentura._make_read(buf, seq, block, byte, length) for_crc = [seq, 18, 0, stentura._READC, 0, 1, 0, 0, 0, length, 0, block, 0, byte, 0] crc = stentura._crc(for_crc) expected = bytearray([1] + for_crc + [crc & 0xFF, crc >> 8]) self.assertEqual(p, expected)
def test_make_request(): buf = bytearray(range(256)) seq = 2 action = stentura._OPEN p1, p2, p3, p4, p5 = 1, 2, 3, 4, 5 p = stentura._make_request(buf, action, seq, p1, p2, p3, p4, p5) for_crc = [seq, 18, 0, action, 0, p1, 0, p2, 0, p3, 0, p4, 0, p5, 0] crc = stentura._crc(for_crc) expected = bytearray([1] + for_crc + [crc & 0xFF, crc >> 8]) assert p == expected # Now with data. data = b'Testing Testing 123' p = stentura._make_request(buf, action, seq, p1, p2, p3, p4, p5, data) length = 18 + len(data) + 2 for_crc = [seq, length & 0xFF, length >> 8, action, 0, p1, 0, p2, 0, p3, 0, p4, 0, p5, 0] crc = stentura._crc(for_crc) data_crc = stentura._crc(data) expected = bytearray([1] + for_crc + [crc & 0xFF, crc >> 8]) expected.extend(data) expected.extend([data_crc & 0xFF, data_crc >> 8]) assert p == expected
def test_make_request(self): buf = array.array('B') seq = 2 action = stentura._OPEN p1, p2, p3, p4, p5 = 1, 2, 3, 4, 5 p = stentura._make_request(buf, action, seq, p1, p2, p3, p4, p5) for_crc = [seq, 18, 0, action, 0, p1, 0, p2, 0, p3, 0, p4, 0, p5, 0] crc = stentura._crc(for_crc) expected = [1] + for_crc + [crc & 0xFF, crc >> 8] self.assertSequenceEqual(p, [chr(b) for b in expected]) # Now with data. data = 'Testing Testing 123' p = stentura._make_request(buf, action, seq, p1, p2, p3, p4, p5, data) length = 18 + len(data) + 2 for_crc = [seq, length & 0xFF, length >> 8, action, 0, p1, 0, p2, 0, p3, 0, p4, 0, p5, 0] crc = stentura._crc(for_crc) data_crc = stentura._crc(data) expected = ([1] + for_crc + [crc & 0xFF, crc >> 8] + [ord(b) for b in data] + [data_crc & 0xFF, data_crc >> 8]) self.assertSequenceEqual(p, [chr(b) for b in expected])
def test_make_read(self): buf = array.array('B', [3] * 20) # Start with junk in the buffer. seq = 32 block = 1 byte = 8 length = 20 p = stentura._make_read(buf, seq, block, byte, length) for_crc = [ seq, 18, 0, stentura._READC, 0, 1, 0, 0, 0, length, 0, block, 0, byte, 0 ] crc = stentura._crc(for_crc) expected = [1] + for_crc + [crc & 0xFF, crc >> 8] self.assertSequenceEqual(p, [chr(b) for b in expected])
def test_make_request(self): buf = array.array('B') seq = 2 action = stentura._OPEN p1, p2, p3, p4, p5 = 1, 2, 3, 4, 5 p = stentura._make_request(buf, action, seq, p1, p2, p3, p4, p5) for_crc = [seq, 18, 0, action, 0, p1, 0, p2, 0, p3, 0, p4, 0, p5, 0] crc = stentura._crc(for_crc) expected = [1] + for_crc + [crc & 0xFF, crc >> 8] self.assertSequenceEqual(p, [chr(b) for b in expected]) # Now with data. data = 'Testing Testing 123' p = stentura._make_request(buf, action, seq, p1, p2, p3, p4, p5, data) length = 18 + len(data) + 2 for_crc = [ seq, length & 0xFF, length >> 8, action, 0, p1, 0, p2, 0, p3, 0, p4, 0, p5, 0 ] crc = stentura._crc(for_crc) data_crc = stentura._crc(data) expected = ([1] + for_crc + [crc & 0xFF, crc >> 8] + [ord(b) for b in data] + [data_crc & 0xFF, data_crc >> 8]) self.assertSequenceEqual(p, [chr(b) for b in expected])
def test_crc(self): data = bytearray(b'123456789') self.assertEqual(stentura._crc(data), 0xBB3D)
def test_crc(): data = bytearray(b'123456789') assert stentura._crc(data) == 0xBB3D
def test_crc(self): data = [ord(x) for x in '123456789'] self.assertEqual(stentura._crc(data), 0xBB3D)