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
));
39 TEST(TagFileTest
,MultipleSections
)
42 createTemporaryFile("bigsection", fd
, NULL
, "Package: pkgA\n"
60 pkgTagFile
tfile(&fd
);
61 pkgTagSection section
;
62 EXPECT_FALSE(section
.Exists("Version"));
64 EXPECT_TRUE(tfile
.Step(section
));
65 EXPECT_EQ(4, section
.Count());
66 EXPECT_TRUE(section
.Exists("Version"));
67 EXPECT_TRUE(section
.Exists("Package"));
68 EXPECT_TRUE(section
.Exists("Size"));
69 EXPECT_FALSE(section
.Exists("Flag"));
70 EXPECT_TRUE(section
.Exists("Description"));
71 EXPECT_EQ("pkgA", section
.FindS("Package"));
72 EXPECT_EQ("1", section
.FindS("Version"));
73 EXPECT_EQ(1, section
.FindULL("Version"));
74 EXPECT_EQ(100, section
.FindULL("Size"));
75 unsigned long Flags
= 1;
76 EXPECT_TRUE(section
.FindFlag("Flag", Flags
, 1));
79 EXPECT_TRUE(section
.FindFlag("Flag", Flags
, 1));
81 EXPECT_EQ("aaa\n aaa", section
.FindS("Description"));
84 EXPECT_TRUE(tfile
.Step(section
));
85 EXPECT_EQ(4, section
.Count());
86 EXPECT_TRUE(section
.Exists("Version"));
87 EXPECT_TRUE(section
.Exists("Package"));
88 EXPECT_FALSE(section
.Exists("Size"));
89 EXPECT_TRUE(section
.Exists("Flag"));
90 EXPECT_TRUE(section
.Exists("Description"));
91 EXPECT_EQ("pkgB", section
.FindS("Package"));
92 EXPECT_EQ("1", section
.FindS("Version"));
93 EXPECT_EQ(1, section
.FindULL("Version"));
94 EXPECT_EQ(0, section
.FindULL("Size"));
96 EXPECT_TRUE(section
.FindFlag("Flag", Flags
, 1));
99 EXPECT_TRUE(section
.FindFlag("Flag", Flags
, 1));
101 EXPECT_EQ("bbb", section
.FindS("Description"));
103 EXPECT_TRUE(tfile
.Step(section
));
104 EXPECT_EQ(4, section
.Count());
105 EXPECT_TRUE(section
.Exists("Version"));
106 EXPECT_TRUE(section
.Exists("Package"));
107 EXPECT_FALSE(section
.Exists("Size"));
108 EXPECT_TRUE(section
.Exists("Flag"));
109 EXPECT_TRUE(section
.Exists("Description"));
110 EXPECT_EQ("pkgC", section
.FindS("Package"));
111 EXPECT_EQ("2", section
.FindS("Version"));
112 EXPECT_EQ(2, section
.FindULL("Version"));
114 EXPECT_TRUE(section
.FindFlag("Flag", Flags
, 1));
117 EXPECT_TRUE(section
.FindFlag("Flag", Flags
, 1));
119 EXPECT_EQ("ccc", section
.FindS("Description"));
121 // There is no section left in this tag file
122 EXPECT_FALSE(tfile
.Step(section
));
125 TEST(TagFileTest
,BigSection
)
127 size_t const count
= 500;
128 std::stringstream content
;
129 for (size_t i
= 0; i
< count
; ++i
)
130 content
<< "Field-" << i
<< ": " << (2000 + i
) << std::endl
;
133 createTemporaryFile("bigsection", fd
, NULL
, content
.str().c_str());
135 pkgTagFile
tfile(&fd
);
136 pkgTagSection section
;
137 EXPECT_TRUE(tfile
.Step(section
));
139 EXPECT_EQ(count
, section
.Count());
140 for (size_t i
= 0; i
< count
; ++i
)
142 std::stringstream name
;
143 name
<< "Field-" << i
;
144 EXPECT_TRUE(section
.Exists(name
.str().c_str())) << name
.str() << " does not exist";
145 EXPECT_EQ((2000 + i
), section
.FindULL(name
.str().c_str()));
148 // There is only one section in this tag file
149 EXPECT_FALSE(tfile
.Step(section
));
152 TEST(TagFileTest
, PickedUpFromPreviousCall
)
154 size_t const count
= 500;
155 std::stringstream contentstream
;
156 for (size_t i
= 0; i
< count
; ++i
)
157 contentstream
<< "Field-" << i
<< ": " << (2000 + i
) << std::endl
;
158 contentstream
<< std::endl
<< std::endl
;
159 std::string content
= contentstream
.str();
161 pkgTagSection section
;
162 EXPECT_FALSE(section
.Scan(content
.c_str(), content
.size()/2));
163 EXPECT_NE(0, section
.Count());
164 EXPECT_NE(count
, section
.Count());
165 EXPECT_TRUE(section
.Scan(content
.c_str(), content
.size(), false));
166 EXPECT_EQ(count
, section
.Count());
168 for (size_t i
= 0; i
< count
; ++i
)
170 std::stringstream name
;
171 name
<< "Field-" << i
;
172 EXPECT_TRUE(section
.Exists(name
.str().c_str())) << name
.str() << " does not exist";
173 EXPECT_EQ((2000 + i
), section
.FindULL(name
.str().c_str()));
177 TEST(TagFileTest
, SpacesEverywhere
)
179 std::string content
=
184 "ValueSpaces: \tyes\n"
185 "BothSpaces \t:\t yes\n"
186 "TrailingSpaces: yes\t \n"
187 "Naming Space: yes\n"
188 "Naming Spaces: yes\n"
190 "Multi-Colon::yes:\n"
193 pkgTagSection section
;
194 EXPECT_TRUE(section
.Scan(content
.c_str(), content
.size()));
195 EXPECT_TRUE(section
.Exists("Package"));
196 EXPECT_TRUE(section
.Exists("NoSpaces"));
197 EXPECT_TRUE(section
.Exists("TagSpaces"));
198 EXPECT_TRUE(section
.Exists("ValueSpaces"));
199 EXPECT_TRUE(section
.Exists("BothSpaces"));
200 EXPECT_TRUE(section
.Exists("TrailingSpaces"));
201 EXPECT_TRUE(section
.Exists("Naming Space"));
202 EXPECT_TRUE(section
.Exists("Naming Spaces"));
203 EXPECT_TRUE(section
.Exists("Multi-Colon"));
204 EXPECT_EQ("pkgC", section
.FindS("Package"));
205 EXPECT_EQ("yes", section
.FindS("NoSpaces"));
206 EXPECT_EQ("yes", section
.FindS("TagSpaces"));
207 EXPECT_EQ("yes", section
.FindS("ValueSpaces"));
208 EXPECT_EQ("yes", section
.FindS("BothSpaces"));
209 EXPECT_EQ("yes", section
.FindS("TrailingSpaces"));
210 EXPECT_EQ("yes", section
.FindS("Naming Space"));
211 EXPECT_EQ("yes", section
.FindS("Naming Spaces"));
212 EXPECT_EQ(":yes:", section
.FindS("Multi-Colon"));
213 // overridden values are still present, but not really accessible
214 EXPECT_EQ(11, section
.Count());