From 895464f50d9b9e9edb294c98056b3c08a9378ada Mon Sep 17 00:00:00 2001 From: "Scott E. Graves" Date: Fri, 20 Dec 2024 07:28:56 -0600 Subject: [PATCH] unit tests and fixes --- repertory/repertory_test/src/atomic_test.cpp | 63 +++++++ .../src/json_serialize_test.cpp | 163 ++++++++++++++++++ 2 files changed, 226 insertions(+) create mode 100644 repertory/repertory_test/src/atomic_test.cpp create mode 100644 repertory/repertory_test/src/json_serialize_test.cpp diff --git a/repertory/repertory_test/src/atomic_test.cpp b/repertory/repertory_test/src/atomic_test.cpp new file mode 100644 index 00000000..809fd743 --- /dev/null +++ b/repertory/repertory_test/src/atomic_test.cpp @@ -0,0 +1,63 @@ +/* + Copyright <2018-2024> + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in all + copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. +*/ +#include "test_common.hpp" + +#include "types/repertory.hpp" + +namespace repertory { +TEST(atomic, atomic_primitive) { + atomic value; + value = 5U; + EXPECT_EQ(5U, static_cast(value)); +} + +TEST(atomic, atomic_primitive_equality) { + atomic value1{5U}; + atomic value2{5U}; + EXPECT_EQ(value1, value1); + EXPECT_EQ(value2, value2); + EXPECT_EQ(value1, value2); + EXPECT_EQ(static_cast(value1), 5U); + EXPECT_EQ(static_cast(value2), 5U); +} + +TEST(atomic, atomic_primitive_inequality) { + atomic value1{5U}; + atomic value2{6U}; + EXPECT_NE(value1, value2); + EXPECT_NE(static_cast(value1), 6U); + EXPECT_NE(static_cast(value2), 5U); +} + +TEST(atomic, atomic_struct) { + atomic value{ + encrypt_config{ + .encryption_token = "token", + .path = "path", + }, + }; + + auto data = static_cast(value); + EXPECT_STREQ("token", data.encryption_token.c_str()); + EXPECT_STREQ("path", data.path.c_str()); +} +} // namespace repertory diff --git a/repertory/repertory_test/src/json_serialize_test.cpp b/repertory/repertory_test/src/json_serialize_test.cpp new file mode 100644 index 00000000..88661876 --- /dev/null +++ b/repertory/repertory_test/src/json_serialize_test.cpp @@ -0,0 +1,163 @@ +/* + Copyright <2018-2024> + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in all + copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. +*/ +#include "test_common.hpp" + +#include "types/remote.hpp" +#include "types/repertory.hpp" + +namespace repertory { +TEST(json_serialize, can_handle_directory_item) { + directory_item cfg{ + "api", "parent", true, 2U, {{META_DIRECTORY, "true"}}, false, + }; + + json data(cfg); + EXPECT_STREQ("api", data.at("ApiPath").get().c_str()); + EXPECT_STREQ("parent", data.at("ApiParent").get().c_str()); + EXPECT_TRUE(data.at("Directory").get()); + EXPECT_STREQ("true", data.at("meta").at(META_DIRECTORY).get()); + EXPECT_FALSE(data.at("Resolved").get()); + + { + auto cfg2 = data.get(); + EXPECT_STREQ(cfg2.api_path.c_str(), cfg.api_path.c_str()); + EXPECT_STREQ(cfg2.api_parent.c_str(), cfg.api_parent.c_str()); + EXPECT_EQ(cfg2.directory, cfg.directory); + EXPECT_STREQ(cfg2.meta.at(META_DIRECTORY).c_str(), + cfg.meta.at(META_DIRECTORY).c_str()); + EXPECT_EQ(cfg2.resolved, cfg.resolved); + } +} + +TEST(json_serialize, can_handle_encrypt_config) { + encrypt_config cfg{ + "token", + "path", + }; + + json data(cfg); + EXPECT_STREQ("token", data.at("EncryptionToken").get().c_str()); + EXPECT_STREQ("path", data.at("Path").get().c_str()); + + { + auto cfg2 = data.get(); + EXPECT_STREQ(cfg2.encryption_token.c_str(), cfg.encryption_token.c_str()); + EXPECT_STREQ(cfg2.path.c_str(), cfg.path.c_str()); + } +} + +TEST(json_serialize, can_handle_host_config) { + host_config cfg{ + "agent", "pwd", "user", 1024U, "host", "path", "http", 11U, + }; + + json data(cfg); + EXPECT_STREQ("agent", data.at("AgentString").get().c_str()); + EXPECT_STREQ("pwd", data.at("ApiPassword").get().c_str()); + EXPECT_STREQ("user", data.at("ApiUser").get().c_str()); + EXPECT_EQ(1024U, data.at("ApiPort").get()); + EXPECT_STREQ("host", data.at("HostNameOrIp").get().c_str()); + EXPECT_STREQ("path", data.at("Path").get().c_str()); + EXPECT_STREQ("http", data.at("Protocol").get().c_str()); + EXPECT_EQ(11U, data.at("TimeoutMs").get()); + + { + auto cfg2 = data.get(); + EXPECT_STREQ(cfg2.agent_string.c_str(), cfg.agent_string.c_str()); + EXPECT_STREQ(cfg2.api_password.c_str(), cfg.api_password.c_str()); + EXPECT_STREQ(cfg2.api_user.c_str(), cfg.api_user.c_str()); + EXPECT_EQ(cfg2.api_port, cfg.api_port); + EXPECT_STREQ(cfg2.host_name_or_ip.c_str(), cfg.host_name_or_ip.c_str()); + EXPECT_STREQ(cfg2.path.c_str(), cfg.path.c_str()); + EXPECT_STREQ(cfg2.protocol.c_str(), cfg.protocol.c_str()); + EXPECT_EQ(cfg2.timeout_ms, cfg.timeout_ms); + } +} + +TEST(json_serialize, can_handle_remote_config) { + remote::remote_config cfg{ + 1024U, "token", "host", 11U, 20U, 21U, + }; + + json data(cfg); + EXPECT_EQ(1024U, data.at("ApiPort").get()); + EXPECT_STREQ("token", data.at("EncryptionToken").get().c_str()); + EXPECT_STREQ("host", data.at("HostNameOrIp").get().c_str()); + EXPECT_EQ(11U, data.at("MaxConnections").get()); + EXPECT_EQ(20U, data.at("ReceiveTimeoutMs").get()); + EXPECT_EQ(21U, data.at("SendTimeoutMs").get()); + + { + auto cfg2 = data.get(); + EXPECT_EQ(cfg2.api_port, cfg.api_port); + EXPECT_STREQ(cfg2.encryption_token.c_str(), cfg.encryption_token.c_str()); + EXPECT_STREQ(cfg2.host_name_or_ip.c_str(), cfg.host_name_or_ip.c_str()); + EXPECT_EQ(cfg2.max_connections, cfg.max_connections); + EXPECT_EQ(cfg2.recv_timeout_ms, cfg.recv_timeout_ms); + EXPECT_EQ(cfg2.send_timeout_ms, cfg.send_timeout_ms); + } +} + +TEST(json_serialize, can_handle_s3_config) { + s3_config cfg{ + "access", "bucket", "token", "region", "secret", 31U, "url", true, false, + }; + + json data(cfg); + EXPECT_STREQ("access", data.at("AccessKey").get().c_str()); + EXPECT_STREQ("bucket", data.at("Bucket").get().c_str()); + EXPECT_STREQ("token", data.at("EncryptionToken").get().c_str()); + EXPECT_STREQ("region", data.at("Region").get().c_str()); + EXPECT_STREQ("secret", data.at("SecretKey").get().c_str()); + EXPECT_EQ(31U, data.at("TimeoutMs").get()); + EXPECT_STREQ("url", data.at("URL").get().c_str()); + EXPECT_TRUE(data.at("UsePathStyle").get()); + EXPECT_FALSE(data.at("UseRegionInURL").get()); + + { + auto cfg2 = data.get(); + EXPECT_STREQ(cfg2.access_key.c_str(), cfg.access_key.c_str()); + EXPECT_STREQ(cfg2.bucket.c_str(), cfg.bucket.c_str()); + EXPECT_STREQ(cfg2.encryption_token.c_str(), cfg.encryption_token.c_str()); + EXPECT_STREQ(cfg2.region.c_str(), cfg.region.c_str()); + EXPECT_STREQ(cfg2.secret_key.c_str(), cfg.secret_key.c_str()); + EXPECT_EQ(cfg2.timeout_ms, cfg.timeout_ms); + EXPECT_STREQ(cfg2.url.c_str(), cfg.url.c_str()); + EXPECT_EQ(cfg2.use_path_style, cfg.use_path_style); + EXPECT_EQ(cfg2.use_region_in_url, cfg.use_region_in_url); + } +} + +TEST(json_serialize, can_handle_sia_config) { + sia_config cfg{ + "bucket", + }; + + json data(cfg); + EXPECT_STREQ("bucket", data.at("Bucket").get().c_str()); + + { + auto cfg2 = data.get(); + EXPECT_STREQ(cfg2.bucket.c_str(), cfg.bucket.c_str()); + } +} +} // namespace repertory