1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/nsprpub/pr/tests/dlltest.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,190 @@ 1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +/*********************************************************************** 1.10 +** 1.11 +** Name: dlltest.c 1.12 +** 1.13 +** Description: test dll functionality. 1.14 +** 1.15 +** Modification History: 1.16 +** 14-May-97 AGarcia- Converted the test to accomodate the debug_mode flag. 1.17 +** The debug mode will print all of the printfs associated with this test. 1.18 +** The regress mode will be the default mode. Since the regress tool limits 1.19 +** the output to a one line status:PASS or FAIL,all of the printf statements 1.20 +** have been handled with an if (debug_mode) statement. 1.21 +** 04-June-97 AGarcia removed the Test_Result function. Regress tool has been updated to 1.22 +** recognize the return code from tha main program. 1.23 +** 12-June-97 Revert to return code 0 and 1. 1.24 +***********************************************************************/ 1.25 + 1.26 +/*********************************************************************** 1.27 +** Includes 1.28 +***********************************************************************/ 1.29 +#include "prinit.h" 1.30 +#include "prlink.h" 1.31 +#include "prmem.h" 1.32 +#include "prerror.h" 1.33 + 1.34 +#include "plstr.h" 1.35 + 1.36 +#include <stdio.h> 1.37 +#include <stdlib.h> 1.38 + 1.39 +typedef PRIntn (PR_CALLBACK *GetFcnType)(void); 1.40 +typedef void (PR_CALLBACK *SetFcnType)(PRIntn); 1.41 + 1.42 +PRIntn failed_already=0; 1.43 +PRIntn debug_mode; 1.44 + 1.45 +int main(int argc, char** argv) 1.46 +{ 1.47 + PRLibrary *lib, *lib2; /* two handles to the same library */ 1.48 + GetFcnType getFcn; 1.49 + SetFcnType setFcn; 1.50 + PRIntn value; 1.51 + PRStatus status; 1.52 + char *libName; 1.53 + 1.54 + if (argc >= 2 && PL_strcmp(argv[1], "-d") == 0) { 1.55 + debug_mode = 1; 1.56 + } 1.57 + 1.58 + PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0); 1.59 + PR_STDIO_INIT(); 1.60 + 1.61 + /* 1.62 + * Test 1: load the library, look up the symbols, call the functions, 1.63 + * and check the results. 1.64 + */ 1.65 + 1.66 + libName = PR_GetLibraryName("dll", "my"); 1.67 + if (debug_mode) printf("Loading library %s\n", libName); 1.68 + lib = PR_LoadLibrary(libName); 1.69 + PR_FreeLibraryName(libName); 1.70 + if (lib == NULL) { 1.71 + PRInt32 textLength = PR_GetErrorTextLength(); 1.72 + char *text = (char*)PR_MALLOC(textLength + 1); 1.73 + text[0] = '\0'; 1.74 + (void)PR_GetErrorText(text); 1.75 + fprintf( 1.76 + stderr, "PR_LoadLibrary failed (%d, %d, %s)\n", 1.77 + PR_GetError(), PR_GetOSError(), text); 1.78 + if (!debug_mode) failed_already=1; 1.79 + } 1.80 + getFcn = (GetFcnType) PR_FindSymbol(lib, "My_GetValue"); 1.81 + setFcn = (SetFcnType) PR_FindFunctionSymbol(lib, "My_SetValue"); 1.82 + (*setFcn)(888); 1.83 + value = (*getFcn)(); 1.84 + if (value != 888) { 1.85 + fprintf(stderr, "Test 1 failed: set value to 888, but got %d\n", value); 1.86 + if (!debug_mode) failed_already=1; 1.87 + } 1.88 + if (debug_mode) printf("Test 1 passed\n"); 1.89 + 1.90 + /* 1.91 + * Test 2: get a second handle to the same library (this should increment 1.92 + * the reference count), look up the symbols, call the functions, and 1.93 + * check the results. 1.94 + */ 1.95 + 1.96 + getFcn = (GetFcnType) PR_FindSymbolAndLibrary("My_GetValue", &lib2); 1.97 + if (NULL == getFcn || lib != lib2) { 1.98 + fprintf(stderr, "Test 2 failed: handles for the same library are not " 1.99 + "equal: handle 1: %p, handle 2: %p\n", lib, lib2); 1.100 + if (!debug_mode) failed_already=1; 1.101 + } 1.102 + setFcn = (SetFcnType) PR_FindSymbol(lib2, "My_SetValue"); 1.103 + value = (*getFcn)(); 1.104 + if (value != 888) { 1.105 + fprintf(stderr, "Test 2 failed: value should be 888, but got %d\n", 1.106 + value); 1.107 + if (!debug_mode) failed_already=1; 1.108 + } 1.109 + (*setFcn)(777); 1.110 + value = (*getFcn)(); 1.111 + if (value != 777) { 1.112 + fprintf(stderr, "Test 2 failed: set value to 777, but got %d\n", value); 1.113 + if (!debug_mode) failed_already=1; 1.114 + goto exit_now; 1.115 + } 1.116 + if (debug_mode) printf("Test 2 passed\n"); 1.117 + 1.118 + /* 1.119 + * Test 3: unload the library. The library should still be accessible 1.120 + * via the second handle. do the same things as above. 1.121 + */ 1.122 + 1.123 + status = PR_UnloadLibrary(lib); 1.124 + if (PR_FAILURE == status) { 1.125 + fprintf(stderr, "Test 3 failed: cannot unload library: (%d, %d)\n", 1.126 + PR_GetError(), PR_GetOSError()); 1.127 + if (!debug_mode) failed_already=1; 1.128 + goto exit_now; 1.129 + } 1.130 + getFcn = (GetFcnType) PR_FindFunctionSymbol(lib2, "My_GetValue"); 1.131 + setFcn = (SetFcnType) PR_FindSymbol(lib2, "My_SetValue"); 1.132 + (*setFcn)(666); 1.133 + value = (*getFcn)(); 1.134 + if (value != 666) { 1.135 + fprintf(stderr, "Test 3 failed: set value to 666, but got %d\n", value); 1.136 + if (!debug_mode) failed_already=1; 1.137 + goto exit_now; 1.138 + } 1.139 + if (debug_mode) printf("Test 3 passed\n"); 1.140 + 1.141 + /* 1.142 + * Test 4: unload the library, testing the reference count mechanism. 1.143 + */ 1.144 + 1.145 + status = PR_UnloadLibrary(lib2); 1.146 + if (PR_FAILURE == status) { 1.147 + fprintf(stderr, "Test 4 failed: cannot unload library: (%d, %d)\n", 1.148 + PR_GetError(), PR_GetOSError()); 1.149 + if (!debug_mode) failed_already=1; 1.150 + goto exit_now; 1.151 + } 1.152 + getFcn = (GetFcnType) PR_FindFunctionSymbolAndLibrary("My_GetValue", &lib2); 1.153 + if (NULL != getFcn) { 1.154 + fprintf(stderr, "Test 4 failed: how can we find a symbol " 1.155 + "in an already unloaded library?\n"); 1.156 + if (!debug_mode) failed_already=1; 1.157 + goto exit_now; 1.158 + } 1.159 + if (debug_mode) { 1.160 + printf("Test 4 passed\n"); 1.161 + } 1.162 + 1.163 + /* 1.164 + ** Test 5: LoadStaticLibrary() 1.165 + */ 1.166 + { 1.167 + PRStaticLinkTable slt[10]; 1.168 + PRLibrary *lib; 1.169 + 1.170 + lib = PR_LoadStaticLibrary( "my.dll", slt ); 1.171 + if ( lib == NULL ) 1.172 + { 1.173 + fprintf(stderr, "Test 5: LoadStatiLibrary() failed\n" ); 1.174 + goto exit_now; 1.175 + } 1.176 + if (debug_mode) 1.177 + { 1.178 + printf("Test 5 passed\n"); 1.179 + } 1.180 + } 1.181 + 1.182 + goto exit_now; 1.183 +exit_now: 1.184 + PR_Cleanup(); 1.185 + 1.186 + if (failed_already) { 1.187 + printf("FAILED\n"); 1.188 + return 1; 1.189 + } else { 1.190 + printf("PASSED\n"); 1.191 + return 0; 1.192 + } 1.193 +}