2024-03-04 03:24:23 +00:00
|
|
|
#include <nlohmann/json.hpp>
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
|
|
|
#include "tests/libstore.hh"
|
2024-03-10 08:32:43 +00:00
|
|
|
#include "tests/characterization.hh"
|
2024-03-04 03:24:23 +00:00
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
|
|
|
template<class Proto, const char * protocolDir>
|
|
|
|
class ProtoTest : public LibStoreTest
|
|
|
|
{
|
2023-10-20 13:34:26 +00:00
|
|
|
protected:
|
2024-03-04 03:24:23 +00:00
|
|
|
Path unitTestData = getUnitTestData() + "/libstore/" + protocolDir;
|
|
|
|
|
|
|
|
Path goldenMaster(std::string_view testStem) {
|
|
|
|
return unitTestData + "/" + testStem + ".bin";
|
|
|
|
}
|
2023-10-20 13:34:26 +00:00
|
|
|
};
|
2024-03-04 03:24:23 +00:00
|
|
|
|
2023-10-20 13:34:26 +00:00
|
|
|
template<class Proto, const char * protocolDir>
|
|
|
|
class VersionedProtoTest : public ProtoTest<Proto, protocolDir>
|
|
|
|
{
|
|
|
|
public:
|
2024-03-04 03:24:23 +00:00
|
|
|
/**
|
|
|
|
* Golden test for `T` reading
|
|
|
|
*/
|
|
|
|
template<typename T>
|
2023-10-20 13:34:26 +00:00
|
|
|
void readTest(PathView testStem, typename Proto::Version version, T value)
|
2024-03-04 03:24:23 +00:00
|
|
|
{
|
|
|
|
if (testAccept())
|
|
|
|
{
|
2024-03-04 04:21:10 +00:00
|
|
|
GTEST_SKIP() << cannotReadGoldenMaster;
|
2024-03-04 03:24:23 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-10-20 13:34:26 +00:00
|
|
|
auto expected = readFile(ProtoTest<Proto, protocolDir>::goldenMaster(testStem));
|
2024-03-04 03:24:23 +00:00
|
|
|
|
|
|
|
T got = ({
|
|
|
|
StringSource from { expected };
|
2023-10-20 13:34:26 +00:00
|
|
|
Proto::template Serialise<T>::read(
|
|
|
|
*LibStoreTest::store,
|
|
|
|
typename Proto::ReadConn {
|
|
|
|
.from = from,
|
|
|
|
.version = version,
|
|
|
|
});
|
2024-03-04 03:24:23 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
ASSERT_EQ(got, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Golden test for `T` write
|
|
|
|
*/
|
|
|
|
template<typename T>
|
2023-10-20 13:34:26 +00:00
|
|
|
void writeTest(PathView testStem, typename Proto::Version version, const T & value)
|
2024-03-04 03:24:23 +00:00
|
|
|
{
|
2023-10-20 13:34:26 +00:00
|
|
|
auto file = ProtoTest<Proto, protocolDir>::goldenMaster(testStem);
|
2024-03-04 03:24:23 +00:00
|
|
|
|
|
|
|
StringSink to;
|
|
|
|
Proto::write(
|
2023-10-20 13:34:26 +00:00
|
|
|
*LibStoreTest::store,
|
|
|
|
typename Proto::WriteConn {
|
|
|
|
.to = to,
|
|
|
|
.version = version,
|
|
|
|
},
|
2024-03-04 03:24:23 +00:00
|
|
|
value);
|
|
|
|
|
|
|
|
if (testAccept())
|
|
|
|
{
|
|
|
|
createDirs(dirOf(file));
|
|
|
|
writeFile(file, to.s);
|
2024-03-04 04:21:10 +00:00
|
|
|
GTEST_SKIP() << updatingGoldenMaster;
|
2024-03-04 03:24:23 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto expected = readFile(file);
|
|
|
|
ASSERT_EQ(to.s, expected);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-10-20 13:34:26 +00:00
|
|
|
#define VERSIONED_CHARACTERIZATION_TEST(FIXTURE, NAME, STEM, VERSION, VALUE) \
|
2024-03-04 03:24:23 +00:00
|
|
|
TEST_F(FIXTURE, NAME ## _read) { \
|
2023-10-20 13:34:26 +00:00
|
|
|
readTest(STEM, VERSION, VALUE); \
|
2024-03-04 03:24:23 +00:00
|
|
|
} \
|
|
|
|
TEST_F(FIXTURE, NAME ## _write) { \
|
2023-10-20 13:34:26 +00:00
|
|
|
writeTest(STEM, VERSION, VALUE); \
|
2024-03-04 03:24:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|