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,tokenize_empty)86 TEST(strings, tokenize_empty) {
87 std::vector<std::string> parts = android::base::Tokenize("", " ");
88 ASSERT_EQ(0U, parts.size());
89 }
90
TEST(strings,tokenize_all_delimiter)91 TEST(strings, tokenize_all_delimiter) {
92 std::vector<std::string> parts = android::base::Tokenize(" \t ", " \t");
93 ASSERT_EQ(0U, parts.size());
94 }
95
TEST(strings,tokenize_trivial)96 TEST(strings, tokenize_trivial) {
97 std::vector<std::string> parts = android::base::Tokenize("foo", "\t");
98 ASSERT_EQ(1U, parts.size());
99 ASSERT_EQ("foo", parts[0]);
100 }
101
TEST(strings,tokenize_single)102 TEST(strings, tokenize_single) {
103 std::vector<std::string> parts = android::base::Tokenize("foo\t", "\t");
104 ASSERT_EQ(1U, parts.size());
105 ASSERT_EQ("foo", parts[0]);
106 }
107
TEST(strings,tokenize_simple)108 TEST(strings, tokenize_simple) {
109 std::vector<std::string> parts = android::base::Tokenize("foo bar baz", " ");
110 ASSERT_EQ(3U, parts.size());
111 ASSERT_EQ("foo", parts[0]);
112 ASSERT_EQ("bar", parts[1]);
113 ASSERT_EQ("baz", parts[2]);
114 }
115
TEST(strings,tokenize_any)116 TEST(strings, tokenize_any) {
117 std::vector<std::string> parts = android::base::Tokenize("foo \tbar\t\t baz", " \t");
118 ASSERT_EQ(3U, parts.size());
119 ASSERT_EQ("foo", parts[0]);
120 ASSERT_EQ("bar", parts[1]);
121 ASSERT_EQ("baz", parts[2]);
122 }
123
TEST(strings,tokenize_beginning_trailing_delimiters)124 TEST(strings, tokenize_beginning_trailing_delimiters) {
125 std::vector<std::string> parts = android::base::Tokenize(" foo bar baz \t", " \t");
126 ASSERT_EQ(3U, parts.size());
127 ASSERT_EQ("foo", parts[0]);
128 ASSERT_EQ("bar", parts[1]);
129 ASSERT_EQ("baz", parts[2]);
130 }
131
TEST(strings,trim_empty)132 TEST(strings, trim_empty) {
133 ASSERT_EQ("", android::base::Trim(""));
134 }
135
TEST(strings,trim_already_trimmed)136 TEST(strings, trim_already_trimmed) {
137 ASSERT_EQ("foo", android::base::Trim("foo"));
138 }
139
TEST(strings,trim_left)140 TEST(strings, trim_left) {
141 ASSERT_EQ("foo", android::base::Trim(" foo"));
142 }
143
TEST(strings,trim_right)144 TEST(strings, trim_right) {
145 ASSERT_EQ("foo", android::base::Trim("foo "));
146 }
147
TEST(strings,trim_both)148 TEST(strings, trim_both) {
149 ASSERT_EQ("foo", android::base::Trim(" foo "));
150 }
151
TEST(strings,trim_no_trim_middle)152 TEST(strings, trim_no_trim_middle) {
153 ASSERT_EQ("foo bar", android::base::Trim("foo bar"));
154 }
155
TEST(strings,trim_other_whitespace)156 TEST(strings, trim_other_whitespace) {
157 ASSERT_EQ("foo", android::base::Trim("\v\tfoo\n\f"));
158 }
159
TEST(strings,trim_build_implicit_string_conversion)160 TEST(strings, trim_build_implicit_string_conversion) {
161 struct Foo {
162 operator std::string() { return " foo "; }
163 explicit operator std::string_view() { return " foo "; }
164 };
165 ASSERT_EQ("foo", android::base::Trim(Foo()));
166 }
167
TEST(strings,trim_build_implicit_string_view_conversion)168 TEST(strings, trim_build_implicit_string_view_conversion) {
169 struct Foo {
170 explicit operator std::string() { return " foo "; }
171 operator std::string_view() { return " foo "; }
172 };
173 ASSERT_EQ("foo", android::base::Trim(Foo()));
174 }
175
TEST(strings,join_nothing)176 TEST(strings, join_nothing) {
177 std::vector<std::string> list = {};
178 ASSERT_EQ("", android::base::Join(list, ','));
179 }
180
TEST(strings,join_single)181 TEST(strings, join_single) {
182 std::vector<std::string> list = {"foo"};
183 ASSERT_EQ("foo", android::base::Join(list, ','));
184 }
185
TEST(strings,join_simple)186 TEST(strings, join_simple) {
187 std::vector<std::string> list = {"foo", "bar", "baz"};
188 ASSERT_EQ("foo,bar,baz", android::base::Join(list, ','));
189 }
190
TEST(strings,join_separator_in_vector)191 TEST(strings, join_separator_in_vector) {
192 std::vector<std::string> list = {",", ","};
193 ASSERT_EQ(",,,", android::base::Join(list, ','));
194 }
195
TEST(strings,join_simple_ints)196 TEST(strings, join_simple_ints) {
197 std::set<int> list = {1, 2, 3};
198 ASSERT_EQ("1,2,3", android::base::Join(list, ','));
199 }
200
TEST(strings,join_unordered_set)201 TEST(strings, join_unordered_set) {
202 std::unordered_set<int> list = {1, 2};
203 ASSERT_TRUE("1,2" == android::base::Join(list, ',') ||
204 "2,1" == android::base::Join(list, ','));
205 }
206
TEST(strings,StartsWith_empty)207 TEST(strings, StartsWith_empty) {
208 ASSERT_FALSE(android::base::StartsWith("", "foo"));
209 ASSERT_TRUE(android::base::StartsWith("", ""));
210 }
211
TEST(strings,StartsWithIgnoreCase_empty)212 TEST(strings, StartsWithIgnoreCase_empty) {
213 ASSERT_FALSE(android::base::StartsWithIgnoreCase("", "foo"));
214 ASSERT_TRUE(android::base::StartsWithIgnoreCase("", ""));
215 }
216
TEST(strings,StartsWith_simple)217 TEST(strings, StartsWith_simple) {
218 ASSERT_TRUE(android::base::StartsWith("foo", ""));
219 ASSERT_TRUE(android::base::StartsWith("foo", "f"));
220 ASSERT_TRUE(android::base::StartsWith("foo", "fo"));
221 ASSERT_TRUE(android::base::StartsWith("foo", "foo"));
222 }
223
TEST(strings,StartsWithIgnoreCase_simple)224 TEST(strings, StartsWithIgnoreCase_simple) {
225 ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", ""));
226 ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "f"));
227 ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "F"));
228 ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "fo"));
229 ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "fO"));
230 ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "Fo"));
231 ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "FO"));
232 ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "foo"));
233 ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "foO"));
234 ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "fOo"));
235 ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "fOO"));
236 ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "Foo"));
237 ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "FoO"));
238 ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "FOo"));
239 ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "FOO"));
240 }
241
TEST(strings,StartsWith_prefix_too_long)242 TEST(strings, StartsWith_prefix_too_long) {
243 ASSERT_FALSE(android::base::StartsWith("foo", "foobar"));
244 }
245
TEST(strings,StartsWithIgnoreCase_prefix_too_long)246 TEST(strings, StartsWithIgnoreCase_prefix_too_long) {
247 ASSERT_FALSE(android::base::StartsWithIgnoreCase("foo", "foobar"));
248 ASSERT_FALSE(android::base::StartsWithIgnoreCase("foo", "FOOBAR"));
249 }
250
TEST(strings,StartsWith_contains_prefix)251 TEST(strings, StartsWith_contains_prefix) {
252 ASSERT_FALSE(android::base::StartsWith("foobar", "oba"));
253 ASSERT_FALSE(android::base::StartsWith("foobar", "bar"));
254 }
255
TEST(strings,StartsWithIgnoreCase_contains_prefix)256 TEST(strings, StartsWithIgnoreCase_contains_prefix) {
257 ASSERT_FALSE(android::base::StartsWithIgnoreCase("foobar", "oba"));
258 ASSERT_FALSE(android::base::StartsWithIgnoreCase("foobar", "OBA"));
259 ASSERT_FALSE(android::base::StartsWithIgnoreCase("foobar", "bar"));
260 ASSERT_FALSE(android::base::StartsWithIgnoreCase("foobar", "BAR"));
261 }
262
TEST(strings,StartsWith_char)263 TEST(strings, StartsWith_char) {
264 ASSERT_FALSE(android::base::StartsWith("", 'f'));
265 ASSERT_TRUE(android::base::StartsWith("foo", 'f'));
266 ASSERT_FALSE(android::base::StartsWith("foo", 'o'));
267 }
268
TEST(strings,EndsWith_empty)269 TEST(strings, EndsWith_empty) {
270 ASSERT_FALSE(android::base::EndsWith("", "foo"));
271 ASSERT_TRUE(android::base::EndsWith("", ""));
272 }
273
TEST(strings,EndsWithIgnoreCase_empty)274 TEST(strings, EndsWithIgnoreCase_empty) {
275 ASSERT_FALSE(android::base::EndsWithIgnoreCase("", "foo"));
276 ASSERT_FALSE(android::base::EndsWithIgnoreCase("", "FOO"));
277 ASSERT_TRUE(android::base::EndsWithIgnoreCase("", ""));
278 }
279
TEST(strings,EndsWith_simple)280 TEST(strings, EndsWith_simple) {
281 ASSERT_TRUE(android::base::EndsWith("foo", ""));
282 ASSERT_TRUE(android::base::EndsWith("foo", "o"));
283 ASSERT_TRUE(android::base::EndsWith("foo", "oo"));
284 ASSERT_TRUE(android::base::EndsWith("foo", "foo"));
285 }
286
TEST(strings,EndsWithIgnoreCase_simple)287 TEST(strings, EndsWithIgnoreCase_simple) {
288 ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", ""));
289 ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "o"));
290 ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "O"));
291 ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "oo"));
292 ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "oO"));
293 ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "Oo"));
294 ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "OO"));
295 ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "foo"));
296 ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "foO"));
297 ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "fOo"));
298 ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "fOO"));
299 ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "Foo"));
300 ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "FoO"));
301 ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "FOo"));
302 ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "FOO"));
303 }
304
TEST(strings,EndsWith_prefix_too_long)305 TEST(strings, EndsWith_prefix_too_long) {
306 ASSERT_FALSE(android::base::EndsWith("foo", "foobar"));
307 }
308
TEST(strings,EndsWithIgnoreCase_prefix_too_long)309 TEST(strings, EndsWithIgnoreCase_prefix_too_long) {
310 ASSERT_FALSE(android::base::EndsWithIgnoreCase("foo", "foobar"));
311 ASSERT_FALSE(android::base::EndsWithIgnoreCase("foo", "FOOBAR"));
312 }
313
TEST(strings,EndsWith_contains_prefix)314 TEST(strings, EndsWith_contains_prefix) {
315 ASSERT_FALSE(android::base::EndsWith("foobar", "oba"));
316 ASSERT_FALSE(android::base::EndsWith("foobar", "foo"));
317 }
318
TEST(strings,EndsWithIgnoreCase_contains_prefix)319 TEST(strings, EndsWithIgnoreCase_contains_prefix) {
320 ASSERT_FALSE(android::base::EndsWithIgnoreCase("foobar", "OBA"));
321 ASSERT_FALSE(android::base::EndsWithIgnoreCase("foobar", "FOO"));
322 }
323
TEST(strings,StartsWith_std_string)324 TEST(strings, StartsWith_std_string) {
325 ASSERT_TRUE(android::base::StartsWith("hello", std::string{"hell"}));
326 ASSERT_FALSE(android::base::StartsWith("goodbye", std::string{"hell"}));
327 }
328
TEST(strings,StartsWithIgnoreCase_std_string)329 TEST(strings, StartsWithIgnoreCase_std_string) {
330 ASSERT_TRUE(android::base::StartsWithIgnoreCase("HeLlO", std::string{"hell"}));
331 ASSERT_FALSE(android::base::StartsWithIgnoreCase("GoOdByE", std::string{"hell"}));
332 }
333
TEST(strings,EndsWith_std_string)334 TEST(strings, EndsWith_std_string) {
335 ASSERT_TRUE(android::base::EndsWith("hello", std::string{"lo"}));
336 ASSERT_FALSE(android::base::EndsWith("goodbye", std::string{"lo"}));
337 }
338
TEST(strings,EndsWithIgnoreCase_std_string)339 TEST(strings, EndsWithIgnoreCase_std_string) {
340 ASSERT_TRUE(android::base::EndsWithIgnoreCase("HeLlO", std::string{"lo"}));
341 ASSERT_FALSE(android::base::EndsWithIgnoreCase("GoOdByE", std::string{"lo"}));
342 }
343
TEST(strings,EndsWith_char)344 TEST(strings, EndsWith_char) {
345 ASSERT_FALSE(android::base::EndsWith("", 'o'));
346 ASSERT_TRUE(android::base::EndsWith("foo", 'o'));
347 ASSERT_FALSE(android::base::EndsWith("foo", "f"));
348 }
349
TEST(strings,EqualsIgnoreCase)350 TEST(strings, EqualsIgnoreCase) {
351 ASSERT_TRUE(android::base::EqualsIgnoreCase("foo", "FOO"));
352 ASSERT_TRUE(android::base::EqualsIgnoreCase("FOO", "foo"));
353 ASSERT_FALSE(android::base::EqualsIgnoreCase("foo", "bar"));
354 ASSERT_FALSE(android::base::EqualsIgnoreCase("foo", "fool"));
355 }
356
TEST(strings,ubsan_28729303)357 TEST(strings, ubsan_28729303) {
358 android::base::Split("/dev/null", ":");
359 }
360
TEST(strings,ConsumePrefix)361 TEST(strings, ConsumePrefix) {
362 std::string_view s{"foo.bar"};
363 ASSERT_FALSE(android::base::ConsumePrefix(&s, "bar."));
364 ASSERT_EQ("foo.bar", s);
365 ASSERT_TRUE(android::base::ConsumePrefix(&s, "foo."));
366 ASSERT_EQ("bar", s);
367 }
368
TEST(strings,ConsumeSuffix)369 TEST(strings, ConsumeSuffix) {
370 std::string_view s{"foo.bar"};
371 ASSERT_FALSE(android::base::ConsumeSuffix(&s, ".foo"));
372 ASSERT_EQ("foo.bar", s);
373 ASSERT_TRUE(android::base::ConsumeSuffix(&s, ".bar"));
374 ASSERT_EQ("foo", s);
375 }
376
TEST(strings,StringReplace_false)377 TEST(strings, StringReplace_false) {
378 // No change.
379 ASSERT_EQ("abcabc", android::base::StringReplace("abcabc", "z", "Z", false));
380 ASSERT_EQ("", android::base::StringReplace("", "z", "Z", false));
381 ASSERT_EQ("abcabc", android::base::StringReplace("abcabc", "", "Z", false));
382
383 // Equal lengths.
384 ASSERT_EQ("Abcabc", android::base::StringReplace("abcabc", "a", "A", false));
385 ASSERT_EQ("aBcabc", android::base::StringReplace("abcabc", "b", "B", false));
386 ASSERT_EQ("abCabc", android::base::StringReplace("abcabc", "c", "C", false));
387
388 // Longer replacement.
389 ASSERT_EQ("foobcabc", android::base::StringReplace("abcabc", "a", "foo", false));
390 ASSERT_EQ("afoocabc", android::base::StringReplace("abcabc", "b", "foo", false));
391 ASSERT_EQ("abfooabc", android::base::StringReplace("abcabc", "c", "foo", false));
392
393 // Shorter replacement.
394 ASSERT_EQ("xxyz", android::base::StringReplace("abcxyz", "abc", "x", false));
395 ASSERT_EQ("axyz", android::base::StringReplace("abcxyz", "bcx", "x", false));
396 ASSERT_EQ("abcx", android::base::StringReplace("abcxyz", "xyz", "x", false));
397 }
398
TEST(strings,StringReplace_true)399 TEST(strings, StringReplace_true) {
400 // No change.
401 ASSERT_EQ("abcabc", android::base::StringReplace("abcabc", "z", "Z", true));
402 ASSERT_EQ("", android::base::StringReplace("", "z", "Z", true));
403 ASSERT_EQ("abcabc", android::base::StringReplace("abcabc", "", "Z", true));
404
405 // Equal lengths.
406 ASSERT_EQ("AbcAbc", android::base::StringReplace("abcabc", "a", "A", true));
407 ASSERT_EQ("aBcaBc", android::base::StringReplace("abcabc", "b", "B", true));
408 ASSERT_EQ("abCabC", android::base::StringReplace("abcabc", "c", "C", true));
409
410 // Longer replacement.
411 ASSERT_EQ("foobcfoobc", android::base::StringReplace("abcabc", "a", "foo", true));
412 ASSERT_EQ("afoocafooc", android::base::StringReplace("abcabc", "b", "foo", true));
413 ASSERT_EQ("abfooabfoo", android::base::StringReplace("abcabc", "c", "foo", true));
414
415 // Shorter replacement.
416 ASSERT_EQ("xxyzx", android::base::StringReplace("abcxyzabc", "abc", "x", true));
417 ASSERT_EQ("<xx>", android::base::StringReplace("<abcabc>", "abc", "x", true));
418 }
419
TEST(strings,ErrnoNumberAsString)420 TEST(strings, ErrnoNumberAsString) {
421 EXPECT_EQ("No such file or directory", android::base::ErrnoNumberAsString(ENOENT));
422 }
423