]> git.saurik.com Git - apt.git/blob - test/libapt/parsedepends_test.cc
Merge commit 'e2073b0276226b625897ef475f225bf8f508719e' as 'triehash'
[apt.git] / test / libapt / parsedepends_test.cc
1 #include <config.h>
2
3 #include <apt-pkg/deblistparser.h>
4 #include <apt-pkg/configuration.h>
5 #include <apt-pkg/pkgcache.h>
6
7 #include <string.h>
8 #include <string>
9
10 #include <gtest/gtest.h>
11
12 static void parseDependency(bool const StripMultiArch, bool const ParseArchFlags, bool const ParseRestrictionsList)
13 {
14 std::string Package;
15 std::string Version;
16 unsigned int Op = 5;
17 unsigned int Null = 0;
18 _config->Set("APT::Architecture","amd64");
19 _config->Set("APT::Build-Profiles","stage1");
20
21 const char* Depends =
22 "debhelper:any (>= 5.0), "
23 "libdb-dev:any, "
24 "gettext:native (<= 0.12), "
25 "libcurl4-gnutls-dev:native | libcurl3-gnutls-dev (>> 7.15.5), "
26 "docbook-xml, "
27 "apt (>= 0.7.25), "
28 "not-for-me [ !amd64 ], "
29 "only-for-me [ amd64 ], "
30 "any-for-me [ any ], "
31 "not-for-darwin [ !darwin-any ], "
32 "cpu-for-me [ any-amd64 ], "
33 "os-for-me [ linux-any ], "
34 "libc-for-me [ gnu-linux-any ], "
35 "libc-not-for-me [ musl-linux-any ], "
36 "cpu-not-for-me [ any-armel ], "
37 "os-not-for-me [ kfreebsd-any ], "
38 "not-in-stage1 <!stage1>, "
39 "not-stage1-and-not-nodoc <!nodoc !stage1>, "
40 "not-stage1-or-not-nodoc <!nodoc> <!stage1>, "
41 "unknown-profile <unknown stage1>, "
42 "overlord-dev:any (= 7.15.3~) | overlord-dev:native (>> 7.15.5), "
43 ;
44
45 // Stripping MultiArch is currently the default setting to not confuse
46 // non-MultiArch capable users of the library with "strange" extensions.
47 const char* Start = Depends;
48 const char* End = Depends + strlen(Depends);
49
50 Start = debListParser::ParseDepends(Start, End, Package, Version, Op, ParseArchFlags, StripMultiArch, ParseRestrictionsList);
51 if (StripMultiArch == true)
52 EXPECT_EQ("debhelper", Package);
53 else
54 EXPECT_EQ("debhelper:any", Package);
55 EXPECT_EQ("5.0", Version);
56 EXPECT_EQ(Null | pkgCache::Dep::GreaterEq, Op);
57
58 Start = debListParser::ParseDepends(Start, End, Package, Version, Op, ParseArchFlags, StripMultiArch, ParseRestrictionsList);
59 if (StripMultiArch == true)
60 EXPECT_EQ("libdb-dev", Package);
61 else
62 EXPECT_EQ("libdb-dev:any", Package);
63 EXPECT_EQ("", Version);
64 EXPECT_EQ(Null | pkgCache::Dep::NoOp, Op);
65
66 Start = debListParser::ParseDepends(Start, End, Package, Version, Op, ParseArchFlags, StripMultiArch, ParseRestrictionsList);
67 if (StripMultiArch == true)
68 EXPECT_EQ("gettext", Package);
69 else
70 EXPECT_EQ("gettext:native", Package);
71 EXPECT_EQ("0.12", Version);
72 EXPECT_EQ(Null | pkgCache::Dep::LessEq, Op);
73
74 Start = debListParser::ParseDepends(Start, End, Package, Version, Op, ParseArchFlags, StripMultiArch, ParseRestrictionsList);
75 if (StripMultiArch == true)
76 EXPECT_EQ("libcurl4-gnutls-dev", Package);
77 else
78 EXPECT_EQ("libcurl4-gnutls-dev:native", Package);
79 EXPECT_EQ("", Version);
80 EXPECT_EQ(Null | pkgCache::Dep::Or, Op);
81
82 Start = debListParser::ParseDepends(Start, End, Package, Version, Op, ParseArchFlags, StripMultiArch, ParseRestrictionsList);
83 EXPECT_EQ("libcurl3-gnutls-dev", Package);
84 EXPECT_EQ("7.15.5", Version);
85 EXPECT_EQ(Null | pkgCache::Dep::Greater, Op);
86
87 Start = debListParser::ParseDepends(Start, End, Package, Version, Op, ParseArchFlags, StripMultiArch, ParseRestrictionsList);
88 EXPECT_EQ("docbook-xml", Package);
89 EXPECT_EQ("", Version);
90 EXPECT_EQ(Null | pkgCache::Dep::NoOp, Op);
91
92 Start = debListParser::ParseDepends(Start, End, Package, Version, Op, ParseArchFlags, StripMultiArch, ParseRestrictionsList);
93 EXPECT_EQ("apt", Package);
94 EXPECT_EQ("0.7.25", Version);
95 EXPECT_EQ(Null | pkgCache::Dep::GreaterEq, Op);
96
97 if (ParseArchFlags == true) {
98 Start = debListParser::ParseDepends(Start, End, Package, Version, Op, ParseArchFlags, StripMultiArch, ParseRestrictionsList);
99 EXPECT_EQ("", Package); // not-for-me
100 } else {
101 EXPECT_EQ(true, 0 == debListParser::ParseDepends(Start, End, Package, Version, Op, ParseArchFlags, StripMultiArch, ParseRestrictionsList));
102 Start = strstr(Start, ",");
103 Start++;
104 }
105
106 if (ParseArchFlags == true) {
107 Start = debListParser::ParseDepends(Start, End, Package, Version, Op, ParseArchFlags, StripMultiArch, ParseRestrictionsList);
108 EXPECT_EQ("only-for-me", Package);
109 EXPECT_EQ("", Version);
110 EXPECT_EQ(Null | pkgCache::Dep::NoOp, Op);
111 } else {
112 EXPECT_EQ(true, 0 == debListParser::ParseDepends(Start, End, Package, Version, Op, ParseArchFlags, StripMultiArch, ParseRestrictionsList));
113 Start = strstr(Start, ",");
114 Start++;
115 }
116
117 if (ParseArchFlags == true) {
118 Start = debListParser::ParseDepends(Start, End, Package, Version, Op, ParseArchFlags, StripMultiArch, ParseRestrictionsList);
119 EXPECT_EQ("any-for-me", Package);
120 EXPECT_EQ("", Version);
121 EXPECT_EQ(Null | pkgCache::Dep::NoOp, Op);
122 } else {
123 EXPECT_EQ(true, 0 == debListParser::ParseDepends(Start, End, Package, Version, Op, ParseArchFlags, StripMultiArch, ParseRestrictionsList));
124 Start = strstr(Start, ",");
125 Start++;
126 }
127
128 if (ParseArchFlags == true) {
129 Start = debListParser::ParseDepends(Start, End, Package, Version, Op, ParseArchFlags, StripMultiArch, ParseRestrictionsList);
130 EXPECT_EQ("not-for-darwin", Package);
131 EXPECT_EQ("", Version);
132 EXPECT_EQ(Null | pkgCache::Dep::NoOp, Op);
133 } else {
134 EXPECT_EQ(true, 0 == debListParser::ParseDepends(Start, End, Package, Version, Op, ParseArchFlags, StripMultiArch, ParseRestrictionsList));
135 Start = strstr(Start, ",");
136 Start++;
137 }
138
139 if (ParseArchFlags == true) {
140 Start = debListParser::ParseDepends(Start, End, Package, Version, Op, ParseArchFlags, StripMultiArch, ParseRestrictionsList);
141 EXPECT_EQ("cpu-for-me", Package);
142 EXPECT_EQ("", Version);
143 EXPECT_EQ(Null | pkgCache::Dep::NoOp, Op);
144 } else {
145 EXPECT_EQ(true, 0 == debListParser::ParseDepends(Start, End, Package, Version, Op, ParseArchFlags, StripMultiArch, ParseRestrictionsList));
146 Start = strstr(Start, ",");
147 Start++;
148 }
149
150 if (ParseArchFlags == true) {
151 Start = debListParser::ParseDepends(Start, End, Package, Version, Op, ParseArchFlags, StripMultiArch, ParseRestrictionsList);
152 EXPECT_EQ("os-for-me", Package);
153 EXPECT_EQ("", Version);
154 EXPECT_EQ(Null | pkgCache::Dep::NoOp, Op);
155 } else {
156 EXPECT_EQ(true, 0 == debListParser::ParseDepends(Start, End, Package, Version, Op, ParseArchFlags, StripMultiArch, ParseRestrictionsList));
157 Start = strstr(Start, ",");
158 Start++;
159 }
160
161 if (ParseArchFlags == true) {
162 Start = debListParser::ParseDepends(Start, End, Package, Version, Op, ParseArchFlags, StripMultiArch, ParseRestrictionsList);
163 EXPECT_EQ("libc-for-me", Package);
164 EXPECT_EQ("", Version);
165 EXPECT_EQ(Null | pkgCache::Dep::NoOp, Op);
166 } else {
167 EXPECT_EQ(true, 0 == debListParser::ParseDepends(Start, End, Package, Version, Op, ParseArchFlags, StripMultiArch, ParseRestrictionsList));
168 Start = strstr(Start, ",");
169 Start++;
170 }
171
172 if (ParseArchFlags == true) {
173 Start = debListParser::ParseDepends(Start, End, Package, Version, Op, ParseArchFlags, StripMultiArch, ParseRestrictionsList);
174 EXPECT_EQ("", Package); // libc-not-for-me
175 } else {
176 EXPECT_EQ(true, 0 == debListParser::ParseDepends(Start, End, Package, Version, Op, ParseArchFlags, StripMultiArch, ParseRestrictionsList));
177 Start = strstr(Start, ",");
178 Start++;
179 }
180
181 if (ParseArchFlags == true) {
182 Start = debListParser::ParseDepends(Start, End, Package, Version, Op, ParseArchFlags, StripMultiArch, ParseRestrictionsList);
183 EXPECT_EQ("", Package); // cpu-not-for-me
184 } else {
185 EXPECT_EQ(true, 0 == debListParser::ParseDepends(Start, End, Package, Version, Op, ParseArchFlags, StripMultiArch, ParseRestrictionsList));
186 Start = strstr(Start, ",");
187 Start++;
188 }
189
190 if (ParseArchFlags == true) {
191 Start = debListParser::ParseDepends(Start, End, Package, Version, Op, ParseArchFlags, StripMultiArch, ParseRestrictionsList);
192 EXPECT_EQ("", Package); // os-not-for-me
193 } else {
194 EXPECT_EQ(true, 0 == debListParser::ParseDepends(Start, End, Package, Version, Op, ParseArchFlags, StripMultiArch, ParseRestrictionsList));
195 Start = strstr(Start, ",");
196 Start++;
197 }
198
199 if (ParseRestrictionsList == true) {
200 Start = debListParser::ParseDepends(Start, End, Package, Version, Op, ParseArchFlags, StripMultiArch, ParseRestrictionsList);
201 EXPECT_EQ("", Package); // not-in-stage1
202 } else {
203 EXPECT_EQ(true, 0 == debListParser::ParseDepends(Start, End, Package, Version, Op, ParseArchFlags, StripMultiArch, ParseRestrictionsList));
204 Start = strstr(Start, ",");
205 Start++;
206 }
207
208 if (ParseRestrictionsList == true) {
209 Start = debListParser::ParseDepends(Start, End, Package, Version, Op, ParseArchFlags, StripMultiArch, ParseRestrictionsList);
210 EXPECT_EQ("", Package); // not-stage1-and-not-nodoc
211 } else {
212 EXPECT_EQ(true, 0 == debListParser::ParseDepends(Start, End, Package, Version, Op, ParseArchFlags, StripMultiArch, ParseRestrictionsList));
213 Start = strstr(Start, ",");
214 Start++;
215 }
216
217 if (ParseRestrictionsList == true) {
218 Start = debListParser::ParseDepends(Start, End, Package, Version, Op, ParseArchFlags, StripMultiArch, ParseRestrictionsList);
219 EXPECT_EQ("not-stage1-or-not-nodoc", Package);
220 } else {
221 EXPECT_EQ(true, 0 == debListParser::ParseDepends(Start, End, Package, Version, Op, ParseArchFlags, StripMultiArch, ParseRestrictionsList));
222 Start = strstr(Start, ",");
223 Start++;
224 }
225
226 if (ParseRestrictionsList == true) {
227 Start = debListParser::ParseDepends(Start, End, Package, Version, Op, ParseArchFlags, StripMultiArch, ParseRestrictionsList);
228 EXPECT_EQ("", Package); // unknown-profile
229 } else {
230 EXPECT_EQ(true, 0 == debListParser::ParseDepends(Start, End, Package, Version, Op, ParseArchFlags, StripMultiArch, ParseRestrictionsList));
231 Start = strstr(Start, ",");
232 Start++;
233 }
234
235 Start = debListParser::ParseDepends(Start, End, Package, Version, Op, ParseArchFlags, StripMultiArch, ParseRestrictionsList);
236 if (StripMultiArch == true)
237 EXPECT_EQ("overlord-dev", Package);
238 else
239 EXPECT_EQ("overlord-dev:any", Package);
240 EXPECT_EQ("7.15.3~", Version);
241 EXPECT_EQ(Null | pkgCache::Dep::Equals | pkgCache::Dep::Or, Op);
242
243 debListParser::ParseDepends(Start, End, Package, Version, Op, ParseArchFlags, StripMultiArch, ParseRestrictionsList);
244 if (StripMultiArch == true)
245 EXPECT_EQ("overlord-dev", Package);
246 else
247 EXPECT_EQ("overlord-dev:native", Package);
248 EXPECT_EQ("7.15.5", Version);
249 EXPECT_EQ(Null | pkgCache::Dep::Greater, Op);
250 }
251
252 // FIXME: This testcase is too big/complex
253 TEST(ParseDependsTest, Everything)
254 {
255 bool StripMultiArch = true;
256 bool ParseArchFlags = false;
257 bool ParseRestrictionsList = false;
258 unsigned short runner = 0;
259
260 test:
261 {
262 SCOPED_TRACE(std::string("StripMultiArch: ") + (StripMultiArch ? "true" : "false"));
263 SCOPED_TRACE(std::string("ParseArchFlags: ") + (ParseArchFlags ? "true" : "false"));
264 SCOPED_TRACE(std::string("ParseRestrictionsList: ") + (ParseRestrictionsList ? "true" : "false"));
265 parseDependency(StripMultiArch, ParseArchFlags, ParseRestrictionsList);
266 }
267 if (StripMultiArch == false) {
268 if (ParseArchFlags == false)
269 ParseRestrictionsList = !ParseRestrictionsList;
270 ParseArchFlags = !ParseArchFlags;
271 }
272 StripMultiArch = !StripMultiArch;
273
274 runner++;
275 if (runner < 8)
276 goto test; // this is the prove: tests are really evil ;)
277 }