Merge pull request #10602 from haenoe/json-infra-tests-misc

Json infra tests misc
This commit is contained in:
John Ericson 2024-04-25 16:53:51 -04:00 committed by GitHub
commit 4722b0c9e9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 64 additions and 13 deletions

View File

@ -419,9 +419,13 @@ namespace nlohmann {
using namespace nix; using namespace nix;
fetchers::PublicKey adl_serializer<fetchers::PublicKey>::from_json(const json & json) { fetchers::PublicKey adl_serializer<fetchers::PublicKey>::from_json(const json & json) {
auto type = optionalValueAt(json, "type").value_or("ssh-ed25519"); fetchers::PublicKey res = { };
auto key = valueAt(json, "key"); if (auto type = optionalValueAt(json, "type"))
return fetchers::PublicKey { getString(type), getString(key) }; res.type = getString(*type);
res.key = getString(valueAt(json, "key"));
return res;
} }
void adl_serializer<fetchers::PublicKey>::to_json(json & json, fetchers::PublicKey p) { void adl_serializer<fetchers::PublicKey>::to_json(json & json, fetchers::PublicKey p) {

View File

@ -0,0 +1,4 @@
{
"key": "ABCDE",
"type": "ssh-ed25519"
}

View File

@ -0,0 +1,4 @@
{
"key": "ABCDE",
"type": "ssh-rsa"
}

View File

@ -1,18 +1,45 @@
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include "fetchers.hh" #include "fetchers.hh"
#include "json-utils.hh" #include "json-utils.hh"
#include <nlohmann/json.hpp>
#include "tests/characterization.hh"
namespace nix { namespace nix {
TEST(PublicKey, jsonSerialization) {
auto json = nlohmann::json(fetchers::PublicKey { .key = "ABCDE" });
ASSERT_EQ(json, R"({ "key": "ABCDE", "type": "ssh-ed25519" })"_json); using nlohmann::json;
}
TEST(PublicKey, jsonDeserialization) {
auto pubKeyJson = R"({ "key": "ABCDE", "type": "ssh-ed25519" })"_json;
fetchers::PublicKey pubKey = pubKeyJson;
ASSERT_EQ(pubKey.key, "ABCDE"); class PublicKeyTest : public CharacterizationTest
ASSERT_EQ(pubKey.type, "ssh-ed25519"); {
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
} }

View File

@ -169,7 +169,19 @@ TEST(optionalValueAt, existing) {
TEST(optionalValueAt, empty) { TEST(optionalValueAt, empty) {
auto json = R"({})"_json; 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 */ } /* namespace nix */