2024-03-04 03:24:23 +00:00
|
|
|
#include <regex>
|
|
|
|
|
|
|
|
#include <nlohmann/json.hpp>
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
|
|
|
#include "common-protocol.hh"
|
|
|
|
#include "common-protocol-impl.hh"
|
|
|
|
#include "build-result.hh"
|
|
|
|
#include "protocol.hh"
|
2024-03-10 08:32:43 +00:00
|
|
|
#include "tests/characterization.hh"
|
2024-03-04 03:24:23 +00:00
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
|
|
|
const char commonProtoDir[] = "common-protocol";
|
|
|
|
|
2023-10-20 13:34:26 +00:00
|
|
|
class CommonProtoTest : public ProtoTest<CommonProto, commonProtoDir>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Golden test for `T` reading
|
|
|
|
*/
|
|
|
|
template<typename T>
|
|
|
|
void readTest(PathView testStem, T value)
|
|
|
|
{
|
|
|
|
if (testAccept())
|
|
|
|
{
|
2024-03-04 04:21:10 +00:00
|
|
|
GTEST_SKIP() << cannotReadGoldenMaster;
|
2023-10-20 13:34:26 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-03-04 04:21:10 +00:00
|
|
|
auto encoded = readFile(goldenMaster(testStem));
|
2023-10-20 13:34:26 +00:00
|
|
|
|
|
|
|
T got = ({
|
2024-03-04 04:21:10 +00:00
|
|
|
StringSource from { encoded };
|
2023-10-20 13:34:26 +00:00
|
|
|
CommonProto::Serialise<T>::read(
|
|
|
|
*store,
|
|
|
|
CommonProto::ReadConn { .from = from });
|
|
|
|
});
|
|
|
|
|
|
|
|
ASSERT_EQ(got, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Golden test for `T` write
|
|
|
|
*/
|
|
|
|
template<typename T>
|
|
|
|
void writeTest(PathView testStem, const T & value)
|
|
|
|
{
|
|
|
|
auto file = goldenMaster(testStem);
|
|
|
|
|
|
|
|
StringSink to;
|
2024-05-18 00:35:34 +00:00
|
|
|
to << CommonProto::write(
|
2023-10-20 13:34:26 +00:00
|
|
|
*store,
|
|
|
|
CommonProto::WriteConn { .to = to },
|
|
|
|
value);
|
|
|
|
|
|
|
|
if (testAccept())
|
|
|
|
{
|
|
|
|
createDirs(dirOf(file));
|
|
|
|
writeFile(file, to.s);
|
2024-03-04 04:21:10 +00:00
|
|
|
GTEST_SKIP() << updatingGoldenMaster;
|
2023-10-20 13:34:26 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto expected = readFile(file);
|
|
|
|
ASSERT_EQ(to.s, expected);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#define CHARACTERIZATION_TEST(NAME, STEM, VALUE) \
|
|
|
|
TEST_F(CommonProtoTest, NAME ## _read) { \
|
|
|
|
readTest(STEM, VALUE); \
|
|
|
|
} \
|
|
|
|
TEST_F(CommonProtoTest, NAME ## _write) { \
|
|
|
|
writeTest(STEM, VALUE); \
|
|
|
|
}
|
2024-03-04 03:24:23 +00:00
|
|
|
|
|
|
|
CHARACTERIZATION_TEST(
|
|
|
|
string,
|
|
|
|
"string",
|
|
|
|
(std::tuple<std::string, std::string, std::string, std::string, std::string> {
|
|
|
|
"",
|
|
|
|
"hi",
|
|
|
|
"white rabbit",
|
|
|
|
"大白兔",
|
|
|
|
"oh no \0\0\0 what was that!",
|
|
|
|
}))
|
|
|
|
|
|
|
|
CHARACTERIZATION_TEST(
|
|
|
|
storePath,
|
|
|
|
"store-path",
|
|
|
|
(std::tuple<StorePath, StorePath> {
|
|
|
|
StorePath { "g1w7hy3qg1w7hy3qg1w7hy3qg1w7hy3q-foo" },
|
|
|
|
StorePath { "g1w7hy3qg1w7hy3qg1w7hy3qg1w7hy3q-foo-bar" },
|
|
|
|
}))
|
|
|
|
|
|
|
|
CHARACTERIZATION_TEST(
|
|
|
|
contentAddress,
|
|
|
|
"content-address",
|
|
|
|
(std::tuple<ContentAddress, ContentAddress, ContentAddress> {
|
|
|
|
ContentAddress {
|
|
|
|
.method = TextIngestionMethod {},
|
|
|
|
.hash = hashString(HashType::htSHA256, "Derive(...)"),
|
|
|
|
},
|
|
|
|
ContentAddress {
|
|
|
|
.method = FileIngestionMethod::Flat,
|
|
|
|
.hash = hashString(HashType::htSHA1, "blob blob..."),
|
|
|
|
},
|
|
|
|
ContentAddress {
|
|
|
|
.method = FileIngestionMethod::Recursive,
|
|
|
|
.hash = hashString(HashType::htSHA256, "(...)"),
|
|
|
|
},
|
|
|
|
}))
|
|
|
|
|
|
|
|
CHARACTERIZATION_TEST(
|
|
|
|
drvOutput,
|
|
|
|
"drv-output",
|
|
|
|
(std::tuple<DrvOutput, DrvOutput> {
|
|
|
|
{
|
|
|
|
.drvHash = Hash::parseSRI("sha256-FePFYIlMuycIXPZbWi7LGEiMmZSX9FMbaQenWBzm1Sc="),
|
|
|
|
.outputName = "baz",
|
|
|
|
},
|
|
|
|
DrvOutput {
|
|
|
|
.drvHash = Hash::parseSRI("sha256-b4afnqKCO9oWXgYHb9DeQ2berSwOjS27rSd9TxXDc/U="),
|
|
|
|
.outputName = "quux",
|
|
|
|
},
|
|
|
|
}))
|
|
|
|
|
|
|
|
CHARACTERIZATION_TEST(
|
|
|
|
realisation,
|
|
|
|
"realisation",
|
|
|
|
(std::tuple<Realisation, Realisation> {
|
|
|
|
Realisation {
|
|
|
|
.id = DrvOutput {
|
|
|
|
.drvHash = Hash::parseSRI("sha256-FePFYIlMuycIXPZbWi7LGEiMmZSX9FMbaQenWBzm1Sc="),
|
|
|
|
.outputName = "baz",
|
|
|
|
},
|
|
|
|
.outPath = StorePath { "g1w7hy3qg1w7hy3qg1w7hy3qg1w7hy3q-foo" },
|
|
|
|
.signatures = { "asdf", "qwer" },
|
|
|
|
},
|
|
|
|
Realisation {
|
|
|
|
.id = {
|
|
|
|
.drvHash = Hash::parseSRI("sha256-FePFYIlMuycIXPZbWi7LGEiMmZSX9FMbaQenWBzm1Sc="),
|
|
|
|
.outputName = "baz",
|
|
|
|
},
|
|
|
|
.outPath = StorePath { "g1w7hy3qg1w7hy3qg1w7hy3qg1w7hy3q-foo" },
|
|
|
|
.signatures = { "asdf", "qwer" },
|
|
|
|
.dependentRealisations = {
|
|
|
|
{
|
|
|
|
DrvOutput {
|
|
|
|
.drvHash = Hash::parseSRI("sha256-b4afnqKCO9oWXgYHb9DeQ2berSwOjS27rSd9TxXDc/U="),
|
|
|
|
.outputName = "quux",
|
|
|
|
},
|
|
|
|
StorePath { "g1w7hy3qg1w7hy3qg1w7hy3qg1w7hy3q-foo" },
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}))
|
|
|
|
|
|
|
|
CHARACTERIZATION_TEST(
|
|
|
|
vector,
|
|
|
|
"vector",
|
|
|
|
(std::tuple<std::vector<std::string>, std::vector<std::string>, std::vector<std::string>, std::vector<std::vector<std::string>>> {
|
|
|
|
{ },
|
|
|
|
{ "" },
|
|
|
|
{ "", "foo", "bar" },
|
|
|
|
{ {}, { "" }, { "", "1", "2" } },
|
|
|
|
}))
|
|
|
|
|
|
|
|
CHARACTERIZATION_TEST(
|
|
|
|
set,
|
|
|
|
"set",
|
|
|
|
(std::tuple<std::set<std::string>, std::set<std::string>, std::set<std::string>, std::set<std::set<std::string>>> {
|
|
|
|
{ },
|
|
|
|
{ "" },
|
|
|
|
{ "", "foo", "bar" },
|
|
|
|
{ {}, { "" }, { "", "1", "2" } },
|
|
|
|
}))
|
|
|
|
|
|
|
|
CHARACTERIZATION_TEST(
|
|
|
|
optionalStorePath,
|
|
|
|
"optional-store-path",
|
|
|
|
(std::tuple<std::optional<StorePath>, std::optional<StorePath>> {
|
|
|
|
std::nullopt,
|
|
|
|
std::optional {
|
|
|
|
StorePath { "g1w7hy3qg1w7hy3qg1w7hy3qg1w7hy3q-foo-bar" },
|
|
|
|
},
|
|
|
|
}))
|
|
|
|
|
|
|
|
CHARACTERIZATION_TEST(
|
|
|
|
optionalContentAddress,
|
|
|
|
"optional-content-address",
|
|
|
|
(std::tuple<std::optional<ContentAddress>, std::optional<ContentAddress>> {
|
|
|
|
std::nullopt,
|
|
|
|
std::optional {
|
|
|
|
ContentAddress {
|
|
|
|
.method = FileIngestionMethod::Flat,
|
|
|
|
.hash = hashString(HashType::htSHA1, "blob blob..."),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}))
|
|
|
|
|
|
|
|
}
|