nsprpub/pr/tests/dlltest.c

Wed, 31 Dec 2014 06:55:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:50 +0100
changeset 2
7e26c7da4463
permissions
-rw-r--r--

Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2

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

mercurial