blob: ea890fe96f2eb1f91f666c702ff34d65a766d6ba [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 Kralevich78d6d982013-04-29 16:29:37 -070067/***********************************************************/
68/* TESTS BELOW HERE DUPLICATE TESTS FROM fortify1_test.cpp */
69/***********************************************************/
70
Nick Kralevich1aae9bd2013-04-29 14:07:06 -070071#if __BIONIC__
72TEST(Fortify2_DeathTest, strcpy_fortified) {
73 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
74 char buf[10];
75 char *orig = strdup("0123456789");
76 ASSERT_EXIT(strcpy(buf, orig), testing::KilledBySignal(SIGSEGV), "");
77 free(orig);
78}
79
80TEST(Fortify2_DeathTest, strlen_fortified) {
81 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
82 char buf[10];
83 memcpy(buf, "0123456789", sizeof(buf));
84 ASSERT_EXIT(printf("%d", strlen(buf)), testing::KilledBySignal(SIGSEGV), "");
85}
86
87TEST(Fortify2_DeathTest, strchr_fortified) {
88 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
89 char buf[10];
90 memcpy(buf, "0123456789", sizeof(buf));
91 ASSERT_EXIT(printf("%s", strchr(buf, 'a')), testing::KilledBySignal(SIGSEGV), "");
92}
93
94TEST(Fortify2_DeathTest, strrchr_fortified) {
95 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
96 char buf[10];
97 memcpy(buf, "0123456789", sizeof(buf));
98 ASSERT_EXIT(printf("%s", strrchr(buf, 'a')), testing::KilledBySignal(SIGSEGV), "");
99}
100#endif
Nick Kralevich78d6d982013-04-29 16:29:37 -0700101
102TEST(Fortify2_DeathTest, sprintf_fortified) {
103 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
104 char buf[10];
105 char source_buf[15];
106 memcpy(source_buf, "12345678901234", 15);
107 ASSERT_EXIT(sprintf(buf, "%s", source_buf), testing::KilledBySignal(SIGSEGV), "");
108}