blob: 97f21c8ec982f183cf0da087488f37d2123caab2 [file] [log] [blame]
Austin Schuhcc6070c2020-10-10 20:25:56 -07001#include "aos/realtime.h"
2
Austin Schuh77f3f222022-06-10 16:49:21 -07003#include "glog/logging.h"
Austin Schuh61226052022-06-20 09:40:08 -07004#include "glog/raw_logging.h"
Austin Schuhcc6070c2020-10-10 20:25:56 -07005#include "gtest/gtest.h"
6
Philipp Schrader790cb542023-07-05 21:06:52 -07007#include "aos/init.h"
8
Austin Schuh77f3f222022-06-10 16:49:21 -07009DECLARE_bool(die_on_malloc);
10
Austin Schuhcc6070c2020-10-10 20:25:56 -070011namespace aos {
12namespace testing {
13
14// Tests that ScopedRealtime handles the simple case.
15TEST(RealtimeTest, ScopedRealtime) {
16 CheckNotRealtime();
17 {
18 ScopedRealtime rt;
19 CheckRealtime();
20 }
21 CheckNotRealtime();
22}
23
24// Tests that ScopedRealtime handles nesting.
25TEST(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.
40TEST(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.
55TEST(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 Schuh34bd4f92022-06-27 16:39:06 -070081// Malloc hooks don't work with asan/msan.
82#if !__has_feature(address_sanitizer) && !__has_feature(memory_sanitizer)
83
Austin Schuh77f3f222022-06-10 16:49:21 -070084// Tests that CHECK statements give real error messages rather than die on
85// malloc.
86TEST(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.
103TEST(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.
114TEST(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 Kuszmaul126dcff2022-08-12 16:30:05 -0700121 "\\*\\*\\* Aborted at .*");
Austin Schuh77f3f222022-06-10 16:49:21 -0700122}
123
Austin Schuh61226052022-06-20 09:40:08 -0700124// Tests that RAW_LOG(FATAL) explodes properly.
125TEST(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 Schuh34bd4f92022-06-27 16:39:06 -0700134#endif
135
Austin Schuhcc6070c2020-10-10 20:25:56 -0700136} // namespace testing
137} // namespace aos
Austin Schuh77f3f222022-06-10 16:49:21 -0700138
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.
141GTEST_API_ int main(int argc, char **argv) {
142 ::testing::InitGoogleTest(&argc, argv);
143 FLAGS_logtostderr = true;
Austin Schuh34bd4f92022-06-27 16:39:06 -0700144
145#if !__has_feature(address_sanitizer) && !__has_feature(memory_sanitizer)
Austin Schuh77f3f222022-06-10 16:49:21 -0700146 FLAGS_die_on_malloc = true;
Austin Schuh34bd4f92022-06-27 16:39:06 -0700147#endif
Austin Schuh77f3f222022-06-10 16:49:21 -0700148
149 aos::InitGoogle(&argc, &argv);
150
151 return RUN_ALL_TESTS();
152}