Nix 2.36.0
Nix, the purely functional package manager; unstable internal interfaces
Loading...
Searching...
No Matches
protocol.hh
Go to the documentation of this file.
1#pragma once
3
4#include <nlohmann/json.hpp>
5#include <gtest/gtest.h>
6
10
11namespace nix {
12
13template<class Proto, const char * protocolDir>
15{
16 std::filesystem::path unitTestData = getUnitTestData() / protocolDir;
17
18 std::filesystem::path goldenMaster(std::string_view testStem) const override
19 {
20 return unitTestData / testStem;
21 }
22
23public:
24 std::string storeDir = "/nix/store";
25 StoreDirConfig store{storeDir};
26
30 template<typename T>
31 void readJsonTest(std::string_view testStem, const T & expected)
32 {
33 nix::readJsonTest(*this, testStem, expected);
34 }
35
39 template<typename T>
40 void writeJsonTest(std::string_view testStem, const T & decoded)
41 {
42 nix::writeJsonTest(*this, testStem, decoded);
43 }
44};
45
46template<class Proto, const char * protocolDir>
47class VersionedProtoTest : public ProtoTest<Proto, protocolDir>
48{
49public:
53 template<typename T>
54 void readProtoTest(std::string_view testStem, typename Proto::Version version, T expected)
55 {
56 CharacterizationTest::readTest(std::string{testStem + ".bin"}, [&](const auto & encoded) {
57 T got = ({
58 StringSource from{encoded};
59 Proto::template Serialise<T>::read(
60 this->store,
61 typename Proto::ReadConn{
62 .from = from,
63 .version = version,
64 });
65 });
66
67 ASSERT_EQ(got, expected);
68 });
69 }
70
74 template<typename T>
75 void writeProtoTest(std::string_view testStem, typename Proto::Version version, const T & decoded)
76 {
77 CharacterizationTest::writeTest(std::string{testStem + ".bin"}, [&]() {
78 StringSink to;
79 Proto::template Serialise<T>::write(
80 this->store,
81 typename Proto::WriteConn{
82 .to = to,
83 .version = version,
84 },
85 decoded);
86 return std::move(to.s);
87 });
88 }
89};
90
91#define VERSIONED_READ_CHARACTERIZATION_TEST_NO_JSON(FIXTURE, NAME, STEM, VERSION, VALUE) \
92 TEST_F(FIXTURE, NAME##_read) \
93 { \
94 readProtoTest(STEM, VERSION, VALUE); \
95 }
96
97#define VERSIONED_WRITE_CHARACTERIZATION_TEST_NO_JSON(FIXTURE, NAME, STEM, VERSION, VALUE) \
98 TEST_F(FIXTURE, NAME##_write) \
99 { \
100 writeProtoTest(STEM, VERSION, VALUE); \
101 }
102
103#define VERSIONED_CHARACTERIZATION_TEST_NO_JSON(FIXTURE, NAME, STEM, VERSION, VALUE) \
104 VERSIONED_READ_CHARACTERIZATION_TEST_NO_JSON(FIXTURE, NAME, STEM, (VERSION), VALUE) \
105 VERSIONED_WRITE_CHARACTERIZATION_TEST_NO_JSON(FIXTURE, NAME, STEM, (VERSION), VALUE)
106
107#define VERSIONED_READ_CHARACTERIZATION_TEST(FIXTURE, NAME, STEM, VERSION, VALUE) \
108 VERSIONED_READ_CHARACTERIZATION_TEST_NO_JSON(FIXTURE, NAME, STEM, (VERSION), VALUE) \
109 TEST_F(FIXTURE, NAME##_json_read) \
110 { \
111 readJsonTest(STEM, VALUE); \
112 }
113
114#define VERSIONED_WRITE_CHARACTERIZATION_TEST(FIXTURE, NAME, STEM, VERSION, VALUE) \
115 VERSIONED_WRITE_CHARACTERIZATION_TEST_NO_JSON(FIXTURE, NAME, STEM, (VERSION), VALUE) \
116 TEST_F(FIXTURE, NAME##_json_write) \
117 { \
118 writeJsonTest(STEM, VALUE); \
119 }
120
121#define VERSIONED_CHARACTERIZATION_TEST(FIXTURE, NAME, STEM, VERSION, VALUE) \
122 VERSIONED_READ_CHARACTERIZATION_TEST(FIXTURE, NAME, STEM, (VERSION), VALUE) \
123 VERSIONED_WRITE_CHARACTERIZATION_TEST(FIXTURE, NAME, STEM, (VERSION), VALUE)
124
126struct NullBufferedSink : BufferedSink
127{
128 void writeUnbuffered(std::string_view data) override {}
129};
130
131} // namespace nix
Definition protocol.hh:15
void writeJsonTest(std::string_view testStem, const T &decoded)
Definition protocol.hh:40
void readJsonTest(std::string_view testStem, const T &expected)
Definition protocol.hh:31
Definition serialise.hh:293
Definition serialise.hh:315
Definition protocol.hh:48
void readProtoTest(std::string_view testStem, typename Proto::Version version, T expected)
Definition protocol.hh:54
void writeProtoTest(std::string_view testStem, typename Proto::Version version, const T &decoded)
Definition protocol.hh:75
void readJsonTest(CharacterizationTest &test, std::string_view testStem, const T &expected, auto... args)
Definition json-characterization.hh:20
void writeJsonTest(CharacterizationTest &test, std::string_view testStem, const T &value)
Definition json-characterization.hh:34
Definition characterization.hh:26
void readTest(std::string_view testStem, auto &&test)
Definition characterization.hh:39
void writeTest(std::string_view testStem, auto &&test, auto &&readFile2, auto &&writeFile2)
Definition characterization.hh:56
Has to be a BufferedSink for handshake.
Definition protocol.hh:127
Definition store-dir-config.hh:29