def __init__(self, name): super().__init__(name) card = MFUCard(bytes=bytes([1,2,3,4]*16)) self.page = MFUPage(card, 0)
class MFUPageTests(unittest.TestCase): def __init__(self, name): super().__init__(name) card = MFUCard(bytes=bytes([1,2,3,4]*16)) self.page = MFUPage(card, 0) def test_iter_bytes(self): byteiter = iter(self.page) b = next(byteiter) self.assertEqual(b, 1) b = next(byteiter) self.assertEqual(b, 2) b = next(byteiter) self.assertEqual(b, 3) b = next(byteiter) self.assertEqual(b, 4) with self.assertRaises(StopIteration): next(byteiter) def test_as_list(self): bytelist = list(self.page) self.assertIsInstance(bytelist, list) self.assertEqual(bytelist, [1, 2, 3, 4]) def test_slice(self): self.assertEqual(self.page[0], 1) self.assertEqual(self.page[1:-1], b'\x02\x03') @unittest.skip('item assignment is not implemented') def test_set_bytes_types(self): self.assertNotEqual(self.page[0], 99) self.page[0] = 99 self.assertEqual(self.page[0], 99) self.page[0] = b'\x99' self.assertEqual(self.page[0], 0x99) @unittest.skip('item assignment is not implemented') def test_set_bytes_negative_index(self): self.assertNotEqual(self.page[-1], 99) self.page[-1] = 99 self.assertEqual(self.page[-1], 99) @unittest.skip('item assignment is not implemented') def test_set_bytes_slice_value_types(self): self.assertNotEqual(self.page[:2], b'\x88\x99') self.page[:2] = bytes([0x88, 0x99]) self.assertEqual(self.page[:2], b'\x88\x99') self.page[:2] = bytes([0x10, 0x20]) self.assertEqual(self.page[:2], b'\x10\x20') self.page[:2] = b'\x11\x21' self.assertEqual(self.page[:2], b'\x11\x21') self.page[:2] = [0x12, 0x22] self.assertEqual(self.page[:2], b'\x12\x22') class C: def __iter__(self): return next(self) def __next__(self): yield 0x13 yield 0x23 self.page[:2] = C() self.assertEqual(self.page[:2], b'\x13\x23') @unittest.skip('item assignment is not implemented') def test_set_bytes_invalid_value(self): for t in (str, complex, float, set, list, tuple, dict): with self.assertRaises(ValueError): self.page[0] = t() with self.assertRaises(ValueError): self.page[0] = 256 with self.assertRaises(ValueError): self.page[0] = -1 @unittest.skip('item assignment is not implemented') def test_set_bytes_invalid_index(self): for t in (str, complex, float, set, list, tuple, dict): with self.assertRaises(TypeError): self.page[t()] = 0 with self.assertRaises(ValueError): self.page[5] = 0 def test_invalid_index(self): for t in (str, list, set, dict, complex, object): with self.assertRaises(TypeError): self.page[t()] def test_to_hex(self): hexstr = self.page.to_hex() self.assertEqual(hexstr, '01020304') def test_to_int(self): value = self.page.to_int() self.assertEqual(value, 0x01020304) def test_length(self): self.assertEqual(len(self.page), 4) def test_init_invalid_page(self): card = MFUCard() with self.assertRaises(ValueError): MFUPage(card, -1) with self.assertRaises(ValueError): MFUPage(card, 16) def test_init_invalid_card(self): card = object() with self.assertRaises(TypeError): MFUPage(card, 0) def test_readonly(self): card = MFUCard() pages = [MFUPage(card, i) for i in range(16)] for p in (0, 1): self.assertTrue(pages[p].readonly) for p in range(2, 16): self.assertFalse(pages[p].readonly) card = MFUCard(bytes= b'\x00\x00\x00\x00' * 2 + # lock bytes value = 0x55aa # meaning: pages 5, 7, 8, 10, 12, 14 are LOCKED # pages 4, 6, 9, 11, 13, 15 are not locked # otp locking protection is off # pages 9-4 locking protection is ON # pages 15-10 locking protection is off # otp area is LOCKED b'\x00\x00\xaa\x55' + b'\x00\x00\x00\x00' * 13 ) pages = [MFUPage(card, i) for i in range(16)] for p in (0, 1): # readonly pages self.assertTrue(pages[p].readonly) for p in (5, 7, 8, 10, 12, 14): # locked pages self.assertTrue(pages[p].readonly) for p in (4, 6, 9, 11, 13, 15): # pages not locked self.assertFalse(pages[p].readonly)