From c7183b57834a732fda9de3ed59d2834dfd98346b Mon Sep 17 00:00:00 2001 From: Ivan Petrov Date: Fri, 10 Jan 2020 16:52:08 +0000 Subject: [PATCH 1/9] Add application configuration serializer --- oak/common/BUILD | 12 +++++- oak/common/app_config.cc | 12 ++++++ oak/common/app_config.h | 6 +++ oak/common/app_config_serializer.cc | 61 +++++++++++++++++++++++++++++ oak/common/utils.h | 10 +++++ 5 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 oak/common/app_config_serializer.cc diff --git a/oak/common/BUILD b/oak/common/BUILD index 61236333ec1..94f57b50c46 100644 --- a/oak/common/BUILD +++ b/oak/common/BUILD @@ -14,7 +14,7 @@ # limitations under the License. # -load("@rules_cc//cc:defs.bzl", "cc_library", "cc_test") +load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library", "cc_test") package( licenses = ["notice"], @@ -43,6 +43,7 @@ cc_library( hdrs = ["app_config.h"], visibility = ["//visibility:public"], deps = [ + "//oak/common:utils", "//oak/proto:manager_cc_proto", "@com_google_absl//absl/memory", "@com_google_asylo//asylo/util:logging", @@ -99,3 +100,12 @@ cc_library( "@com_google_asylo//asylo/util:logging", ], ) + +cc_binary( + name = "app_config_serializer", + srcs = ["app_config_serializer.cc"] + visibility = ["//visibility:public"], + deps = [ + ":app_config", + ], +) diff --git a/oak/common/app_config.cc b/oak/common/app_config.cc index 7e2d5592fda..7741c3328c3 100644 --- a/oak/common/app_config.cc +++ b/oak/common/app_config.cc @@ -15,6 +15,7 @@ */ #include "oak/common/app_config.h" +#include "oak/common/utils.h" #include #include @@ -45,6 +46,17 @@ std::unique_ptr DefaultConfig(const std::string& modul return config; } +std::unique ReadConfigFromFile(const std::string& file) { + auto config = absl::make_unique(); + auto data = utils::read_file(file); + return config.ParseFromString(data); +} + +void WriteConfigToFile(const ApplicationConfiguration* config, const std::string& file) { + auto data = config->SerializeAsString(); + utils::write_file(data, file); +} + void AddLoggingToConfig(ApplicationConfiguration* config) { NodeConfiguration* node_config = config->add_node_configs(); node_config->set_name(kLogConfigName); diff --git a/oak/common/app_config.h b/oak/common/app_config.h index c5e1a253347..1b1d16a2bf3 100644 --- a/oak/common/app_config.h +++ b/oak/common/app_config.h @@ -27,6 +27,12 @@ namespace oak { // name and contents, accessible via gRPC. std::unique_ptr DefaultConfig(const std::string& module_bytes); +// Reads a serialized application configuration from `file`. +std::unique ReadConfigFromFile(const std::string& file); + +// Serializes an application configuration from `config` and writes it into a `file`. +void WriteConfigToFile(const ApplicationConfiguration* config, const std::string& file); + // Modify application configuration to make logging available. void AddLoggingToConfig(ApplicationConfiguration* config); diff --git a/oak/common/app_config_serializer.cc b/oak/common/app_config_serializer.cc new file mode 100644 index 00000000000..ae5d6b8cd53 --- /dev/null +++ b/oak/common/app_config_serializer.cc @@ -0,0 +1,61 @@ +/* + * Copyright 2019 The Project Oak Authors + * + * 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 + * limitations under the License. + */ + +#ifndef OAK_COMMON_APP_CONFIG_SERIALIZER_H_ +#define OAK_COMMON_APP_CONFIG_SERIALIZER_H_ + +#include + +#include "oak/common/app_config.h" + +ABSL_FLAG(std::string, enclave_path, "", "Path of the enclave to load"); +ABSL_FLAG(std::string, module, "", "File containing the compiled WebAssembly module"); +ABSL_FLAG(bool, logging, false, "Enable application logging"); +ABSL_FLAG(std::string, storage_address, "127.0.0.1:7867", + "Address ot the storage provider to connect to"); +ABSL_FLAG(int, grpc_port, 8080, "Port for the Application to listen on"); +ABSL_FLAG(std::string, output_file, "", "File to write an application configuration to") + +void sigint_handler(int param) { + LOG(QFATAL) << "SIGINT received"; + exit(1); +} + +int main(int argc, char* argv[]) { + absl::ParseCommandLine(argc, argv); + + // We install an explicit SIGINT handler, as for some reason the default one + // does not seem to work. + std::signal(SIGINT, sigint_handler); + + // Create an application configuration. + std::string module_bytes = oak::utils::read_file(absl::GetFlag(FLAGS_module)); + std::unique_ptr application_config = + oak::DefaultConfig(module_bytes); + + // Set application configuration parameters. + if (absl::GetFlag(FLAGS_logging)) { + oak::AddLoggingToConfig(); + } + oak::AddStorageToConfig(application_config.get(), absl::GetFlag(FLAGS_storage_address)); + oak::AddGrpcPortToConfig(application_config.get(), absl::GetFlag(FLAGS_grpc_port)); + + oak::WriteConfigToFile(application_config.get(), output_file); + + return 0; +} + +#endif // OAK_COMMON_APP_CONFIG_SERIALIZER_H_ diff --git a/oak/common/utils.h b/oak/common/utils.h index dc2bc7df2d9..3a19c0dca0e 100644 --- a/oak/common/utils.h +++ b/oak/common/utils.h @@ -15,5 +15,15 @@ std::string read_file(const std::string& module_path) { buffer << t.rdbuf(); return buffer.str(); } + +// Writes `data` string into a binary `file`. +void write_file(const std::string& data, const std::string& file) { + std::ofstream t(file, std::ofstream::out); + if (!t.is_open()) { + LOG(QFATAL) << "Could not open file " << file; + } + t.rdbuf() << data; +} + } // namespace utils } // namespace oak From f81c5026efbbbf6366ac1c84933c6117cac32303 Mon Sep 17 00:00:00 2001 From: Ivan Petrov Date: Fri, 10 Jan 2020 17:29:16 +0000 Subject: [PATCH 2/9] Update serializer --- oak/common/BUILD | 10 ++++-- oak/common/app_config.cc | 11 ++++--- oak/common/app_config.h | 2 +- oak/common/app_config_serializer.cc | 24 +++++++++------ oak/common/utils.cc | 48 +++++++++++++++++++++++++++++ oak/common/utils.h | 41 +++++++++++++----------- 6 files changed, 101 insertions(+), 35 deletions(-) create mode 100644 oak/common/utils.cc diff --git a/oak/common/BUILD b/oak/common/BUILD index 94f57b50c46..8febd40d586 100644 --- a/oak/common/BUILD +++ b/oak/common/BUILD @@ -94,6 +94,7 @@ cc_library( cc_library( name = "utils", + srcs = ["utils.cc"], hdrs = ["utils.h"], visibility = ["//visibility:public"], deps = [ @@ -103,9 +104,12 @@ cc_library( cc_binary( name = "app_config_serializer", - srcs = ["app_config_serializer.cc"] - visibility = ["//visibility:public"], + srcs = ["app_config_serializer.cc"], deps = [ - ":app_config", + "//oak/common:app_config", + "//oak/common:utils", + "@com_google_absl//absl/flags:flag", + "@com_google_absl//absl/flags:parse", + "@com_google_asylo//asylo/util:logging", ], ) diff --git a/oak/common/app_config.cc b/oak/common/app_config.cc index 7741c3328c3..e112d2c13c6 100644 --- a/oak/common/app_config.cc +++ b/oak/common/app_config.cc @@ -46,14 +46,17 @@ std::unique_ptr DefaultConfig(const std::string& modul return config; } -std::unique ReadConfigFromFile(const std::string& file) { +std::unique_ptr ReadConfigFromFile(const std::string& file) { auto config = absl::make_unique(); - auto data = utils::read_file(file); - return config.ParseFromString(data); + + std::string data = utils::read_file(file); + config->ParseFromString(data); + + return config; } void WriteConfigToFile(const ApplicationConfiguration* config, const std::string& file) { - auto data = config->SerializeAsString(); + std::string data = config->SerializeAsString(); utils::write_file(data, file); } diff --git a/oak/common/app_config.h b/oak/common/app_config.h index 1b1d16a2bf3..7b7b144c868 100644 --- a/oak/common/app_config.h +++ b/oak/common/app_config.h @@ -28,7 +28,7 @@ namespace oak { std::unique_ptr DefaultConfig(const std::string& module_bytes); // Reads a serialized application configuration from `file`. -std::unique ReadConfigFromFile(const std::string& file); +std::unique_ptr ReadConfigFromFile(const std::string& file); // Serializes an application configuration from `config` and writes it into a `file`. void WriteConfigToFile(const ApplicationConfiguration* config, const std::string& file); diff --git a/oak/common/app_config_serializer.cc b/oak/common/app_config_serializer.cc index ae5d6b8cd53..0d1ac1a6d2c 100644 --- a/oak/common/app_config_serializer.cc +++ b/oak/common/app_config_serializer.cc @@ -14,12 +14,16 @@ * limitations under the License. */ -#ifndef OAK_COMMON_APP_CONFIG_SERIALIZER_H_ -#define OAK_COMMON_APP_CONFIG_SERIALIZER_H_ +#include "oak/common/app_config.h" +#include "oak/common/utils.h" +#include #include +#include -#include "oak/common/app_config.h" +#include "absl/flags/flag.h" +#include "absl/flags/parse.h" +#include "asylo/util/logging.h" ABSL_FLAG(std::string, enclave_path, "", "Path of the enclave to load"); ABSL_FLAG(std::string, module, "", "File containing the compiled WebAssembly module"); @@ -27,7 +31,7 @@ ABSL_FLAG(bool, logging, false, "Enable application logging"); ABSL_FLAG(std::string, storage_address, "127.0.0.1:7867", "Address ot the storage provider to connect to"); ABSL_FLAG(int, grpc_port, 8080, "Port for the Application to listen on"); -ABSL_FLAG(std::string, output_file, "", "File to write an application configuration to") +ABSL_FLAG(std::string, output_file, "", "File to write an application configuration to"); void sigint_handler(int param) { LOG(QFATAL) << "SIGINT received"; @@ -43,19 +47,21 @@ int main(int argc, char* argv[]) { // Create an application configuration. std::string module_bytes = oak::utils::read_file(absl::GetFlag(FLAGS_module)); - std::unique_ptr application_config = - oak::DefaultConfig(module_bytes); + auto application_config = oak::DefaultConfig(module_bytes); // Set application configuration parameters. if (absl::GetFlag(FLAGS_logging)) { - oak::AddLoggingToConfig(); + oak::AddLoggingToConfig(application_config.get()); } oak::AddStorageToConfig(application_config.get(), absl::GetFlag(FLAGS_storage_address)); oak::AddGrpcPortToConfig(application_config.get(), absl::GetFlag(FLAGS_grpc_port)); + std::string output_file = absl::GetFlag(FLAGS_output_file); + if (output_file.empty()) { + LOG(QFATAL) << "Output file is not specified"; + return 1; + } oak::WriteConfigToFile(application_config.get(), output_file); return 0; } - -#endif // OAK_COMMON_APP_CONFIG_SERIALIZER_H_ diff --git a/oak/common/utils.cc b/oak/common/utils.cc new file mode 100644 index 00000000000..054d932562c --- /dev/null +++ b/oak/common/utils.cc @@ -0,0 +1,48 @@ +/* + * Copyright 2020 The Project Oak Authors + * + * 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 + * limitations under the License. + */ + +#include "oak/common/utils.h" + +#include + +#include "asylo/util/logging.h" + +namespace oak { +namespace utils { + +// Reads a binary file and returns its contents as a std::string. +std::string read_file(const std::string& module_path) { + std::ifstream t(module_path, std::ifstream::in); + if (!t.is_open()) { + LOG(QFATAL) << "Could not open module " << module_path; + } + std::stringstream buffer; + buffer << t.rdbuf(); + return buffer.str(); +} + +// Writes `data` string into a binary `file`. +void write_file(const std::string& data, const std::string& file) { + std::ofstream t(file, std::ofstream::out); + if (!t.is_open()) { + LOG(QFATAL) << "Could not open file " << file; + } + t << data; + t.close(); +} + +} // namespace utils +} // namespace oak diff --git a/oak/common/utils.h b/oak/common/utils.h index 3a19c0dca0e..a9932631f11 100644 --- a/oak/common/utils.h +++ b/oak/common/utils.h @@ -1,29 +1,34 @@ -#include +/* + * Copyright 2020 The Project Oak Authors + * + * 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 + * limitations under the License. + */ -#include "asylo/util/logging.h" +#ifndef OAK_COMMON_UTILS_H_ +#define OAK_COMMON_UTILS_H_ + +#include namespace oak { namespace utils { // Reads a binary file and returns its contents as a std::string. -std::string read_file(const std::string& module_path) { - std::ifstream t(module_path, std::ifstream::in); - if (!t.is_open()) { - LOG(QFATAL) << "Could not open module " << module_path; - } - std::stringstream buffer; - buffer << t.rdbuf(); - return buffer.str(); -} +std::string read_file(const std::string& module_path); // Writes `data` string into a binary `file`. -void write_file(const std::string& data, const std::string& file) { - std::ofstream t(file, std::ofstream::out); - if (!t.is_open()) { - LOG(QFATAL) << "Could not open file " << file; - } - t.rdbuf() << data; -} +void write_file(const std::string& data, const std::string& file); } // namespace utils } // namespace oak + +#endif // OAK_COMMON_UTILS_H_ From 548b78cafbd2a20f1fdfb6740ab8c725e82ef163 Mon Sep 17 00:00:00 2001 From: Ivan Petrov Date: Fri, 10 Jan 2020 18:03:36 +0000 Subject: [PATCH 3/9] Minor fixes --- oak/common/BUILD | 1 + oak/common/app_config_serializer.cc | 3 +-- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/oak/common/BUILD b/oak/common/BUILD index 8febd40d586..5b513c5a719 100644 --- a/oak/common/BUILD +++ b/oak/common/BUILD @@ -105,6 +105,7 @@ cc_library( cc_binary( name = "app_config_serializer", srcs = ["app_config_serializer.cc"], + visibility = ["//visibility:public"], deps = [ "//oak/common:app_config", "//oak/common:utils", diff --git a/oak/common/app_config_serializer.cc b/oak/common/app_config_serializer.cc index 0d1ac1a6d2c..5f8c4ece7e3 100644 --- a/oak/common/app_config_serializer.cc +++ b/oak/common/app_config_serializer.cc @@ -1,5 +1,5 @@ /* - * Copyright 2019 The Project Oak Authors + * Copyright 2020 The Project Oak Authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -25,7 +25,6 @@ #include "absl/flags/parse.h" #include "asylo/util/logging.h" -ABSL_FLAG(std::string, enclave_path, "", "Path of the enclave to load"); ABSL_FLAG(std::string, module, "", "File containing the compiled WebAssembly module"); ABSL_FLAG(bool, logging, false, "Enable application logging"); ABSL_FLAG(std::string, storage_address, "127.0.0.1:7867", From 7fccd280fabf8f6e1e30e94637fbe4f348826dcc Mon Sep 17 00:00:00 2001 From: Ivan Petrov Date: Fri, 10 Jan 2020 18:11:25 +0000 Subject: [PATCH 4/9] Fix formatting --- oak/common/app_config_serializer.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/oak/common/app_config_serializer.cc b/oak/common/app_config_serializer.cc index 5f8c4ece7e3..51d8253e259 100644 --- a/oak/common/app_config_serializer.cc +++ b/oak/common/app_config_serializer.cc @@ -61,6 +61,6 @@ int main(int argc, char* argv[]) { return 1; } oak::WriteConfigToFile(application_config.get(), output_file); - + return 0; } From c515579357c1b30c5820e9d0b292324ceeae69fb Mon Sep 17 00:00:00 2001 From: Ivan Petrov Date: Fri, 10 Jan 2020 18:43:26 +0000 Subject: [PATCH 5/9] Delete unused parameter --- oak/common/app_config_serializer.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/oak/common/app_config_serializer.cc b/oak/common/app_config_serializer.cc index 51d8253e259..dd883fd5a1c 100644 --- a/oak/common/app_config_serializer.cc +++ b/oak/common/app_config_serializer.cc @@ -32,7 +32,7 @@ ABSL_FLAG(std::string, storage_address, "127.0.0.1:7867", ABSL_FLAG(int, grpc_port, 8080, "Port for the Application to listen on"); ABSL_FLAG(std::string, output_file, "", "File to write an application configuration to"); -void sigint_handler(int param) { +void sigint_handler(int) { LOG(QFATAL) << "SIGINT received"; exit(1); } From 830520936f77ba1b5b671822271e5c96a675bd9a Mon Sep 17 00:00:00 2001 From: Ivan Petrov Date: Mon, 13 Jan 2020 14:53:34 +0000 Subject: [PATCH 6/9] Add ApplicationConfig tests --- oak/common/app_config.cc | 8 ++-- oak/common/app_config.h | 8 ++-- oak/common/app_config_serializer.cc | 31 +++++++------- oak/common/app_config_test.cc | 65 +++++++++++++++++++---------- oak/common/utils.cc | 17 ++++---- oak/common/utils.h | 6 +-- 6 files changed, 74 insertions(+), 61 deletions(-) diff --git a/oak/common/app_config.cc b/oak/common/app_config.cc index e112d2c13c6..70cc8aa0514 100644 --- a/oak/common/app_config.cc +++ b/oak/common/app_config.cc @@ -46,18 +46,18 @@ std::unique_ptr DefaultConfig(const std::string& modul return config; } -std::unique_ptr ReadConfigFromFile(const std::string& file) { +std::unique_ptr ReadConfigFromFile(const std::string& filename) { auto config = absl::make_unique(); - std::string data = utils::read_file(file); + std::string data = utils::read_file(filename); config->ParseFromString(data); return config; } -void WriteConfigToFile(const ApplicationConfiguration* config, const std::string& file) { +void WriteConfigToFile(const ApplicationConfiguration* config, const std::string& filename) { std::string data = config->SerializeAsString(); - utils::write_file(data, file); + utils::write_file(data, filename); } void AddLoggingToConfig(ApplicationConfiguration* config) { diff --git a/oak/common/app_config.h b/oak/common/app_config.h index 7b7b144c868..f3cb42c07e9 100644 --- a/oak/common/app_config.h +++ b/oak/common/app_config.h @@ -27,11 +27,11 @@ namespace oak { // name and contents, accessible via gRPC. std::unique_ptr DefaultConfig(const std::string& module_bytes); -// Reads a serialized application configuration from `file`. -std::unique_ptr ReadConfigFromFile(const std::string& file); +// Reads a serialized application configuration from file. +std::unique_ptr ReadConfigFromFile(const std::string& filename); -// Serializes an application configuration from `config` and writes it into a `file`. -void WriteConfigToFile(const ApplicationConfiguration* config, const std::string& file); +// Serializes an application configuration from `config` and writes it into a file. +void WriteConfigToFile(const ApplicationConfiguration* config, const std::string& filename); // Modify application configuration to make logging available. void AddLoggingToConfig(ApplicationConfiguration* config); diff --git a/oak/common/app_config_serializer.cc b/oak/common/app_config_serializer.cc index dd883fd5a1c..ca0b04b3980 100644 --- a/oak/common/app_config_serializer.cc +++ b/oak/common/app_config_serializer.cc @@ -14,16 +14,14 @@ * limitations under the License. */ -#include "oak/common/app_config.h" -#include "oak/common/utils.h" - -#include #include #include #include "absl/flags/flag.h" #include "absl/flags/parse.h" #include "asylo/util/logging.h" +#include "oak/common/app_config.h" +#include "oak/common/utils.h" ABSL_FLAG(std::string, module, "", "File containing the compiled WebAssembly module"); ABSL_FLAG(bool, logging, false, "Enable application logging"); @@ -32,21 +30,19 @@ ABSL_FLAG(std::string, storage_address, "127.0.0.1:7867", ABSL_FLAG(int, grpc_port, 8080, "Port for the Application to listen on"); ABSL_FLAG(std::string, output_file, "", "File to write an application configuration to"); -void sigint_handler(int) { - LOG(QFATAL) << "SIGINT received"; - exit(1); -} - int main(int argc, char* argv[]) { absl::ParseCommandLine(argc, argv); - // We install an explicit SIGINT handler, as for some reason the default one - // does not seem to work. - std::signal(SIGINT, sigint_handler); + std::string output_file = absl::GetFlag(FLAGS_output_file); + if (output_file.empty()) { + LOG(QFATAL) << "Output file is not specified"; + return 1; + } // Create an application configuration. std::string module_bytes = oak::utils::read_file(absl::GetFlag(FLAGS_module)); - auto application_config = oak::DefaultConfig(module_bytes); + std::unique_ptr application_config = oak::DefaultConfig( + module_bytes); // Set application configuration parameters. if (absl::GetFlag(FLAGS_logging)) { @@ -55,12 +51,13 @@ int main(int argc, char* argv[]) { oak::AddStorageToConfig(application_config.get(), absl::GetFlag(FLAGS_storage_address)); oak::AddGrpcPortToConfig(application_config.get(), absl::GetFlag(FLAGS_grpc_port)); - std::string output_file = absl::GetFlag(FLAGS_output_file); - if (output_file.empty()) { - LOG(QFATAL) << "Output file is not specified"; + if (ValidApplicationConfig(application_config.get())) { + oak::WriteConfigToFile(application_config.get(), output_file); + } + else { + LOG(QFATAL) << "Incorrect application configuration"; return 1; } - oak::WriteConfigToFile(application_config.get(), output_file); return 0; } diff --git a/oak/common/app_config_test.cc b/oak/common/app_config_test.cc index 298a38eba91..838608aa6ce 100644 --- a/oak/common/app_config_test.cc +++ b/oak/common/app_config_test.cc @@ -27,21 +27,30 @@ namespace oak { namespace { -std::unique_ptr ConfigFrom(const std::string& filename) { - std::ifstream ifs(filename.c_str(), std::ios::in | std::ios::binary); - EXPECT_TRUE(ifs.is_open()) << "failed to open " << filename; - std::stringstream ss; - ss << ifs.rdbuf(); - ifs.close(); - - auto config = absl::make_unique(); - google::protobuf::TextFormat::MergeFromString(ss.str(), config.get()); - return config; -} - -} // namespace - -TEST(ApplicationConfiguration, Default) { +// Fixture class for testing `ApplicationConfiguration` correctness. +class ApplicationConfigurationTest : public ::testing::Test { + protected: + virtual void TearDown() { + // Clean up temporary files. + std::remove(kTmpFilename.c_str()); + } + + std::unique_ptr ConfigFrom(const std::string& filename) { + std::ifstream ifs(filename.c_str(), std::ios::in | std::ios::binary); + EXPECT_TRUE(ifs.is_open()) << "failed to open " << filename; + std::stringstream ss; + ss << ifs.rdbuf(); + ifs.close(); + + auto config = absl::make_unique(); + google::protobuf::TextFormat::MergeFromString(ss.str(), config.get()); + return config; + } + + const std::string kTmpFilename = "oak/common/testdata/tmp.bin"; +}; + +TEST_F(ApplicationConfigurationTest, Default) { std::unique_ptr got = DefaultConfig(""); std::unique_ptr want = ConfigFrom("oak/common/testdata/barenode.textproto"); @@ -49,7 +58,15 @@ TEST(ApplicationConfiguration, Default) { ASSERT_EQ(true, ValidApplicationConfig(*got)); } -TEST(ApplicationConfiguration, DefaultPlusLogging) { +TEST_F(ApplicationConfigurationTest, ReadWriteFile) { + std::unique_ptr want = DefaultConfig(""); + WriteConfigToFile(want.get(), kTmpFilename); + std::unique_ptr got = ReadConfigFromFile(kTmpFilename); + ASSERT_EQ(want->DebugString(), got->DebugString()); + ASSERT_EQ(true, ValidApplicationConfig(*got)); +} + +TEST_F(ApplicationConfigurationTest, DefaultPlusLogging) { std::unique_ptr got = DefaultConfig(""); AddLoggingToConfig(got.get()); std::unique_ptr want = @@ -58,7 +75,7 @@ TEST(ApplicationConfiguration, DefaultPlusLogging) { ASSERT_EQ(true, ValidApplicationConfig(*got)); } -TEST(ApplicationConfiguration, DefaultPlusStorage) { +TEST_F(ApplicationConfigurationTest, DefaultPlusStorage) { std::unique_ptr got = DefaultConfig(""); AddStorageToConfig(got.get(), "localhost:8888"); std::unique_ptr want = @@ -67,7 +84,7 @@ TEST(ApplicationConfiguration, DefaultPlusStorage) { ASSERT_EQ(true, ValidApplicationConfig(*got)); } -TEST(ApplicationConfiguration, DefaultPlusGrpcPort) { +TEST_F(ApplicationConfigurationTest, DefaultPlusGrpcPort) { std::unique_ptr got = DefaultConfig(""); AddGrpcPortToConfig(got.get(), 8080); std::unique_ptr want = @@ -76,30 +93,32 @@ TEST(ApplicationConfiguration, DefaultPlusGrpcPort) { ASSERT_EQ(true, ValidApplicationConfig(*got)); } -TEST(ApplicationConfiguration, Valid) { +TEST_F(ApplicationConfigurationTest, Valid) { auto config = ConfigFrom("oak/common/testdata/default.textproto"); ASSERT_EQ(true, ValidApplicationConfig(*config)); } -TEST(ApplicationConfiguration, DuplicateConfigName) { +TEST_F(ApplicationConfigurationTest, DuplicateConfigName) { auto config = ConfigFrom("oak/common/testdata/dup_config_name.textproto"); ASSERT_EQ(false, ValidApplicationConfig(*config)); } -TEST(ApplicationConfiguration, MultipleLogNodes) { +TEST_F(ApplicationConfigurationTest, MultipleLogNodes) { // Two log configs are OK. auto config = ConfigFrom("oak/common/testdata/two_log.textproto"); ASSERT_EQ(true, ValidApplicationConfig(*config)); } -TEST(ApplicationConfiguration, NonWasmCode) { +TEST_F(ApplicationConfigurationTest, NonWasmCode) { auto config = ConfigFrom("oak/common/testdata/missing_wasm.textproto"); ASSERT_EQ(false, ValidApplicationConfig(*config)); } -TEST(ApplicationConfiguration, DuplicateWasmName) { +TEST_F(ApplicationConfigurationTest, DuplicateWasmName) { auto config = ConfigFrom("oak/common/testdata/dup_wasm.textproto"); ASSERT_EQ(false, ValidApplicationConfig(*config)); } +} // namespace + } // namespace oak diff --git a/oak/common/utils.cc b/oak/common/utils.cc index 054d932562c..3b82b417421 100644 --- a/oak/common/utils.cc +++ b/oak/common/utils.cc @@ -14,31 +14,28 @@ * limitations under the License. */ -#include "oak/common/utils.h" - #include #include "asylo/util/logging.h" +#include "oak/common/utils.h" namespace oak { namespace utils { -// Reads a binary file and returns its contents as a std::string. -std::string read_file(const std::string& module_path) { - std::ifstream t(module_path, std::ifstream::in); +std::string read_file(const std::string& filename) { + std::ifstream t(filename, std::ifstream::in); if (!t.is_open()) { - LOG(QFATAL) << "Could not open module " << module_path; + LOG(QFATAL) << "Could not open file " << filename; } std::stringstream buffer; buffer << t.rdbuf(); return buffer.str(); } -// Writes `data` string into a binary `file`. -void write_file(const std::string& data, const std::string& file) { - std::ofstream t(file, std::ofstream::out); +void write_file(const std::string& data, const std::string& filename) { + std::ofstream t(filename, std::ofstream::out); if (!t.is_open()) { - LOG(QFATAL) << "Could not open file " << file; + LOG(QFATAL) << "Could not open file " << filename; } t << data; t.close(); diff --git a/oak/common/utils.h b/oak/common/utils.h index a9932631f11..7aaf1fc61ea 100644 --- a/oak/common/utils.h +++ b/oak/common/utils.h @@ -23,10 +23,10 @@ namespace oak { namespace utils { // Reads a binary file and returns its contents as a std::string. -std::string read_file(const std::string& module_path); +std::string read_file(const std::string& filename); -// Writes `data` string into a binary `file`. -void write_file(const std::string& data, const std::string& file); +// Writes `data` string into a binary file. +void write_file(const std::string& data, const std::string& filename); } // namespace utils } // namespace oak From 442c813a684c61a3c2277018870d7f02107a700d Mon Sep 17 00:00:00 2001 From: Ivan Petrov Date: Mon, 13 Jan 2020 15:00:33 +0000 Subject: [PATCH 7/9] Update tests --- oak/common/app_config_test.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/oak/common/app_config_test.cc b/oak/common/app_config_test.cc index 838608aa6ce..96bfe17085e 100644 --- a/oak/common/app_config_test.cc +++ b/oak/common/app_config_test.cc @@ -30,7 +30,7 @@ namespace { // Fixture class for testing `ApplicationConfiguration` correctness. class ApplicationConfigurationTest : public ::testing::Test { protected: - virtual void TearDown() { + void TearDown() override { // Clean up temporary files. std::remove(kTmpFilename.c_str()); } From 92f5365d47e97e8d33f6d98447a8cb12ae628210 Mon Sep 17 00:00:00 2001 From: Ivan Petrov Date: Mon, 13 Jan 2020 18:53:13 +0000 Subject: [PATCH 8/9] Fix formatting --- oak/common/app_config_serializer.cc | 12 +++++++----- oak/common/app_config_test.cc | 2 ++ 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/oak/common/app_config_serializer.cc b/oak/common/app_config_serializer.cc index ca0b04b3980..d93becb7706 100644 --- a/oak/common/app_config_serializer.cc +++ b/oak/common/app_config_serializer.cc @@ -41,20 +41,22 @@ int main(int argc, char* argv[]) { // Create an application configuration. std::string module_bytes = oak::utils::read_file(absl::GetFlag(FLAGS_module)); - std::unique_ptr application_config = oak::DefaultConfig( - module_bytes); + std::unique_ptr application_config = + oak::DefaultConfig(module_bytes); // Set application configuration parameters. if (absl::GetFlag(FLAGS_logging)) { oak::AddLoggingToConfig(application_config.get()); } - oak::AddStorageToConfig(application_config.get(), absl::GetFlag(FLAGS_storage_address)); + std::string storage_address = absl::GetFlag(FLAGS_storage_address); + if (!storage_address.empty()) { + oak::AddStorageToConfig(application_config.get(), storage_address); + } oak::AddGrpcPortToConfig(application_config.get(), absl::GetFlag(FLAGS_grpc_port)); if (ValidApplicationConfig(application_config.get())) { oak::WriteConfigToFile(application_config.get(), output_file); - } - else { + } else { LOG(QFATAL) << "Incorrect application configuration"; return 1; } diff --git a/oak/common/app_config_test.cc b/oak/common/app_config_test.cc index 96bfe17085e..3b8d78db1cd 100644 --- a/oak/common/app_config_test.cc +++ b/oak/common/app_config_test.cc @@ -59,6 +59,8 @@ TEST_F(ApplicationConfigurationTest, Default) { } TEST_F(ApplicationConfigurationTest, ReadWriteFile) { + std::ifstream existing_file(kTmpFilename, std::ifstream::in); + ASSERT_FALSE(existing_file.is_open()); std::unique_ptr want = DefaultConfig(""); WriteConfigToFile(want.get(), kTmpFilename); std::unique_ptr got = ReadConfigFromFile(kTmpFilename); From caa047c7f1497c1478c8b51000e50d9868b7209d Mon Sep 17 00:00:00 2001 From: Ivan Petrov Date: Tue, 14 Jan 2020 11:26:29 +0000 Subject: [PATCH 9/9] Delete invalid check --- oak/common/app_config_serializer.cc | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/oak/common/app_config_serializer.cc b/oak/common/app_config_serializer.cc index d93becb7706..30c9de462ed 100644 --- a/oak/common/app_config_serializer.cc +++ b/oak/common/app_config_serializer.cc @@ -54,12 +54,6 @@ int main(int argc, char* argv[]) { } oak::AddGrpcPortToConfig(application_config.get(), absl::GetFlag(FLAGS_grpc_port)); - if (ValidApplicationConfig(application_config.get())) { - oak::WriteConfigToFile(application_config.get(), output_file); - } else { - LOG(QFATAL) << "Incorrect application configuration"; - return 1; - } - + oak::WriteConfigToFile(application_config.get(), output_file); return 0; }