aboutsummaryrefslogtreecommitdiff
path: root/tests/unit/libstore/protocol.hh
blob: 8212959f039767af67f7d0e2c3016cd13ffb5283 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <nlohmann/json.hpp>
#include <gtest/gtest.h>

#include "strings.hh"
#include "tests/libstore.hh"
#include "tests/characterization.hh"

namespace nix {

template<class Proto, const char * protocolDir>
class ProtoTest : public LibStoreTest
{
protected:
    Path unitTestData = getUnitTestData() + "/libstore/" + protocolDir;

    Path goldenMaster(std::string_view testStem) {
        return unitTestData + "/" + testStem + ".bin";
    }
};

template<class Proto, const char * protocolDir>
class VersionedProtoTest : public ProtoTest<Proto, protocolDir>
{
public:
    /**
     * Golden test for `T` reading
     */
    template<typename T>
    void readTest(PathView testStem, typename Proto::Version version, T value)
    {
        if (testAccept())
        {
            GTEST_SKIP() << cannotReadGoldenMaster;
        }
        else
        {
            auto expected = readFile(ProtoTest<Proto, protocolDir>::goldenMaster(testStem));

            T got = ({
                StringSource from { expected };
                Proto::template Serialise<T>::read(
                    *LibStoreTest::store,
                    typename Proto::ReadConn {from, version}
                );
            });

            ASSERT_EQ(got, value);
        }
    }

    /**
     * Golden test for `T` write
     */
    template<typename T>
    void writeTest(PathView testStem, typename Proto::Version version, const T & value)
    {
        auto file = ProtoTest<Proto, protocolDir>::goldenMaster(testStem);

        StringSink to;
        to << Proto::write(
            *LibStoreTest::store,
            typename Proto::WriteConn {version},
            value);

        if (testAccept())
        {
            createDirs(dirOf(file));
            writeFile(file, to.s);
            GTEST_SKIP() << updatingGoldenMaster;
        }
        else
        {
            auto expected = readFile(file);
            ASSERT_EQ(to.s, expected);
        }
    }
};

#define VERSIONED_CHARACTERIZATION_TEST(FIXTURE, NAME, STEM, VERSION, VALUE) \
    TEST_F(FIXTURE, NAME ## _read) { \
        readTest(STEM, VERSION, VALUE); \
    } \
    TEST_F(FIXTURE, NAME ## _write) { \
        writeTest(STEM, VERSION, VALUE); \
    }

}