|
| 1 | +#! /usr/bin/env python |
| 2 | +# vim: set fileencoding=utf-8 : |
| 3 | +""" hobeta.py tests """ |
| 4 | + |
| 5 | +import os |
| 6 | +import io |
| 7 | +import struct |
| 8 | +import unittest |
| 9 | +import tempfile |
| 10 | +from collections import namedtuple |
| 11 | +from zxtools import hobeta |
| 12 | + |
| 13 | + |
| 14 | +class TestHobeta(unittest.TestCase): |
| 15 | + def test_ckechsum(self): |
| 16 | + data = b'F.load.AC\x00\x80\xf9\x06\x00\x07' |
| 17 | + self.assertEqual(hobeta.calc_checksum(data), 20661) |
| 18 | + |
| 19 | + def test_format_size(self): |
| 20 | + header_len = struct.calcsize(hobeta.HEADER_FMT) |
| 21 | + self.assertEqual(header_len, 17) |
| 22 | + |
| 23 | + def test_format(self): |
| 24 | + data = b"\x46\x2E\x6C\x6F\x61\x64\x2E\x41" \ |
| 25 | + b"\x43\x00\x80\xF9\x06\x00\x07\xB5\x50" |
| 26 | + record = hobeta.Header._make(struct.unpack_from(hobeta.HEADER_FMT, data)) |
| 27 | + |
| 28 | + self.assertEqual(record.filename, b"F.load.A") |
| 29 | + self.assertEqual(record.filetype, ord('C')) |
| 30 | + self.assertEqual(record.start, 32768) |
| 31 | + self.assertEqual(record.length, 1785) |
| 32 | + self.assertEqual(record.first_sector, 0) |
| 33 | + self.assertEqual(record.occupied_sectors, 7) |
| 34 | + self.assertEqual(record.check_sum, 20661) |
| 35 | + |
| 36 | + def test_parse_info(self): |
| 37 | + test_file = io.BytesIO(b"\x46\x2E\x6C\x6F\x61\x64\x2E\x41\x43\x00\x80\xF9\x06\x00\x07\xB5" |
| 38 | + b"\x50\x00\x00\x3B\x20\x4C\x4F\x41\x44\x45\x52\x20\x66\x6F\x72\x20") |
| 39 | + header, crc = hobeta.parse_info(test_file) |
| 40 | + |
| 41 | + self.assertEqual(header.filename, b"F.load.A") |
| 42 | + self.assertEqual(header.filetype, ord('C')) |
| 43 | + self.assertEqual(header.start, 32768) |
| 44 | + self.assertEqual(header.length, 1785) |
| 45 | + self.assertEqual(header.first_sector, 0) |
| 46 | + self.assertEqual(header.occupied_sectors, 7) |
| 47 | + self.assertEqual(header.check_sum, 20661) |
| 48 | + self.assertEqual(header.check_sum, crc) |
| 49 | + |
| 50 | + def test_parse_info2(self): |
| 51 | + test_file = io.BytesIO(b"\x46\x2E\x6C\x6F\x61\x64\x2E\x41\x43\x00\x80\x02\x00\x00\x07\xB5" |
| 52 | + b"\x50\x00\x00\x3B\x20\x4C\x4F\x41\x44\x45\x52\x20\x66\x6F\x72\x20") |
| 53 | + header, crc = hobeta.parse_info(test_file) |
| 54 | + |
| 55 | + self.assertEqual(header.filename, b"F.load.A") |
| 56 | + self.assertEqual(header.filetype, ord('C')) |
| 57 | + self.assertEqual(header.start, 32768) |
| 58 | + self.assertEqual(header.length, 2) |
| 59 | + self.assertEqual(header.first_sector, 0) |
| 60 | + self.assertEqual(header.occupied_sectors, 7) |
| 61 | + self.assertEqual(header.check_sum, 20661) |
| 62 | + self.assertNotEqual(header.check_sum, crc) |
| 63 | + |
| 64 | + def strip_header(self, test_input_file, ignore_header_info): |
| 65 | + temp_output_path = tempfile.mkstemp()[1] |
| 66 | + temp_output_file = open(temp_output_path, "wb") |
| 67 | + Args = namedtuple('Args', "hobeta_file output_file ignore_header") |
| 68 | + parsed_args = Args(test_input_file, temp_output_file, ignore_header_info) |
| 69 | + copied_bytes = hobeta.strip_header(parsed_args) |
| 70 | + |
| 71 | + return temp_output_path, copied_bytes |
| 72 | + |
| 73 | + def test_strip_header1(self): |
| 74 | + test_input_file = io.BytesIO(b"\x46\x2E\x6C\x6F\x61\x64\x2E\x41\x43\x00\x80\xF9\x06\x00\x07\xB5" |
| 75 | + b"\x50\x00\x00\x3B\x20\x4C\x4F\x41\x44\x45\x52\x20\x66\x6F\x72\x20") |
| 76 | + temp_output_path, bytes_count = self.strip_header(test_input_file, True) |
| 77 | + try: |
| 78 | + temp_output_file = open(temp_output_path, "rb") |
| 79 | + temp_output_file.seek(0, os.SEEK_END) |
| 80 | + self.assertEqual(temp_output_file.tell(), 15) |
| 81 | + self.assertEqual(bytes_count, 15) |
| 82 | + finally: |
| 83 | + temp_output_file.close() |
| 84 | + os.remove(temp_output_path) |
| 85 | + |
| 86 | + def test_strip_header2(self): |
| 87 | + test_input_file = io.BytesIO(b"\x46\x2E\x6C\x6F\x61\x64\x2E\x41\x43\x00\x80\xF9\x06\x00\x07\xB5" |
| 88 | + b"\x50\x00\x00\x3B\x20\x4C\x4F\x41\x44\x45\x52\x20\x66\x6F\x72\x20") |
| 89 | + temp_output_path, bytes_count = self.strip_header(test_input_file, False) |
| 90 | + try: |
| 91 | + temp_output_file = open(temp_output_path, "rb") |
| 92 | + temp_output_file.seek(0, os.SEEK_END) |
| 93 | + self.assertEqual(temp_output_file.tell(), 15) |
| 94 | + self.assertEqual(bytes_count, 15) |
| 95 | + finally: |
| 96 | + temp_output_file.close() |
| 97 | + os.remove(temp_output_path) |
| 98 | + |
| 99 | + def test_strip_header3(self): |
| 100 | + test_input_file = io.BytesIO(b"\x46\x2E\x6C\x6F\x61\x64\x2E\x41\x43\x00\x80\x0A\x00\x00\x07\xB5" |
| 101 | + b"\x50\x00\x00\x3B\x20\x4C\x4F\x41\x44\x45\x52\x20\x66\x6F\x72\x20") |
| 102 | + temp_output_path, bytes_count = self.strip_header(test_input_file, False) |
| 103 | + try: |
| 104 | + temp_output_file = open(temp_output_path, "rb") |
| 105 | + temp_output_file.seek(0, os.SEEK_END) |
| 106 | + self.assertEqual(temp_output_file.tell(), 10) |
| 107 | + self.assertEqual(bytes_count, 10) |
| 108 | + finally: |
| 109 | + temp_output_file.close() |
| 110 | + os.remove(temp_output_path) |
| 111 | + |
| 112 | + |
| 113 | +if __name__ == '__main__': |
| 114 | + unittest.main() |
0 commit comments