def test_bad_srec(self): # pack with self.assertRaises(bincopy.Error) as cm: bincopy.pack_srec('q', 0, 0, '') self.assertEqual(str(cm.exception), "expected record type 0..3 or 5..9, but got 'q'") # unpack too short record with self.assertRaises(bincopy.Error) as cm: bincopy.unpack_srec('') self.assertEqual(str(cm.exception), "record '' too short") # unpack bad first character with self.assertRaises(bincopy.Error) as cm: bincopy.unpack_srec('T0000011') self.assertEqual(str(cm.exception), "record 'T0000011' not starting with an 'S'") # unpack bad type with self.assertRaises(bincopy.Error) as cm: bincopy.unpack_srec('S.000011') self.assertEqual(str(cm.exception), "expected record type 0..3 or 5..9, but got '.'") # unpack bad crc with self.assertRaises(bincopy.Error) as cm: bincopy.unpack_srec('S1000011') self.assertEqual(str(cm.exception), "expected crc 'FF' in record S1000011, but got '11'")
def test_bad_srec(self): # pack with self.assertRaises(bincopy.Error) as cm: bincopy.pack_srec('q', 0, 0, '') self.assertEqual(str(cm.exception), "bad type 'q'") # unpack too short record with self.assertRaises(bincopy.Error) as cm: bincopy.unpack_srec('') self.assertEqual(str(cm.exception), "bad record ''") # unpack bad first character with self.assertRaises(bincopy.Error) as cm: bincopy.unpack_srec('T0000011') self.assertEqual(str(cm.exception), "bad record 'T0000011'") # unpack bad type with self.assertRaises(bincopy.Error) as cm: bincopy.unpack_srec('S.000011') self.assertEqual(str(cm.exception), "bad record type '.'") # unpack bad crc with self.assertRaises(bincopy.Error) as cm: bincopy.unpack_srec('S1000011') self.assertEqual(str(cm.exception), "bad crc in record 'S1000011'")
def unpack_function(file_path: str, tmp_dir: str) -> Dict[str, str]: ''' file_path specifies the input file. tmp_dir should be used to store the extracted files. ''' target_file = Path(tmp_dir) / _get_unpacked_filename(file_path) decoded = b'' try: srec = Path(file_path).read_bytes().splitlines() for line in srec: try: _, _, _, data = bincopy.unpack_srec(line.decode()) decoded += data except UnicodeDecodeError: break target_file.write_bytes(decoded) except bincopy.Error as srec_error: return { 'output': 'Unknown error in srec decoding: {}'.format(str(srec_error)) } except FileNotFoundError as fnf_error: return {'output': 'Failed to open file: {}'.format(str(fnf_error))} return {'output': 'Successfully decoded srec file'}
def unpack_function(file_path, tmp_dir): ''' file_path specifies the input file. tmp_dir should be used to store the extracted files. ''' target_file = Path(tmp_dir, Path(file_path).name) decoded = b'' try: srec = Path(file_path).read_text().splitlines() for line in srec: _, _, _, data = bincopy.unpack_srec(line) decoded += data Path(target_file).write_bytes(decoded) except bincopy.Error as srec_error: return {'output': 'Unknown error in srec decoding: {}'.format(str(srec_error))} except FileNotFoundError as fnf_error: return {'output': 'Failed to open file: {}'.format(str(fnf_error))} return {'output': 'Successfully decoded srec file'}