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"));
45 TEST(TagFileTest
,MultipleSections
)
48 createTemporaryFile("bigsection", fd
, NULL
, "Package: pkgA\n"
66 pkgTagFile
tfile(&fd
);
67 pkgTagSection section
;
68 EXPECT_FALSE(section
.Exists("Version"));
70 EXPECT_TRUE(tfile
.Step(section
));
71 EXPECT_EQ(4, section
.Count());
72 EXPECT_TRUE(section
.Exists("Version"));
73 EXPECT_TRUE(section
.Exists("Package"));
74 EXPECT_TRUE(section
.Exists("Size"));
75 EXPECT_FALSE(section
.Exists("Flag"));
76 EXPECT_TRUE(section
.Exists("Description"));
77 EXPECT_EQ("pkgA", section
.FindS("Package"));
78 EXPECT_EQ("1", section
.FindS("Version"));
79 EXPECT_EQ(1, section
.FindULL("Version"));
80 EXPECT_EQ(100, section
.FindULL("Size"));
81 unsigned long Flags
= 1;
82 EXPECT_TRUE(section
.FindFlag("Flag", Flags
, 1));
85 EXPECT_TRUE(section
.FindFlag("Flag", Flags
, 1));
87 EXPECT_EQ("aaa\n aaa", section
.FindS("Description"));
90 EXPECT_TRUE(tfile
.Step(section
));
91 EXPECT_EQ(4, section
.Count());
92 EXPECT_TRUE(section
.Exists("Version"));
93 EXPECT_TRUE(section
.Exists("Package"));
94 EXPECT_FALSE(section
.Exists("Size"));
95 EXPECT_TRUE(section
.Exists("Flag"));
96 EXPECT_TRUE(section
.Exists("Description"));
97 EXPECT_EQ("pkgB", section
.FindS("Package"));
98 EXPECT_EQ("1", section
.FindS("Version"));
99 EXPECT_EQ(1, section
.FindULL("Version"));
100 EXPECT_EQ(0, section
.FindULL("Size"));
102 EXPECT_TRUE(section
.FindFlag("Flag", Flags
, 1));
105 EXPECT_TRUE(section
.FindFlag("Flag", Flags
, 1));
107 EXPECT_EQ("bbb", section
.FindS("Description"));
109 EXPECT_TRUE(tfile
.Step(section
));
110 EXPECT_EQ(4, section
.Count());
111 EXPECT_TRUE(section
.Exists("Version"));
112 EXPECT_TRUE(section
.Exists("Package"));
113 EXPECT_FALSE(section
.Exists("Size"));
114 EXPECT_TRUE(section
.Exists("Flag"));
115 EXPECT_TRUE(section
.Exists("Description"));
116 EXPECT_EQ("pkgC", section
.FindS("Package"));
117 EXPECT_EQ("2", section
.FindS("Version"));
118 EXPECT_EQ(2, section
.FindULL("Version"));
120 EXPECT_TRUE(section
.FindFlag("Flag", Flags
, 1));
123 EXPECT_TRUE(section
.FindFlag("Flag", Flags
, 1));
125 EXPECT_EQ("ccc", section
.FindS("Description"));
127 // There is no section left in this tag file
128 EXPECT_FALSE(tfile
.Step(section
));
131 TEST(TagFileTest
,BigSection
)
133 size_t const count
= 500;
134 std::stringstream content
;
135 for (size_t i
= 0; i
< count
; ++i
)
136 content
<< "Field-" << i
<< ": " << (2000 + i
) << std::endl
;
139 createTemporaryFile("bigsection", fd
, NULL
, content
.str().c_str());
141 pkgTagFile
tfile(&fd
);
142 pkgTagSection section
;
143 EXPECT_TRUE(tfile
.Step(section
));
145 EXPECT_EQ(count
, section
.Count());
146 for (size_t i
= 0; i
< count
; ++i
)
148 std::stringstream name
;
149 name
<< "Field-" << i
;
150 EXPECT_TRUE(section
.Exists(name
.str().c_str())) << name
.str() << " does not exist";
151 EXPECT_EQ((2000 + i
), section
.FindULL(name
.str().c_str()));
154 // There is only one section in this tag file
155 EXPECT_FALSE(tfile
.Step(section
));
158 TEST(TagFileTest
, PickedUpFromPreviousCall
)
160 size_t const count
= 500;
161 std::stringstream contentstream
;
162 for (size_t i
= 0; i
< count
; ++i
)
163 contentstream
<< "Field-" << i
<< ": " << (2000 + i
) << std::endl
;
164 contentstream
<< std::endl
<< std::endl
;
165 std::string content
= contentstream
.str();
167 pkgTagSection section
;
168 EXPECT_FALSE(section
.Scan(content
.c_str(), content
.size()/2));
169 EXPECT_NE(0, section
.Count());
170 EXPECT_NE(count
, section
.Count());
171 EXPECT_TRUE(section
.Scan(content
.c_str(), content
.size(), false));
172 EXPECT_EQ(count
, section
.Count());
174 for (size_t i
= 0; i
< count
; ++i
)
176 std::stringstream name
;
177 name
<< "Field-" << i
;
178 EXPECT_TRUE(section
.Exists(name
.str().c_str())) << name
.str() << " does not exist";
179 EXPECT_EQ((2000 + i
), section
.FindULL(name
.str().c_str()));
183 TEST(TagFileTest
, SpacesEverywhere
)
185 std::string content
=
191 "ValueSpaces: \tyes\n"
192 "BothSpaces \t:\t yes\n"
193 "TrailingSpaces: yes\t \n"
194 "Naming Space: yes\n"
195 "Naming Spaces: yes\n"
197 "Multi-Colon::yes:\n"
200 pkgTagSection section
;
201 EXPECT_TRUE(section
.Scan(content
.c_str(), content
.size()));
202 EXPECT_TRUE(section
.Exists("Package"));
203 EXPECT_TRUE(section
.Exists("NoSpaces"));
204 EXPECT_TRUE(section
.Exists("NoValue"));
205 EXPECT_TRUE(section
.Exists("TagSpaces"));
206 EXPECT_TRUE(section
.Exists("ValueSpaces"));
207 EXPECT_TRUE(section
.Exists("BothSpaces"));
208 EXPECT_TRUE(section
.Exists("TrailingSpaces"));
209 EXPECT_TRUE(section
.Exists("Naming Space"));
210 EXPECT_TRUE(section
.Exists("Naming Spaces"));
211 EXPECT_TRUE(section
.Exists("Multi-Colon"));
212 EXPECT_EQ("pkgC", section
.FindS("Package"));
213 EXPECT_EQ("yes", section
.FindS("NoSpaces"));
214 EXPECT_EQ("", section
.FindS("NoValue"));
215 EXPECT_EQ("yes", section
.FindS("TagSpaces"));
216 EXPECT_EQ("yes", section
.FindS("ValueSpaces"));
217 EXPECT_EQ("yes", section
.FindS("BothSpaces"));
218 EXPECT_EQ("yes", section
.FindS("TrailingSpaces"));
219 EXPECT_EQ("yes", section
.FindS("Naming Space"));
220 EXPECT_EQ("yes", section
.FindS("Naming Spaces"));
221 EXPECT_EQ(":yes:", section
.FindS("Multi-Colon"));
222 // overridden values are still present, but not really accessible
223 EXPECT_EQ(12, section
.Count());