def modify_poc(): npk_file = Npk(Path("tests/testData/6_48_4/gps-6.48.4.npk")) # print overview print("----Overview--------------------") pprint([ f"pos: {pos:2} - Name: {cnt.cnt_id_name} (id:{cnt.cnt_id:2})" for pos, cnt in npk_file.pck_enumerate_cnt ]) print("-------------------------------") # The following code example will modify the payload section of PckDescription CNT_ID = 4 # PckDescription print("Payload original:") print_overview(npk_file, cnt_id=CNT_ID) print("overwrite payload - same size:") npk_file.pck_cnt_list[CNT_ID].cnt_payload = b"a" * 25 print_overview(npk_file, cnt_id=CNT_ID) # Modifying the size of the payload can affect the whole npk package and # forces recalculations in other containers of this package print("Payload new - small size:") npk_file.pck_cnt_list[CNT_ID].cnt_payload = b"b" * 10 print_overview(npk_file, cnt_id=CNT_ID) print("Payload new - increased:") npk_file.pck_cnt_list[CNT_ID].cnt_payload = b"c" * 100 print_overview(npk_file, cnt_id=CNT_ID) print("Write File: modified.npk") Path("modified.npk").write_bytes(npk_file.pck_full_binary)
def test_getEnumeratedListOfCntInNpk(self): cntList = list(Npk(self.npkFile).pck_enumerate_cnt) cntId, cnt = cntList[0] self.assertEqual(1, len(cntList)) self.assertEqual(0, cntId) self.assertTrue(isinstance(cnt, PckHeader))
def test_fileIsNoNpkFile(self): self.npkFile.write_bytes(b"NoMagicBytesAtHeadOfFile") with self.assertRaises(NPKMagicBytesError) as e: _ = Npk(self.npkFile).pck_magic_bytes self.assertEqual(e.exception.args[0], "Magic bytes not found in Npk file")
def test_createFile_changePayloadTwice(self): oldPayload = self.npk.pck_header.cnt_payload self.npk.pck_header.cnt_payload = b"A" self.npk.pck_header.cnt_payload = oldPayload self.assertEqual( Npk(self.npkFile).file.read_bytes(), self.npk.pck_full_binary)
def test_npkFileIsCorrupt_fileCorruptException(self): self.npkFile.write_bytes(MAGIC_BYTES + b"CorruptFile") with self.assertRaises(NPKError) as e: _ = Npk(self.npkFile).pck_cnt_list self.assertEqual( e.exception.args[0], f"File maybe corrupted. Please download again. File: {self.npkFile.absolute()}" )
def test_getAllCnt_exceptionWithUnknownCntInNpk(self): unknownCnt = DummyHeaderCnt() unknownCnt._00_cnt_id = struct.pack("H", 999) self.npkFile.write_bytes( get_dummy_npk_binary(cnt=unknownCnt.get_binary)) with self.assertRaises(NPKIdError) as e: _ = Npk(self.npkFile).pck_cnt_list self.assertEqual( e.exception.args[0], f"Failed with cnt id: 999\n" f"New cnt id discovered in file: {self.npkFile.absolute()}")
def unpack_function(file_path, tmp_dir): try: npk_file = Npk(Path(file_path)) except NPKMagicBytesError: return {'error': 'Invalid file. No npk magic found.'} meta = {'output': get_full_pkt_info(npk_file)} export_folder = Path(tmp_dir) / f"{npk_file.file.stem}" export_folder.mkdir(parents=True, exist_ok=True) extract_container(npk_file, export_folder, CNT_HANDLER.keys()) return meta
def test_extractPayloadFromCnt_createFilesWithPayload(self): self.file.write_bytes(get_dummy_npk_binary()) npkFile = Npk(self.file) extract_container(npkFile, self.output_folder, [NPK_PCK_HEADER]) created_files = list(self.output_folder.rglob("*")) self.assertEqual(1, len(created_files)) self.assertEqual([self.output_folder / '000_cnt_PckHeader.raw'], created_files) self.assertEqual(DummyHeaderCnt()._02_payload, created_files[0].read_bytes())
def test_getFullPktInfo_asString(self): self.file.write_bytes(get_dummy_npk_binary()) npkFile = Npk(self.file) result = get_full_pkt_info(npkFile) self.assertEqual([ f"{self.file.name}", 'Cnt: 0:PckHeader', 'PckHeader', ' Cnt id: 1', ' Cnt offset: 8', ' Cnt len: 41', ' Payload len: 35', " Payload[0:10]: b'0123456789' [...] ", ' Program name: 01234567890abcde', ' Os version: 1.2.3 - rc(?): 4', ' Created at: 1970-01-01 00:00:01', ' NullBlock: (0, 0, 0, 0)', ' Flags: (0, 0, 0, 0, 0, 0, 0)' ], result)
def setUp(self) -> None: self.npkFile = Path("tests/testData/6_45_6/gps-6.45.6.npk") self.npk = Npk(self.npkFile) self.cnt = self.npk.pck_cnt_list
def test_extractMagicBytes(self): self.assertEqual(MAGIC_BYTES, Npk(self.npkFile).pck_magic_bytes)
def main(): opts = parse_args() files = (Npk(f) for f in (opts.files if opts.files else get_all_nkp_files( opts.src_folder, opts.glob))) analyse_npk(opts, files)
def test_getBasicCntInfo(self): self.file.write_bytes(get_dummy_npk_binary()) self.assertEqual(['Cnt: 0:PckHeader'], get_short_cnt_info(Npk(self.file)))
def test_extractLenOfNpkPayload_propagatedSizeIsValid(self): self.assertEqual(len(DummyHeaderCnt().get_binary), Npk(self.npkFile).pck_payload_len)
def test_calculatePckFullSize_equalsFileSize(self): self.assertEqual(self.npkFile.stat().st_size, Npk(self.npkFile).pck_full_size)
def test_getAllCnt_returnAsList(self): cntList = Npk(self.npkFile).pck_cnt_list self.assertEqual(1, len(cntList)) self.assertTrue(isinstance(cntList[0], PckHeader))
def test_getNpkBinary_equalsOriginalBinary(self): npkBinary = self.npkFile.read_bytes() self.assertEqual(npkBinary, Npk(self.npkFile).pck_full_binary)