Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 41 additions & 11 deletions src/libstore-test-support/include/nix/store/tests/protocol.hh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include "nix/store/tests/libstore.hh"
#include "nix/util/tests/characterization.hh"
#include "nix/util/tests/json-characterization.hh"

namespace nix {

Expand All @@ -16,12 +17,30 @@ class ProtoTest : public CharacterizationTest

std::filesystem::path goldenMaster(std::string_view testStem) const override
{
return unitTestData / (std::string{testStem + ".bin"});
return unitTestData / testStem;
}

public:
Path storeDir = "/nix/store";
StoreDirConfig store{storeDir};

/**
* Golden test for `T` JSON reading
*/
template<typename T>
void readJsonTest(PathView testStem, const T & expected)
{
nix::readJsonTest(*this, testStem, expected);
}

/**
* Golden test for `T` JSON write
*/
template<typename T>
void writeJsonTest(PathView testStem, const T & decoded)
{
nix::writeJsonTest(*this, testStem, decoded);
}
};

template<class Proto, const char * protocolDir>
Expand All @@ -34,7 +53,7 @@ public:
template<typename T>
void readProtoTest(PathView testStem, typename Proto::Version version, T expected)
{
CharacterizationTest::readTest(testStem, [&](const auto & encoded) {
CharacterizationTest::readTest(std::string{testStem + ".bin"}, [&](const auto & encoded) {
T got = ({
StringSource from{encoded};
Proto::template Serialise<T>::read(
Expand All @@ -55,7 +74,7 @@ public:
template<typename T>
void writeProtoTest(PathView testStem, typename Proto::Version version, const T & decoded)
{
CharacterizationTest::writeTest(testStem, [&]() {
CharacterizationTest::writeTest(std::string{testStem + ".bin"}, [&]() {
StringSink to;
Proto::template Serialise<T>::write(
this->store,
Expand All @@ -69,14 +88,25 @@ public:
}
};

#define VERSIONED_CHARACTERIZATION_TEST(FIXTURE, NAME, STEM, VERSION, VALUE) \
TEST_F(FIXTURE, NAME##_read) \
{ \
readProtoTest(STEM, VERSION, VALUE); \
} \
TEST_F(FIXTURE, NAME##_write) \
{ \
writeProtoTest(STEM, VERSION, VALUE); \
#define VERSIONED_CHARACTERIZATION_TEST_NO_JSON(FIXTURE, NAME, STEM, VERSION, VALUE) \
TEST_F(FIXTURE, NAME##_read) \
{ \
readProtoTest(STEM, VERSION, VALUE); \
} \
TEST_F(FIXTURE, NAME##_write) \
{ \
writeProtoTest(STEM, VERSION, VALUE); \
}

#define VERSIONED_CHARACTERIZATION_TEST(FIXTURE, NAME, STEM, VERSION, VALUE) \
VERSIONED_CHARACTERIZATION_TEST_NO_JSON(FIXTURE, NAME, STEM, VERSION, VALUE) \
TEST_F(FIXTURE, NAME##_json_read) \
{ \
readJsonTest(STEM, VALUE); \
} \
TEST_F(FIXTURE, NAME##_json_write) \
{ \
writeJsonTest(STEM, VALUE); \
}

} // namespace nix
13 changes: 11 additions & 2 deletions src/libstore-tests/common-protocol.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <nlohmann/json.hpp>
#include <gtest/gtest.h>

#include "nix/util/json-utils.hh"
#include "nix/store/common-protocol.hh"
#include "nix/store/common-protocol-impl.hh"
#include "nix/store/build-result.hh"
Expand All @@ -22,7 +23,7 @@ class CommonProtoTest : public ProtoTest<CommonProto, commonProtoDir>
template<typename T>
void readProtoTest(PathView testStem, const T & expected)
{
CharacterizationTest::readTest(testStem, [&](const auto & encoded) {
CharacterizationTest::readTest(std::string{testStem + ".bin"}, [&](const auto & encoded) {
T got = ({
StringSource from{encoded};
CommonProto::Serialise<T>::read(store, CommonProto::ReadConn{.from = from});
Expand All @@ -38,7 +39,7 @@ class CommonProtoTest : public ProtoTest<CommonProto, commonProtoDir>
template<typename T>
void writeProtoTest(PathView testStem, const T & decoded)
{
CharacterizationTest::writeTest(testStem, [&]() -> std::string {
CharacterizationTest::writeTest(std::string{testStem + ".bin"}, [&]() -> std::string {
StringSink to;
CommonProto::Serialise<T>::write(store, CommonProto::WriteConn{.to = to}, decoded);
return to.s;
Expand All @@ -54,6 +55,14 @@ class CommonProtoTest : public ProtoTest<CommonProto, commonProtoDir>
TEST_F(CommonProtoTest, NAME##_write) \
{ \
writeProtoTest(STEM, VALUE); \
} \
TEST_F(CommonProtoTest, NAME##_json_read) \
{ \
readJsonTest(STEM, VALUE); \
} \
TEST_F(CommonProtoTest, NAME##_json_write) \
{ \
writeJsonTest(STEM, VALUE); \
}

CHARACTERIZATION_TEST(
Expand Down
26 changes: 26 additions & 0 deletions src/libstore-tests/data/common-protocol/content-address.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[
{
"hash": {
"algorithm": "sha256",
"format": "base64",
"hash": "+Xc9Ll6mcPltwaewrk/BAQ56Y3G5T//wzhKUc0zrYu0="
},
"method": "text"
},
{
"hash": {
"algorithm": "sha1",
"format": "base64",
"hash": "gGemBoenViNZM3hiwqns/Fgzqwo="
},
"method": "flat"
},
{
"hash": {
"algorithm": "sha256",
"format": "base64",
"hash": "EMIJ+giQ/gLIWoxmPKjno3zHZrxbGymgzGGyZvZBIdM="
},
"method": "nar"
}
]
4 changes: 4 additions & 0 deletions src/libstore-tests/data/common-protocol/drv-output.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[
"sha256:15e3c560894cbb27085cf65b5a2ecb18488c999497f4531b6907a7581ce6d527!baz",
"sha256:6f869f9ea2823bda165e06076fd0de4366dead2c0e8d2dbbad277d4f15c373f5!quux"
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[
null,
{
"hash": {
"algorithm": "sha1",
"format": "base64",
"hash": "gGemBoenViNZM3hiwqns/Fgzqwo="
},
"method": "flat"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[
null,
"g1w7hy3qg1w7hy3qg1w7hy3qg1w7hy3q-foo-bar"
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[
{
"dependentRealisations": {
"sha256:6f869f9ea2823bda165e06076fd0de4366dead2c0e8d2dbbad277d4f15c373f5!quux": "g1w7hy3qg1w7hy3qg1w7hy3qg1w7hy3q-foo"
},
"id": "sha256:15e3c560894cbb27085cf65b5a2ecb18488c999497f4531b6907a7581ce6d527!baz",
"outPath": "g1w7hy3qg1w7hy3qg1w7hy3qg1w7hy3q-foo",
"signatures": [
"asdf",
"qwer"
]
}
]
17 changes: 17 additions & 0 deletions src/libstore-tests/data/common-protocol/realisation.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[
{
"dependentRealisations": {},
"id": "sha256:15e3c560894cbb27085cf65b5a2ecb18488c999497f4531b6907a7581ce6d527!baz",
"outPath": "g1w7hy3qg1w7hy3qg1w7hy3qg1w7hy3q-foo",
"signatures": []
},
{
"dependentRealisations": {},
"id": "sha256:15e3c560894cbb27085cf65b5a2ecb18488c999497f4531b6907a7581ce6d527!baz",
"outPath": "g1w7hy3qg1w7hy3qg1w7hy3qg1w7hy3q-foo",
"signatures": [
"asdf",
"qwer"
]
}
]
22 changes: 22 additions & 0 deletions src/libstore-tests/data/common-protocol/set.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[
[],
[
""
],
[
"",
"bar",
"foo"
],
[
[],
[
""
],
[
"",
"1",
"2"
]
]
]
4 changes: 4 additions & 0 deletions src/libstore-tests/data/common-protocol/store-path.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[
"g1w7hy3qg1w7hy3qg1w7hy3qg1w7hy3q-foo",
"g1w7hy3qg1w7hy3qg1w7hy3qg1w7hy3q-foo-bar"
]
7 changes: 7 additions & 0 deletions src/libstore-tests/data/common-protocol/string.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
"",
"hi",
"white rabbit",
"大白兔",
"oh no "
]
22 changes: 22 additions & 0 deletions src/libstore-tests/data/common-protocol/vector.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[
[],
[
""
],
[
"",
"foo",
"bar"
],
[
[],
[
""
],
[
"",
"1",
"2"
]
]
]
28 changes: 28 additions & 0 deletions src/libstore-tests/data/serve-protocol/build-result-2.2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
[
{
"errorMsg": "no idea why",
"isNonDeterministic": false,
"startTime": 0,
"status": "OutputRejected",
"stopTime": 0,
"success": false,
"timesBuilt": 0
},
{
"errorMsg": "no idea why",
"isNonDeterministic": false,
"startTime": 0,
"status": "NotDeterministic",
"stopTime": 0,
"success": false,
"timesBuilt": 0
},
{
"builtOutputs": {},
"startTime": 0,
"status": "Built",
"stopTime": 0,
"success": true,
"timesBuilt": 0
}
]
28 changes: 28 additions & 0 deletions src/libstore-tests/data/serve-protocol/build-result-2.3.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
[
{
"errorMsg": "no idea why",
"isNonDeterministic": false,
"startTime": 0,
"status": "OutputRejected",
"stopTime": 0,
"success": false,
"timesBuilt": 0
},
{
"errorMsg": "no idea why",
"isNonDeterministic": true,
"startTime": 30,
"status": "NotDeterministic",
"stopTime": 50,
"success": false,
"timesBuilt": 3
},
{
"builtOutputs": {},
"startTime": 30,
"status": "Built",
"stopTime": 50,
"success": true,
"timesBuilt": 0
}
]
41 changes: 41 additions & 0 deletions src/libstore-tests/data/serve-protocol/build-result-2.6.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
[
{
"errorMsg": "no idea why",
"isNonDeterministic": false,
"startTime": 0,
"status": "OutputRejected",
"stopTime": 0,
"success": false,
"timesBuilt": 0
},
{
"errorMsg": "no idea why",
"isNonDeterministic": true,
"startTime": 30,
"status": "NotDeterministic",
"stopTime": 50,
"success": false,
"timesBuilt": 3
},
{
"builtOutputs": {
"bar": {
"dependentRealisations": {},
"id": "sha256:6f869f9ea2823bda165e06076fd0de4366dead2c0e8d2dbbad277d4f15c373f5!bar",
"outPath": "g1w7hy3qg1w7hy3qg1w7hy3qg1w7hy3q-bar",
"signatures": []
},
"foo": {
"dependentRealisations": {},
"id": "sha256:6f869f9ea2823bda165e06076fd0de4366dead2c0e8d2dbbad277d4f15c373f5!foo",
"outPath": "g1w7hy3qg1w7hy3qg1w7hy3qg1w7hy3q-foo",
"signatures": []
}
},
"startTime": 30,
"status": "Built",
"stopTime": 50,
"success": true,
"timesBuilt": 1
}
]
26 changes: 26 additions & 0 deletions src/libstore-tests/data/serve-protocol/content-address.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[
{
"hash": {
"algorithm": "sha256",
"format": "base64",
"hash": "+Xc9Ll6mcPltwaewrk/BAQ56Y3G5T//wzhKUc0zrYu0="
},
"method": "text"
},
{
"hash": {
"algorithm": "sha1",
"format": "base64",
"hash": "gGemBoenViNZM3hiwqns/Fgzqwo="
},
"method": "flat"
},
{
"hash": {
"algorithm": "sha256",
"format": "base64",
"hash": "EMIJ+giQ/gLIWoxmPKjno3zHZrxbGymgzGGyZvZBIdM="
},
"method": "nar"
}
]
Loading
Loading