Skip to content

Commit 2047492

Browse files
committed
JSON alongside binary proto serialization test data
This makes the proto serializer characterisation test data be accompanied by JSON data. This is arguably useful for a reasons: - The JSON data is human-readable while the binary data is not, so it provides some indication of what the test data means beyond the C++ literals. - The JSON data is language-agnostic, and so can be used to quickly rig up tests for implementation in other languages, without having source code literals at all (just go back and forth between the JSON and the binary). - Even though we have no concrete plans to place the binary protocol 1-1 or with JSON, it is still nice to ensure that the JSON serializers and binary protocols have (near) equal coverage over data types, to help ensure we didn't forget a JSON (de)serializer.
1 parent f5390e7 commit 2047492

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+1097
-42
lines changed

src/libstore-test-support/include/nix/store/tests/protocol.hh

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include "nix/store/tests/libstore.hh"
88
#include "nix/util/tests/characterization.hh"
9+
#include "nix/util/tests/json-characterization.hh"
910

1011
namespace nix {
1112

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

1718
std::filesystem::path goldenMaster(std::string_view testStem) const override
1819
{
19-
return unitTestData / (std::string{testStem + ".bin"});
20+
return unitTestData / testStem;
2021
}
2122

2223
public:
2324
Path storeDir = "/nix/store";
2425
StoreDirConfig store{storeDir};
26+
27+
/**
28+
* Golden test for `T` JSON reading
29+
*/
30+
template<typename T>
31+
void readJsonTest(PathView testStem, const T & expected)
32+
{
33+
nix::readJsonTest(*this, testStem, expected);
34+
}
35+
36+
/**
37+
* Golden test for `T` JSON write
38+
*/
39+
template<typename T>
40+
void writeJsonTest(PathView testStem, const T & decoded)
41+
{
42+
nix::writeJsonTest(*this, testStem, decoded);
43+
}
2544
};
2645

