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 2 |
| 19 | |
| 20 | #include <gtest/gtest.h> |
| 21 | #include <string.h> |
| 22 | |
| 23 | struct 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 Kralevich | 78d6d98 | 2013-04-29 16:29:37 -0700 | [diff] [blame] | 30 | TEST(Fortify2_DeathTest, strncpy_fortified2) { |
Nick Kralevich | 1aae9bd | 2013-04-29 14:07:06 -0700 | [diff] [blame] | 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 | |
Nick Kralevich | 78d6d98 | 2013-04-29 16:29:37 -0700 | [diff] [blame] | 38 | TEST(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 Kralevich | 8054192 | 2013-05-01 14:55:33 -0700 | [diff] [blame] | 47 | #if __BIONIC__ |
Nick Kralevich | 4f40e51 | 2013-04-19 16:54:22 -0700 | [diff] [blame] | 48 | TEST(Fortify2_DeathTest, strchr_fortified2) { |
| 49 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 50 | foo myfoo; |
| 51 | memcpy(myfoo.a, "0123456789", sizeof(myfoo.a)); |
| 52 | myfoo.b[0] = '\0'; |
| 53 | ASSERT_EXIT(printf("%s", strchr(myfoo.a, 'a')), |
| 54 | testing::KilledBySignal(SIGSEGV), ""); |
| 55 | } |
| 56 | |
Nick Kralevich | 277226b | 2013-05-01 15:05:01 -0700 | [diff] [blame] | 57 | TEST(Fortify2_DeathTest, strrchr_fortified2) { |
Nick Kralevich | 8054192 | 2013-05-01 14:55:33 -0700 | [diff] [blame] | 58 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 59 | foo myfoo; |
| 60 | memcpy(myfoo.a, "0123456789", 10); |
| 61 | memcpy(myfoo.b, "01234", 6); |
| 62 | ASSERT_EXIT(printf("%s", strrchr(myfoo.a, 'a')), |
| 63 | testing::KilledBySignal(SIGSEGV), ""); |
| 64 | } |
| 65 | #endif |
| 66 | |
Nick Kralevich | 78d6d98 | 2013-04-29 16:29:37 -0700 | [diff] [blame] | 67 | /***********************************************************/ |
| 68 | /* TESTS BELOW HERE DUPLICATE TESTS FROM fortify1_test.cpp */ |
| 69 | /***********************************************************/ |
| 70 | |
Nick Kralevich | 1aae9bd | 2013-04-29 14:07:06 -0700 | [diff] [blame] | 71 | #if __BIONIC__ |
| 72 | TEST(Fortify2_DeathTest, strcpy_fortified) { |
| 73 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 74 | char buf[10]; |
| 75 | char *orig = strdup("0123456789"); |
| 76 | ASSERT_EXIT(strcpy(buf, orig), testing::KilledBySignal(SIGSEGV), ""); |
| 77 | free(orig); |
| 78 | } |
| 79 | |
| 80 | TEST(Fortify2_DeathTest, strlen_fortified) { |
| 81 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 82 | char buf[10]; |
| 83 | memcpy(buf, "0123456789", sizeof(buf)); |
| 84 | ASSERT_EXIT(printf("%d", strlen(buf)), testing::KilledBySignal(SIGSEGV), ""); |
| 85 | } |
| 86 | |
| 87 | TEST(Fortify2_DeathTest, strchr_fortified) { |
| 88 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 89 | char buf[10]; |
| 90 | memcpy(buf, "0123456789", sizeof(buf)); |
| 91 | ASSERT_EXIT(printf("%s", strchr(buf, 'a')), testing::KilledBySignal(SIGSEGV), ""); |
| 92 | } |
| 93 | |
| 94 | TEST(Fortify2_DeathTest, strrchr_fortified) { |
| 95 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 96 | char buf[10]; |
| 97 | memcpy(buf, "0123456789", sizeof(buf)); |
| 98 | ASSERT_EXIT(printf("%s", strrchr(buf, 'a')), testing::KilledBySignal(SIGSEGV), ""); |
| 99 | } |
| 100 | #endif |
Nick Kralevich | 78d6d98 | 2013-04-29 16:29:37 -0700 | [diff] [blame] | 101 | |
| 102 | TEST(Fortify2_DeathTest, sprintf_fortified) { |
| 103 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 104 | char buf[10]; |
| 105 | char source_buf[15]; |
| 106 | memcpy(source_buf, "12345678901234", 15); |
| 107 | ASSERT_EXIT(sprintf(buf, "%s", source_buf), testing::KilledBySignal(SIGSEGV), ""); |
| 108 | } |