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: libfilename.c |
michael@0 | 9 | ** |
michael@0 | 10 | ** Description: test PR_GetLibraryFilePathname. |
michael@0 | 11 | ** |
michael@0 | 12 | ***********************************************************************/ |
michael@0 | 13 | |
michael@0 | 14 | #include "nspr.h" |
michael@0 | 15 | #include "pprio.h" |
michael@0 | 16 | #include <stdio.h> |
michael@0 | 17 | #include <stdlib.h> |
michael@0 | 18 | #include <string.h> |
michael@0 | 19 | |
michael@0 | 20 | PRBool debug_mode = PR_FALSE; |
michael@0 | 21 | |
michael@0 | 22 | static PRStatus RunTest(const char *name, PRFuncPtr addr) |
michael@0 | 23 | { |
michael@0 | 24 | char *pathname; |
michael@0 | 25 | PRFileDesc *fd; |
michael@0 | 26 | |
michael@0 | 27 | pathname = PR_GetLibraryFilePathname(name, addr); |
michael@0 | 28 | if (pathname == NULL) { |
michael@0 | 29 | fprintf(stderr, "PR_GetLibraryFilePathname failed\n"); |
michael@0 | 30 | /* we let this test pass if this function is not implemented */ |
michael@0 | 31 | if (PR_GetError() == PR_NOT_IMPLEMENTED_ERROR) { |
michael@0 | 32 | return PR_SUCCESS; |
michael@0 | 33 | } |
michael@0 | 34 | return PR_FAILURE; |
michael@0 | 35 | } |
michael@0 | 36 | |
michael@0 | 37 | if (debug_mode) printf("Pathname is %s\n", pathname); |
michael@0 | 38 | fd = PR_OpenFile(pathname, PR_RDONLY, 0); |
michael@0 | 39 | if (fd == NULL) { |
michael@0 | 40 | fprintf(stderr, "PR_Open failed: %d\n", (int)PR_GetError()); |
michael@0 | 41 | return PR_FAILURE; |
michael@0 | 42 | } |
michael@0 | 43 | if (PR_Close(fd) == PR_FAILURE) { |
michael@0 | 44 | fprintf(stderr, "PR_Close failed: %d\n", (int)PR_GetError()); |
michael@0 | 45 | return PR_FAILURE; |
michael@0 | 46 | } |
michael@0 | 47 | PR_Free(pathname); |
michael@0 | 48 | return PR_SUCCESS; |
michael@0 | 49 | } |
michael@0 | 50 | |
michael@0 | 51 | int main(int argc, char **argv) |
michael@0 | 52 | { |
michael@0 | 53 | char *name; |
michael@0 | 54 | PRFuncPtr addr; |
michael@0 | 55 | PRLibrary *lib; |
michael@0 | 56 | PRBool failed = PR_FALSE; |
michael@0 | 57 | |
michael@0 | 58 | if (argc >= 2 && strcmp(argv[1], "-d") == 0) { |
michael@0 | 59 | debug_mode = PR_TRUE; |
michael@0 | 60 | } |
michael@0 | 61 | |
michael@0 | 62 | /* First test a library that is implicitly linked. */ |
michael@0 | 63 | #ifdef WINNT |
michael@0 | 64 | name = PR_Malloc(strlen("libnspr4.dll")+1); |
michael@0 | 65 | strcpy(name, "libnspr4.dll"); |
michael@0 | 66 | #else |
michael@0 | 67 | name = PR_GetLibraryName(NULL, "nspr4"); |
michael@0 | 68 | #endif |
michael@0 | 69 | addr = (PRFuncPtr)PR_GetTCPMethods()->close; |
michael@0 | 70 | if (RunTest(name, addr) == PR_FAILURE) { |
michael@0 | 71 | failed = PR_TRUE; |
michael@0 | 72 | } |
michael@0 | 73 | PR_FreeLibraryName(name); |
michael@0 | 74 | |
michael@0 | 75 | /* Next test a library that is dynamically loaded. */ |
michael@0 | 76 | name = PR_GetLibraryName("dll", "my"); |
michael@0 | 77 | if (debug_mode) printf("Loading library %s\n", name); |
michael@0 | 78 | lib = PR_LoadLibrary(name); |
michael@0 | 79 | if (!lib) { |
michael@0 | 80 | fprintf(stderr, "PR_LoadLibrary failed\n"); |
michael@0 | 81 | exit(1); |
michael@0 | 82 | } |
michael@0 | 83 | PR_FreeLibraryName(name); |
michael@0 | 84 | name = PR_GetLibraryName(NULL, "my"); |
michael@0 | 85 | addr = PR_FindFunctionSymbol(lib, "My_GetValue"); |
michael@0 | 86 | if (RunTest(name, addr) == PR_FAILURE) { |
michael@0 | 87 | failed = PR_TRUE; |
michael@0 | 88 | } |
michael@0 | 89 | PR_FreeLibraryName(name); |
michael@0 | 90 | PR_UnloadLibrary(lib); |
michael@0 | 91 | if (failed) { |
michael@0 | 92 | printf("FAIL\n"); |
michael@0 | 93 | return 1; |
michael@0 | 94 | } |
michael@0 | 95 | printf("PASS\n"); |
michael@0 | 96 | return 0; |
michael@0 | 97 | } |