diff --git a/tests/test_strings.py b/tests/test_strings.py index eab801360..fe6aca2a6 100644 --- a/tests/test_strings.py +++ b/tests/test_strings.py @@ -1,6 +1,7 @@ # pylint: disable=missing-class-docstring,no-self-use from __future__ import annotations +import re import unittest import claripy @@ -12,6 +13,19 @@ class TestStrings(unittest.TestCase): def get_solver(self): return claripy.SolverStrings(backend=claripy.backends.z3) + def replace_unicode(self, match): + """ + Converts a Unicode escape sequence to the corresponding character. + + Args: + - match (re.Match): A regex match object containing a Unicode escape sequence. + + Returns: + - str: The Unicode character corresponding to the hexadecimal code. + """ + hex_code = match.group(1) + return chr(int(hex_code, 16)) + def test_concat(self): str_concrete = claripy.StringV("conc") str_symbol = claripy.StringS("symb_concat", 4, explicit_name=True) @@ -268,7 +282,6 @@ def test_index_of_simplification(self): self.assertEqual((), tuple(solver.constraints)) self.assertEqual((target_idx,), solver.eval(res, 2)) - @unittest.skip("Usually hangs") def test_index_of_symbolic_start_idx(self): str_symb = claripy.StringS("symb_index_of", 4, explicit_name=True) start_idx = claripy.BVS("symb_start_idx", 32, explicit_name=True) @@ -281,12 +294,14 @@ def test_index_of_symbolic_start_idx(self): solver.add(res != -1) solver.add(res < 38) + self.assertTrue(solver.satisfiable()) - self.assertEqual({33, 34, 35, 36, 37}, set(solver.eval(res, 10))) + self.assertEqual({33, 34, 35, 36, 37}, set(solver.eval(res, 5))) strs = solver.eval(str_symb, 10 if KEEP_TEST_PERFORMANT else 100) for s in strs: - self.assertTrue(32 < s.index("an") < 38) + converted_string = re.sub(r"\\u\{([0-9a-fA-F]+)\}", self.replace_unicode, s) + self.assertTrue(32 < converted_string.index("an") < 38) def test_str_to_int(self): str_symb = claripy.StringS("symb_strtoint", 4, explicit_name=True)