From f61614feab663a0e68fe4e3bce33600ec1bdfefd Mon Sep 17 00:00:00 2001 From: Francesco Bertolaccini Date: Thu, 1 Aug 2024 14:02:44 +0200 Subject: [PATCH] sarif: add rountrip tests --- test/CMakeLists.txt | 1 + test/sarif/CMakeLists.txt | 10 +++ test/sarif/sarif.cpp | 143 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 154 insertions(+) create mode 100644 test/sarif/CMakeLists.txt create mode 100644 test/sarif/sarif.cpp diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 4c3aebe..ed69d20 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -34,6 +34,7 @@ endfunction() add_subdirectory(core) add_subdirectory(coro) add_subdirectory(graph) +add_subdirectory(sarif) if (${GAP_ENABLE_MLIR}) add_subdirectory(mlir) diff --git a/test/sarif/CMakeLists.txt b/test/sarif/CMakeLists.txt new file mode 100644 index 0000000..fe0b48c --- /dev/null +++ b/test/sarif/CMakeLists.txt @@ -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 +) \ No newline at end of file diff --git a/test/sarif/sarif.cpp b/test/sarif/sarif.cpp new file mode 100644 index 0000000..96cbda4 --- /dev/null +++ b/test/sarif/sarif.cpp @@ -0,0 +1,143 @@ +// Copyright (c) 2024-present, Trail of Bits, Inc. + +#include +#include + +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); + } +} \ No newline at end of file