Debian Codegraph @

nsprpub/pr/tests/libfilename.c

raw
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6 /***********************************************************************
7 **
8 ** Name: libfilename.c
9 **
10 ** Description: test PR_GetLibraryFilePathname.
11 **
12 ***********************************************************************/
13
14 #include "nspr.h"
15 #include "pprio.h"
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19
20 PRBool debug_mode = PR_FALSE;
21
22 static PRStatus RunTest(const char* name, PRFuncPtr addr) {
23 char* pathname;
24 PRFileDesc* fd;
25
26 pathname = PR_GetLibraryFilePathname(name, addr);
27 if (pathname == NULL) {
28 fprintf(stderr, "PR_GetLibraryFilePathname failed\n");
29 /* we let this test pass if this function is not implemented */
30 if (PR_GetError() == PR_NOT_IMPLEMENTED_ERROR) {
31 return PR_SUCCESS;
32 }
33 return PR_FAILURE;
34 }
35
36 if (debug_mode) {
37 printf("Pathname is %s\n", pathname);
38 }
39 fd = PR_OpenFile(pathname, PR_RDONLY, 0);
40 if (fd == NULL) {
41 fprintf(stderr, "PR_Open failed: %d\n", (int)PR_GetError());
42 return PR_FAILURE;
43 }
44 if (PR_Close(fd) == PR_FAILURE) {
45 fprintf(stderr, "PR_Close failed: %d\n", (int)PR_GetError());
46 return PR_FAILURE;
47 }
48 PR_Free(pathname);
49 return PR_SUCCESS;
50 }
51
52 int main(int argc, char** argv) {
53 char* name;
54 PRFuncPtr addr;
55 PRLibrary* lib;
56 PRBool failed = PR_FALSE;
57
58 if (argc >= 2 && strcmp(argv[1], "-d") == 0) {
59 debug_mode = PR_TRUE;
60 }
61
62 /* First test a library that is implicitly linked. */
63 #ifdef WINNT
64 name = PR_Malloc(strlen("libnspr4.dll") + 1);
65 strcpy(name, "libnspr4.dll");
66 #else
67 name = PR_GetLibraryName(NULL, "nspr4");
68 #endif
69 addr = (PRFuncPtr)PR_GetTCPMethods()->close;
70 if (RunTest(name, addr) == PR_FAILURE) {
71 failed = PR_TRUE;
72 }
73 PR_FreeLibraryName(name);
74
75 /* Next test a library that is dynamically loaded. */
76 name = PR_GetLibraryName("dll", "my");
77 if (debug_mode) {
78 printf("Loading library %s\n", name);
79 }
80 lib = PR_LoadLibrary(name);
81 if (!lib) {
82 fprintf(stderr, "PR_LoadLibrary failed\n");
83 exit(1);
84 }
85 PR_FreeLibraryName(name);
86 name = PR_GetLibraryName(NULL, "my");
87 addr = PR_FindFunctionSymbol(lib, "My_GetValue");
88 if (RunTest(name, addr) == PR_FAILURE) {
89 failed = PR_TRUE;
90 }
91 PR_FreeLibraryName(name);
92 PR_UnloadLibrary(lib);
93 if (failed) {
94 printf("FAIL\n");
95 return 1;
96 }
97 printf("PASS\n");
98 return 0;
99 }