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 g_sigCount++;
38 }
39
SigPrint1(int sig)40 static void SigPrint1(int sig)
41 {
42 g_sigCount += 100; // 100, Used to calculate the progress of the program.
43 }
44
TestSigSet()45 static int TestSigSet()
46 {
47 sigset_t set = { 0 };
48 sigset_t set1 = { 0 };
49 sigset_t left = { 0 };
50 sigset_t right = { 0 };
51
52 void (*ret)(int);
53 int retValue;
54
55 left.__bits[0] = 0x1;
56 right.__bits[0] = 0x2;
57
58 retValue = sigandset(&set, &left, &right);
59 ICUNIT_ASSERT_EQUAL(set.__bits[0], 0, set.__bits[0]);
60 ICUNIT_ASSERT_EQUAL(retValue, 0, retValue);
61
62 left.__bits[0] = 0x11;
63 right.__bits[0] = 0x1;
64
65 retValue = sigandset(&set, &left, &right);
66 ICUNIT_ASSERT_EQUAL(set.__bits[0], 1, set.__bits[0]);
67 ICUNIT_ASSERT_EQUAL(retValue, 0, retValue);
68
69 left.__bits[0] = 0x11;
70 right.__bits[0] = 0x11;
71
72 retValue = sigandset(&set, &left, &right);
73 ICUNIT_ASSERT_EQUAL(set.__bits[0], 0x11, set.__bits[0]);
74 ICUNIT_ASSERT_EQUAL(retValue, 0, retValue);
75
76 retValue = sigorset(&set, &left, &right);
77 ICUNIT_ASSERT_EQUAL(set.__bits[0], 0x11, set.__bits[0]);
78 ICUNIT_ASSERT_EQUAL(retValue, 0, retValue);
79
80 retValue = sigdelset(&set, 1);
81 ICUNIT_ASSERT_EQUAL(set.__bits[0], 0x10, set.__bits[0]);
82 ICUNIT_ASSERT_EQUAL(retValue, 0, retValue);
83
84 int sigs;
85 sigset(1, SigPrint1);
86
87 retValue = raise(1);
88 ICUNIT_ASSERT_EQUAL(g_sigCount, 100, g_sigCount); // 100, assert that function Result is equal to this.
89 ICUNIT_ASSERT_EQUAL(retValue, 0, retValue);
90
91 retValue = sigfillset(&set);
92 ICUNIT_ASSERT_EQUAL(set.__bits[0], 0x7fffffff, set.__bits[0]);
93 ICUNIT_ASSERT_EQUAL(set.__bits[1], 0xfffffffc, set.__bits[1]);
94 ICUNIT_ASSERT_EQUAL(retValue, 0, retValue);
95
96 retValue = sigaddset(&set1, 1);
97 ICUNIT_ASSERT_EQUAL(retValue, 0, retValue);
98
99 ret = signal(1, SigPrint);
100 ICUNIT_ASSERT_NOT_EQUAL(ret, NULL, ret);
101 retValue = sighold(1);
102 ICUNIT_ASSERT_EQUAL(retValue, 0, retValue);
103
104 retValue = raise(1);
105 ICUNIT_ASSERT_EQUAL(retValue, 0, retValue);
106 retValue = sigrelse(1);
107 ICUNIT_ASSERT_EQUAL(retValue, 0, retValue);
108
109 retValue = raise(1);
110 ICUNIT_ASSERT_EQUAL(retValue, 0, retValue);
111 ICUNIT_ASSERT_EQUAL(g_sigCount, 102, g_sigCount); // 102, assert that function Result is equal to this.
112
113 retValue = sigisemptyset(&set1);
114 ICUNIT_ASSERT_EQUAL(retValue, 0, retValue);
115
116 retValue = siginterrupt(1, 0);
117 ICUNIT_ASSERT_EQUAL(retValue, 0, retValue);
118 return 0;
119 }
120
121
ItPosixSignal009(void)122 void ItPosixSignal009(void)
123 {
124 TEST_ADD_CASE(__FUNCTION__, TestSigSet, TEST_POSIX, TEST_SIGNAL, TEST_LEVEL0, TEST_FUNCTION);
125 }
126