blob: 0cbb8cf677fa7268b99f6d22678803ce3740be1a [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