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 #include "pthread.h"
35
SigtimedwaitFailTest(const sigset_t * set,siginfo_t * info,const struct timespec * timeout,unsigned int expectErrno)36 static int SigtimedwaitFailTest(const sigset_t *set, siginfo_t *info, const struct timespec *timeout,
37 unsigned int expectErrno)
38 {
39 int ret;
40 errno = 0;
41 if (expectErrno == 0) {
42 ret = sigtimedwait(set, info, timeout);
43 if (ret != 0) {
44 printf("err: line %d, errno %d\n", __LINE__, errno);
45 return errno;
46 }
47 return 0;
48 }
49 ret = sigtimedwait(set, info, timeout);
50 if (ret == 0) {
51 printf("err: line %d\n", __LINE__);
52 return -1;
53 }
54 if (errno != expectErrno) {
55 printf("err: line %d\n", __LINE__);
56 return errno;
57 }
58 return 0;
59 }
60
TestCase()61 static UINT32 TestCase()
62 {
63 int ret;
64 struct timespec time1 = { 0, 50 };
65 sigset_t set;
66
67 sigemptyset(&set);
68 sigprocmask(SIG_SETMASK, &set, NULL);
69
70 printf("check invalid sigset ...\n");
71 int rt = sigaddset(&set, 0);
72 ICUNIT_ASSERT_EQUAL(rt, -1, rt);
73 ICUNIT_ASSERT_EQUAL(errno, EINVAL, errno);
74 rt = sigaddset(&set, 100); // 100, set signal num
75 ICUNIT_ASSERT_EQUAL(rt, -1, rt);
76 ICUNIT_ASSERT_EQUAL(errno, EINVAL, errno);
77 rt = sigaddset(&set, SIGALRM);
78 ICUNIT_ASSERT_EQUAL(rt, 0, rt);
79
80 siginfo_t si;
81 time1.tv_sec = -1;
82 printf("check invalid timespec: tv_sec=-1 ...\n");
83 ret = SigtimedwaitFailTest(&set, &si, &time1, EINVAL);
84 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
85
86 time1.tv_sec = 1;
87 time1.tv_nsec = -1;
88 printf("check invalid timespec: tv_nsec=-1 ...\n");
89 ret = SigtimedwaitFailTest(&set, &si, &time1, EINVAL);
90 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
91
92 time1.tv_sec = 1;
93 time1.tv_nsec = 1000 * 1000 * 1000 + 1; // 1000, set the nsec of time.
94 printf("check invalid timespec: tv_nsec overflow ...\n");
95 ret = SigtimedwaitFailTest(&set, &si, &time1, EINVAL);
96 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
97
98 return 0;
99 }
100
ItPosixSignal042(void)101 void ItPosixSignal042(void)
102 {
103 TEST_ADD_CASE(__FUNCTION__, TestCase, TEST_POSIX, TEST_SIGNAL, TEST_LEVEL0, TEST_FUNCTION);
104 }
105