]> git.saurik.com Git - apt.git/blob - test/libapt/configuration_test.cc
647d8a4af6377010a55f3fa2d3c632156a3be88a
[apt.git] / test / libapt / configuration_test.cc
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
13 TEST(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 }
71 TEST(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 }
84 TEST(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 }
115 TEST(ConfigurationTest,Vector)
116 {
117 Configuration Cnf;
118
119 std::vector<std::string> vec = Cnf.FindVector("Test::Vector", "");
120 EXPECT_EQ(0, vec.size());
121 vec = Cnf.FindVector("Test::Vector", "foo");
122 ASSERT_EQ(1, vec.size());
123 EXPECT_EQ("foo", vec[0]);
124 vec = Cnf.FindVector("Test::Vector", "foo,bar");
125 EXPECT_EQ(2, vec.size());
126 EXPECT_EQ("foo", vec[0]);
127 EXPECT_EQ("bar", vec[1]);
128 Cnf.Set("Test::Vector::", "baz");
129 Cnf.Set("Test::Vector::", "bob");
130 Cnf.Set("Test::Vector::", "dob");
131 vec = Cnf.FindVector("Test::Vector");
132 ASSERT_EQ(3, vec.size());
133 EXPECT_EQ("baz", vec[0]);
134 EXPECT_EQ("bob", vec[1]);
135 EXPECT_EQ("dob", vec[2]);
136 vec = Cnf.FindVector("Test::Vector", "foo,bar");
137 ASSERT_EQ(3, vec.size());
138 EXPECT_EQ("baz", vec[0]);
139 EXPECT_EQ("bob", vec[1]);
140 EXPECT_EQ("dob", vec[2]);
141 Cnf.Set("Test::Vector", "abel,bravo");
142 vec = Cnf.FindVector("Test::Vector", "foo,bar");
143 ASSERT_EQ(2, vec.size());
144 EXPECT_EQ("abel", vec[0]);
145 EXPECT_EQ("bravo", vec[1]);
146 }