aboutsummaryrefslogtreecommitdiff
path: root/src/libutil/tests/canon-path.cc
blob: c1c5adadf49810d9bb9dd3818e1b626a69ae10cc (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
#include "canon-path.hh"

#include <gtest/gtest.h>

namespace nix {

    TEST(CanonPath, basic) {
        {
            CanonPath p("/");
            ASSERT_EQ(p.abs(), "/");
            ASSERT_EQ(p.rel(), "");
            ASSERT_EQ(p.baseName(), std::nullopt);
            ASSERT_EQ(p.dirOf(), std::nullopt);
            ASSERT_FALSE(p.parent());
        }

        {
            CanonPath p("/foo//");
            ASSERT_EQ(p.abs(), "/foo");
            ASSERT_EQ(p.rel(), "foo");
            ASSERT_EQ(*p.baseName(), "foo");
            ASSERT_EQ(*p.dirOf(), ""); // FIXME: do we want this?
            ASSERT_EQ(p.parent()->abs(), "/");
        }

        {
            CanonPath p("foo/bar");
            ASSERT_EQ(p.abs(), "/foo/bar");
            ASSERT_EQ(p.rel(), "foo/bar");
            ASSERT_EQ(*p.baseName(), "bar");
            ASSERT_EQ(*p.dirOf(), "/foo");
            ASSERT_EQ(p.parent()->abs(), "/foo");
        }

        {
            CanonPath p("foo//bar/");
            ASSERT_EQ(p.abs(), "/foo/bar");
            ASSERT_EQ(p.rel(), "foo/bar");
            ASSERT_EQ(*p.baseName(), "bar");
            ASSERT_EQ(*p.dirOf(), "/foo");
        }
    }

    TEST(CanonPath, pop) {
        CanonPath p("foo/bar/x");
        ASSERT_EQ(p.abs(), "/foo/bar/x");
        p.pop();
        ASSERT_EQ(p.abs(), "/foo/bar");
        p.pop();
        ASSERT_EQ(p.abs(), "/foo");
        p.pop();
        ASSERT_EQ(p.abs(), "/");
    }

    TEST(CanonPath, removePrefix) {
        CanonPath p1("foo/bar");
        CanonPath p2("foo/bar/a/b/c");
        ASSERT_EQ(p2.removePrefix(p1).abs(), "/a/b/c");
        ASSERT_EQ(p1.removePrefix(p1).abs(), "/");
        ASSERT_EQ(p1.removePrefix(CanonPath("/")).abs(), "/foo/bar");
    }

    TEST(CanonPath, iter) {
        {
            CanonPath p("a//foo/bar//");
            std::vector<std::string_view> ss;
            for (auto & c : p) ss.push_back(c);
            ASSERT_EQ(ss, std::vector<std::string_view>({"a", "foo", "bar"}));
        }

        {
            CanonPath p("/");
            std::vector<std::string_view> ss;
            for (auto & c : p) ss.push_back(c);
            ASSERT_EQ(ss, std::vector<std::string_view>());
        }
    }

    TEST(CanonPath, concat) {
        {
            CanonPath p1("a//foo/bar//");
            CanonPath p2("xyzzy/bla");
            ASSERT_EQ((p1 + p2).abs(), "/a/foo/bar/xyzzy/bla");
        }

        {
            CanonPath p1("/");
            CanonPath p2("/a/b");
            ASSERT_EQ((p1 + p2).abs(), "/a/b");
        }

        {
            CanonPath p1("/a/b");
            CanonPath p2("/");
            ASSERT_EQ((p1 + p2).abs(), "/a/b");
        }

        {
            CanonPath p("/foo/bar");
            ASSERT_EQ((p + "x").abs(), "/foo/bar/x");
        }

        {
            CanonPath p("/");
            ASSERT_EQ((p + "foo" + "bar").abs(), "/foo/bar");
        }
    }

    TEST(CanonPath, within) {
        {
            ASSERT_TRUE(CanonPath("foo").isWithin(CanonPath("foo")));
            ASSERT_FALSE(CanonPath("foo").isWithin(CanonPath("bar")));
            ASSERT_FALSE(CanonPath("foo").isWithin(CanonPath("fo")));
            ASSERT_TRUE(CanonPath("foo/bar").isWithin(CanonPath("foo")));
            ASSERT_FALSE(CanonPath("foo").isWithin(CanonPath("foo/bar")));
            ASSERT_TRUE(CanonPath("/foo/bar/default.nix").isWithin(CanonPath("/")));
            ASSERT_TRUE(CanonPath("/").isWithin(CanonPath("/")));
        }
    }

    TEST(CanonPath, sort) {
        ASSERT_FALSE(CanonPath("foo") < CanonPath("foo"));
        ASSERT_TRUE (CanonPath("foo") < CanonPath("foo/bar"));
        ASSERT_TRUE (CanonPath("foo/bar") < CanonPath("foo!"));
        ASSERT_FALSE(CanonPath("foo!") < CanonPath("foo"));
        ASSERT_TRUE (CanonPath("foo") < CanonPath("foo!"));
    }

    TEST(CanonPath, allowed) {
        {
            std::set<CanonPath> allowed {
                CanonPath("foo/bar"),
                CanonPath("foo!"),
                CanonPath("xyzzy"),
                CanonPath("a/b/c"),
            };

            ASSERT_TRUE (CanonPath("foo/bar").isAllowed(allowed));
            ASSERT_TRUE (CanonPath("foo/bar/bla").isAllowed(allowed));
            ASSERT_TRUE (CanonPath("foo").isAllowed(allowed));
            ASSERT_FALSE(CanonPath("bar").isAllowed(allowed));
            ASSERT_FALSE(CanonPath("bar/a").isAllowed(allowed));
            ASSERT_TRUE (CanonPath("a").isAllowed(allowed));
            ASSERT_TRUE (CanonPath("a/b").isAllowed(allowed));
            ASSERT_TRUE (CanonPath("a/b/c").isAllowed(allowed));
            ASSERT_TRUE (CanonPath("a/b/c/d").isAllowed(allowed));
            ASSERT_TRUE (CanonPath("a/b/c/d/e").isAllowed(allowed));
            ASSERT_FALSE(CanonPath("a/b/a").isAllowed(allowed));
            ASSERT_FALSE(CanonPath("a/b/d").isAllowed(allowed));
            ASSERT_FALSE(CanonPath("aaa").isAllowed(allowed));
            ASSERT_FALSE(CanonPath("zzz").isAllowed(allowed));
            ASSERT_TRUE (CanonPath("/").isAllowed(allowed));
        }
    }
}