blob: c937e91e76ca5e80b0d309616e8a5181d078240c [file] [log] [blame]
Nick Kralevich1aae9bd2013-04-29 14:07:06 -07001/*
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#undef _FORTIFY_SOURCE
18#define _FORTIFY_SOURCE 2
19
20#include <gtest/gtest.h>
21#include <string.h>
22
23struct foo {
24 char a[10];
25 char b[10];
26};
27
28// We have to say "DeathTest" here so gtest knows to run this test (which exits)
29// in its own process.
Nick Kralevich78d6d982013-04-29 16:29:37 -070030TEST(Fortify2_DeathTest, strncpy_fortified2) {
Nick Kralevich1aae9bd2013-04-29 14:07:06 -070031 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
32 foo myfoo;
33 int copy_amt = atoi("11");
34 ASSERT_EXIT(strncpy(myfoo.a, "01234567890", copy_amt),
35 testing::KilledBySignal(SIGSEGV), "");
36}
37
Nick Kralevich78d6d982013-04-29 16:29:37 -070038TEST(Fortify2_DeathTest, sprintf_fortified2) {
39 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
40 foo myfoo;
41 char source_buf[15];
42 memcpy(source_buf, "12345678901234", 15);
43 ASSERT_EXIT(sprintf(myfoo.a, "%s", source_buf),
44 testing::KilledBySignal(SIGSEGV), "");
45}
46
Nick Kralevich80541922013-05-01 14:55:33 -070047#if __BIONIC__
Nick Kralevich4f40e512013-04-19 16:54:22 -070048TEST(Fortify2_DeathTest, strchr_fortified2) {
49 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
50 foo myfoo;
51 memcpy(myfoo.a, "0123456789", sizeof(myfoo.a));
52 myfoo.b[0] = '\0';
53 ASSERT_EXIT(printf("%s", strchr(myfoo.a, 'a')),
54 testing::KilledBySignal(SIGSEGV), "");
55}
56
Nick Kralevich277226b2013-05-01 15:05:01 -070057TEST(Fortify2_DeathTest, strrchr_fortified2) {
Nick Kralevich80541922013-05-01 14:55:33 -070058 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
59 foo myfoo;
60 memcpy(myfoo.a, "0123456789", 10);
61 memcpy(myfoo.b, "01234", 6);
62 ASSERT_EXIT(printf("%s", strrchr(myfoo.a, 'a')),
63 testing::KilledBySignal(SIGSEGV), "");
64}
65#endif
66
Nick Kralevich8cc145e2013-05-30 13:21:14 -070067TEST(Fortify2_DeathTest, strncat_fortified2) {
68 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
69 foo myfoo;
70 size_t n = atoi("10"); // avoid compiler optimizations
71 strncpy(myfoo.a, "012345678", n);
72 ASSERT_EXIT(strncat(myfoo.a, "9", n), testing::KilledBySignal(SIGSEGV), "");
73}
74
75TEST(Fortify2_DeathTest, strncat2_fortified2) {
76 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
77 foo myfoo;
78 myfoo.a[0] = '\0';
79 size_t n = atoi("10"); // avoid compiler optimizations
80 ASSERT_EXIT(strncat(myfoo.a, "0123456789", n), testing::KilledBySignal(SIGSEGV), "");
81}
82
Nick Kralevich78d6d982013-04-29 16:29:37 -070083/***********************************************************/
84/* TESTS BELOW HERE DUPLICATE TESTS FROM fortify1_test.cpp */
85/***********************************************************/
86
Nick Kralevich1aae9bd2013-04-29 14:07:06 -070087#if __BIONIC__
88TEST(Fortify2_DeathTest, strcpy_fortified) {
89 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
90 char buf[10];
91 char *orig = strdup("0123456789");
92 ASSERT_EXIT(strcpy(buf, orig), testing::KilledBySignal(SIGSEGV), "");
93 free(orig);
94}
95
96TEST(Fortify2_DeathTest, strlen_fortified) {
97 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
98 char buf[10];
99 memcpy(buf, "0123456789", sizeof(buf));
100 ASSERT_EXIT(printf("%d", strlen(buf)), testing::KilledBySignal(SIGSEGV), "");
101}
102
103TEST(Fortify2_DeathTest, strchr_fortified) {
104 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
105 char buf[10];
106 memcpy(buf, "0123456789", sizeof(buf));
107 ASSERT_EXIT(printf("%s", strchr(buf, 'a')), testing::KilledBySignal(SIGSEGV), "");
108}
109
110TEST(Fortify2_DeathTest, strrchr_fortified) {
111 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
112 char buf[10];
113 memcpy(buf, "0123456789", sizeof(buf));
114 ASSERT_EXIT(printf("%s", strrchr(buf, 'a')), testing::KilledBySignal(SIGSEGV), "");
115}
116#endif
Nick Kralevich78d6d982013-04-29 16:29:37 -0700117
118TEST(Fortify2_DeathTest, sprintf_fortified) {
119 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
120 char buf[10];
121 char source_buf[15];
122 memcpy(source_buf, "12345678901234", 15);
123 ASSERT_EXIT(sprintf(buf, "%s", source_buf), testing::KilledBySignal(SIGSEGV), "");
124}
Nick Kralevich8cc145e2013-05-30 13:21:14 -0700125
126TEST(Fortify2_DeathTest, strncat_fortified) {
127 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
128 char buf[10];
129 size_t n = atoi("10"); // avoid compiler optimizations
130 strncpy(buf, "012345678", n);
131 ASSERT_EXIT(strncat(buf, "9", n), testing::KilledBySignal(SIGSEGV), "");
132}
133
134TEST(Fortify2_DeathTest, strncat2_fortified) {
135 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
136 char buf[10];
137 buf[0] = '\0';
138 size_t n = atoi("10"); // avoid compiler optimizations
139 ASSERT_EXIT(strncat(buf, "0123456789", n), testing::KilledBySignal(SIGSEGV), "");
140}