1/*
2 * Copyright (c) 2021 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#ifndef SIGNAL_TEST
17#define SIGNAL_TEST
18
19#include <gtest/gtest.h>
20#include <signal.h>
21
22const int MAX_SIGNAL = 31;
23const int MAX_DESCRP_LEN = 32;
24const int KEEP_RUN_TIME = 150;
25enum SignalAction {TERMINATE, COREDUMP, IGNORE, STOP, CONTINUE};
26struct SignalNameAction {
27    char signame[12];
28    char description[MAX_DESCRP_LEN];
29    SignalAction action;
30};
31extern SignalNameAction const ALL_SIGNALS[MAX_SIGNAL];
32
33using handler_type = void (*) (int);
34
35class IpcSignalTest : public::testing::TestWithParam<int> {
36public:
37    void SetUp()
38    {
39        mReceivedSignal = 0;
40    };
41    // fail test util-func for sig api
42    void SignalFailTest(int signum, handler_type h, int expectErrno = EINVAL);
43    void SigpendingFailTest(sigset_t* pset);
44    void SigtimedwaitFailTest(const sigset_t *set, siginfo_t* info,
45        const struct timespec* timeout, int expectErrno = EINVAL);
46
47    // utils for all signal test
48    void DefaultActionTest(int signum, bool expectStop, bool coredump = false);
49    void SendAndRecvTest(int signum);
50    int CheckSigString(const char* outfile, const char* expectStr);
51
52    // general signal handler
53    static void SignalHandler(int signum);
54    // special signal handler for function 'abort'
55    static void SigAbortHandler(int signum);
56    // special signal handler for function 'sigaction'
57    static void SigactionHandler(int signum, siginfo_t* si, void* ucontext);
58
59    static void* ThreadFunc1(void* arg);
60    static void* ThreadFunc2(void* arg);
61    static void* ThreadFuncForSigmask1(void* arg);
62    static void* ThreadFuncForSigmask2(void* arg);
63
64protected:
65    static int mReceivedSignal;
66    static int mShmid;
67    static siginfo_t mSiginfo;
68};
69
70
71#endif