1 /*
2 * Copyright (C) 2012 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 #define _GNU_SOURCE 1
18
19 #include <string.h>
20
21 #include <errno.h>
22 #include <gtest/gtest.h>
23 #include <malloc.h>
24 #include <math.h>
25 #include <stdint.h>
26 #include <sys/cdefs.h>
27
28 #include <algorithm>
29 #include <vector>
30
31 #include "buffer_tests.h"
32 #include "utils.h"
33
34 #if defined(NOFORTIFY)
35 #define STRING_TEST string_nofortify
36 #else
37 #define STRING_TEST string
38 #endif
39
40 #if defined(__BIONIC__)
41 #define STRLCPY_SUPPORTED
42 #define STRLCAT_SUPPORTED
43 #endif
44
45 constexpr auto KB = 1024;
46 constexpr auto SMALL = 1 * KB;
47 constexpr auto MEDIUM = 4 * KB;
48 constexpr auto LARGE = 64 * KB;
49
signum(int i)50 static int signum(int i) {
51 if (i < 0) {
52 return -1;
53 } else if (i > 0) {
54 return 1;
55 }
56 return 0;
57 }
58
TEST(STRING_TEST,strerror)59 TEST(STRING_TEST, strerror) {
60 // Valid.
61 ASSERT_STREQ("Success", strerror(0));
62 ASSERT_STREQ("Operation not permitted", strerror(1));
63
64 // Invalid.
65 ASSERT_STREQ("Unknown error -1", strerror(-1));
66 ASSERT_STREQ("Unknown error 134", strerror(EHWPOISON + 1));
67 }
68
TEST(STRING_TEST,strerror_l)69 TEST(STRING_TEST, strerror_l) {
70 // bionic just forwards to strerror(3).
71 ASSERT_STREQ("Success", strerror_l(0, LC_GLOBAL_LOCALE));
72 }
73
74 #if defined(__BIONIC__)
ConcurrentStrErrorFn(void *)75 static void* ConcurrentStrErrorFn(void*) {
76 bool equal = (strcmp("Unknown error 2002", strerror(2002)) == 0);
77 return reinterpret_cast<void*>(equal);
78 }
79 #endif // __BIONIC__
80
81 // glibc's strerror isn't thread safe, only its strsignal.
TEST(STRING_TEST,strerror_concurrent)82 TEST(STRING_TEST, strerror_concurrent) {
83 #if defined(__BIONIC__)
84 const char* strerror1001 = strerror(1001);
85 ASSERT_STREQ("Unknown error 1001", strerror1001);
86
87 pthread_t t;
88 ASSERT_EQ(0, pthread_create(&t, nullptr, ConcurrentStrErrorFn, nullptr));
89 void* result;
90 ASSERT_EQ(0, pthread_join(t, &result));
91 ASSERT_TRUE(static_cast<bool>(result));
92
93 ASSERT_STREQ("Unknown error 1001", strerror1001);
94 #else // __BIONIC__
95 GTEST_SKIP() << "thread-safe strerror not available";
96 #endif // __BIONIC__
97 }
98
TEST(STRING_TEST,gnu_strerror_r)99 TEST(STRING_TEST, gnu_strerror_r) {
100 #if !defined(ANDROID_HOST_MUSL)
101 char buf[256];
102
103 // Note that glibc doesn't necessarily write into the buffer.
104
105 // Valid.
106 ASSERT_STREQ("Success", strerror_r(0, buf, sizeof(buf)));
107 #if defined(__BIONIC__)
108 ASSERT_STREQ("Success", buf);
109 #endif
110 ASSERT_STREQ("Operation not permitted", strerror_r(1, buf, sizeof(buf)));
111 #if defined(__BIONIC__)
112 ASSERT_STREQ("Operation not permitted", buf);
113 #endif
114
115 // Invalid.
116 ASSERT_STREQ("Unknown error -1", strerror_r(-1, buf, sizeof(buf)));
117 ASSERT_STREQ("Unknown error -1", buf);
118 ASSERT_STREQ("Unknown error 1234", strerror_r(1234, buf, sizeof(buf)));
119 ASSERT_STREQ("Unknown error 1234", buf);
120
121 // Buffer too small.
122 errno = 0;
123 memset(buf, 0, sizeof(buf));
124 ASSERT_EQ(buf, strerror_r(4567, buf, 2));
125 ASSERT_STREQ("U", buf);
126 // The GNU strerror_r doesn't set errno (the POSIX one sets it to ERANGE).
127 ASSERT_ERRNO(0);
128 #else
129 GTEST_SKIP() << "musl doesn't have GNU strerror_r";
130 #endif
131 }
132
TEST(STRING_TEST,strsignal)133 TEST(STRING_TEST, strsignal) {
134 // A regular signal.
135 ASSERT_STREQ("Hangup", strsignal(1));
136
137 // A real-time signal.
138 ASSERT_STREQ("Real-time signal 14", strsignal(SIGRTMIN + 14));
139 // One of the signals the C library keeps to itself.
140 ASSERT_STREQ("Unknown signal 32", strsignal(32)); // __SIGRTMIN
141
142 // Errors.
143 ASSERT_STREQ("Unknown signal -1", strsignal(-1)); // Too small.
144 ASSERT_STREQ("Unknown signal 0", strsignal(0)); // Still too small.
145 ASSERT_STREQ("Unknown signal 1234", strsignal(1234)); // Too large.
146 }
147
ConcurrentStrSignalFn(void *)148 static void* ConcurrentStrSignalFn(void*) {
149 bool equal = (strcmp("Unknown signal 2002", strsignal(2002)) == 0);
150 return reinterpret_cast<void*>(equal);
151 }
152
TEST(STRING_TEST,strsignal_concurrent)153 TEST(STRING_TEST, strsignal_concurrent) {
154 const char* strsignal1001 = strsignal(1001);
155 ASSERT_STREQ("Unknown signal 1001", strsignal1001);
156
157 pthread_t t;
158 ASSERT_EQ(0, pthread_create(&t, nullptr, ConcurrentStrSignalFn, nullptr));
159 void* result;
160 ASSERT_EQ(0, pthread_join(t, &result));
161 ASSERT_TRUE(static_cast<bool>(result));
162
163 ASSERT_STREQ("Unknown signal 1001", strsignal1001);
164 }
165
166 // TODO: where did this number come from?
167 #define ITER 500
168
169 // For every length we want to test, vary and change alignment
170 // of allocated memory, fill it with some values, calculate
171 // expected result and then run function and compare what we got.
172 // These tests contributed by Intel Corporation.
173 // TODO: make these tests more intention-revealing and less random.
174 template<class Character>
175 class StringTestState {
176 public:
StringTestState(size_t MAX_LEN)177 explicit StringTestState(size_t MAX_LEN) : MAX_LEN(MAX_LEN), align1_index_(0), align2_index_(0) {
178 int max_alignment = 64;
179
180 // TODO: fix the tests to not sometimes use twice their specified "MAX_LEN".
181 glob_ptr = reinterpret_cast<Character*>(memalign(sysconf(_SC_PAGESIZE), 2 * sizeof(Character) * MAX_LEN + max_alignment));
182 glob_ptr1 = reinterpret_cast<Character*>(memalign(sysconf(_SC_PAGESIZE), 2 * sizeof(Character) * MAX_LEN + max_alignment));
183 glob_ptr2 = reinterpret_cast<Character*>(memalign(sysconf(_SC_PAGESIZE), 2 * sizeof(Character) * MAX_LEN + max_alignment));
184
185 InitLenArray();
186
187 srandom(1234);
188 }
189
~StringTestState()190 ~StringTestState() {
191 free(glob_ptr);
192 free(glob_ptr1);
193 free(glob_ptr2);
194 }
195
BeginIterations()196 void BeginIterations() {
197 align1_index_ = 0;
198 align2_index_ = 0;
199
200 ResetPointers();
201 }
202
HasNextIteration()203 bool HasNextIteration() {
204 return (align1_index_ != (alignments_size - 1) || align2_index_ != (alignments_size - 1));
205 }
206
NextIteration()207 void NextIteration() {
208 if (align1_index_ == (alignments_size - 1) && align2_index_ == (alignments_size - 1)) {
209 return;
210 }
211
212 if (align1_index_ == (alignments_size - 1)) {
213 align1_index_ = 0;
214 align2_index_++;
215 } else {
216 align1_index_++;
217 }
218
219 ResetPointers();
220 }
221
222 const size_t MAX_LEN;
223 Character *ptr, *ptr1, *ptr2;
224 size_t n;
225 size_t len[ITER + 1];
226
227 private:
228 static size_t alignments[];
229 static size_t alignments_size;
230 Character *glob_ptr, *glob_ptr1, *glob_ptr2;
231 size_t align1_index_, align2_index_;
232
233 // Calculate input lengths and fill state.len with them.
234 // Test small lengths with more density than big ones. Manually push
235 // smallest (0) and biggest (MAX_LEN) lengths. Avoid repeats.
236 // Return number of lengths to test.
InitLenArray()237 void InitLenArray() {
238 n = 0;
239 len[n++] = 0;
240 for (size_t i = 1; i < ITER; ++i) {
241 size_t l = static_cast<size_t>(exp(log(static_cast<double>(MAX_LEN)) * i / ITER));
242 if (l != len[n - 1]) {
243 len[n++] = l;
244 }
245 }
246 len[n++] = MAX_LEN;
247 }
248
ResetPointers()249 void ResetPointers() {
250 if (align1_index_ == alignments_size || align2_index_ == alignments_size) {
251 ptr = ptr1 = ptr2 = nullptr;
252 } else {
253 ptr = glob_ptr + alignments[align1_index_];
254 ptr1 = glob_ptr1 + alignments[align1_index_];
255 ptr2 = glob_ptr2 + alignments[align2_index_];
256 }
257 }
258 };
259
260 template<class Character>
261 size_t StringTestState<Character>::alignments[] = { 24, 32, 16, 48, 0, 1, 2, 3, 4, 5, 6, 7, 11 };
262
263 template<class Character>
264 size_t StringTestState<Character>::alignments_size = sizeof(alignments)/sizeof(size_t);
265
TEST(STRING_TEST,strcat)266 TEST(STRING_TEST, strcat) {
267 StringTestState<char> state(SMALL);
268 for (size_t i = 1; i < state.n; i++) {
269 for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) {
270 memset(state.ptr2, '\2', state.MAX_LEN);
271 state.ptr2[state.MAX_LEN - 1] = '\0';
272 memcpy(state.ptr, state.ptr2, 2 * state.MAX_LEN);
273
274 memset(state.ptr1, 'L', state.len[i]);
275 state.ptr1[random() % state.len[i]] = '\0';
276 state.ptr1[state.len[i] - 1] = '\0';
277
278 strcpy(state.ptr + state.MAX_LEN - 1, state.ptr1);
279
280 EXPECT_TRUE(strcat(state.ptr2, state.ptr1) == state.ptr2);
281 EXPECT_TRUE(memcmp(state.ptr, state.ptr2, 2 * state.MAX_LEN) == 0);
282 }
283 }
284 }
285
286 // one byte target with "\0" source
TEST(STRING_TEST,strcpy2)287 TEST(STRING_TEST, strcpy2) {
288 char buf[1];
289 char* orig = strdup("");
290 ASSERT_EQ(buf, strcpy(buf, orig));
291 ASSERT_EQ('\0', buf[0]);
292 free(orig);
293 }
294
295 // multibyte target where we under fill target
TEST(STRING_TEST,strcpy3)296 TEST(STRING_TEST, strcpy3) {
297 char buf[10];
298 char* orig = strdup("12345");
299 memset(buf, 'A', sizeof(buf));
300 ASSERT_EQ(buf, strcpy(buf, orig));
301 ASSERT_STREQ("12345", buf);
302 ASSERT_EQ('A', buf[6]);
303 ASSERT_EQ('A', buf[7]);
304 ASSERT_EQ('A', buf[8]);
305 ASSERT_EQ('A', buf[9]);
306 free(orig);
307 }
308
309 // multibyte target where we fill target exactly
TEST(STRING_TEST,strcpy4)310 TEST(STRING_TEST, strcpy4) {
311 char buf[10];
312 char* orig = strdup("123456789");
313 memset(buf, 'A', sizeof(buf));
314 ASSERT_EQ(buf, strcpy(buf, orig));
315 ASSERT_STREQ("123456789", buf);
316 free(orig);
317 }
318
319 // one byte target with "\0" source
TEST(STRING_TEST,stpcpy2)320 TEST(STRING_TEST, stpcpy2) {
321 char buf[1];
322 memset(buf, 'A', sizeof(buf));
323 char* orig = strdup("");
324 EXPECT_EQ(buf, stpcpy(buf, orig));
325 EXPECT_EQ('\0', buf[0]);
326 free(orig);
327 }
328
329 // multibyte target where we under fill target
TEST(STRING_TEST,stpcpy3)330 TEST(STRING_TEST, stpcpy3) {
331 char buf[10];
332 memset(buf, 'A', sizeof(buf));
333 char* orig = strdup("12345");
334 EXPECT_EQ(buf+strlen(orig), stpcpy(buf, orig));
335 EXPECT_STREQ("12345", buf);
336 EXPECT_EQ('A', buf[6]);
337 EXPECT_EQ('A', buf[7]);
338 EXPECT_EQ('A', buf[8]);
339 EXPECT_EQ('A', buf[9]);
340 free(orig);
341 }
342
343 // multibyte target where we fill target exactly
TEST(STRING_TEST,stpcpy4)344 TEST(STRING_TEST, stpcpy4) {
345 char buf[10];
346 memset(buf, 'A', sizeof(buf));
347 char* orig = strdup("123456789");
348 EXPECT_EQ(buf+strlen(orig), stpcpy(buf, orig));
349 EXPECT_STREQ("123456789", buf);
350 free(orig);
351 }
352
TEST(STRING_TEST,strcat2)353 TEST(STRING_TEST, strcat2) {
354 char buf[10];
355 memset(buf, 'A', sizeof(buf));
356 buf[0] = 'a';
357 buf[1] = '\0';
358 char* res = strcat(buf, "01234");
359 ASSERT_EQ(buf, res);
360 ASSERT_STREQ("a01234", buf);
361 ASSERT_EQ('A', buf[7]);
362 ASSERT_EQ('A', buf[8]);
363 ASSERT_EQ('A', buf[9]);
364 }
365
TEST(STRING_TEST,strcat3)366 TEST(STRING_TEST, strcat3) {
367 char buf[10];
368 memset(buf, 'A', sizeof(buf));
369 buf[0] = 'a';
370 buf[1] = '\0';
371 char* res = strcat(buf, "01234567");
372 ASSERT_EQ(buf, res);
373 ASSERT_STREQ("a01234567", buf);
374 }
375
TEST(STRING_TEST,strncat2)376 TEST(STRING_TEST, strncat2) {
377 char buf[10];
378 memset(buf, 'A', sizeof(buf));
379 buf[0] = 'a';
380 buf[1] = '\0';
381 char* res = strncat(buf, "01234", sizeof(buf) - strlen(buf) - 1);
382 ASSERT_EQ(buf, res);
383 ASSERT_STREQ("a01234", buf);
384 ASSERT_EQ('A', buf[7]);
385 ASSERT_EQ('A', buf[8]);
386 ASSERT_EQ('A', buf[9]);
387 }
388
TEST(STRING_TEST,strncat3)389 TEST(STRING_TEST, strncat3) {
390 char buf[10];
391 memset(buf, 'A', sizeof(buf));
392 buf[0] = 'a';
393 buf[1] = '\0';
394 char* res = strncat(buf, "0123456789", 5);
395 ASSERT_EQ(buf, res);
396 ASSERT_STREQ("a01234", buf);
397 ASSERT_EQ('A', buf[7]);
398 ASSERT_EQ('A', buf[8]);
399 ASSERT_EQ('A', buf[9]);
400 }
401
TEST(STRING_TEST,strncat4)402 TEST(STRING_TEST, strncat4) {
403 char buf[10];
404 memset(buf, 'A', sizeof(buf));
405 buf[0] = 'a';
406 buf[1] = '\0';
407 char* res = strncat(buf, "01234567", 8);
408 ASSERT_EQ(buf, res);
409 ASSERT_STREQ("a01234567", buf);
410 }
411
TEST(STRING_TEST,strncat5)412 TEST(STRING_TEST, strncat5) {
413 char buf[10];
414 memset(buf, 'A', sizeof(buf));
415 buf[0] = 'a';
416 buf[1] = '\0';
417 char* res = strncat(buf, "01234567", 9);
418 ASSERT_EQ(buf, res);
419 ASSERT_STREQ("a01234567", buf);
420 }
421
TEST(STRING_TEST,strchr_with_0)422 TEST(STRING_TEST, strchr_with_0) {
423 char buf[10];
424 const char* s = "01234";
425 memcpy(buf, s, strlen(s) + 1);
426 EXPECT_TRUE(strchr(buf, '\0') == (buf + strlen(s)));
427 }
428
TEST(STRING_TEST,strchr_multiple)429 TEST(STRING_TEST, strchr_multiple) {
430 char str[128];
431 memset(str, 'a', sizeof(str) - 1);
432 str[sizeof(str)-1] = '\0';
433
434 // Verify that strchr finds the first occurrence of 'a' in a string
435 // filled with 'a' characters. Iterate over the string putting
436 // non 'a' characters at the front of the string during each iteration
437 // and continue to verify that strchr can find the first occurrence
438 // properly. The idea is to cover all possible alignments of the location
439 // of the first occurrence of the 'a' character and which includes
440 // other 'a' characters close by.
441 for (size_t i = 0; i < sizeof(str) - 1; i++) {
442 EXPECT_EQ(&str[i], strchr(str, 'a'));
443 str[i] = 'b';
444 }
445 }
446
TEST(STRING_TEST,strchr)447 TEST(STRING_TEST, strchr) {
448 int seek_char = 'R';
449
450 StringTestState<char> state(SMALL);
451 for (size_t i = 1; i < state.n; i++) {
452 for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) {
453 if (~seek_char > 0) {
454 memset(state.ptr1, ~seek_char, state.len[i]);
455 } else {
456 memset(state.ptr1, '\1', state.len[i]);
457 }
458 state.ptr1[state.len[i] - 1] = '\0';
459
460 size_t pos = random() % state.MAX_LEN;
461 char* expected;
462 if (pos >= state.len[i] - 1) {
463 if (seek_char == 0) {
464 expected = state.ptr1 + state.len[i] - 1;
465 } else {
466 expected = nullptr;
467 }
468 } else {
469 state.ptr1[pos] = seek_char;
470 expected = state.ptr1 + pos;
471 }
472
473 ASSERT_TRUE(strchr(state.ptr1, seek_char) == expected);
474 }
475 }
476 }
477
TEST(STRING_TEST,strchrnul)478 TEST(STRING_TEST, strchrnul) {
479 const char* s = "01234222";
480 EXPECT_TRUE(strchrnul(s, '2') == &s[2]);
481 EXPECT_TRUE(strchrnul(s, '8') == (s + strlen(s)));
482 EXPECT_TRUE(strchrnul(s, '\0') == (s + strlen(s)));
483 }
484
TEST(STRING_TEST,strcmp)485 TEST(STRING_TEST, strcmp) {
486 StringTestState<char> state(SMALL);
487 for (size_t i = 1; i < state.n; i++) {
488 for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) {
489 memset(state.ptr1, 'v', state.MAX_LEN);
490 memset(state.ptr2, 'n', state.MAX_LEN);
491 state.ptr1[state.len[i] - 1] = '\0';
492 state.ptr2[state.len[i] - 1] = '\0';
493
494 size_t pos = 1 + (random() % (state.MAX_LEN - 1));
495 int actual;
496 int expected;
497 if (pos >= state.len[i] - 1) {
498 memcpy(state.ptr1, state.ptr2, state.len[i]);
499 expected = 0;
500 actual = strcmp(state.ptr1, state.ptr2);
501 } else {
502 memcpy(state.ptr1, state.ptr2, pos);
503 if (state.ptr1[pos] > state.ptr2[pos]) {
504 expected = 1;
505 } else if (state.ptr1[pos] == state.ptr2[pos]) {
506 state.ptr1[pos + 1] = '\0';
507 state.ptr2[pos + 1] = '\0';
508 expected = 0;
509 } else {
510 expected = -1;
511 }
512 actual = strcmp(state.ptr1, state.ptr2);
513 }
514
515 ASSERT_EQ(expected, signum(actual));
516 }
517 }
518 }
519
TEST(STRING_TEST,stpcpy)520 TEST(STRING_TEST, stpcpy) {
521 StringTestState<char> state(SMALL);
522 for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) {
523 size_t pos = random() % state.MAX_LEN;
524
525 memset(state.ptr1, '\2', pos);
526 state.ptr1[pos] = '\0';
527 state.ptr1[state.MAX_LEN - 1] = '\0';
528
529 memcpy(state.ptr, state.ptr1, state.MAX_LEN);
530
531 memset(state.ptr2, '\1', state.MAX_LEN);
532 state.ptr2[state.MAX_LEN - 1] = '\0';
533
534 memset(state.ptr + state.MAX_LEN, '\1', state.MAX_LEN);
535 memcpy(state.ptr + state.MAX_LEN, state.ptr1, pos + 1);
536 state.ptr[2 * state.MAX_LEN - 1] = '\0';
537
538 ASSERT_TRUE(stpcpy(state.ptr2, state.ptr1) == state.ptr2 + strlen(state.ptr1));
539 ASSERT_FALSE((memcmp(state.ptr1, state.ptr, state.MAX_LEN)) != 0 ||
540 (memcmp(state.ptr2, state.ptr + state.MAX_LEN, state.MAX_LEN) != 0));
541 }
542 }
543
TEST(STRING_TEST,strcpy)544 TEST(STRING_TEST, strcpy) {
545 StringTestState<char> state(SMALL);
546 for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) {
547 size_t pos = random() % state.MAX_LEN;
548
549 memset(state.ptr1, '\2', pos);
550 state.ptr1[pos] = '\0';
551 state.ptr1[state.MAX_LEN - 1] = '\0';
552
553 memcpy(state.ptr, state.ptr1, state.MAX_LEN);
554
555 memset(state.ptr2, '\1', state.MAX_LEN);
556 state.ptr2[state.MAX_LEN - 1] = '\0';
557
558 memset(state.ptr + state.MAX_LEN, '\1', state.MAX_LEN);
559 memcpy(state.ptr + state.MAX_LEN, state.ptr1, pos + 1);
560 state.ptr[2 * state.MAX_LEN - 1] = '\0';
561
562 ASSERT_TRUE(strcpy(state.ptr2, state.ptr1) == state.ptr2);
563 ASSERT_FALSE((memcmp(state.ptr1, state.ptr, state.MAX_LEN)) != 0 ||
564 (memcmp(state.ptr2, state.ptr + state.MAX_LEN, state.MAX_LEN) != 0));
565 }
566 }
567
TEST(STRING_TEST,strlcat)568 TEST(STRING_TEST, strlcat) {
569 #if defined(STRLCAT_SUPPORTED)
570 StringTestState<char> state(SMALL);
571 for (size_t i = 0; i < state.n; i++) {
572 for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) {
573 memset(state.ptr2, '\2', state.MAX_LEN + state.len[i]);
574 state.ptr2[state.MAX_LEN - 1] = '\0';
575 memcpy(state.ptr, state.ptr2, state.MAX_LEN + state.len[i]);
576
577 size_t pos = random() % state.MAX_LEN;
578 memset(state.ptr1, '\3', pos);
579 state.ptr1[pos] = '\0';
580 if (pos < state.len[i]) {
581 memcpy(state.ptr + state.MAX_LEN - 1, state.ptr1, pos + 1);
582 } else {
583 memcpy(state.ptr + state.MAX_LEN - 1, state.ptr1, state.len[i]);
584 state.ptr[state.MAX_LEN + state.len[i] - 1] = '\0';
585 }
586
587 strlcat(state.ptr2, state.ptr1, state.MAX_LEN + state.len[i]);
588
589 ASSERT_TRUE(memcmp(state.ptr, state.ptr2, state.MAX_LEN + state.len[i]) == 0);
590 }
591 }
592 #else
593 GTEST_SKIP() << "strlcat not available";
594 #endif
595 }
596
TEST(STRING_TEST,strlcpy)597 TEST(STRING_TEST, strlcpy) {
598 #if defined(STRLCPY_SUPPORTED)
599 StringTestState<char> state(SMALL);
600 for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) {
601 int rand = 'O';
602 memset(state.ptr1, rand, state.MAX_LEN);
603
604 size_t pos = random() % state.MAX_LEN;
605 if (pos < state.MAX_LEN) {
606 state.ptr1[pos] = '\0';
607 }
608 memcpy(state.ptr, state.ptr1, state.MAX_LEN);
609
610 memset(state.ptr2, 'I', state.MAX_LEN);
611 memcpy(state.ptr + state.MAX_LEN, state.ptr2, state.MAX_LEN);
612
613 if (pos > state.MAX_LEN - 1) {
614 memcpy(state.ptr + state.MAX_LEN, state.ptr1, state.MAX_LEN);
615 state.ptr[2 * state.MAX_LEN - 1] = '\0';
616 } else {
617 memcpy(state.ptr + state.MAX_LEN, state.ptr1, pos + 1);
618 }
619
620 ASSERT_EQ(strlcpy(state.ptr2, state.ptr1, state.MAX_LEN), strlen(state.ptr1));
621 ASSERT_FALSE((memcmp(state.ptr1, state.ptr, state.MAX_LEN) != 0) ||
622 (memcmp(state.ptr2, state.ptr + state.MAX_LEN, state.MAX_LEN) != 0));
623 }
624 #else
625 GTEST_SKIP() << "strlcpy not available";
626 #endif
627 }
628
TEST(STRING_TEST,strncat)629 TEST(STRING_TEST, strncat) {
630 StringTestState<char> state(SMALL);
631 for (size_t i = 1; i < state.n; i++) {
632 for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) {
633 memset(state.ptr2, '\2', state.MAX_LEN);
634 state.ptr2[state.MAX_LEN - 1] = '\0';
635 memcpy(state.ptr, state.ptr2, 2 * state.MAX_LEN);
636
637 memset(state.ptr1, 'I', state.len[i]);
638 state.ptr1[random() % state.len[i]] = '\0';
639 state.ptr1[state.len[i] - 1] = '\0';
640
641 size_t pos = strlen(state.ptr1);
642
643 size_t actual = random() % state.len[i];
644 strncpy(state.ptr + state.MAX_LEN - 1, state.ptr1, std::min(actual, pos));
645 state.ptr[state.MAX_LEN + std::min(actual, pos) - 1] = '\0';
646
647 ASSERT_TRUE(strncat(state.ptr2, state.ptr1, actual) == state.ptr2);
648 ASSERT_EQ(memcmp(state.ptr, state.ptr2, 2 * state.MAX_LEN), 0);
649 }
650 }
651 }
652
TEST(STRING_TEST,strncmp)653 TEST(STRING_TEST, strncmp) {
654 StringTestState<char> state(SMALL);
655 for (size_t i = 1; i < state.n; i++) {
656 for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) {
657 memset(state.ptr1, 'v', state.MAX_LEN);
658 memset(state.ptr2, 'n', state.MAX_LEN);
659 state.ptr1[state.len[i] - 1] = '\0';
660 state.ptr2[state.len[i] - 1] = '\0';
661
662 size_t pos = 1 + (random() % (state.MAX_LEN - 1));
663 int actual;
664 int expected;
665 if (pos >= state.len[i] - 1) {
666 memcpy(state.ptr1, state.ptr2, state.len[i]);
667 expected = 0;
668 actual = strncmp(state.ptr1, state.ptr2, state.len[i]);
669 } else {
670 memcpy(state.ptr1, state.ptr2, pos);
671 if (state.ptr1[pos] > state.ptr2[pos]) {
672 expected = 1;
673 } else if (state.ptr1[pos] == state.ptr2[pos]) {
674 state.ptr1[pos + 1] = '\0';
675 state.ptr2[pos + 1] = '\0';
676 expected = 0;
677 } else {
678 expected = -1;
679 }
680 actual = strncmp(state.ptr1, state.ptr2, state.len[i]);
681 }
682
683 ASSERT_EQ(expected, signum(actual));
684 }
685 }
686 }
687
TEST(STRING_TEST,stpncpy)688 TEST(STRING_TEST, stpncpy) {
689 StringTestState<char> state(SMALL);
690 for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) {
691 memset(state.ptr1, 'J', state.MAX_LEN);
692 // Choose a random size for our src buffer.
693 size_t ptr1_len = random() % state.MAX_LEN;
694 state.ptr1[ptr1_len] = '\0';
695 // Copy ptr1 into ptr, used to verify that ptr1 does not get modified.
696 memcpy(state.ptr, state.ptr1, state.MAX_LEN);
697 // Init ptr2 to a set value.
698 memset(state.ptr2, '\1', state.MAX_LEN);
699
700 // Choose a random amount of data to copy.
701 size_t copy_len = random() % state.MAX_LEN;
702
703 // Set the second half of ptr to the expected pattern in ptr2.
704 memset(state.ptr + state.MAX_LEN, '\1', state.MAX_LEN);
705 memcpy(state.ptr + state.MAX_LEN, state.ptr1, copy_len);
706 size_t expected_end;
707 if (copy_len > ptr1_len) {
708 memset(state.ptr + state.MAX_LEN + ptr1_len, '\0', copy_len - ptr1_len);
709 expected_end = ptr1_len;
710 } else {
711 expected_end = copy_len;
712 }
713
714 ASSERT_EQ(state.ptr2 + expected_end, stpncpy(state.ptr2, state.ptr1, copy_len));
715
716 // Verify ptr1 was not modified.
717 ASSERT_EQ(0, memcmp(state.ptr1, state.ptr, state.MAX_LEN));
718 // Verify ptr2 contains the expected data.
719 ASSERT_EQ(0, memcmp(state.ptr2, state.ptr + state.MAX_LEN, state.MAX_LEN));
720 }
721 }
722
TEST(STRING_TEST,strncpy)723 TEST(STRING_TEST, strncpy) {
724 StringTestState<char> state(SMALL);
725 for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) {
726 // Choose a random value to fill the string, except \0 (string terminator),
727 // or \1 (guarantees it's different from anything in ptr2).
728 memset(state.ptr1, 'K', state.MAX_LEN);
729 // Choose a random size for our src buffer.
730 size_t ptr1_len = random() % state.MAX_LEN;
731 state.ptr1[ptr1_len] = '\0';
732 // Copy ptr1 into ptr, used to verify that ptr1 does not get modified.
733 memcpy(state.ptr, state.ptr1, state.MAX_LEN);
734 // Init ptr2 to a set value.
735 memset(state.ptr2, '\1', state.MAX_LEN);
736
737 // Choose a random amount of data to copy.
738 size_t copy_len = random() % state.MAX_LEN;
739
740 // Set the second half of ptr to the expected pattern in ptr2.
741 memset(state.ptr + state.MAX_LEN, '\1', state.MAX_LEN);
742 memcpy(state.ptr + state.MAX_LEN, state.ptr1, copy_len);
743 size_t expected_end;
744 if (copy_len > ptr1_len) {
745 memset(state.ptr + state.MAX_LEN + ptr1_len, '\0', copy_len - ptr1_len);
746 expected_end = ptr1_len;
747 } else {
748 expected_end = copy_len;
749 }
750
751 ASSERT_EQ(state.ptr2 + expected_end, stpncpy(state.ptr2, state.ptr1, copy_len));
752
753 // Verify ptr1 was not modified.
754 ASSERT_EQ(0, memcmp(state.ptr1, state.ptr, state.MAX_LEN));
755 // Verify ptr2 contains the expected data.
756 ASSERT_EQ(0, memcmp(state.ptr2, state.ptr + state.MAX_LEN, state.MAX_LEN));
757 }
758 }
759
TEST(STRING_TEST,strrchr)760 TEST(STRING_TEST, strrchr) {
761 int seek_char = 'M';
762 StringTestState<char> state(SMALL);
763 for (size_t i = 1; i < state.n; i++) {
764 for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) {
765 if (~seek_char > 0) {
766 memset(state.ptr1, ~seek_char, state.len[i]);
767 } else {
768 memset(state.ptr1, '\1', state.len[i]);
769 }
770 state.ptr1[state.len[i] - 1] = '\0';
771
772 size_t pos = random() % state.MAX_LEN;
773 char* expected;
774 if (pos >= state.len[i] - 1) {
775 if (seek_char == 0) {
776 expected = state.ptr1 + state.len[i] - 1;
777 } else {
778 expected = nullptr;
779 }
780 } else {
781 state.ptr1[pos] = seek_char;
782 expected = state.ptr1 + pos;
783 }
784
785 ASSERT_TRUE(strrchr(state.ptr1, seek_char) == expected);
786 }
787 }
788 }
789
TEST(STRING_TEST,memchr)790 TEST(STRING_TEST, memchr) {
791 int seek_char = 'N';
792 StringTestState<char> state(SMALL);
793 for (size_t i = 0; i < state.n; i++) {
794 for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) {
795 memset(state.ptr1, ~seek_char, state.len[i]);
796
797 size_t pos = random() % state.MAX_LEN;
798 char* expected;
799 if (pos >= state.len[i]) {
800 expected = nullptr;
801 } else {
802 state.ptr1[pos] = seek_char;
803 expected = state.ptr1 + pos;
804 }
805
806 ASSERT_TRUE(memchr(state.ptr1, seek_char, state.len[i]) == expected);
807 }
808 }
809 }
810
TEST(STRING_TEST,memchr_zero)811 TEST(STRING_TEST, memchr_zero) {
812 uint8_t* buffer;
813 ASSERT_EQ(0, posix_memalign(reinterpret_cast<void**>(&buffer), 64, 64));
814 memset(buffer, 10, 64);
815 ASSERT_TRUE(nullptr == memchr(buffer, 5, 0));
816 ASSERT_TRUE(nullptr == memchr(buffer, 10, 0));
817 }
818
TEST(STRING_TEST,memrchr)819 TEST(STRING_TEST, memrchr) {
820 int seek_char = 'P';
821 StringTestState<char> state(SMALL);
822 for (size_t i = 0; i < state.n; i++) {
823 for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) {
824 memset(state.ptr1, ~seek_char, state.len[i]);
825
826 size_t pos = random() % state.MAX_LEN;
827 char* expected;
828 if (pos >= state.len[i]) {
829 expected = nullptr;
830 } else {
831 state.ptr1[pos] = seek_char;
832 expected = state.ptr1 + pos;
833 }
834
835 ASSERT_TRUE(memrchr(state.ptr1, seek_char, state.len[i]) == expected);
836 }
837 }
838 }
839
TEST(STRING_TEST,memcmp)840 TEST(STRING_TEST, memcmp) {
841 StringTestState<char> state(SMALL);
842 for (size_t i = 0; i < state.n; i++) {
843 for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) {
844 int c1 = 'A';
845 int c2 = 'N';
846 memset(state.ptr1, c1, state.MAX_LEN);
847 memset(state.ptr2, c1, state.MAX_LEN);
848
849 int pos = (state.len[i] == 0) ? 0 : (random() % state.len[i]);
850 state.ptr2[pos] = c2;
851
852 int expected = (static_cast<int>(c1) - static_cast<int>(c2));
853 int actual = memcmp(state.ptr1, state.ptr2, state.MAX_LEN);
854
855 ASSERT_EQ(signum(expected), signum(actual));
856 }
857 }
858 }
859
TEST(STRING_TEST,wmemcmp)860 TEST(STRING_TEST, wmemcmp) {
861 StringTestState<wchar_t> state(SMALL);
862
863 for (size_t i = 0; i < state.n; i++) {
864 for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) {
865 long long mask = ((long long) 1 << 8 * sizeof(wchar_t)) - 1;
866 int c1 = rand() & mask;
867 int c2 = rand() & mask;
868 wmemset(state.ptr1, c1, state.MAX_LEN);
869 wmemset(state.ptr2, c1, state.MAX_LEN);
870
871 int pos = (state.len[i] == 0) ? 0 : (random() % state.len[i]);
872 state.ptr2[pos] = c2;
873
874 int expected = (static_cast<int>(c1) - static_cast<int>(c2));
875 int actual = wmemcmp(state.ptr1, state.ptr2, (size_t) state.MAX_LEN);
876
877 ASSERT_EQ(signum(expected), signum(actual));
878 }
879 }
880 }
881
TEST(STRING_TEST,memcpy)882 TEST(STRING_TEST, memcpy) {
883 StringTestState<char> state(LARGE);
884 int rand = 4;
885 for (size_t i = 0; i < state.n - 1; i++) {
886 for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) {
887 size_t pos = random() % (state.MAX_LEN - state.len[i]);
888
889 memset(state.ptr1, rand, state.len[i]);
890 memset(state.ptr1 + state.len[i], ~rand, state.MAX_LEN - state.len[i]);
891
892 memset(state.ptr2, rand, state.len[i]);
893 memset(state.ptr2 + state.len[i], ~rand, state.MAX_LEN - state.len[i]);
894 memset(state.ptr2 + pos, '\0', state.len[i]);
895
896 ASSERT_FALSE(memcpy(state.ptr2 + pos, state.ptr1 + pos, state.len[i]) != state.ptr2 + pos);
897 ASSERT_EQ(0, memcmp(state.ptr1, state.ptr2, state.MAX_LEN));
898 }
899 }
900 }
901
TEST(STRING_TEST,memset)902 TEST(STRING_TEST, memset) {
903 StringTestState<char> state(LARGE);
904 char ch = 'P';
905 for (size_t i = 0; i < state.n - 1; i++) {
906 for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) {
907 memset(state.ptr1, ~ch, state.MAX_LEN);
908 memcpy(state.ptr2, state.ptr1, state.MAX_LEN);
909
910 size_t pos = random () % (state.MAX_LEN - state.len[i]);
911 for (size_t k = pos; k < pos + state.len[i]; k++) {
912 state.ptr1[k] = ch;
913 }
914
915 ASSERT_TRUE(memset(state.ptr2 + pos, ch, state.len[i]) == state.ptr2 + pos);
916
917 ASSERT_EQ(0, memcmp(state.ptr1, state.ptr2, state.MAX_LEN));
918 }
919 }
920 }
921
TEST(STRING_TEST,memmove)922 TEST(STRING_TEST, memmove) {
923 StringTestState<char> state(LARGE);
924 for (size_t i = 0; i < state.n - 1; i++) {
925 for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) {
926 memset(state.ptr1, 'Q', 2 * state.MAX_LEN);
927
928 size_t pos = random() % (state.MAX_LEN - state.len[i]);
929
930 memset(state.ptr1, 'R', state.len[i]);
931 memcpy(state.ptr2, state.ptr1, 2 * state.MAX_LEN);
932 memcpy(state.ptr, state.ptr1, state.len[i]);
933 memcpy(state.ptr1 + pos, state.ptr, state.len[i]);
934
935 ASSERT_TRUE(memmove(state.ptr2 + pos, state.ptr2, state.len[i]) == state.ptr2 + pos);
936 ASSERT_EQ(0, memcmp(state.ptr2, state.ptr1, 2 * state.MAX_LEN));
937 }
938 }
939 }
940
TEST(STRING_TEST,memmove_cache_size)941 TEST(STRING_TEST, memmove_cache_size) {
942 size_t len = 600000;
943 int max_alignment = 31;
944 int alignments[] = {0, 5, 11, 29, 30};
945 char* ptr = reinterpret_cast<char*>(malloc(sizeof(char) * len));
946 char* ptr1 = reinterpret_cast<char*>(malloc(2 * sizeof(char) * len));
947 char* glob_ptr2 = reinterpret_cast<char*>(malloc(2 * sizeof(char) * len + max_alignment));
948 size_t pos = 64;
949
950 ASSERT_TRUE(ptr != nullptr);
951 ASSERT_TRUE(ptr1 != nullptr);
952 ASSERT_TRUE(glob_ptr2 != nullptr);
953
954 for (int i = 0; i < 5; i++) {
955 char* ptr2 = glob_ptr2 + alignments[i];
956 memset(ptr1, 'S', 2 * len);
957 memset(ptr1, 'T', len);
958 memcpy(ptr2, ptr1, 2 * len);
959 memcpy(ptr, ptr1, len);
960 memcpy(ptr1 + pos, ptr, len);
961
962 ASSERT_TRUE(memmove(ptr2 + pos, ptr, len) == ptr2 + pos);
963 ASSERT_EQ(0, memcmp(ptr2, ptr1, 2 * len));
964 }
965 free(ptr);
966 free(ptr1);
967 free(glob_ptr2);
968 }
969
verify_memmove(char * src_copy,char * dst,char * src,size_t size)970 static void verify_memmove(char* src_copy, char* dst, char* src, size_t size) {
971 memset(dst, 0, size);
972 memcpy(src, src_copy, size);
973 ASSERT_EQ(dst, memmove(dst, src, size));
974 ASSERT_EQ(0, memcmp(dst, src_copy, size));
975 }
976
977 #define MEMMOVE_DATA_SIZE (1024*1024*3)
978
TEST(STRING_TEST,memmove_check)979 TEST(STRING_TEST, memmove_check) {
980 char* buffer = reinterpret_cast<char*>(malloc(MEMMOVE_DATA_SIZE));
981 ASSERT_TRUE(buffer != nullptr);
982
983 char* src_data = reinterpret_cast<char*>(malloc(MEMMOVE_DATA_SIZE));
984 ASSERT_TRUE(src_data != nullptr);
985 // Initialize to a known pattern to copy into src for each test and
986 // to compare dst against.
987 for (size_t i = 0; i < MEMMOVE_DATA_SIZE; i++) {
988 src_data[i] = (i + 1) % 255;
989 }
990
991 // Check all different dst offsets between 0 and 127 inclusive.
992 char* src = buffer;
993 for (size_t i = 0; i < 127; i++) {
994 char* dst = buffer + 256 + i;
995 // Small copy.
996 verify_memmove(src_data, dst, src, 1024);
997
998 // Medium copy.
999 verify_memmove(src_data, dst, src, 64 * 1024);
1000
1001 // Medium copy.
1002 verify_memmove(src_data, dst, src, 1024 * 1024 + 128 * 1024);
1003 }
1004
1005 // Check all leftover size offsets between 1 and 127 inclusive.
1006 char* dst = buffer + 256;
1007 src = buffer;
1008 for (size_t size = 1; size < 127; size++) {
1009 // Small copy.
1010 verify_memmove(src_data, dst, src, 1024);
1011
1012 // Medium copy.
1013 verify_memmove(src_data, dst, src, 64 * 1024);
1014
1015 // Large copy.
1016 verify_memmove(src_data, dst, src, 1024 * 1024 + 128 * 1024);
1017 }
1018 }
1019
TEST(STRING_TEST,bcopy)1020 TEST(STRING_TEST, bcopy) {
1021 StringTestState<char> state(LARGE);
1022 for (size_t i = 0; i < state.n; i++) {
1023 for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) {
1024 memset(state.ptr1, '4', state.MAX_LEN);
1025 memset(state.ptr1 + state.MAX_LEN, 'a', state.MAX_LEN);
1026 memcpy(state.ptr2, state.ptr1, 2 * state.MAX_LEN);
1027
1028 size_t start = random() % (2 * state.MAX_LEN - state.len[i]);
1029 memcpy(state.ptr2 + start, state.ptr1, state.len[i]);
1030
1031 bcopy(state.ptr1, state.ptr1 + start, state.len[i]);
1032 ASSERT_EQ(0, memcmp(state.ptr1, state.ptr2, 2 * state.MAX_LEN));
1033 }
1034 }
1035 }
1036
TEST(STRING_TEST,bzero)1037 TEST(STRING_TEST, bzero) {
1038 StringTestState<char> state(LARGE);
1039 for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) {
1040 memset(state.ptr1, 'R', state.MAX_LEN);
1041
1042 size_t start = random() % state.MAX_LEN;
1043 size_t end = start + random() % (state.MAX_LEN - start);
1044
1045 memcpy(state.ptr2, state.ptr1, start);
1046 memset(state.ptr2 + start, '\0', end - start);
1047 memcpy(state.ptr2 + end, state.ptr1 + end, state.MAX_LEN - end);
1048
1049 bzero(state.ptr1 + start, end - start);
1050
1051 ASSERT_EQ(0, memcmp(state.ptr1, state.ptr2, state.MAX_LEN));
1052 }
1053 }
1054
DoMemcpyTest(uint8_t * src,uint8_t * dst,size_t len)1055 static void DoMemcpyTest(uint8_t* src, uint8_t* dst, size_t len) {
1056 memset(src, (len % 255) + 1, len);
1057 memset(dst, 0, len);
1058
1059 ASSERT_EQ(dst, memcpy(dst, src, len));
1060 ASSERT_TRUE(memcmp(src, dst, len) == 0);
1061 }
1062
TEST(STRING_TEST,memcpy_align)1063 TEST(STRING_TEST, memcpy_align) {
1064 RunSrcDstBufferAlignTest(LARGE, DoMemcpyTest);
1065 }
1066
TEST(STRING_TEST,memcpy_overread)1067 TEST(STRING_TEST, memcpy_overread) {
1068 RunSrcDstBufferOverreadTest(DoMemcpyTest);
1069 }
1070
DoMemmoveTest(uint8_t * src,uint8_t * dst,size_t len)1071 static void DoMemmoveTest(uint8_t* src, uint8_t* dst, size_t len) {
1072 memset(src, (len % 255) + 1, len);
1073 memset(dst, 0, len);
1074
1075 ASSERT_EQ(dst, memmove(dst, src, len));
1076 ASSERT_TRUE(memcmp(src, dst, len) == 0);
1077 }
1078
TEST(STRING_TEST,memmove_align)1079 TEST(STRING_TEST, memmove_align) {
1080 RunSrcDstBufferAlignTest(LARGE, DoMemmoveTest);
1081 }
1082
TEST(STRING_TEST,memmove_overread)1083 TEST(STRING_TEST, memmove_overread) {
1084 RunSrcDstBufferOverreadTest(DoMemmoveTest);
1085 }
1086
DoMemsetTest(uint8_t * buf,size_t len)1087 static void DoMemsetTest(uint8_t* buf, size_t len) {
1088 for (size_t i = 0; i < len; i++) {
1089 buf[i] = 0;
1090 }
1091 int value = (len % 255) + 1;
1092 ASSERT_EQ(buf, memset(buf, value, len));
1093 for (size_t i = 0; i < len; i++) {
1094 ASSERT_EQ(value, buf[i]);
1095 }
1096 }
1097
TEST(STRING_TEST,memset_align)1098 TEST(STRING_TEST, memset_align) {
1099 RunSingleBufferAlignTest(LARGE, DoMemsetTest);
1100 }
1101
DoStrlenTest(uint8_t * buf,size_t len)1102 static void DoStrlenTest(uint8_t* buf, size_t len) {
1103 if (len >= 1) {
1104 memset(buf, (32 + (len % 96)), len - 1);
1105 buf[len-1] = '\0';
1106 ASSERT_EQ(len-1, strlen(reinterpret_cast<char*>(buf)));
1107 }
1108 }
1109
TEST(STRING_TEST,strlen_align)1110 TEST(STRING_TEST, strlen_align) {
1111 RunSingleBufferAlignTest(LARGE, DoStrlenTest);
1112 }
1113
TEST(STRING_TEST,strlen_overread)1114 TEST(STRING_TEST, strlen_overread) {
1115 RunSingleBufferOverreadTest(DoStrlenTest);
1116 }
1117
DoStrcpyTest(uint8_t * src,uint8_t * dst,size_t len)1118 static void DoStrcpyTest(uint8_t* src, uint8_t* dst, size_t len) {
1119 if (len >= 1) {
1120 memset(src, (32 + (len % 96)), len - 1);
1121 src[len-1] = '\0';
1122 memset(dst, 0, len);
1123 ASSERT_EQ(dst, reinterpret_cast<uint8_t*>(strcpy(reinterpret_cast<char*>(dst),
1124 reinterpret_cast<char*>(src))));
1125 ASSERT_TRUE(memcmp(src, dst, len) == 0);
1126 }
1127 }
1128
TEST(STRING_TEST,strcpy_align)1129 TEST(STRING_TEST, strcpy_align) {
1130 RunSrcDstBufferAlignTest(LARGE, DoStrcpyTest);
1131 }
1132
TEST(STRING_TEST,strcpy_overread)1133 TEST(STRING_TEST, strcpy_overread) {
1134 RunSrcDstBufferOverreadTest(DoStrcpyTest);
1135 }
1136
1137 #if defined(STRLCPY_SUPPORTED)
DoStrlcpyTest(uint8_t * src,uint8_t * dst,size_t len)1138 static void DoStrlcpyTest(uint8_t* src, uint8_t* dst, size_t len) {
1139 if (len >= 1) {
1140 memset(src, (32 + (len % 96)), len - 1);
1141 src[len-1] = '\0';
1142 memset(dst, 0, len);
1143 ASSERT_EQ(len-1, strlcpy(reinterpret_cast<char*>(dst),
1144 reinterpret_cast<char*>(src), len));
1145 ASSERT_TRUE(memcmp(src, dst, len) == 0);
1146 }
1147 }
1148 #endif
1149
TEST(STRING_TEST,strlcpy_align)1150 TEST(STRING_TEST, strlcpy_align) {
1151 #if defined(STRLCPY_SUPPORTED)
1152 RunSrcDstBufferAlignTest(LARGE, DoStrlcpyTest);
1153 #else
1154 GTEST_SKIP() << "strlcpy not available";
1155 #endif
1156 }
1157
TEST(STRING_TEST,strlcpy_overread)1158 TEST(STRING_TEST, strlcpy_overread) {
1159 #if defined(STRLCPY_SUPPORTED)
1160 RunSrcDstBufferOverreadTest(DoStrlcpyTest);
1161 #else
1162 GTEST_SKIP() << "strlcpy not available";
1163 #endif
1164 }
1165
1166
DoStpcpyTest(uint8_t * src,uint8_t * dst,size_t len)1167 static void DoStpcpyTest(uint8_t* src, uint8_t* dst, size_t len) {
1168 if (len >= 1) {
1169 memset(src, (32 + (len % 96)), len - 1);
1170 src[len-1] = '\0';
1171 memset(dst, 0, len);
1172 ASSERT_EQ(dst+len-1, reinterpret_cast<uint8_t*>(stpcpy(reinterpret_cast<char*>(dst),
1173 reinterpret_cast<char*>(src))));
1174 ASSERT_TRUE(memcmp(src, dst, len) == 0);
1175 }
1176 }
1177
TEST(STRING_TEST,stpcpy_align)1178 TEST(STRING_TEST, stpcpy_align) {
1179 RunSrcDstBufferAlignTest(LARGE, DoStpcpyTest);
1180 }
1181
TEST(STRING_TEST,stpcpy_overread)1182 TEST(STRING_TEST, stpcpy_overread) {
1183 RunSrcDstBufferOverreadTest(DoStpcpyTest);
1184 }
1185
1186 // Use our own incrementer to cut down on the total number of calls.
LargeSetIncrement(size_t len)1187 static size_t LargeSetIncrement(size_t len) {
1188 if (len >= 4096) {
1189 return 4096;
1190 } else if (len >= 1024) {
1191 return 1024;
1192 } else if (len >= 256) {
1193 return 256;
1194 }
1195 return 1;
1196 }
1197
1198 #define STRCAT_DST_LEN 64
1199
DoStrcatTest(uint8_t * src,uint8_t * dst,size_t len)1200 static void DoStrcatTest(uint8_t* src, uint8_t* dst, size_t len) {
1201 if (len >= 1) {
1202 int value = 32 + (len % 96);
1203 memset(src, value, len - 1);
1204 src[len-1] = '\0';
1205
1206 if (len >= STRCAT_DST_LEN) {
1207 // Create a small buffer for doing quick compares in each loop.
1208 uint8_t cmp_buf[STRCAT_DST_LEN];
1209 // Make sure dst string contains a different value then the src string.
1210 int value2 = 32 + (value + 2) % 96;
1211 memset(cmp_buf, value2, sizeof(cmp_buf));
1212
1213 for (size_t i = 1; i <= STRCAT_DST_LEN;) {
1214 memset(dst, value2, i-1);
1215 memset(dst+i-1, 0, len-i);
1216 src[len-i] = '\0';
1217 ASSERT_EQ(dst, reinterpret_cast<uint8_t*>(strcat(reinterpret_cast<char*>(dst),
1218 reinterpret_cast<char*>(src))));
1219 ASSERT_TRUE(memcmp(dst, cmp_buf, i-1) == 0);
1220 ASSERT_TRUE(memcmp(src, dst+i-1, len-i+1) == 0);
1221 // This is an expensive loop, so don't loop through every value,
1222 // get to a certain size and then start doubling.
1223 if (i < 16) {
1224 i++;
1225 } else {
1226 i <<= 1;
1227 }
1228 }
1229 } else {
1230 dst[0] = '\0';
1231 ASSERT_EQ(dst, reinterpret_cast<uint8_t*>(strcat(reinterpret_cast<char*>(dst),
1232 reinterpret_cast<char*>(src))));
1233 ASSERT_TRUE(memcmp(src, dst, len) == 0);
1234 }
1235 }
1236 }
1237
TEST(STRING_TEST,strcat_align)1238 TEST(STRING_TEST, strcat_align) {
1239 RunSrcDstBufferAlignTest(MEDIUM, DoStrcatTest, LargeSetIncrement);
1240 }
1241
TEST(STRING_TEST,strcat_overread)1242 TEST(STRING_TEST, strcat_overread) {
1243 RunSrcDstBufferOverreadTest(DoStrcatTest);
1244 }
1245
1246 #if defined(STRLCAT_SUPPORTED)
DoStrlcatTest(uint8_t * src,uint8_t * dst,size_t len)1247 static void DoStrlcatTest(uint8_t* src, uint8_t* dst, size_t len) {
1248 if (len >= 1) {
1249 int value = 32 + (len % 96);
1250 memset(src, value, len - 1);
1251 src[len-1] = '\0';
1252
1253 if (len >= STRCAT_DST_LEN) {
1254 // Create a small buffer for doing quick compares in each loop.
1255 uint8_t cmp_buf[STRCAT_DST_LEN];
1256 // Make sure dst string contains a different value then the src string.
1257 int value2 = 32 + (value + 2) % 96;
1258 memset(cmp_buf, value2, sizeof(cmp_buf));
1259
1260 for (size_t i = 1; i <= STRCAT_DST_LEN;) {
1261 memset(dst, value2, i-1);
1262 memset(dst+i-1, 0, len-i);
1263 src[len-i] = '\0';
1264 ASSERT_EQ(len-1, strlcat(reinterpret_cast<char*>(dst),
1265 reinterpret_cast<char*>(src), len));
1266 ASSERT_TRUE(memcmp(dst, cmp_buf, i-1) == 0);
1267 ASSERT_TRUE(memcmp(src, dst+i-1, len-i+1) == 0);
1268 // This is an expensive loop, so don't loop through every value,
1269 // get to a certain size and then start doubling.
1270 if (i < 16) {
1271 i++;
1272 } else {
1273 i <<= 1;
1274 }
1275 }
1276 } else {
1277 dst[0] = '\0';
1278 ASSERT_EQ(len-1, strlcat(reinterpret_cast<char*>(dst),
1279 reinterpret_cast<char*>(src), len));
1280 ASSERT_TRUE(memcmp(src, dst, len) == 0);
1281 }
1282 }
1283 }
1284 #endif
1285
TEST(STRING_TEST,strlcat_align)1286 TEST(STRING_TEST, strlcat_align) {
1287 #if defined(STRLCAT_SUPPORTED)
1288 RunSrcDstBufferAlignTest(MEDIUM, DoStrlcatTest, LargeSetIncrement);
1289 #else
1290 GTEST_SKIP() << "strlcat not available";
1291 #endif
1292 }
1293
TEST(STRING_TEST,strlcat_overread)1294 TEST(STRING_TEST, strlcat_overread) {
1295 #if defined(STRLCAT_SUPPORTED)
1296 RunSrcDstBufferOverreadTest(DoStrlcatTest);
1297 #else
1298 GTEST_SKIP() << "strlcat not available";
1299 #endif
1300 }
1301
DoStrcmpTest(uint8_t * buf1,uint8_t * buf2,size_t len)1302 static void DoStrcmpTest(uint8_t* buf1, uint8_t* buf2, size_t len) {
1303 if (len >= 1) {
1304 memset(buf1, (32 + (len % 96)), len - 1);
1305 buf1[len-1] = '\0';
1306 memset(buf2, (32 + (len % 96)), len - 1);
1307 buf2[len-1] = '\0';
1308 ASSERT_EQ(0, strcmp(reinterpret_cast<char*>(buf1),
1309 reinterpret_cast<char*>(buf2)));
1310 }
1311 }
1312
DoStrcmpFailTest(uint8_t * buf1,uint8_t * buf2,size_t len1,size_t len2)1313 static void DoStrcmpFailTest(uint8_t* buf1, uint8_t* buf2, size_t len1, size_t len2) {
1314 // Do string length differences.
1315 int c = (32 + (len1 % 96));
1316 memset(buf1, c, len1 - 1);
1317 buf1[len1-1] = '\0';
1318 memset(buf2, c, len2 - 1);
1319 buf2[len2-1] = '\0';
1320 ASSERT_NE(0, strcmp(reinterpret_cast<char*>(buf1),
1321 reinterpret_cast<char*>(buf2)));
1322
1323 // Do single character differences.
1324 size_t len;
1325 if (len1 > len2) {
1326 len = len2;
1327 } else {
1328 len = len1;
1329 }
1330 // Need at least a two character buffer to do this test.
1331 if (len > 1) {
1332 buf1[len-1] = '\0';
1333 buf2[len-1] = '\0';
1334 int diff_c = (c + 1) % 96;
1335
1336 buf1[len-2] = diff_c;
1337 ASSERT_NE(0, strcmp(reinterpret_cast<char*>(buf1),
1338 reinterpret_cast<char*>(buf2)));
1339
1340 buf1[len-2] = c;
1341 buf2[len-2] = diff_c;
1342 ASSERT_NE(0, strcmp(reinterpret_cast<char*>(buf1),
1343 reinterpret_cast<char*>(buf2)));
1344 }
1345 }
1346
TEST(STRING_TEST,strcmp_align)1347 TEST(STRING_TEST, strcmp_align) {
1348 RunCmpBufferAlignTest(MEDIUM, DoStrcmpTest, DoStrcmpFailTest, LargeSetIncrement);
1349 }
1350
TEST(STRING_TEST,strcmp_overread)1351 TEST(STRING_TEST, strcmp_overread) {
1352 RunCmpBufferOverreadTest(DoStrcmpTest, DoStrcmpFailTest);
1353 }
1354
DoMemcmpTest(uint8_t * buf1,uint8_t * buf2,size_t len)1355 static void DoMemcmpTest(uint8_t* buf1, uint8_t* buf2, size_t len) {
1356 memset(buf1, len+1, len);
1357 memset(buf2, len+1, len);
1358 ASSERT_EQ(0, memcmp(buf1, buf2, len));
1359 }
1360
DoMemcmpFailTest(uint8_t * buf1,uint8_t * buf2,size_t len1,size_t len2)1361 static void DoMemcmpFailTest(uint8_t* buf1, uint8_t* buf2, size_t len1, size_t len2) {
1362 size_t len;
1363 if (len1 > len2) {
1364 len = len2;
1365 } else {
1366 len = len1;
1367 }
1368
1369 memset(buf1, len2+1, len);
1370 buf1[len-1] = len2;
1371 memset(buf2, len2+1, len);
1372 ASSERT_NE(0, memcmp(buf1, buf2, len));
1373
1374 buf1[len-1] = len2+1;
1375 buf2[len-1] = len2;
1376 ASSERT_NE(0, memcmp(buf1, buf2, len));
1377 }
1378
TEST(STRING_TEST,memcmp_align)1379 TEST(STRING_TEST, memcmp_align) {
1380 RunCmpBufferAlignTest(MEDIUM, DoMemcmpTest, DoMemcmpFailTest, LargeSetIncrement);
1381 }
1382
TEST(STRING_TEST,memcmp_overread)1383 TEST(STRING_TEST, memcmp_overread) {
1384 RunCmpBufferOverreadTest(DoMemcmpTest, DoMemcmpFailTest);
1385 }
1386
DoMemchrTest(uint8_t * buf,size_t len)1387 static void DoMemchrTest(uint8_t* buf, size_t len) {
1388 if (len >= 1) {
1389 int value = len % 128;
1390 int search_value = (len % 128) + 1;
1391 memset(buf, value, len);
1392 // The buffer does not contain the search value.
1393 ASSERT_EQ(nullptr, memchr(buf, search_value, len));
1394 if (len >= 2) {
1395 buf[0] = search_value;
1396 // The search value is the first element in the buffer.
1397 ASSERT_EQ(&buf[0], memchr(buf, search_value, len));
1398
1399 buf[0] = value;
1400 buf[len - 1] = search_value;
1401 // The search value is the last element in the buffer.
1402 ASSERT_EQ(&buf[len - 1], memchr(buf, search_value, len));
1403 }
1404 }
1405 }
1406
TEST(STRING_TEST,memchr_align)1407 TEST(STRING_TEST, memchr_align) {
1408 RunSingleBufferAlignTest(MEDIUM, DoMemchrTest);
1409 }
1410
TEST(STRING_TEST,memchr_overread)1411 TEST(STRING_TEST, memchr_overread) {
1412 RunSingleBufferOverreadTest(DoMemchrTest);
1413 }
1414
DoStrchrTest(uint8_t * buf,size_t len)1415 static void DoStrchrTest(uint8_t* buf, size_t len) {
1416 if (len >= 1) {
1417 char value = 32 + (len % 96);
1418 char search_value = 33 + (len % 96);
1419 memset(buf, value, len - 1);
1420 buf[len - 1] = '\0';
1421 // The buffer does not contain the search value.
1422 ASSERT_EQ(nullptr, strchr(reinterpret_cast<char*>(buf), search_value));
1423 // Search for the special '\0' character.
1424 ASSERT_EQ(reinterpret_cast<char*>(&buf[len - 1]), strchr(reinterpret_cast<char*>(buf), '\0'));
1425 if (len >= 2) {
1426 buf[0] = search_value;
1427 // The search value is the first element in the buffer.
1428 ASSERT_EQ(reinterpret_cast<char*>(&buf[0]), strchr(reinterpret_cast<char*>(buf),
1429 search_value));
1430
1431 buf[0] = value;
1432 buf[len - 2] = search_value;
1433 // The search value is the second to last element in the buffer.
1434 // The last element is the '\0' character.
1435 ASSERT_EQ(reinterpret_cast<char*>(&buf[len - 2]), strchr(reinterpret_cast<char*>(buf),
1436 search_value));
1437 }
1438 }
1439 }
1440
TEST(STRING_TEST,strchr_align)1441 TEST(STRING_TEST, strchr_align) {
1442 RunSingleBufferAlignTest(MEDIUM, DoStrchrTest);
1443 }
1444
TEST(STRING_TEST,strchr_overread)1445 TEST(STRING_TEST, strchr_overread) {
1446 RunSingleBufferOverreadTest(DoStrchrTest);
1447 }
1448
DoStrrchrTest(uint8_t * buf,size_t len)1449 static void DoStrrchrTest(uint8_t* buf, size_t len) {
1450 if (len >= 1) {
1451 char value = 32 + (len % 96);
1452 char search_value = 33 + (len % 96);
1453 memset(buf, value, len - 1);
1454 buf[len - 1] = '\0';
1455 // The buffer does not contain the search value.
1456 ASSERT_EQ(nullptr, strrchr(reinterpret_cast<char*>(buf), search_value));
1457 // Search for the special '\0' character.
1458 ASSERT_EQ(reinterpret_cast<char*>(&buf[len - 1]), strrchr(reinterpret_cast<char*>(buf), '\0'));
1459 if (len >= 2) {
1460 buf[0] = search_value;
1461 // The search value is the first element in the buffer.
1462 ASSERT_EQ(reinterpret_cast<char*>(&buf[0]), strrchr(reinterpret_cast<char*>(buf),
1463 search_value));
1464
1465 buf[0] = value;
1466 buf[len - 2] = search_value;
1467 // The search value is the second to last element in the buffer.
1468 // The last element is the '\0' character.
1469 ASSERT_EQ(reinterpret_cast<char*>(&buf[len - 2]), strrchr(reinterpret_cast<char*>(buf),
1470 search_value));
1471 }
1472 }
1473 }
1474
TEST(STRING_TEST,strrchr_align)1475 TEST(STRING_TEST, strrchr_align) {
1476 RunSingleBufferAlignTest(MEDIUM, DoStrrchrTest);
1477 }
1478
TEST(STRING_TEST,strrchr_overread)1479 TEST(STRING_TEST, strrchr_overread) {
1480 RunSingleBufferOverreadTest(DoStrrchrTest);
1481 }
1482
1483 #if !defined(ANDROID_HOST_MUSL)
TestBasename(const char * in,const char * expected_out)1484 static void TestBasename(const char* in, const char* expected_out) {
1485 errno = 0;
1486 const char* out = basename(in);
1487 ASSERT_STREQ(expected_out, out) << in;
1488 ASSERT_ERRNO(0) << in;
1489 }
1490 #endif
1491
TEST(STRING_TEST,__gnu_basename)1492 TEST(STRING_TEST, __gnu_basename) {
1493 #if !defined(ANDROID_HOST_MUSL)
1494 TestBasename("", "");
1495 TestBasename("/usr/lib", "lib");
1496 TestBasename("/usr/", "");
1497 TestBasename("usr", "usr");
1498 TestBasename("/", "");
1499 TestBasename(".", ".");
1500 TestBasename("..", "..");
1501 TestBasename("///", "");
1502 TestBasename("//usr//lib//", "");
1503 #else
1504 GTEST_SKIP() << "musl doesn't have GNU basename";
1505 #endif
1506 }
1507
TEST(STRING_TEST,strnlen_147048)1508 TEST(STRING_TEST, strnlen_147048) {
1509 // https://code.google.com/p/android/issues/detail?id=147048
1510 char stack_src[64] = {0};
1511 EXPECT_EQ(0U, strnlen(stack_src, 1024*1024*1024));
1512 char* heap_src = new char[1];
1513 *heap_src = '\0';
1514 EXPECT_EQ(0U, strnlen(heap_src, 1024*1024*1024));
1515 delete[] heap_src;
1516 }
1517
TEST(STRING_TEST,strnlen_74741)1518 TEST(STRING_TEST, strnlen_74741) {
1519 ASSERT_EQ(4U, strnlen("test", SIZE_MAX));
1520 }
1521
TEST(STRING_TEST,mempcpy)1522 TEST(STRING_TEST, mempcpy) {
1523 char dst[6];
1524 ASSERT_EQ(&dst[4], reinterpret_cast<char*>(mempcpy(dst, "hello", 4)));
1525 }
1526
1527 // clang depends on the fact that a memcpy where src and dst is the same
1528 // still operates correctly. This test verifies that this assumption
1529 // holds true.
1530 // See https://llvm.org/bugs/show_bug.cgi?id=11763 for more information.
1531 static std::vector<uint8_t> g_memcpy_same_buffer;
1532
DoMemcpySameTest(uint8_t * buffer,size_t len)1533 static void DoMemcpySameTest(uint8_t* buffer, size_t len) {
1534 memcpy(buffer, g_memcpy_same_buffer.data(), len);
1535 ASSERT_EQ(buffer, memcpy(buffer, buffer, len));
1536 ASSERT_TRUE(memcmp(buffer, g_memcpy_same_buffer.data(), len) == 0);
1537 }
1538
TEST(STRING_TEST,memcpy_src_dst_same)1539 TEST(STRING_TEST, memcpy_src_dst_same) {
1540 g_memcpy_same_buffer.resize(MEDIUM);
1541 for (size_t i = 0; i < MEDIUM; i++) {
1542 g_memcpy_same_buffer[i] = i;
1543 }
1544 RunSingleBufferAlignTest(MEDIUM, DoMemcpySameTest);
1545 }
1546
TEST(STRING_TEST,memmem_strstr_empty_needle)1547 TEST(STRING_TEST, memmem_strstr_empty_needle) {
1548 const char* some_haystack = "haystack";
1549 const char* empty_haystack = "";
1550
1551 ASSERT_EQ(some_haystack, memmem(some_haystack, 8, "", 0));
1552 ASSERT_EQ(empty_haystack, memmem(empty_haystack, 0, "", 0));
1553
1554 ASSERT_EQ(some_haystack, strstr(some_haystack, ""));
1555 ASSERT_EQ(empty_haystack, strstr(empty_haystack, ""));
1556 }
1557
TEST(STRING_TEST,memmem_smoke)1558 TEST(STRING_TEST, memmem_smoke) {
1559 const char haystack[] = "big\0daddy/giant\0haystacks!";
1560
1561 // The current memmem() implementation has special cases for needles of
1562 // lengths 0, 1, 2, 3, and 4, plus a long needle case. We test matches at the
1563 // beginning, middle, and end of the haystack.
1564
1565 ASSERT_EQ(haystack + 0, memmem(haystack, sizeof(haystack), "", 0));
1566
1567 ASSERT_EQ(haystack + 0, memmem(haystack, sizeof(haystack), "b", 1));
1568 ASSERT_EQ(haystack + 0, memmem(haystack, sizeof(haystack), "bi", 2));
1569 ASSERT_EQ(haystack + 0, memmem(haystack, sizeof(haystack), "big", 3));
1570 ASSERT_EQ(haystack + 0, memmem(haystack, sizeof(haystack), "big\0", 4));
1571 ASSERT_EQ(haystack + 0, memmem(haystack, sizeof(haystack), "big\0d", 5));
1572
1573 ASSERT_EQ(haystack + 2, memmem(haystack, sizeof(haystack), "g", 1));
1574 ASSERT_EQ(haystack + 10, memmem(haystack, sizeof(haystack), "gi", 2));
1575 ASSERT_EQ(haystack + 10, memmem(haystack, sizeof(haystack), "gia", 3));
1576 ASSERT_EQ(haystack + 10, memmem(haystack, sizeof(haystack), "gian", 4));
1577 ASSERT_EQ(haystack + 10, memmem(haystack, sizeof(haystack), "giant", 5));
1578
1579 ASSERT_EQ(haystack + 25, memmem(haystack, sizeof(haystack), "!", 1));
1580 ASSERT_EQ(haystack + 24, memmem(haystack, sizeof(haystack), "s!", 2));
1581 ASSERT_EQ(haystack + 23, memmem(haystack, sizeof(haystack), "ks!", 3));
1582 ASSERT_EQ(haystack + 22, memmem(haystack, sizeof(haystack), "cks!", 4));
1583 ASSERT_EQ(haystack + 21, memmem(haystack, sizeof(haystack), "acks!", 5));
1584 }
1585
TEST(STRING_TEST,strstr_smoke)1586 TEST(STRING_TEST, strstr_smoke) {
1587 const char* haystack = "big daddy/giant haystacks!";
1588
1589 // The current strstr() implementation has special cases for needles of
1590 // lengths 0, 1, 2, 3, and 4, plus a long needle case. We test matches at the
1591 // beginning, middle, and end of the haystack.
1592
1593 ASSERT_EQ(haystack + 0, strstr(haystack, ""));
1594
1595 ASSERT_EQ(haystack + 0, strstr(haystack, "b"));
1596 ASSERT_EQ(haystack + 0, strstr(haystack, "bi"));
1597 ASSERT_EQ(haystack + 0, strstr(haystack, "big"));
1598 ASSERT_EQ(haystack + 0, strstr(haystack, "big "));
1599 ASSERT_EQ(haystack + 0, strstr(haystack, "big d"));
1600
1601 ASSERT_EQ(haystack + 2, strstr(haystack, "g"));
1602 ASSERT_EQ(haystack + 10, strstr(haystack, "gi"));
1603 ASSERT_EQ(haystack + 10, strstr(haystack, "gia"));
1604 ASSERT_EQ(haystack + 10, strstr(haystack, "gian"));
1605 ASSERT_EQ(haystack + 10, strstr(haystack, "giant"));
1606
1607 ASSERT_EQ(haystack + 25, strstr(haystack, "!"));
1608 ASSERT_EQ(haystack + 24, strstr(haystack, "s!"));
1609 ASSERT_EQ(haystack + 23, strstr(haystack, "ks!"));
1610 ASSERT_EQ(haystack + 22, strstr(haystack, "cks!"));
1611 ASSERT_EQ(haystack + 21, strstr(haystack, "acks!"));
1612 }
1613
TEST(STRING_TEST,strcasestr_smoke)1614 TEST(STRING_TEST, strcasestr_smoke) {
1615 const char* haystack = "bIg dAdDy/gIaNt hAyStAcKs";
1616 ASSERT_EQ(haystack, strcasestr(haystack, ""));
1617 ASSERT_EQ(haystack + 0, strcasestr(haystack, "B"));
1618 ASSERT_EQ(haystack + 1, strcasestr(haystack, "i"));
1619 ASSERT_EQ(haystack + 4, strcasestr(haystack, "Da"));
1620 }
1621
TEST(STRING_TEST,strcoll_smoke)1622 TEST(STRING_TEST, strcoll_smoke) {
1623 ASSERT_TRUE(strcoll("aab", "aac") < 0);
1624 ASSERT_TRUE(strcoll("aab", "aab") == 0);
1625 ASSERT_TRUE(strcoll("aac", "aab") > 0);
1626 }
1627
TEST(STRING_TEST,strcoll_l_smoke)1628 TEST(STRING_TEST, strcoll_l_smoke) {
1629 // bionic just forwards to strcoll(3).
1630 ASSERT_TRUE(strcoll_l("aab", "aac", LC_GLOBAL_LOCALE) < 0);
1631 ASSERT_TRUE(strcoll_l("aab", "aab", LC_GLOBAL_LOCALE) == 0);
1632 ASSERT_TRUE(strcoll_l("aac", "aab", LC_GLOBAL_LOCALE) > 0);
1633 }
1634
TEST(STRING_TEST,strxfrm_smoke)1635 TEST(STRING_TEST, strxfrm_smoke) {
1636 const char* src1 = "aab";
1637 char dst1[16] = {};
1638 // Dry run.
1639 ASSERT_EQ(strxfrm(dst1, src1, 0), 3U);
1640 ASSERT_STREQ(dst1, "");
1641 // Really do it.
1642 ASSERT_EQ(strxfrm(dst1, src1, sizeof(dst1)), 3U);
1643
1644 const char* src2 = "aac";
1645 char dst2[16] = {};
1646 // Dry run.
1647 ASSERT_EQ(strxfrm(dst2, src2, 0), 3U);
1648 ASSERT_STREQ(dst2, "");
1649 // Really do it.
1650 ASSERT_EQ(strxfrm(dst2, src2, sizeof(dst2)), 3U);
1651
1652 // The "transform" of two different strings should cause different outputs.
1653 ASSERT_TRUE(strcmp(dst1, dst2) < 0);
1654 }
1655
TEST(STRING_TEST,strxfrm_l_smoke)1656 TEST(STRING_TEST, strxfrm_l_smoke) {
1657 // bionic just forwards to strxfrm(3), so this is a subset of the
1658 // strxfrm test.
1659 const char* src1 = "aab";
1660 char dst1[16] = {};
1661 ASSERT_EQ(strxfrm_l(dst1, src1, 0, LC_GLOBAL_LOCALE), 3U);
1662 ASSERT_STREQ(dst1, "");
1663 ASSERT_EQ(strxfrm_l(dst1, src1, sizeof(dst1), LC_GLOBAL_LOCALE), 3U);
1664 }
1665
TEST(STRING_TEST,memccpy_smoke)1666 TEST(STRING_TEST, memccpy_smoke) {
1667 char dst[32];
1668
1669 memset(dst, 0, sizeof(dst));
1670 char* p = static_cast<char*>(memccpy(dst, "hello world", ' ', 32));
1671 ASSERT_STREQ("hello ", dst);
1672 ASSERT_EQ(ptrdiff_t(6), p - dst);
1673
1674 memset(dst, 0, sizeof(dst));
1675 ASSERT_EQ(nullptr, memccpy(dst, "hello world", ' ', 4));
1676 ASSERT_STREQ("hell", dst);
1677 }
1678
TEST(STRING_TEST,memset_explicit_smoke)1679 TEST(STRING_TEST, memset_explicit_smoke) {
1680 #if defined(__BIONIC__)
1681 // We can't reliably test that the compiler won't optimize out calls to
1682 // memset_explicit(), but we can at least check that it behaves like memset.
1683 char buf[32];
1684 memset_explicit(buf, 'x', sizeof(buf));
1685 ASSERT_TRUE(memcmp(buf, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", sizeof(buf)) == 0);
1686 #else
1687 GTEST_SKIP() << "memset_explicit not available";
1688 #endif
1689 }
1690
TEST(STRING_TEST,strerrorname_np)1691 TEST(STRING_TEST, strerrorname_np) {
1692 #if defined(__BIONIC__)
1693 ASSERT_STREQ("0", strerrorname_np(0));
1694 ASSERT_STREQ("EINVAL", strerrorname_np(EINVAL));
1695 ASSERT_STREQ("ENOSYS", strerrorname_np(ENOSYS));
1696
1697 ASSERT_EQ(nullptr, strerrorname_np(-1));
1698 ASSERT_EQ(nullptr, strerrorname_np(666));
1699 #else
1700 GTEST_SKIP() << "strerrorname_np not available";
1701 #endif
1702 }
1703