mirror of
https://github.com/privatevoid-net/nix-super.git
synced 2024-11-25 15:26:17 +02:00
refactor fetchers::PublicKey
tests
This commit is contained in:
parent
03eb4f7baa
commit
4ff7f5aa9c
3 changed files with 44 additions and 9 deletions
4
tests/unit/libfetchers/data/public-key/defaultType.json
Normal file
4
tests/unit/libfetchers/data/public-key/defaultType.json
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
{
|
||||||
|
"key": "ABCDE",
|
||||||
|
"type": "ssh-ed25519"
|
||||||
|
}
|
4
tests/unit/libfetchers/data/public-key/simple.json
Normal file
4
tests/unit/libfetchers/data/public-key/simple.json
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
{
|
||||||
|
"key": "ABCDE",
|
||||||
|
"type": "ssh-rsa"
|
||||||
|
}
|
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue