home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / nsprpub / pr / tests / dlltest.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  6.3 KB  |  202 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /*
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  * 
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  * 
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. /***********************************************************************
  20. **
  21. ** Name: dlltest.c
  22. **
  23. ** Description: test dll functionality.
  24. **
  25. ** Modification History:
  26. ** 14-May-97 AGarcia- Converted the test to accomodate the debug_mode flag.
  27. **             The debug mode will print all of the printfs associated with this test.
  28. **             The regress mode will be the default mode. Since the regress tool limits
  29. **           the output to a one line status:PASS or FAIL,all of the printf statements
  30. **             have been handled with an if (debug_mode) statement.
  31. ** 04-June-97 AGarcia removed the Test_Result function. Regress tool has been updated to
  32. **            recognize the return code from tha main program.
  33. ** 12-June-97 Revert to return code 0 and 1.
  34. ***********************************************************************/
  35.  
  36. /***********************************************************************
  37. ** Includes
  38. ***********************************************************************/
  39. #include "prinit.h"
  40. #include "prlink.h"
  41. #include "prmem.h"
  42. #include "prerror.h"
  43.  
  44. #include "plstr.h"
  45.  
  46. #include <stdio.h>
  47. #include <stdlib.h>
  48.  
  49. typedef PRIntn (PR_CALLBACK *GetFcnType)(void);
  50. typedef void (PR_CALLBACK *SetFcnType)(PRIntn);
  51.  
  52. PRIntn failed_already=0;
  53. PRIntn debug_mode;
  54.  
  55. int main(int argc, char** argv)
  56. {
  57.     PRLibrary *lib, *lib2;  /* two handles to the same library */
  58.     GetFcnType getFcn;
  59.     SetFcnType setFcn;
  60.     PRIntn value;
  61.     PRStatus status;
  62.     char *libName;
  63.  
  64.     if (argc >= 2 && PL_strcmp(argv[1], "-d") == 0) {
  65.         debug_mode = 1;
  66.     }
  67.  
  68.     PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
  69.     PR_STDIO_INIT();
  70.  
  71.     /*
  72.      * Test 1: load the library, look up the symbols, call the functions,
  73.      * and check the results.
  74.      */
  75.  
  76.     libName = PR_GetLibraryName("dll", "my");
  77.     if (debug_mode) printf("Loading library %s\n", libName);
  78.     lib = PR_LoadLibrary(libName);
  79.     PR_FreeLibraryName(libName);
  80.     if (lib == NULL) {
  81.         PRInt32 textLength = PR_GetErrorTextLength();
  82.         char *text = (char*)PR_MALLOC(textLength);
  83.         (void)PR_GetErrorText(text);
  84.         fprintf(
  85.             stderr, "PR_LoadLibrary failed (%d, %d, %s)\n",
  86.             PR_GetError(), PR_GetOSError(), text);
  87.             if (!debug_mode) failed_already=1;
  88.     }
  89.     getFcn = (GetFcnType) PR_FindSymbol(lib, "My_GetValue");
  90.     setFcn = (SetFcnType) PR_FindSymbol(lib, "My_SetValue");
  91.     (*setFcn)(888);
  92.     value = (*getFcn)();
  93.     if (value != 888) {
  94.     fprintf(stderr, "Test 1 failed: set value to 888, but got %d\n", value);
  95.     if (!debug_mode) failed_already=1;
  96.     }
  97.     if (debug_mode) printf("Test 1 passed\n");
  98.  
  99.     /*
  100.      * Test 2: get a second handle to the same library (this should increment
  101.      * the reference count), look up the symbols, call the functions, and
  102.      * check the results.
  103.      */
  104.  
  105.     getFcn = (GetFcnType) PR_FindSymbolAndLibrary("My_GetValue", &lib2);
  106.     if (lib != lib2) {
  107.     fprintf(stderr, "Test 2 failed: handles for the same library are not "
  108.         "equal: handle 1: %p, handle 2: %p\n", lib, lib2);
  109.         if (!debug_mode) failed_already=1;
  110.  
  111.     }
  112.     setFcn = (SetFcnType) PR_FindSymbol(lib2, "My_SetValue");
  113.     value = (*getFcn)();
  114.     if (value != 888) {
  115.     fprintf(stderr, "Test 2 failed: value should be 888, but got %d\n",
  116.         value);
  117.         if (!debug_mode) failed_already=1;
  118.  
  119.     }
  120.     (*setFcn)(777);
  121.     value = (*getFcn)();
  122.     if (value != 777) {
  123.     fprintf(stderr, "Test 2 failed: set value to 777, but got %d\n", value);
  124.         if (!debug_mode) failed_already=1;
  125.     goto exit_now;
  126.     }
  127.     if (debug_mode) printf("Test 2 passed\n");
  128.  
  129.     /*
  130.      * Test 3: unload the library.  The library should still be accessible
  131.      * via the second handle.  do the same things as above.
  132.      */
  133.  
  134.     status = PR_UnloadLibrary(lib);
  135.     if (PR_FAILURE == status) {
  136.     fprintf(stderr, "Test 3 failed: cannot unload library: (%d, %d)\n",
  137.         PR_GetError(), PR_GetOSError());
  138.     if (!debug_mode) failed_already=1;
  139.     goto exit_now;
  140.     }
  141.     getFcn = (GetFcnType) PR_FindSymbol(lib2, "My_GetValue");
  142.     setFcn = (SetFcnType) PR_FindSymbol(lib2, "My_SetValue");
  143.     (*setFcn)(666);
  144.     value = (*getFcn)();
  145.     if (value != 666) {
  146.     fprintf(stderr, "Test 3 failed: set value to 666, but got %d\n", value);
  147.     if (!debug_mode) failed_already=1;
  148.     goto exit_now;
  149.     }
  150.     if (debug_mode) printf("Test 3 passed\n");
  151.  
  152.     /*
  153.      * Test 4: unload the library, testing the reference count mechanism.
  154.      */
  155.  
  156.     status = PR_UnloadLibrary(lib2);
  157.     if (PR_FAILURE == status) {
  158.     fprintf(stderr, "Test 4 failed: cannot unload library: (%d, %d)\n",
  159.         PR_GetError(), PR_GetOSError());
  160.     if (!debug_mode) failed_already=1;
  161.     goto exit_now;
  162.     }
  163.     status = PR_UnloadLibrary(lib2);
  164.     if (PR_FAILURE != status || PR_GetError() != PR_INVALID_ARGUMENT_ERROR) {
  165.     fprintf(stderr, "Test 4 failed: how can an already unloaded library "
  166.                 "be unloaded again?\n");
  167.     if (!debug_mode) failed_already=1;
  168.     goto exit_now;
  169.     }
  170.     if (debug_mode) {
  171.         printf("Test 4 passed\n");
  172.     }
  173.  
  174.     /*
  175.     ** Test 5: LoadStaticLibrary()
  176.     */
  177.     {
  178.         PRStaticLinkTable   slt[10];
  179.         PRLibrary           *lib;
  180.         
  181.         lib = PR_LoadStaticLibrary( "my.dll", slt );
  182.         if ( lib == NULL )
  183.         {
  184.             printf("dlltest: Test 5: LoadStatiLibrary() failed\n" );
  185.             goto exit_now;
  186.         }
  187.         printf("Test 5 passed\n");
  188.     }
  189.  
  190.     goto exit_now;
  191. exit_now: 
  192.     PR_Cleanup();
  193.  
  194.     if (failed_already) {
  195.         printf("FAILED\n");
  196.         return 1;
  197.     } else {
  198.         printf("PASSED\n");
  199.         return 0;
  200.     }
  201. }
  202.