2746
template<class Proto, const char * protocolDir>
@@ -34,7 +53,7 @@ public:
3453
template<typename T>
3554
void readProtoTest(PathView testStem, typename Proto::Version version, T expected)
3655
{
37-
CharacterizationTest::readTest(testStem, [&](const auto & encoded) {
56+
CharacterizationTest::readTest(std::string{testStem + ".bin"}, [&](const auto & encoded) {
3857
T got = ({
3958
StringSource from{encoded};
4059
Proto::template Serialise<T>::read(
@@ -55,7 +74,7 @@ public:
5574
template<typename T>
5675
void writeProtoTest(PathView testStem, typename Proto::Version version, const T & decoded)
5776
{
58-
CharacterizationTest::writeTest(testStem, [&]() {
77+
CharacterizationTest::writeTest(std::string{testStem + ".bin"}, [&]() {
5978
StringSink to;
6079
Proto::template Serialise<T>::write(
6180
this->store,
@@ -69,14 +88,25 @@ public:
6988
}
7089
};
7190

72-
#define VERSIONED_CHARACTERIZATION_TEST(FIXTURE, NAME, STEM, VERSION, VALUE) \
73-
TEST_F(FIXTURE, NAME##_read) \
74-
{ \
75-
readProtoTest(STEM, VERSION, VALUE); \
76-
} \
77-
TEST_F(FIXTURE, NAME##_write) \
78-
{ \
79-
writeProtoTest(STEM, VERSION, VALUE); \
91+
#define VERSIONED_CHARACTERIZATION_TEST_NO_JSON(FIXTURE, NAME, STEM, VERSION, VALUE) \
92+
TEST_F(FIXTURE, NAME##_read) \
93+
{ \
94+
readProtoTest(STEM, VERSION, VALUE); \
95+
} \
96+
TEST_F(FIXTURE, NAME##_write) \
97+
{ \
98+
writeProtoTest(STEM, VERSION, VALUE); \
99+
}
100+
101+
#define VERSIONED_CHARACTERIZATION_TEST(FIXTURE, NAME, STEM, VERSION, VALUE) \
102+
VERSIONED_CHARACTERIZATION_TEST_NO_JSON(FIXTURE, NAME, STEM, VERSION, VALUE) \
103+
TEST_F(FIXTURE, NAME##_json_read) \
104+
{ \
105+
readJsonTest(STEM, VALUE); \
106+
} \
107+
TEST_F(FIXTURE, NAME##_json_write) \
108+
{ \
109+
writeJsonTest(STEM, VALUE); \
80110
}
81111

82112
} // namespace nix

src/libstore-tests/common-protocol.cc

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <nlohmann/json.hpp>
44
#include <gtest/gtest.h>
55

6+
#include "nix/util/json-utils.hh"
67
#include "nix/store/common-protocol.hh"
78
#include "nix/store/common-protocol-impl.hh"
89
#include "nix/store/build-result.hh"
@@ -22,7 +23,7 @@ class CommonProtoTest : public ProtoTest<CommonProto, commonProtoDir>
2223
template<typename T>
2324
void readProtoTest(PathView testStem, const T & expected)
2425
{
25-
CharacterizationTest::readTest(testStem, [&](const auto & encoded) {
26+
CharacterizationTest::readTest(std::string{testStem + ".bin"}, [&](const auto & encoded) {
2627
T got = ({
2728
StringSource from{encoded};
2829
CommonProto::Serialise<T>::read(store, CommonProto::ReadConn{.from = from});
@@ -38,7 +39,7 @@ class CommonProtoTest : public ProtoTest<CommonProto, commonProtoDir>
3839
template<typename T>
3940
void writeProtoTest(PathView testStem, const T & decoded)
4041
{
41-
CharacterizationTest::writeTest(testStem, [&]() -> std::string {
42+
CharacterizationTest::writeTest(std::string{testStem + ".bin"}, [&]() -> std::string {
4243
StringSink to;
4344
CommonProto::Serialise<T>::write(store, CommonProto::WriteConn{.to = to}, decoded);
4445
return to.s;
@@ -54,6 +55,14 @@ class CommonProtoTest : public ProtoTest<CommonProto, commonProtoDir>
5455
TEST_F(CommonProtoTest, NAME##_write) \
5556
{ \
5657
writeProtoTest(STEM, VALUE); \
58+
} \
59+
TEST_F(CommonProtoTest, NAME##_json_read) \
60+
{ \
61+
readJsonTest(STEM, VALUE); \
62+
} \
63+
TEST_F(CommonProtoTest, NAME##_json_write) \
64+
{ \
65+
writeJsonTest(STEM, VALUE); \
5766
}
5867

5968
CHARACTERIZATION_TEST(
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
[
2+
{
3+
"hash": {
4+
"algorithm": "sha256",
5+
"format": "base64",
6+
"hash": "+Xc9Ll6mcPltwaewrk/BAQ56Y3G5T//wzhKUc0zrYu0="
7+
},
8+
"method": "text"
9+
},
10+
{
11+
"hash": {
12+
"algorithm": "sha1",
13+
"format": "base64",
14+
"hash": "gGemBoenViNZM3hiwqns/Fgzqwo="
15+
},
16+
"method": "flat"
17+
},
18+
{
19+
"hash": {
20+
"algorithm": "sha256",
21+
"format": "base64",
22+
"hash": "EMIJ+giQ/gLIWoxmPKjno3zHZrxbGymgzGGyZvZBIdM="
23+
},
24+
"method": "nar"
25+
}
26+
]
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[
2+
"sha256:15e3c560894cbb27085cf65b5a2ecb18488c999497f4531b6907a7581ce6d527!baz",
3+
"sha256:6f869f9ea2823bda165e06076fd0de4366dead2c0e8d2dbbad277d4f15c373f5!quux"
4+
]
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[
2+
null,
3+
{
4+
"hash": {
5+
"algorithm": "sha1",
6+
"format": "base64",
7+
"hash": "gGemBoenViNZM3hiwqns/Fgzqwo="
8+
},
9+
"method": "flat"
10+
}
11+
]
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[
2+
null,
3+
"g1w7hy3qg1w7hy3qg1w7hy3qg1w7hy3q-foo-bar"
4+
]
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[
2+
{
3+
"dependentRealisations": {
4+
"sha256:6f869f9ea2823bda165e06076fd0de4366dead2c0e8d2dbbad277d4f15c373f5!quux": "g1w7hy3qg1w7hy3qg1w7hy3qg1w7hy3q-foo"
5+
},
6+
"id": "sha256:15e3c560894cbb27085cf65b5a2ecb18488c999497f4531b6907a7581ce6d527!baz",
7+
"outPath": "g1w7hy3qg1w7hy3qg1w7hy3qg1w7hy3q-foo",
8+
"signatures": [
9+
"asdf",
10+
"qwer"
11+
]
12+
}
13+
]
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[
2+
{
3+
"dependentRealisations": {},
4+
"id": "sha256:15e3c560894cbb27085cf65b5a2ecb18488c999497f4531b6907a7581ce6d527!baz",
5+
"outPath": "g1w7hy3qg1w7hy3qg1w7hy3qg1w7hy3q-foo",
6+
"signatures": []
7+
},
8+
{
9+
"dependentRealisations": {},
10+
"id": "sha256:15e3c560894cbb27085cf65b5a2ecb18488c999497f4531b6907a7581ce6d527!baz",
11+
"outPath": "g1w7hy3qg1w7hy3qg1w7hy3qg1w7hy3q-foo",
12+
"signatures": [
13+
"asdf",
14+
"qwer"
15+
]
16+
}
17+
]
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
[
2+
[],
3+
[
4+
""
5+
],
6+
[
7+
"",
8+
"bar",
9+
"foo"
10+
],
11+
[
12+
[],
13+
[
14+
""
15+
],
16+
[
17+
"",
18+
"1",
19+
"2"
20+
]
21+
]
22+
]
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[
2+
"g1w7hy3qg1w7hy3qg1w7hy3qg1w7hy3q-foo",
3+
"g1w7hy3qg1w7hy3qg1w7hy3qg1w7hy3q-foo-bar"
4+
]

0 commit comments

Comments
 (0)