1 /*
2 * Copyright (C) 2025 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 #include <cstdio>
16 #include <dlfcn.h>
17 #include <string>
18
19 #include "cpp-lib-iface.h"
20 #include "testdlopen.h"
21
22 namespace testdlopen {
23 const int defaultCtorInitValue = 0;
24
25 // open library
openLib(const char * filename)26 static void *openLib(const char *filename) {
27 printf("\tLoading library %s\n", filename);
28 void *handle = dlopen(filename, RTLD_LAZY);
29 if (!handle) {
30 printf("Error loading library!\n");
31 dlerror();
32 }
33
34 return handle;
35 }
36
37 // close library
closeLib(void * handle)38 static void closeLib(void *handle) {
39 printf("\tClosing library\n");
40 dlclose(handle);
41 }
42
testVer(const char * filename)43 std::string testVer(const char *filename) {
44 printf("Testing CppDynamicLib::getVersion\n");
45 void *handle = openLib(filename);
46 if (!handle) {
47 printf("Exiting!\n");
48 return "test failure";
49 }
50
51 printf("\tLoading symbol create\n");
52 create_t* create_class = (create_t*) dlsym(handle, "create");
53 printf("\tLoading symbol destroy\n");
54 destroy_t* destroy_class = (destroy_t*) dlsym(handle, "destroy");
55 if (!create_class || !destroy_class) {
56 printf("Exiting!\n");
57 return "test failure";
58 }
59
60 printf("\tInstantiating CppDynamicLib class\n");
61 ICppDynamicLib* testclass = create_class(defaultCtorInitValue);
62
63 printf("\tCalling getVerion\n");
64 std::string ver = testclass->getVersion();
65
66 printf("\tDestroying CppDynamicLib class\n");
67 destroy_class(testclass);
68
69 closeLib(handle);
70
71 printf("Exiting getVersion test\n");
72 return ver;
73 }
74
testPrintf(const char * filename,const char * text)75 int testPrintf(const char *filename, const char *text) {
76 printf("Testing CppDynamicLib testPrintf\n");
77 void *handle = openLib(filename);
78 if (!handle) {
79 printf("Exiting!\n");
80 return -1;
81 }
82
83 printf("\tLoading symbol create\n");
84 create_t* create_class = (create_t*) dlsym(handle, "create");
85 printf("\tLoading symbol destroy\n");
86 destroy_t* destroy_class = (destroy_t*) dlsym(handle, "destroy");
87 if (!create_class || !destroy_class) {
88 printf("Exiting!\n");
89 return -1;
90 }
91
92 printf("\tInstantiating CppDynamicLib class\n");
93 ICppDynamicLib* testclass = create_class(defaultCtorInitValue);
94
95 printf("\tCalling testPrintf\n");
96 testclass->testPrintf(text);
97
98 printf("\tDestroying CppDynamicLib class\n");
99 destroy_class(testclass);
100
101 closeLib(handle);
102
103 printf("Exiting testPrintf test\n");
104 return 0;
105 }
106
testMax(const char * filename,int a,int b)107 int testMax(const char *filename, int a, int b) {
108 printf("Testing CppDynamicLib: getMax\n");
109 void *handle = openLib(filename);
110 if (!handle) {
111 printf("Exiting!\n");
112 return -1;
113 }
114
115 printf("\tLoading symbol create\n");
116 create_t* create_class = (create_t*) dlsym(handle, "create");
117 printf("\tLoading symbol destroy\n");
118 destroy_t* destroy_class = (destroy_t*) dlsym(handle, "destroy");
119 if (!create_class || !destroy_class) {
120 printf("Exiting!\n");
121 return -1;
122 }
123
124 printf("\tInstantiating CppDynamicLib class\n");
125 ICppDynamicLib* testclass = create_class(defaultCtorInitValue);
126
127 printf("\tCalling getMax\n");
128 int max = testclass->getMax(a, b);
129
130 printf("\tDestroying CppDynamicLib class\n");
131 destroy_class(testclass);
132
133 closeLib(handle);
134
135 printf("Exiting getMax test\n");
136 return max;
137 }
138
testCtor(const char * filename,int initStorage)139 int testCtor(const char *filename, int initStorage) {
140 printf("Testing getStorage\n");
141 void *handle = openLib(filename);
142 if (!handle) {
143 printf("Exiting!\n");
144 return initStorage-1;
145 }
146
147 printf("\tLoading symbol create\n");
148 create_t* create_class = (create_t*) dlsym(handle, "create");
149 printf("\tLoading symbol destroy\n");
150 destroy_t* destroy_class = (destroy_t*) dlsym(handle, "destroy");
151 if (!create_class || !destroy_class) {
152 printf("Exiting!\n");
153 return initStorage-1;
154 }
155
156 printf("\tInstantiating CppDynamicLib class\n");
157 ICppDynamicLib* testclass = create_class(initStorage);
158
159 printf("\tCalling getStorage\n");
160 auto storage = testclass->getStorage();
161
162 printf("\tDestroying CppDynamicLib class\n");
163 destroy_class(testclass);
164
165 closeLib(handle);
166
167 printf("Exiting getStorage test\n");
168 return storage;
169 }
170 } // namespace testdlopen