blob: 70d458a0879a28e0037d71c07e953ff23f311116 [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}