blob: a9d8afd64dca180c2b46bbe5da69267fe4651664 [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 1
19
20#include <gtest/gtest.h>
21#include <string.h>
22
23#if __BIONIC__
24// We have to say "DeathTest" here so gtest knows to run this test (which exits)
25// in its own process.
26TEST(Fortify1_DeathTest, strcpy_fortified) {
27 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
28 char buf[10];
29 char *orig = strdup("0123456789");
30 ASSERT_EXIT(strcpy(buf, orig), testing::KilledBySignal(SIGSEGV), "");
31 free(orig);
32}
33
34TEST(Fortify1_DeathTest, strlen_fortified) {
35 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
36 char buf[10];
37 memcpy(buf, "0123456789", sizeof(buf));
38 ASSERT_EXIT(printf("%d", strlen(buf)), testing::KilledBySignal(SIGSEGV), "");
39}
40
41TEST(Fortify1_DeathTest, strchr_fortified) {
42 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
43 char buf[10];
44 memcpy(buf, "0123456789", sizeof(buf));
45 ASSERT_EXIT(printf("%s", strchr(buf, 'a')), testing::KilledBySignal(SIGSEGV), "");
46}
47
48TEST(Fortify1_DeathTest, strrchr_fortified) {
49 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
50 char buf[10];
51 memcpy(buf, "0123456789", sizeof(buf));
52 ASSERT_EXIT(printf("%s", strrchr(buf, 'a')), testing::KilledBySignal(SIGSEGV), "");
53}
54#endif
Nick Kralevich78d6d982013-04-29 16:29:37 -070055
56TEST(Fortify1_DeathTest, sprintf_fortified) {
57 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
58 char buf[10];
59 char source_buf[15];
60 memcpy(source_buf, "12345678901234", 15);
61 ASSERT_EXIT(sprintf(buf, "%s", source_buf), testing::KilledBySignal(SIGSEGV), "");
62}
Nick Kralevich8cc145e2013-05-30 13:21:14 -070063
64TEST(Fortify1_DeathTest, strncat_fortified) {
65 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
66 char buf[10];
67 size_t n = atoi("10"); // avoid compiler optimizations
68 strncpy(buf, "012345678", n);
69 ASSERT_EXIT(strncat(buf, "9", n), testing::KilledBySignal(SIGSEGV), "");
70}
71
72TEST(Fortify1_DeathTest, strncat2_fortified) {
73 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
74 char buf[10];
75 buf[0] = '\0';
76 size_t n = atoi("10"); // avoid compiler optimizations
77 ASSERT_EXIT(strncat(buf, "0123456789", n), testing::KilledBySignal(SIGSEGV), "");
78}
Nick Kralevichcf870192013-05-30 16:48:53 -070079
80TEST(Fortify1_DeathTest, strcat_fortified) {
81 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
82 char src[11];
83 strcpy(src, "0123456789");
84 char buf[10];
85 buf[0] = '\0';
86 ASSERT_EXIT(strcat(buf, src), testing::KilledBySignal(SIGSEGV), "");
87}
88
89extern "C" char* __strncat_chk(char*, const char*, size_t, size_t);
90extern "C" char* __strcat_chk(char*, const char*, size_t);
91
92TEST(Fortify1, strncat) {
93 char buf[10];
94 memset(buf, 'A', sizeof(buf));
95 buf[0] = 'a';
96 buf[1] = '\0';
97 char* res = __strncat_chk(buf, "01234", sizeof(buf) - strlen(buf) - 1, sizeof(buf));
98 ASSERT_EQ(buf, res);
99 ASSERT_EQ('a', buf[0]);
100 ASSERT_EQ('0', buf[1]);
101 ASSERT_EQ('1', buf[2]);
102 ASSERT_EQ('2', buf[3]);
103 ASSERT_EQ('3', buf[4]);
104 ASSERT_EQ('4', buf[5]);
105 ASSERT_EQ('\0', buf[6]);
106 ASSERT_EQ('A', buf[7]);
107 ASSERT_EQ('A', buf[8]);
108 ASSERT_EQ('A', buf[9]);
109}
110
111TEST(Fortify1, strncat2) {
112 char buf[10];
113 memset(buf, 'A', sizeof(buf));
114 buf[0] = 'a';
115 buf[1] = '\0';
116 char* res = __strncat_chk(buf, "0123456789", 5, sizeof(buf));
117 ASSERT_EQ(buf, res);
118 ASSERT_EQ('a', buf[0]);
119 ASSERT_EQ('0', buf[1]);
120 ASSERT_EQ('1', buf[2]);
121 ASSERT_EQ('2', buf[3]);
122 ASSERT_EQ('3', buf[4]);
123 ASSERT_EQ('4', buf[5]);
124 ASSERT_EQ('\0', buf[6]);
125 ASSERT_EQ('A', buf[7]);
126 ASSERT_EQ('A', buf[8]);
127 ASSERT_EQ('A', buf[9]);
128}
129
130TEST(Fortify1, strncat3) {
131 char buf[10];
132 memset(buf, 'A', sizeof(buf));
133 buf[0] = '\0';
134 char* res = __strncat_chk(buf, "0123456789", 5, sizeof(buf));
135 ASSERT_EQ(buf, res);
136 ASSERT_EQ('0', buf[0]);
137 ASSERT_EQ('1', buf[1]);
138 ASSERT_EQ('2', buf[2]);
139 ASSERT_EQ('3', buf[3]);
140 ASSERT_EQ('4', buf[4]);
141 ASSERT_EQ('\0', buf[5]);
142 ASSERT_EQ('A', buf[6]);
143 ASSERT_EQ('A', buf[7]);
144 ASSERT_EQ('A', buf[8]);
145 ASSERT_EQ('A', buf[9]);
146}
147
148TEST(Fortify1, strncat4) {
149 char buf[10];
150 memset(buf, 'A', sizeof(buf));
151 buf[9] = '\0';
152 char* res = __strncat_chk(buf, "", 5, sizeof(buf));
153 ASSERT_EQ(buf, res);
154 ASSERT_EQ('A', buf[0]);
155 ASSERT_EQ('A', buf[1]);
156 ASSERT_EQ('A', buf[2]);
157 ASSERT_EQ('A', buf[3]);
158 ASSERT_EQ('A', buf[4]);
159 ASSERT_EQ('A', buf[5]);
160 ASSERT_EQ('A', buf[6]);
161 ASSERT_EQ('A', buf[7]);
162 ASSERT_EQ('A', buf[8]);
163 ASSERT_EQ('\0', buf[9]);
164}
165
166TEST(Fortify1, strncat5) {
167 char buf[10];
168 memset(buf, 'A', sizeof(buf));
169 buf[0] = 'a';
170 buf[1] = '\0';
171 char* res = __strncat_chk(buf, "01234567", 8, sizeof(buf));
172 ASSERT_EQ(buf, res);
173 ASSERT_EQ('a', buf[0]);
174 ASSERT_EQ('0', buf[1]);
175 ASSERT_EQ('1', buf[2]);
176 ASSERT_EQ('2', buf[3]);
177 ASSERT_EQ('3', buf[4]);
178 ASSERT_EQ('4', buf[5]);
179 ASSERT_EQ('5', buf[6]);
180 ASSERT_EQ('6', buf[7]);
181 ASSERT_EQ('7', buf[8]);
182 ASSERT_EQ('\0', buf[9]);
183}
184
185TEST(Fortify1, strncat6) {
186 char buf[10];
187 memset(buf, 'A', sizeof(buf));
188 buf[0] = 'a';
189 buf[1] = '\0';
190 char* res = __strncat_chk(buf, "01234567", 9, sizeof(buf));
191 ASSERT_EQ(buf, res);
192 ASSERT_EQ('a', buf[0]);
193 ASSERT_EQ('0', buf[1]);
194 ASSERT_EQ('1', buf[2]);
195 ASSERT_EQ('2', buf[3]);
196 ASSERT_EQ('3', buf[4]);
197 ASSERT_EQ('4', buf[5]);
198 ASSERT_EQ('5', buf[6]);
199 ASSERT_EQ('6', buf[7]);
200 ASSERT_EQ('7', buf[8]);
201 ASSERT_EQ('\0', buf[9]);
202}
203
204
205TEST(Fortify1, strcat) {
206 char buf[10];
207 memset(buf, 'A', sizeof(buf));
208 buf[0] = 'a';
209 buf[1] = '\0';
210 char* res = __strcat_chk(buf, "01234", sizeof(buf));
211 ASSERT_EQ(buf, res);
212 ASSERT_EQ('a', buf[0]);
213 ASSERT_EQ('0', buf[1]);
214 ASSERT_EQ('1', buf[2]);
215 ASSERT_EQ('2', buf[3]);
216 ASSERT_EQ('3', buf[4]);
217 ASSERT_EQ('4', buf[5]);
218 ASSERT_EQ('\0', buf[6]);
219 ASSERT_EQ('A', buf[7]);
220 ASSERT_EQ('A', buf[8]);
221 ASSERT_EQ('A', buf[9]);
222}
223
224TEST(Fortify1, strcat2) {
225 char buf[10];
226 memset(buf, 'A', sizeof(buf));
227 buf[0] = 'a';
228 buf[1] = '\0';
229 char* res = __strcat_chk(buf, "01234567", sizeof(buf));
230 ASSERT_EQ(buf, res);
231 ASSERT_EQ('a', buf[0]);
232 ASSERT_EQ('0', buf[1]);
233 ASSERT_EQ('1', buf[2]);
234 ASSERT_EQ('2', buf[3]);
235 ASSERT_EQ('3', buf[4]);
236 ASSERT_EQ('4', buf[5]);
237 ASSERT_EQ('5', buf[6]);
238 ASSERT_EQ('6', buf[7]);
239 ASSERT_EQ('7', buf[8]);
240 ASSERT_EQ('\0', buf[9]);
241}