]> git.saurik.com Git - apt.git/blob - test/libapt/strutil_test.cc
abort connection on '.' target replies in SRV
[apt.git] / test / libapt / strutil_test.cc
1 #include <config.h>
2 #include <apt-pkg/strutl.h>
3 #include <apt-pkg/fileutl.h>
4 #include <string>
5 #include <vector>
6
7 #include <gtest/gtest.h>
8
9 #include "file-helpers.h"
10
11 TEST(StrUtilTest,DeEscapeString)
12 {
13 // nothing special
14 EXPECT_EQ("", DeEscapeString(""));
15 EXPECT_EQ("foobar", DeEscapeString("foobar"));
16 // hex and octal
17 EXPECT_EQ("foo bar\nbaz", DeEscapeString("foo\\040bar\\x0abaz"));
18 EXPECT_EQ("foo ", DeEscapeString("foo\\040"));
19 EXPECT_EQ("\nbaz", DeEscapeString("\\x0abaz"));
20 EXPECT_EQ("/media/Ubuntu 11.04 amd64", DeEscapeString("/media/Ubuntu\\04011.04\\040amd64"));
21 // double slashes
22 EXPECT_EQ("foo\\ x", DeEscapeString("foo\\\\ x"));
23 EXPECT_EQ("\\foo\\", DeEscapeString("\\\\foo\\\\"));
24 }
25 TEST(StrUtilTest,StringStrip)
26 {
27 EXPECT_EQ("", APT::String::Strip(""));
28 EXPECT_EQ("foobar", APT::String::Strip("foobar"));
29 EXPECT_EQ("foo bar", APT::String::Strip("foo bar"));
30
31 EXPECT_EQ("", APT::String::Strip(" "));
32 EXPECT_EQ("", APT::String::Strip(" \r\n \t "));
33
34 EXPECT_EQ("foo bar", APT::String::Strip("foo bar "));
35 EXPECT_EQ("foo bar", APT::String::Strip("foo bar \r\n \t "));
36 EXPECT_EQ("foo bar", APT::String::Strip("\r\n \t foo bar"));
37 EXPECT_EQ("bar foo", APT::String::Strip("\r\n \t bar foo \r\n \t "));
38 EXPECT_EQ("bar \t\r\n foo", APT::String::Strip("\r\n \t bar \t\r\n foo \r\n \t "));
39 }
40 TEST(StrUtilTest,StringSplitBasic)
41 {
42 std::vector<std::string> result = StringSplit("", "");
43 EXPECT_EQ(result.size(), 0);
44
45 result = StringSplit("abc", "");
46 EXPECT_EQ(result.size(), 0);
47
48 result = StringSplit("", "abc");
49 EXPECT_EQ(result.size(), 1);
50
51 result = StringSplit("abc", "b");
52 ASSERT_EQ(result.size(), 2);
53 EXPECT_EQ(result[0], "a");
54 EXPECT_EQ(result[1], "c");
55
56 result = StringSplit("abc", "abc");
57 ASSERT_EQ(result.size(), 2);
58 EXPECT_EQ(result[0], "");
59 EXPECT_EQ(result[1], "");
60 }
61 TEST(StrUtilTest,StringSplitDpkgStatus)
62 {
63 std::string const input = "status: libnet1:amd64: unpacked";
64 std::vector<std::string> result = StringSplit(input, "xxx");
65 ASSERT_EQ(result.size(), 1);
66 EXPECT_EQ(result[0], input);
67
68 result = StringSplit(input, "");
69 EXPECT_EQ(result.size(), 0);
70
71 result = StringSplit(input, ": ");
72 ASSERT_EQ(result.size(), 3);
73 EXPECT_EQ(result[0], "status");
74 EXPECT_EQ(result[1], "libnet1:amd64");
75 EXPECT_EQ(result[2], "unpacked");
76
77 result = StringSplit("x:y:z", ":", 2);
78 ASSERT_EQ(result.size(), 2);
79 EXPECT_EQ(result[0], "x");
80 EXPECT_EQ(result[1], "y:z");
81 }
82 TEST(StrUtilTest,EndsWith)
83 {
84 using APT::String::Endswith;
85 EXPECT_TRUE(Endswith("abcd", "d"));
86 EXPECT_TRUE(Endswith("abcd", "cd"));
87 EXPECT_TRUE(Endswith("abcd", "abcd"));
88 EXPECT_FALSE(Endswith("abcd", "x"));
89 EXPECT_FALSE(Endswith("abcd", "abcndefg"));
90 }
91 TEST(StrUtilTest,StartsWith)
92 {
93 using APT::String::Startswith;
94 EXPECT_TRUE(Startswith("abcd", "a"));
95 EXPECT_TRUE(Startswith("abcd", "ab"));
96 EXPECT_TRUE(Startswith("abcd", "abcd"));
97 EXPECT_FALSE(Startswith("abcd", "x"));
98 EXPECT_FALSE(Startswith("abcd", "abcndefg"));
99 }
100 TEST(StrUtilTest,TimeToStr)
101 {
102 EXPECT_EQ("0s", TimeToStr(0));
103 EXPECT_EQ("42s", TimeToStr(42));
104 EXPECT_EQ("9min 21s", TimeToStr((9*60) + 21));
105 EXPECT_EQ("20min 42s", TimeToStr((20*60) + 42));
106 EXPECT_EQ("10h 42min 21s", TimeToStr((10*3600) + (42*60) + 21));
107 EXPECT_EQ("10h 42min 21s", TimeToStr((10*3600) + (42*60) + 21));
108 EXPECT_EQ("1988d 3h 29min 7s", TimeToStr((1988*86400) + (3*3600) + (29*60) + 7));
109
110 EXPECT_EQ("59s", TimeToStr(59));
111 EXPECT_EQ("60s", TimeToStr(60));
112 EXPECT_EQ("1min 1s", TimeToStr(61));
113 EXPECT_EQ("59min 59s", TimeToStr(3599));
114 EXPECT_EQ("60min 0s", TimeToStr(3600));
115 EXPECT_EQ("1h 0min 1s", TimeToStr(3601));
116 EXPECT_EQ("1h 1min 0s", TimeToStr(3660));
117 EXPECT_EQ("23h 59min 59s", TimeToStr(86399));
118 EXPECT_EQ("24h 0min 0s", TimeToStr(86400));
119 EXPECT_EQ("1d 0h 0min 1s", TimeToStr(86401));
120 EXPECT_EQ("1d 0h 1min 0s", TimeToStr(86460));
121 }
122 TEST(StrUtilTest,SubstVar)
123 {
124 EXPECT_EQ("", SubstVar("", "fails", "passes"));
125 EXPECT_EQ("test ", SubstVar("test fails", "fails", ""));
126 EXPECT_EQ("test passes", SubstVar("test passes", "", "fails"));
127
128 EXPECT_EQ("test passes", SubstVar("test passes", "fails", "passes"));
129 EXPECT_EQ("test passes", SubstVar("test fails", "fails", "passes"));
130
131 EXPECT_EQ("starts with", SubstVar("beginnt with", "beginnt", "starts"));
132 EXPECT_EQ("beginnt with", SubstVar("starts with", "starts", "beginnt"));
133 EXPECT_EQ("is in middle", SubstVar("is in der middle", "in der", "in"));
134 EXPECT_EQ("is in der middle", SubstVar("is in middle", "in", "in der"));
135 EXPECT_EQ("does end", SubstVar("does enden", "enden", "end"));
136 EXPECT_EQ("does enden", SubstVar("does end", "end", "enden"));
137
138 EXPECT_EQ("abc", SubstVar("abc", "d", "a"));
139 EXPECT_EQ("abc", SubstVar("abd", "d", "c"));
140 EXPECT_EQ("abc", SubstVar("adc", "d", "b"));
141 EXPECT_EQ("abc", SubstVar("dbc", "d", "a"));
142
143 EXPECT_EQ("b", SubstVar("b", "aa", "a"));
144 EXPECT_EQ("bb", SubstVar("bb", "aa", "a"));
145 EXPECT_EQ("bbb", SubstVar("bbb", "aa", "a"));
146
147 EXPECT_EQ("aa", SubstVar("aaaa", "aa", "a"));
148 EXPECT_EQ("aaaa", SubstVar("aa", "a", "aa"));
149 EXPECT_EQ("aaaa", SubstVar("aaaa", "a", "a"));
150 EXPECT_EQ("a a a a ", SubstVar("aaaa", "a", "a "));
151
152 EXPECT_EQ(" bb bb bb bb ", SubstVar(" a a a a ", "a", "bb"));
153 EXPECT_EQ(" bb bb bb bb ", SubstVar(" aaa aaa aaa aaa ", "aaa", "bb"));
154 EXPECT_EQ(" bb a bb a bb a bb ", SubstVar(" aaa a aaa a aaa a aaa ", "aaa", "bb"));
155
156 }
157 TEST(StrUtilTest,Base64Encode)
158 {
159 EXPECT_EQ("QWxhZGRpbjpvcGVuIHNlc2FtZQ==", Base64Encode("Aladdin:open sesame"));
160 EXPECT_EQ("cGxlYXN1cmUu", Base64Encode("pleasure."));
161 EXPECT_EQ("bGVhc3VyZS4=", Base64Encode("leasure."));
162 EXPECT_EQ("ZWFzdXJlLg==", Base64Encode("easure."));
163 EXPECT_EQ("YXN1cmUu", Base64Encode("asure."));
164 EXPECT_EQ("c3VyZS4=", Base64Encode("sure."));
165 EXPECT_EQ("dXJlLg==", Base64Encode("ure."));
166 EXPECT_EQ("cmUu", Base64Encode("re."));
167 EXPECT_EQ("ZS4=", Base64Encode("e."));
168 EXPECT_EQ("Lg==", Base64Encode("."));
169 EXPECT_EQ("", Base64Encode(""));
170 }
171 static void ReadMessagesTestWithNewLine(char const * const nl, char const * const ab)
172 {
173 SCOPED_TRACE(SubstVar(SubstVar(nl, "\n", "n"), "\r", "r") + " # " + ab);
174 FileFd fd;
175 std::string pkgA = "Package: pkgA\n"
176 "Version: 1\n"
177 "Size: 100\n"
178 "Description: aaa\n"
179 " aaa";
180 std::string pkgB = "Package: pkgB\n"
181 "Version: 1\n"
182 "Flag: no\n"
183 "Description: bbb";
184 std::string pkgC = "Package: pkgC\n"
185 "Version: 2\n"
186 "Flag: yes\n"
187 "Description:\n"
188 " ccc";
189
190 createTemporaryFile("readmessage", fd, NULL, (pkgA + nl + pkgB + nl + pkgC + nl).c_str());
191 std::vector<std::string> list;
192 EXPECT_TRUE(ReadMessages(fd.Fd(), list));
193 EXPECT_EQ(3, list.size());
194 EXPECT_EQ(pkgA, list[0]);
195 EXPECT_EQ(pkgB, list[1]);
196 EXPECT_EQ(pkgC, list[2]);
197
198 size_t const msgsize = 63990;
199 createTemporaryFile("readmessage", fd, NULL, NULL);
200 for (size_t j = 0; j < msgsize; ++j)
201 fd.Write(ab, strlen(ab));
202 for (size_t i = 0; i < 21; ++i)
203 {
204 std::string msg;
205 strprintf(msg, "msgsize=%zu i=%zu", msgsize, i);
206 SCOPED_TRACE(msg);
207 fd.Seek((msgsize + (i - 1)) * strlen(ab));
208 fd.Write(ab, strlen(ab));
209 fd.Write(nl, strlen(nl));
210 fd.Seek(0);
211 list.clear();
212 EXPECT_TRUE(ReadMessages(fd.Fd(), list));
213 EXPECT_EQ(1, list.size());
214 EXPECT_EQ((msgsize + i) * strlen(ab), list[0].length());
215 EXPECT_EQ(std::string::npos, list[0].find_first_not_of(ab));
216 }
217
218 list.clear();
219 fd.Write(pkgA.c_str(), pkgA.length());
220 fd.Write(nl, strlen(nl));
221 fd.Seek(0);
222 EXPECT_TRUE(ReadMessages(fd.Fd(), list));
223 EXPECT_EQ(2, list.size());
224 EXPECT_EQ((msgsize + 20) * strlen(ab), list[0].length());
225 EXPECT_EQ(std::string::npos, list[0].find_first_not_of(ab));
226 EXPECT_EQ(pkgA, list[1]);
227
228
229 fd.Close();
230 }
231 TEST(StrUtilTest,ReadMessages)
232 {
233 ReadMessagesTestWithNewLine("\n\n", "a");
234 ReadMessagesTestWithNewLine("\r\n\r\n", "a");
235 ReadMessagesTestWithNewLine("\n\n", "ab");
236 ReadMessagesTestWithNewLine("\r\n\r\n", "ab");
237 }
238 TEST(StrUtilTest,QuoteString)
239 {
240 EXPECT_EQ("", QuoteString("", ""));
241 EXPECT_EQ("K%c3%b6ln", QuoteString("Köln", ""));
242 EXPECT_EQ("Köln", DeQuoteString(QuoteString("Köln", "")));
243 EXPECT_EQ("Köln", DeQuoteString(DeQuoteString(QuoteString(QuoteString("Köln", ""), ""))));
244 EXPECT_EQ("~-_$#|u%c3%a4%c3%b6%c5%a6%e2%84%a2%e2%85%9e%c2%b1%c3%86%e1%ba%9e%c2%aa%c3%9f", QuoteString("~-_$#|uäöŦ™⅞±Æẞªß", ""));
245 EXPECT_EQ("~-_$#|uäöŦ™⅞±Æẞªß", DeQuoteString(QuoteString("~-_$#|uäöŦ™⅞±Æẞªß", "")));
246 EXPECT_EQ("%45ltvill%65%2d%45rbach", QuoteString("Eltville-Erbach", "E-Ae"));
247 EXPECT_EQ("Eltville-Erbach", DeQuoteString(QuoteString("Eltville-Erbach", "")));
248 }
249
250 TEST(StrUtilTest,RFC1123StrToTime)
251 {
252 {
253 time_t t;
254 EXPECT_TRUE(RFC1123StrToTime("Sun, 06 Nov 1994 08:49:37 GMT", t));
255 EXPECT_EQ(784111777, t);
256 } {
257 time_t t;
258 EXPECT_TRUE(RFC1123StrToTime("Sun, 6 Nov 1994 08:49:37 UTC", t));
259 EXPECT_EQ(784111777, t);
260 } {
261 time_t t;
262 EXPECT_TRUE(RFC1123StrToTime("Sun, 6 Nov 1994 08:49:37 UTC", t));
263 EXPECT_EQ(784111777, t);
264 } {
265 time_t t;
266 EXPECT_TRUE(RFC1123StrToTime("Sun, 06 Nov 1994 8:49:37 UTC", t));
267 EXPECT_EQ(784111777, t);
268 } {
269 time_t t;
270 EXPECT_TRUE(RFC1123StrToTime("Sun, 06 Nov 1994 08:49:37 UTC", t));
271 EXPECT_EQ(784111777, t);
272 } {
273 time_t t;
274 EXPECT_TRUE(RFC1123StrToTime("Sun, 06 Nov 1994 08:49:37 -0000", t));
275 EXPECT_EQ(784111777, t);
276 } {
277 time_t t;
278 EXPECT_TRUE(RFC1123StrToTime("Sun, 06 Nov 1994 08:49:37 +0000", t));
279 EXPECT_EQ(784111777, t);
280 } {
281 time_t t;
282 EXPECT_TRUE(RFC1123StrToTime("Sunday, 06-Nov-94 08:49:37 GMT", t));
283 EXPECT_EQ(784111777, t);
284 } {
285 time_t t;
286 EXPECT_TRUE(RFC1123StrToTime("Sunday, 6-Nov-94 08:49:37 GMT", t));
287 EXPECT_EQ(784111777, t);
288 } {
289 time_t t;
290 EXPECT_TRUE(RFC1123StrToTime("Sunday, 06-Nov-94 8:49:37 GMT", t));
291 EXPECT_EQ(784111777, t);
292 } {
293 time_t t;
294 EXPECT_TRUE(RFC1123StrToTime("Sun Nov 6 08:49:37 1994", t));
295 EXPECT_EQ(784111777, t);
296 } {
297 time_t t;
298 EXPECT_TRUE(RFC1123StrToTime("Sun Nov 06 08:49:37 1994", t));
299 EXPECT_EQ(784111777, t);
300 } {
301 time_t t;
302 EXPECT_TRUE(RFC1123StrToTime("Sun Nov 6 8:49:37 1994", t));
303 EXPECT_EQ(784111777, t);
304 }
305 time_t t;
306 EXPECT_FALSE(RFC1123StrToTime("So, 06 Nov 1994 08:49:37 UTC", t));
307 EXPECT_FALSE(RFC1123StrToTime(", 06 Nov 1994 08:49:37 UTC", t));
308 EXPECT_FALSE(RFC1123StrToTime("Son, 06 Nov 1994 08:49:37 UTC", t));
309 EXPECT_FALSE(RFC1123StrToTime("Sun: 06 Nov 1994 08:49:37 UTC", t));
310 EXPECT_FALSE(RFC1123StrToTime("Sun, 06 Nov 1994 08:49:37", t));
311 EXPECT_FALSE(RFC1123StrToTime("Sun, 06 Nov 1994 08:49:37 GMT+1", t));
312 EXPECT_FALSE(RFC1123StrToTime("Sun, 06 Nov 1994 GMT", t));
313 EXPECT_FALSE(RFC1123StrToTime("Sunday, 06 Nov 1994 GMT", t));
314 EXPECT_FALSE(RFC1123StrToTime("Sonntag, 06 Nov 1994 08:49:37 GMT", t));
315 EXPECT_FALSE(RFC1123StrToTime("domingo Nov 6 08:49:37 1994", t));
316 EXPECT_FALSE(RFC1123StrToTime("Sunday: 06-Nov-94 08:49:37 GMT", t));
317 EXPECT_FALSE(RFC1123StrToTime("Sunday, 06-Nov-94 08:49:37 GMT+1", t));
318 EXPECT_FALSE(RFC1123StrToTime("Sunday, 06-Nov-94 08:49:37 EDT", t));
319 EXPECT_FALSE(RFC1123StrToTime("Sunday, 06-Nov-94 08:49:37 -0100", t));
320 EXPECT_FALSE(RFC1123StrToTime("Sunday, 06-Nov-94 08:49:37 -0.1", t));
321 }