mirror of
https://github.com/privatevoid-net/nix-super.git
synced 2024-11-24 14:56:15 +02:00
a97a08411c
We're not replacing `Path` in exposed definitions in many cases, but just adding alternatives. This will allow us to "top down" change `Path` to `std::fileysystem::path`, and then we can remove the `Path`-using utilities which will become unused. Also add some test files which we forgot to include in the libutil unit tests `meson.build`. Co-Authored-By: siddhantCodes <siddhantk232@gmail.com>
54 lines
2.2 KiB
C++
54 lines
2.2 KiB
C++
#include <gtest/gtest.h>
|
|
#include "fetchers.hh"
|
|
#include "json-utils.hh"
|
|
#include <nlohmann/json.hpp>
|
|
#include "tests/characterization.hh"
|
|
|
|
namespace nix {
|
|
|
|
using nlohmann::json;
|
|
|
|
class PublicKeyTest : public CharacterizationTest
|
|
{
|
|
std::filesystem::path unitTestData = getUnitTestData() / "public-key";
|
|
|
|
public:
|
|
std::filesystem::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 }; \
|
|
fetchers::PublicKey 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
|
|
|
|
TEST_F(PublicKeyTest, PublicKey_noRoundTrip_from_json) {
|
|
readTest("noRoundTrip.json", [&](const auto & encoded_) {
|
|
fetchers::PublicKey expected = { .type = "ssh-ed25519", .key = "ABCDE" };
|
|
fetchers::PublicKey got = nlohmann::json::parse(encoded_);
|
|
ASSERT_EQ(got, expected);
|
|
});
|
|
}
|
|
|
|
}
|