def test_auto_patch(test_files, tmp_db): tourniquet = Tourniquet(tmp_db) test_file = test_files / "patch_test.c" tourniquet.collect_info(test_file) class DummyErrorAnalysis(Expression): def __init__(self, expr): self.lit = Lit(expr) def concretize(self, db, location): yield from self.lit.concretize(db, location) class DummyCallable: def __init__(self, unused): self._unused = unused def __call__(self, line, col): return line == 32 and col == 3 location = L(test_file, SC(32, 3)) template = PatchTemplate( FixPattern( IfStmt(LessThanExpr(Lit("len"), Lit("buff_len")), NodeStmt()), ElseStmt(ReturnStmt(DummyErrorAnalysis("1"))), ), DummyCallable("unused"), ) tourniquet.register_template("buffer_guard", template) assert ( tourniquet.auto_patch( "buffer_guard", [("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 1), ("password", 0)], location, ) is not None )
def test_fixpattern(): fp = FixPattern(IfStmt(Lit("1"), Lit("exit(1);")), ElseStmt(Lit("exit(2);"))) concretized = list(fp.concretize(None, None)) assert len(concretized) == 1 assert concretized[0] == "if (1) {\nexit(1);\n}\n\nelse {\nexit(2);\n}\n"
def test_concretize_returnstmt(): rets = ReturnStmt(Lit("foo")) assert set(rets.concretize(None, None)) == {"return foo;"}
def test_concretize_elsestmt(): elses = ElseStmt(Lit("bark;")) assert set(elses.concretize(None, None)) == {"else {\nbark;\n}\n"}
def test_concretize_ifstmt(): ifs = IfStmt(Lit("1"), Lit("bark;")) assert set(ifs.concretize(None, None)) == {"if (1) {\nbark;\n}\n"}
def test_concretize_lessthanexpr(): lte = LessThanExpr(Lit("1"), Lit("2")) concretized = set(lte.concretize(None, None)) assert concretized == {"1 < 2"}
def test_concretize_binarybooloperator(): bbo = BinaryBoolOperator(Lit("1"), Lit("2")) concretized = set(bbo.concretize(None, None)) assert concretized == { "1 == 2", "1 != 2", "1 <= 2", "1 < 2", "1 >= 2", "1 > 2" }
def test_concretize_binarymathoperator(): bmo = BinaryMathOperator(Lit("1"), Lit("2")) concretized = set(bmo.concretize(None, None)) assert concretized == {"1 + 2", "1 - 2", "1 / 2", "1 * 2", "1 << 2"}
def test_concretize_lit(): lit = Lit("1") assert set(lit.concretize(None, None)) == {"1"}
class DummyErrorAnalysis(Expression): def __init__(self, expr): self.lit = Lit(expr) def concretize(self, db, location): yield from self.lit.concretize(db, location)
def __init__(self, expr): self.lit = Lit(expr)