Austin Schuh | cc6070c | 2020-10-10 20:25:56 -0700 | [diff] [blame] | 1 | #include "aos/realtime.h" |
| 2 | |
Austin Schuh | 77f3f22 | 2022-06-10 16:49:21 -0700 | [diff] [blame] | 3 | #include "glog/logging.h" |
Austin Schuh | 6122605 | 2022-06-20 09:40:08 -0700 | [diff] [blame] | 4 | #include "glog/raw_logging.h" |
Austin Schuh | cc6070c | 2020-10-10 20:25:56 -0700 | [diff] [blame] | 5 | #include "gtest/gtest.h" |
| 6 | |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame^] | 7 | #include "aos/init.h" |
| 8 | |
Austin Schuh | 77f3f22 | 2022-06-10 16:49:21 -0700 | [diff] [blame] | 9 | DECLARE_bool(die_on_malloc); |
| 10 | |
Austin Schuh | cc6070c | 2020-10-10 20:25:56 -0700 | [diff] [blame] | 11 | namespace aos { |
| 12 | namespace testing { |
| 13 | |
| 14 | // Tests that ScopedRealtime handles the simple case. |
| 15 | TEST(RealtimeTest, ScopedRealtime) { |
| 16 | CheckNotRealtime(); |
| 17 | { |
| 18 | ScopedRealtime rt; |
| 19 | CheckRealtime(); |
| 20 | } |
| 21 | CheckNotRealtime(); |
| 22 | } |
| 23 | |
| 24 | // Tests that ScopedRealtime handles nesting. |
| 25 | TEST(RealtimeTest, DoubleScopedRealtime) { |
| 26 | CheckNotRealtime(); |
| 27 | { |
| 28 | ScopedRealtime rt; |
| 29 | CheckRealtime(); |
| 30 | { |
| 31 | ScopedRealtime rt2; |
| 32 | CheckRealtime(); |
| 33 | } |
| 34 | CheckRealtime(); |
| 35 | } |
| 36 | CheckNotRealtime(); |
| 37 | } |
| 38 | |
| 39 | // Tests that ScopedRealtime handles nesting with ScopedNotRealtime. |
| 40 | TEST(RealtimeTest, ScopedNotRealtime) { |
| 41 | CheckNotRealtime(); |
| 42 | { |
| 43 | ScopedRealtime rt; |
| 44 | CheckRealtime(); |
| 45 | { |
| 46 | ScopedNotRealtime nrt; |
| 47 | CheckNotRealtime(); |
| 48 | } |
| 49 | CheckRealtime(); |
| 50 | } |
| 51 | CheckNotRealtime(); |
| 52 | } |
| 53 | |
| 54 | // Tests that ScopedRealtimeRestorer works both when starting RT and nonrt. |
| 55 | TEST(RealtimeTest, ScopedRealtimeRestorer) { |
| 56 | CheckNotRealtime(); |
| 57 | { |
| 58 | ScopedRealtime rt; |
| 59 | CheckRealtime(); |
| 60 | { |
| 61 | ScopedRealtimeRestorer restore; |
| 62 | CheckRealtime(); |
| 63 | |
| 64 | MarkRealtime(false); |
| 65 | CheckNotRealtime(); |
| 66 | } |
| 67 | CheckRealtime(); |
| 68 | } |
| 69 | CheckNotRealtime(); |
| 70 | |
| 71 | { |
| 72 | ScopedRealtimeRestorer restore; |
| 73 | CheckNotRealtime(); |
| 74 | |
| 75 | MarkRealtime(true); |
| 76 | CheckRealtime(); |
| 77 | } |
| 78 | CheckNotRealtime(); |
| 79 | } |
| 80 | |
Austin Schuh | 34bd4f9 | 2022-06-27 16:39:06 -0700 | [diff] [blame] | 81 | // Malloc hooks don't work with asan/msan. |
| 82 | #if !__has_feature(address_sanitizer) && !__has_feature(memory_sanitizer) |
| 83 | |
Austin Schuh | 77f3f22 | 2022-06-10 16:49:21 -0700 | [diff] [blame] | 84 | // Tests that CHECK statements give real error messages rather than die on |
| 85 | // malloc. |
| 86 | TEST(RealtimeDeathTest, Check) { |
| 87 | EXPECT_DEATH( |
| 88 | { |
| 89 | ScopedRealtime rt; |
| 90 | CHECK_EQ(1, 2) << ": Numbers aren't equal."; |
| 91 | }, |
| 92 | "Numbers aren't equal"); |
| 93 | EXPECT_DEATH( |
| 94 | { |
| 95 | ScopedRealtime rt; |
| 96 | CHECK_GT(1, 2) << ": Cute error message"; |
| 97 | }, |
| 98 | "Cute error message"); |
| 99 | } |
| 100 | |
| 101 | // Tests that CHECK statements give real error messages rather than die on |
| 102 | // malloc. |
| 103 | TEST(RealtimeDeathTest, Fatal) { |
| 104 | EXPECT_DEATH( |
| 105 | { |
| 106 | ScopedRealtime rt; |
| 107 | LOG(FATAL) << "Cute message here"; |
| 108 | }, |
| 109 | "Cute message here"); |
| 110 | } |
| 111 | |
| 112 | // Tests that the signal handler drops RT permission and prints out a real |
| 113 | // backtrace instead of crashing on the resulting mallocs. |
| 114 | TEST(RealtimeDeathTest, SignalHandler) { |
| 115 | EXPECT_DEATH( |
| 116 | { |
| 117 | ScopedRealtime rt; |
| 118 | int x = reinterpret_cast<const volatile int *>(0)[0]; |
| 119 | LOG(INFO) << x; |
| 120 | }, |
James Kuszmaul | 126dcff | 2022-08-12 16:30:05 -0700 | [diff] [blame] | 121 | "\\*\\*\\* Aborted at .*"); |
Austin Schuh | 77f3f22 | 2022-06-10 16:49:21 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Austin Schuh | 6122605 | 2022-06-20 09:40:08 -0700 | [diff] [blame] | 124 | // Tests that RAW_LOG(FATAL) explodes properly. |
| 125 | TEST(RealtimeDeathTest, RawFatal) { |
| 126 | EXPECT_DEATH( |
| 127 | { |
| 128 | ScopedRealtime rt; |
| 129 | RAW_LOG(FATAL, "Cute message here\n"); |
| 130 | }, |
| 131 | "Cute message here"); |
| 132 | } |
| 133 | |
Austin Schuh | 34bd4f9 | 2022-06-27 16:39:06 -0700 | [diff] [blame] | 134 | #endif |
| 135 | |
Austin Schuh | cc6070c | 2020-10-10 20:25:56 -0700 | [diff] [blame] | 136 | } // namespace testing |
| 137 | } // namespace aos |
Austin Schuh | 77f3f22 | 2022-06-10 16:49:21 -0700 | [diff] [blame] | 138 | |
| 139 | // We need a special gtest main to force die_on_malloc support on. Otherwise |
| 140 | // we can't test CHECK statements before turning die_on_malloc on globally. |
| 141 | GTEST_API_ int main(int argc, char **argv) { |
| 142 | ::testing::InitGoogleTest(&argc, argv); |
| 143 | FLAGS_logtostderr = true; |
Austin Schuh | 34bd4f9 | 2022-06-27 16:39:06 -0700 | [diff] [blame] | 144 | |
| 145 | #if !__has_feature(address_sanitizer) && !__has_feature(memory_sanitizer) |
Austin Schuh | 77f3f22 | 2022-06-10 16:49:21 -0700 | [diff] [blame] | 146 | FLAGS_die_on_malloc = true; |
Austin Schuh | 34bd4f9 | 2022-06-27 16:39:06 -0700 | [diff] [blame] | 147 | #endif |
Austin Schuh | 77f3f22 | 2022-06-10 16:49:21 -0700 | [diff] [blame] | 148 | |
| 149 | aos::InitGoogle(&argc, &argv); |
| 150 | |
| 151 | return RUN_ALL_TESTS(); |
| 152 | } |