Skip to content

Commit

Permalink
sarif: add rountrip tests
Browse files Browse the repository at this point in the history
  • Loading branch information
frabert authored and xlauko committed Aug 1, 2024
1 parent 5fd613f commit e5136e8
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 0 deletions.
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ endfunction()
add_subdirectory(core)
add_subdirectory(coro)
add_subdirectory(graph)
add_subdirectory(sarif)

if (${GAP_ENABLE_MLIR})
add_subdirectory(mlir)
Expand Down
10 changes: 10 additions & 0 deletions test/sarif/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Copyright 2024, Trail of Bits, Inc. All rights reserved.

add_gap_test(test-gap-sarif
sarif.cpp
)

target_link_libraries(test-gap-sarif
PUBLIC
gap-sarif
)
143 changes: 143 additions & 0 deletions test/sarif/sarif.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
// Copyright (c) 2024-present, Trail of Bits, Inc.

#include <doctest/doctest.h>
#include <gap/sarif/sarif.hpp>

namespace gap::sarif {
TEST_CASE("Struct to JSON roundtrip") {
// Sample SARIF output from https://github.com/microsoft/sarif-tutorials/blob/main/docs/1-Introduction.md
root obj{
.version = version::k2_1_0,
.runs = {
{
.tool = {
.driver = {
.name = "ESLint",
.informationUri = "https://eslint.org",
.rules = {
{
.id = "no-unused-vars",
.shortDescription = {{
.text = "disallow unused variables",
}},
.helpUri = "https://eslint.org/docs/rules/no-unused-vars",
.properties = {{
.additional_properties = {
{"category", "Variable"},
},
}},
}
},
},
},
.artifacts = {
{
.location = {{
.uri = "file:///C:/dev/sarif/sarif-tutorials/samples/Introduction/simple-example.js",
}},
},
},
.results = {{
{
.ruleId = "no-unused-vars",
.ruleIndex = 0,
.level = level::kError,
.message = {
.text = "'x' is assigned a value but never used.",
},
.locations = {{
{
.physicalLocation = {{
.artifactLocation = {{
.uri = "file:///C:/dev/sarif/sarif-tutorials/samples/Introduction/simple-example.js",
.index = 0,
}},
}},
.properties = {{
.additional_properties = {
{"region", {
{"startLine", 1},
{"startColumn", 5},
}},
},
}},
},
}},
},
}},
}
},
};

json obj_json = obj;
root deser = obj;
json deser_json = deser;

CHECK_EQ(obj_json, deser_json);
}

TEST_CASE("JSON to struct roundtrip") {
auto j = json::parse(R"({
"version": "2.1.0",
"runs": [
{
"tool": {
"driver": {
"name": "ESLint",
"informationUri": "https://eslint.org",
"rules": [
{
"id": "no-unused-vars",
"shortDescription": {
"text": "disallow unused variables"
},
"helpUri": "https://eslint.org/docs/rules/no-unused-vars",
"properties": {
"category": "Variables"
}
}
]
}
},
"artifacts": [
{
"location": {
"uri": "file:///C:/dev/sarif/sarif-tutorials/samples/Introduction/simple-example.js"
}
}
],
"results": [
{
"level": "error",
"message": {
"text": "'x' is assigned a value but never used."
},
"locations": [
{
"physicalLocation": {
"artifactLocation": {
"uri": "file:///C:/dev/sarif/sarif-tutorials/samples/Introduction/simple-example.js",
"index": 0
},
"region": {
"startLine": 1,
"startColumn": 5
}
}
}
],
"ruleId": "no-unused-vars",
"ruleIndex": 0
}
]
}
]
})");

root root = j;

json j2 = root;

CHECK_EQ(j, j2);
}
}

0 comments on commit e5136e8

Please sign in to comment.