sandboxed-api/oss-internship-2020/jsonnet/tests/jsonnet_tests_utils.cc

161 lines
5.6 KiB
C++
Raw Normal View History

2020-09-15 21:22:47 +08:00
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
2020-09-16 00:16:05 +08:00
// limitations under the License.
#include "jsonnet_tests.h" // NOLINT(build/include)
2020-09-16 00:16:05 +08:00
2020-09-28 22:33:43 +08:00
// Prepares what is needed to perform a test.
2020-09-16 00:16:05 +08:00
void JsonnetTestHelper::TestSetUp() {
2020-09-16 22:43:42 +08:00
// Get paths to where input and output is stored.
2020-09-17 23:59:03 +08:00
char buffer[256];
int error = readlink("/proc/self/exe", buffer, 256);
2020-09-28 22:33:43 +08:00
ASSERT_GE(error, 0);
2020-09-28 22:41:32 +08:00
std::pair<absl::string_view, absl::string_view> parts_of_path =
sandbox2::file::SplitPath(buffer);
2020-09-28 22:33:43 +08:00
absl::string_view binary_path = parts_of_path.first;
2020-09-28 22:41:32 +08:00
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");
2020-09-16 22:43:42 +08:00
2020-09-16 00:16:05 +08:00
// Set up sandbox and api.
2020-09-28 22:41:32 +08:00
sandbox_ = absl::make_unique<JsonnetBaseSandbox>(input_path, output_path);
2020-09-28 22:33:43 +08:00
ASSERT_THAT(sandbox_->Init(), sapi::IsOk());
api_ = absl::make_unique<JsonnetApi>(sandbox_.get());
2020-09-16 00:16:05 +08:00
// Initialize library's main structure.
2020-09-28 22:33:43 +08:00
SAPI_ASSERT_OK_AND_ASSIGN(JsonnetVm * vm_ptr, api_->c_jsonnet_make());
vm_ = absl::make_unique<sapi::v::RemotePtr>(vm_ptr);
2020-09-16 00:16:05 +08:00
jsonnet_vm_was_used_ = false;
input_was_read_ = false;
2020-09-16 00:16:05 +08:00
}
2020-09-28 22:33:43 +08:00
// Cleans up after a test.
2020-09-16 00:16:05 +08:00
void JsonnetTestHelper::TestTearDown() {
if (jsonnet_vm_was_used_) {
SAPI_ASSERT_OK_AND_ASSIGN(char* result,
api_->c_jsonnet_realloc(vm_.get(), output_.get(), 0));
2020-09-16 22:43:42 +08:00
}
2020-09-28 22:33:43 +08:00
ASSERT_THAT(api_->c_jsonnet_destroy(vm_.get()), sapi::IsOk());
if (input_was_read_) {
2020-09-28 22:33:43 +08:00
ASSERT_THAT(api_->c_free_input(input_.get()), sapi::IsOk());
2020-09-16 22:43:42 +08:00
}
2020-09-16 00:16:05 +08:00
}
2020-09-28 22:33:43 +08:00
// Reads input from file.
void JsonnetTestHelper::ReadInput(const char* filename) {
2020-09-16 00:16:05 +08:00
std::string in_file_in_sandboxee(std::string("/input/") +
2020-09-28 22:33:43 +08:00
basename(const_cast<char*>(&filename[0])));
input_filename_in_sandboxee_ = std::move(in_file_in_sandboxee);
sapi::v::ConstCStr in_file_var(input_filename_in_sandboxee_.c_str());
2020-09-16 00:16:05 +08:00
SAPI_ASSERT_OK_AND_ASSIGN(char* input_ptr,
api_->c_read_input(0, in_file_var.PtrBefore()));
2020-09-28 22:33:43 +08:00
input_ = absl::make_unique<sapi::v::RemotePtr>(input_ptr);
2020-09-16 00:16:05 +08:00
input_was_read_ = true;
2020-09-16 00:16:05 +08:00
}
2020-09-28 22:33:43 +08:00
// Evaluates jsonnet code.
void JsonnetTestHelper::Evaluate_jsonnet_code(Evaluation type,
2020-09-18 18:16:35 +08:00
bool expected_correct) {
2020-09-28 22:33:43 +08:00
sapi::v::ConstCStr in_file_var(input_filename_in_sandboxee_.c_str());
2020-09-16 00:16:05 +08:00
sapi::v::Int error;
char* output_ptr;
switch (type) {
2020-09-28 22:33:43 +08:00
case kBase: {
SAPI_ASSERT_OK_AND_ASSIGN(output_ptr, api_->c_jsonnet_evaluate_snippet(
vm_.get(), in_file_var.PtrBefore(),
2020-09-28 22:41:32 +08:00
input_.get(), error.PtrAfter()));
2020-09-16 00:16:05 +08:00
break;
}
2020-09-28 22:33:43 +08:00
case kMultipleFiles: {
SAPI_ASSERT_OK_AND_ASSIGN(output_ptr, api_->c_jsonnet_evaluate_snippet_multi(
vm_.get(), in_file_var.PtrBefore(),
input_.get(), error.PtrAfter()));
2020-09-16 00:16:05 +08:00
break;
}
2020-09-28 22:33:43 +08:00
case kYamlStream: {
SAPI_ASSERT_OK_AND_ASSIGN(output_ptr, api_->c_jsonnet_evaluate_snippet_stream(
vm_.get(), in_file_var.PtrBefore(),
input_.get(), error.PtrAfter()));
2020-09-16 00:16:05 +08:00
break;
}
}
2020-09-18 17:58:52 +08:00
if (expected_correct) {
ASSERT_THAT(error.GetValue(), testing::Eq(0));
} else {
ASSERT_THAT(error.GetValue(), testing::Eq(1));
}
2020-09-28 22:33:43 +08:00
output_ = absl::make_unique<sapi::v::RemotePtr>(output_ptr);
2020-09-18 17:58:52 +08:00
jsonnet_vm_was_used_ = true;
2020-09-16 00:16:05 +08:00
}
2020-09-28 22:33:43 +08:00
// Writes output to file.
void JsonnetTestHelper::WriteOutput(const char* filename_or_directory,
Evaluation type) {
2020-09-16 00:16:05 +08:00
bool success;
switch (type) {
2020-09-28 22:33:43 +08:00
case kBase: {
2020-09-28 22:41:32 +08:00
std::string out_file_in_sandboxee(
std::string("/output/") +
basename(const_cast<char*>(&filename_or_directory[0])));
2020-09-16 00:16:05 +08:00
sapi::v::ConstCStr out_file_var(out_file_in_sandboxee.c_str());
SAPI_ASSERT_OK_AND_ASSIGN(
success,
2020-09-28 22:33:43 +08:00
api_->c_write_output_file(output_.get(), out_file_var.PtrBefore()));
2020-09-16 00:16:05 +08:00
break;
}
2020-09-28 22:33:43 +08:00
case kMultipleFiles: {
2020-09-16 00:16:05 +08:00
std::string out_file_in_sandboxee(std::string("/output/"));
sapi::v::ConstCStr out_file_var(out_file_in_sandboxee.c_str());
SAPI_ASSERT_OK_AND_ASSIGN(success,
api_->c_write_multi_output_files(
output_.get(), out_file_var.PtrBefore(), false));
2020-09-16 00:16:05 +08:00
break;
}
2020-09-28 22:33:43 +08:00
case kYamlStream: {
2020-09-28 22:41:32 +08:00
std::string out_file_in_sandboxee(
std::string("/output/") +
basename(const_cast<char*>(&filename_or_directory[0])));
2020-09-16 00:16:05 +08:00
sapi::v::ConstCStr out_file_var(out_file_in_sandboxee.c_str());
SAPI_ASSERT_OK_AND_ASSIGN(
success,
2020-09-28 22:33:43 +08:00
api_->c_write_output_stream(output_.get(), out_file_var.PtrBefore()));
2020-09-16 00:16:05 +08:00
break;
}
}
ASSERT_THAT(success, testing::Eq(true));
}
2020-09-17 23:59:03 +08:00
2020-09-28 22:33:43 +08:00
// Reads the output written to a file by library function / expected output
std::string JsonnetTestHelper::ReadOutput(const char* filename) {
2020-09-17 23:59:03 +08:00
std::ifstream input_stream(filename);
2020-09-18 18:16:35 +08:00
std::string contents((std::istreambuf_iterator<char>(input_stream)),
std::istreambuf_iterator<char>());
2020-09-17 23:59:03 +08:00
return contents;
}