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((int)(intptr_t)retptr, -1, retptr);
66 //retptr = mmap(nullptr, 0, PROT_READ, MAP_PRIVATE, fd, 0);
67 //ICUNIT_ASSERT_EQUAL((int)(intptr_t)retptr, -1, retptr);
68 retptr = mmap(nullptr, -1, PROT_READ, MAP_PRIVATE, fd, 0);
69 ICUNIT_ASSERT_EQUAL((int)(intptr_t)retptr, -1, retptr);
70 retptr = mmap(nullptr, 4096, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
71 ICUNIT_ASSERT_EQUAL((int)(intptr_t)retptr, -1, retptr);
72 retptr = mmap(nullptr, 4096, PROT_READ, MAP_SHARED, fd, 0);
73 ICUNIT_ASSERT_EQUAL((int)(intptr_t)retptr, -1, retptr);
74
75 retptr = mmap(nullptr, 1, PROT_READ, MAP_PRIVATE, fd, 0);
76 ICUNIT_ASSERT_NOT_EQUAL((int)(intptr_t)retptr, -1, retptr);
77 retptr = mmap(nullptr, 4095, PROT_READ, MAP_PRIVATE, fd, 0);
78 ICUNIT_ASSERT_EQUAL((int)(intptr_t)retptr, -1, retptr);
79
80 /* testing read/write api */
81 char buf[10] = {0};
82 ret = read(fd, buf, 10);
83 ICUNIT_ASSERT_EQUAL(ret, -1, ret);
84 ret = write(fd, buf, 10);
85 ICUNIT_ASSERT_EQUAL(ret, -1, ret);
86
87 /* before set cms, testing ioctl cmd */
88 ret = ioctl(fd, IPC_CMS_CMD, 0);
89 ICUNIT_ASSERT_NOT_EQUAL(ret, 0, ret);
90 ret = ioctl(fd, IPC_SET_IPC_THREAD, 0);
91 ICUNIT_ASSERT_NOT_EQUAL(ret, 0, ret);
92 ret = ioctl(fd, IPC_SEND_RECV_MSG, 0);
93 ICUNIT_ASSERT_NOT_EQUAL(ret, 0, ret);
94 ret = ioctl(fd, IPC_SEND_RECV_MSG, &tmp);
95 ICUNIT_ASSERT_NOT_EQUAL(ret, 0, ret);
96
97 sleep(2);
98 /* after set cms, testing set cms cmd */
99 ret = ioctl(fd, IPC_SET_CMS, 200);
100 ICUNIT_ASSERT_NOT_EQUAL(ret, 0, ret);
101
102 exit(0);
103 return 0;
104 }
105
TestCase(void)106 static int TestCase(void)
107 {
108 unsigned int ret;
109 int fd = -1;
110 void *retptr = nullptr;
111 int status = 0;
112 pid_t pid = fork();
113 ICUNIT_GOTO_WITHIN_EQUAL(pid, 0, 100000, pid, EXIT);
114 if (pid == 0) {
115 LiteIpcTest();
116 exit(-1);
117 }
118
119 sleep(1);
120 fd = open(LITEIPC_DRIVER, O_RDONLY);
121 ICUNIT_ASSERT_NOT_EQUAL(fd, -1, fd);
122
123 retptr = mmap(nullptr, 16 * 4096, PROT_READ, MAP_PRIVATE, fd, 0);
124 ICUNIT_ASSERT_NOT_EQUAL((int)(intptr_t)retptr, -1, retptr);
125 ret = ioctl(fd, IPC_SET_CMS, 0);
126 ICUNIT_ASSERT_NOT_EQUAL(ret, 0, ret);
127 ret = ioctl(fd, IPC_SET_CMS, 200);
128 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
129
130 ret = ioctl(fd, IPC_SET_CMS, 200);
131 ICUNIT_ASSERT_NOT_EQUAL(ret, 0, ret);
132
133 ret = waitpid(pid, &status, 0);
134 ICUNIT_GOTO_EQUAL(ret, pid, ret, EXIT);
135 status = WEXITSTATUS(status);
136 ICUNIT_GOTO_EQUAL(status, 0, status, EXIT);
137
138 return 0;
139 EXIT:
140 return 1;
141 }
142
ItPosixLiteIpc001(void)143 void ItPosixLiteIpc001(void)
144 {
145 TEST_ADD_CASE("ItPosixLiteIpc001", TestCase, TEST_POSIX, TEST_MEM, TEST_LEVEL0, TEST_FUNCTION);
146 }
147
148