blob: 7a607bd2ea70d5244644bc34c3ea785ce21f1c9f [file] [log] [blame]
John Park33858a32018-09-28 23:05:48 -07001#include "aos/libc/aos_strsignal.h"
Brian Silvermanaf784862014-05-13 08:14:55 -07002
Stephan Pleines4b02e7b2024-05-30 20:29:39 -07003#include <string.h>
4
Tyler Chatowbf0609c2021-07-31 16:13:27 -07005#include <csignal>
Stephan Pleines4b02e7b2024-05-30 20:29:39 -07006#include <functional>
7#include <memory>
Austin Schuh9fd93e52014-10-21 22:26:16 -07008#include <thread>
Brian Silvermanaf784862014-05-13 08:14:55 -07009
10#include "gtest/gtest.h"
11
Stephan Pleinesf63bde82024-01-13 15:59:33 -080012namespace aos::libc::testing {
Brian Silvermanaf784862014-05-13 08:14:55 -070013
14// Tries a couple of easy ones.
15TEST(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 Schuh9fd93e52014-10-21 22:26:16 -070022class 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 Kuszmaul89199182023-02-17 14:29:56 -080031// msan doesn't seem to like strsignal().
32#if !__has_feature(memory_sanitizer)
Brian Silvermanaf784862014-05-13 08:14:55 -070033// Tests that all the signals give the same result as strsignal(3).
34TEST(StrsignalTest, All) {
Austin Schuh9fd93e52014-10-21 22:26:16 -070035 // 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 Kuszmaul89199182023-02-17 14:29:56 -080039#if defined(AOS_SANITIZER_thread)
Brian Silverman8d981f92014-12-29 21:38:08 -080040 // 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 Schuh9fd93e52014-10-21 22:26:16 -070044 ::std::thread thread(::std::ref(t));
45 thread.join();
Brian Silverman8d981f92014-12-29 21:38:08 -080046#endif
Brian Silvermanaf784862014-05-13 08:14:55 -070047}
James Kuszmaul89199182023-02-17 14:29:56 -080048#endif
Brian Silvermanaf784862014-05-13 08:14:55 -070049
Stephan Pleinesf63bde82024-01-13 15:59:33 -080050} // namespace aos::libc::testing