Esempio n. 1
0
def test_tourniquet_extract_ast(test_files, tmp_db):
    # No DB name for now
    test_extractor = Tourniquet(tmp_db)
    test_file = test_files / "patch_test.c"
    ast_dict: dict = test_extractor._extract_ast(test_file)

    # The dictionary produced from the AST has three top-level keys:
    # module_name, globals, and functions.
    assert "module_name" in ast_dict
    assert "globals" in ast_dict
    assert "functions" in ast_dict

    # The module name is our source file.
    assert ast_dict["module_name"] == str(test_file)

    # Everything in globals is a "var_type".
    assert all(global_[0] == "var_type" for global_ in ast_dict["globals"])

    # There's at least one global named "pass".
    assert any(global_[5] == "pass" for global_ in ast_dict["globals"])

    # There's a "main" function in the "functions" dictionary.
    assert "main" in ast_dict["functions"]
    main = ast_dict["functions"]["main"]

    # There are 4 variables in "main", plus "argc" and "argv".
    main_vars = [var_decl[5] for var_decl in main if var_decl[0] == "var_type"]
    assert set(main_vars) == {"argc", "argv", "buff", "buff_len", "pov", "len"}
Esempio n. 2
0
def test_tourniquet_extract_ast_invalid_file(test_files, tmp_db):
    test_extractor = Tourniquet(tmp_db)
    test_file = test_files / "does-not-exist"
    with pytest.raises(FileNotFoundError):
        test_extractor._extract_ast(test_file)