dlltest.c (5922B)
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 60 /* 61 * Test 1: load the library, look up the symbols, call the functions, 62 * and check the results. 63 */ 64 65 libName = PR_GetLibraryName("dll", "my"); 66 if (debug_mode) { 67 printf("Loading library %s\n", libName); 68 } 69 lib = PR_LoadLibrary(libName); 70 PR_FreeLibraryName(libName); 71 if (lib == NULL) { 72 PRInt32 textLength = PR_GetErrorTextLength(); 73 char* text = (char*)PR_MALLOC(textLength + 1); 74 text[0] = '\0'; 75 (void)PR_GetErrorText(text); 76 fprintf(stderr, "PR_LoadLibrary failed (%d, %d, %s)\n", PR_GetError(), 77 PR_GetOSError(), text); 78 if (!debug_mode) { 79 failed_already = 1; 80 } 81 } 82 getFcn = (GetFcnType)PR_FindSymbol(lib, "My_GetValue"); 83 setFcn = (SetFcnType)PR_FindFunctionSymbol(lib, "My_SetValue"); 84 (*setFcn)(888); 85 value = (*getFcn)(); 86 if (value != 888) { 87 fprintf(stderr, "Test 1 failed: set value to 888, but got %d\n", value); 88 if (!debug_mode) { 89 failed_already = 1; 90 } 91 } 92 if (debug_mode) { 93 printf("Test 1 passed\n"); 94 } 95 96 /* 97 * Test 2: get a second handle to the same library (this should increment 98 * the reference count), look up the symbols, call the functions, and 99 * check the results. 100 */ 101 102 getFcn = (GetFcnType)PR_FindSymbolAndLibrary("My_GetValue", &lib2); 103 if (NULL == getFcn || lib != lib2) { 104 fprintf(stderr, 105 "Test 2 failed: handles for the same library are not " 106 "equal: handle 1: %p, handle 2: %p\n", 107 lib, lib2); 108 if (!debug_mode) { 109 failed_already = 1; 110 } 111 } 112 setFcn = (SetFcnType)PR_FindSymbol(lib2, "My_SetValue"); 113 value = (*getFcn)(); 114 if (value != 888) { 115 fprintf(stderr, "Test 2 failed: value should be 888, but got %d\n", value); 116 if (!debug_mode) { 117 failed_already = 1; 118 } 119 } 120 (*setFcn)(777); 121 value = (*getFcn)(); 122 if (value != 777) { 123 fprintf(stderr, "Test 2 failed: set value to 777, but got %d\n", value); 124 if (!debug_mode) { 125 failed_already = 1; 126 } 127 goto exit_now; 128 } 129 if (debug_mode) { 130 printf("Test 2 passed\n"); 131 } 132 133 /* 134 * Test 3: unload the library. The library should still be accessible 135 * via the second handle. do the same things as above. 136 */ 137 138 status = PR_UnloadLibrary(lib); 139 if (PR_FAILURE == status) { 140 fprintf(stderr, "Test 3 failed: cannot unload library: (%d, %d)\n", 141 PR_GetError(), PR_GetOSError()); 142 if (!debug_mode) { 143 failed_already = 1; 144 } 145 goto exit_now; 146 } 147 getFcn = (GetFcnType)PR_FindFunctionSymbol(lib2, "My_GetValue"); 148 setFcn = (SetFcnType)PR_FindSymbol(lib2, "My_SetValue"); 149 (*setFcn)(666); 150 value = (*getFcn)(); 151 if (value != 666) { 152 fprintf(stderr, "Test 3 failed: set value to 666, but got %d\n", value); 153 if (!debug_mode) { 154 failed_already = 1; 155 } 156 goto exit_now; 157 } 158 if (debug_mode) { 159 printf("Test 3 passed\n"); 160 } 161 162 /* 163 * Test 4: unload the library, testing the reference count mechanism. 164 */ 165 166 status = PR_UnloadLibrary(lib2); 167 if (PR_FAILURE == status) { 168 fprintf(stderr, "Test 4 failed: cannot unload library: (%d, %d)\n", 169 PR_GetError(), PR_GetOSError()); 170 if (!debug_mode) { 171 failed_already = 1; 172 } 173 goto exit_now; 174 } 175 getFcn = (GetFcnType)PR_FindFunctionSymbolAndLibrary("My_GetValue", &lib2); 176 if (NULL != getFcn) { 177 fprintf(stderr, 178 "Test 4 failed: how can we find a symbol " 179 "in an already unloaded library?\n"); 180 if (!debug_mode) { 181 failed_already = 1; 182 } 183 goto exit_now; 184 } 185 if (debug_mode) { 186 printf("Test 4 passed\n"); 187 } 188 189 /* 190 ** Test 5: LoadStaticLibrary() 191 */ 192 { 193 PRStaticLinkTable slt[10]; 194 PRLibrary* lib; 195 196 lib = PR_LoadStaticLibrary("my.dll", slt); 197 if (lib == NULL) { 198 fprintf(stderr, "Test 5: LoadStatiLibrary() failed\n"); 199 goto exit_now; 200 } 201 if (debug_mode) { 202 printf("Test 5 passed\n"); 203 } 204 } 205 206 goto exit_now; 207 exit_now: 208 PR_Cleanup(); 209 210 if (failed_already) { 211 printf("FAILED\n"); 212 return 1; 213 } else { 214 printf("PASSED\n"); 215 return 0; 216 } 217 }