From 4ff7f5aa9cf15a3c8922ecb2c52875f4f8672899 Mon Sep 17 00:00:00 2001 From: HaeNoe Date: Tue, 16 Apr 2024 13:26:20 +0200 Subject: [PATCH 1/3] refactor `fetchers::PublicKey` tests --- .../data/public-key/defaultType.json | 4 ++ .../libfetchers/data/public-key/simple.json | 4 ++ tests/unit/libfetchers/public-key.cc | 45 +++++++++++++++---- 3 files changed, 44 insertions(+), 9 deletions(-) create mode 100644 tests/unit/libfetchers/data/public-key/defaultType.json create mode 100644 tests/unit/libfetchers/data/public-key/simple.json diff --git a/tests/unit/libfetchers/data/public-key/defaultType.json b/tests/unit/libfetchers/data/public-key/defaultType.json new file mode 100644 index 000000000..43f02a420 --- /dev/null +++ b/tests/unit/libfetchers/data/public-key/defaultType.json @@ -0,0 +1,4 @@ +{ + "key": "ABCDE", + "type": "ssh-ed25519" +} diff --git a/tests/unit/libfetchers/data/public-key/simple.json b/tests/unit/libfetchers/data/public-key/simple.json new file mode 100644 index 000000000..f83b927ac --- /dev/null +++ b/tests/unit/libfetchers/data/public-key/simple.json @@ -0,0 +1,4 @@ +{ + "key": "ABCDE", + "type": "ssh-rsa" +} diff --git a/tests/unit/libfetchers/public-key.cc b/tests/unit/libfetchers/public-key.cc index fcd5c3af0..941ae9db7 100644 --- a/tests/unit/libfetchers/public-key.cc +++ b/tests/unit/libfetchers/public-key.cc @@ -1,18 +1,45 @@ #include #include "fetchers.hh" #include "json-utils.hh" +#include +#include "tests/characterization.hh" namespace nix { - TEST(PublicKey, jsonSerialization) { - auto json = nlohmann::json(fetchers::PublicKey { .key = "ABCDE" }); - ASSERT_EQ(json, R"({ "key": "ABCDE", "type": "ssh-ed25519" })"_json); - } - TEST(PublicKey, jsonDeserialization) { - auto pubKeyJson = R"({ "key": "ABCDE", "type": "ssh-ed25519" })"_json; - fetchers::PublicKey pubKey = pubKeyJson; +using nlohmann::json; - ASSERT_EQ(pubKey.key, "ABCDE"); - ASSERT_EQ(pubKey.type, "ssh-ed25519"); +class PublicKeyTest : public CharacterizationTest +{ + Path unitTestData = getUnitTestData() + "/public-key"; + +public: + Path goldenMaster(std::string_view testStem) const override { + return unitTestData + "/" + testStem; } +}; + +#define TEST_JSON(FIXTURE, NAME, VAL) \ + TEST_F(FIXTURE, PublicKey_ ## NAME ## _from_json) { \ + readTest(#NAME ".json", [&](const auto & encoded_) { \ + fetchers::PublicKey expected { VAL }; \ + auto got = nlohmann::json::parse(encoded_); \ + ASSERT_EQ(got, expected); \ + }); \ + } \ + \ + TEST_F(FIXTURE, PublicKey_ ## NAME ## _to_json) { \ + writeTest(#NAME ".json", [&]() -> json { \ + return nlohmann::json(fetchers::PublicKey { VAL }); \ + }, [](const auto & file) { \ + return json::parse(readFile(file)); \ + }, [](const auto & file, const auto & got) { \ + return writeFile(file, got.dump(2) + "\n"); \ + }); \ + } + +TEST_JSON(PublicKeyTest, simple, (fetchers::PublicKey { .type = "ssh-rsa", .key = "ABCDE" })) + +TEST_JSON(PublicKeyTest, defaultType, fetchers::PublicKey { .key = "ABCDE" }) + +#undef TEST_JSON } From c73172e9864dbb0b38e480c9e284245bbbd616e7 Mon Sep 17 00:00:00 2001 From: HaeNoe Date: Tue, 16 Apr 2024 13:37:39 +0200 Subject: [PATCH 2/3] add unit tests for `getNullable` --- tests/unit/libutil/json-utils.cc | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/tests/unit/libutil/json-utils.cc b/tests/unit/libutil/json-utils.cc index ec653fff5..c9370a74b 100644 --- a/tests/unit/libutil/json-utils.cc +++ b/tests/unit/libutil/json-utils.cc @@ -169,7 +169,19 @@ TEST(optionalValueAt, existing) { TEST(optionalValueAt, empty) { auto json = R"({})"_json; - ASSERT_EQ(optionalValueAt(json, "string2"), std::nullopt); + ASSERT_EQ(optionalValueAt(json, "string"), std::nullopt); +} + +TEST(getNullable, null) { + auto json = R"(null)"_json; + + ASSERT_EQ(getNullable(json), std::nullopt); +} + +TEST(getNullable, empty) { + auto json = R"({})"_json; + + ASSERT_EQ(getNullable(json), std::optional { R"({})"_json }); } } /* namespace nix */ From 943a877a6a189bd3ccfc77b6100ee7df80038b5a Mon Sep 17 00:00:00 2001 From: HaeNoe Date: Tue, 16 Apr 2024 13:48:58 +0200 Subject: [PATCH 3/3] use default value in `fetchers::PublicKey` json deserialization --- src/libfetchers/fetchers.cc | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/libfetchers/fetchers.cc b/src/libfetchers/fetchers.cc index a06d931db..0577b8d9d 100644 --- a/src/libfetchers/fetchers.cc +++ b/src/libfetchers/fetchers.cc @@ -419,9 +419,13 @@ namespace nlohmann { using namespace nix; fetchers::PublicKey adl_serializer::from_json(const json & json) { - auto type = optionalValueAt(json, "type").value_or("ssh-ed25519"); - auto key = valueAt(json, "key"); - return fetchers::PublicKey { getString(type), getString(key) }; + fetchers::PublicKey res = { }; + if (auto type = optionalValueAt(json, "type")) + res.type = getString(*type); + + res.key = getString(valueAt(json, "key")); + + return res; } void adl_serializer::to_json(json & json, fetchers::PublicKey p) {