1
1
# pylint: disable=missing-class-docstring,no-self-use
2
2
from __future__ import annotations
3
3
4
+ import re
4
5
import unittest
5
6
6
7
import claripy
@@ -12,6 +13,19 @@ class TestStrings(unittest.TestCase):
12
13
def get_solver (self ):
13
14
return claripy .SolverStrings (backend = claripy .backends .z3 )
14
15
16
+ def replace_unicode (self , match ):
17
+ """
18
+ Converts a Unicode escape sequence to the corresponding character.
19
+
20
+ Args:
21
+ - match (re.Match): A regex match object containing a Unicode escape sequence.
22
+
23
+ Returns:
24
+ - str: The Unicode character corresponding to the hexadecimal code.
25
+ """
26
+ hex_code = match .group (1 )
27
+ return chr (int (hex_code , 16 ))
28
+
15
29
def test_concat (self ):
16
30
str_concrete = claripy .StringV ("conc" )
17
31
str_symbol = claripy .StringS ("symb_concat" , 4 , explicit_name = True )
@@ -268,7 +282,6 @@ def test_index_of_simplification(self):
268
282
self .assertEqual ((), tuple (solver .constraints ))
269
283
self .assertEqual ((target_idx ,), solver .eval (res , 2 ))
270
284
271
- @unittest .skip ("Usually hangs" )
272
285
def test_index_of_symbolic_start_idx (self ):
273
286
str_symb = claripy .StringS ("symb_index_of" , 4 , explicit_name = True )
274
287
start_idx = claripy .BVS ("symb_start_idx" , 32 , explicit_name = True )
@@ -281,12 +294,14 @@ def test_index_of_symbolic_start_idx(self):
281
294
282
295
solver .add (res != - 1 )
283
296
solver .add (res < 38 )
297
+
284
298
self .assertTrue (solver .satisfiable ())
285
- self .assertEqual ({33 , 34 , 35 , 36 , 37 }, set (solver .eval (res , 10 )))
299
+ self .assertEqual ({33 , 34 , 35 , 36 , 37 }, set (solver .eval (res , 5 )))
286
300
287
301
strs = solver .eval (str_symb , 10 if KEEP_TEST_PERFORMANT else 100 )
288
302
for s in strs :
289
- self .assertTrue (32 < s .index ("an" ) < 38 )
303
+ converted_string = re .sub (r"\\u\{([0-9a-fA-F]+)\}" , self .replace_unicode , s )
304
+ self .assertTrue (32 < converted_string .index ("an" ) < 38 )
290
305
291
306
def test_str_to_int (self ):
292
307
str_symb = claripy .StringS ("symb_strtoint" , 4 , explicit_name = True )
0 commit comments