aboutsummaryrefslogtreecommitdiff
path: root/tests/unit/libstore/path.cc
blob: 213b6e95f5257de0f3e1cb9f17ec9311c96e175e (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include <regex>

#include <nlohmann/json.hpp>
#include <gtest/gtest.h>
#include <rapidcheck/gtest.h>

#include "path-regex.hh"
#include "store-api.hh"

#include "tests/hash.hh"
#include "tests/libstore.hh"
#include "tests/path.hh"

namespace nix {

#define STORE_DIR "/nix/store/"
#define HASH_PART "g1w7hy3qg1w7hy3qg1w7hy3qg1w7hy3q"

class StorePathTest : public LibStoreTest
{
};

static std::regex nameRegex { std::string { nameRegexStr } };

#define TEST_DONT_PARSE(NAME, STR)                           \
    TEST_F(StorePathTest, bad_ ## NAME) {                    \
        std::string_view str =                               \
            STORE_DIR HASH_PART "-" STR;                     \
        ASSERT_THROW(                                        \
            store->parseStorePath(str),                      \
            BadStorePath);                                   \
        std::string name { STR };                            \
        EXPECT_FALSE(std::regex_match(name, nameRegex));     \
    }

TEST_DONT_PARSE(empty, "")
TEST_DONT_PARSE(garbage, "&*()")
TEST_DONT_PARSE(double_star, "**")
TEST_DONT_PARSE(star_first, "*,foo")
TEST_DONT_PARSE(star_second, "foo,*")
TEST_DONT_PARSE(bang, "foo!o")
TEST_DONT_PARSE(dot, ".")
TEST_DONT_PARSE(dot_dot, "..")
TEST_DONT_PARSE(dot_dot_dash, "..-1")
TEST_DONT_PARSE(dot_dash, ".-1")
TEST_DONT_PARSE(dot_dot_dash_a, "..-a")
TEST_DONT_PARSE(dot_dash_a, ".-a")

#undef TEST_DONT_PARSE

#define TEST_DO_PARSE(NAME, STR)                             \
    TEST_F(StorePathTest, good_ ## NAME) {                   \
        std::string_view str =                               \
            STORE_DIR HASH_PART "-" STR;                     \
        auto p = store->parseStorePath(str);                 \
        std::string name { p.name() };                       \
        EXPECT_TRUE(std::regex_match(name, nameRegex));      \
    }

// 0-9 a-z A-Z + - . _ ? =

TEST_DO_PARSE(numbers, "02345")
TEST_DO_PARSE(lower_case, "foo")
TEST_DO_PARSE(upper_case, "FOO")
TEST_DO_PARSE(plus, "foo+bar")
TEST_DO_PARSE(dash, "foo-dev")
TEST_DO_PARSE(underscore, "foo_bar")
TEST_DO_PARSE(period, "foo.txt")
TEST_DO_PARSE(question_mark, "foo?why")
TEST_DO_PARSE(equals_sign, "foo=foo")
TEST_DO_PARSE(dotfile, ".gitignore")
TEST_DO_PARSE(triple_dot_a, "...a")
TEST_DO_PARSE(triple_dot_1, "...1")
TEST_DO_PARSE(triple_dot_dash, "...-")
TEST_DO_PARSE(triple_dot, "...")

#undef TEST_DO_PARSE

#ifndef COVERAGE

RC_GTEST_FIXTURE_PROP(
    StorePathTest,
    prop_regex_accept,
    (const StorePath & p))
{
    RC_ASSERT(std::regex_match(std::string { p.name() }, nameRegex));
}

RC_GTEST_FIXTURE_PROP(
    StorePathTest,
    prop_round_rip,
    (const StorePath & p))
{
    RC_ASSERT(p == store->parseStorePath(store->printStorePath(p)));
}


RC_GTEST_FIXTURE_PROP(
    StorePathTest,
    prop_check_regex_eq_parse,
    ())
{
    static auto nameFuzzer =
        rc::gen::container<std::string>(
            rc::gen::oneOf(
                // alphanum, repeated to weigh heavier
                rc::gen::oneOf(
                    rc::gen::inRange('0', '9'),
                    rc::gen::inRange('a', 'z'),
                    rc::gen::inRange('A', 'Z')
                ),
                // valid symbols
                rc::gen::oneOf(
                    rc::gen::just('+'),
                    rc::gen::just('-'),
                    rc::gen::just('.'),
                    rc::gen::just('_'),
                    rc::gen::just('?'),
                    rc::gen::just('=')
                ),
                // symbols for scary .- and ..- cases, repeated for weight
                rc::gen::just('.'), rc::gen::just('.'),
                rc::gen::just('.'), rc::gen::just('.'),
                rc::gen::just('-'), rc::gen::just('-'),
                // ascii symbol ranges
                rc::gen::oneOf(
                    rc::gen::inRange(' ', '/'),
                    rc::gen::inRange(':', '@'),
                    rc::gen::inRange('[', '`'),
                    rc::gen::inRange('{', '~')
                ),
                // typical whitespace
                rc::gen::oneOf(
                    rc::gen::just(' '),
                    rc::gen::just('\t'),
                    rc::gen::just('\n'),
                    rc::gen::just('\r')
                ),
                // some chance of control codes, non-ascii or other garbage we missed
                rc::gen::inRange('\0', '\xff')
        ));

    auto name = *nameFuzzer;

    std::string path = store->storeDir + "/575s52sh487i0ylmbs9pvi606ljdszr0-" + name;
    bool parsed = false;
    try {
        store->parseStorePath(path);
        parsed = true;
    } catch (const BadStorePath &) {
    }
    RC_ASSERT(parsed == std::regex_match(std::string { name }, nameRegex));
}

#endif

}