1 /*
2  * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3  * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice, this list of
9  * conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12  * of conditions and the following disclaimer in the documentation and/or other materials
13  * provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16  * to endorse or promote products derived from this software without specific prior written
17  * permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 #include "it_test_signal.h"
32 #include "signal.h"
33 
34 static int g_sigCount = 0;
SigPrint(int sig)35 static void SigPrint(int sig)
36 {
37     (void)sig;
38     g_sigCount++;
39 }
40 
SigPrint1(int sig)41 static void SigPrint1(int sig)
42 {
43     (void)sig;
44     g_sigCount += 100; // 100, Used to calculate the progress of the program.
45 }
46 
TestSigSet(void)47 static int TestSigSet(void)
48 {
49     sigset_t set = { 0 };
50     sigset_t set1 = { 0 };
51     sigset_t left = { 0 };
52     sigset_t right = { 0 };
53 
54     void (*ret)(int);
55     int retValue;
56 
57     left.__bits[0] = 0x1;
58     right.__bits[0] = 0x2;
59 
60     retValue = sigandset(&set, &left, &right);
61     ICUNIT_ASSERT_EQUAL(set.__bits[0], 0, set.__bits[0]);
62     ICUNIT_ASSERT_EQUAL(retValue, 0, retValue);
63 
64     left.__bits[0] = 0x11;
65     right.__bits[0] = 0x1;
66 
67     retValue = sigandset(&set, &left, &right);
68     ICUNIT_ASSERT_EQUAL(set.__bits[0], 1, set.__bits[0]);
69     ICUNIT_ASSERT_EQUAL(retValue, 0, retValue);
70 
71     left.__bits[0] = 0x11;
72     right.__bits[0] = 0x11;
73 
74     retValue = sigandset(&set, &left, &right);
75     ICUNIT_ASSERT_EQUAL(set.__bits[0], 0x11, set.__bits[0]);
76     ICUNIT_ASSERT_EQUAL(retValue, 0, retValue);
77 
78     retValue = sigorset(&set, &left, &right);
79     ICUNIT_ASSERT_EQUAL(set.__bits[0], 0x11, set.__bits[0]);
80     ICUNIT_ASSERT_EQUAL(retValue, 0, retValue);
81 
82     retValue = sigdelset(&set, 1);
83     ICUNIT_ASSERT_EQUAL(set.__bits[0], 0x10, set.__bits[0]);
84     ICUNIT_ASSERT_EQUAL(retValue, 0, retValue);
85 
86     int sigs;
87     sigset(1, SigPrint1);
88 
89     retValue = raise(1);
90     ICUNIT_ASSERT_EQUAL(g_sigCount, 100, g_sigCount); // 100, assert that function Result is equal to this.
91     ICUNIT_ASSERT_EQUAL(retValue, 0, retValue);
92 
93     retValue = sigfillset(&set);
94     ICUNIT_ASSERT_EQUAL(set.__bits[0], 0x7fffffff, set.__bits[0]);
95     ICUNIT_ASSERT_EQUAL(set.__bits[1], 0xfffffffc, set.__bits[1]);
96     ICUNIT_ASSERT_EQUAL(retValue, 0, retValue);
97 
98     retValue = sigaddset(&set1, 1);
99     ICUNIT_ASSERT_EQUAL(retValue, 0, retValue);
100 
101     ret = signal(1, SigPrint);
102     ICUNIT_ASSERT_NOT_EQUAL(ret, NULL, ret);
103     retValue = sighold(1);
104     ICUNIT_ASSERT_EQUAL(retValue, 0, retValue);
105 
106     retValue = raise(1);
107     ICUNIT_ASSERT_EQUAL(retValue, 0, retValue);
108     retValue = sigrelse(1);
109     ICUNIT_ASSERT_EQUAL(retValue, 0, retValue);
110 
111     retValue = raise(1);
112     ICUNIT_ASSERT_EQUAL(retValue, 0, retValue);
113     ICUNIT_ASSERT_EQUAL(g_sigCount, 102, g_sigCount); // 102, assert that function Result is equal to this.
114 
115     retValue = sigisemptyset(&set1);
116     ICUNIT_ASSERT_EQUAL(retValue, 0, retValue);
117 
118     retValue = siginterrupt(1, 0);
119     ICUNIT_ASSERT_EQUAL(retValue, 0, retValue);
120     return 0;
121 }
122 
123 
ItPosixSignal009(void)124 void ItPosixSignal009(void)
125 {
126     TEST_ADD_CASE(__FUNCTION__, TestSigSet, TEST_POSIX, TEST_SIGNAL, TEST_LEVEL0, TEST_FUNCTION);
127 }
128