예제 #1
0
    def test3hex_machine_too_long_value(self):
        """Test the hex_machine with too long hex values. All values longer than 4 digits are stripped."""
        def check_value(data):  # skipcq: PY-D0003
            self.assertEqual(data, i)

        # only 00FF is read.
        i = 255
        string = b'0x00FFFF'[2:]  # remove 0x
        state = hex_machine(check_value)
        j = 0
        for j, c in enumerate(string):
            state = state(c)
            if state is None:
                break
        self.assertEqual(j, 3)
        self.assertIsNone(state)

        # only 0F12 is read.
        i = 3858
        string = b'0x0F1234'[2:]  # remove 0x
        state = hex_machine(check_value)
        j = 0
        for j, c in enumerate(string):
            state = state(c)
            if state is None:
                break
        self.assertEqual(j, 3)
        self.assertIsNone(state)
예제 #2
0
    def test1hex_machine_valid_values(self):
        """Test the hex_machine with all valid four digit values from 0x0000 to 0xFFFF."""
        def check_value(data):  # skipcq: PY-D0003
            self.assertEqual(data, i)

        for i in range(65536):
            string = str(format(i, '#06x')).encode()[2:]  # remove 0x
            state = hex_machine(check_value)
            for c in string:
                state = state(c)
            self.assertIsNone(state)

        for i in range(65536):
            string = str(format(i, '#06x')).upper().encode()[2:]  # remove 0x
            state = hex_machine(check_value)
            for c in string:
                state = state(c)
            self.assertIsNone(state)
예제 #3
0
    def test2hex_machine_too_short_value(self):
        """Test the hex_machine with too short hex values."""
        def check_value(data):  # skipcq: PY-D0003
            self.assertEqual(data, i)

        for i in range(4096):
            # converts the integer to the shortest possible hex string.
            string = str(hex(i)).encode()[2:]  # remove 0x
            state = hex_machine(check_value)
            for c in string:
                state = state(c)

        i = 4096
        string = str(hex(i)).encode()[2:]  # remove 0x
        state = hex_machine(check_value)
        for c in string:
            state = state(c)
        self.assertIsNone(state)
예제 #4
0
    def test4hex_machine_boundary_values(self):
        """Test boundary values before and after 0-9, a-f, A-F."""
        def check_value(data):  # skipcq: PY-D0003
            self.assertEqual(data, i)

        allowed_value_list = '0123456789abcdefABCDEF'
        forbidden_value_list = [int(hex(j), 16) for j in range(48)] + [
            int(hex(j), 16) for j in range(58, 65)
        ] + [int(hex(j), 16) for j in range(71, 97)
             ] + [int(hex(j), 16) for j in range(103, 128)]
        for a in allowed_value_list:
            state = hex_machine(check_value)
            string = '0x' + a + a + a + a
            i = int(string, 16)  # convert hex string to integer
            for _ in range(4):
                state = state(ord(a))
            self.assertEqual(state, None)

        for f in forbidden_value_list:
            state = hex_machine(check_value)
            self.assertIsNone(
                state(f),
                "value: %d, char: '%s' should not be allowed in the hex_machine!"
                % (f, chr(f)))