def test_decode_v2_zero_bits(self): code = [int(bit) for bit in self.v2_codes[0]] for bit in [0, 1, 40, 41]: broken_code = code.copy() broken_code[bit] = 1 with self.assertRaisesRegex( ValueError, "First two bits of packet were not zero"): secplus.decode_v2(broken_code)
def test_decode_v2_invalid_ternary(self): code = [int(bit) for bit in self.v2_codes[0]] for bit in [2, 4, 6, 8, 42, 44, 46, 48]: broken_code = code.copy() broken_code[bit] = 1 broken_code[bit + 1] = 1 with self.assertRaisesRegex(ValueError, "Illegal value for ternary bit"): secplus.decode_v2(broken_code)
def test_decode_v2_invalid_rolling_code(self): code = "00101010011101111000010100011000010011100010101001110100011110110010110001000101" code = [int(bit) for bit in code] broken_code = code.copy() for bit in [12, 18, 24, 30, 36, 52, 58, 64, 70, 76]: broken_code[bit] = 1 broken_code[bit + 3] = 0 with self.assertRaisesRegex(ValueError, "Rolling code was not in expected range"): secplus.decode_v2(broken_code)
def test_decode_v2_invalid_ternary_2(self): code = "00101010011101111000010100011000010011100010101001110100011110110010110001000101" code = [int(bit) for bit in code] for bit in [12, 18, 24, 30, 36, 52, 58, 64, 70, 76]: broken_code = code.copy() broken_code[bit] = 1 broken_code[bit + 3] = 1 with self.assertRaisesRegex(ValueError, "Illegal value for ternary bit"): secplus.decode_v2(broken_code)
def process_buffer(self): manchester = "".join(str(b) for b in self.buffer) start = manchester.find("1010101010101010101010101010101001010101") if start == -1: return manchester = manchester[start:start+124] baseband = [] for i in range(0, len(manchester), 2): if manchester[i:i+2] == "01": baseband.append(1) elif manchester[i:i+2] == "10": baseband.append(0) else: return if baseband[21] == 0: self.pair = baseband[22:] elif baseband[21] == 1 and len(self.pair) == 40: self.pair += baseband[22:] if len(self.pair) == 80 and self.pair != self.last_pair: try: rolling, fixed = secplus.decode_v2(self.pair) print(secplus.pretty_v2(rolling, fixed)) self.last_pair = self.pair except ValueError: pass
def test_decode_v2(self): for code, rolling, fixed in zip(self.v2_codes, self.v2_rolling_list, self.v2_fixed_list): code = [int(bit) for bit in code] rolling_out, fixed_out = secplus.decode_v2(code) self.assertEqual(rolling, rolling_out) self.assertEqual(fixed, fixed_out)
def test_decode_v2_robustness(self): for _ in range(20000): random_code = [random.randrange(2) for _ in range(80)] try: rolling, fixed = secplus.decode_v2(random_code) self.assertLess(rolling, 2**28) self.assertLess(fixed, 2**40) except ValueError: pass
def test_encode_v2_decode_v2(self): for _ in range(20000): rolling = random.randrange(2**28) fixed = random.randrange(2**40) rolling_out, fixed_out = secplus.decode_v2( secplus.encode_v2(rolling, fixed)) self.assertEqual(rolling, rolling_out) self.assertEqual(fixed, fixed_out)