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