]> git.saurik.com Git - apt.git/blame - test/libapt/strutil_test.cc
srv test: do 100 pulls twice and compare list
[apt.git] / test / libapt / strutil_test.cc
CommitLineData
453b82a3 1#include <config.h>
a513ace2 2#include <apt-pkg/strutl.h>
d8c71b3b 3#include <apt-pkg/fileutl.h>
453b82a3
DK
4#include <string>
5#include <vector>
6
f00832cc 7#include <gtest/gtest.h>
a513ace2 8
d8c71b3b
DK
9#include "file-helpers.h"
10
f00832cc 11TEST(StrUtilTest,DeEscapeString)
a513ace2 12{
f00832cc
DK
13 // nothing special
14 EXPECT_EQ("", DeEscapeString(""));
15 EXPECT_EQ("foobar", DeEscapeString("foobar"));
a513ace2 16 // hex and octal
f00832cc
DK
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}
b5787388
DK
25TEST(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}
f00832cc
DK
40TEST(StrUtilTest,StringSplitBasic)
41{
42 std::vector<std::string> result = StringSplit("", "");
43 EXPECT_EQ(result.size(), 0);
b9dc4706 44
f00832cc
DK
45 result = StringSplit("abc", "");
46 EXPECT_EQ(result.size(), 0);
00f4d9ff 47
f00832cc
DK
48 result = StringSplit("", "abc");
49 EXPECT_EQ(result.size(), 1);
00f4d9ff 50
f00832cc
DK
51 result = StringSplit("abc", "b");
52 ASSERT_EQ(result.size(), 2);
53 EXPECT_EQ(result[0], "a");
54 EXPECT_EQ(result[1], "c");
00f4d9ff 55
f00832cc
DK
56 result = StringSplit("abc", "abc");
57 ASSERT_EQ(result.size(), 2);
58 EXPECT_EQ(result[0], "");
59 EXPECT_EQ(result[1], "");
60}
61TEST(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);
85bf0019 67
2ddab3fb 68 result = StringSplit(input, "");
f00832cc
DK
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}
82TEST(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"));
a513ace2 90}
b0314abb 91TEST(StrUtilTest,StartsWith)
a5bb5e1e
MV
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}
b8eba208
DK
100TEST(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}
224dc038
DK
122TEST(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}
b0314abb
DK
157TEST(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}
d8c71b3b
DK
171void 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}
231TEST(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}