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_posix_queue.h"
32
Child(VOID)33 static int Child(VOID)
34 {
35 int ret;
36 CHAR mqname[MQUEUE_STANDARD_NAME_LENGTH] = "";
37 int pid;
38 int status;
39 mqd_t mqueue;
40 struct sigevent notification;
41
42 (void)snprintf_s(mqname, MQUEUE_STANDARD_NAME_LENGTH - 1, MQUEUE_STANDARD_NAME_LENGTH, "/mq206_%d", getpid());
43
44 mqueue = mq_open(mqname, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, nullptr);
45 ICUNIT_GOTO_NOT_EQUAL(mqueue, (mqd_t)-1, mqueue, EXIT);
46
47 notification.sigev_notify = 5; // 5, User-defined signal.
48
49 ret = mq_notify(mqueue, ¬ification);
50 ICUNIT_GOTO_EQUAL(ret, -1, ret, EXIT);
51 ICUNIT_GOTO_EQUAL(errno, EINVAL, errno, EXIT);
52
53 notification.sigev_notify = SIGEV_THREAD;
54
55 ret = mq_notify(mqueue, ¬ification);
56 ICUNIT_GOTO_EQUAL(ret, -1, ret, EXIT);
57 ICUNIT_GOTO_EQUAL(errno, ENOTSUP, errno, EXIT);
58
59 notification.sigev_notify = SIGEV_NONE;
60
61 ret = mq_notify(-1, ¬ification);
62 ICUNIT_GOTO_EQUAL(ret, -1, ret, EXIT);
63 ICUNIT_GOTO_EQUAL(errno, EBADF, errno, EXIT);
64
65 ret = mq_notify(mqueue, ¬ification);
66 ICUNIT_GOTO_EQUAL(ret, MQUEUE_NO_ERROR, ret, EXIT);
67
68 pid = fork();
69 ICUNIT_GOTO_WITHIN_EQUAL(pid, 0, 100000, pid, EXIT); // 100000, Valid range value of pid.
70
71 if (pid == 0) {
72 ret = mq_notify(mqueue, ¬ification);
73 ICUNIT_ASSERT_EQUAL(ret, -1, ret);
74 ICUNIT_ASSERT_EQUAL(errno, EBUSY, errno);
75
76 exit(8); // 8, Set a exit status.
77 } else {
78 ret = waitpid(pid, &status, 0);
79 ICUNIT_GOTO_EQUAL(ret, pid, ret, EXIT);
80
81 status = WEXITSTATUS(status);
82 ICUNIT_GOTO_EQUAL(status, 8, status, EXIT); // 8, Here, assert the ret.
83
84 ret = mq_notify(mqueue, nullptr);
85 ICUNIT_GOTO_EQUAL(ret, MQUEUE_NO_ERROR, ret, EXIT);
86
87 pid = fork();
88 ICUNIT_GOTO_WITHIN_EQUAL(pid, 0, 100000, pid, EXIT); // 100000, Valid range value of pid.
89 if (pid == 0) {
90 notification.sigev_notify = SIGEV_SIGNAL;
91 notification.sigev_signo = SIGUSR1;
92
93 ret = mq_notify(mqueue, ¬ification);
94 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
95
96 exit(11); // 11, Set a exit status.
97 }
98
99 ret = waitpid(pid, &status, 0);
100 ICUNIT_GOTO_EQUAL(ret, pid, ret, EXIT);
101
102 status = WEXITSTATUS(status);
103 ICUNIT_GOTO_EQUAL(status, 11, status, EXIT); // 11, Here, assert the ret.
104
105 mq_close(mqueue);
106 mq_unlink(mqname);
107 }
108 exit(10); // 10, Set a exit status.
109
110 EXIT:
111 mq_close(mqueue);
112 mq_unlink(mqname);
113 return MQUEUE_IS_ERROR;
114 }
Testcase(VOID)115 static UINT32 Testcase(VOID)
116 {
117 int ret;
118 int status;
119
120 pid_t pid = fork();
121 ICUNIT_GOTO_WITHIN_EQUAL(pid, 0, 100000, pid, EXIT); // 100000, Valid range value of pid.
122
123 if (pid == 0) {
124 Child();
125 exit(255); // 255, Set a exit status.
126 }
127
128 ret = waitpid(pid, &status, 0);
129 ICUNIT_ASSERT_EQUAL(ret, pid, ret);
130
131 status = WEXITSTATUS(status);
132 ICUNIT_ASSERT_EQUAL(status, 10, status); // 10, Here, assert the ret.
133
134 return 0;
135 EXIT:
136 return 1;
137 }
138
ItPosixQueue206(VOID)139 VOID ItPosixQueue206(VOID)
140 {
141 TEST_ADD_CASE("IT_POSIX_QUEUE_206", Testcase, TEST_POSIX, TEST_QUE, TEST_LEVEL2, TEST_FUNCTION);
142 }