]> git.saurik.com Git - apt.git/blame - test/libapt/strutil_test.cc
rewrite ReadMessages()
[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}
224dc038
DK
100TEST(StrUtilTest,SubstVar)
101{
102 EXPECT_EQ("", SubstVar("", "fails", "passes"));
103 EXPECT_EQ("test ", SubstVar("test fails", "fails", ""));
104 EXPECT_EQ("test passes", SubstVar("test passes", "", "fails"));
105
106 EXPECT_EQ("test passes", SubstVar("test passes", "fails", "passes"));
107 EXPECT_EQ("test passes", SubstVar("test fails", "fails", "passes"));
108
109 EXPECT_EQ("starts with", SubstVar("beginnt with", "beginnt", "starts"));
110 EXPECT_EQ("beginnt with", SubstVar("starts with", "starts", "beginnt"));
111 EXPECT_EQ("is in middle", SubstVar("is in der middle", "in der", "in"));
112 EXPECT_EQ("is in der middle", SubstVar("is in middle", "in", "in der"));
113 EXPECT_EQ("does end", SubstVar("does enden", "enden", "end"));
114 EXPECT_EQ("does enden", SubstVar("does end", "end", "enden"));
115
116 EXPECT_EQ("abc", SubstVar("abc", "d", "a"));
117 EXPECT_EQ("abc", SubstVar("abd", "d", "c"));
118 EXPECT_EQ("abc", SubstVar("adc", "d", "b"));
119 EXPECT_EQ("abc", SubstVar("dbc", "d", "a"));
120
121 EXPECT_EQ("b", SubstVar("b", "aa", "a"));
122 EXPECT_EQ("bb", SubstVar("bb", "aa", "a"));
123 EXPECT_EQ("bbb", SubstVar("bbb", "aa", "a"));
124
125 EXPECT_EQ("aa", SubstVar("aaaa", "aa", "a"));
126 EXPECT_EQ("aaaa", SubstVar("aa", "a", "aa"));
127 EXPECT_EQ("aaaa", SubstVar("aaaa", "a", "a"));
128 EXPECT_EQ("a a a a ", SubstVar("aaaa", "a", "a "));
129
130 EXPECT_EQ(" bb bb bb bb ", SubstVar(" a a a a ", "a", "bb"));
131 EXPECT_EQ(" bb bb bb bb ", SubstVar(" aaa aaa aaa aaa ", "aaa", "bb"));
132 EXPECT_EQ(" bb a bb a bb a bb ", SubstVar(" aaa a aaa a aaa a aaa ", "aaa", "bb"));
133
134}
b0314abb
DK
135TEST(StrUtilTest,Base64Encode)
136{
137 EXPECT_EQ("QWxhZGRpbjpvcGVuIHNlc2FtZQ==", Base64Encode("Aladdin:open sesame"));
138 EXPECT_EQ("cGxlYXN1cmUu", Base64Encode("pleasure."));
139 EXPECT_EQ("bGVhc3VyZS4=", Base64Encode("leasure."));
140 EXPECT_EQ("ZWFzdXJlLg==", Base64Encode("easure."));
141 EXPECT_EQ("YXN1cmUu", Base64Encode("asure."));
142 EXPECT_EQ("c3VyZS4=", Base64Encode("sure."));
143 EXPECT_EQ("dXJlLg==", Base64Encode("ure."));
144 EXPECT_EQ("cmUu", Base64Encode("re."));
145 EXPECT_EQ("ZS4=", Base64Encode("e."));
146 EXPECT_EQ("Lg==", Base64Encode("."));
147 EXPECT_EQ("", Base64Encode(""));
148}
d8c71b3b
DK
149void ReadMessagesTestWithNewLine(char const * const nl, char const * const ab)
150{
151 SCOPED_TRACE(SubstVar(SubstVar(nl, "\n", "n"), "\r", "r") + " # " + ab);
152 FileFd fd;
153 std::string pkgA = "Package: pkgA\n"
154 "Version: 1\n"
155 "Size: 100\n"
156 "Description: aaa\n"
157 " aaa";
158 std::string pkgB = "Package: pkgB\n"
159 "Version: 1\n"
160 "Flag: no\n"
161 "Description: bbb";
162 std::string pkgC = "Package: pkgC\n"
163 "Version: 2\n"
164 "Flag: yes\n"
165 "Description:\n"
166 " ccc";
167
168 createTemporaryFile("readmessage", fd, NULL, (pkgA + nl + pkgB + nl + pkgC + nl).c_str());
169 std::vector<std::string> list;
170 EXPECT_TRUE(ReadMessages(fd.Fd(), list));
171 EXPECT_EQ(3, list.size());
172 EXPECT_EQ(pkgA, list[0]);
173 EXPECT_EQ(pkgB, list[1]);
174 EXPECT_EQ(pkgC, list[2]);
175
176 size_t const msgsize = 63990;
177 createTemporaryFile("readmessage", fd, NULL, NULL);
178 for (size_t j = 0; j < msgsize; ++j)
179 fd.Write(ab, strlen(ab));
180 for (size_t i = 0; i < 21; ++i)
181 {
182 std::string msg;
183 strprintf(msg, "msgsize=%zu i=%zu", msgsize, i);
184 SCOPED_TRACE(msg);
185 fd.Seek((msgsize + (i - 1)) * strlen(ab));
186 fd.Write(ab, strlen(ab));
187 fd.Write(nl, strlen(nl));
188 fd.Seek(0);
189 list.clear();
190 EXPECT_TRUE(ReadMessages(fd.Fd(), list));
191 EXPECT_EQ(1, list.size());
192 EXPECT_EQ((msgsize + i) * strlen(ab), list[0].length());
193 EXPECT_EQ(std::string::npos, list[0].find_first_not_of(ab));
194 }
195
196 list.clear();
197 fd.Write(pkgA.c_str(), pkgA.length());
198 fd.Write(nl, strlen(nl));
199 fd.Seek(0);
200 EXPECT_TRUE(ReadMessages(fd.Fd(), list));
201 EXPECT_EQ(2, list.size());
202 EXPECT_EQ((msgsize + 20) * strlen(ab), list[0].length());
203 EXPECT_EQ(std::string::npos, list[0].find_first_not_of(ab));
204 EXPECT_EQ(pkgA, list[1]);
205
206
207 fd.Close();
208}
209TEST(StrUtilTest,ReadMessages)
210{
211 ReadMessagesTestWithNewLine("\n\n", "a");
212 ReadMessagesTestWithNewLine("\r\n\r\n", "a");
213 ReadMessagesTestWithNewLine("\n\n", "ab");
214 ReadMessagesTestWithNewLine("\r\n\r\n", "ab");
215}