John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 1 | #include "aos/libc/aos_strsignal.h" |
Brian Silverman | af78486 | 2014-05-13 08:14:55 -0700 | [diff] [blame] | 2 | |
Stephan Pleines | 4b02e7b | 2024-05-30 20:29:39 -0700 | [diff] [blame^] | 3 | #include <string.h> |
| 4 | |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 5 | #include <csignal> |
Stephan Pleines | 4b02e7b | 2024-05-30 20:29:39 -0700 | [diff] [blame^] | 6 | #include <functional> |
| 7 | #include <memory> |
Austin Schuh | 9fd93e5 | 2014-10-21 22:26:16 -0700 | [diff] [blame] | 8 | #include <thread> |
Brian Silverman | af78486 | 2014-05-13 08:14:55 -0700 | [diff] [blame] | 9 | |
| 10 | #include "gtest/gtest.h" |
| 11 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 12 | namespace aos::libc::testing { |
Brian Silverman | af78486 | 2014-05-13 08:14:55 -0700 | [diff] [blame] | 13 | |
| 14 | // Tries a couple of easy ones. |
| 15 | TEST(StrsignalTest, Basic) { |
| 16 | EXPECT_STREQ("Hangup", aos_strsignal(SIGHUP)); |
| 17 | EXPECT_STREQ("Broken pipe", aos_strsignal(SIGPIPE)); |
| 18 | EXPECT_STREQ("Real-time signal 2", aos_strsignal(SIGRTMIN + 2)); |
| 19 | EXPECT_STREQ("Unknown signal 155", aos_strsignal(155)); |
| 20 | } |
| 21 | |
Austin Schuh | 9fd93e5 | 2014-10-21 22:26:16 -0700 | [diff] [blame] | 22 | class SignalNameTester { |
| 23 | public: |
| 24 | void operator()() { |
| 25 | for (int i = 0; i < SIGRTMAX + 5; ++i) { |
| 26 | EXPECT_STREQ(strsignal(i), aos_strsignal(i)); |
| 27 | } |
| 28 | } |
| 29 | }; |
| 30 | |
James Kuszmaul | 8919918 | 2023-02-17 14:29:56 -0800 | [diff] [blame] | 31 | // msan doesn't seem to like strsignal(). |
| 32 | #if !__has_feature(memory_sanitizer) |
Brian Silverman | af78486 | 2014-05-13 08:14:55 -0700 | [diff] [blame] | 33 | // Tests that all the signals give the same result as strsignal(3). |
| 34 | TEST(StrsignalTest, All) { |
Austin Schuh | 9fd93e5 | 2014-10-21 22:26:16 -0700 | [diff] [blame] | 35 | // Sigh, strsignal allocates a buffer that uses pthread local storage. This |
| 36 | // interacts poorly with asan. Spawning a thread causes the storage to get |
| 37 | // cleaned up before asan checks. |
| 38 | SignalNameTester t; |
James Kuszmaul | 8919918 | 2023-02-17 14:29:56 -0800 | [diff] [blame] | 39 | #if defined(AOS_SANITIZER_thread) |
Brian Silverman | 8d981f9 | 2014-12-29 21:38:08 -0800 | [diff] [blame] | 40 | // tsan doesn't like this usage of ::std::thread. It looks like |
| 41 | // <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=57507>. |
| 42 | t(); |
| 43 | #else |
Austin Schuh | 9fd93e5 | 2014-10-21 22:26:16 -0700 | [diff] [blame] | 44 | ::std::thread thread(::std::ref(t)); |
| 45 | thread.join(); |
Brian Silverman | 8d981f9 | 2014-12-29 21:38:08 -0800 | [diff] [blame] | 46 | #endif |
Brian Silverman | af78486 | 2014-05-13 08:14:55 -0700 | [diff] [blame] | 47 | } |
James Kuszmaul | 8919918 | 2023-02-17 14:29:56 -0800 | [diff] [blame] | 48 | #endif |
Brian Silverman | af78486 | 2014-05-13 08:14:55 -0700 | [diff] [blame] | 49 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 50 | } // namespace aos::libc::testing |