blob: c35976a3e15eb5e5886c9a121f3e25587ac4d17e [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("");
225 strcpy(buf, orig);
226 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));
235 strcpy(buf, orig);
236 ASSERT_EQ('1', buf[0]);
237 ASSERT_EQ('2', buf[1]);
238 ASSERT_EQ('3', buf[2]);
239 ASSERT_EQ('4', buf[3]);
240 ASSERT_EQ('5', buf[4]);
241 ASSERT_EQ('\0', buf[5]);
242 ASSERT_EQ('A', buf[6]);
243 ASSERT_EQ('A', buf[7]);
244 ASSERT_EQ('A', buf[8]);
245 ASSERT_EQ('A', buf[9]);
246 free(orig);
247}
248
249// multibyte target where we fill target exactly
250TEST(string, strcpy4) {
251 char buf[10];
252 char* orig = strdup("123456789");
253 memset(buf, 'A', sizeof(buf));
254 strcpy(buf, orig);
255 ASSERT_EQ('1', buf[0]);
256 ASSERT_EQ('2', buf[1]);
257 ASSERT_EQ('3', buf[2]);
258 ASSERT_EQ('4', buf[3]);
259 ASSERT_EQ('5', buf[4]);
260 ASSERT_EQ('6', buf[5]);
261 ASSERT_EQ('7', buf[6]);
262 ASSERT_EQ('8', buf[7]);
263 ASSERT_EQ('9', buf[8]);
264 ASSERT_EQ('\0', buf[9]);
265 free(orig);
266}
267
Nick Kralevichcf870192013-05-30 16:48:53 -0700268TEST(string, strcat2) {
269 char buf[10];
270 memset(buf, 'A', sizeof(buf));
271 buf[0] = 'a';
272 buf[1] = '\0';
273 char* res = strcat(buf, "01234");
274 ASSERT_EQ(buf, res);
275 ASSERT_EQ('a', buf[0]);
276 ASSERT_EQ('0', buf[1]);
277 ASSERT_EQ('1', buf[2]);
278 ASSERT_EQ('2', buf[3]);
279 ASSERT_EQ('3', buf[4]);
280 ASSERT_EQ('4', buf[5]);
281 ASSERT_EQ('\0', buf[6]);
282 ASSERT_EQ('A', buf[7]);
283 ASSERT_EQ('A', buf[8]);
284 ASSERT_EQ('A', buf[9]);
285}
286
287TEST(string, strcat3) {
288 char buf[10];
289 memset(buf, 'A', sizeof(buf));
290 buf[0] = 'a';
291 buf[1] = '\0';
292 char* res = strcat(buf, "01234567");
293 ASSERT_EQ(buf, res);
294 ASSERT_EQ('a', buf[0]);
295 ASSERT_EQ('0', buf[1]);
296 ASSERT_EQ('1', buf[2]);
297 ASSERT_EQ('2', buf[3]);
298 ASSERT_EQ('3', buf[4]);
299 ASSERT_EQ('4', buf[5]);
300 ASSERT_EQ('5', buf[6]);
301 ASSERT_EQ('6', buf[7]);
302 ASSERT_EQ('7', buf[8]);
303 ASSERT_EQ('\0', buf[9]);
304}
305
306TEST(string, strncat2) {
307 char buf[10];
308 memset(buf, 'A', sizeof(buf));
309 buf[0] = 'a';
310 buf[1] = '\0';
311 char* res = strncat(buf, "01234", sizeof(buf) - strlen(buf) - 1);
312 ASSERT_EQ(buf, res);
313 ASSERT_EQ('a', buf[0]);
314 ASSERT_EQ('0', buf[1]);
315 ASSERT_EQ('1', buf[2]);
316 ASSERT_EQ('2', buf[3]);
317 ASSERT_EQ('3', buf[4]);
318 ASSERT_EQ('4', buf[5]);
319 ASSERT_EQ('\0', buf[6]);
320 ASSERT_EQ('A', buf[7]);
321 ASSERT_EQ('A', buf[8]);
322 ASSERT_EQ('A', buf[9]);
323}
324
325TEST(string, strncat3) {
326 char buf[10];
327 memset(buf, 'A', sizeof(buf));
328 buf[0] = 'a';
329 buf[1] = '\0';
330 char* res = strncat(buf, "0123456789", 5);
331 ASSERT_EQ(buf, res);
332 ASSERT_EQ('a', buf[0]);
333 ASSERT_EQ('0', buf[1]);
334 ASSERT_EQ('1', buf[2]);
335 ASSERT_EQ('2', buf[3]);
336 ASSERT_EQ('3', buf[4]);
337 ASSERT_EQ('4', buf[5]);
338 ASSERT_EQ('\0', buf[6]);
339 ASSERT_EQ('A', buf[7]);
340 ASSERT_EQ('A', buf[8]);
341 ASSERT_EQ('A', buf[9]);
342}
343
344TEST(string, strncat4) {
345 char buf[10];
346 memset(buf, 'A', sizeof(buf));
347 buf[0] = 'a';
348 buf[1] = '\0';
349 char* res = strncat(buf, "01234567", 8);
350 ASSERT_EQ(buf, res);
351 ASSERT_EQ('a', buf[0]);
352 ASSERT_EQ('0', buf[1]);
353 ASSERT_EQ('1', buf[2]);
354 ASSERT_EQ('2', buf[3]);
355 ASSERT_EQ('3', buf[4]);
356 ASSERT_EQ('4', buf[5]);
357 ASSERT_EQ('5', buf[6]);
358 ASSERT_EQ('6', buf[7]);
359 ASSERT_EQ('7', buf[8]);
360 ASSERT_EQ('\0', buf[9]);
361}
362
363TEST(string, strncat5) {
364 char buf[10];
365 memset(buf, 'A', sizeof(buf));
366 buf[0] = 'a';
367 buf[1] = '\0';
368 char* res = strncat(buf, "01234567", 9);
369 ASSERT_EQ(buf, res);
370 ASSERT_EQ('a', buf[0]);
371 ASSERT_EQ('0', buf[1]);
372 ASSERT_EQ('1', buf[2]);
373 ASSERT_EQ('2', buf[3]);
374 ASSERT_EQ('3', buf[4]);
375 ASSERT_EQ('4', buf[5]);
376 ASSERT_EQ('5', buf[6]);
377 ASSERT_EQ('6', buf[7]);
378 ASSERT_EQ('7', buf[8]);
379 ASSERT_EQ('\0', buf[9]);
380}
381
Nick Kralevich4f40e512013-04-19 16:54:22 -0700382TEST(string, strchr_with_0) {
383 char buf[10];
384 const char* s = "01234";
385 memcpy(buf, s, strlen(s) + 1);
386 EXPECT_TRUE(strchr(buf, '\0') == (buf + strlen(s)));
387}
388
Anna Tikhonova036154b2012-10-05 15:21:11 +0400389TEST(string, strchr) {
390 int seek_char = random() & 255;
391
Alexander Ivchenkobaa91f42013-06-27 12:55:46 +0400392 StringTestState<char> state(SMALL);
Anna Tikhonova036154b2012-10-05 15:21:11 +0400393 for (size_t i = 1; i < state.n; i++) {
394 for (size_t j = 0; j < POS_ITER; j++) {
395 state.NewIteration();
396
397 if (~seek_char > 0) {
398 memset(state.ptr1, ~seek_char, state.len[i]);
399 } else {
400 memset(state.ptr1, '\1', state.len[i]);
401 }
402 state.ptr1[state.len[i] - 1] = '\0';
403
404 int pos = random() % state.MAX_LEN;
405 char* expected;
406 if (pos >= state.len[i] - 1) {
407 if (seek_char == 0) {
408 expected = state.ptr1 + state.len[i] - 1;
409 } else {
410 expected = NULL;
411 }
412 } else {
413 state.ptr1[pos] = seek_char;
414 expected = state.ptr1 + pos;
415 }
416
417 ASSERT_TRUE(strchr(state.ptr1, seek_char) == expected);
418 }
419 }
420}
421
422TEST(string, strcmp) {
Alexander Ivchenkobaa91f42013-06-27 12:55:46 +0400423 StringTestState<char> state(SMALL);
Anna Tikhonova036154b2012-10-05 15:21:11 +0400424 for (size_t i = 1; i < state.n; i++) {
425 for (size_t j = 0; j < POS_ITER; j++) {
426 state.NewIteration();
427
428 memset(state.ptr1, 'v', state.MAX_LEN);
429 memset(state.ptr2, 'n', state.MAX_LEN);
430 state.ptr1[state.len[i] - 1] = '\0';
431 state.ptr2[state.len[i] - 1] = '\0';
432
433 int pos = 1 + (random() % (state.MAX_LEN - 1));
434 int actual;
435 int expected;
436 if (pos >= state.len[i] - 1) {
437 memcpy(state.ptr1, state.ptr2, state.len[i]);
438 expected = 0;
439 actual = strcmp(state.ptr1, state.ptr2);
440 } else {
441 memcpy(state.ptr1, state.ptr2, pos);
442 if (state.ptr1[pos] > state.ptr2[pos]) {
443 expected = 1;
444 } else if (state.ptr1[pos] == state.ptr2[pos]) {
445 state.ptr1[pos + 1] = '\0';
446 state.ptr2[pos + 1] = '\0';
447 expected = 0;
448 } else {
449 expected = -1;
450 }
451 actual = strcmp(state.ptr1, state.ptr2);
452 }
453
454 ASSERT_EQ(expected, signum(actual));
455 }
456 }
457}
458
459TEST(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
Nick Kralevichdcab1b22013-01-10 17:12:29 -0800485
Anna Tikhonova036154b2012-10-05 15:21:11 +0400486TEST(string, strlcat) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800487#if defined(__BIONIC__)
Alexander Ivchenkobaa91f42013-06-27 12:55:46 +0400488 StringTestState<char> state(SMALL);
Anna Tikhonova036154b2012-10-05 15:21:11 +0400489 for (size_t i = 0; i < state.n; i++) {
490 for (size_t j = 0; j < POS_ITER; j++) {
491 state.NewIteration();
492
493 memset(state.ptr2, '\2', state.MAX_LEN + state.len[i]);
494 state.ptr2[state.MAX_LEN - 1] = '\0';
495 memcpy(state.ptr, state.ptr2, state.MAX_LEN + state.len[i]);
496
497 int pos = random() % state.MAX_LEN;
498 memset(state.ptr1, '\3', pos);
499 state.ptr1[pos] = '\0';
500 if (pos < state.len[i]) {
501 memcpy(state.ptr + state.MAX_LEN - 1, state.ptr1, pos + 1);
502 } else {
503 memcpy(state.ptr + state.MAX_LEN - 1, state.ptr1, state.len[i]);
504 state.ptr[state.MAX_LEN + state.len[i] - 1] = '\0';
505 }
506
507 strlcat(state.ptr2, state.ptr1, state.MAX_LEN + state.len[i]);
508
509 ASSERT_TRUE(memcmp(state.ptr, state.ptr2, state.MAX_LEN + state.len[i]) == 0);
510 }
511 }
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800512#else // __BIONIC__
513 GTEST_LOG_(INFO) << "This test does nothing.\n";
514#endif // __BIONIC__
Anna Tikhonova036154b2012-10-05 15:21:11 +0400515}
Anna Tikhonova036154b2012-10-05 15:21:11 +0400516
Anna Tikhonova036154b2012-10-05 15:21:11 +0400517TEST(string, strlcpy) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800518#if defined(__BIONIC__)
Alexander Ivchenkobaa91f42013-06-27 12:55:46 +0400519 StringTestState<char> state(SMALL);
Anna Tikhonova036154b2012-10-05 15:21:11 +0400520 for (size_t j = 0; j < POS_ITER; j++) {
521 state.NewIteration();
522
523 int rand = random() & 255;
524 if (rand < 1) {
525 rand = 1;
526 }
527 memset(state.ptr1, rand, state.MAX_LEN);
528
529 size_t pos = random() % state.MAX_LEN;
530 if (pos < state.MAX_LEN) {
531 state.ptr1[pos] = '\0';
532 }
533 memcpy(state.ptr, state.ptr1, state.MAX_LEN);
534
535 memset(state.ptr2, random() & 255, state.MAX_LEN);
536 memcpy(state.ptr + state.MAX_LEN, state.ptr2, state.MAX_LEN);
537
538 if (pos > state.MAX_LEN - 1) {
539 memcpy(state.ptr + state.MAX_LEN, state.ptr1, state.MAX_LEN);
540 state.ptr[2 * state.MAX_LEN - 1] = '\0';
541 } else {
542 memcpy(state.ptr + state.MAX_LEN, state.ptr1, pos + 1);
543 }
544
545 ASSERT_EQ(strlcpy(state.ptr2, state.ptr1, state.MAX_LEN), strlen(state.ptr1));
546 ASSERT_FALSE((memcmp(state.ptr1, state.ptr, state.MAX_LEN) != 0) ||
547 (memcmp(state.ptr2, state.ptr + state.MAX_LEN, state.MAX_LEN) != 0));
548 }
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800549#else // __BIONIC__
550 GTEST_LOG_(INFO) << "This test does nothing.\n";
551#endif // __BIONIC__
Anna Tikhonova036154b2012-10-05 15:21:11 +0400552}
Anna Tikhonova036154b2012-10-05 15:21:11 +0400553
554TEST(string, strncat) {
Alexander Ivchenkobaa91f42013-06-27 12:55:46 +0400555 StringTestState<char> state(SMALL);
Anna Tikhonova036154b2012-10-05 15:21:11 +0400556 for (size_t i = 1; i < state.n; i++) {
557 for (size_t j = 0; j < POS_ITER; j++) {
558 state.NewIteration();
559
560 memset(state.ptr2, '\2', state.MAX_LEN);
561 state.ptr2[state.MAX_LEN - 1] = '\0';
562 memcpy(state.ptr, state.ptr2, 2 * state.MAX_LEN);
563
564 memset(state.ptr1, random() & 255, state.len[i]);
565 state.ptr1[random() % state.len[i]] = '\0';
566 state.ptr1[state.len[i] - 1] = '\0';
567
568 size_t pos = strlen(state.ptr1);
569
570 size_t actual = random() % state.len[i];
571 strncpy(state.ptr + state.MAX_LEN - 1, state.ptr1, std::min(actual, pos));
572 state.ptr[state.MAX_LEN + std::min(actual, pos) - 1] = '\0';
573
574 ASSERT_TRUE(strncat(state.ptr2, state.ptr1, actual) == state.ptr2);
575 ASSERT_EQ(memcmp(state.ptr, state.ptr2, 2 * state.MAX_LEN), 0);
576 }
577 }
578}
579
580TEST(string, strncmp) {
Alexander Ivchenkobaa91f42013-06-27 12:55:46 +0400581 StringTestState<char> state(SMALL);
Anna Tikhonova036154b2012-10-05 15:21:11 +0400582 for (size_t i = 1; i < state.n; i++) {
583 for (size_t j = 0; j < POS_ITER; j++) {
584 state.NewIteration();
585
586 memset(state.ptr1, 'v', state.MAX_LEN);
587 memset(state.ptr2, 'n', state.MAX_LEN);
588 state.ptr1[state.len[i] - 1] = '\0';
589 state.ptr2[state.len[i] - 1] = '\0';
590
591 int pos = 1 + (random() % (state.MAX_LEN - 1));
592 int actual;
593 int expected;
594 if (pos >= state.len[i] - 1) {
595 memcpy(state.ptr1, state.ptr2, state.len[i]);
596 expected = 0;
597 actual = strncmp(state.ptr1, state.ptr2, state.len[i]);
598 } else {
599 memcpy(state.ptr1, state.ptr2, pos);
600 if (state.ptr1[pos] > state.ptr2[pos]) {
601 expected = 1;
602 } else if (state.ptr1[pos] == state.ptr2[pos]) {
603 state.ptr1[pos + 1] = '\0';
604 state.ptr2[pos + 1] = '\0';
605 expected = 0;
606 } else {
607 expected = -1;
608 }
609 actual = strncmp(state.ptr1, state.ptr2, state.len[i]);
610 }
611
612 ASSERT_EQ(expected, signum(actual));
613 }
614 }
615}
616
617TEST(string, strncpy) {
Alexander Ivchenkobaa91f42013-06-27 12:55:46 +0400618 StringTestState<char> state(SMALL);
Anna Tikhonova036154b2012-10-05 15:21:11 +0400619 for (size_t j = 0; j < ITER; j++) {
620 state.NewIteration();
621
622 memset(state.ptr1, random() & 255, state.MAX_LEN);
623 state.ptr1[random () % state.MAX_LEN] = '\0';
624 memcpy(state.ptr, state.ptr1, state.MAX_LEN);
625
626 memset(state.ptr2, '\1', state.MAX_LEN);
627
628 size_t pos;
629 if (memchr(state.ptr1, 0, state.MAX_LEN)) {
630 pos = strlen(state.ptr1);
631 } else {
632 pos = state.MAX_LEN - 1;
633 }
634
635 memset(state.ptr + state.MAX_LEN, '\0', state.MAX_LEN);
636 memcpy(state.ptr + state.MAX_LEN, state.ptr1, pos + 1);
637
638 ASSERT_TRUE(strncpy(state.ptr2, state.ptr1, state.MAX_LEN) == state.ptr2);
639 ASSERT_FALSE(memcmp(state.ptr1, state.ptr, state.MAX_LEN) != 0 ||
640 memcmp(state.ptr2, state.ptr + state.MAX_LEN, state.MAX_LEN) != 0);
641 }
642}
643
644TEST(string, strrchr) {
645 int seek_char = random() & 255;
Alexander Ivchenkobaa91f42013-06-27 12:55:46 +0400646 StringTestState<char> state(SMALL);
Anna Tikhonova036154b2012-10-05 15:21:11 +0400647 for (size_t i = 1; i < state.n; i++) {
648 for (size_t j = 0; j < POS_ITER; j++) {
649 state.NewIteration();
650
651 if (~seek_char > 0) {
652 memset(state.ptr1, ~seek_char, state.len[i]);
653 } else {
654 memset(state.ptr1, '\1', state.len[i]);
655 }
656 state.ptr1[state.len[i] - 1] = '\0';
657
658 int pos = random() % state.MAX_LEN;
659 char* expected;
660 if (pos >= state.len[i] - 1) {
661 if (seek_char == 0) {
662 expected = state.ptr1 + state.len[i] - 1;
663 } else {
664 expected = NULL;
665 }
666 } else {
667 state.ptr1[pos] = seek_char;
668 expected = state.ptr1 + pos;
669 }
670
671 ASSERT_TRUE(strrchr(state.ptr1, seek_char) == expected);
672 }
673 }
674}
675
676TEST(string, memchr) {
677 int seek_char = random() & 255;
Alexander Ivchenkobaa91f42013-06-27 12:55:46 +0400678 StringTestState<char> state(SMALL);
Anna Tikhonova036154b2012-10-05 15:21:11 +0400679 for (size_t i = 0; i < state.n; i++) {
680 for (size_t j = 0; j < POS_ITER; j++) {
681 state.NewIteration();
682
683 memset(state.ptr1, ~seek_char, state.len[i]);
684
685 int pos = random() % state.MAX_LEN;
686 char* expected;
687 if (pos >= state.len[i]) {
688 expected = NULL;
689 } else {
690 state.ptr1[pos] = seek_char;
691 expected = state.ptr1 + pos;
692 }
693
694 ASSERT_TRUE(memchr(state.ptr1, seek_char, state.len[i]) == expected);
695 }
696 }
697}
698
699TEST(string, memrchr) {
700 int seek_char = random() & 255;
Alexander Ivchenkobaa91f42013-06-27 12:55:46 +0400701 StringTestState<char> state(SMALL);
Anna Tikhonova036154b2012-10-05 15:21:11 +0400702 for (size_t i = 0; i < state.n; i++) {
703 for (size_t j = 0; j < POS_ITER; j++) {
704 state.NewIteration();
705
706 memset(state.ptr1, ~seek_char, state.len[i]);
707
708 int pos = random() % state.MAX_LEN;
709 char* expected;
710 if (pos >= state.len[i]) {
711 expected = NULL;
712 } else {
713 state.ptr1[pos] = seek_char;
714 expected = state.ptr1 + pos;
715 }
716
717 ASSERT_TRUE(memrchr(state.ptr1, seek_char, state.len[i]) == expected);
718 }
719 }
720}
721
722TEST(string, memcmp) {
Alexander Ivchenkobaa91f42013-06-27 12:55:46 +0400723 StringTestState<char> state(SMALL);
Anna Tikhonova036154b2012-10-05 15:21:11 +0400724 for (size_t i = 0; i < state.n; i++) {
725 for (size_t j = 0; j < POS_ITER; j++) {
726 state.NewIteration();
727
728 int c1 = random() & 0xff;
729 int c2 = random() & 0xff;
730 memset(state.ptr1, c1, state.MAX_LEN);
731 memset(state.ptr2, c1, state.MAX_LEN);
732
733 int pos = (state.len[i] == 0) ? 0 : (random() % state.len[i]);
734 state.ptr2[pos] = c2;
735
736 int expected = (static_cast<int>(c1) - static_cast<int>(c2));
737 int actual = memcmp(state.ptr1, state.ptr2, state.MAX_LEN);
738
739 ASSERT_EQ(signum(expected), signum(actual));
740 }
741 }
742}
743
Alexander Ivchenkobaa91f42013-06-27 12:55:46 +0400744extern "C" int __memcmp16(const unsigned short *ptr1, const unsigned short *ptr2, size_t n);
745
746TEST(string, __memcmp16) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800747#if defined(__BIONIC__)
Alexander Ivchenkobaa91f42013-06-27 12:55:46 +0400748 StringTestState<unsigned short> state(SMALL);
749
750 for (size_t i = 0; i < state.n; i++) {
751 for (size_t j = 0; j < POS_ITER; j++) {
752 state.NewIteration();
753
754 unsigned short mask = 0xffff;
755 unsigned short c1 = rand() & mask;
756 unsigned short c2 = rand() & mask;
757
758 std::fill(state.ptr1, state.ptr1 + state.MAX_LEN, c1);
759 std::fill(state.ptr2, state.ptr2 + state.MAX_LEN, c1);
760
761 int pos = (state.len[i] == 0) ? 0 : (random() % state.len[i]);
762 state.ptr2[pos] = c2;
763
764 int expected = (static_cast<unsigned short>(c1) - static_cast<unsigned short>(c2));
765 int actual = __memcmp16(state.ptr1, state.ptr2, (size_t) state.MAX_LEN);
766
767 ASSERT_EQ(expected, actual);
768 }
769 }
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800770#else // __BIONIC__
771 GTEST_LOG_(INFO) << "This test does nothing.\n";
772#endif // __BIONIC__
Alexander Ivchenkobaa91f42013-06-27 12:55:46 +0400773}
Alexander Ivchenkobaa91f42013-06-27 12:55:46 +0400774
775TEST(string, wmemcmp) {
776 StringTestState<wchar_t> state(SMALL);
777
778 for (size_t i = 0; i < state.n; i++) {
779 for (size_t j = 0; j < POS_ITER; j++) {
780 state.NewIteration();
781
782 long long mask = ((long long) 1 << 8 * sizeof(wchar_t)) - 1;
783 int c1 = rand() & mask;
784 int c2 = rand() & mask;
785 wmemset(state.ptr1, c1, state.MAX_LEN);
786 wmemset(state.ptr2, c1, state.MAX_LEN);
787
788 int pos = (state.len[i] == 0) ? 0 : (random() % state.len[i]);
789 state.ptr2[pos] = c2;
790
791 int expected = (static_cast<int>(c1) - static_cast<int>(c2));
792 int actual = wmemcmp(state.ptr1, state.ptr2, (size_t) state.MAX_LEN);
793
794 ASSERT_EQ(signum(expected), signum(actual));
795 }
796 }
797}
798
Anna Tikhonova036154b2012-10-05 15:21:11 +0400799TEST(string, memcpy) {
Alexander Ivchenkobaa91f42013-06-27 12:55:46 +0400800 StringTestState<char> state(LARGE);
Anna Tikhonova036154b2012-10-05 15:21:11 +0400801 int rand = random() & 255;
802 for (size_t i = 0; i < state.n - 1; i++) {
803 for (size_t j = 0; j < POS_ITER; j++) {
804 state.NewIteration();
805
806 size_t pos = random() % (state.MAX_LEN - state.len[i]);
807
808 memset(state.ptr1, rand, state.len[i]);
809 memset(state.ptr1 + state.len[i], ~rand, state.MAX_LEN - state.len[i]);
810
811 memset(state.ptr2, rand, state.len[i]);
812 memset(state.ptr2 + state.len[i], ~rand, state.MAX_LEN - state.len[i]);
813 memset(state.ptr2 + pos, '\0', state.len[i]);
814
815 ASSERT_FALSE(memcpy(state.ptr2 + pos, state.ptr1 + pos, state.len[i]) != state.ptr2 + pos);
816 ASSERT_EQ(0, memcmp(state.ptr1, state.ptr2, state.MAX_LEN));
817 }
818 }
819}
820
821TEST(string, memset) {
Alexander Ivchenkobaa91f42013-06-27 12:55:46 +0400822 StringTestState<char> state(LARGE);
Anna Tikhonova036154b2012-10-05 15:21:11 +0400823 char ch = random () & 255;
824 for (size_t i = 0; i < state.n - 1; i++) {
825 for (size_t j = 0; j < POS_ITER; j++) {
826 state.NewIteration();
827
828 memset(state.ptr1, ~ch, state.MAX_LEN);
829 memcpy(state.ptr2, state.ptr1, state.MAX_LEN);
830
831 size_t pos = random () % (state.MAX_LEN - state.len[i]);
832 for (size_t k = pos; k < pos + state.len[i]; k++) {
833 state.ptr1[k] = ch;
834 }
835
836 ASSERT_TRUE(memset(state.ptr2 + pos, ch, state.len[i]) == state.ptr2 + pos);
837
838 ASSERT_EQ(0, memcmp(state.ptr1, state.ptr2, state.MAX_LEN));
839 }
840 }
841}
842
843TEST(string, memmove) {
Alexander Ivchenkobaa91f42013-06-27 12:55:46 +0400844 StringTestState<char> state(LARGE);
Anna Tikhonova036154b2012-10-05 15:21:11 +0400845 for (size_t i = 0; i < state.n - 1; i++) {
846 for (size_t j = 0; j < POS_ITER; j++) {
847 state.NewIteration();
848
849 memset(state.ptr1, random() & 255, 2 * state.MAX_LEN);
850
851 size_t pos = random() % (state.MAX_LEN - state.len[i]);
852
853 memset(state.ptr1, random() & 255, state.len[i]);
854 memcpy(state.ptr2, state.ptr1, 2 * state.MAX_LEN);
855 memcpy(state.ptr, state.ptr1, state.len[i]);
856 memcpy(state.ptr1 + pos, state.ptr, state.len[i]);
857
858 ASSERT_TRUE(memmove(state.ptr2 + pos, state.ptr2, state.len[i]) == state.ptr2 + pos);
859 ASSERT_EQ(0, memcmp(state.ptr2, state.ptr1, 2 * state.MAX_LEN));
860 }
861 }
862}
863
864TEST(string, bcopy) {
Alexander Ivchenkobaa91f42013-06-27 12:55:46 +0400865 StringTestState<char> state(LARGE);
Anna Tikhonova036154b2012-10-05 15:21:11 +0400866 for (size_t i = 0; i < state.n; i++) {
867 for (size_t j = 0; j < POS_ITER; j++) {
868 state.NewIteration();
869
870 memset(state.ptr1, random() & 255, state.MAX_LEN);
871 memset(state.ptr1 + state.MAX_LEN, random() & 255, state.MAX_LEN);
872 memcpy(state.ptr2, state.ptr1, 2 * state.MAX_LEN);
873
874 size_t start = random() % (2 * state.MAX_LEN - state.len[i]);
875 memcpy(state.ptr2 + start, state.ptr1, state.len[i]);
876
877 bcopy(state.ptr1, state.ptr1 + start, state.len[i]);
878 ASSERT_EQ(0, memcmp(state.ptr1, state.ptr2, 2 * state.MAX_LEN));
879 }
880 }
881}
882
883TEST(string, bzero) {
Alexander Ivchenkobaa91f42013-06-27 12:55:46 +0400884 StringTestState<char> state(LARGE);
Anna Tikhonova036154b2012-10-05 15:21:11 +0400885 for (size_t j = 0; j < ITER; j++) {
886 state.NewIteration();
887
888 memset(state.ptr1, random() & 255, state.MAX_LEN);
889
890 size_t start = random() % state.MAX_LEN;
891 size_t end = start + random() % (state.MAX_LEN - start);
892
893 memcpy(state.ptr2, state.ptr1, start);
894 memset(state.ptr2 + start, '\0', end - start);
895 memcpy(state.ptr2 + end, state.ptr1 + end, state.MAX_LEN - end);
896
897 bzero(state.ptr1 + start, end - start);
898
899 ASSERT_EQ(0, memcmp(state.ptr1, state.ptr2, state.MAX_LEN));
900 }
901}
Christopher Ferrisb687ad32013-11-06 17:32:11 -0800902
903static void DoMemcpyTest(uint8_t* src, uint8_t* dst, size_t len) {
904 memset(src, (len % 255) + 1, len);
905 memset(dst, 0, len);
906
907 ASSERT_EQ(dst, memcpy(dst, src, len));
908 ASSERT_TRUE(memcmp(src, dst, len) == 0);
909}
910
911TEST(string, memcpy_align) {
912 RunSrcDstBufferAlignTest(LARGE, DoMemcpyTest);
913}
914
915TEST(string, memcpy_overread) {
916 RunSrcDstBufferOverreadTest(DoMemcpyTest);
917}
918
919static void DoMemsetTest(uint8_t* buf, size_t len) {
920 for (size_t i = 0; i < len; i++) {
921 buf[i] = 0;
922 }
923 int value = (len % 255) + 1;
924 ASSERT_EQ(buf, memset(buf, value, len));
925 for (size_t i = 0; i < len; i++) {
926 ASSERT_EQ(value, buf[i]);
927 }
928}
929
930TEST(string, memset_align) {
931 RunSingleBufferAlignTest(LARGE, DoMemsetTest);
932}
933
934static void DoStrlenTest(uint8_t* buf, size_t len) {
935 if (len >= 1) {
936 memset(buf, (32 + (len % 96)), len - 1);
937 buf[len-1] = '\0';
938 ASSERT_EQ(len-1, strlen(reinterpret_cast<char*>(buf)));
939 }
940}
941
942TEST(string, strlen_align) {
943 RunSingleBufferAlignTest(LARGE, DoStrlenTest);
944}
945
946TEST(string, strlen_overread) {
947 RunSingleBufferOverreadTest(DoStrlenTest);
948}
949
950static void DoStrcpyTest(uint8_t* src, uint8_t* dst, size_t len) {
951 if (len >= 1) {
952 memset(src, (32 + (len % 96)), len - 1);
953 src[len-1] = '\0';
954 memset(dst, 0, len);
955 ASSERT_EQ(dst, reinterpret_cast<uint8_t*>(strcpy(reinterpret_cast<char*>(dst),
956 reinterpret_cast<char*>(src))));
957 ASSERT_TRUE(memcmp(src, dst, len) == 0);
958 }
959}
960
961TEST(string, strcpy_align) {
962 RunSrcDstBufferAlignTest(LARGE, DoStrcpyTest);
963}
964
965TEST(string, strcpy_overread) {
966 RunSrcDstBufferOverreadTest(DoStrcpyTest);
967}
968
969// Use our own incrementer to cut down on the total number of calls.
Christopher Ferrise5bbb6b2013-12-03 18:39:10 -0800970static size_t LargeSetIncrement(size_t len) {
Christopher Ferrisb687ad32013-11-06 17:32:11 -0800971 if (len >= 4096) {
972 return 4096;
973 } else if (len >= 1024) {
974 return 1024;
975 } else if (len >= 256) {
976 return 256;
977 }
978 return 1;
979}
980
981#define STRCAT_DST_LEN 128
982
983static void DoStrcatTest(uint8_t* src, uint8_t* dst, size_t len) {
984 if (len >= 1) {
985 int value = 32 + (len % 96);
986 memset(src, value, len - 1);
987 src[len-1] = '\0';
988
989 if (len >= STRCAT_DST_LEN) {
990 // Create a small buffer for doing quick compares in each loop.
991 uint8_t cmp_buf[STRCAT_DST_LEN];
992 // Make sure dst string contains a different value then the src string.
993 int value2 = 32 + (value + 2) % 96;
994 memset(cmp_buf, value2, sizeof(cmp_buf));
995
996 for (size_t i = 1; i <= STRCAT_DST_LEN; i++) {
997 memset(dst, value2, i-1);
998 memset(dst+i-1, 0, len-i);
999 src[len-i] = '\0';
1000 ASSERT_EQ(dst, reinterpret_cast<uint8_t*>(strcat(reinterpret_cast<char*>(dst),
1001 reinterpret_cast<char*>(src))));
1002 ASSERT_TRUE(memcmp(dst, cmp_buf, i-1) == 0);
1003 ASSERT_TRUE(memcmp(src, dst+i-1, len-i+1) == 0);
1004 }
1005 } else {
1006 dst[0] = '\0';
1007 ASSERT_EQ(dst, reinterpret_cast<uint8_t*>(strcat(reinterpret_cast<char*>(dst),
1008 reinterpret_cast<char*>(src))));
1009 ASSERT_TRUE(memcmp(src, dst, len) == 0);
1010 }
1011 }
1012}
1013
1014TEST(string, strcat_align) {
Christopher Ferrise5bbb6b2013-12-03 18:39:10 -08001015 RunSrcDstBufferAlignTest(MEDIUM, DoStrcatTest, LargeSetIncrement);
Christopher Ferrisb687ad32013-11-06 17:32:11 -08001016}
1017
1018TEST(string, strcat_overread) {
1019 RunSrcDstBufferOverreadTest(DoStrcatTest);
1020}
Christopher Ferrise5bbb6b2013-12-03 18:39:10 -08001021
1022static void DoStrcmpTest(uint8_t* buf1, uint8_t* buf2, size_t len) {
1023 if (len >= 1) {
1024 memset(buf1, (32 + (len % 96)), len - 1);
1025 buf1[len-1] = '\0';
1026 memset(buf2, (32 + (len % 96)), len - 1);
1027 buf2[len-1] = '\0';
1028 ASSERT_EQ(0, strcmp(reinterpret_cast<char*>(buf1),
1029 reinterpret_cast<char*>(buf2)));
1030 }
1031}
1032
1033static void DoStrcmpFailTest(uint8_t* buf1, uint8_t* buf2, size_t len1, size_t len2) {
1034 // Do string length differences.
1035 int c = (32 + (len1 % 96));
1036 memset(buf1, c, len1 - 1);
1037 buf1[len1-1] = '\0';
1038 memset(buf2, c, len2 - 1);
1039 buf2[len2-1] = '\0';
1040 ASSERT_NE(0, strcmp(reinterpret_cast<char*>(buf1),
1041 reinterpret_cast<char*>(buf2)));
1042
1043 // Do single character differences.
1044 size_t len;
1045 if (len1 > len2) {
1046 len = len2;
1047 } else {
1048 len = len1;
1049 }
1050 // Need at least a two character buffer to do this test.
1051 if (len > 1) {
1052 buf1[len-1] = '\0';
1053 buf2[len-1] = '\0';
1054 int diff_c = (c + 1) % 96;
1055
1056 buf1[len-2] = diff_c;
1057 ASSERT_NE(0, strcmp(reinterpret_cast<char*>(buf1),
1058 reinterpret_cast<char*>(buf2)));
1059
1060 buf1[len-2] = c;
1061 buf2[len-2] = diff_c;
1062 ASSERT_NE(0, strcmp(reinterpret_cast<char*>(buf1),
1063 reinterpret_cast<char*>(buf2)));
1064 }
1065}
1066
1067TEST(string, strcmp_align) {
1068 RunCmpBufferAlignTest(MEDIUM, DoStrcmpTest, DoStrcmpFailTest, LargeSetIncrement);
1069}
1070
1071TEST(string, strcmp_overread) {
1072 RunCmpBufferOverreadTest(DoStrcmpTest, DoStrcmpFailTest);
1073}
1074
1075static void DoMemcmpTest(uint8_t* buf1, uint8_t* buf2, size_t len) {
1076 memset(buf1, len+1, len);
1077 memset(buf2, len+1, len);
1078 ASSERT_EQ(0, memcmp(buf1, buf2, len));
1079}
1080
1081static void DoMemcmpFailTest(uint8_t* buf1, uint8_t* buf2, size_t len1, size_t len2) {
1082 size_t len;
1083 if (len1 > len2) {
1084 len = len2;
1085 } else {
1086 len = len1;
1087 }
1088
1089 memset(buf1, len2+1, len);
1090 buf1[len-1] = len2;
1091 memset(buf2, len2+1, len);
1092 ASSERT_NE(0, memcmp(buf1, buf2, len));
1093
1094 buf1[len-1] = len2+1;
1095 buf2[len-1] = len2;
1096 ASSERT_NE(0, memcmp(buf1, buf2, len));
1097}
1098
1099TEST(string, memcmp_align) {
1100 RunCmpBufferAlignTest(MEDIUM, DoMemcmpTest, DoMemcmpFailTest, LargeSetIncrement);
1101}
1102
1103TEST(string, memcmp_overread) {
1104 RunCmpBufferOverreadTest(DoMemcmpTest, DoMemcmpFailTest);
1105}