1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "android-base/strings.h"
18 
19 #include <gtest/gtest.h>
20 
21 #include <string>
22 #include <vector>
23 #include <set>
24 #include <unordered_set>
25 
TEST(strings,split_empty)26 TEST(strings, split_empty) {
27   std::vector<std::string> parts = android::base::Split("", ",");
28   ASSERT_EQ(1U, parts.size());
29   ASSERT_EQ("", parts[0]);
30 }
31 
TEST(strings,split_single)32 TEST(strings, split_single) {
33   std::vector<std::string> parts = android::base::Split("foo", ",");
34   ASSERT_EQ(1U, parts.size());
35   ASSERT_EQ("foo", parts[0]);
36 }
37 
TEST(strings,split_simple)38 TEST(strings, split_simple) {
39   std::vector<std::string> parts = android::base::Split("foo,bar,baz", ",");
40   ASSERT_EQ(3U, parts.size());
41   ASSERT_EQ("foo", parts[0]);
42   ASSERT_EQ("bar", parts[1]);
43   ASSERT_EQ("baz", parts[2]);
44 }
45 
TEST(strings,split_with_empty_part)46 TEST(strings, split_with_empty_part) {
47   std::vector<std::string> parts = android::base::Split("foo,,bar", ",");
48   ASSERT_EQ(3U, parts.size());
49   ASSERT_EQ("foo", parts[0]);
50   ASSERT_EQ("", parts[1]);
51   ASSERT_EQ("bar", parts[2]);
52 }
53 
TEST(strings,split_with_trailing_empty_part)54 TEST(strings, split_with_trailing_empty_part) {
55   std::vector<std::string> parts = android::base::Split("foo,bar,", ",");
56   ASSERT_EQ(3U, parts.size());
57   ASSERT_EQ("foo", parts[0]);
58   ASSERT_EQ("bar", parts[1]);
59   ASSERT_EQ("", parts[2]);
60 }
61 
TEST(strings,split_null_char)62 TEST(strings, split_null_char) {
63   std::vector<std::string> parts =
64       android::base::Split(std::string("foo\0bar", 7), std::string("\0", 1));
65   ASSERT_EQ(2U, parts.size());
66   ASSERT_EQ("foo", parts[0]);
67   ASSERT_EQ("bar", parts[1]);
68 }
69 
TEST(strings,split_any)70 TEST(strings, split_any) {
71   std::vector<std::string> parts = android::base::Split("foo:bar,baz", ",:");
72   ASSERT_EQ(3U, parts.size());
73   ASSERT_EQ("foo", parts[0]);
74   ASSERT_EQ("bar", parts[1]);
75   ASSERT_EQ("baz", parts[2]);
76 }
77 
TEST(strings,split_any_with_empty_part)78 TEST(strings, split_any_with_empty_part) {
79   std::vector<std::string> parts = android::base::Split("foo:,bar", ",:");
80   ASSERT_EQ(3U, parts.size());
81   ASSERT_EQ("foo", parts[0]);
82   ASSERT_EQ("", parts[1]);
83   ASSERT_EQ("bar", parts[2]);
84 }
85 
TEST(strings,trim_empty)86 TEST(strings, trim_empty) {
87   ASSERT_EQ("", android::base::Trim(""));
88 }
89 
TEST(strings,trim_already_trimmed)90 TEST(strings, trim_already_trimmed) {
91   ASSERT_EQ("foo", android::base::Trim("foo"));
92 }
93 
TEST(strings,trim_left)94 TEST(strings, trim_left) {
95   ASSERT_EQ("foo", android::base::Trim(" foo"));
96 }
97 
TEST(strings,trim_right)98 TEST(strings, trim_right) {
99   ASSERT_EQ("foo", android::base::Trim("foo "));
100 }
101 
TEST(strings,trim_both)102 TEST(strings, trim_both) {
103   ASSERT_EQ("foo", android::base::Trim(" foo "));
104 }
105 
TEST(strings,trim_no_trim_middle)106 TEST(strings, trim_no_trim_middle) {
107   ASSERT_EQ("foo bar", android::base::Trim("foo bar"));
108 }
109 
TEST(strings,trim_other_whitespace)110 TEST(strings, trim_other_whitespace) {
111   ASSERT_EQ("foo", android::base::Trim("\v\tfoo\n\f"));
112 }
113 
TEST(strings,join_nothing)114 TEST(strings, join_nothing) {
115   std::vector<std::string> list = {};
116   ASSERT_EQ("", android::base::Join(list, ','));
117 }
118 
TEST(strings,join_single)119 TEST(strings, join_single) {
120   std::vector<std::string> list = {"foo"};
121   ASSERT_EQ("foo", android::base::Join(list, ','));
122 }
123 
TEST(strings,join_simple)124 TEST(strings, join_simple) {
125   std::vector<std::string> list = {"foo", "bar", "baz"};
126   ASSERT_EQ("foo,bar,baz", android::base::Join(list, ','));
127 }
128 
TEST(strings,join_separator_in_vector)129 TEST(strings, join_separator_in_vector) {
130   std::vector<std::string> list = {",", ","};
131   ASSERT_EQ(",,,", android::base::Join(list, ','));
132 }
133 
TEST(strings,join_simple_ints)134 TEST(strings, join_simple_ints) {
135   std::set<int> list = {1, 2, 3};
136   ASSERT_EQ("1,2,3", android::base::Join(list, ','));
137 }
138 
TEST(strings,join_unordered_set)139 TEST(strings, join_unordered_set) {
140   std::unordered_set<int> list = {1, 2};
141   ASSERT_TRUE("1,2" == android::base::Join(list, ',') ||
142               "2,1" == android::base::Join(list, ','));
143 }
144 
TEST(strings,StartsWith_empty)145 TEST(strings, StartsWith_empty) {
146   ASSERT_FALSE(android::base::StartsWith("", "foo"));
147   ASSERT_TRUE(android::base::StartsWith("", ""));
148 }
149 
TEST(strings,StartsWithIgnoreCase_empty)150 TEST(strings, StartsWithIgnoreCase_empty) {
151   ASSERT_FALSE(android::base::StartsWithIgnoreCase("", "foo"));
152   ASSERT_TRUE(android::base::StartsWithIgnoreCase("", ""));
153 }
154 
TEST(strings,StartsWith_simple)155 TEST(strings, StartsWith_simple) {
156   ASSERT_TRUE(android::base::StartsWith("foo", ""));
157   ASSERT_TRUE(android::base::StartsWith("foo", "f"));
158   ASSERT_TRUE(android::base::StartsWith("foo", "fo"));
159   ASSERT_TRUE(android::base::StartsWith("foo", "foo"));
160 }
161 
TEST(strings,StartsWithIgnoreCase_simple)162 TEST(strings, StartsWithIgnoreCase_simple) {
163   ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", ""));
164   ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "f"));
165   ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "F"));
166   ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "fo"));
167   ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "fO"));
168   ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "Fo"));
169   ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "FO"));
170   ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "foo"));
171   ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "foO"));
172   ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "fOo"));
173   ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "fOO"));
174   ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "Foo"));
175   ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "FoO"));
176   ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "FOo"));
177   ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "FOO"));
178 }
179 
TEST(strings,StartsWith_prefix_too_long)180 TEST(strings, StartsWith_prefix_too_long) {
181   ASSERT_FALSE(android::base::StartsWith("foo", "foobar"));
182 }
183 
TEST(strings,StartsWithIgnoreCase_prefix_too_long)184 TEST(strings, StartsWithIgnoreCase_prefix_too_long) {
185   ASSERT_FALSE(android::base::StartsWithIgnoreCase("foo", "foobar"));
186   ASSERT_FALSE(android::base::StartsWithIgnoreCase("foo", "FOOBAR"));
187 }
188 
TEST(strings,StartsWith_contains_prefix)189 TEST(strings, StartsWith_contains_prefix) {
190   ASSERT_FALSE(android::base::StartsWith("foobar", "oba"));
191   ASSERT_FALSE(android::base::StartsWith("foobar", "bar"));
192 }
193 
TEST(strings,StartsWithIgnoreCase_contains_prefix)194 TEST(strings, StartsWithIgnoreCase_contains_prefix) {
195   ASSERT_FALSE(android::base::StartsWithIgnoreCase("foobar", "oba"));
196   ASSERT_FALSE(android::base::StartsWithIgnoreCase("foobar", "OBA"));
197   ASSERT_FALSE(android::base::StartsWithIgnoreCase("foobar", "bar"));
198   ASSERT_FALSE(android::base::StartsWithIgnoreCase("foobar", "BAR"));
199 }
200 
TEST(strings,StartsWith_char)201 TEST(strings, StartsWith_char) {
202   ASSERT_FALSE(android::base::StartsWith("", 'f'));
203   ASSERT_TRUE(android::base::StartsWith("foo", 'f'));
204   ASSERT_FALSE(android::base::StartsWith("foo", 'o'));
205 }
206 
TEST(strings,EndsWith_empty)207 TEST(strings, EndsWith_empty) {
208   ASSERT_FALSE(android::base::EndsWith("", "foo"));
209   ASSERT_TRUE(android::base::EndsWith("", ""));
210 }
211 
TEST(strings,EndsWithIgnoreCase_empty)212 TEST(strings, EndsWithIgnoreCase_empty) {
213   ASSERT_FALSE(android::base::EndsWithIgnoreCase("", "foo"));
214   ASSERT_FALSE(android::base::EndsWithIgnoreCase("", "FOO"));
215   ASSERT_TRUE(android::base::EndsWithIgnoreCase("", ""));
216 }
217 
TEST(strings,EndsWith_simple)218 TEST(strings, EndsWith_simple) {
219   ASSERT_TRUE(android::base::EndsWith("foo", ""));
220   ASSERT_TRUE(android::base::EndsWith("foo", "o"));
221   ASSERT_TRUE(android::base::EndsWith("foo", "oo"));
222   ASSERT_TRUE(android::base::EndsWith("foo", "foo"));
223 }
224 
TEST(strings,EndsWithIgnoreCase_simple)225 TEST(strings, EndsWithIgnoreCase_simple) {
226   ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", ""));
227   ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "o"));
228   ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "O"));
229   ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "oo"));
230   ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "oO"));
231   ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "Oo"));
232   ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "OO"));
233   ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "foo"));
234   ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "foO"));
235   ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "fOo"));
236   ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "fOO"));
237   ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "Foo"));
238   ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "FoO"));
239   ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "FOo"));
240   ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "FOO"));
241 }
242 
TEST(strings,EndsWith_prefix_too_long)243 TEST(strings, EndsWith_prefix_too_long) {
244   ASSERT_FALSE(android::base::EndsWith("foo", "foobar"));
245 }
246 
TEST(strings,EndsWithIgnoreCase_prefix_too_long)247 TEST(strings, EndsWithIgnoreCase_prefix_too_long) {
248   ASSERT_FALSE(android::base::EndsWithIgnoreCase("foo", "foobar"));
249   ASSERT_FALSE(android::base::EndsWithIgnoreCase("foo", "FOOBAR"));
250 }
251 
TEST(strings,EndsWith_contains_prefix)252 TEST(strings, EndsWith_contains_prefix) {
253   ASSERT_FALSE(android::base::EndsWith("foobar", "oba"));
254   ASSERT_FALSE(android::base::EndsWith("foobar", "foo"));
255 }
256 
TEST(strings,EndsWithIgnoreCase_contains_prefix)257 TEST(strings, EndsWithIgnoreCase_contains_prefix) {
258   ASSERT_FALSE(android::base::EndsWithIgnoreCase("foobar", "OBA"));
259   ASSERT_FALSE(android::base::EndsWithIgnoreCase("foobar", "FOO"));
260 }
261 
TEST(strings,StartsWith_std_string)262 TEST(strings, StartsWith_std_string) {
263   ASSERT_TRUE(android::base::StartsWith("hello", std::string{"hell"}));
264   ASSERT_FALSE(android::base::StartsWith("goodbye", std::string{"hell"}));
265 }
266 
TEST(strings,StartsWithIgnoreCase_std_string)267 TEST(strings, StartsWithIgnoreCase_std_string) {
268   ASSERT_TRUE(android::base::StartsWithIgnoreCase("HeLlO", std::string{"hell"}));
269   ASSERT_FALSE(android::base::StartsWithIgnoreCase("GoOdByE", std::string{"hell"}));
270 }
271 
TEST(strings,EndsWith_std_string)272 TEST(strings, EndsWith_std_string) {
273   ASSERT_TRUE(android::base::EndsWith("hello", std::string{"lo"}));
274   ASSERT_FALSE(android::base::EndsWith("goodbye", std::string{"lo"}));
275 }
276 
TEST(strings,EndsWithIgnoreCase_std_string)277 TEST(strings, EndsWithIgnoreCase_std_string) {
278   ASSERT_TRUE(android::base::EndsWithIgnoreCase("HeLlO", std::string{"lo"}));
279   ASSERT_FALSE(android::base::EndsWithIgnoreCase("GoOdByE", std::string{"lo"}));
280 }
281 
TEST(strings,EndsWith_char)282 TEST(strings, EndsWith_char) {
283   ASSERT_FALSE(android::base::EndsWith("", 'o'));
284   ASSERT_TRUE(android::base::EndsWith("foo", 'o'));
285   ASSERT_FALSE(android::base::EndsWith("foo", "f"));
286 }
287 
TEST(strings,EqualsIgnoreCase)288 TEST(strings, EqualsIgnoreCase) {
289   ASSERT_TRUE(android::base::EqualsIgnoreCase("foo", "FOO"));
290   ASSERT_TRUE(android::base::EqualsIgnoreCase("FOO", "foo"));
291   ASSERT_FALSE(android::base::EqualsIgnoreCase("foo", "bar"));
292   ASSERT_FALSE(android::base::EqualsIgnoreCase("foo", "fool"));
293 }
294 
TEST(strings,ubsan_28729303)295 TEST(strings, ubsan_28729303) {
296   android::base::Split("/dev/null", ":");
297 }
298 
TEST(strings,ConsumePrefix)299 TEST(strings, ConsumePrefix) {
300   std::string_view s{"foo.bar"};
301   ASSERT_FALSE(android::base::ConsumePrefix(&s, "bar."));
302   ASSERT_EQ("foo.bar", s);
303   ASSERT_TRUE(android::base::ConsumePrefix(&s, "foo."));
304   ASSERT_EQ("bar", s);
305 }
306 
TEST(strings,ConsumeSuffix)307 TEST(strings, ConsumeSuffix) {
308   std::string_view s{"foo.bar"};
309   ASSERT_FALSE(android::base::ConsumeSuffix(&s, ".foo"));
310   ASSERT_EQ("foo.bar", s);
311   ASSERT_TRUE(android::base::ConsumeSuffix(&s, ".bar"));
312   ASSERT_EQ("foo", s);
313 }
314 
TEST(strings,StringReplace_false)315 TEST(strings, StringReplace_false) {
316   // No change.
317   ASSERT_EQ("abcabc", android::base::StringReplace("abcabc", "z", "Z", false));
318   ASSERT_EQ("", android::base::StringReplace("", "z", "Z", false));
319   ASSERT_EQ("abcabc", android::base::StringReplace("abcabc", "", "Z", false));
320 
321   // Equal lengths.
322   ASSERT_EQ("Abcabc", android::base::StringReplace("abcabc", "a", "A", false));
323   ASSERT_EQ("aBcabc", android::base::StringReplace("abcabc", "b", "B", false));
324   ASSERT_EQ("abCabc", android::base::StringReplace("abcabc", "c", "C", false));
325 
326   // Longer replacement.
327   ASSERT_EQ("foobcabc", android::base::StringReplace("abcabc", "a", "foo", false));
328   ASSERT_EQ("afoocabc", android::base::StringReplace("abcabc", "b", "foo", false));
329   ASSERT_EQ("abfooabc", android::base::StringReplace("abcabc", "c", "foo", false));
330 
331   // Shorter replacement.
332   ASSERT_EQ("xxyz", android::base::StringReplace("abcxyz", "abc", "x", false));
333   ASSERT_EQ("axyz", android::base::StringReplace("abcxyz", "bcx", "x", false));
334   ASSERT_EQ("abcx", android::base::StringReplace("abcxyz", "xyz", "x", false));
335 }
336 
TEST(strings,StringReplace_true)337 TEST(strings, StringReplace_true) {
338   // No change.
339   ASSERT_EQ("abcabc", android::base::StringReplace("abcabc", "z", "Z", true));
340   ASSERT_EQ("", android::base::StringReplace("", "z", "Z", true));
341   ASSERT_EQ("abcabc", android::base::StringReplace("abcabc", "", "Z", true));
342 
343   // Equal lengths.
344   ASSERT_EQ("AbcAbc", android::base::StringReplace("abcabc", "a", "A", true));
345   ASSERT_EQ("aBcaBc", android::base::StringReplace("abcabc", "b", "B", true));
346   ASSERT_EQ("abCabC", android::base::StringReplace("abcabc", "c", "C", true));
347 
348   // Longer replacement.
349   ASSERT_EQ("foobcfoobc", android::base::StringReplace("abcabc", "a", "foo", true));
350   ASSERT_EQ("afoocafooc", android::base::StringReplace("abcabc", "b", "foo", true));
351   ASSERT_EQ("abfooabfoo", android::base::StringReplace("abcabc", "c", "foo", true));
352 
353   // Shorter replacement.
354   ASSERT_EQ("xxyzx", android::base::StringReplace("abcxyzabc", "abc", "x", true));
355   ASSERT_EQ("<xx>", android::base::StringReplace("<abcabc>", "abc", "x", true));
356 }
357