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