Nick Kralevich | 1aae9bd | 2013-04-29 14:07:06 -0700 | [diff] [blame] | 1 | /* |
| 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. |
| 26 | TEST(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 | |
| 34 | TEST(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 | |
| 41 | TEST(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 | |
| 48 | TEST(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 Kralevich | 78d6d98 | 2013-04-29 16:29:37 -0700 | [diff] [blame] | 55 | |
| 56 | TEST(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 Kralevich | 8cc145e | 2013-05-30 13:21:14 -0700 | [diff] [blame^] | 63 | |
| 64 | TEST(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 | |
| 72 | TEST(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 | } |