]> git.saurik.com Git - apt.git/blame - test/libapt/tagfile_test.cc
fix tests run via sudo checking the history file
[apt.git] / test / libapt / tagfile_test.cc
CommitLineData
453b82a3
DK
1#include <config.h>
2
c8b860fb
MV
3#include <apt-pkg/fileutl.h>
4#include <apt-pkg/tagfile.h>
5
453b82a3 6#include <string>
c8b860fb
MV
7#include <stdlib.h>
8#include <string.h>
0c98ee5a 9#include <unistd.h>
8710a36a 10#include <sstream>
c8b860fb 11
f00832cc 12#include <gtest/gtest.h>
453b82a3 13
f00832cc 14#include "file-helpers.h"
c8b860fb 15
f00832cc 16TEST(TagFileTest,SingleField)
c8b860fb
MV
17{
18 FileFd fd;
f00832cc 19 createTemporaryFile("singlefield", fd, NULL, "FieldA-12345678: the value of the field");
c8b860fb
MV
20
21 pkgTagFile tfile(&fd);
22 pkgTagSection section;
f00832cc
DK
23 ASSERT_TRUE(tfile.Step(section));
24
25 // It has one field
26 EXPECT_EQ(1, section.Count());
27 // ... and it is called FieldA-12345678
28 EXPECT_TRUE(section.Exists("FieldA-12345678"));
29 // its value is correct
30 EXPECT_EQ("the value of the field", section.FindS("FieldA-12345678"));
31 // A non-existent field has an empty string as value
32 EXPECT_EQ("", section.FindS("FieldB-12345678"));
33 // ... and Exists does not lie about missing fields...
34 EXPECT_FALSE(section.Exists("FieldB-12345678"));
35 // There is only one section in this tag file
36 EXPECT_FALSE(tfile.Step(section));
8d058ea5
DK
37
38 // Now we scan an empty section to test reset
39 ASSERT_TRUE(section.Scan("\n\n", 2, true));
40 EXPECT_EQ(0, section.Count());
41 EXPECT_FALSE(section.Exists("FieldA-12345678"));
42 EXPECT_FALSE(section.Exists("FieldB-12345678"));
55153bf9
DK
43
44 createTemporaryFile("emptyfile", fd, NULL, NULL);
45 ASSERT_FALSE(tfile.Step(section));
46 EXPECT_EQ(0, section.Count());
c8b860fb 47}
8710a36a
DK
48
49TEST(TagFileTest,MultipleSections)
50{
51 FileFd fd;
52 createTemporaryFile("bigsection", fd, NULL, "Package: pkgA\n"
53 "Version: 1\n"
54 "Size: 100\n"
55 "Description: aaa\n"
56 " aaa\n"
57 "\n"
58 "Package: pkgB\n"
59 "Version: 1\n"
60 "Flag: no\n"
61 "Description: bbb\n"
62 "\n"
63 "Package: pkgC\n"
64 "Version: 2\n"
65 "Flag: yes\n"
66 "Description:\n"
67 " ccc\n"
68 );
69
70 pkgTagFile tfile(&fd);
71 pkgTagSection section;
72 EXPECT_FALSE(section.Exists("Version"));
73
74 EXPECT_TRUE(tfile.Step(section));
75 EXPECT_EQ(4, section.Count());
76 EXPECT_TRUE(section.Exists("Version"));
77 EXPECT_TRUE(section.Exists("Package"));
78 EXPECT_TRUE(section.Exists("Size"));
79 EXPECT_FALSE(section.Exists("Flag"));
80 EXPECT_TRUE(section.Exists("Description"));
81 EXPECT_EQ("pkgA", section.FindS("Package"));
82 EXPECT_EQ("1", section.FindS("Version"));
83 EXPECT_EQ(1, section.FindULL("Version"));
84 EXPECT_EQ(100, section.FindULL("Size"));
85 unsigned long Flags = 1;
86 EXPECT_TRUE(section.FindFlag("Flag", Flags, 1));
87 EXPECT_EQ(1, Flags);
88 Flags = 0;
89 EXPECT_TRUE(section.FindFlag("Flag", Flags, 1));
90 EXPECT_EQ(0, Flags);
91 EXPECT_EQ("aaa\n aaa", section.FindS("Description"));
92
93
94 EXPECT_TRUE(tfile.Step(section));
95 EXPECT_EQ(4, section.Count());
96 EXPECT_TRUE(section.Exists("Version"));
97 EXPECT_TRUE(section.Exists("Package"));
98 EXPECT_FALSE(section.Exists("Size"));
99 EXPECT_TRUE(section.Exists("Flag"));
100 EXPECT_TRUE(section.Exists("Description"));
101 EXPECT_EQ("pkgB", section.FindS("Package"));
102 EXPECT_EQ("1", section.FindS("Version"));
103 EXPECT_EQ(1, section.FindULL("Version"));
104 EXPECT_EQ(0, section.FindULL("Size"));
105 Flags = 1;
106 EXPECT_TRUE(section.FindFlag("Flag", Flags, 1));
107 EXPECT_EQ(0, Flags);
108 Flags = 0;
109 EXPECT_TRUE(section.FindFlag("Flag", Flags, 1));
110 EXPECT_EQ(0, Flags);
111 EXPECT_EQ("bbb", section.FindS("Description"));
112
113 EXPECT_TRUE(tfile.Step(section));
114 EXPECT_EQ(4, section.Count());
115 EXPECT_TRUE(section.Exists("Version"));
116 EXPECT_TRUE(section.Exists("Package"));
117 EXPECT_FALSE(section.Exists("Size"));
118 EXPECT_TRUE(section.Exists("Flag"));
119 EXPECT_TRUE(section.Exists("Description"));
120 EXPECT_EQ("pkgC", section.FindS("Package"));
121 EXPECT_EQ("2", section.FindS("Version"));
122 EXPECT_EQ(2, section.FindULL("Version"));
123 Flags = 0;
124 EXPECT_TRUE(section.FindFlag("Flag", Flags, 1));
125 EXPECT_EQ(1, Flags);
126 Flags = 1;
127 EXPECT_TRUE(section.FindFlag("Flag", Flags, 1));
128 EXPECT_EQ(1, Flags);
129 EXPECT_EQ("ccc", section.FindS("Description"));
130
131 // There is no section left in this tag file
132 EXPECT_FALSE(tfile.Step(section));
133}
134
135TEST(TagFileTest,BigSection)
136{
137 size_t const count = 500;
138 std::stringstream content;
139 for (size_t i = 0; i < count; ++i)
140 content << "Field-" << i << ": " << (2000 + i) << std::endl;
141
142 FileFd fd;
143 createTemporaryFile("bigsection", fd, NULL, content.str().c_str());
144
145 pkgTagFile tfile(&fd);
146 pkgTagSection section;
147 EXPECT_TRUE(tfile.Step(section));
148
149 EXPECT_EQ(count, section.Count());
150 for (size_t i = 0; i < count; ++i)
151 {
152 std::stringstream name;
153 name << "Field-" << i;
154 EXPECT_TRUE(section.Exists(name.str().c_str())) << name.str() << " does not exist";
155 EXPECT_EQ((2000 + i), section.FindULL(name.str().c_str()));
156 }
157
158 // There is only one section in this tag file
159 EXPECT_FALSE(tfile.Step(section));
160}
161
162TEST(TagFileTest, PickedUpFromPreviousCall)
163{
164 size_t const count = 500;
165 std::stringstream contentstream;
166 for (size_t i = 0; i < count; ++i)
167 contentstream << "Field-" << i << ": " << (2000 + i) << std::endl;
168 contentstream << std::endl << std::endl;
169 std::string content = contentstream.str();
170
171 pkgTagSection section;
172 EXPECT_FALSE(section.Scan(content.c_str(), content.size()/2));
173 EXPECT_NE(0, section.Count());
174 EXPECT_NE(count, section.Count());
175 EXPECT_TRUE(section.Scan(content.c_str(), content.size(), false));
176 EXPECT_EQ(count, section.Count());
177
178 for (size_t i = 0; i < count; ++i)
179 {
180 std::stringstream name;
181 name << "Field-" << i;
182 EXPECT_TRUE(section.Exists(name.str().c_str())) << name.str() << " does not exist";
183 EXPECT_EQ((2000 + i), section.FindULL(name.str().c_str()));
184 }
185}
186
187TEST(TagFileTest, SpacesEverywhere)
188{
189 std::string content =
190 "Package: pkgA\n"
191 "Package: pkgB\n"
192 "NoSpaces:yes\n"
c72f5c4f 193 "NoValue:\n"
8710a36a
DK
194 "TagSpaces\t :yes\n"
195 "ValueSpaces: \tyes\n"
196 "BothSpaces \t:\t yes\n"
197 "TrailingSpaces: yes\t \n"
198 "Naming Space: yes\n"
199 "Naming Spaces: yes\n"
200 "Package : pkgC \n"
201 "Multi-Colon::yes:\n"
202 "\n\n";
203
204 pkgTagSection section;
205 EXPECT_TRUE(section.Scan(content.c_str(), content.size()));
206 EXPECT_TRUE(section.Exists("Package"));
207 EXPECT_TRUE(section.Exists("NoSpaces"));
c72f5c4f 208 EXPECT_TRUE(section.Exists("NoValue"));
8710a36a
DK
209 EXPECT_TRUE(section.Exists("TagSpaces"));
210 EXPECT_TRUE(section.Exists("ValueSpaces"));
211 EXPECT_TRUE(section.Exists("BothSpaces"));
212 EXPECT_TRUE(section.Exists("TrailingSpaces"));
213 EXPECT_TRUE(section.Exists("Naming Space"));
214 EXPECT_TRUE(section.Exists("Naming Spaces"));
215 EXPECT_TRUE(section.Exists("Multi-Colon"));
216 EXPECT_EQ("pkgC", section.FindS("Package"));
217 EXPECT_EQ("yes", section.FindS("NoSpaces"));
c72f5c4f 218 EXPECT_EQ("", section.FindS("NoValue"));
8710a36a
DK
219 EXPECT_EQ("yes", section.FindS("TagSpaces"));
220 EXPECT_EQ("yes", section.FindS("ValueSpaces"));
221 EXPECT_EQ("yes", section.FindS("BothSpaces"));
222 EXPECT_EQ("yes", section.FindS("TrailingSpaces"));
223 EXPECT_EQ("yes", section.FindS("Naming Space"));
224 EXPECT_EQ("yes", section.FindS("Naming Spaces"));
225 EXPECT_EQ(":yes:", section.FindS("Multi-Colon"));
226 // overridden values are still present, but not really accessible
c72f5c4f 227 EXPECT_EQ(12, section.Count());
8710a36a 228}
55153bf9
DK
229
230TEST(TagFileTest, Comments)
231{
232 FileFd fd;
233 createTemporaryFile("commentfile", fd, NULL, "# Leading comments should be ignored.\n"
234"\n"
235"Source: foo\n"
236"#Package: foo\n"
237"Section: bar\n"
238"#Section: overriden\n"
239"Priority: optional\n"
240"Build-Depends: debhelper,\n"
241"# apt-utils, (temporarily disabled)\n"
242" apt\n"
243"\n"
244"# Comments in the middle shouldn't result in extra blank paragraphs either.\n"
245"\n"
246"# Ditto.\n"
247"\n"
248"# A comment at the top of a paragraph should be ignored.\n"
249"Package: foo\n"
250"Architecture: any\n"
251"Description: An awesome package\n"
252" # This should still appear in the result.\n"
253"# this one shouldn't\n"
254" Blah, blah, blah. # but this again.\n"
255"# A comment at the end of a paragraph should be ignored.\n"
256"\n"
257"# Trailing comments shouldn't cause extra blank paragraphs."
258 );
259
260 pkgTagFile tfile(&fd, pkgTagFile::SUPPORT_COMMENTS, 1);
261 pkgTagSection section;
262 EXPECT_TRUE(tfile.Step(section));
263 EXPECT_FALSE(section.Exists("Package"));
264 EXPECT_TRUE(section.Exists("Source"));
265 EXPECT_EQ("foo", section.FindS("Source"));
266 EXPECT_TRUE(section.Exists("Section"));
267 EXPECT_EQ("bar", section.FindS("Section"));
268 EXPECT_TRUE(section.Exists("Priority"));
269 EXPECT_EQ("optional", section.FindS("Priority"));
270 EXPECT_TRUE(section.Exists("Build-Depends"));
271 EXPECT_EQ("debhelper,\n apt", section.FindS("Build-Depends"));
272
273 EXPECT_TRUE(tfile.Step(section));
274 EXPECT_FALSE(section.Exists("Source"));
275 EXPECT_TRUE(section.Exists("Package"));
276 EXPECT_EQ("foo", section.FindS("Package"));
277 EXPECT_FALSE(section.Exists("Section"));
278 EXPECT_TRUE(section.Exists("Architecture"));
279 EXPECT_EQ("any", section.FindS("Architecture"));
280 EXPECT_FALSE(section.Exists("Build-Depends"));
281 EXPECT_TRUE(section.Exists("Description"));
282 EXPECT_EQ("An awesome package\n # This should still appear in the result.\n Blah, blah, blah. # but this again.", section.FindS("Description"));
283
284 EXPECT_FALSE(tfile.Step(section));
285}