• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <gtest/gtest.h>
17 #include "platforms/unix/libpandabase/signal.h"
18 
19 namespace panda::os::unix {
20 
operator ==(const::panda::os::unix::SignalCtl & l,const::panda::os::unix::SignalCtl & r)21 bool operator==(const ::panda::os::unix::SignalCtl &l, const ::panda::os::unix::SignalCtl &r)
22 {
23     return memcmp(&l, &r, sizeof(l)) == 0;
24 }
25 
operator !=(const::panda::os::unix::SignalCtl & l,const::panda::os::unix::SignalCtl & r)26 bool operator!=(const ::panda::os::unix::SignalCtl &l, const ::panda::os::unix::SignalCtl &r)
27 {
28     return !(l == r);
29 }
30 
31 }  // namespace panda::os::unix
32 
33 namespace panda::test {
34 
35 class UnixSignal : public ::testing::Test {
36 protected:
SetUpTestSuite()37     static void SetUpTestSuite() {}
38 
SetUp()39     void SetUp() override
40     {
41         os::unix::SignalCtl::GetCurrent(signal_ctl_);
42         sig_action_count_ = 0;
43     }
44 
TearDown()45     void TearDown() override
46     {
47         os::unix::SignalCtl signal_ctl;
48         os::unix::SignalCtl::GetCurrent(signal_ctl);
49     }
50 
SigAction(int sig,UnixSignal * self)51     static void SigAction(int sig, UnixSignal *self)
52     {
53         self->sig_action_count_ += sig;
54     }
55 
Delay()56     static void Delay()
57     {
58         usleep(TIME_TO_WAIT);
59     }
60 
CheckSignalsInsideCycle(int signals)61     void CheckSignalsInsideCycle(int signals)
62     {
63         uint32_t timeout_counter = 0;
64         while (true) {
65             if (sig_action_count_ == signals) {
66                 break;
67             } else {
68                 ++timeout_counter;
69             }
70             Delay();
71 
72             ASSERT_NE(timeout_counter, MAX_TIMEOUT_COUNTER_WAIT) << "Timeout error: Signals not got in time";
73         }
74     }
75 
76     static const uint32_t TIME_TO_WAIT = 100 * 1000;                                // 0.1 second
77     const uint32_t MAX_TIMEOUT_COUNTER_WAIT = 5 * 60 * 1000 * 1000 / TIME_TO_WAIT;  // 5 minutes
78 
79     os::unix::SignalCtl signal_ctl_ {};
80     std::atomic_int sig_action_count_ {0};
81 };
82 
TEST_F(UnixSignal,CheckRestoringSigSet)83 TEST_F(UnixSignal, CheckRestoringSigSet)
84 {
85     // This check is performed by UnixSignal.TearDown()
86 }
87 
TEST_F(UnixSignal,CheckRestoringSigSet2)88 TEST_F(UnixSignal, CheckRestoringSigSet2)
89 {
90     sigset_t sigset;
91     sigemptyset(&sigset);
92     sigaddset(&sigset, SIGUSR1);
93     pthread_sigmask(SIG_BLOCK, &sigset, nullptr);
94 
95     os::unix::SignalCtl signal_ctl;
96     os::unix::SignalCtl::GetCurrent(signal_ctl);
97     ASSERT_NE(signal_ctl_, signal_ctl);
98 
99     pthread_sigmask(SIG_UNBLOCK, &sigset, nullptr);
100 }
101 
TEST_F(UnixSignal,StartStopThread)102 TEST_F(UnixSignal, StartStopThread)
103 {
104     os::unix::SignalCatcherThread catcher_thread;
105 
106     catcher_thread.StartThread(&UnixSignal::SigAction, this);
107     catcher_thread.StopThread();
108 }
109 
TEST_F(UnixSignal,StartStopThreadCatchOnlyCatcherThread)110 TEST_F(UnixSignal, StartStopThreadCatchOnlyCatcherThread)
111 {
112     os::unix::SignalCatcherThread catcher_thread;
113     catcher_thread.CatchOnlyCatcherThread();
114 
115     catcher_thread.StartThread(&UnixSignal::SigAction, this);
116     catcher_thread.StopThread();
117 }
118 
TEST_F(UnixSignal,SendSignalsToMainThread)119 TEST_F(UnixSignal, SendSignalsToMainThread)
120 {
121     os::unix::SignalCatcherThread catcher_thread({SIGUSR1, SIGUSR2});
122     catcher_thread.StartThread(&UnixSignal::SigAction, this);
123 
124     // Send signals to main thread
125     kill(getpid(), SIGUSR1);
126     kill(getpid(), SIGUSR2);
127 
128     // Wait for the signals inside the cycle
129     CheckSignalsInsideCycle(SIGUSR1 + SIGUSR2);
130     catcher_thread.StopThread();
131 
132     ASSERT_EQ(sig_action_count_, SIGUSR1 + SIGUSR2);
133 }
134 
TEST_F(UnixSignal,SendSignalsToMainThread2)135 TEST_F(UnixSignal, SendSignalsToMainThread2)
136 {
137     EXPECT_DEATH(
138         {
139             os::unix::SignalCatcherThread catcher_thread({SIGUSR1});
140             catcher_thread.CatchOnlyCatcherThread();
141 
142             catcher_thread.StartThread(&UnixSignal::SigAction, this);
143 
144             // Send signals to main thread
145             catcher_thread.SendSignal(SIGUSR1);
146             CheckSignalsInsideCycle(SIGUSR1);
147 
148             // After kill process must die
149             kill(getpid(), SIGUSR1);
150             CheckSignalsInsideCycle(SIGUSR1 + SIGUSR1);
151             catcher_thread.StopThread();
152 
153             ASSERT_EQ(true, false) << "Error: Thread must die before this assert";
154         },
155         "");
156 }
157 
TEST_F(UnixSignal,SendSignalsToCatcherThread)158 TEST_F(UnixSignal, SendSignalsToCatcherThread)
159 {
160     os::unix::SignalCatcherThread catcher_thread({SIGUSR1, SIGUSR2});
161     catcher_thread.CatchOnlyCatcherThread();
162 
163     catcher_thread.StartThread(&UnixSignal::SigAction, this);
164 
165     // Send signals to catcher thread
166     catcher_thread.SendSignal(SIGUSR1);
167     catcher_thread.SendSignal(SIGUSR2);
168 
169     // Wait for the signals inside the cycle
170     CheckSignalsInsideCycle(SIGUSR1 + SIGUSR2);
171     catcher_thread.StopThread();
172 
173     ASSERT_EQ(sig_action_count_, SIGUSR1 + SIGUSR2);
174 }
175 
TEST_F(UnixSignal,SendUnhandledSignal)176 TEST_F(UnixSignal, SendUnhandledSignal)
177 {
178     EXPECT_DEATH(
179         {
180             os::unix::SignalCatcherThread catcher_thread({SIGUSR1});
181             catcher_thread.StartThread(&UnixSignal::SigAction, this);
182 
183             // Send signals to catcher thread
184             catcher_thread.SendSignal(SIGUSR1);
185             catcher_thread.SendSignal(SIGUSR2);
186 
187             // Wait for the signals inside the cycle
188             CheckSignalsInsideCycle(SIGUSR1 + SIGUSR2);
189             catcher_thread.StopThread();
190 
191             ASSERT_EQ(sig_action_count_, SIGUSR1 + SIGUSR2);
192         },
193         "");
194 }
195 
TEST_F(UnixSignal,SendSomeSignalsToCatcherThread)196 TEST_F(UnixSignal, SendSomeSignalsToCatcherThread)
197 {
198     os::unix::SignalCatcherThread catcher_thread({SIGQUIT, SIGUSR1, SIGUSR2});
199     catcher_thread.CatchOnlyCatcherThread();
200 
201     catcher_thread.StartThread(&UnixSignal::SigAction, this);
202 
203     // Send signals to catcher thread
204     catcher_thread.SendSignal(SIGQUIT);
205     catcher_thread.SendSignal(SIGUSR1);
206     catcher_thread.SendSignal(SIGUSR2);
207 
208     // Wait for the signals inside the cycle
209     CheckSignalsInsideCycle(SIGQUIT + SIGUSR1 + SIGUSR2);
210     catcher_thread.StopThread();
211 
212     ASSERT_EQ(sig_action_count_, SIGQUIT + SIGUSR1 + SIGUSR2);
213 }
214 
TEST_F(UnixSignal,AfterThreadStartCallback)215 TEST_F(UnixSignal, AfterThreadStartCallback)
216 {
217     os::unix::SignalCatcherThread catcher_thread({SIGQUIT});
218 
219     catcher_thread.SetupCallbacks([this]() { sig_action_count_ += 1000; }, nullptr);
220 
221     catcher_thread.StartThread(&UnixSignal::SigAction, this);
222 
223     // Send signals to catcher thread
224     catcher_thread.SendSignal(SIGQUIT);
225 
226     // Wait for the signals inside the cycle
227     CheckSignalsInsideCycle(SIGQUIT + 1000);
228     catcher_thread.StopThread();
229 
230     ASSERT_EQ(sig_action_count_, SIGQUIT + 1000);
231 }
232 
TEST_F(UnixSignal,BeforeThreadStopCallback)233 TEST_F(UnixSignal, BeforeThreadStopCallback)
234 {
235     os::unix::SignalCatcherThread catcher_thread({SIGQUIT});
236 
237     catcher_thread.SetupCallbacks(nullptr, [this]() { sig_action_count_ += 2000; });
238 
239     catcher_thread.StartThread(&UnixSignal::SigAction, this);
240 
241     // Send signals to catcher thread
242     catcher_thread.SendSignal(SIGQUIT);
243 
244     // Wait for the signals inside the cycle
245     CheckSignalsInsideCycle(SIGQUIT);
246     catcher_thread.StopThread();
247 
248     ASSERT_EQ(sig_action_count_, SIGQUIT + 2000);
249 }
250 
TEST_F(UnixSignal,ThreadCallbacks)251 TEST_F(UnixSignal, ThreadCallbacks)
252 {
253     os::unix::SignalCatcherThread catcher_thread({SIGQUIT});
254 
255     catcher_thread.SetupCallbacks([this]() { sig_action_count_ += 1000; }, [this]() { sig_action_count_ += 2000; });
256 
257     catcher_thread.StartThread(&UnixSignal::SigAction, this);
258 
259     // Send signals to catcher thread
260     catcher_thread.SendSignal(SIGQUIT);
261 
262     // Wait for the signals inside the cycle
263     CheckSignalsInsideCycle(1000 + SIGQUIT);
264     catcher_thread.StopThread();
265 
266     ASSERT_EQ(sig_action_count_, 1000 + SIGQUIT + 2000);
267 }
268 
269 }  // namespace panda::test
270