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