1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/nsprpub/pr/tests/libfilename.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,97 @@ 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: libfilename.c 1.12 +** 1.13 +** Description: test PR_GetLibraryFilePathname. 1.14 +** 1.15 +***********************************************************************/ 1.16 + 1.17 +#include "nspr.h" 1.18 +#include "pprio.h" 1.19 +#include <stdio.h> 1.20 +#include <stdlib.h> 1.21 +#include <string.h> 1.22 + 1.23 +PRBool debug_mode = PR_FALSE; 1.24 + 1.25 +static PRStatus RunTest(const char *name, PRFuncPtr addr) 1.26 +{ 1.27 + char *pathname; 1.28 + PRFileDesc *fd; 1.29 + 1.30 + pathname = PR_GetLibraryFilePathname(name, addr); 1.31 + if (pathname == NULL) { 1.32 + fprintf(stderr, "PR_GetLibraryFilePathname failed\n"); 1.33 + /* we let this test pass if this function is not implemented */ 1.34 + if (PR_GetError() == PR_NOT_IMPLEMENTED_ERROR) { 1.35 + return PR_SUCCESS; 1.36 + } 1.37 + return PR_FAILURE; 1.38 + } 1.39 + 1.40 + if (debug_mode) printf("Pathname is %s\n", pathname); 1.41 + fd = PR_OpenFile(pathname, PR_RDONLY, 0); 1.42 + if (fd == NULL) { 1.43 + fprintf(stderr, "PR_Open failed: %d\n", (int)PR_GetError()); 1.44 + return PR_FAILURE; 1.45 + } 1.46 + if (PR_Close(fd) == PR_FAILURE) { 1.47 + fprintf(stderr, "PR_Close failed: %d\n", (int)PR_GetError()); 1.48 + return PR_FAILURE; 1.49 + } 1.50 + PR_Free(pathname); 1.51 + return PR_SUCCESS; 1.52 +} 1.53 + 1.54 +int main(int argc, char **argv) 1.55 +{ 1.56 + char *name; 1.57 + PRFuncPtr addr; 1.58 + PRLibrary *lib; 1.59 + PRBool failed = PR_FALSE; 1.60 + 1.61 + if (argc >= 2 && strcmp(argv[1], "-d") == 0) { 1.62 + debug_mode = PR_TRUE; 1.63 + } 1.64 + 1.65 + /* First test a library that is implicitly linked. */ 1.66 +#ifdef WINNT 1.67 + name = PR_Malloc(strlen("libnspr4.dll")+1); 1.68 + strcpy(name, "libnspr4.dll"); 1.69 +#else 1.70 + name = PR_GetLibraryName(NULL, "nspr4"); 1.71 +#endif 1.72 + addr = (PRFuncPtr)PR_GetTCPMethods()->close; 1.73 + if (RunTest(name, addr) == PR_FAILURE) { 1.74 + failed = PR_TRUE; 1.75 + } 1.76 + PR_FreeLibraryName(name); 1.77 + 1.78 + /* Next test a library that is dynamically loaded. */ 1.79 + name = PR_GetLibraryName("dll", "my"); 1.80 + if (debug_mode) printf("Loading library %s\n", name); 1.81 + lib = PR_LoadLibrary(name); 1.82 + if (!lib) { 1.83 + fprintf(stderr, "PR_LoadLibrary failed\n"); 1.84 + exit(1); 1.85 + } 1.86 + PR_FreeLibraryName(name); 1.87 + name = PR_GetLibraryName(NULL, "my"); 1.88 + addr = PR_FindFunctionSymbol(lib, "My_GetValue"); 1.89 + if (RunTest(name, addr) == PR_FAILURE) { 1.90 + failed = PR_TRUE; 1.91 + } 1.92 + PR_FreeLibraryName(name); 1.93 + PR_UnloadLibrary(lib); 1.94 + if (failed) { 1.95 + printf("FAIL\n"); 1.96 + return 1; 1.97 + } 1.98 + printf("PASS\n"); 1.99 + return 0; 1.100 +}