]> git.saurik.com Git - apt.git/blame_incremental - test/libapt/configuration_test.cc
Make directory paths configurable
[apt.git] / test / libapt / configuration_test.cc
... / ...
CommitLineData
1#include <config.h>
2
3#include <apt-pkg/configuration.h>
4
5#include <string>
6#include <vector>
7
8#include <gtest/gtest.h>
9
10//FIXME: Test for configuration file parsing;
11// currently only integration/ tests test them implicitly
12
13TEST(ConfigurationTest,Lists)
14{
15 Configuration Cnf;
16
17 Cnf.Set("APT::Keep-Fds::",28);
18 Cnf.Set("APT::Keep-Fds::",17);
19 Cnf.Set("APT::Keep-Fds::2",47);
20 Cnf.Set("APT::Keep-Fds::","broken");
21 std::vector<std::string> fds = Cnf.FindVector("APT::Keep-Fds");
22 ASSERT_EQ(4, fds.size());
23 EXPECT_EQ("28", fds[0]);
24 EXPECT_EQ("17", fds[1]);
25 EXPECT_EQ("47", fds[2]);
26 EXPECT_EQ("broken", fds[3]);
27
28 EXPECT_TRUE(Cnf.Exists("APT::Keep-Fds::2"));
29 EXPECT_EQ("47", Cnf.Find("APT::Keep-Fds::2"));
30 EXPECT_EQ(47, Cnf.FindI("APT::Keep-Fds::2"));
31 EXPECT_FALSE(Cnf.Exists("APT::Keep-Fds::3"));
32 EXPECT_EQ("", Cnf.Find("APT::Keep-Fds::3"));
33 EXPECT_EQ(56, Cnf.FindI("APT::Keep-Fds::3", 56));
34 EXPECT_EQ("not-set", Cnf.Find("APT::Keep-Fds::3", "not-set"));
35
36 Cnf.Clear("APT::Keep-Fds::2");
37 EXPECT_TRUE(Cnf.Exists("APT::Keep-Fds::2"));
38 fds = Cnf.FindVector("APT::Keep-Fds");
39 ASSERT_EQ(4, fds.size());
40 EXPECT_EQ("28", fds[0]);
41 EXPECT_EQ("17", fds[1]);
42 EXPECT_EQ("", fds[2]);
43 EXPECT_EQ("broken", fds[3]);
44
45 Cnf.Clear("APT::Keep-Fds",28);
46 fds = Cnf.FindVector("APT::Keep-Fds");
47 ASSERT_EQ(3, fds.size());
48 EXPECT_EQ("17", fds[0]);
49 EXPECT_EQ("", fds[1]);
50 EXPECT_EQ("broken", fds[2]);
51
52 Cnf.Clear("APT::Keep-Fds","");
53 EXPECT_FALSE(Cnf.Exists("APT::Keep-Fds::2"));
54
55 Cnf.Clear("APT::Keep-Fds",17);
56 Cnf.Clear("APT::Keep-Fds","broken");
57 fds = Cnf.FindVector("APT::Keep-Fds");
58 EXPECT_TRUE(fds.empty());
59
60 Cnf.Set("APT::Keep-Fds::",21);
61 Cnf.Set("APT::Keep-Fds::",42);
62 fds = Cnf.FindVector("APT::Keep-Fds");
63 ASSERT_EQ(2, fds.size());
64 EXPECT_EQ("21", fds[0]);
65 EXPECT_EQ("42", fds[1]);
66
67 Cnf.Clear("APT::Keep-Fds");
68 fds = Cnf.FindVector("APT::Keep-Fds");
69 EXPECT_TRUE(fds.empty());
70}
71TEST(ConfigurationTest,Integers)
72{
73 Configuration Cnf;
74
75 Cnf.CndSet("APT::Version", 42);
76 Cnf.CndSet("APT::Version", "66");
77 EXPECT_EQ("42", Cnf.Find("APT::Version"));
78 EXPECT_EQ(42, Cnf.FindI("APT::Version"));
79 EXPECT_EQ("42", Cnf.Find("APT::Version", "33"));
80 EXPECT_EQ(42, Cnf.FindI("APT::Version", 33));
81 EXPECT_EQ("33", Cnf.Find("APT2::Version", "33"));
82 EXPECT_EQ(33, Cnf.FindI("APT2::Version", 33));
83}
84TEST(ConfigurationTest,DirsAndFiles)
85{
86 Configuration Cnf;
87
88 EXPECT_EQ("", Cnf.FindFile("Dir::State"));
89 EXPECT_EQ("", Cnf.FindFile("Dir::Aptitude::State"));
90 Cnf.Set("Dir", "/srv/sid");
91 EXPECT_EQ("", Cnf.FindFile("Dir::State"));
92 Cnf.Set("Dir::State", "var/lib/apt");
93 Cnf.Set("Dir::Aptitude::State", "var/lib/aptitude");
94 EXPECT_EQ("/srv/sid/var/lib/apt", Cnf.FindFile("Dir::State"));
95 EXPECT_EQ("/srv/sid/var/lib/aptitude", Cnf.FindFile("Dir::Aptitude::State"));
96
97 Cnf.Set("RootDir", "/");
98 EXPECT_EQ("/srv/sid/var/lib/apt", Cnf.FindFile("Dir::State"));
99 EXPECT_EQ("/srv/sid/var/lib/aptitude", Cnf.FindFile("Dir::Aptitude::State"));
100 Cnf.Set("RootDir", "//./////.////");
101 EXPECT_EQ("/srv/sid/var/lib/apt", Cnf.FindFile("Dir::State"));
102 EXPECT_EQ("/srv/sid/var/lib/aptitude", Cnf.FindFile("Dir::Aptitude::State"));
103 Cnf.Set("RootDir", "/rootdir");
104 EXPECT_EQ("/rootdir/srv/sid/var/lib/apt", Cnf.FindFile("Dir::State"));
105 EXPECT_EQ("/rootdir/srv/sid/var/lib/aptitude", Cnf.FindFile("Dir::Aptitude::State"));
106 Cnf.Set("RootDir", "/rootdir/");
107 EXPECT_EQ("/rootdir/srv/sid/var/lib/apt", Cnf.FindFile("Dir::State"));
108 EXPECT_EQ("/rootdir/srv/sid/var/lib/aptitude", Cnf.FindFile("Dir::Aptitude::State"));
109
110 Cnf.Set("Dir::State", "/dev/null");
111 Cnf.Set("Dir::State::lists", "lists/");
112 EXPECT_EQ("/rootdir/dev/null", Cnf.FindDir("Dir::State"));
113 EXPECT_EQ("/rootdir/dev/null", Cnf.FindDir("Dir::State::lists"));
114}
115TEST(ConfigurationTest,DevNullInPaths)
116{
117 Configuration Cnf;
118 EXPECT_EQ("", Cnf.FindFile("Dir"));
119 EXPECT_EQ("", Cnf.FindFile("Dir::State"));
120 EXPECT_EQ("", Cnf.FindFile("Dir::State::status"));
121 Cnf.Set("Dir::State", "/dev/null");
122 EXPECT_EQ("/dev/null", Cnf.FindFile("Dir::State"));
123 Cnf.Set("Dir", "/");
124 Cnf.Set("Dir::State::status", "status");
125 EXPECT_EQ("/dev/null", Cnf.FindFile("Dir::State"));
126 EXPECT_EQ("/dev/null", Cnf.FindFile("Dir::State::status"));
127 Cnf.Set("Dir::State", "");
128 EXPECT_EQ("", Cnf.FindFile("Dir::State"));
129 EXPECT_EQ("/status", Cnf.FindFile("Dir::State::status"));
130 Cnf.Set("Dir", "/dev/null");
131 EXPECT_EQ("/dev/null", Cnf.FindFile("Dir"));
132 EXPECT_EQ("", Cnf.FindFile("Dir::State"));
133 EXPECT_EQ("/dev/null", Cnf.FindFile("Dir::State::status"));
134 Cnf.Set("Dir", "/rootdir");
135 EXPECT_EQ("/rootdir", Cnf.FindFile("Dir"));
136 EXPECT_EQ("", Cnf.FindFile("Dir::State"));
137 EXPECT_EQ("/rootdir/status", Cnf.FindFile("Dir::State::status"));
138 Cnf.Set("Dir::State::status", "/foo/status");
139 EXPECT_EQ("/rootdir", Cnf.FindFile("Dir"));
140 EXPECT_EQ("", Cnf.FindFile("Dir::State"));
141 EXPECT_EQ("/foo/status", Cnf.FindFile("Dir::State::status"));
142}
143TEST(ConfigurationTest,Vector)
144{
145 Configuration Cnf;
146
147 std::vector<std::string> vec = Cnf.FindVector("Test::Vector", "");
148 EXPECT_EQ(0, vec.size());
149 vec = Cnf.FindVector("Test::Vector", "foo");
150 ASSERT_EQ(1, vec.size());
151 EXPECT_EQ("foo", vec[0]);
152 vec = Cnf.FindVector("Test::Vector", "foo,bar");
153 EXPECT_EQ(2, vec.size());
154 EXPECT_EQ("foo", vec[0]);
155 EXPECT_EQ("bar", vec[1]);
156 Cnf.Set("Test::Vector::", "baz");
157 Cnf.Set("Test::Vector::", "bob");
158 Cnf.Set("Test::Vector::", "dob");
159 vec = Cnf.FindVector("Test::Vector");
160 ASSERT_EQ(3, vec.size());
161 EXPECT_EQ("baz", vec[0]);
162 EXPECT_EQ("bob", vec[1]);
163 EXPECT_EQ("dob", vec[2]);
164 vec = Cnf.FindVector("Test::Vector", "foo,bar");
165 ASSERT_EQ(3, vec.size());
166 EXPECT_EQ("baz", vec[0]);
167 EXPECT_EQ("bob", vec[1]);
168 EXPECT_EQ("dob", vec[2]);
169 Cnf.Set("Test::Vector", "abel,bravo");
170 vec = Cnf.FindVector("Test::Vector", "foo,bar");
171 ASSERT_EQ(2, vec.size());
172 EXPECT_EQ("abel", vec[0]);
173 EXPECT_EQ("bravo", vec[1]);
174}
175TEST(ConfigurationTest,Merge)
176{
177 Configuration Cnf;
178 Cnf.Set("Binary::apt::option::foo", "bar");
179 Cnf.Set("Binary::apt::option::empty", "");
180 Cnf.Set("option::foo", "foo");
181
182 Cnf.MoveSubTree("Binary::apt", "Binary::apt2");
183 EXPECT_FALSE(Cnf.Exists("Binary::apt::option"));
184 EXPECT_TRUE(Cnf.Exists("option"));
185 EXPECT_EQ("foo", Cnf.Find("option::foo"));
186 EXPECT_EQ("bar", Cnf.Find("Binary::apt2::option::foo"));
187
188 EXPECT_FALSE(Cnf.Exists("option::empty"));
189 EXPECT_TRUE(Cnf.Exists("Binary::apt2::option::empty"));
190 Cnf.Set("option::empty", "not");
191
192 Cnf.MoveSubTree("Binary::apt2", NULL);
193 EXPECT_FALSE(Cnf.Exists("Binary::apt2::option"));
194 EXPECT_TRUE(Cnf.Exists("option"));
195 EXPECT_EQ("bar", Cnf.Find("option::foo"));
196 EXPECT_EQ("", Cnf.Find("option::empty"));
197}