def test_concrete_calldata_calldatasize(): # Arrange calldata = ConcreteCalldata(0, [1, 4, 7, 3, 7, 2, 9]) solver = Solver() # Act solver.check() model = solver.model() result = model.eval(calldata.calldatasize.raw) # Assert assert result == 7
def test_concrete_calldata_constrain_index(): # Arrange calldata = ConcreteCalldata(0, [1, 4, 7, 3, 7, 2, 9]) solver = Solver() # Act value = calldata[2] constraint = value == 3 solver.add([constraint]) result = solver.check() # Assert assert str(result) == "unsat"
def test_symbolic_calldata_constrain_index(): # Arrange calldata = SymbolicCalldata(0) solver = Solver() # Act value = calldata[51] constraints = [value == 1, calldata.calldatasize == 50] solver.add(constraints) result = solver.check() # Assert assert str(result) == "unsat"
def test_bitvecfunc_ext_unequal_nested_comparison_f(): # Arrange s = Solver() input1 = symbol_factory.BitVecSym("input1", 256) input2 = symbol_factory.BitVecSym("input2", 256) input3 = symbol_factory.BitVecSym("input3", 256) input4 = symbol_factory.BitVecSym("input4", 256) bvf1 = symbol_factory.BitVecFuncSym("bvf1", "sha3", 256, input_=input1) bvf2 = symbol_factory.BitVecFuncSym("bvf2", "sha3", 256, input_=bvf1 + input3) bvf3 = symbol_factory.BitVecFuncSym("bvf3", "sha3", 256, input_=input2) bvf4 = symbol_factory.BitVecFuncSym("bvf4", "sha3", 256, input_=bvf3 + input4) # Act s.add(input1 != input2) s.add(input3 == input4) s.add(bvf2 == bvf4) # Assert assert s.check() == z3.unsat
def is_possible(self) -> bool: """ :return: True/False based on the existence of solution of constraints """ if self._is_possible is not None: return self._is_possible solver = Solver() solver.set_timeout(self._default_timeout) for constraint in self[:]: solver.add(constraint) self._is_possible = solver.check() != unsat return self._is_possible
def test_bitvecfunc_find_input(): # Arrange s = Solver() input1 = symbol_factory.BitVecSym("input1", 256) input2 = symbol_factory.BitVecSym("input2", 256) bvf1 = symbol_factory.BitVecFuncSym("bvf1", "sha3", 256, input_=input1) bvf2 = symbol_factory.BitVecFuncSym("bvf3", "sha3", 256, input_=input2) # Act s.add(input1 == symbol_factory.BitVecVal(1, 256)) s.add(bvf1 == bvf2) # Assert assert s.check() == z3.sat assert s.model()[input2.raw] == 1
def test_bitvecfunc_bitvecfunc_comparison(operation, expected): # Arrange s = Solver() input1 = symbol_factory.BitVecSym("input1", 256) input2 = symbol_factory.BitVecSym("input2", 256) bvf1 = symbol_factory.BitVecFuncSym("bvf1", "sha3", 256, input_=input1) bvf2 = symbol_factory.BitVecFuncSym("bvf2", "sha3", 256, input_=input2) # Act s.add(operation(bvf1, bvf2)) s.add(input1 == input2) # Assert assert s.check() == expected
def test_symbolic_calldata_equal_indices(): calldata = SymbolicCalldata(0) index_a = symbol_factory.BitVecSym("index_a", 256) index_b = symbol_factory.BitVecSym("index_b", 256) # Act a = calldata[index_a] b = calldata[index_b] s = Solver() s.append(index_a == index_b) s.append(a != b) # Assert assert unsat == s.check()
def test_bitvecfunc_arithmetic(operation, expected): # Arrange s = Solver() input_ = symbol_factory.BitVecVal(1, 8) bvf = symbol_factory.BitVecFuncSym("bvf", "sha3", 256, input_=input_) x = symbol_factory.BitVecSym("x", 256) y = symbol_factory.BitVecSym("y", 256) # Act s.add(x != y) s.add(operation(bvf, x) == operation(y, bvf)) # Assert assert s.check() == expected
def test_bitvecfunc_nested_comparison(): # arrange s = Solver() input1 = symbol_factory.BitVecSym("input1", 256) input2 = symbol_factory.BitVecSym("input2", 256) bvf1 = symbol_factory.BitVecFuncSym("bvf1", "sha3", 256, input_=input1) bvf2 = symbol_factory.BitVecFuncSym("bvf2", "sha3", 256, input_=bvf1) bvf3 = symbol_factory.BitVecFuncSym("bvf3", "sha3", 256, input_=input2) bvf4 = symbol_factory.BitVecFuncSym("bvf4", "sha3", 256, input_=bvf3) # Act s.add(input1 == input2) s.add(bvf2 == bvf4) # Assert assert s.check() == z3.sat
def test_as_long(): # Arrange solver = Solver() x = symbol_factory.BitVecSym("x", 256) expression = x == symbol_factory.BitVecVal(2, 256) # Act solver.add(expression) result = solver.check() model = solver.model() x_concrete = model.eval(x.raw).as_long() # Assert assert z3.sat == result assert 2 == x_concrete
def test_decls(): # Arrange solver = Solver() x = symbol_factory.BitVecSym("x", 256) expression = x == symbol_factory.BitVecVal(2, 256) # Act solver.add(expression) result = solver.check() model = solver.model() decls = model.decls() # Assert assert z3.sat == result assert x.raw.decl() in decls
def test_get_item(): # Arrange solver = Solver() x = symbol_factory.BitVecSym("x", 256) expression = x == symbol_factory.BitVecVal(2, 256) # Act solver.add(expression) result = solver.check() model = solver.model() x_concrete = model[x.raw.decl()] # Assert assert z3.sat == result assert 2 == x_concrete
def test_bitvecfunc_bitvecfuncval_comparison(): # Arrange s = Solver() input1 = symbol_factory.BitVecSym("input1", 256) input2 = symbol_factory.BitVecVal(1337, 256) bvf1 = symbol_factory.BitVecFuncSym("bvf1", "sha3", 256, input_=input1) bvf2 = symbol_factory.BitVecFuncVal(12345678910, "sha3", 256, input_=input2) # Act s.add(bvf1 == bvf2) # Assert assert s.check() == z3.sat assert s.model().eval(input2.raw) == 1337