michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /*********************************************************************** michael@0: ** michael@0: ** Name: dlltest.c michael@0: ** michael@0: ** Description: test dll functionality. michael@0: ** michael@0: ** Modification History: michael@0: ** 14-May-97 AGarcia- Converted the test to accomodate the debug_mode flag. michael@0: ** The debug mode will print all of the printfs associated with this test. michael@0: ** The regress mode will be the default mode. Since the regress tool limits michael@0: ** the output to a one line status:PASS or FAIL,all of the printf statements michael@0: ** have been handled with an if (debug_mode) statement. michael@0: ** 04-June-97 AGarcia removed the Test_Result function. Regress tool has been updated to michael@0: ** recognize the return code from tha main program. michael@0: ** 12-June-97 Revert to return code 0 and 1. michael@0: ***********************************************************************/ michael@0: michael@0: /*********************************************************************** michael@0: ** Includes michael@0: ***********************************************************************/ michael@0: #include "prinit.h" michael@0: #include "prlink.h" michael@0: #include "prmem.h" michael@0: #include "prerror.h" michael@0: michael@0: #include "plstr.h" michael@0: michael@0: #include michael@0: #include michael@0: michael@0: typedef PRIntn (PR_CALLBACK *GetFcnType)(void); michael@0: typedef void (PR_CALLBACK *SetFcnType)(PRIntn); michael@0: michael@0: PRIntn failed_already=0; michael@0: PRIntn debug_mode; michael@0: michael@0: int main(int argc, char** argv) michael@0: { michael@0: PRLibrary *lib, *lib2; /* two handles to the same library */ michael@0: GetFcnType getFcn; michael@0: SetFcnType setFcn; michael@0: PRIntn value; michael@0: PRStatus status; michael@0: char *libName; michael@0: michael@0: if (argc >= 2 && PL_strcmp(argv[1], "-d") == 0) { michael@0: debug_mode = 1; michael@0: } michael@0: michael@0: PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0); michael@0: PR_STDIO_INIT(); michael@0: michael@0: /* michael@0: * Test 1: load the library, look up the symbols, call the functions, michael@0: * and check the results. michael@0: */ michael@0: michael@0: libName = PR_GetLibraryName("dll", "my"); michael@0: if (debug_mode) printf("Loading library %s\n", libName); michael@0: lib = PR_LoadLibrary(libName); michael@0: PR_FreeLibraryName(libName); michael@0: if (lib == NULL) { michael@0: PRInt32 textLength = PR_GetErrorTextLength(); michael@0: char *text = (char*)PR_MALLOC(textLength + 1); michael@0: text[0] = '\0'; michael@0: (void)PR_GetErrorText(text); michael@0: fprintf( michael@0: stderr, "PR_LoadLibrary failed (%d, %d, %s)\n", michael@0: PR_GetError(), PR_GetOSError(), text); michael@0: if (!debug_mode) failed_already=1; michael@0: } michael@0: getFcn = (GetFcnType) PR_FindSymbol(lib, "My_GetValue"); michael@0: setFcn = (SetFcnType) PR_FindFunctionSymbol(lib, "My_SetValue"); michael@0: (*setFcn)(888); michael@0: value = (*getFcn)(); michael@0: if (value != 888) { michael@0: fprintf(stderr, "Test 1 failed: set value to 888, but got %d\n", value); michael@0: if (!debug_mode) failed_already=1; michael@0: } michael@0: if (debug_mode) printf("Test 1 passed\n"); michael@0: michael@0: /* michael@0: * Test 2: get a second handle to the same library (this should increment michael@0: * the reference count), look up the symbols, call the functions, and michael@0: * check the results. michael@0: */ michael@0: michael@0: getFcn = (GetFcnType) PR_FindSymbolAndLibrary("My_GetValue", &lib2); michael@0: if (NULL == getFcn || lib != lib2) { michael@0: fprintf(stderr, "Test 2 failed: handles for the same library are not " michael@0: "equal: handle 1: %p, handle 2: %p\n", lib, lib2); michael@0: if (!debug_mode) failed_already=1; michael@0: } michael@0: setFcn = (SetFcnType) PR_FindSymbol(lib2, "My_SetValue"); michael@0: value = (*getFcn)(); michael@0: if (value != 888) { michael@0: fprintf(stderr, "Test 2 failed: value should be 888, but got %d\n", michael@0: value); michael@0: if (!debug_mode) failed_already=1; michael@0: } michael@0: (*setFcn)(777); michael@0: value = (*getFcn)(); michael@0: if (value != 777) { michael@0: fprintf(stderr, "Test 2 failed: set value to 777, but got %d\n", value); michael@0: if (!debug_mode) failed_already=1; michael@0: goto exit_now; michael@0: } michael@0: if (debug_mode) printf("Test 2 passed\n"); michael@0: michael@0: /* michael@0: * Test 3: unload the library. The library should still be accessible michael@0: * via the second handle. do the same things as above. michael@0: */ michael@0: michael@0: status = PR_UnloadLibrary(lib); michael@0: if (PR_FAILURE == status) { michael@0: fprintf(stderr, "Test 3 failed: cannot unload library: (%d, %d)\n", michael@0: PR_GetError(), PR_GetOSError()); michael@0: if (!debug_mode) failed_already=1; michael@0: goto exit_now; michael@0: } michael@0: getFcn = (GetFcnType) PR_FindFunctionSymbol(lib2, "My_GetValue"); michael@0: setFcn = (SetFcnType) PR_FindSymbol(lib2, "My_SetValue"); michael@0: (*setFcn)(666); michael@0: value = (*getFcn)(); michael@0: if (value != 666) { michael@0: fprintf(stderr, "Test 3 failed: set value to 666, but got %d\n", value); michael@0: if (!debug_mode) failed_already=1; michael@0: goto exit_now; michael@0: } michael@0: if (debug_mode) printf("Test 3 passed\n"); michael@0: michael@0: /* michael@0: * Test 4: unload the library, testing the reference count mechanism. michael@0: */ michael@0: michael@0: status = PR_UnloadLibrary(lib2); michael@0: if (PR_FAILURE == status) { michael@0: fprintf(stderr, "Test 4 failed: cannot unload library: (%d, %d)\n", michael@0: PR_GetError(), PR_GetOSError()); michael@0: if (!debug_mode) failed_already=1; michael@0: goto exit_now; michael@0: } michael@0: getFcn = (GetFcnType) PR_FindFunctionSymbolAndLibrary("My_GetValue", &lib2); michael@0: if (NULL != getFcn) { michael@0: fprintf(stderr, "Test 4 failed: how can we find a symbol " michael@0: "in an already unloaded library?\n"); michael@0: if (!debug_mode) failed_already=1; michael@0: goto exit_now; michael@0: } michael@0: if (debug_mode) { michael@0: printf("Test 4 passed\n"); michael@0: } michael@0: michael@0: /* michael@0: ** Test 5: LoadStaticLibrary() michael@0: */ michael@0: { michael@0: PRStaticLinkTable slt[10]; michael@0: PRLibrary *lib; michael@0: michael@0: lib = PR_LoadStaticLibrary( "my.dll", slt ); michael@0: if ( lib == NULL ) michael@0: { michael@0: fprintf(stderr, "Test 5: LoadStatiLibrary() failed\n" ); michael@0: goto exit_now; michael@0: } michael@0: if (debug_mode) michael@0: { michael@0: printf("Test 5 passed\n"); michael@0: } michael@0: } michael@0: michael@0: goto exit_now; michael@0: exit_now: michael@0: PR_Cleanup(); michael@0: michael@0: if (failed_already) { michael@0: printf("FAILED\n"); michael@0: return 1; michael@0: } else { michael@0: printf("PASSED\n"); michael@0: return 0; michael@0: } michael@0: }