Example #1
0
    def test7utf8_machine_forbidden_2_byte_boundary_values(self):
        """Test all boundary values for 2 byte utf8 values."""
        def raise_error(_):  # skipcq: PY-D0003
            raise Exception("Valid UTF-8 value found in boundary test!")

        self.assertIsNone(utf8_machine(191, raise_error))
        self.assertIsNone(utf8_machine(192, raise_error)(127))
        self.assertIsNone(utf8_machine(192, raise_error)(192))
        self.assertRaises(Exception, utf8_machine(192, raise_error), 128)
        self.assertRaises(Exception, utf8_machine(192, raise_error), 191)
Example #2
0
    def test6utf8_machine_allowed_2_byte_values(self):
        """
        Test all allowed values for the utf8_machine with 2 byte values. Only every 4th value is checked to save time.
        This can be changed by changing the step variable. When checking every 4th value the boundary values are also checked.
        """
        def check_value_hex2(data):  # skipcq: PY-D0003
            self.assertEqual(data, (i - 194) * 64 + j)

        step = 4
        for i in range(192, 224):
            for j in range(128, 192, step):
                state = utf8_machine(i, check_value_hex2)
                state = state(j)
        # check if the state is None only once to save time.
        self.assertIsNone(state)
Example #3
0
    def test10utf8_machine_allowed_4_byte_values(self):
        """
        Test all allowed values for the utf8_machine with 4 byte values. Only every 4th value is checked to save time.
        This can be changed by changing the step variable. When checking every 4th value the boundary values are also checked.
        """
        def check_value_hex4(data):  # skipcq: PY-D0003
            self.assertEqual(data, (i - 240) * 64 * 64 * 64 +
                             (j - 128) * 64 * 64 + (k - 128) * 64 + m - 128)

        step = 4
        for i in range(240, 248):
            for j in range(128, 192, step):
                for k in range(128, 192, step):
                    for m in range(128, 192, step):
                        state = utf8_machine(i, check_value_hex4)
                        state = state(j)
                        state = state(k)
                        state = state(m)
        # check if the state is None only once to save time.
        self.assertIsNone(state)