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
18 #include "dso_symver.h"
19
20 /**
21 * @tc.name : dlvsym_no_symver_0100
22 * @tc.desc : invoke a symbol with an invalid version
23 * @tc.level : Level 2
24 */
dlvsym_no_symver_0100(void)25 void dlvsym_no_symver_0100(void)
26 {
27 symver_log("start");
28
29 void *handle = dlopen(dso_no_symver_name, RTLD_LAZY);
30 if (!handle) {
31 symver_error("%s", dlerror());
32 return;
33 }
34
35 // Clear any existing error
36 dlerror();
37
38 functype func = (functype)dlvsym(handle, dso_no_symver_symbol, dso_version_invalid);
39 const char *error = dlerror();
40 if (error == NULL) {
41 symver_error();
42 return;
43 }
44
45 symver_log("error = %s", error);
46
47 dlclose(handle);
48
49 symver_log("end");
50 }
51
52 /**
53 * @tc.name : dlvsym_no_symver_0200
54 * @tc.desc : invoke an invalid symbol with an invalid version
55 * @tc.level : Level 2
56 */
dlvsym_no_symver_0200(void)57 void dlvsym_no_symver_0200(void)
58 {
59 symver_log("start");
60
61 void *handle = dlopen(dso_no_symver_name, RTLD_LAZY);
62 if (!handle) {
63 symver_error("%s", dlerror());
64 return;
65 }
66
67 // Clear any existing error
68 dlerror();
69
70 functype func = (functype)dlvsym(handle, dso_symbol_invalid, dso_version_invalid);
71 const char *error = dlerror();
72 if (error == NULL) {
73 symver_error();
74 return;
75 }
76
77 symver_log("error = %s", error);
78
79 dlclose(handle);
80
81 symver_log("end");
82 }
83
84 /**
85 * @tc.name : dlvsym_easy_symver_0100
86 * @tc.desc : invoke a symbol with the default version
87 * @tc.level : Level 0
88 */
dlvsym_easy_symver_0100(void)89 void dlvsym_easy_symver_0100(void)
90 {
91 symver_log("start");
92
93 void *handle = dlopen(dso_easy_symver_name, RTLD_LAZY);
94 if (!handle) {
95 symver_error("%s", dlerror());
96 return;
97 }
98
99 // Clear any existing error
100 dlerror();
101
102 functype func = (functype)dlvsym(handle, dso_easy_symver_symbol, dso_easy_symver_version_stable);
103 const char *error = dlerror();
104 if (error != NULL) {
105 symver_error("%s", error);
106 return;
107 }
108
109 const char *result = func();
110 symver_streq(result, dso_easy_symver_symbol_stable);
111
112 dlclose(handle);
113
114 symver_log("end");
115 }
116
117 /**
118 * @tc.name : dlvsym_easy_symver_0200
119 * @tc.desc : invoke a symbol with a non-default version
120 * @tc.level : Level 1
121 */
dlvsym_easy_symver_0200(void)122 void dlvsym_easy_symver_0200(void)
123 {
124 symver_log("start");
125
126 void *handle = dlopen(dso_easy_symver_name, RTLD_LAZY);
127 if (!handle) {
128 symver_error("%s", dlerror());
129 return;
130 }
131
132 // Clear any existing error
133 dlerror();
134
135 functype func = (functype)dlvsym(handle, dso_easy_symver_symbol, dso_easy_symver_version_old);
136 const char *error = dlerror();
137 if (error != NULL) {
138 symver_error("%s", error);
139 return;
140 }
141
142 const char *result = func();
143 symver_streq(result, dso_easy_symver_symbol_old);
144
145 dlclose(handle);
146
147 symver_log("end");
148 }
149
150 /**
151 * @tc.name : dlvsym_easy_symver_0300
152 * @tc.desc : invoke an invalid symbol with an invalid version
153 * @tc.level : Level 2
154 */
dlvsym_easy_symver_0300(void)155 void dlvsym_easy_symver_0300(void)
156 {
157 symver_log("start");
158
159 void *handle = dlopen(dso_easy_symver_name, RTLD_LAZY);
160 if (!handle) {
161 symver_error("%s", dlerror());
162 return;
163 }
164
165 // Clear any existing error
166 dlerror();
167
168 functype func = (functype)dlvsym(handle, dso_symbol_invalid, dso_version_invalid);
169 const char *error = dlerror();
170 if (error == NULL) {
171 symver_error();
172 return;
173 }
174
175 symver_log("error = %s", error);
176
177 dlclose(handle);
178
179 symver_log("end");
180 }
181
182 /**
183 * @tc.name : dlvsym_easy_symver_0400
184 * @tc.desc : invoke a symbol with an invalid version
185 * @tc.level : Level 2
186 */
dlvsym_easy_symver_0400(void)187 void dlvsym_easy_symver_0400(void)
188 {
189 symver_log("start");
190
191 void *handle = dlopen(dso_easy_symver_name, RTLD_LAZY);
192 if (!handle) {
193 symver_error("%s", dlerror());
194 return;
195 }
196
197 // Clear any existing error
198 dlerror();
199
200 functype func = (functype)dlvsym(handle, dso_easy_symver_symbol, dso_version_invalid);
201 const char *error = dlerror();
202 if (error == NULL) {
203 symver_error();
204 return;
205 }
206
207 symver_log("error = %s", error);
208
209 dlclose(handle);
210
211 symver_log("end");
212 }
213
214 /**
215 * @tc.name : dlvsym_hard_symver_0100
216 * @tc.desc : invoke a symbol with the default version
217 * @tc.level : Level 0
218 */
dlvsym_hard_symver_0100(void)219 void dlvsym_hard_symver_0100(void)
220 {
221 symver_log("start");
222
223 void *handle = dlopen(dso_hard_symver_name, RTLD_LAZY);
224 if (!handle) {
225 symver_error("%s", dlerror());
226 return;
227 }
228
229 // Clear any existing error
230 dlerror();
231
232 functype func = (functype)dlvsym(handle, dso_hard_symver_if_symbol, dso_hard_symver_version_stable);
233 const char *error = dlerror();
234 if (error != NULL) {
235 symver_error("%s", error);
236 return;
237 }
238
239 const char *result = func();
240 symver_streq(result, dso_hard_symver_if_symbol_stable);
241
242 dlclose(handle);
243
244 symver_log("end");
245 }
246
247 /**
248 * @tc.name : dlvsym_hard_symver_0200
249 * @tc.desc : invoke a symbol with a non-default version
250 * @tc.level : Level 1
251 */
dlvsym_hard_symver_0200(void)252 void dlvsym_hard_symver_0200(void)
253 {
254 symver_log("start");
255
256 void *handle = dlopen(dso_hard_symver_name, RTLD_LAZY);
257 if (!handle) {
258 symver_error("%s", dlerror());
259 return;
260 }
261
262 // Clear any existing error
263 dlerror();
264
265 functype func = (functype)dlvsym(handle, dso_hard_symver_if_symbol, dso_hard_symver_version_old);
266 const char *error = dlerror();
267 if (error != NULL) {
268 symver_error("%s", error);
269 return;
270 }
271
272 const char *result = func();
273 symver_streq(result, dso_hard_symver_if_symbol_old);
274
275 dlclose(handle);
276
277 symver_log("end");
278 }
279
280 /**
281 * @tc.name : dlvsym_hard_symver_0300
282 * @tc.desc : invoke an invalid symbol with an invalid version
283 * @tc.level : Level 2
284 */
dlvsym_hard_symver_0300(void)285 void dlvsym_hard_symver_0300(void)
286 {
287 symver_log("start");
288
289 void *handle = dlopen(dso_hard_symver_name, RTLD_LAZY);
290 if (!handle) {
291 symver_error("%s", dlerror());
292 return;
293 }
294
295 // Clear any existing error
296 dlerror();
297
298 functype func = (functype)dlvsym(handle, dso_symbol_invalid, dso_version_invalid);
299 const char *error = dlerror();
300 if (error == NULL) {
301 symver_error();
302 return;
303 }
304
305 symver_log("error = %s", error);
306
307 dlclose(handle);
308
309 symver_log("end");
310 }
311
312 /**
313 * @tc.name : dlvsym_hard_symver_0400
314 * @tc.desc : invoke a symbol with an invalid version
315 * @tc.level : Level 2
316 */
dlvsym_hard_symver_0400(void)317 void dlvsym_hard_symver_0400(void)
318 {
319 symver_log("start");
320
321 void *handle = dlopen(dso_hard_symver_name, RTLD_LAZY);
322 if (!handle) {
323 symver_error("%s", dlerror());
324 return;
325 }
326
327 // Clear any existing error
328 dlerror();
329
330 functype func = (functype)dlvsym(handle, dso_hard_symver_if_symbol, dso_version_invalid);
331 const char *error = dlerror();
332 if (error == NULL) {
333 symver_error();
334 return;
335 }
336
337 symver_log("error = %s", error);
338
339 dlclose(handle);
340
341 symver_log("end");
342 }
343
main(int argc,char * argv[])344 int main(int argc, char *argv[])
345 {
346 symver_log("start");
347
348 dlvsym_no_symver_0100();
349 dlvsym_no_symver_0200();
350
351 dlvsym_easy_symver_0100();
352 dlvsym_easy_symver_0200();
353 dlvsym_easy_symver_0300();
354 dlvsym_easy_symver_0400();
355
356 dlvsym_hard_symver_0100();
357 dlvsym_hard_symver_0200();
358 dlvsym_hard_symver_0300();
359 dlvsym_hard_symver_0400();
360
361 symver_log("t_status = %d", t_status);
362 symver_log("end");
363
364 return t_status;
365 }
366