fortify_test.cpp revision 60f4f9a5b99a0a66817f50edfc2194a49f8b5146
1/*
2 * Copyright (C) 2013 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#include <string.h>
19#include <stdarg.h>
20#include <sys/types.h>
21#include <sys/stat.h>
22#include <sys/socket.h>
23
24// We have to say "DeathTest" here so gtest knows to run this test (which exits)
25// in its own process. Unfortunately, the C preprocessor doesn't give us an
26// easy way to concatenate strings, so we need to use the complicated method
27// below. *sigh*
28#define DEATHTEST_PASTER(name) name##_DeathTest
29#define DEATHTEST_EVALUATOR(name) DEATHTEST_PASTER(name)
30#define DEATHTEST DEATHTEST_EVALUATOR(TEST_NAME)
31
32#if defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE == 2
33struct foo {
34  char empty[0];
35  char one[1];
36  char a[10];
37  char b[10];
38};
39
40#ifndef __clang__
41// This test is disabled in clang because clang doesn't properly detect
42// this buffer overflow. TODO: Fix clang.
43TEST(DEATHTEST, strncpy_fortified2) {
44  ::testing::FLAGS_gtest_death_test_style = "threadsafe";
45  foo myfoo;
46  int copy_amt = atoi("11");
47  ASSERT_EXIT(strncpy(myfoo.a, "01234567890", copy_amt),
48              testing::KilledBySignal(SIGABRT), "");
49}
50#endif
51
52#ifndef __clang__
53// This test is disabled in clang because clang doesn't properly detect
54// this buffer overflow. TODO: Fix clang.
55TEST(DEATHTEST, strncpy2_fortified2) {
56  ::testing::FLAGS_gtest_death_test_style = "threadsafe";
57  foo myfoo;
58  memset(&myfoo, 0, sizeof(myfoo));
59  myfoo.one[0] = 'A'; // not null terminated string
60  ASSERT_EXIT(strncpy(myfoo.b, myfoo.one, sizeof(myfoo.b)),
61              testing::KilledBySignal(SIGABRT), "");
62}
63#endif
64
65#ifndef __clang__
66// This test is disabled in clang because clang doesn't properly detect
67// this buffer overflow. TODO: Fix clang.
68TEST(DEATHTEST, sprintf_fortified2) {
69  ::testing::FLAGS_gtest_death_test_style = "threadsafe";
70  foo myfoo;
71  char source_buf[15];
72  memcpy(source_buf, "12345678901234", 15);
73  ASSERT_EXIT(sprintf(myfoo.a, "%s", source_buf),
74              testing::KilledBySignal(SIGABRT), "");
75}
76#endif
77
78#ifndef __clang__
79// This test is disabled in clang because clang doesn't properly detect
80// this buffer overflow. TODO: Fix clang.
81TEST(DEATHTEST, sprintf2_fortified2) {
82  ::testing::FLAGS_gtest_death_test_style = "threadsafe";
83  foo myfoo;
84  ASSERT_EXIT(sprintf(myfoo.a, "0123456789"),
85              testing::KilledBySignal(SIGABRT), "");
86}
87#endif
88
89#ifndef __clang__
90// These tests are disabled in clang because clang doesn't properly detect
91// this buffer overflow. TODO: Fix clang.
92static int vsprintf_helper2(const char *fmt, ...) {
93  foo myfoo;
94  va_list va;
95  int result;
96
97  va_start(va, fmt);
98  result = vsprintf(myfoo.a, fmt, va); // should crash here
99  va_end(va);
100  return result;
101}
102
103TEST(DEATHTEST, vsprintf_fortified2) {
104  ::testing::FLAGS_gtest_death_test_style = "threadsafe";
105  ASSERT_EXIT(vsprintf_helper2("%s", "0123456789"), testing::KilledBySignal(SIGABRT), "");
106}
107
108TEST(DEATHTEST, vsprintf2_fortified2) {
109  ::testing::FLAGS_gtest_death_test_style = "threadsafe";
110  ASSERT_EXIT(vsprintf_helper2("0123456789"), testing::KilledBySignal(SIGABRT), "");
111}
112#endif
113
114#ifndef __clang__
115// These tests are disabled in clang because clang doesn't properly detect
116// this buffer overflow. TODO: Fix clang.
117static int vsnprintf_helper2(const char *fmt, ...) {
118  foo myfoo;
119  va_list va;
120  int result;
121  size_t size = atoi("11");
122
123  va_start(va, fmt);
124  result = vsnprintf(myfoo.a, size, fmt, va); // should crash here
125  va_end(va);
126  return result;
127}
128
129TEST(DEATHTEST, vsnprintf_fortified2) {
130  ::testing::FLAGS_gtest_death_test_style = "threadsafe";
131  ASSERT_EXIT(vsnprintf_helper2("%s", "0123456789"), testing::KilledBySignal(SIGABRT), "");
132}
133
134TEST(DEATHTEST, vsnprintf2_fortified2) {
135  ::testing::FLAGS_gtest_death_test_style = "threadsafe";
136  ASSERT_EXIT(vsnprintf_helper2("0123456789"), testing::KilledBySignal(SIGABRT), "");
137}
138#endif
139
140#if __BIONIC__
141
142#ifndef __clang__
143// zero sized target with "\0" source (should fail)
144// This test is disabled in clang because clang doesn't properly detect
145// this buffer overflow. TODO: Fix clang.
146TEST(DEATHTEST, strcpy_fortified2) {
147  ::testing::FLAGS_gtest_death_test_style = "threadsafe";
148  foo myfoo;
149  char* src = strdup("");
150  ASSERT_EXIT(strcpy(myfoo.empty, src),
151              testing::KilledBySignal(SIGABRT), "");
152  free(src);
153}
154#endif
155
156#ifndef __clang__
157// zero sized target with longer source (should fail)
158// This test is disabled in clang because clang doesn't properly detect
159// this buffer overflow. TODO: Fix clang.
160TEST(DEATHTEST, strcpy2_fortified2) {
161  ::testing::FLAGS_gtest_death_test_style = "threadsafe";
162  foo myfoo;
163  char* src = strdup("1");
164  ASSERT_EXIT(strcpy(myfoo.empty, src),
165              testing::KilledBySignal(SIGABRT), "");
166  free(src);
167}
168#endif
169
170#ifndef __clang__
171// one byte target with longer source (should fail)
172// This test is disabled in clang because clang doesn't properly detect
173// this buffer overflow. TODO: Fix clang.
174TEST(DEATHTEST, strcpy3_fortified2) {
175  ::testing::FLAGS_gtest_death_test_style = "threadsafe";
176  foo myfoo;
177  char* src = strdup("12");
178  ASSERT_EXIT(strcpy(myfoo.one, src),
179              testing::KilledBySignal(SIGABRT), "");
180  free(src);
181}
182#endif
183
184#ifndef __clang__
185// This test is disabled in clang because clang doesn't properly detect
186// this buffer overflow. TODO: Fix clang.
187TEST(DEATHTEST, strchr_fortified2) {
188  ::testing::FLAGS_gtest_death_test_style = "threadsafe";
189  foo myfoo;
190  memcpy(myfoo.a, "0123456789", sizeof(myfoo.a));
191  myfoo.b[0] = '\0';
192  ASSERT_EXIT(printf("%s", strchr(myfoo.a, 'a')),
193              testing::KilledBySignal(SIGABRT), "");
194}
195#endif
196
197#ifndef __clang__
198// This test is disabled in clang because clang doesn't properly detect
199// this buffer overflow. TODO: Fix clang.
200TEST(DEATHTEST, strrchr_fortified2) {
201  ::testing::FLAGS_gtest_death_test_style = "threadsafe";
202  foo myfoo;
203  memcpy(myfoo.a, "0123456789", 10);
204  memcpy(myfoo.b, "01234", 6);
205  ASSERT_EXIT(printf("%s", strrchr(myfoo.a, 'a')),
206              testing::KilledBySignal(SIGABRT), "");
207}
208#endif
209
210#ifndef __clang__
211// This test is disabled in clang because clang doesn't properly detect
212// this buffer overflow. TODO: Fix clang.
213TEST(DEATHTEST, strlcpy_fortified2) {
214  ::testing::FLAGS_gtest_death_test_style = "threadsafe";
215  foo myfoo;
216  strcpy(myfoo.a, "01");
217  size_t n = strlen(myfoo.a);
218  ASSERT_EXIT(strlcpy(myfoo.one, myfoo.a, n),
219              testing::KilledBySignal(SIGABRT), "");
220}
221#endif
222
223#ifndef __clang__
224// This test is disabled in clang because clang doesn't properly detect
225// this buffer overflow. TODO: Fix clang.
226TEST(DEATHTEST, strlcat_fortified2) {
227  ::testing::FLAGS_gtest_death_test_style = "threadsafe";
228  foo myfoo;
229  strcpy(myfoo.a, "01");
230  myfoo.one[0] = '\0';
231  size_t n = strlen(myfoo.a);
232  ASSERT_EXIT(strlcat(myfoo.one, myfoo.a, n),
233              testing::KilledBySignal(SIGABRT), "");
234}
235#endif
236
237#endif /* __BIONIC__ */
238
239#ifndef __clang__
240// This test is disabled in clang because clang doesn't properly detect
241// this buffer overflow. TODO: Fix clang.
242TEST(DEATHTEST, strncat_fortified2) {
243  ::testing::FLAGS_gtest_death_test_style = "threadsafe";
244  foo myfoo;
245  size_t n = atoi("10"); // avoid compiler optimizations
246  strncpy(myfoo.a, "012345678", n);
247  ASSERT_EXIT(strncat(myfoo.a, "9", n), testing::KilledBySignal(SIGABRT), "");
248}
249#endif
250
251#ifndef __clang__
252// This test is disabled in clang because clang doesn't properly detect
253// this buffer overflow. TODO: Fix clang.
254TEST(DEATHTEST, strncat2_fortified2) {
255  ::testing::FLAGS_gtest_death_test_style = "threadsafe";
256  foo myfoo;
257  myfoo.a[0] = '\0';
258  size_t n = atoi("10"); // avoid compiler optimizations
259  ASSERT_EXIT(strncat(myfoo.a, "0123456789", n), testing::KilledBySignal(SIGABRT), "");
260}
261#endif
262
263TEST(DEATHTEST, strncat3_fortified2) {
264  ::testing::FLAGS_gtest_death_test_style = "threadsafe";
265  foo myfoo;
266  memcpy(myfoo.a, "0123456789", sizeof(myfoo.a)); // unterminated string
267  myfoo.b[0] = '\0';
268  size_t n = atoi("10"); // avoid compiler optimizations
269  ASSERT_EXIT(strncat(myfoo.b, myfoo.a, n), testing::KilledBySignal(SIGABRT), "");
270}
271
272#ifndef __clang__
273// This test is disabled in clang because clang doesn't properly detect
274// this buffer overflow. TODO: Fix clang.
275TEST(DEATHTEST, strcat_fortified2) {
276  ::testing::FLAGS_gtest_death_test_style = "threadsafe";
277  char src[11];
278  strcpy(src, "0123456789");
279  foo myfoo;
280  myfoo.a[0] = '\0';
281  ASSERT_EXIT(strcat(myfoo.a, src), testing::KilledBySignal(SIGABRT), "");
282}
283#endif
284
285TEST(DEATHTEST, strcat2_fortified2) {
286  ::testing::FLAGS_gtest_death_test_style = "threadsafe";
287  foo myfoo;
288  memcpy(myfoo.a, "0123456789", sizeof(myfoo.a)); // unterminated string
289  myfoo.b[0] = '\0';
290  ASSERT_EXIT(strcat(myfoo.b, myfoo.a), testing::KilledBySignal(SIGABRT), "");
291}
292
293TEST(DEATHTEST, snprintf_fortified2) {
294  ::testing::FLAGS_gtest_death_test_style = "threadsafe";
295  foo myfoo;
296  strcpy(myfoo.a, "012345678");
297  size_t n = strlen(myfoo.a) + 2;
298  ASSERT_EXIT(snprintf(myfoo.b, n, "a%s", myfoo.a), testing::KilledBySignal(SIGABRT), "");
299}
300
301TEST(DEATHTEST, bzero_fortified2) {
302  ::testing::FLAGS_gtest_death_test_style = "threadsafe";
303  foo myfoo;
304  memcpy(myfoo.b, "0123456789", sizeof(myfoo.b));
305  size_t n = atoi("11");
306  ASSERT_EXIT(bzero(myfoo.b, n), testing::KilledBySignal(SIGABRT), "");
307}
308
309#endif /* defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE=2 */
310
311#if __BIONIC__
312// multibyte target where we over fill (should fail)
313TEST(DEATHTEST, strcpy_fortified) {
314  ::testing::FLAGS_gtest_death_test_style = "threadsafe";
315  char buf[10];
316  char *orig = strdup("0123456789");
317  ASSERT_EXIT(strcpy(buf, orig), testing::KilledBySignal(SIGABRT), "");
318  free(orig);
319}
320
321// zero sized target with "\0" source (should fail)
322TEST(DEATHTEST, strcpy2_fortified) {
323  ::testing::FLAGS_gtest_death_test_style = "threadsafe";
324  char buf[0];
325  char *orig = strdup("");
326  ASSERT_EXIT(strcpy(buf, orig), testing::KilledBySignal(SIGABRT), "");
327  free(orig);
328}
329
330// zero sized target with longer source (should fail)
331TEST(DEATHTEST, strcpy3_fortified) {
332  ::testing::FLAGS_gtest_death_test_style = "threadsafe";
333  char buf[0];
334  char *orig = strdup("1");
335  ASSERT_EXIT(strcpy(buf, orig), testing::KilledBySignal(SIGABRT), "");
336  free(orig);
337}
338
339// one byte target with longer source (should fail)
340TEST(DEATHTEST, strcpy4_fortified) {
341  ::testing::FLAGS_gtest_death_test_style = "threadsafe";
342  char buf[1];
343  char *orig = strdup("12");
344  ASSERT_EXIT(strcpy(buf, orig), testing::KilledBySignal(SIGABRT), "");
345  free(orig);
346}
347
348TEST(DEATHTEST, strlen_fortified) {
349  ::testing::FLAGS_gtest_death_test_style = "threadsafe";
350  char buf[10];
351  memcpy(buf, "0123456789", sizeof(buf));
352  ASSERT_EXIT(printf("%d", strlen(buf)), testing::KilledBySignal(SIGABRT), "");
353}
354
355TEST(DEATHTEST, strchr_fortified) {
356  ::testing::FLAGS_gtest_death_test_style = "threadsafe";
357  char buf[10];
358  memcpy(buf, "0123456789", sizeof(buf));
359  ASSERT_EXIT(printf("%s", strchr(buf, 'a')), testing::KilledBySignal(SIGABRT), "");
360}
361
362TEST(DEATHTEST, strrchr_fortified) {
363  ::testing::FLAGS_gtest_death_test_style = "threadsafe";
364  char buf[10];
365  memcpy(buf, "0123456789", sizeof(buf));
366  ASSERT_EXIT(printf("%s", strrchr(buf, 'a')), testing::KilledBySignal(SIGABRT), "");
367}
368
369TEST(DEATHTEST, strlcpy_fortified) {
370  ::testing::FLAGS_gtest_death_test_style = "threadsafe";
371  char bufa[15];
372  char bufb[10];
373  strcpy(bufa, "01234567890123");
374  size_t n = strlen(bufa);
375  ASSERT_EXIT(strlcpy(bufb, bufa, n), testing::KilledBySignal(SIGABRT), "");
376}
377
378TEST(DEATHTEST, strlcat_fortified) {
379  ::testing::FLAGS_gtest_death_test_style = "threadsafe";
380  char bufa[15];
381  char bufb[10];
382  bufb[0] = '\0';
383  strcpy(bufa, "01234567890123");
384  size_t n = strlen(bufa);
385  ASSERT_EXIT(strlcat(bufb, bufa, n), testing::KilledBySignal(SIGABRT), "");
386}
387
388#endif
389
390TEST(DEATHTEST, sprintf_fortified) {
391  ::testing::FLAGS_gtest_death_test_style = "threadsafe";
392  char buf[10];
393  char source_buf[15];
394  memcpy(source_buf, "12345678901234", 15);
395  ASSERT_EXIT(sprintf(buf, "%s", source_buf), testing::KilledBySignal(SIGABRT), "");
396}
397
398TEST(DEATHTEST, sprintf2_fortified) {
399  ::testing::FLAGS_gtest_death_test_style = "threadsafe";
400  char buf[5];
401  ASSERT_EXIT(sprintf(buf, "aaaaa"), testing::KilledBySignal(SIGABRT), "");
402}
403
404static int vsprintf_helper(const char *fmt, ...) {
405  char buf[10];
406  va_list va;
407  int result;
408
409  va_start(va, fmt);
410  result = vsprintf(buf, fmt, va); // should crash here
411  va_end(va);
412  return result;
413}
414
415TEST(DEATHTEST, vsprintf_fortified) {
416  ::testing::FLAGS_gtest_death_test_style = "threadsafe";
417  ASSERT_EXIT(vsprintf_helper("%s", "0123456789"), testing::KilledBySignal(SIGABRT), "");
418}
419
420TEST(DEATHTEST, vsprintf2_fortified) {
421  ::testing::FLAGS_gtest_death_test_style = "threadsafe";
422  ASSERT_EXIT(vsprintf_helper("0123456789"), testing::KilledBySignal(SIGABRT), "");
423}
424
425static int vsnprintf_helper(const char *fmt, ...) {
426  char buf[10];
427  va_list va;
428  int result;
429  size_t size = atoi("11");
430
431  va_start(va, fmt);
432  result = vsnprintf(buf, size, fmt, va); // should crash here
433  va_end(va);
434  return result;
435}
436
437TEST(DEATHTEST, vsnprintf_fortified) {
438  ::testing::FLAGS_gtest_death_test_style = "threadsafe";
439  ASSERT_EXIT(vsnprintf_helper("%s", "0123456789"), testing::KilledBySignal(SIGABRT), "");
440}
441
442TEST(DEATHTEST, vsnprintf2_fortified) {
443  ::testing::FLAGS_gtest_death_test_style = "threadsafe";
444  ASSERT_EXIT(vsnprintf_helper("0123456789"), testing::KilledBySignal(SIGABRT), "");
445}
446
447TEST(DEATHTEST, strncat_fortified) {
448  ::testing::FLAGS_gtest_death_test_style = "threadsafe";
449  char buf[10];
450  size_t n = atoi("10"); // avoid compiler optimizations
451  strncpy(buf, "012345678", n);
452  ASSERT_EXIT(strncat(buf, "9", n), testing::KilledBySignal(SIGABRT), "");
453}
454
455TEST(DEATHTEST, strncat2_fortified) {
456  ::testing::FLAGS_gtest_death_test_style = "threadsafe";
457  char buf[10];
458  buf[0] = '\0';
459  size_t n = atoi("10"); // avoid compiler optimizations
460  ASSERT_EXIT(strncat(buf, "0123456789", n), testing::KilledBySignal(SIGABRT), "");
461}
462
463TEST(DEATHTEST, strcat_fortified) {
464  ::testing::FLAGS_gtest_death_test_style = "threadsafe";
465  char src[11];
466  strcpy(src, "0123456789");
467  char buf[10];
468  buf[0] = '\0';
469  ASSERT_EXIT(strcat(buf, src), testing::KilledBySignal(SIGABRT), "");
470}
471
472TEST(DEATHTEST, memmove_fortified) {
473  ::testing::FLAGS_gtest_death_test_style = "threadsafe";
474  char buf[20];
475  strcpy(buf, "0123456789");
476  size_t n = atoi("10");
477  ASSERT_EXIT(memmove(buf + 11, buf, n), testing::KilledBySignal(SIGABRT), "");
478}
479
480TEST(DEATHTEST, memcpy_fortified) {
481  ::testing::FLAGS_gtest_death_test_style = "threadsafe";
482  char bufa[10];
483  char bufb[10];
484  strcpy(bufa, "012345678");
485  size_t n = atoi("11");
486  ASSERT_EXIT(memcpy(bufb, bufa, n), testing::KilledBySignal(SIGABRT), "");
487}
488
489TEST(DEATHTEST, strncpy_fortified) {
490  ::testing::FLAGS_gtest_death_test_style = "threadsafe";
491  char bufa[15];
492  char bufb[10];
493  strcpy(bufa, "01234567890123");
494  size_t n = strlen(bufa);
495  ASSERT_EXIT(strncpy(bufb, bufa, n), testing::KilledBySignal(SIGABRT), "");
496}
497
498TEST(DEATHTEST, strncpy2_fortified) {
499  ::testing::FLAGS_gtest_death_test_style = "threadsafe";
500  char dest[11];
501  char src[10];
502  memcpy(src, "0123456789", sizeof(src)); // src is not null terminated
503  ASSERT_EXIT(strncpy(dest, src, sizeof(dest)), testing::KilledBySignal(SIGABRT), "");
504}
505
506TEST(DEATHTEST, snprintf_fortified) {
507  ::testing::FLAGS_gtest_death_test_style = "threadsafe";
508  char bufa[15];
509  char bufb[10];
510  strcpy(bufa, "0123456789");
511  size_t n = strlen(bufa) + 1;
512  ASSERT_EXIT(snprintf(bufb, n, "%s", bufa), testing::KilledBySignal(SIGABRT), "");
513}
514
515TEST(DEATHTEST, bzero_fortified) {
516  ::testing::FLAGS_gtest_death_test_style = "threadsafe";
517  char buf[10];
518  memcpy(buf, "0123456789", sizeof(buf));
519  size_t n = atoi("11");
520  ASSERT_EXIT(bzero(buf, n), testing::KilledBySignal(SIGABRT), "");
521}
522
523TEST(DEATHTEST, umask_fortified) {
524  ::testing::FLAGS_gtest_death_test_style = "threadsafe";
525  mode_t mask = atoi("1023");  // 01777 in octal
526  ASSERT_EXIT(umask(mask), testing::KilledBySignal(SIGABRT), "");
527}
528
529TEST(DEATHTEST, recv_fortified) {
530  ::testing::FLAGS_gtest_death_test_style = "threadsafe";
531  size_t data_len = atoi("11"); // suppress compiler optimizations
532  char buf[10];
533  ASSERT_EXIT(recv(0, buf, data_len, 0), testing::KilledBySignal(SIGABRT), "");
534}
535
536extern "C" char* __strncat_chk(char*, const char*, size_t, size_t);
537extern "C" char* __strcat_chk(char*, const char*, size_t);
538
539TEST(TEST_NAME, strncat) {
540  char buf[10];
541  memset(buf, 'A', sizeof(buf));
542  buf[0] = 'a';
543  buf[1] = '\0';
544  char* res = __strncat_chk(buf, "01234", sizeof(buf) - strlen(buf) - 1, sizeof(buf));
545  ASSERT_EQ(buf, res);
546  ASSERT_EQ('a',  buf[0]);
547  ASSERT_EQ('0',  buf[1]);
548  ASSERT_EQ('1',  buf[2]);
549  ASSERT_EQ('2',  buf[3]);
550  ASSERT_EQ('3',  buf[4]);
551  ASSERT_EQ('4',  buf[5]);
552  ASSERT_EQ('\0', buf[6]);
553  ASSERT_EQ('A',  buf[7]);
554  ASSERT_EQ('A',  buf[8]);
555  ASSERT_EQ('A',  buf[9]);
556}
557
558TEST(TEST_NAME, strncat2) {
559  char buf[10];
560  memset(buf, 'A', sizeof(buf));
561  buf[0] = 'a';
562  buf[1] = '\0';
563  char* res = __strncat_chk(buf, "0123456789", 5, sizeof(buf));
564  ASSERT_EQ(buf, res);
565  ASSERT_EQ('a',  buf[0]);
566  ASSERT_EQ('0',  buf[1]);
567  ASSERT_EQ('1',  buf[2]);
568  ASSERT_EQ('2',  buf[3]);
569  ASSERT_EQ('3',  buf[4]);
570  ASSERT_EQ('4',  buf[5]);
571  ASSERT_EQ('\0', buf[6]);
572  ASSERT_EQ('A',  buf[7]);
573  ASSERT_EQ('A',  buf[8]);
574  ASSERT_EQ('A',  buf[9]);
575}
576
577TEST(TEST_NAME, strncat3) {
578  char buf[10];
579  memset(buf, 'A', sizeof(buf));
580  buf[0] = '\0';
581  char* res = __strncat_chk(buf, "0123456789", 5, sizeof(buf));
582  ASSERT_EQ(buf, res);
583  ASSERT_EQ('0',  buf[0]);
584  ASSERT_EQ('1',  buf[1]);
585  ASSERT_EQ('2',  buf[2]);
586  ASSERT_EQ('3',  buf[3]);
587  ASSERT_EQ('4',  buf[4]);
588  ASSERT_EQ('\0', buf[5]);
589  ASSERT_EQ('A',  buf[6]);
590  ASSERT_EQ('A',  buf[7]);
591  ASSERT_EQ('A',  buf[8]);
592  ASSERT_EQ('A',  buf[9]);
593}
594
595TEST(TEST_NAME, strncat4) {
596  char buf[10];
597  memset(buf, 'A', sizeof(buf));
598  buf[9] = '\0';
599  char* res = __strncat_chk(buf, "", 5, sizeof(buf));
600  ASSERT_EQ(buf, res);
601  ASSERT_EQ('A',  buf[0]);
602  ASSERT_EQ('A',  buf[1]);
603  ASSERT_EQ('A',  buf[2]);
604  ASSERT_EQ('A',  buf[3]);
605  ASSERT_EQ('A',  buf[4]);
606  ASSERT_EQ('A',  buf[5]);
607  ASSERT_EQ('A',  buf[6]);
608  ASSERT_EQ('A',  buf[7]);
609  ASSERT_EQ('A',  buf[8]);
610  ASSERT_EQ('\0', buf[9]);
611}
612
613TEST(TEST_NAME, strncat5) {
614  char buf[10];
615  memset(buf, 'A', sizeof(buf));
616  buf[0] = 'a';
617  buf[1] = '\0';
618  char* res = __strncat_chk(buf, "01234567", 8, sizeof(buf));
619  ASSERT_EQ(buf, res);
620  ASSERT_EQ('a',  buf[0]);
621  ASSERT_EQ('0',  buf[1]);
622  ASSERT_EQ('1',  buf[2]);
623  ASSERT_EQ('2',  buf[3]);
624  ASSERT_EQ('3',  buf[4]);
625  ASSERT_EQ('4',  buf[5]);
626  ASSERT_EQ('5', buf[6]);
627  ASSERT_EQ('6',  buf[7]);
628  ASSERT_EQ('7',  buf[8]);
629  ASSERT_EQ('\0',  buf[9]);
630}
631
632TEST(TEST_NAME, strncat6) {
633  char buf[10];
634  memset(buf, 'A', sizeof(buf));
635  buf[0] = 'a';
636  buf[1] = '\0';
637  char* res = __strncat_chk(buf, "01234567", 9, sizeof(buf));
638  ASSERT_EQ(buf, res);
639  ASSERT_EQ('a',  buf[0]);
640  ASSERT_EQ('0',  buf[1]);
641  ASSERT_EQ('1',  buf[2]);
642  ASSERT_EQ('2',  buf[3]);
643  ASSERT_EQ('3',  buf[4]);
644  ASSERT_EQ('4',  buf[5]);
645  ASSERT_EQ('5', buf[6]);
646  ASSERT_EQ('6',  buf[7]);
647  ASSERT_EQ('7',  buf[8]);
648  ASSERT_EQ('\0',  buf[9]);
649}
650
651
652TEST(TEST_NAME, strcat) {
653  char buf[10];
654  memset(buf, 'A', sizeof(buf));
655  buf[0] = 'a';
656  buf[1] = '\0';
657  char* res = __strcat_chk(buf, "01234", sizeof(buf));
658  ASSERT_EQ(buf, res);
659  ASSERT_EQ('a',  buf[0]);
660  ASSERT_EQ('0',  buf[1]);
661  ASSERT_EQ('1',  buf[2]);
662  ASSERT_EQ('2',  buf[3]);
663  ASSERT_EQ('3',  buf[4]);
664  ASSERT_EQ('4',  buf[5]);
665  ASSERT_EQ('\0', buf[6]);
666  ASSERT_EQ('A',  buf[7]);
667  ASSERT_EQ('A',  buf[8]);
668  ASSERT_EQ('A',  buf[9]);
669}
670
671TEST(TEST_NAME, strcat2) {
672  char buf[10];
673  memset(buf, 'A', sizeof(buf));
674  buf[0] = 'a';
675  buf[1] = '\0';
676  char* res = __strcat_chk(buf, "01234567", sizeof(buf));
677  ASSERT_EQ(buf, res);
678  ASSERT_EQ('a',  buf[0]);
679  ASSERT_EQ('0',  buf[1]);
680  ASSERT_EQ('1',  buf[2]);
681  ASSERT_EQ('2',  buf[3]);
682  ASSERT_EQ('3',  buf[4]);
683  ASSERT_EQ('4',  buf[5]);
684  ASSERT_EQ('5', buf[6]);
685  ASSERT_EQ('6',  buf[7]);
686  ASSERT_EQ('7',  buf[8]);
687  ASSERT_EQ('\0',  buf[9]);
688}
689
690TEST(TEST_NAME, strncpy) {
691  char src[10];
692  char dst[10];
693  memcpy(src, "0123456789", sizeof(src)); // non null terminated string
694  strncpy(dst, src, sizeof(dst));
695  ASSERT_EQ('0', dst[0]);
696  ASSERT_EQ('1', dst[1]);
697  ASSERT_EQ('2', dst[2]);
698  ASSERT_EQ('3', dst[3]);
699  ASSERT_EQ('4', dst[4]);
700  ASSERT_EQ('5', dst[5]);
701  ASSERT_EQ('6', dst[6]);
702  ASSERT_EQ('7', dst[7]);
703  ASSERT_EQ('8', dst[8]);
704  ASSERT_EQ('9', dst[9]);
705}
706
707TEST(TEST_NAME, strncpy2) {
708  char src[10];
709  char dst[15];
710  memcpy(src, "012345678\0", sizeof(src));
711  strncpy(dst, src, sizeof(dst));
712  ASSERT_EQ('0',  dst[0]);
713  ASSERT_EQ('1',  dst[1]);
714  ASSERT_EQ('2',  dst[2]);
715  ASSERT_EQ('3',  dst[3]);
716  ASSERT_EQ('4',  dst[4]);
717  ASSERT_EQ('5',  dst[5]);
718  ASSERT_EQ('6',  dst[6]);
719  ASSERT_EQ('7',  dst[7]);
720  ASSERT_EQ('8',  dst[8]);
721  ASSERT_EQ('\0', dst[9]);
722  ASSERT_EQ('\0', dst[10]);
723  ASSERT_EQ('\0', dst[11]);
724  ASSERT_EQ('\0', dst[12]);
725  ASSERT_EQ('\0', dst[13]);
726  ASSERT_EQ('\0', dst[14]);
727}
728
729TEST(TEST_NAME, strcat_chk_max_int_size) {
730  char buf[10];
731  memset(buf, 'A', sizeof(buf));
732  buf[0] = 'a';
733  buf[1] = '\0';
734  char* res = __strcat_chk(buf, "01234567", (size_t)-1);
735  ASSERT_EQ(buf, res);
736  ASSERT_EQ('a',  buf[0]);
737  ASSERT_EQ('0',  buf[1]);
738  ASSERT_EQ('1',  buf[2]);
739  ASSERT_EQ('2',  buf[3]);
740  ASSERT_EQ('3',  buf[4]);
741  ASSERT_EQ('4',  buf[5]);
742  ASSERT_EQ('5',  buf[6]);
743  ASSERT_EQ('6',  buf[7]);
744  ASSERT_EQ('7',  buf[8]);
745  ASSERT_EQ('\0', buf[9]);
746}
747
748extern "C" char* __strcpy_chk(char*, const char*, size_t);
749
750TEST(TEST_NAME, strcpy_chk_max_int_size) {
751  char buf[10];
752  char* res = __strcpy_chk(buf, "012345678", (size_t)-1);
753  ASSERT_EQ(buf, res);
754  ASSERT_EQ('0',  buf[0]);
755  ASSERT_EQ('1',  buf[1]);
756  ASSERT_EQ('2',  buf[2]);
757  ASSERT_EQ('3',  buf[3]);
758  ASSERT_EQ('4',  buf[4]);
759  ASSERT_EQ('5',  buf[5]);
760  ASSERT_EQ('6',  buf[6]);
761  ASSERT_EQ('7',  buf[7]);
762  ASSERT_EQ('8',  buf[8]);
763  ASSERT_EQ('\0', buf[9]);
764}
765
766extern "C" void* __memcpy_chk(void*, const void*, size_t, size_t);
767
768TEST(TEST_NAME, memcpy_chk_max_int_size) {
769  char buf[10];
770  void* res = __memcpy_chk(buf, "012345678", sizeof(buf), (size_t)-1);
771  ASSERT_EQ((void*)buf, res);
772  ASSERT_EQ('0',  buf[0]);
773  ASSERT_EQ('1',  buf[1]);
774  ASSERT_EQ('2',  buf[2]);
775  ASSERT_EQ('3',  buf[3]);
776  ASSERT_EQ('4',  buf[4]);
777  ASSERT_EQ('5',  buf[5]);
778  ASSERT_EQ('6',  buf[6]);
779  ASSERT_EQ('7',  buf[7]);
780  ASSERT_EQ('8',  buf[8]);
781  ASSERT_EQ('\0', buf[9]);
782}
783