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