blob: 14b284e2d2693d8de95a553028e104bad069d47a [file] [log] [blame]
Irina Tirdeab5f053b2012-09-08 09:17:54 +03001/*
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#include <gtest/gtest.h>
18
19#include <errno.h>
Anna Tikhonova036154b2012-10-05 15:21:11 +040020#include <math.h>
Irina Tirdeab5f053b2012-09-08 09:17:54 +030021#include <string.h>
22
Christopher Ferrisb687ad32013-11-06 17:32:11 -080023#include "buffer_tests.h"
24
Anna Tikhonova036154b2012-10-05 15:21:11 +040025#define KB 1024
26#define SMALL 1*KB
Christopher Ferrisb687ad32013-11-06 17:32:11 -080027#define MEDIUM 4*KB
Anna Tikhonova036154b2012-10-05 15:21:11 +040028#define LARGE 64*KB
29
30static int signum(int i) {
31 if (i < 0) {
32 return -1;
33 } else if (i > 0) {
34 return 1;
35 }
36 return 0;
37}
38
Irina Tirdeab5f053b2012-09-08 09:17:54 +030039TEST(string, strerror) {
40 // Valid.
41 ASSERT_STREQ("Success", strerror(0));
42 ASSERT_STREQ("Operation not permitted", strerror(1));
43
44 // Invalid.
Elliott Hughese6e60062013-01-10 16:01:59 -080045 ASSERT_STREQ("Unknown error -1", strerror(-1));
Irina Tirdeab5f053b2012-09-08 09:17:54 +030046 ASSERT_STREQ("Unknown error 1234", strerror(1234));
47}
48
Christopher Ferrisf04935c2013-12-20 18:43:21 -080049#if defined(__BIONIC__)
Elliott Hughesad88a082012-10-24 18:37:21 -070050static void* ConcurrentStrErrorFn(void*) {
Irina Tirdeab5f053b2012-09-08 09:17:54 +030051 bool equal = (strcmp("Unknown error 2002", strerror(2002)) == 0);
52 return reinterpret_cast<void*>(equal);
53}
Christopher Ferrisf04935c2013-12-20 18:43:21 -080054#endif // __BIONIC__
Irina Tirdeab5f053b2012-09-08 09:17:54 +030055
Christopher Ferrisf04935c2013-12-20 18:43:21 -080056// glibc's strerror isn't thread safe, only its strsignal.
Irina Tirdeab5f053b2012-09-08 09:17:54 +030057TEST(string, strerror_concurrent) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -080058#if defined(__BIONIC__)
Irina Tirdeab5f053b2012-09-08 09:17:54 +030059 const char* strerror1001 = strerror(1001);
60 ASSERT_STREQ("Unknown error 1001", strerror1001);
61
62 pthread_t t;
63 ASSERT_EQ(0, pthread_create(&t, NULL, ConcurrentStrErrorFn, NULL));
64 void* result;
65 ASSERT_EQ(0, pthread_join(t, &result));
66 ASSERT_TRUE(static_cast<bool>(result));
67
68 ASSERT_STREQ("Unknown error 1001", strerror1001);
Christopher Ferrisf04935c2013-12-20 18:43:21 -080069#else // __BIONIC__
70 GTEST_LOG_(INFO) << "This test does nothing.\n";
71#endif // __BIONIC__
Irina Tirdeab5f053b2012-09-08 09:17:54 +030072}
Elliott Hughesad88a082012-10-24 18:37:21 -070073
Irina Tirdeab5f053b2012-09-08 09:17:54 +030074TEST(string, strerror_r) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -080075#if defined(__BIONIC__) // glibc's strerror_r doesn't even have the same signature as the POSIX one.
Irina Tirdeab5f053b2012-09-08 09:17:54 +030076 char buf[256];
77
78 // Valid.
79 ASSERT_EQ(0, strerror_r(0, buf, sizeof(buf)));
80 ASSERT_STREQ("Success", buf);
81 ASSERT_EQ(0, strerror_r(1, buf, sizeof(buf)));
82 ASSERT_STREQ("Operation not permitted", buf);
83
84 // Invalid.
85 ASSERT_EQ(0, strerror_r(-1, buf, sizeof(buf)));
Nick Kralevich60605892013-01-15 10:35:09 -080086 ASSERT_STREQ("Unknown error -1", buf);
Irina Tirdeab5f053b2012-09-08 09:17:54 +030087 ASSERT_EQ(0, strerror_r(1234, buf, sizeof(buf)));
88 ASSERT_STREQ("Unknown error 1234", buf);
89
90 // Buffer too small.
91 ASSERT_EQ(-1, strerror_r(0, buf, 2));
92 ASSERT_EQ(ERANGE, errno);
Christopher Ferrisf04935c2013-12-20 18:43:21 -080093#else // __BIONIC__
94 GTEST_LOG_(INFO) << "This test does nothing.\n";
95#endif // __BIONIC__
Irina Tirdeab5f053b2012-09-08 09:17:54 +030096}
Irina Tirdeab5f053b2012-09-08 09:17:54 +030097
98TEST(string, strsignal) {
99 // A regular signal.
100 ASSERT_STREQ("Hangup", strsignal(1));
101
102 // A real-time signal.
103#ifdef __GLIBC__ // glibc reserves real-time signals for internal use, and doesn't count those.
104 ASSERT_STREQ("Real-time signal 14", strsignal(48));
105#else
106 ASSERT_STREQ("Real-time signal 16", strsignal(48));
107#endif
108
109 // Errors.
110 ASSERT_STREQ("Unknown signal -1", strsignal(-1)); // Too small.
111 ASSERT_STREQ("Unknown signal 0", strsignal(0)); // Still too small.
112 ASSERT_STREQ("Unknown signal 1234", strsignal(1234)); // Too large.
113}
114
Elliott Hughesad88a082012-10-24 18:37:21 -0700115static void* ConcurrentStrSignalFn(void*) {
Irina Tirdeab5f053b2012-09-08 09:17:54 +0300116 bool equal = (strcmp("Unknown signal 2002", strsignal(2002)) == 0);
117 return reinterpret_cast<void*>(equal);
118}
119
120TEST(string, strsignal_concurrent) {
121 const char* strsignal1001 = strsignal(1001);
122 ASSERT_STREQ("Unknown signal 1001", strsignal1001);
123
124 pthread_t t;
125 ASSERT_EQ(0, pthread_create(&t, NULL, ConcurrentStrSignalFn, NULL));
126 void* result;
127 ASSERT_EQ(0, pthread_join(t, &result));
128 ASSERT_TRUE(static_cast<bool>(result));
129
130 ASSERT_STREQ("Unknown signal 1001", strsignal1001);
131}
Anna Tikhonova036154b2012-10-05 15:21:11 +0400132
133// TODO: where did these numbers come from?
134#define POS_ITER 10
135#define ITER 500
136
137// For every length we want to test, vary and change alignment
138// of allocated memory, fill it with some values, calculate
139// expected result and then run function and compare what we got.
140// These tests contributed by Intel Corporation.
141// TODO: make these tests more intention-revealing and less random.
Alexander Ivchenkobaa91f42013-06-27 12:55:46 +0400142template<class Character>
Anna Tikhonova036154b2012-10-05 15:21:11 +0400143struct StringTestState {
144 StringTestState(size_t MAX_LEN) : MAX_LEN(MAX_LEN) {
145 int max_alignment = 64;
146
147 // TODO: fix the tests to not sometimes use twice their specified "MAX_LEN".
Alexander Ivchenkobaa91f42013-06-27 12:55:46 +0400148 glob_ptr = reinterpret_cast<Character*>(valloc(2 * sizeof(Character) * MAX_LEN + max_alignment));
149 glob_ptr1 = reinterpret_cast<Character*>(valloc(2 * sizeof(Character) * MAX_LEN + max_alignment));
150 glob_ptr2 = reinterpret_cast<Character*>(valloc(2 * sizeof(Character) * MAX_LEN + max_alignment));
Anna Tikhonova036154b2012-10-05 15:21:11 +0400151
152 InitLenArray();
153
154 srandom(1234);
155 }
156
157 ~StringTestState() {
158 free(glob_ptr);
159 free(glob_ptr1);
160 free(glob_ptr2);
161 }
162
163 void NewIteration() {
164 int alignments[] = { 24, 32, 16, 48, 1, 2, 3, 0, 5, 11 };
165 int usable_alignments = 10;
166 int align1 = alignments[random() % (usable_alignments - 1)];
167 int align2 = alignments[random() % (usable_alignments - 1)];
168
169 ptr = glob_ptr + align1;
170 ptr1 = glob_ptr1 + align1;
171 ptr2 = glob_ptr2 + align2;
172 }
173
174 const size_t MAX_LEN;
Alexander Ivchenkobaa91f42013-06-27 12:55:46 +0400175 Character *ptr, *ptr1, *ptr2;
Anna Tikhonova036154b2012-10-05 15:21:11 +0400176 size_t n;
177 int len[ITER + 1];
178
179 private:
Alexander Ivchenkobaa91f42013-06-27 12:55:46 +0400180 Character *glob_ptr, *glob_ptr1, *glob_ptr2;
Anna Tikhonova036154b2012-10-05 15:21:11 +0400181
182 // Calculate input lengths and fill state.len with them.
183 // Test small lengths with more density than big ones. Manually push
184 // smallest (0) and biggest (MAX_LEN) lengths. Avoid repeats.
185 // Return number of lengths to test.
186 void InitLenArray() {
187 n = 0;
188 len[n++] = 0;
189 for (size_t i = 1; i < ITER; ++i) {
190 int l = (int) exp(log((double) MAX_LEN) * i / ITER);
191 if (l != len[n - 1]) {
192 len[n++] = l;
193 }
194 }
195 len[n++] = MAX_LEN;
196 }
197};
198
199TEST(string, strcat) {
Alexander Ivchenkobaa91f42013-06-27 12:55:46 +0400200 StringTestState<char> state(SMALL);
Anna Tikhonova036154b2012-10-05 15:21:11 +0400201 for (size_t i = 1; i < state.n; i++) {
202 for (size_t j = 0; j < POS_ITER; j++) {
203 state.NewIteration();
204
205 memset(state.ptr2, '\2', state.MAX_LEN);
206 state.ptr2[state.MAX_LEN - 1] = '\0';
207 memcpy(state.ptr, state.ptr2, 2 * state.MAX_LEN);
208
209 memset(state.ptr1, random() & 255, state.len[i]);
210 state.ptr1[random() % state.len[i]] = '\0';
211 state.ptr1[state.len[i] - 1] = '\0';
212
213 strcpy(state.ptr + state.MAX_LEN - 1, state.ptr1);
214
215 EXPECT_TRUE(strcat(state.ptr2, state.ptr1) == state.ptr2);
216 EXPECT_TRUE(memcmp(state.ptr, state.ptr2, 2 * state.MAX_LEN) == 0);
217 }
218 }
219}
220
Nick Kralevich13476de2013-06-03 10:58:06 -0700221// one byte target with "\0" source
222TEST(string, strcpy2) {
223 char buf[1];
224 char* orig = strdup("");
Christopher Ferris950a58e2014-04-04 14:38:18 -0700225 ASSERT_EQ(buf, strcpy(buf, orig));
Nick Kralevich13476de2013-06-03 10:58:06 -0700226 ASSERT_EQ('\0', buf[0]);
227 free(orig);
228}
229
230// multibyte target where we under fill target
231TEST(string, strcpy3) {
232 char buf[10];
233 char* orig = strdup("12345");
234 memset(buf, 'A', sizeof(buf));
Christopher Ferris950a58e2014-04-04 14:38:18 -0700235 ASSERT_EQ(buf, strcpy(buf, orig));
236 ASSERT_STREQ("12345", buf);
Nick Kralevich13476de2013-06-03 10:58:06 -0700237 ASSERT_EQ('A', buf[6]);
238 ASSERT_EQ('A', buf[7]);
239 ASSERT_EQ('A', buf[8]);
240 ASSERT_EQ('A', buf[9]);
241 free(orig);
242}
243
244// multibyte target where we fill target exactly
245TEST(string, strcpy4) {
246 char buf[10];
247 char* orig = strdup("123456789");
248 memset(buf, 'A', sizeof(buf));
Christopher Ferris950a58e2014-04-04 14:38:18 -0700249 ASSERT_EQ(buf, strcpy(buf, orig));
250 ASSERT_STREQ("123456789", buf);
251 free(orig);
252}
253
254// one byte target with "\0" source
255TEST(string, stpcpy2) {
256 char buf[1];
257 char* orig = strdup("");
258 ASSERT_EQ(buf, stpcpy(buf, orig));
259 ASSERT_EQ('\0', buf[0]);
260 free(orig);
261}
262
263// multibyte target where we under fill target
264TEST(string, stpcpy3) {
265 char buf[10];
266 char* orig = strdup("12345");
267 memset(buf, 'A', sizeof(buf));
268 ASSERT_EQ(buf+strlen(orig), stpcpy(buf, orig));
269 ASSERT_STREQ("12345", buf);
270 ASSERT_EQ('A', buf[6]);
271 ASSERT_EQ('A', buf[7]);
272 ASSERT_EQ('A', buf[8]);
273 ASSERT_EQ('A', buf[9]);
274 free(orig);
275}
276
277// multibyte target where we fill target exactly
278TEST(string, stpcpy4) {
279 char buf[10];
280 char* orig = strdup("123456789");
281 memset(buf, 'A', sizeof(buf));
282 ASSERT_EQ(buf+strlen(orig), stpcpy(buf, orig));
283 ASSERT_STREQ("123456789", buf);
Nick Kralevich13476de2013-06-03 10:58:06 -0700284 free(orig);
285}
286
Nick Kralevichcf870192013-05-30 16:48:53 -0700287TEST(string, strcat2) {
288 char buf[10];
289 memset(buf, 'A', sizeof(buf));
290 buf[0] = 'a';
291 buf[1] = '\0';
292 char* res = strcat(buf, "01234");
293 ASSERT_EQ(buf, res);
Christopher Ferris950a58e2014-04-04 14:38:18 -0700294 ASSERT_STREQ("a01234", buf);
Nick Kralevichcf870192013-05-30 16:48:53 -0700295 ASSERT_EQ('A', buf[7]);
296 ASSERT_EQ('A', buf[8]);
297 ASSERT_EQ('A', buf[9]);
298}
299
300TEST(string, strcat3) {
301 char buf[10];
302 memset(buf, 'A', sizeof(buf));
303 buf[0] = 'a';
304 buf[1] = '\0';
305 char* res = strcat(buf, "01234567");
306 ASSERT_EQ(buf, res);
Christopher Ferris950a58e2014-04-04 14:38:18 -0700307 ASSERT_STREQ("a01234567", buf);
Nick Kralevichcf870192013-05-30 16:48:53 -0700308}
309
310TEST(string, strncat2) {
311 char buf[10];
312 memset(buf, 'A', sizeof(buf));
313 buf[0] = 'a';
314 buf[1] = '\0';
315 char* res = strncat(buf, "01234", sizeof(buf) - strlen(buf) - 1);
316 ASSERT_EQ(buf, res);
Christopher Ferris950a58e2014-04-04 14:38:18 -0700317 ASSERT_STREQ("a01234", buf);
Nick Kralevichcf870192013-05-30 16:48:53 -0700318 ASSERT_EQ('A', buf[7]);
319 ASSERT_EQ('A', buf[8]);
320 ASSERT_EQ('A', buf[9]);
321}
322
323TEST(string, strncat3) {
324 char buf[10];
325 memset(buf, 'A', sizeof(buf));
326 buf[0] = 'a';
327 buf[1] = '\0';
328 char* res = strncat(buf, "0123456789", 5);
329 ASSERT_EQ(buf, res);
Christopher Ferris950a58e2014-04-04 14:38:18 -0700330 ASSERT_STREQ("a01234", buf);
Nick Kralevichcf870192013-05-30 16:48:53 -0700331 ASSERT_EQ('A', buf[7]);
332 ASSERT_EQ('A', buf[8]);
333 ASSERT_EQ('A', buf[9]);
334}
335
336TEST(string, strncat4) {
337 char buf[10];
338 memset(buf, 'A', sizeof(buf));
339 buf[0] = 'a';
340 buf[1] = '\0';
341 char* res = strncat(buf, "01234567", 8);
342 ASSERT_EQ(buf, res);
Christopher Ferris950a58e2014-04-04 14:38:18 -0700343 ASSERT_STREQ("a01234567", buf);
Nick Kralevichcf870192013-05-30 16:48:53 -0700344}
345
346TEST(string, strncat5) {
347 char buf[10];
348 memset(buf, 'A', sizeof(buf));
349 buf[0] = 'a';
350 buf[1] = '\0';
351 char* res = strncat(buf, "01234567", 9);
352 ASSERT_EQ(buf, res);
Christopher Ferris950a58e2014-04-04 14:38:18 -0700353 ASSERT_STREQ("a01234567", buf);
Nick Kralevichcf870192013-05-30 16:48:53 -0700354}
355
Nick Kralevich4f40e512013-04-19 16:54:22 -0700356TEST(string, strchr_with_0) {
357 char buf[10];
358 const char* s = "01234";
359 memcpy(buf, s, strlen(s) + 1);
360 EXPECT_TRUE(strchr(buf, '\0') == (buf + strlen(s)));
361}
362
Anna Tikhonova036154b2012-10-05 15:21:11 +0400363TEST(string, strchr) {
364 int seek_char = random() & 255;
365
Alexander Ivchenkobaa91f42013-06-27 12:55:46 +0400366 StringTestState<char> state(SMALL);
Anna Tikhonova036154b2012-10-05 15:21:11 +0400367 for (size_t i = 1; i < state.n; i++) {
368 for (size_t j = 0; j < POS_ITER; j++) {
369 state.NewIteration();
370
371 if (~seek_char > 0) {
372 memset(state.ptr1, ~seek_char, state.len[i]);
373 } else {
374 memset(state.ptr1, '\1', state.len[i]);
375 }
376 state.ptr1[state.len[i] - 1] = '\0';
377
378 int pos = random() % state.MAX_LEN;
379 char* expected;
380 if (pos >= state.len[i] - 1) {
381 if (seek_char == 0) {
382 expected = state.ptr1 + state.len[i] - 1;
383 } else {
384 expected = NULL;
385 }
386 } else {
387 state.ptr1[pos] = seek_char;
388 expected = state.ptr1 + pos;
389 }
390
391 ASSERT_TRUE(strchr(state.ptr1, seek_char) == expected);
392 }
393 }
394}
395
396TEST(string, strcmp) {
Alexander Ivchenkobaa91f42013-06-27 12:55:46 +0400397 StringTestState<char> state(SMALL);
Anna Tikhonova036154b2012-10-05 15:21:11 +0400398 for (size_t i = 1; i < state.n; i++) {
399 for (size_t j = 0; j < POS_ITER; j++) {
400 state.NewIteration();
401
402 memset(state.ptr1, 'v', state.MAX_LEN);
403 memset(state.ptr2, 'n', state.MAX_LEN);
404 state.ptr1[state.len[i] - 1] = '\0';
405 state.ptr2[state.len[i] - 1] = '\0';
406
407 int pos = 1 + (random() % (state.MAX_LEN - 1));
408 int actual;
409 int expected;
410 if (pos >= state.len[i] - 1) {
411 memcpy(state.ptr1, state.ptr2, state.len[i]);
412 expected = 0;
413 actual = strcmp(state.ptr1, state.ptr2);
414 } else {
415 memcpy(state.ptr1, state.ptr2, pos);
416 if (state.ptr1[pos] > state.ptr2[pos]) {
417 expected = 1;
418 } else if (state.ptr1[pos] == state.ptr2[pos]) {
419 state.ptr1[pos + 1] = '\0';
420 state.ptr2[pos + 1] = '\0';
421 expected = 0;
422 } else {
423 expected = -1;
424 }
425 actual = strcmp(state.ptr1, state.ptr2);
426 }
427
428 ASSERT_EQ(expected, signum(actual));
429 }
430 }
431}
432
Christopher Ferris950a58e2014-04-04 14:38:18 -0700433TEST(string, stpcpy) {
434 StringTestState<char> state(SMALL);
435 for (size_t j = 0; j < POS_ITER; j++) {
436 state.NewIteration();
437
438 size_t pos = random() % state.MAX_LEN;
439
440 memset(state.ptr1, '\2', pos);
441 state.ptr1[pos] = '\0';
442 state.ptr1[state.MAX_LEN - 1] = '\0';
443
444 memcpy(state.ptr, state.ptr1, state.MAX_LEN);
445
446 memset(state.ptr2, '\1', state.MAX_LEN);
447 state.ptr2[state.MAX_LEN - 1] = '\0';
448
449 memset(state.ptr + state.MAX_LEN, '\1', state.MAX_LEN);
450 memcpy(state.ptr + state.MAX_LEN, state.ptr1, pos + 1);
451 state.ptr[2 * state.MAX_LEN - 1] = '\0';
452
453 ASSERT_TRUE(stpcpy(state.ptr2, state.ptr1) == state.ptr2 + strlen(state.ptr1));
454 ASSERT_FALSE((memcmp(state.ptr1, state.ptr, state.MAX_LEN)) != 0 ||
455 (memcmp(state.ptr2, state.ptr + state.MAX_LEN, state.MAX_LEN) != 0));
456 }
457}
458
Anna Tikhonova036154b2012-10-05 15:21:11 +0400459TEST(string, strcpy) {
Alexander Ivchenkobaa91f42013-06-27 12:55:46 +0400460 StringTestState<char> state(SMALL);
Anna Tikhonova036154b2012-10-05 15:21:11 +0400461 for (size_t j = 0; j < POS_ITER; j++) {
462 state.NewIteration();
463
464 size_t pos = random() % state.MAX_LEN;
465
466 memset(state.ptr1, '\2', pos);
467 state.ptr1[pos] = '\0';
468 state.ptr1[state.MAX_LEN - 1] = '\0';
469
470 memcpy(state.ptr, state.ptr1, state.MAX_LEN);
471
472 memset(state.ptr2, '\1', state.MAX_LEN);
473 state.ptr2[state.MAX_LEN - 1] = '\0';
474
475 memset(state.ptr + state.MAX_LEN, '\1', state.MAX_LEN);
476 memcpy(state.ptr + state.MAX_LEN, state.ptr1, pos + 1);
477 state.ptr[2 * state.MAX_LEN - 1] = '\0';
478
479 ASSERT_TRUE(strcpy(state.ptr2, state.ptr1) == state.ptr2);
480 ASSERT_FALSE((memcmp(state.ptr1, state.ptr, state.MAX_LEN)) != 0 ||
481 (memcmp(state.ptr2, state.ptr + state.MAX_LEN, state.MAX_LEN) != 0));
482 }
483}
484
Anna Tikhonova036154b2012-10-05 15:21:11 +0400485TEST(string, strlcat) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800486#if defined(__BIONIC__)
Alexander Ivchenkobaa91f42013-06-27 12:55:46 +0400487 StringTestState<char> state(SMALL);
Anna Tikhonova036154b2012-10-05 15:21:11 +0400488 for (size_t i = 0; i < state.n; i++) {
489 for (size_t j = 0; j < POS_ITER; j++) {
490 state.NewIteration();
491
492 memset(state.ptr2, '\2', state.MAX_LEN + state.len[i]);
493 state.ptr2[state.MAX_LEN - 1] = '\0';
494 memcpy(state.ptr, state.ptr2, state.MAX_LEN + state.len[i]);
495
496 int pos = random() % state.MAX_LEN;
497 memset(state.ptr1, '\3', pos);
498 state.ptr1[pos] = '\0';
499 if (pos < state.len[i]) {
500 memcpy(state.ptr + state.MAX_LEN - 1, state.ptr1, pos + 1);
501 } else {
502 memcpy(state.ptr + state.MAX_LEN - 1, state.ptr1, state.len[i]);
503 state.ptr[state.MAX_LEN + state.len[i] - 1] = '\0';
504 }
505
506 strlcat(state.ptr2, state.ptr1, state.MAX_LEN + state.len[i]);
507
508 ASSERT_TRUE(memcmp(state.ptr, state.ptr2, state.MAX_LEN + state.len[i]) == 0);
509 }
510 }
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800511#else // __BIONIC__
512 GTEST_LOG_(INFO) << "This test does nothing.\n";
513#endif // __BIONIC__
Anna Tikhonova036154b2012-10-05 15:21:11 +0400514}
Anna Tikhonova036154b2012-10-05 15:21:11 +0400515
Anna Tikhonova036154b2012-10-05 15:21:11 +0400516TEST(string, strlcpy) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800517#if defined(__BIONIC__)
Alexander Ivchenkobaa91f42013-06-27 12:55:46 +0400518 StringTestState<char> state(SMALL);
Anna Tikhonova036154b2012-10-05 15:21:11 +0400519 for (size_t j = 0; j < POS_ITER; j++) {
520 state.NewIteration();
521
522 int rand = random() & 255;
523 if (rand < 1) {
524 rand = 1;
525 }
526 memset(state.ptr1, rand, state.MAX_LEN);
527
528 size_t pos = random() % state.MAX_LEN;
529 if (pos < state.MAX_LEN) {
530 state.ptr1[pos] = '\0';
531 }
532 memcpy(state.ptr, state.ptr1, state.MAX_LEN);
533
534 memset(state.ptr2, random() & 255, state.MAX_LEN);
535 memcpy(state.ptr + state.MAX_LEN, state.ptr2, state.MAX_LEN);
536
537 if (pos > state.MAX_LEN - 1) {
538 memcpy(state.ptr + state.MAX_LEN, state.ptr1, state.MAX_LEN);
539 state.ptr[2 * state.MAX_LEN - 1] = '\0';
540 } else {
541 memcpy(state.ptr + state.MAX_LEN, state.ptr1, pos + 1);
542 }
543
544 ASSERT_EQ(strlcpy(state.ptr2, state.ptr1, state.MAX_LEN), strlen(state.ptr1));
545 ASSERT_FALSE((memcmp(state.ptr1, state.ptr, state.MAX_LEN) != 0) ||
546 (memcmp(state.ptr2, state.ptr + state.MAX_LEN, state.MAX_LEN) != 0));
547 }
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800548#else // __BIONIC__
549 GTEST_LOG_(INFO) << "This test does nothing.\n";
550#endif // __BIONIC__
Anna Tikhonova036154b2012-10-05 15:21:11 +0400551}
Anna Tikhonova036154b2012-10-05 15:21:11 +0400552
553TEST(string, strncat) {
Alexander Ivchenkobaa91f42013-06-27 12:55:46 +0400554 StringTestState<char> state(SMALL);
Anna Tikhonova036154b2012-10-05 15:21:11 +0400555 for (size_t i = 1; i < state.n; i++) {
556 for (size_t j = 0; j < POS_ITER; j++) {
557 state.NewIteration();
558
559 memset(state.ptr2, '\2', state.MAX_LEN);
560 state.ptr2[state.MAX_LEN - 1] = '\0';
561 memcpy(state.ptr, state.ptr2, 2 * state.MAX_LEN);
562
563 memset(state.ptr1, random() & 255, state.len[i]);
564 state.ptr1[random() % state.len[i]] = '\0';
565 state.ptr1[state.len[i] - 1] = '\0';
566
567 size_t pos = strlen(state.ptr1);
568
569 size_t actual = random() % state.len[i];
570 strncpy(state.ptr + state.MAX_LEN - 1, state.ptr1, std::min(actual, pos));
571 state.ptr[state.MAX_LEN + std::min(actual, pos) - 1] = '\0';
572
573 ASSERT_TRUE(strncat(state.ptr2, state.ptr1, actual) == state.ptr2);
574 ASSERT_EQ(memcmp(state.ptr, state.ptr2, 2 * state.MAX_LEN), 0);
575 }
576 }
577}
578
579TEST(string, strncmp) {
Alexander Ivchenkobaa91f42013-06-27 12:55:46 +0400580 StringTestState<char> state(SMALL);
Anna Tikhonova036154b2012-10-05 15:21:11 +0400581 for (size_t i = 1; i < state.n; i++) {
582 for (size_t j = 0; j < POS_ITER; j++) {
583 state.NewIteration();
584
585 memset(state.ptr1, 'v', state.MAX_LEN);
586 memset(state.ptr2, 'n', state.MAX_LEN);
587 state.ptr1[state.len[i] - 1] = '\0';
588 state.ptr2[state.len[i] - 1] = '\0';
589
590 int pos = 1 + (random() % (state.MAX_LEN - 1));
591 int actual;
592 int expected;
593 if (pos >= state.len[i] - 1) {
594 memcpy(state.ptr1, state.ptr2, state.len[i]);
595 expected = 0;
596 actual = strncmp(state.ptr1, state.ptr2, state.len[i]);
597 } else {
598 memcpy(state.ptr1, state.ptr2, pos);
599 if (state.ptr1[pos] > state.ptr2[pos]) {
600 expected = 1;
601 } else if (state.ptr1[pos] == state.ptr2[pos]) {
602 state.ptr1[pos + 1] = '\0';
603 state.ptr2[pos + 1] = '\0';
604 expected = 0;
605 } else {
606 expected = -1;
607 }
608 actual = strncmp(state.ptr1, state.ptr2, state.len[i]);
609 }
610
611 ASSERT_EQ(expected, signum(actual));
612 }
613 }
614}
615
Christopher Ferris950a58e2014-04-04 14:38:18 -0700616TEST(string, stpncpy) {
617 StringTestState<char> state(SMALL);
618 for (size_t j = 0; j < ITER; j++) {
619 state.NewIteration();
620
621 // Choose a random value to fill the string, except \0 (string terminator),
622 // or \1 (guarantees it's different from anything in ptr2).
623 memset(state.ptr1, (random() % 254) + 2, state.MAX_LEN);
624 // Choose a random size for our src buffer.
625 size_t ptr1_len = random() % state.MAX_LEN;
626 state.ptr1[ptr1_len] = '\0';
627 // Copy ptr1 into ptr, used to verify that ptr1 does not get modified.
628 memcpy(state.ptr, state.ptr1, state.MAX_LEN);
629 // Init ptr2 to a set value.
630 memset(state.ptr2, '\1', state.MAX_LEN);
631
632 // Choose a random amount of data to copy.
633 size_t copy_len = random() % state.MAX_LEN;
634
635 // Set the second half of ptr to the expected pattern in ptr2.
636 memset(state.ptr + state.MAX_LEN, '\1', state.MAX_LEN);
637 memcpy(state.ptr + state.MAX_LEN, state.ptr1, copy_len);
638 size_t expected_end;
639 if (copy_len > ptr1_len) {
640 memset(state.ptr + state.MAX_LEN + ptr1_len, '\0', copy_len - ptr1_len);
641 expected_end = ptr1_len;
642 } else {
643 expected_end = copy_len;
644 }
645
646 ASSERT_EQ(state.ptr2 + expected_end, stpncpy(state.ptr2, state.ptr1, copy_len));
647
648 // Verify ptr1 was not modified.
649 ASSERT_EQ(0, memcmp(state.ptr1, state.ptr, state.MAX_LEN));
650 // Verify ptr2 contains the expected data.
651 ASSERT_EQ(0, memcmp(state.ptr2, state.ptr + state.MAX_LEN, state.MAX_LEN));
652 }
653}
654
Anna Tikhonova036154b2012-10-05 15:21:11 +0400655TEST(string, strncpy) {
Alexander Ivchenkobaa91f42013-06-27 12:55:46 +0400656 StringTestState<char> state(SMALL);
Anna Tikhonova036154b2012-10-05 15:21:11 +0400657 for (size_t j = 0; j < ITER; j++) {
658 state.NewIteration();
659
Christopher Ferris950a58e2014-04-04 14:38:18 -0700660 // Choose a random value to fill the string, except \0 (string terminator),
661 // or \1 (guarantees it's different from anything in ptr2).
662 memset(state.ptr1, (random() % 254) + 2, state.MAX_LEN);
663 // Choose a random size for our src buffer.
664 size_t ptr1_len = random() % state.MAX_LEN;
665 state.ptr1[ptr1_len] = '\0';
666 // Copy ptr1 into ptr, used to verify that ptr1 does not get modified.
Anna Tikhonova036154b2012-10-05 15:21:11 +0400667 memcpy(state.ptr, state.ptr1, state.MAX_LEN);
Christopher Ferris950a58e2014-04-04 14:38:18 -0700668 // Init ptr2 to a set value.
Anna Tikhonova036154b2012-10-05 15:21:11 +0400669 memset(state.ptr2, '\1', state.MAX_LEN);
670
Christopher Ferris950a58e2014-04-04 14:38:18 -0700671 // Choose a random amount of data to copy.
672 size_t copy_len = random() % state.MAX_LEN;
673
674 // Set the second half of ptr to the expected pattern in ptr2.
675 memset(state.ptr + state.MAX_LEN, '\1', state.MAX_LEN);
676 memcpy(state.ptr + state.MAX_LEN, state.ptr1, copy_len);
677 size_t expected_end;
678 if (copy_len > ptr1_len) {
679 memset(state.ptr + state.MAX_LEN + ptr1_len, '\0', copy_len - ptr1_len);
680 expected_end = ptr1_len;
Anna Tikhonova036154b2012-10-05 15:21:11 +0400681 } else {
Christopher Ferris950a58e2014-04-04 14:38:18 -0700682 expected_end = copy_len;
Anna Tikhonova036154b2012-10-05 15:21:11 +0400683 }
684
Christopher Ferris950a58e2014-04-04 14:38:18 -0700685 ASSERT_EQ(state.ptr2 + expected_end, stpncpy(state.ptr2, state.ptr1, copy_len));
Anna Tikhonova036154b2012-10-05 15:21:11 +0400686
Christopher Ferris950a58e2014-04-04 14:38:18 -0700687 // Verify ptr1 was not modified.
688 ASSERT_EQ(0, memcmp(state.ptr1, state.ptr, state.MAX_LEN));
689 // Verify ptr2 contains the expected data.
690 ASSERT_EQ(0, memcmp(state.ptr2, state.ptr + state.MAX_LEN, state.MAX_LEN));
Anna Tikhonova036154b2012-10-05 15:21:11 +0400691 }
692}
693
694TEST(string, strrchr) {
695 int seek_char = random() & 255;
Alexander Ivchenkobaa91f42013-06-27 12:55:46 +0400696 StringTestState<char> state(SMALL);
Anna Tikhonova036154b2012-10-05 15:21:11 +0400697 for (size_t i = 1; i < state.n; i++) {
698 for (size_t j = 0; j < POS_ITER; j++) {
699 state.NewIteration();
700
701 if (~seek_char > 0) {
702 memset(state.ptr1, ~seek_char, state.len[i]);
703 } else {
704 memset(state.ptr1, '\1', state.len[i]);
705 }
706 state.ptr1[state.len[i] - 1] = '\0';
707
708 int pos = random() % state.MAX_LEN;
709 char* expected;
710 if (pos >= state.len[i] - 1) {
711 if (seek_char == 0) {
712 expected = state.ptr1 + state.len[i] - 1;
713 } else {
714 expected = NULL;
715 }
716 } else {
717 state.ptr1[pos] = seek_char;
718 expected = state.ptr1 + pos;
719 }
720
721 ASSERT_TRUE(strrchr(state.ptr1, seek_char) == expected);
722 }
723 }
724}
725
726TEST(string, memchr) {
727 int seek_char = random() & 255;
Alexander Ivchenkobaa91f42013-06-27 12:55:46 +0400728 StringTestState<char> state(SMALL);
Anna Tikhonova036154b2012-10-05 15:21:11 +0400729 for (size_t i = 0; i < state.n; i++) {
730 for (size_t j = 0; j < POS_ITER; j++) {
731 state.NewIteration();
732
733 memset(state.ptr1, ~seek_char, state.len[i]);
734
735 int pos = random() % state.MAX_LEN;
736 char* expected;
737 if (pos >= state.len[i]) {
738 expected = NULL;
739 } else {
740 state.ptr1[pos] = seek_char;
741 expected = state.ptr1 + pos;
742 }
743
744 ASSERT_TRUE(memchr(state.ptr1, seek_char, state.len[i]) == expected);
745 }
746 }
747}
748
749TEST(string, memrchr) {
750 int seek_char = random() & 255;
Alexander Ivchenkobaa91f42013-06-27 12:55:46 +0400751 StringTestState<char> state(SMALL);
Anna Tikhonova036154b2012-10-05 15:21:11 +0400752 for (size_t i = 0; i < state.n; i++) {
753 for (size_t j = 0; j < POS_ITER; j++) {
754 state.NewIteration();
755
756 memset(state.ptr1, ~seek_char, state.len[i]);
757
758 int pos = random() % state.MAX_LEN;
759 char* expected;
760 if (pos >= state.len[i]) {
761 expected = NULL;
762 } else {
763 state.ptr1[pos] = seek_char;
764 expected = state.ptr1 + pos;
765 }
766
767 ASSERT_TRUE(memrchr(state.ptr1, seek_char, state.len[i]) == expected);
768 }
769 }
770}
771
772TEST(string, memcmp) {
Alexander Ivchenkobaa91f42013-06-27 12:55:46 +0400773 StringTestState<char> state(SMALL);
Anna Tikhonova036154b2012-10-05 15:21:11 +0400774 for (size_t i = 0; i < state.n; i++) {
775 for (size_t j = 0; j < POS_ITER; j++) {
776 state.NewIteration();
777
778 int c1 = random() & 0xff;
779 int c2 = random() & 0xff;
780 memset(state.ptr1, c1, state.MAX_LEN);
781 memset(state.ptr2, c1, state.MAX_LEN);
782
783 int pos = (state.len[i] == 0) ? 0 : (random() % state.len[i]);
784 state.ptr2[pos] = c2;
785
786 int expected = (static_cast<int>(c1) - static_cast<int>(c2));
787 int actual = memcmp(state.ptr1, state.ptr2, state.MAX_LEN);
788
789 ASSERT_EQ(signum(expected), signum(actual));
790 }
791 }
792}
793
Alexander Ivchenkobaa91f42013-06-27 12:55:46 +0400794extern "C" int __memcmp16(const unsigned short *ptr1, const unsigned short *ptr2, size_t n);
795
796TEST(string, __memcmp16) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800797#if defined(__BIONIC__)
Alexander Ivchenkobaa91f42013-06-27 12:55:46 +0400798 StringTestState<unsigned short> state(SMALL);
799
800 for (size_t i = 0; i < state.n; i++) {
801 for (size_t j = 0; j < POS_ITER; j++) {
802 state.NewIteration();
803
804 unsigned short mask = 0xffff;
805 unsigned short c1 = rand() & mask;
806 unsigned short c2 = rand() & mask;
807
808 std::fill(state.ptr1, state.ptr1 + state.MAX_LEN, c1);
809 std::fill(state.ptr2, state.ptr2 + state.MAX_LEN, c1);
810
811 int pos = (state.len[i] == 0) ? 0 : (random() % state.len[i]);
812 state.ptr2[pos] = c2;
813
814 int expected = (static_cast<unsigned short>(c1) - static_cast<unsigned short>(c2));
815 int actual = __memcmp16(state.ptr1, state.ptr2, (size_t) state.MAX_LEN);
816
817 ASSERT_EQ(expected, actual);
818 }
819 }
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800820#else // __BIONIC__
821 GTEST_LOG_(INFO) << "This test does nothing.\n";
822#endif // __BIONIC__
Alexander Ivchenkobaa91f42013-06-27 12:55:46 +0400823}
Alexander Ivchenkobaa91f42013-06-27 12:55:46 +0400824
825TEST(string, wmemcmp) {
826 StringTestState<wchar_t> state(SMALL);
827
828 for (size_t i = 0; i < state.n; i++) {
829 for (size_t j = 0; j < POS_ITER; j++) {
830 state.NewIteration();
831
832 long long mask = ((long long) 1 << 8 * sizeof(wchar_t)) - 1;
833 int c1 = rand() & mask;
834 int c2 = rand() & mask;
835 wmemset(state.ptr1, c1, state.MAX_LEN);
836 wmemset(state.ptr2, c1, state.MAX_LEN);
837
838 int pos = (state.len[i] == 0) ? 0 : (random() % state.len[i]);
839 state.ptr2[pos] = c2;
840
841 int expected = (static_cast<int>(c1) - static_cast<int>(c2));
842 int actual = wmemcmp(state.ptr1, state.ptr2, (size_t) state.MAX_LEN);
843
844 ASSERT_EQ(signum(expected), signum(actual));
845 }
846 }
847}
848
Anna Tikhonova036154b2012-10-05 15:21:11 +0400849TEST(string, memcpy) {
Alexander Ivchenkobaa91f42013-06-27 12:55:46 +0400850 StringTestState<char> state(LARGE);
Anna Tikhonova036154b2012-10-05 15:21:11 +0400851 int rand = random() & 255;
852 for (size_t i = 0; i < state.n - 1; i++) {
853 for (size_t j = 0; j < POS_ITER; j++) {
854 state.NewIteration();
855
856 size_t pos = random() % (state.MAX_LEN - state.len[i]);
857
858 memset(state.ptr1, rand, state.len[i]);
859 memset(state.ptr1 + state.len[i], ~rand, state.MAX_LEN - state.len[i]);
860
861 memset(state.ptr2, rand, state.len[i]);
862 memset(state.ptr2 + state.len[i], ~rand, state.MAX_LEN - state.len[i]);
863 memset(state.ptr2 + pos, '\0', state.len[i]);
864
865 ASSERT_FALSE(memcpy(state.ptr2 + pos, state.ptr1 + pos, state.len[i]) != state.ptr2 + pos);
866 ASSERT_EQ(0, memcmp(state.ptr1, state.ptr2, state.MAX_LEN));
867 }
868 }
869}
870
871TEST(string, memset) {
Alexander Ivchenkobaa91f42013-06-27 12:55:46 +0400872 StringTestState<char> state(LARGE);
Anna Tikhonova036154b2012-10-05 15:21:11 +0400873 char ch = random () & 255;
874 for (size_t i = 0; i < state.n - 1; i++) {
875 for (size_t j = 0; j < POS_ITER; j++) {
876 state.NewIteration();
877
878 memset(state.ptr1, ~ch, state.MAX_LEN);
879 memcpy(state.ptr2, state.ptr1, state.MAX_LEN);
880
881 size_t pos = random () % (state.MAX_LEN - state.len[i]);
882 for (size_t k = pos; k < pos + state.len[i]; k++) {
883 state.ptr1[k] = ch;
884 }
885
886 ASSERT_TRUE(memset(state.ptr2 + pos, ch, state.len[i]) == state.ptr2 + pos);
887
888 ASSERT_EQ(0, memcmp(state.ptr1, state.ptr2, state.MAX_LEN));
889 }
890 }
891}
892
893TEST(string, memmove) {
Alexander Ivchenkobaa91f42013-06-27 12:55:46 +0400894 StringTestState<char> state(LARGE);
Anna Tikhonova036154b2012-10-05 15:21:11 +0400895 for (size_t i = 0; i < state.n - 1; i++) {
896 for (size_t j = 0; j < POS_ITER; j++) {
897 state.NewIteration();
898
899 memset(state.ptr1, random() & 255, 2 * state.MAX_LEN);
900
901 size_t pos = random() % (state.MAX_LEN - state.len[i]);
902
903 memset(state.ptr1, random() & 255, state.len[i]);
904 memcpy(state.ptr2, state.ptr1, 2 * state.MAX_LEN);
905 memcpy(state.ptr, state.ptr1, state.len[i]);
906 memcpy(state.ptr1 + pos, state.ptr, state.len[i]);
907
908 ASSERT_TRUE(memmove(state.ptr2 + pos, state.ptr2, state.len[i]) == state.ptr2 + pos);
909 ASSERT_EQ(0, memcmp(state.ptr2, state.ptr1, 2 * state.MAX_LEN));
910 }
911 }
912}
913
914TEST(string, bcopy) {
Alexander Ivchenkobaa91f42013-06-27 12:55:46 +0400915 StringTestState<char> state(LARGE);
Anna Tikhonova036154b2012-10-05 15:21:11 +0400916 for (size_t i = 0; i < state.n; i++) {
917 for (size_t j = 0; j < POS_ITER; j++) {
918 state.NewIteration();
919
920 memset(state.ptr1, random() & 255, state.MAX_LEN);
921 memset(state.ptr1 + state.MAX_LEN, random() & 255, state.MAX_LEN);
922 memcpy(state.ptr2, state.ptr1, 2 * state.MAX_LEN);
923
924 size_t start = random() % (2 * state.MAX_LEN - state.len[i]);
925 memcpy(state.ptr2 + start, state.ptr1, state.len[i]);
926
927 bcopy(state.ptr1, state.ptr1 + start, state.len[i]);
928 ASSERT_EQ(0, memcmp(state.ptr1, state.ptr2, 2 * state.MAX_LEN));
929 }
930 }
931}
932
933TEST(string, bzero) {
Alexander Ivchenkobaa91f42013-06-27 12:55:46 +0400934 StringTestState<char> state(LARGE);
Anna Tikhonova036154b2012-10-05 15:21:11 +0400935 for (size_t j = 0; j < ITER; j++) {
936 state.NewIteration();
937
938 memset(state.ptr1, random() & 255, state.MAX_LEN);
939
940 size_t start = random() % state.MAX_LEN;
941 size_t end = start + random() % (state.MAX_LEN - start);
942
943 memcpy(state.ptr2, state.ptr1, start);
944 memset(state.ptr2 + start, '\0', end - start);
945 memcpy(state.ptr2 + end, state.ptr1 + end, state.MAX_LEN - end);
946
947 bzero(state.ptr1 + start, end - start);
948
949 ASSERT_EQ(0, memcmp(state.ptr1, state.ptr2, state.MAX_LEN));
950 }
951}
Christopher Ferrisb687ad32013-11-06 17:32:11 -0800952
953static void DoMemcpyTest(uint8_t* src, uint8_t* dst, size_t len) {
954 memset(src, (len % 255) + 1, len);
955 memset(dst, 0, len);
956
957 ASSERT_EQ(dst, memcpy(dst, src, len));
958 ASSERT_TRUE(memcmp(src, dst, len) == 0);
959}
960
961TEST(string, memcpy_align) {
962 RunSrcDstBufferAlignTest(LARGE, DoMemcpyTest);
963}
964
965TEST(string, memcpy_overread) {
966 RunSrcDstBufferOverreadTest(DoMemcpyTest);
967}
968
969static void DoMemsetTest(uint8_t* buf, size_t len) {
970 for (size_t i = 0; i < len; i++) {
971 buf[i] = 0;
972 }
973 int value = (len % 255) + 1;
974 ASSERT_EQ(buf, memset(buf, value, len));
975 for (size_t i = 0; i < len; i++) {
976 ASSERT_EQ(value, buf[i]);
977 }
978}
979
980TEST(string, memset_align) {
981 RunSingleBufferAlignTest(LARGE, DoMemsetTest);
982}
983
984static void DoStrlenTest(uint8_t* buf, size_t len) {
985 if (len >= 1) {
986 memset(buf, (32 + (len % 96)), len - 1);
987 buf[len-1] = '\0';
988 ASSERT_EQ(len-1, strlen(reinterpret_cast<char*>(buf)));
989 }
990}
991
992TEST(string, strlen_align) {
993 RunSingleBufferAlignTest(LARGE, DoStrlenTest);
994}
995
996TEST(string, strlen_overread) {
997 RunSingleBufferOverreadTest(DoStrlenTest);
998}
999
1000static void DoStrcpyTest(uint8_t* src, uint8_t* dst, size_t len) {
1001 if (len >= 1) {
1002 memset(src, (32 + (len % 96)), len - 1);
1003 src[len-1] = '\0';
1004 memset(dst, 0, len);
1005 ASSERT_EQ(dst, reinterpret_cast<uint8_t*>(strcpy(reinterpret_cast<char*>(dst),
1006 reinterpret_cast<char*>(src))));
1007 ASSERT_TRUE(memcmp(src, dst, len) == 0);
1008 }
1009}
1010
1011TEST(string, strcpy_align) {
1012 RunSrcDstBufferAlignTest(LARGE, DoStrcpyTest);
1013}
1014
1015TEST(string, strcpy_overread) {
1016 RunSrcDstBufferOverreadTest(DoStrcpyTest);
1017}
1018
Christopher Ferris950a58e2014-04-04 14:38:18 -07001019static void DoStpcpyTest(uint8_t* src, uint8_t* dst, size_t len) {
1020 if (len >= 1) {
1021 memset(src, (32 + (len % 96)), len - 1);
1022 src[len-1] = '\0';
1023 memset(dst, 0, len);
1024 ASSERT_EQ(dst+len-1, reinterpret_cast<uint8_t*>(stpcpy(reinterpret_cast<char*>(dst),
1025 reinterpret_cast<char*>(src))));
1026 ASSERT_TRUE(memcmp(src, dst, len) == 0);
1027 }
1028}
1029
1030TEST(string, stpcpy_align) {
1031 RunSrcDstBufferAlignTest(LARGE, DoStpcpyTest);
1032}
1033
1034TEST(string, stpcpy_overread) {
1035 RunSrcDstBufferOverreadTest(DoStpcpyTest);
1036}
1037
Christopher Ferrisb687ad32013-11-06 17:32:11 -08001038// Use our own incrementer to cut down on the total number of calls.
Christopher Ferrise5bbb6b2013-12-03 18:39:10 -08001039static size_t LargeSetIncrement(size_t len) {
Christopher Ferrisb687ad32013-11-06 17:32:11 -08001040 if (len >= 4096) {
1041 return 4096;
1042 } else if (len >= 1024) {
1043 return 1024;
1044 } else if (len >= 256) {
1045 return 256;
1046 }
1047 return 1;
1048}
1049
1050#define STRCAT_DST_LEN 128
1051
1052static void DoStrcatTest(uint8_t* src, uint8_t* dst, size_t len) {
1053 if (len >= 1) {
1054 int value = 32 + (len % 96);
1055 memset(src, value, len - 1);
1056 src[len-1] = '\0';
1057
1058 if (len >= STRCAT_DST_LEN) {
1059 // Create a small buffer for doing quick compares in each loop.
1060 uint8_t cmp_buf[STRCAT_DST_LEN];
1061 // Make sure dst string contains a different value then the src string.
1062 int value2 = 32 + (value + 2) % 96;
1063 memset(cmp_buf, value2, sizeof(cmp_buf));
1064
1065 for (size_t i = 1; i <= STRCAT_DST_LEN; i++) {
1066 memset(dst, value2, i-1);
1067 memset(dst+i-1, 0, len-i);
1068 src[len-i] = '\0';
1069 ASSERT_EQ(dst, reinterpret_cast<uint8_t*>(strcat(reinterpret_cast<char*>(dst),
1070 reinterpret_cast<char*>(src))));
1071 ASSERT_TRUE(memcmp(dst, cmp_buf, i-1) == 0);
1072 ASSERT_TRUE(memcmp(src, dst+i-1, len-i+1) == 0);
1073 }
1074 } else {
1075 dst[0] = '\0';
1076 ASSERT_EQ(dst, reinterpret_cast<uint8_t*>(strcat(reinterpret_cast<char*>(dst),
1077 reinterpret_cast<char*>(src))));
1078 ASSERT_TRUE(memcmp(src, dst, len) == 0);
1079 }
1080 }
1081}
1082
1083TEST(string, strcat_align) {
Christopher Ferrise5bbb6b2013-12-03 18:39:10 -08001084 RunSrcDstBufferAlignTest(MEDIUM, DoStrcatTest, LargeSetIncrement);
Christopher Ferrisb687ad32013-11-06 17:32:11 -08001085}
1086
1087TEST(string, strcat_overread) {
1088 RunSrcDstBufferOverreadTest(DoStrcatTest);
1089}
Christopher Ferrise5bbb6b2013-12-03 18:39:10 -08001090
1091static void DoStrcmpTest(uint8_t* buf1, uint8_t* buf2, size_t len) {
1092 if (len >= 1) {
1093 memset(buf1, (32 + (len % 96)), len - 1);
1094 buf1[len-1] = '\0';
1095 memset(buf2, (32 + (len % 96)), len - 1);
1096 buf2[len-1] = '\0';
1097 ASSERT_EQ(0, strcmp(reinterpret_cast<char*>(buf1),
1098 reinterpret_cast<char*>(buf2)));
1099 }
1100}
1101
1102static void DoStrcmpFailTest(uint8_t* buf1, uint8_t* buf2, size_t len1, size_t len2) {
1103 // Do string length differences.
1104 int c = (32 + (len1 % 96));
1105 memset(buf1, c, len1 - 1);
1106 buf1[len1-1] = '\0';
1107 memset(buf2, c, len2 - 1);
1108 buf2[len2-1] = '\0';
1109 ASSERT_NE(0, strcmp(reinterpret_cast<char*>(buf1),
1110 reinterpret_cast<char*>(buf2)));
1111
1112 // Do single character differences.
1113 size_t len;
1114 if (len1 > len2) {
1115 len = len2;
1116 } else {
1117 len = len1;
1118 }
1119 // Need at least a two character buffer to do this test.
1120 if (len > 1) {
1121 buf1[len-1] = '\0';
1122 buf2[len-1] = '\0';
1123 int diff_c = (c + 1) % 96;
1124
1125 buf1[len-2] = diff_c;
1126 ASSERT_NE(0, strcmp(reinterpret_cast<char*>(buf1),
1127 reinterpret_cast<char*>(buf2)));
1128
1129 buf1[len-2] = c;
1130 buf2[len-2] = diff_c;
1131 ASSERT_NE(0, strcmp(reinterpret_cast<char*>(buf1),
1132 reinterpret_cast<char*>(buf2)));
1133 }
1134}
1135
1136TEST(string, strcmp_align) {
1137 RunCmpBufferAlignTest(MEDIUM, DoStrcmpTest, DoStrcmpFailTest, LargeSetIncrement);
1138}
1139
1140TEST(string, strcmp_overread) {
1141 RunCmpBufferOverreadTest(DoStrcmpTest, DoStrcmpFailTest);
1142}
1143
1144static void DoMemcmpTest(uint8_t* buf1, uint8_t* buf2, size_t len) {
1145 memset(buf1, len+1, len);
1146 memset(buf2, len+1, len);
1147 ASSERT_EQ(0, memcmp(buf1, buf2, len));
1148}
1149
1150static void DoMemcmpFailTest(uint8_t* buf1, uint8_t* buf2, size_t len1, size_t len2) {
1151 size_t len;
1152 if (len1 > len2) {
1153 len = len2;
1154 } else {
1155 len = len1;
1156 }
1157
1158 memset(buf1, len2+1, len);
1159 buf1[len-1] = len2;
1160 memset(buf2, len2+1, len);
1161 ASSERT_NE(0, memcmp(buf1, buf2, len));
1162
1163 buf1[len-1] = len2+1;
1164 buf2[len-1] = len2;
1165 ASSERT_NE(0, memcmp(buf1, buf2, len));
1166}
1167
1168TEST(string, memcmp_align) {
1169 RunCmpBufferAlignTest(MEDIUM, DoMemcmpTest, DoMemcmpFailTest, LargeSetIncrement);
1170}
1171
1172TEST(string, memcmp_overread) {
1173 RunCmpBufferOverreadTest(DoMemcmpTest, DoMemcmpFailTest);
1174}