def test_reach_to_end_with_shuffle(self): # TODO why some are NC of NC and some NC of NODs? node_chain = PRT.build_from_string('T', None) + \ PRT.build_from_string('h', None) + \ PRT.build_from_string('e', None) + \ PRT.build_from_string('A', None) + \ PRT.build_from_string('n', None) + \ PRT.build_from_string('o', None) + \ PRT.build_from_string('s', None) + \ PRT.build_from_string('m', None) + \ PRT.build_from_string('i', None) + \ PRT.build_from_string('c', None) last = node_chain[-1] atoms = break_to_atoms(node_chain) atoms = add_jmp_opcodes(atoms) atoms = shuffle(atoms) atom = atoms[0] for _ in range(len(node_chain) - 1): next_atom = travel(atoms, atom) if next_atom: atom = next_atom else: self.fail("Chain ended too soon") self.assertIn(last, atom)
def test_linker_address(self): pn = PRT() pn2 = PRT() pn3 = PRT() nc = pn + pn2 + pn3 self.assertRaises(AttributeError, getattr, pn, 'address') link_to_real_address(nc) self.assertEqual(pn.address, 0) self.assertEqual(pn2.address, 8) self.assertEqual(pn3.address, 16)
def test_fix_arg(self): pn = PRT() pn2 = PRT() jn = JMP(pn2) arg = Int(0) nc = pn + pn2 + jn + arg self.assertEqual(arg.build(), '\x00\x00\x00\x00') naddresses = {pn2.uuid: 8} ni = iter(nc) ni.next() # pn ni.next() # pn2 ni.next() # jn self.assertEqual(arg.build(), '\x00\x00\x00\x00') jn.fix_arg(naddresses, ni) self.assertEqual(arg.build(), Int(8).build())
def test_copy(self): n = Byte(0xFF) n2 = PRT() nc = n + n2 nc2 = nc.copy() nc2.append(Byte(0x30)) self.assertEqual(len(nc), 2) self.assertEqual(len(nc2), 3)
def test_nothing_happend_on_one_and_no_jmp_init(self): atom = PRT.build_from_string('i', None) atoms = break_to_atoms(atom) atoms = add_jmp_opcodes(atoms, first_step_is_jmp=False) self.assertEqual(atoms[0][0], atom[0]) self.assertEqual(atoms[0][1], atom[1]) self.assertEqual(len(atoms), 1) self.assertEqual(len(atoms[0]), 2)
def test_added_init_jmp(self): node_chain = PRT.build_from_string('u', None) atoms = break_to_atoms(node_chain) atoms = add_jmp_opcodes(atoms) self.assertEqual(len(atoms), 2) self.assertEqual(len(atoms[0]), 2) self.assertEqual(len(atoms[1]), 2)
def test_linker_jmp_reverse_order(self): pn = PRT() jn = JMP(pn) nc = jn + Int(0) + pn self.assertRaises(AttributeError, getattr, jn, 'address') self.assertRaises(AttributeError, getattr, jn, 'other_node_address') link_to_real_address(nc) self.assertEqual(40, jn.other_node_address)
def test_fix_arg_raise_index_error(self): pn = PRT() jn = JMP(pn) nc = pn + jn naddresses = {pn.uuid: 8} ni = iter(nc) ni.next() # pn ni.next() # jn self.assertRaises(IndexError, jn.fix_arg, naddresses, ni)
def test_linker_jmp(self): pn = PRT() jn = JMP(pn) i = Int(0) nc = pn + jn + i self.assertRaises(AttributeError, getattr, jn, 'address') self.assertRaises(AttributeError, getattr, jn, 'other_node_address') link_to_real_address(nc) self.assertEqual(0, jn.other_node_address)
def test_fix_arg_raise_value_error_on_arg_size_mismatch(self): pn = PRT() jn = JMP(pn) bn = Byte(0) nc = pn + jn + bn naddresses = {pn.uuid: 8} ni = iter(nc) ni.next() # pn ni.next() # jn self.assertRaises(ValueError, jn.fix_arg, naddresses, ni)
def test_add_node_chain(self): n = PRT() n2 = Byte(0x20) nc = n + n2 n3 = Byte(0x10) n4 = Byte(0x20) nc2 = n3 + n4 nc3 = nc + nc2 self.assertIsInstance(nc3, NodeChain) self.assertIsInstance(nc3[0], Node) self.assertNotIsInstance(nc3[0], NodeChain)
def test_contains_not_supported_type(self): byte = Byte(0x40) nc = PRT() + byte self.assertRaises(NotImplementedError, nc.__contains__, 'TheAnosmic')
def test_not_contains_node(self): byte = Byte(0x40) nc = PRT() + byte self.assertNotIn(PRT(), nc)
def test_not_contains_uuid(self): byte = Byte(0x40) nc = PRT() + byte self.assertNotIn(uuid4(), nc)
def test_contains_node(self): byte = Byte(0x40) nc = PRT() + byte self.assertIn(byte, nc)
def test_break_to_one(self): nc = PRT.build_from_string('t', None) broken = break_to_atoms(nc) self.assertEqual(len(broken), 1)
def test_prt_code(self): op = PRT.build_from_string('\x10', None) exp = chr(PRT.CODE) + chr(0x10) self.assertEqual(op.build(), exp)
def test_prt_name(self): self.assertEqual(PRT().NAME, 'PRT')
def test_break_to_one_with_too_small_arg_size(self): nc = JMP(PRT()) + Byte(100) self.assertRaises(ValueError, break_to_atoms, nc)
def test_first_jmp_points_to_first_node(self): atom = PRT.build_from_string('o', None) first_node = atom[0] atoms = break_to_atoms(atom) atoms = add_jmp_opcodes(atoms) self.assertEqual(atoms[0][0].target_uuid, first_node.uuid)
def test_break_to_one_and_one_byte(self): prt = PRT() nc = JMP(prt) + Int(100) + prt + Byte(255) broken = break_to_atoms(nc) self.assertEqual(len(broken), 2)
def test_break_to_one_int(self): nc = JMP(PRT()) + Int(0xFFFFFFFF) broken = break_to_atoms(nc) self.assertEqual(len(broken), 1)
def test_not_found_node(self): byte = Byte(0x40) nc = PRT() + byte self.assertRaises(IndexError, nc.find, PRT())
def test_break_to_one_with_too_big_arg_size(self): nc = PRT() + Int(100) self.assertRaises(ValueError, break_to_atoms, nc)
def test_find_node(self): byte = Byte(0x40) nc = PRT() + byte found = nc.find(byte) self.assertEqual(found, byte)
def test_break_to_two(self): nc = PRT.build_from_string('t', None) nc2 = PRT.build_from_string('q', None) nc.extend(nc2) broken = break_to_atoms(nc) self.assertEqual(len(broken), 2)