Debian Codegraph @

nsprpub/pr/tests/dlltest.c

raw
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6 /***********************************************************************
7 **
8 ** Name: dlltest.c
9 **
10 ** Description: test dll functionality.
11 **
12 ** Modification History:
13 ** 14-May-97 AGarcia- Converted the test to accomodate the debug_mode flag.
14 ** The debug mode will print all of the printfs associated with this
15 *test.
16 ** The regress mode will be the default mode. Since the regress tool
17 *limits
18 ** the output to a one line status:PASS or FAIL,all of the printf
19 *statements
20 ** have been handled with an if (debug_mode) statement.
21 ** 04-June-97 AGarcia removed the Test_Result function. Regress tool has been
22 *updated to
23 ** recognize the return code from tha main program.
24 ** 12-June-97 Revert to return code 0 and 1.
25 ***********************************************************************/
26
27 /***********************************************************************
28 ** Includes
29 ***********************************************************************/
30 #include "prinit.h"
31 #include "prlink.h"
32 #include "prmem.h"
33 #include "prerror.h"
34
35 #include "plstr.h"
36
37 #include <stdio.h>
38 #include <stdlib.h>
39
40 typedef PRIntn(PR_CALLBACK* GetFcnType)(void);
41 typedef void(PR_CALLBACK* SetFcnType)(PRIntn);
42
43 PRIntn failed_already = 0;
44 PRIntn debug_mode;
45
46 int main(int argc, char** argv) {
47 PRLibrary *lib, *lib2; /* two handles to the same library */
48 GetFcnType getFcn;
49 SetFcnType setFcn;
50 PRIntn value;
51 PRStatus status;
52 char* libName;
53
54 if (argc >= 2 && PL_strcmp(argv[1], "-d") == 0) {
55 debug_mode = 1;
56 }
57
58 PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
59 PR_STDIO_INIT();
60
61 /*
62 * Test 1: load the library, look up the symbols, call the functions,
63 * and check the results.
64 */
65
66 libName = PR_GetLibraryName("dll", "my");
67 if (debug_mode) {
68 printf("Loading library %s\n", libName);
69 }
70 lib = PR_LoadLibrary(libName);
71 PR_FreeLibraryName(libName);
72 if (lib == NULL) {
73 PRInt32 textLength = PR_GetErrorTextLength();
74 char* text = (char*)PR_MALLOC(textLength + 1);
75 text[0] = '\0';
76 (void)PR_GetErrorText(text);
77 fprintf(stderr, "PR_LoadLibrary failed (%d, %d, %s)\n", PR_GetError(),
78 PR_GetOSError(), text);
79 if (!debug_mode) {
80 failed_already = 1;
81 }
82 }
83 getFcn = (GetFcnType)PR_FindSymbol(lib, "My_GetValue");
84 setFcn = (SetFcnType)PR_FindFunctionSymbol(lib, "My_SetValue");
85 (*setFcn)(888);
86 value = (*getFcn)();
87 if (value != 888) {
88 fprintf(stderr, "Test 1 failed: set value to 888, but got %d\n", value);
89 if (!debug_mode) {
90 failed_already = 1;
91 }
92 }
93 if (debug_mode) {
94 printf("Test 1 passed\n");
95 }
96
97 /*
98 * Test 2: get a second handle to the same library (this should increment
99 * the reference count), look up the symbols, call the functions, and
100 * check the results.
101 */
102
103 getFcn = (GetFcnType)PR_FindSymbolAndLibrary("My_GetValue", &lib2);
104 if (NULL == getFcn || lib != lib2) {
105 fprintf(stderr,
106 "Test 2 failed: handles for the same library are not "
107 "equal: handle 1: %p, handle 2: %p\n",
108 lib, lib2);
109 if (!debug_mode) {
110 failed_already = 1;
111 }
112 }
113 setFcn = (SetFcnType)PR_FindSymbol(lib2, "My_SetValue");
114 value = (*getFcn)();
115 if (value != 888) {
116 fprintf(stderr, "Test 2 failed: value should be 888, but got %d\n", value);
117 if (!debug_mode) {
118 failed_already = 1;
119 }
120 }
121 (*setFcn)(777);
122 value = (*getFcn)();
123 if (value != 777) {
124 fprintf(stderr, "Test 2 failed: set value to 777, but got %d\n", value);
125 if (!debug_mode) {
126 failed_already = 1;
127 }
128 goto exit_now;
129 }
130 if (debug_mode) {
131 printf("Test 2 passed\n");
132 }
133
134 /*
135 * Test 3: unload the library. The library should still be accessible
136 * via the second handle. do the same things as above.
137 */
138
139 status = PR_UnloadLibrary(lib);
140 if (PR_FAILURE == status) {
141 fprintf(stderr, "Test 3 failed: cannot unload library: (%d, %d)\n",
142 PR_GetError(), PR_GetOSError());
143 if (!debug_mode) {
144 failed_already = 1;
145 }
146 goto exit_now;
147 }
148 getFcn = (GetFcnType)PR_FindFunctionSymbol(lib2, "My_GetValue");
149 setFcn = (SetFcnType)PR_FindSymbol(lib2, "My_SetValue");
150 (*setFcn)(666);
151 value = (*getFcn)();
152 if (value != 666) {
153 fprintf(stderr, "Test 3 failed: set value to 666, but got %d\n", value);
154 if (!debug_mode) {
155 failed_already = 1;
156 }
157 goto exit_now;
158 }
159 if (debug_mode) {
160 printf("Test 3 passed\n");
161 }
162
163 /*
164 * Test 4: unload the library, testing the reference count mechanism.
165 */
166
167 status = PR_UnloadLibrary(lib2);
168 if (PR_FAILURE == status) {
169 fprintf(stderr, "Test 4 failed: cannot unload library: (%d, %d)\n",
170 PR_GetError(), PR_GetOSError());
171 if (!debug_mode) {
172 failed_already = 1;
173 }
174 goto exit_now;
175 }
176 getFcn = (GetFcnType)PR_FindFunctionSymbolAndLibrary("My_GetValue", &lib2);
177 if (NULL != getFcn) {
178 fprintf(stderr,
179 "Test 4 failed: how can we find a symbol "
180 "in an already unloaded library?\n");
181 if (!debug_mode) {
182 failed_already = 1;
183 }
184 goto exit_now;
185 }
186 if (debug_mode) {
187 printf("Test 4 passed\n");
188 }
189
190 /*
191 ** Test 5: LoadStaticLibrary()
192 */
193 {
194 PRStaticLinkTable slt[10];
195 PRLibrary* lib;
196
197 lib = PR_LoadStaticLibrary("my.dll", slt);
198 if (lib == NULL) {
199 fprintf(stderr, "Test 5: LoadStatiLibrary() failed\n");
200 goto exit_now;
201 }
202 if (debug_mode) {
203 printf("Test 5 passed\n");
204 }
205 }
206
207 goto exit_now;
208 exit_now:
209 PR_Cleanup();
210
211 if (failed_already) {
212 printf("FAILED\n");
213 return 1;
214 } else {
215 printf("PASSED\n");
216 return 0;
217 }
218 }