def do_raw_action(self, action, size, output_size=None, data=None, timeout=2): # type: (str, int, Optional[int], Optional[bytearray], Union[T_co, int]) -> Union[T_co, Dict[str, Any]] input_field = Field.padding(13) if data is None else Field.bytes('data', len(data)) command = MasterCommandSpec(action, [input_field], [Field.bytes('data', size), Field.lit('\r\n')]) return self.do_command(command, fields={'data': data}, timeout=timeout)
def test_output_wiht_crc(self): """ Test crc and is_crc functions. """ field = Field.crc() self.assertEqual('crc', field.name) self.assertTrue(Field.is_crc(field)) field = Field.padding(1) self.assertFalse(Field.is_crc(field))
def test_create_input(self): """ Test for MasterCommandSpec.create_input """ basic_action = MasterCommandSpec('BA', [ Field.byte('actionType'), Field.byte('actionNumber'), Field.padding(11) ], []) ba_input = basic_action.create_input(1, { "actionType": 2, "actionNumber": 4 }) self.assertEqual(21, len(ba_input)) self.assertEqual( bytearray(b'STRBA\x01\x02\x04' + (b'\x00' * 11) + b'\r\n'), ba_input)
def test_create_basic_action(self): """ Test for MasterCommandSpec.basic_action """ self.master_version = (3, 143, 113) basic_action = MasterCommandSpec('BA', [ Field.byte('actionType'), Field.byte('actionNumber'), Field.byte('parameter'), Field.padding(10) ], []) ba_input = basic_action.create_input(1, { "actionType": 2, "actionNumber": 4, "parameter": 9 }) self.assertEqual(21, len(ba_input)) self.assertEqual( bytearray(b'STRBA\x01\x02\x04\x09' + (b'\x00' * 10) + b'\r\n'), ba_input)
def test_encode_padding_field(self): """ Test for Field.padding.encode """ self.assertEqual(bytearray(b''), Field.padding(0).encode(None)) self.assertEqual(bytearray(b'\x00\x00'), Field.padding(2).encode(None))
def test_consume_output(self): """ Test for MasterCommandSpec.consume_output """ basic_action = MasterCommandSpec('BA', [], [ Field.string('response', 2), Field.padding(11), Field.lit('\r\n') ]) # Simple case, full string without offset at once (bytes_consumed, result, done) = basic_action.consume_output( bytearray(b'OK' + (b'\x00' * 11) + b'\r\n'), None) self.assertEqual((15, True), (bytes_consumed, done)) self.assertEqual('OK', result["response"]) # Full string with extra padding in the back (bytes_consumed, result, done) = basic_action.consume_output( bytearray(b'OK' + (b'\x00' * 11) + b'\r\nSome\x04Junk'), None) self.assertEqual((15, True), (bytes_consumed, done)) self.assertEqual('OK', result["response"]) # String in 2 pieces (bytes_consumed, result, done) = basic_action.consume_output(bytearray(b'OK' + (b'\x00' * 5)), None) self.assertEqual((7, False), (bytes_consumed, done)) self.assertEqual(bytearray(b'\x00' * 5), result.pending_bytes) (bytes_consumed, result, done) = basic_action.consume_output( bytearray((b'\x00' * 6) + b'\r\n'), result) self.assertEqual((8, True), (bytes_consumed, done)) self.assertEqual('OK', result["response"]) # String in 2 pieces with extra padding in back (bytes_consumed, result, done) = basic_action.consume_output(bytearray(b'OK' + (b'\x00' * 5)), None) self.assertEqual((7, False), (bytes_consumed, done)) self.assertEqual(bytearray(b'\x00' * 5), result.pending_bytes) (bytes_consumed, result, done) = basic_action.consume_output( bytearray((b'\x00' * 6) + b'\r\nWorld'), result) self.assertEqual((8, True), (bytes_consumed, done)) self.assertEqual('OK', result["response"]) # String in 3 pieces (bytes_consumed, result, done) = basic_action.consume_output(bytearray(b'OK' + (b'\x00' * 5)), None) self.assertEqual((7, False), (bytes_consumed, done)) self.assertEqual(bytearray(b'\x00' * 5), result.pending_bytes) (bytes_consumed, result, done) = basic_action.consume_output(bytearray(b'\x00' * 3), result) self.assertEqual((3, False), (bytes_consumed, done)) self.assertEqual(bytearray(b'\x00' * 8), result.pending_bytes) (bytes_consumed, result, done) = basic_action.consume_output(bytearray(b'\x00' * 3), result) self.assertEqual((3, False), (bytes_consumed, done)) self.assertEqual(bytearray(b''), result.pending_bytes) (bytes_consumed, result, done) = basic_action.consume_output(bytearray(b'\r\n'), result) self.assertEqual((2, True), (bytes_consumed, done)) self.assertEqual('OK', result["response"])
def test_decode_padding_field(self): """ Test for Field.padding.decode """ self.assertEqual('', Field.padding(1).decode(bytearray(b'\x00'))) with self.assertRaises(ValueError): Field.padding(1).decode(bytearray(b'\x00\x00'))