]>
git.saurik.com Git - apt.git/blob - test/libapt/strutil_test.cc
9bc3c76fdd12d86b1279b358762ed1081a968bc3
2 #include <apt-pkg/strutl.h>
3 #include <apt-pkg/fileutl.h>
7 #include <gtest/gtest.h>
9 #include "file-helpers.h"
11 TEST(StrUtilTest
,DeEscapeString
)
14 EXPECT_EQ("", DeEscapeString(""));
15 EXPECT_EQ("foobar", DeEscapeString("foobar"));
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"));
22 EXPECT_EQ("foo\\ x", DeEscapeString("foo\\\\ x"));
23 EXPECT_EQ("\\foo\\", DeEscapeString("\\\\foo\\\\"));
25 TEST(StrUtilTest
,StringStrip
)
27 EXPECT_EQ("", APT::String::Strip(""));
28 EXPECT_EQ("foobar", APT::String::Strip("foobar"));
29 EXPECT_EQ("foo bar", APT::String::Strip("foo bar"));
31 EXPECT_EQ("", APT::String::Strip(" "));
32 EXPECT_EQ("", APT::String::Strip(" \r\n \t "));
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 "));
40 TEST(StrUtilTest
,StringSplitBasic
)
42 std::vector
<std::string
> result
= StringSplit("", "");
43 EXPECT_EQ(result
.size(), 0);
45 result
= StringSplit("abc", "");
46 EXPECT_EQ(result
.size(), 0);
48 result
= StringSplit("", "abc");
49 EXPECT_EQ(result
.size(), 1);
51 result
= StringSplit("abc", "b");
52 ASSERT_EQ(result
.size(), 2);
53 EXPECT_EQ(result
[0], "a");
54 EXPECT_EQ(result
[1], "c");
56 result
= StringSplit("abc", "abc");
57 ASSERT_EQ(result
.size(), 2);
58 EXPECT_EQ(result
[0], "");
59 EXPECT_EQ(result
[1], "");
61 TEST(StrUtilTest
,StringSplitDpkgStatus
)
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
);
68 result
= StringSplit(input
, "");
69 EXPECT_EQ(result
.size(), 0);
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");
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");
82 TEST(StrUtilTest
,EndsWith
)
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"));
91 TEST(StrUtilTest
,StartsWith
)
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"));
100 TEST(StrUtilTest
,SubstVar
)
102 EXPECT_EQ("", SubstVar("", "fails", "passes"));
103 EXPECT_EQ("test ", SubstVar("test fails", "fails", ""));
104 EXPECT_EQ("test passes", SubstVar("test passes", "", "fails"));
106 EXPECT_EQ("test passes", SubstVar("test passes", "fails", "passes"));
107 EXPECT_EQ("test passes", SubstVar("test fails", "fails", "passes"));
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"));
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"));
121 EXPECT_EQ("b", SubstVar("b", "aa", "a"));
122 EXPECT_EQ("bb", SubstVar("bb", "aa", "a"));
123 EXPECT_EQ("bbb", SubstVar("bbb", "aa", "a"));
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 "));
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"));
135 TEST(StrUtilTest
,Base64Encode
)
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(""));
149 void ReadMessagesTestWithNewLine(char const * const nl
, char const * const ab
)
151 SCOPED_TRACE(SubstVar(SubstVar(nl
, "\n", "n"), "\r", "r") + " # " + ab
);
153 std::string pkgA
= "Package: pkgA\n"
158 std::string pkgB
= "Package: pkgB\n"
162 std::string pkgC
= "Package: pkgC\n"
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]);
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
)
183 strprintf(msg
, "msgsize=%zu i=%zu", msgsize
, i
);
185 fd
.Seek((msgsize
+ (i
- 1)) * strlen(ab
));
186 fd
.Write(ab
, strlen(ab
));
187 fd
.Write(nl
, strlen(nl
));
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
));
197 fd
.Write(pkgA
.c_str(), pkgA
.length());
198 fd
.Write(nl
, strlen(nl
));
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]);
209 TEST(StrUtilTest
,ReadMessages
)
211 ReadMessagesTestWithNewLine("\n\n", "a");
212 ReadMessagesTestWithNewLine("\r\n\r\n", "a");
213 ReadMessagesTestWithNewLine("\n\n", "ab");
214 ReadMessagesTestWithNewLine("\r\n\r\n", "ab");