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
32 #include "it_test_liteipc.h"
33 #include "sys/wait.h"
34
35 #include "unistd.h"
36 #include "stdlib.h"
37 #include "stdio.h"
38 #include "string.h"
39 #include "sys/time.h"
40 #include "sys/ioctl.h"
41 #include "fcntl.h"
42
43 #include "liteipc.h"
44 #include "smgr_demo.h"
45
LiteIpcTest(void)46 static int LiteIpcTest(void)
47 {
48 unsigned int ret;
49 IpcContent tmp;
50 void *retptr = nullptr;
51 /* testing open liteipc driver with different flag, expecting result is that flag matters nothing */
52 int fd = open(LITEIPC_DRIVER, O_WRONLY | O_CLOEXEC);
53 ICUNIT_ASSERT_NOT_EQUAL(fd, -1, fd);
54 ret = close(fd);
55 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
56 fd = open(LITEIPC_DRIVER, O_RDONLY | O_CREAT);
57 ICUNIT_ASSERT_NOT_EQUAL(fd, -1, fd);
58 ret = close(fd);
59 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
60 fd = open(LITEIPC_DRIVER, O_RDWR | O_SYNC);
61 ICUNIT_ASSERT_NOT_EQUAL(fd, -1, fd);
62
63 /* testing mmap liteipc mem pool with different size and flag */
64 retptr = mmap(nullptr, 1024 * 4096, PROT_READ, MAP_PRIVATE, fd, 0);
65 ICUNIT_ASSERT_EQUAL(static_cast<int>(static_cast<intptr_t>(retptr)), -1, retptr);
66 retptr = mmap(nullptr, -1, PROT_READ, MAP_PRIVATE, fd, 0);
67 ICUNIT_ASSERT_EQUAL(static_cast<int>(static_cast<intptr_t>(retptr)), -1, retptr);
68 retptr = mmap(nullptr, 4096, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
69 ICUNIT_ASSERT_EQUAL(static_cast<int>(static_cast<intptr_t>(retptr)), -1, retptr);
70 retptr = mmap(nullptr, 4096, PROT_READ, MAP_SHARED, fd, 0); // 4096: length of mapped memory
71 ICUNIT_ASSERT_EQUAL(static_cast<int>(static_cast<intptr_t>(retptr)), -1, retptr);
72
73 retptr = mmap(nullptr, 1, PROT_READ, MAP_PRIVATE, fd, 0);
74 ICUNIT_ASSERT_NOT_EQUAL(static_cast<int>(static_cast<intptr_t>(retptr)), -1, retptr);
75 retptr = mmap(nullptr, 4095, PROT_READ, MAP_PRIVATE, fd, 0);
76 ICUNIT_ASSERT_EQUAL(static_cast<int>(static_cast<intptr_t>(retptr)), -1, retptr);
77
78 /* testing read/write api */
79 char buf[10] = {0};
80 ret = read(fd, buf, 10);
81 ICUNIT_ASSERT_EQUAL(ret, -1, ret);
82 ret = write(fd, buf, 10); // 10: size of buf
83 ICUNIT_ASSERT_EQUAL(ret, -1, ret);
84
85 /* before set cms, testing ioctl cmd */
86 ret = ioctl(fd, IPC_CMS_CMD, 0);
87 ICUNIT_ASSERT_NOT_EQUAL(ret, 0, ret);
88 ret = ioctl(fd, IPC_SET_IPC_THREAD, 0);
89 ICUNIT_ASSERT_NOT_EQUAL(ret, 0, ret);
90 ret = ioctl(fd, IPC_SEND_RECV_MSG, 0);
91 ICUNIT_ASSERT_NOT_EQUAL(ret, 0, ret);
92 ret = ioctl(fd, IPC_SEND_RECV_MSG, &tmp);
93 ICUNIT_ASSERT_NOT_EQUAL(ret, 0, ret);
94
95 sleep(2);
96 /* after set cms, testing set cms cmd */
97 ret = ioctl(fd, IPC_SET_CMS, 200); // 200: use 200 for set cms cmd testing
98 ICUNIT_ASSERT_NOT_EQUAL(ret, 0, ret);
99
100 exit(0);
101 return 0;
102 }
103
TestCase(void)104 static int TestCase(void)
105 {
106 unsigned int ret;
107 int fd = -1;
108 void *retptr = nullptr;
109 int status = 0;
110 pid_t pid = fork();
111 ICUNIT_GOTO_WITHIN_EQUAL(pid, 0, 100000, pid, EXIT);
112 if (pid == 0) {
113 LiteIpcTest();
114 exit(-1);
115 }
116
117 sleep(1);
118 fd = open(LITEIPC_DRIVER, O_RDONLY);
119 ICUNIT_ASSERT_NOT_EQUAL(fd, -1, fd);
120
121 retptr = mmap(nullptr, 16 * 4096, PROT_READ, MAP_PRIVATE, fd, 0);
122 ICUNIT_ASSERT_NOT_EQUAL(static_cast<int>(static_cast<intptr_t>(retptr)), -1, retptr);
123 ret = ioctl(fd, IPC_SET_CMS, 0);
124 ICUNIT_ASSERT_NOT_EQUAL(ret, 0, ret);
125 ret = ioctl(fd, IPC_SET_CMS, 200);
126 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
127
128 ret = ioctl(fd, IPC_SET_CMS, 200);
129 ICUNIT_ASSERT_NOT_EQUAL(ret, 0, ret);
130
131 ret = waitpid(pid, &status, 0);
132 ICUNIT_GOTO_EQUAL(ret, pid, ret, EXIT);
133 status = WEXITSTATUS(status);
134 ICUNIT_GOTO_EQUAL(status, 0, status, EXIT);
135
136 return 0;
137 EXIT:
138 return 1;
139 }
140
ItPosixLiteIpc001(void)141 void ItPosixLiteIpc001(void)
142 {
143 TEST_ADD_CASE("ItPosixLiteIpc001", TestCase, TEST_POSIX, TEST_MEM, TEST_LEVEL0, TEST_FUNCTION);
144 }
145
146