requested changes applied

This commit is contained in:
Katarzyna Miernikiewicz 2020-09-28 14:33:43 +00:00
parent ac7c1ee1ee
commit e411799d0d
7 changed files with 166 additions and 161 deletions

View File

@ -22,7 +22,7 @@ add_custom_command(
COMMAND mv ${PROJECT_BINARY_DIR}/gen_files/jsonnet.cpp ${PROJECT_BINARY_DIR}/gen_files/write_helper.cc COMMAND mv ${PROJECT_BINARY_DIR}/gen_files/jsonnet.cpp ${PROJECT_BINARY_DIR}/gen_files/write_helper.cc
) )
include_directories( list(APPEND JSONNET_SAPI_HEADERS
${PROJECT_SOURCE_DIR} ${PROJECT_SOURCE_DIR}
${PROJECT_SOURCE_DIR}/headers ${PROJECT_SOURCE_DIR}/headers
${PROJECT_BINARY_DIR}/gen_files ${PROJECT_BINARY_DIR}/gen_files
@ -36,6 +36,10 @@ add_library(jsonnet_helper STATIC
${PROJECT_BINARY_DIR}/gen_files/write_helper.cc ${PROJECT_BINARY_DIR}/gen_files/write_helper.cc
) )
target_include_directories(jsonnet_helper PUBLIC
${JSONNET_SAPI_HEADERS}
)
target_link_libraries(jsonnet_helper target_link_libraries(jsonnet_helper
libjsonnet_for_binaries libjsonnet_for_binaries
) )
@ -51,6 +55,10 @@ foreach(exe base multiple_files yaml_stream formatter)
jsonnet_sapi jsonnet_sapi
sapi::sapi sapi::sapi
) )
target_include_directories(jsonnet_${exe}_sandboxed PUBLIC
${JSONNET_SAPI_HEADERS}
)
endforeach() endforeach()
add_executable(jsonnet_base_transacted add_executable(jsonnet_base_transacted

View File

@ -25,7 +25,7 @@ absl::Status JsonnetMain(std::string in_file, std::string out_file) {
JsonnetApi api(&sandbox); JsonnetApi api(&sandbox);
// Initialize library's main structure. // Initialize library's main structure.
SAPI_ASSIGN_OR_RETURN(JsonnetVm * jsonnet_vm, api.c_jsonnet_make()); SAPI_ASSIGN_OR_RETURN(JsonnetVm* jsonnet_vm, api.c_jsonnet_make());
sapi::v::RemotePtr vm_pointer(jsonnet_vm); sapi::v::RemotePtr vm_pointer(jsonnet_vm);
// Read input file. // Read input file.

View File

@ -25,27 +25,28 @@
#include "jsonnet_sapi.sapi.h" #include "jsonnet_sapi.sapi.h"
#include "sandboxed_api/util/flag.h" #include "sandboxed_api/util/flag.h"
#include "sandboxed_api/util/status_matchers.h" #include "sandboxed_api/util/status_matchers.h"
#include "sandboxed_api/sandbox2/util/path.h"
class JsonnetTestHelper { class JsonnetTestHelper {
protected: protected:
enum Evaluation { BASE, MULTIPLE_FILES, YAML_STREAM }; enum Evaluation { kBase, kMultipleFiles, kYamlStream };
void TestSetUp(); void TestSetUp();
void TestTearDown(); void TestTearDown();
void Read_input(char* filename); void Read_input(const char* filename);
void Evaluate_jsonnet_code(char* filename, Evaluation type, void Evaluate_jsonnet_code(Evaluation type,
bool expected_correct); bool expected_correct);
void Write_output(char* filename_or_directory, Evaluation type); void Write_output(const char* filename_or_directory, Evaluation type);
std::string Read_output(char* filename); std::string Read_output(const char* filename);
std::unique_ptr<JsonnetBaseSandbox> sandbox; std::unique_ptr<JsonnetBaseSandbox> sandbox_;
std::unique_ptr<JsonnetApi> api; std::unique_ptr<JsonnetApi> api_;
std::unique_ptr<sapi::v::RemotePtr> input; std::unique_ptr<sapi::v::RemotePtr> input_;
std::unique_ptr<sapi::v::RemotePtr> output; std::unique_ptr<sapi::v::RemotePtr> output_;
std::unique_ptr<sapi::v::RemotePtr> vm; std::unique_ptr<sapi::v::RemotePtr> vm_;
std::string input_filename_in_sandboxee; std::string input_filename_in_sandboxee_;
bool if_jsonnet_vm_was_used; bool if_jsonnet_vm_was_used_;
bool if_input_was_read; bool if_input_was_read_;
}; };

View File

@ -14,7 +14,7 @@
#include "jsonnet_helper.h" // NOLINT(build/include) #include "jsonnet_helper.h" // NOLINT(build/include)
#include <string.h> #include <cstring>
struct JsonnetVm* c_jsonnet_make(void) { struct JsonnetVm* c_jsonnet_make(void) {
return jsonnet_make(); return jsonnet_make();

View File

@ -24,16 +24,16 @@ add_custom_target(test_preparation ALL
COMMAND cp ${PROJECT_SOURCE_DIR}/examples/jsonnet_codes_expected_output/* ${PROJECT_BINARY_DIR}/tests/tests_expected_output COMMAND cp ${PROJECT_SOURCE_DIR}/examples/jsonnet_codes_expected_output/* ${PROJECT_BINARY_DIR}/tests/tests_expected_output
) )
include_directories(
${PROJECT_SOURCE_DIR}/headers
)
add_executable(tests add_executable(tests
${PROJECT_SOURCE_DIR}/headers/jsonnet_tests.h ${PROJECT_SOURCE_DIR}/headers/jsonnet_tests.h
jsonnet_tests.cc jsonnet_tests.cc
jsonnet_tests_utils.cc jsonnet_tests_utils.cc
) )
target_include_directories(tests PUBLIC
${PROJECT_SOURCE_DIR}/headers
)
target_link_libraries(tests target_link_libraries(tests
jsonnet_sapi sapi::sapi jsonnet_sapi sapi::sapi
gtest gmock gtest_main gtest gmock gtest_main

View File

@ -14,95 +14,99 @@
#include "jsonnet_tests.h" #include "jsonnet_tests.h"
class JsonnetTest : public JsonnetTestHelper, public testing::Test { namespace {
class JsonnetTest : public JsonnetTestHelper, public testing::Test {
protected: protected:
void SetUp() override { TestSetUp(); } void SetUp() override { TestSetUp(); }
void TearDown() override { TestTearDown(); } void TearDown() override { TestTearDown(); }
}; };
// Basic test // Basic test
TEST_F(JsonnetTest, SetUp_TearDown) { TEST_F(JsonnetTest, SetUp_TearDown) {
ASSERT_FALSE(if_jsonnet_vm_was_used); ASSERT_FALSE(if_jsonnet_vm_was_used_);
ASSERT_FALSE(if_input_was_read); ASSERT_FALSE(if_input_was_read_);
} }
// One file evaluation to one file // One file evaluation to one file
TEST_F(JsonnetTest, One_file_no_dependencies) { TEST_F(JsonnetTest, One_file_no_dependencies) {
char input_file[] = "arith.jsonnet"; constexpr char kInputFile[] = "arith.jsonnet";
char output_file[] = "arith_output"; constexpr char kOutputFile[] = "arith_output";
char output_to_read[] = "tests_output/arith_output"; constexpr char kOutputToRead[] = "tests_output/arith_output";
char output_to_expect[] = "tests_expected_output/arith.golden"; constexpr char kOutputToExpect[] = "tests_expected_output/arith.golden";
Read_input(input_file); Read_input(kInputFile);
Evaluate_jsonnet_code(input_file, BASE, true); Evaluate_jsonnet_code(kBase, true);
Write_output(output_file, BASE); Write_output(kOutputFile, kBase);
std::string produced_output = Read_output(output_to_read); std::string produced_output = Read_output(kOutputToRead);
std::string expected_output = Read_output(output_to_expect); std::string expected_output = Read_output(kOutputToExpect);
ASSERT_STREQ(produced_output.c_str(), expected_output.c_str()); ASSERT_STREQ(produced_output.c_str(), expected_output.c_str());
} }
// One file evaluating to one file, dependent on some other files // One file evaluating to one file, dependent on some other files
TEST_F(JsonnetTest, One_file_some_dependencies) { TEST_F(JsonnetTest, One_file_some_dependencies) {
char input_file[] = "negroni.jsonnet"; constexpr char kInputFile[] = "negroni.jsonnet";
char output_file[] = "negroni_output"; constexpr char kOutputFile[] = "negroni_output";
char output_to_read[] = "tests_output/negroni_output"; constexpr char kOutputToRead[] = "tests_output/negroni_output";
char output_to_expect[] = "tests_expected_output/negroni.golden"; constexpr char kOutputToExpect[] = "tests_expected_output/negroni.golden";
Read_input(input_file); Read_input(kInputFile);
Evaluate_jsonnet_code(input_file, BASE, true); Evaluate_jsonnet_code(kBase, true);
Write_output(output_file, BASE); Write_output(kOutputFile, kBase);
std::string produced_output = Read_output(output_to_read); const std::string produced_output = Read_output(kOutputToRead);
std::string expected_output = Read_output(output_to_expect); const std::string expected_output = Read_output(kOutputToExpect);
ASSERT_STREQ(produced_output.c_str(), expected_output.c_str()); ASSERT_STREQ(produced_output.c_str(), expected_output.c_str());
} }
// One file evaluating to two files // One file evaluating to two files
TEST_F(JsonnetTest, Multiple_files) { TEST_F(JsonnetTest, Multiple_files) {
char input_file[] = "multiple_files_example.jsonnet"; constexpr char kInputFile[] = "multiple_files_example.jsonnet";
char output_file[] = ""; constexpr char kOutputFile[] = "";
char output_to_read_1[] = "tests_output/first_file.json"; constexpr char kOutputToRead1[] = "tests_output/first_file.json";
char output_to_read_2[] = "tests_output/second_file.json"; constexpr char kOutputToRead2[] = "tests_output/second_file.json";
char output_to_expect_1[] = "tests_expected_output/first_file.json"; constexpr char kOutputToExpect1[] = "tests_expected_output/first_file.json";
char output_to_expect_2[] = "tests_expected_output/second_file.json"; constexpr char kOutputToExpect2[] = "tests_expected_output/second_file.json";
Read_input(input_file); Read_input(kInputFile);
Evaluate_jsonnet_code(input_file, MULTIPLE_FILES, true); Evaluate_jsonnet_code(kMultipleFiles, true);
Write_output(output_file, MULTIPLE_FILES); Write_output(kOutputFile, kMultipleFiles);
std::string produced_output_1 = Read_output(output_to_read_1); const std::string produced_output_1 = Read_output(kOutputToRead1);
std::string produced_output_2 = Read_output(output_to_read_2); const std::string produced_output_2 = Read_output(kOutputToRead2);
std::string expected_output_1 = Read_output(output_to_expect_1); const std::string expected_output_1 = Read_output(kOutputToExpect1);
std::string expected_output_2 = Read_output(output_to_expect_2); const std::string expected_output_2 = Read_output(kOutputToExpect2);
ASSERT_STREQ(produced_output_1.c_str(), expected_output_1.c_str()); ASSERT_STREQ(produced_output_1.c_str(), expected_output_1.c_str());
ASSERT_STREQ(produced_output_2.c_str(), expected_output_2.c_str()); ASSERT_STREQ(produced_output_2.c_str(), expected_output_2.c_str());
} }
// One file evaluating to yaml stream format // One file evaluating to yaml stream format
TEST_F(JsonnetTest, Yaml_stream) { TEST_F(JsonnetTest, Yaml_stream) {
char input_file[] = "yaml_stream_example.jsonnet"; constexpr char kInputFile[] = "yaml_stream_example.jsonnet";
char output_file[] = "yaml_stream_example.yaml"; constexpr char kOutputFile[] = "yaml_stream_example.yaml";
char output_to_read[] = "tests_output/yaml_stream_example.yaml"; constexpr char kOutputToRead[] = "tests_output/yaml_stream_example.yaml";
char output_to_expect[] = "tests_expected_output/yaml_stream_example.yaml"; constexpr char kOutputToExpect[] = "tests_expected_output/yaml_stream_example.yaml";
Read_input(input_file); Read_input(kInputFile);
Evaluate_jsonnet_code(input_file, YAML_STREAM, true); Evaluate_jsonnet_code(kYamlStream, true);
Write_output(output_file, YAML_STREAM); Write_output(kOutputFile, kYamlStream);
std::string produced_output = Read_output(output_to_read); const std::string produced_output = Read_output(kOutputToRead);
std::string expected_output = Read_output(output_to_expect); const std::string expected_output = Read_output(kOutputToExpect);
ASSERT_STREQ(produced_output.c_str(), expected_output.c_str()); ASSERT_STREQ(produced_output.c_str(), expected_output.c_str());
} }
// One file depended on some other files not accessible by the sandbox // One file depended on some other files not accessible by the sandbox
TEST_F(JsonnetTest, Bad_evaluation) { TEST_F(JsonnetTest, Bad_evaluation) {
char input_file[] = "imports.jsonnet"; constexpr char kInputFile[] = "imports.jsonnet";
Read_input(input_file); Read_input(kInputFile);
Evaluate_jsonnet_code(input_file, BASE, false); Evaluate_jsonnet_code(kBase, false);
} }
} //namespace

View File

@ -14,91 +14,87 @@
#include "jsonnet_tests.h" #include "jsonnet_tests.h"
// Prepare what is needed to perform a test. // Prepares what is needed to perform a test.
void JsonnetTestHelper::TestSetUp() { void JsonnetTestHelper::TestSetUp() {
// Get paths to where input and output is stored. // Get paths to where input and output is stored.
char buffer[256]; char buffer[256];
int error = readlink("/proc/self/exe", buffer, 256); int error = readlink("/proc/self/exe", buffer, 256);
std::filesystem::path binary_path = dirname(buffer); ASSERT_GE(error, 0);
std::filesystem::path input_path =
binary_path / "tests_input" / "dummy_input"; std::pair<absl::string_view, absl::string_view> parts_of_path = sandbox2::file::SplitPath(buffer);
std::filesystem::path output_path = absl::string_view binary_path = parts_of_path.first;
binary_path / "tests_output" / "dummy_output";
std::string input_path = sandbox2::file::JoinPath(binary_path, "tests_input", "dummy_input");
std::string output_path = sandbox2::file::JoinPath(binary_path, "tests_output", "dummy_input");
// Set up sandbox and api. // Set up sandbox and api.
sandbox = std::make_unique<JsonnetBaseSandbox>(input_path.string(), sandbox_ = absl::make_unique<JsonnetBaseSandbox>(input_path,
output_path.string()); output_path);
ASSERT_THAT(sandbox->Init(), sapi::IsOk()); ASSERT_THAT(sandbox_->Init(), sapi::IsOk());
api = std::make_unique<JsonnetApi>(sandbox.get()); api_ = absl::make_unique<JsonnetApi>(sandbox_.get());
// Initialize library's main structure. // Initialize library's main structure.
SAPI_ASSERT_OK_AND_ASSIGN(JsonnetVm * vm_ptr, api->c_jsonnet_make()); SAPI_ASSERT_OK_AND_ASSIGN(JsonnetVm * vm_ptr, api_->c_jsonnet_make());
vm = std::make_unique<sapi::v::RemotePtr>(vm_ptr); vm_ = absl::make_unique<sapi::v::RemotePtr>(vm_ptr);
if_jsonnet_vm_was_used = false; if_jsonnet_vm_was_used_ = false;
if_input_was_read = false; if_input_was_read_ = false;
return;
} }
// Clean up after a test. // Cleans up after a test.
void JsonnetTestHelper::TestTearDown() { void JsonnetTestHelper::TestTearDown() {
if (if_jsonnet_vm_was_used) { if (if_jsonnet_vm_was_used_) {
SAPI_ASSERT_OK_AND_ASSIGN( SAPI_ASSERT_OK_AND_ASSIGN(
char* result, api->c_jsonnet_realloc(vm.get(), output.get(), 0)); char* result, api_->c_jsonnet_realloc(vm_.get(), output_.get(), 0));
} }
ASSERT_THAT(api->c_jsonnet_destroy(vm.get()), sapi::IsOk()); ASSERT_THAT(api_->c_jsonnet_destroy(vm_.get()), sapi::IsOk());
if (if_input_was_read) { if (if_input_was_read_) {
ASSERT_THAT(api->c_free_input(input.get()), sapi::IsOk()); ASSERT_THAT(api_->c_free_input(input_.get()), sapi::IsOk());
} }
return;
} }
// Read input from file. // Reads input from file.
void JsonnetTestHelper::Read_input(char* filename) { void JsonnetTestHelper::Read_input(const char* filename) {
std::string in_file_in_sandboxee(std::string("/input/") + std::string in_file_in_sandboxee(std::string("/input/") +
basename(&filename[0])); basename(const_cast<char*>(&filename[0])));
input_filename_in_sandboxee = std::move(in_file_in_sandboxee); input_filename_in_sandboxee_ = std::move(in_file_in_sandboxee);
sapi::v::ConstCStr in_file_var(input_filename_in_sandboxee.c_str()); sapi::v::ConstCStr in_file_var(input_filename_in_sandboxee_.c_str());
SAPI_ASSERT_OK_AND_ASSIGN(char* input_ptr, SAPI_ASSERT_OK_AND_ASSIGN(char* input_ptr,
api->c_read_input(0, in_file_var.PtrBefore())); api_->c_read_input(0, in_file_var.PtrBefore()));
input = std::make_unique<sapi::v::RemotePtr>(input_ptr); input_ = absl::make_unique<sapi::v::RemotePtr>(input_ptr);
if_input_was_read = true; if_input_was_read_ = true;
return;
} }
// Evaluate jsonnet code. // Evaluates jsonnet code.
void JsonnetTestHelper::Evaluate_jsonnet_code(char* filename, Evaluation type, void JsonnetTestHelper::Evaluate_jsonnet_code(Evaluation type,
bool expected_correct) { bool expected_correct) {
sapi::v::ConstCStr in_file_var(input_filename_in_sandboxee.c_str()); sapi::v::ConstCStr in_file_var(input_filename_in_sandboxee_.c_str());
sapi::v::Int error; sapi::v::Int error;
char* output_ptr; char* output_ptr;
switch (type) { switch (type) {
case BASE: { case kBase: {
SAPI_ASSERT_OK_AND_ASSIGN( SAPI_ASSERT_OK_AND_ASSIGN(
output_ptr, output_ptr,
api->c_jsonnet_evaluate_snippet(vm.get(), in_file_var.PtrBefore(), api_->c_jsonnet_evaluate_snippet(vm_.get(), in_file_var.PtrBefore(),
input.get(), error.PtrAfter())); input_.get(), error.PtrAfter()));
break; break;
} }
case MULTIPLE_FILES: { case kMultipleFiles: {
SAPI_ASSERT_OK_AND_ASSIGN( SAPI_ASSERT_OK_AND_ASSIGN(
output_ptr, api->c_jsonnet_evaluate_snippet_multi( output_ptr, api_->c_jsonnet_evaluate_snippet_multi(
vm.get(), in_file_var.PtrBefore(), input.get(), vm_.get(), in_file_var.PtrBefore(), input_.get(),
error.PtrAfter())); error.PtrAfter()));
break; break;
} }
case YAML_STREAM: { case kYamlStream: {
SAPI_ASSERT_OK_AND_ASSIGN( SAPI_ASSERT_OK_AND_ASSIGN(
output_ptr, api->c_jsonnet_evaluate_snippet_stream( output_ptr, api_->c_jsonnet_evaluate_snippet_stream(
vm.get(), in_file_var.PtrBefore(), input.get(), vm_.get(), in_file_var.PtrBefore(), input_.get(),
error.PtrAfter())); error.PtrAfter()));
break; break;
} }
@ -110,56 +106,52 @@ void JsonnetTestHelper::Evaluate_jsonnet_code(char* filename, Evaluation type,
ASSERT_THAT(error.GetValue(), testing::Eq(1)); ASSERT_THAT(error.GetValue(), testing::Eq(1));
} }
output = std::make_unique<sapi::v::RemotePtr>(output_ptr); output_ = absl::make_unique<sapi::v::RemotePtr>(output_ptr);
if_jsonnet_vm_was_used = true; if_jsonnet_vm_was_used_ = true;
return;
} }
// Write output to file. // Writes output to file.
void JsonnetTestHelper::Write_output(char* filename_or_directory, void JsonnetTestHelper::Write_output(const char* filename_or_directory,
Evaluation type) { Evaluation type) {
bool success; bool success;
switch (type) { switch (type) {
case BASE: { case kBase: {
std::string out_file_in_sandboxee(std::string("/output/") + std::string out_file_in_sandboxee(std::string("/output/") +
basename(&filename_or_directory[0])); basename(const_cast<char*>(&filename_or_directory[0])));
sapi::v::ConstCStr out_file_var(out_file_in_sandboxee.c_str()); sapi::v::ConstCStr out_file_var(out_file_in_sandboxee.c_str());
SAPI_ASSERT_OK_AND_ASSIGN( SAPI_ASSERT_OK_AND_ASSIGN(
success, success,
api->c_write_output_file(output.get(), out_file_var.PtrBefore())); api_->c_write_output_file(output_.get(), out_file_var.PtrBefore()));
break; break;
} }
case MULTIPLE_FILES: { case kMultipleFiles: {
std::string out_file_in_sandboxee(std::string("/output/")); std::string out_file_in_sandboxee(std::string("/output/"));
sapi::v::ConstCStr out_file_var(out_file_in_sandboxee.c_str()); sapi::v::ConstCStr out_file_var(out_file_in_sandboxee.c_str());
SAPI_ASSERT_OK_AND_ASSIGN( SAPI_ASSERT_OK_AND_ASSIGN(
success, api->c_write_multi_output_files( success, api_->c_write_multi_output_files(
output.get(), out_file_var.PtrBefore(), false)); output_.get(), out_file_var.PtrBefore(), false));
break; break;
} }
case YAML_STREAM: { case kYamlStream: {
std::string out_file_in_sandboxee(std::string("/output/") + std::string out_file_in_sandboxee(std::string("/output/") +
basename(&filename_or_directory[0])); basename(const_cast<char*>(&filename_or_directory[0])));
sapi::v::ConstCStr out_file_var(out_file_in_sandboxee.c_str()); sapi::v::ConstCStr out_file_var(out_file_in_sandboxee.c_str());
SAPI_ASSERT_OK_AND_ASSIGN( SAPI_ASSERT_OK_AND_ASSIGN(
success, success,
api->c_write_output_stream(output.get(), out_file_var.PtrBefore())); api_->c_write_output_stream(output_.get(), out_file_var.PtrBefore()));
break; break;
} }
} }
ASSERT_THAT(success, testing::Eq(true)); ASSERT_THAT(success, testing::Eq(true));
return;
} }
// Reading the output written to a file by library function / expected output // Reads the output written to a file by library function / expected output
std::string JsonnetTestHelper::Read_output(char* filename) { std::string JsonnetTestHelper::Read_output(const char* filename) {
std::ifstream input_stream(filename); std::ifstream input_stream(filename);
std::string contents((std::istreambuf_iterator<char>(input_stream)), std::string contents((std::istreambuf_iterator<char>(input_stream)),
std::istreambuf_iterator<char>()); std::istreambuf_iterator<char>());