Skip to content

Commit

Permalink
Fix string test
Browse files Browse the repository at this point in the history
  • Loading branch information
spshah1701 committed Aug 29, 2024
1 parent b5d2e1b commit 698a7e1
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions tests/test_strings.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# pylint: disable=missing-class-docstring,no-self-use
from __future__ import annotations

import re
import unittest

import claripy
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 698a7e1

Please sign in to comment.