nix-super/tests/unit/libfetchers/public-key.cc

46 lines
1.9 KiB
C++
Raw Normal View History

#include <gtest/gtest.h>
#include "fetchers.hh"
#include "json-utils.hh"
2024-04-16 14:26:20 +03:00
#include <nlohmann/json.hpp>
#include "tests/characterization.hh"
namespace nix {
2024-04-16 14:26:20 +03:00
using nlohmann::json;
class PublicKeyTest : public CharacterizationTest
{
Path unitTestData = getUnitTestData() + "/public-key";
public:
Path goldenMaster(std::string_view testStem) const override {
return unitTestData + "/" + testStem;
}
2024-04-16 14:26:20 +03:00
};
2024-04-16 14:26:20 +03:00
#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"); \
}); \
}
2024-04-16 14:26:20 +03:00
TEST_JSON(PublicKeyTest, simple, (fetchers::PublicKey { .type = "ssh-rsa", .key = "ABCDE" }))
TEST_JSON(PublicKeyTest, defaultType, fetchers::PublicKey { .key = "ABCDE" })
#undef TEST_JSON
}