3 #include <apt-pkg/fileutl.h>
4 #include <apt-pkg/tagfile.h>
12 #include <gtest/gtest.h>
14 #include "file-helpers.h"
16 TEST(TagFileTest
,SingleField
)
19 createTemporaryFile("singlefield", fd
, NULL
, "FieldA-12345678: the value of the field");
21 pkgTagFile
tfile(&fd
);
22 pkgTagSection section
;
23 ASSERT_TRUE(tfile
.Step(section
));
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
));
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"));
44 createTemporaryFile("emptyfile", fd
, NULL
, NULL
);
45 ASSERT_FALSE(tfile
.Step(section
));
46 EXPECT_EQ(0, section
.Count());
49 TEST(TagFileTest
,MultipleSections
)
52 createTemporaryFile("bigsection", fd
, NULL
, "Package: pkgA\n"
70 pkgTagFile
tfile(&fd
);
71 pkgTagSection section
;
72 EXPECT_FALSE(section
.Exists("Version"));
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));
89 EXPECT_TRUE(section
.FindFlag("Flag", Flags
, 1));
91 EXPECT_EQ("aaa\n aaa", section
.FindS("Description"));
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"));
106 EXPECT_TRUE(section
.FindFlag("Flag", Flags
, 1));
109 EXPECT_TRUE(section
.FindFlag("Flag", Flags
, 1));
111 EXPECT_EQ("bbb", section
.FindS("Description"));
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"));
124 EXPECT_TRUE(section
.FindFlag("Flag", Flags
, 1));
127 EXPECT_TRUE(section
.FindFlag("Flag", Flags
, 1));
129 EXPECT_EQ("ccc", section
.FindS("Description"));
131 // There is no section left in this tag file
132 EXPECT_FALSE(tfile
.Step(section
));
135 TEST(TagFileTest
,BigSection
)
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
;
143 createTemporaryFile("bigsection", fd
, NULL
, content
.str().c_str());
145 pkgTagFile
tfile(&fd
);
146 pkgTagSection section
;
147 EXPECT_TRUE(tfile
.Step(section
));
149 EXPECT_EQ(count
, section
.Count());
150 for (size_t i
= 0; i
< count
; ++i
)
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()));
158 // There is only one section in this tag file
159 EXPECT_FALSE(tfile
.Step(section
));
162 TEST(TagFileTest
, PickedUpFromPreviousCall
)
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();
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());
178 for (size_t i
= 0; i
< count
; ++i
)
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()));
187 TEST(TagFileTest
, SpacesEverywhere
)
189 std::string content
=
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"
201 "Multi-Colon::yes:\n"
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"));
208 EXPECT_TRUE(section
.Exists("NoValue"));
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"));
218 EXPECT_EQ("", section
.FindS("NoValue"));
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
227 EXPECT_EQ(12, section
.Count());
230 TEST(TagFileTest
, Comments
)
233 createTemporaryFile("commentfile", fd
, NULL
, "# Leading comments should be ignored.\n"
238 "#Section: overriden\n"
239 "Priority: optional\n"
240 "Build-Depends: debhelper,\n"
241 "# apt-utils, (temporarily disabled)\n"
244 "# Comments in the middle shouldn't result in extra blank paragraphs either.\n"
248 "# A comment at the top of a paragraph should be ignored.\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"
257 "# Trailing comments shouldn't cause extra blank paragraphs."
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"));
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"));
284 EXPECT_FALSE(tfile
.Step(section
));