blob: 9bedbe5047d82162919ee4898d8fc791ff3fbd7c [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.
30TEST(Fortify2_DeathTest, strncpy_fortified) {
31 ::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
38#if __BIONIC__
39TEST(Fortify2_DeathTest, strcpy_fortified) {
40 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
41 char buf[10];
42 char *orig = strdup("0123456789");
43 ASSERT_EXIT(strcpy(buf, orig), testing::KilledBySignal(SIGSEGV), "");
44 free(orig);
45}
46
47TEST(Fortify2_DeathTest, strlen_fortified) {
48 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
49 char buf[10];
50 memcpy(buf, "0123456789", sizeof(buf));
51 ASSERT_EXIT(printf("%d", strlen(buf)), testing::KilledBySignal(SIGSEGV), "");
52}
53
54TEST(Fortify2_DeathTest, strchr_fortified) {
55 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
56 char buf[10];
57 memcpy(buf, "0123456789", sizeof(buf));
58 ASSERT_EXIT(printf("%s", strchr(buf, 'a')), testing::KilledBySignal(SIGSEGV), "");
59}
60
61TEST(Fortify2_DeathTest, strrchr_fortified) {
62 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
63 char buf[10];
64 memcpy(buf, "0123456789", sizeof(buf));
65 ASSERT_EXIT(printf("%s", strrchr(buf, 'a')), testing::KilledBySignal(SIGSEGV), "");
66}
67#endif