1 /**
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <dlfcn.h>
17 #include <errno.h>
18 #include <stdio.h>
19 #include <string.h>
20
21 #include "dlns_test.h"
22 #include "functionalext.h"
23
24
25 /**
26 * @tc.name : dlopen_0100
27 * @tc.desc : Enter a valid path, dlopen compatibility test
28 * @tc.level : Level 0
29 */
dlopen_0100(void)30 void dlopen_0100(void)
31 {
32 void* handle = dlopen(dllNamePath, RTLD_LAZY);
33 EXPECT_TRUE("dlopen_0100", handle);
34 dlclose(handle);
35 }
36
37
38 /**
39 * @tc.name : dlopen_0200
40 * @tc.desc : Enter invalid path, dlopen compatibility test
41 * @tc.level : Level 2
42 */
dlopen_0200(void)43 void dlopen_0200(void)
44 {
45 void* handle = dlopen(errPath_ns, RTLD_LAZY);
46 EXPECT_FALSE("dlopen_0200", handle);
47 }
48
49 /**
50 * @tc.name : dlns_init_0100
51 * @tc.desc : Use a string less than 255 bytes as the dlns_init parameter
52 * @tc.level : Level 1
53 */
dlns_init_0100(void)54 void dlns_init_0100(void)
55 {
56 Dl_namespace dlns;
57 char n[] = "dlns_init_0100";
58 dlns_init(&dlns, n);
59 EXPECT_EQ("dlns_init_0100", strcmp(dlns.name, n), 0);
60 }
61
62 /**
63 * @tc.name : dlns_init_0200
64 * @tc.desc : Use very long strings as dlns_init parameters
65 * @tc.level : Level 2
66 */
dlns_init_0200(void)67 void dlns_init_0200(void)
68 {
69 Dl_namespace dlns;
70 int size = 256;
71 char n[] = "012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789 \
72 012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789 \
73 012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789 \
74 012345678901234567890123456789";
75 int len = strlen(n);
76 dlns_init(&dlns, n);
77 EXPECT_TRUE("dlns_init_0200", size < len);
78
79 int dlnslen = strlen(dlns.name);
80 EXPECT_TRUE("dlns_init_0200", size > dlnslen);
81 }
82
83 /**
84 * @tc.name : dlns_create_0100
85 * @tc.desc : Using dlns_create, create a new namespace
86 * @tc.level : Level 1
87 */
dlns_create_0100(void)88 void dlns_create_0100(void)
89 {
90 Dl_namespace dlns;
91 dlns_init(&dlns, "dlns_create_0100");
92
93 EXPECT_EQ("dlns_create_0100", dlns_create(&dlns, path), EOK);
94 }
95
96 /**
97 * @tc.name : dlns_create_0200
98 * @tc.desc : Using dlns_create, create a new namespace
99 * @tc.level : Level 2
100 */
dlns_create_0200(void)101 void dlns_create_0200(void)
102 {
103 // ns_no_allowed_libs already exists
104 Dl_namespace dlns;
105 dlns_init(&dlns, "ns_no_allowed_libs");
106
107 EXPECT_EQ("dlns_create_0200", dlns_create(&dlns, NULL), EEXIST);
108 }
109
110 /**
111 * @tc.name : dlns_create_0300
112 * @tc.desc : After creating a new namespace, check lib_path validity
113 * @tc.level : Level 1
114 */
dlns_create_0300(void)115 void dlns_create_0300(void)
116 {
117 // ns_no_allowed_libs already exists
118 Dl_namespace dlns;
119 dlns_init(&dlns, "dlns_create_0300");
120
121 EXPECT_EQ("dlns_create_0300", dlns_create(&dlns, path), EOK);
122
123 void* handle = dlopen_ns(&dlns, dllName, RTLD_LAZY);
124 EXPECT_TRUE("dlns_create_0300", handle);
125 dlclose(handle);
126 }
127
128 /**
129 * @tc.name : dlns_create_0400
130 * @tc.desc : Namespace creation when dlns=NULL
131 * @tc.level : Level 2
132 */
dlns_create_0400(void)133 void dlns_create_0400(void)
134 {
135 EXPECT_EQ("dlns_create_0400", dlns_create(NULL, path), EINVAL);
136 }
137
138 /**
139 * @tc.name : dlns_create_0500
140 * @tc.desc : Namespace creation when lib_path=NULL
141 * @tc.level : Level 2
142 */
dlns_create_0500(void)143 void dlns_create_0500(void)
144 {
145 Dl_namespace dlns;
146 dlns_init(&dlns, "dlns_create_0500");
147
148 EXPECT_EQ("dlns_create_0500", dlns_create(&dlns, NULL), EOK);
149 }
150
151
152 /**
153 * @tc.name : dlopen_ns_0100
154 * @tc.desc : dlopen_ns valid full path test
155 * @tc.level : Level 1
156 */
dlopen_ns_0100(void)157 void dlopen_ns_0100(void)
158 {
159 Dl_namespace dlns;
160 dlns_init(&dlns, "ns_no_allowed_libs");
161
162 void* handle = dlopen_ns(&dlns, dllNamePath, RTLD_LAZY);
163 EXPECT_TRUE("dlopen_ns_0100", handle);
164 dlclose(handle);
165 }
166
167 /**
168 * @tc.name : dlopen_ns_0200
169 * @tc.desc : dlopen_ns invalid full path test
170 * @tc.level : Level 2
171 */
dlopen_ns_0200(void)172 void dlopen_ns_0200(void)
173 {
174 Dl_namespace dlns;
175 dlns_init(&dlns, "ns_no_allowed_libs");
176
177 void* handle = dlopen_ns(&dlns, errdllNamePath, RTLD_LAZY);
178 EXPECT_FALSE("dlopen_ns_0200", handle);
179 }
180
181 /**
182 * @tc.name : dlopen_ns_0300
183 * @tc.desc : dlopen_ns valid short name test
184 * @tc.level : Level 1
185 */
dlopen_ns_0300(void)186 void dlopen_ns_0300(void)
187 {
188 Dl_namespace dlns;
189 dlns_init(&dlns, "ns_no_allowed_libs");
190
191 void* handle = dlopen_ns(&dlns, dllName, RTLD_LAZY);
192 EXPECT_TRUE("dlopen_ns_0300", handle);
193 dlclose(handle);
194 }
195
196 /**
197 * @tc.name : dlopen_ns_0400
198 * @tc.desc : When dlns=NULL, call dlopen_ns to load the library
199 * @tc.level : Level 1
200 */
dlopen_ns_0400(void)201 void dlopen_ns_0400(void)
202 {
203 void* handle = dlopen_ns(NULL, dllNamePath, RTLD_LAZY);
204 EXPECT_TRUE("dlopen_ns_0400", handle);
205 dlclose(handle);
206 }
207
208 /**
209 * @tc.name : dlopen_ns_0500
210 * @tc.desc : When file=NULL, call dlopen_ns to load the library
211 * @tc.level : Level 2
212 */
dlopen_ns_0500(void)213 void dlopen_ns_0500(void)
214 {
215 Dl_namespace dlns;
216 dlns_init(&dlns, "ns_no_allowed_libs");
217
218 void *handle = dlopen_ns(&dlns, NULL, RTLD_LAZY);
219 EXPECT_TRUE("dlopen_ns_0500", handle);
220 dlclose(handle);
221 }
222
223 /**
224 * @tc.name : dlns_get_0100
225 * @tc.desc : Gets an existing namespace handle.
226 * @tc.level : Level 1
227 */
dlns_get_0100(void)228 void dlns_get_0100(void)
229 {
230 Dl_namespace dlns;
231 dlns_init(&dlns, "dlns_get_0100");
232 dlns_create(&dlns, NULL);
233 dlns_get("dlns_get_0100", &dlns);
234 EXPECT_EQ("dlns_get_0100", strcmp(dlns.name, "dlns_get_0100"), 0);
235 }
236
237 /**
238 * @tc.name : dlns_get_0200
239 * @tc.desc : Gets the current namespace handle when name=NULL.
240 * @tc.level : Level 1
241 */
dlns_get_0200(void)242 void dlns_get_0200(void)
243 {
244 Dl_namespace dlns;
245 dlns_get(NULL, &dlns);
246 EXPECT_EQ("dlns_get_0200", strcmp(dlns.name, "default"), 0);
247 }
248
249 /**
250 * @tc.name : dlns_get_0300
251 * @tc.desc : Call dlns_get when dlns=NULL.
252 * @tc.level : Level 2
253 */
dlns_get_0300(void)254 void dlns_get_0300(void)
255 {
256 dlns_get(NULL, NULL);
257 EXPECT_EQ("dlns_get_0300", dlns_get(NULL, NULL), EINVAL);
258 }
259
260 /**
261 * @tc.name : dlns_get_0400
262 * @tc.desc : Gets a namespace handle that does not exist.
263 * @tc.level : Level 2
264 */
dlns_get_0400(void)265 void dlns_get_0400(void)
266 {
267 Dl_namespace dlns;
268 dlns_init(&dlns, "dlns_get_0400");
269 EXPECT_EQ("dlns_get_0400", dlns_get("dlns_get_0400", &dlns), ENOKEY);
270 }
271
272 TEST_FUN G_Fun_Array[] = {
273 dlopen_0100,
274 dlopen_0200,
275 dlns_init_0100,
276 dlns_init_0200,
277 dlns_create_0100,
278 dlns_create_0200,
279 dlns_create_0300,
280 dlns_create_0400,
281 dlns_create_0500,
282 dlopen_ns_0100,
283 dlopen_ns_0200,
284 dlopen_ns_0300,
285 dlopen_ns_0400,
286 dlopen_ns_0500,
287 dlns_get_0100,
288 dlns_get_0200,
289 dlns_get_0300,
290 dlns_get_0400,
291 };
292
main(void)293 int main(void)
294 {
295 int num = sizeof(G_Fun_Array)/sizeof(TEST_FUN);
296 for (int pos = 0; pos < num; ++pos) {
297 G_Fun_Array[pos]();
298 }
299
300 return t_status;
301 }