blob: 2e905c5a18fb21ef10b2cdf70d6d630d81727393 [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__
48TEST(Fortify2_DeathTest, strrchr2) {
49 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
50 foo myfoo;
51 memcpy(myfoo.a, "0123456789", 10);
52 memcpy(myfoo.b, "01234", 6);
53 ASSERT_EXIT(printf("%s", strrchr(myfoo.a, 'a')),
54 testing::KilledBySignal(SIGSEGV), "");
55}
56#endif
57
Nick Kralevich78d6d982013-04-29 16:29:37 -070058/***********************************************************/
59/* TESTS BELOW HERE DUPLICATE TESTS FROM fortify1_test.cpp */
60/***********************************************************/
61
Nick Kralevich1aae9bd2013-04-29 14:07:06 -070062#if __BIONIC__
63TEST(Fortify2_DeathTest, strcpy_fortified) {
64 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
65 char buf[10];
66 char *orig = strdup("0123456789");
67 ASSERT_EXIT(strcpy(buf, orig), testing::KilledBySignal(SIGSEGV), "");
68 free(orig);
69}
70
71TEST(Fortify2_DeathTest, strlen_fortified) {
72 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
73 char buf[10];
74 memcpy(buf, "0123456789", sizeof(buf));
75 ASSERT_EXIT(printf("%d", strlen(buf)), testing::KilledBySignal(SIGSEGV), "");
76}
77
78TEST(Fortify2_DeathTest, strchr_fortified) {
79 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
80 char buf[10];
81 memcpy(buf, "0123456789", sizeof(buf));
82 ASSERT_EXIT(printf("%s", strchr(buf, 'a')), testing::KilledBySignal(SIGSEGV), "");
83}
84
85TEST(Fortify2_DeathTest, strrchr_fortified) {
86 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
87 char buf[10];
88 memcpy(buf, "0123456789", sizeof(buf));
89 ASSERT_EXIT(printf("%s", strrchr(buf, 'a')), testing::KilledBySignal(SIGSEGV), "");
90}
91#endif
Nick Kralevich78d6d982013-04-29 16:29:37 -070092
93TEST(Fortify2_DeathTest, sprintf_fortified) {
94 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
95 char buf[10];
96 char source_buf[15];
97 memcpy(source_buf, "12345678901234", 15);
98 ASSERT_EXIT(sprintf(buf, "%s", source_buf), testing::KilledBySignal(SIGSEGV), "");
99}