]> git.saurik.com Git - apt.git/blob - test/libapt/tagfile_test.cc
implement a more c++-style TFRewrite alternative
[apt.git] / test / libapt / tagfile_test.cc
1 #include <config.h>
2
3 #include <apt-pkg/fileutl.h>
4 #include <apt-pkg/tagfile.h>
5
6 #include <string>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <unistd.h>
10 #include <sstream>
11
12 #include <gtest/gtest.h>
13
14 #include "file-helpers.h"
15
16 TEST(TagFileTest,SingleField)
17 {
18 FileFd fd;
19 createTemporaryFile("singlefield", fd, NULL, "FieldA-12345678: the value of the field");
20
21 pkgTagFile tfile(&fd);
22 pkgTagSection section;
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));
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"));
43 }
44
45 TEST(TagFileTest,MultipleSections)
46 {
47 FileFd fd;
48 createTemporaryFile("bigsection", fd, NULL, "Package: pkgA\n"
49 "Version: 1\n"
50 "Size: 100\n"
51 "Description: aaa\n"
52 " aaa\n"
53 "\n"
54 "Package: pkgB\n"
55 "Version: 1\n"
56 "Flag: no\n"
57 "Description: bbb\n"
58 "\n"
59 "Package: pkgC\n"
60 "Version: 2\n"
61 "Flag: yes\n"
62 "Description:\n"
63 " ccc\n"
64 );
65
66 pkgTagFile tfile(&fd);
67 pkgTagSection section;
68 EXPECT_FALSE(section.Exists("Version"));
69
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));
83 EXPECT_EQ(1, Flags);
84 Flags = 0;
85 EXPECT_TRUE(section.FindFlag("Flag", Flags, 1));
86 EXPECT_EQ(0, Flags);
87 EXPECT_EQ("aaa\n aaa", section.FindS("Description"));
88
89
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"));
101 Flags = 1;
102 EXPECT_TRUE(section.FindFlag("Flag", Flags, 1));
103 EXPECT_EQ(0, Flags);
104 Flags = 0;
105 EXPECT_TRUE(section.FindFlag("Flag", Flags, 1));
106 EXPECT_EQ(0, Flags);
107 EXPECT_EQ("bbb", section.FindS("Description"));
108
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"));
119 Flags = 0;
120 EXPECT_TRUE(section.FindFlag("Flag", Flags, 1));
121 EXPECT_EQ(1, Flags);
122 Flags = 1;
123 EXPECT_TRUE(section.FindFlag("Flag", Flags, 1));
124 EXPECT_EQ(1, Flags);
125 EXPECT_EQ("ccc", section.FindS("Description"));
126
127 // There is no section left in this tag file
128 EXPECT_FALSE(tfile.Step(section));
129 }
130
131 TEST(TagFileTest,BigSection)
132 {
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;
137
138 FileFd fd;
139 createTemporaryFile("bigsection", fd, NULL, content.str().c_str());
140
141 pkgTagFile tfile(&fd);
142 pkgTagSection section;
143 EXPECT_TRUE(tfile.Step(section));
144
145 EXPECT_EQ(count, section.Count());
146 for (size_t i = 0; i < count; ++i)
147 {
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()));
152 }
153
154 // There is only one section in this tag file
155 EXPECT_FALSE(tfile.Step(section));
156 }
157
158 TEST(TagFileTest, PickedUpFromPreviousCall)
159 {
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();
166
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());
173
174 for (size_t i = 0; i < count; ++i)
175 {
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()));
180 }
181 }
182
183 TEST(TagFileTest, SpacesEverywhere)
184 {
185 std::string content =
186 "Package: pkgA\n"
187 "Package: pkgB\n"
188 "NoSpaces:yes\n"
189 "TagSpaces\t :yes\n"
190 "ValueSpaces: \tyes\n"
191 "BothSpaces \t:\t yes\n"
192 "TrailingSpaces: yes\t \n"
193 "Naming Space: yes\n"
194 "Naming Spaces: yes\n"
195 "Package : pkgC \n"
196 "Multi-Colon::yes:\n"
197 "\n\n";
198
199 pkgTagSection section;
200 EXPECT_TRUE(section.Scan(content.c_str(), content.size()));
201 EXPECT_TRUE(section.Exists("Package"));
202 EXPECT_TRUE(section.Exists("NoSpaces"));
203 EXPECT_TRUE(section.Exists("TagSpaces"));
204 EXPECT_TRUE(section.Exists("ValueSpaces"));
205 EXPECT_TRUE(section.Exists("BothSpaces"));
206 EXPECT_TRUE(section.Exists("TrailingSpaces"));
207 EXPECT_TRUE(section.Exists("Naming Space"));
208 EXPECT_TRUE(section.Exists("Naming Spaces"));
209 EXPECT_TRUE(section.Exists("Multi-Colon"));
210 EXPECT_EQ("pkgC", section.FindS("Package"));
211 EXPECT_EQ("yes", section.FindS("NoSpaces"));
212 EXPECT_EQ("yes", section.FindS("TagSpaces"));
213 EXPECT_EQ("yes", section.FindS("ValueSpaces"));
214 EXPECT_EQ("yes", section.FindS("BothSpaces"));
215 EXPECT_EQ("yes", section.FindS("TrailingSpaces"));
216 EXPECT_EQ("yes", section.FindS("Naming Space"));
217 EXPECT_EQ("yes", section.FindS("Naming Spaces"));
218 EXPECT_EQ(":yes:", section.FindS("Multi-Colon"));
219 // overridden values are still present, but not really accessible
220 EXPECT_EQ(11, section.Count());
221 }