Back to index

lightning-sunbird  0.9+nobinonly
dlltest.c
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is the Netscape Portable Runtime (NSPR).
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998-2000
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 /***********************************************************************
00039 **
00040 ** Name: dlltest.c
00041 **
00042 ** Description: test dll functionality.
00043 **
00044 ** Modification History:
00045 ** 14-May-97 AGarcia- Converted the test to accomodate the debug_mode flag.
00046 **              The debug mode will print all of the printfs associated with this test.
00047 **                    The regress mode will be the default mode. Since the regress tool limits
00048 **           the output to a one line status:PASS or FAIL,all of the printf statements
00049 **                    have been handled with an if (debug_mode) statement.
00050 ** 04-June-97 AGarcia removed the Test_Result function. Regress tool has been updated to
00051 **                   recognize the return code from tha main program.
00052 ** 12-June-97 Revert to return code 0 and 1.
00053 ***********************************************************************/
00054 
00055 /***********************************************************************
00056 ** Includes
00057 ***********************************************************************/
00058 #include "prinit.h"
00059 #include "prlink.h"
00060 #include "prmem.h"
00061 #include "prerror.h"
00062 
00063 #include "plstr.h"
00064 
00065 #include <stdio.h>
00066 #include <stdlib.h>
00067 
00068 typedef PRIntn (PR_CALLBACK *GetFcnType)(void);
00069 typedef void (PR_CALLBACK *SetFcnType)(PRIntn);
00070 
00071 PRIntn failed_already=0;
00072 PRIntn debug_mode;
00073 
00074 int main(int argc, char** argv)
00075 {
00076     PRLibrary *lib, *lib2;  /* two handles to the same library */
00077     GetFcnType getFcn;
00078     SetFcnType setFcn;
00079     PRIntn value;
00080     PRStatus status;
00081     char *libName;
00082 
00083     if (argc >= 2 && PL_strcmp(argv[1], "-d") == 0) {
00084         debug_mode = 1;
00085     }
00086 
00087     PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
00088     PR_STDIO_INIT();
00089 
00090     /*
00091      * Test 1: load the library, look up the symbols, call the functions,
00092      * and check the results.
00093      */
00094 
00095     libName = PR_GetLibraryName("dll", "my");
00096     if (debug_mode) printf("Loading library %s\n", libName);
00097     lib = PR_LoadLibrary(libName);
00098     PR_FreeLibraryName(libName);
00099     if (lib == NULL) {
00100         PRInt32 textLength = PR_GetErrorTextLength();
00101         char *text = (char*)PR_MALLOC(textLength);
00102         (void)PR_GetErrorText(text);
00103         fprintf(
00104             stderr, "PR_LoadLibrary failed (%d, %d, %s)\n",
00105             PR_GetError(), PR_GetOSError(), text);
00106         if (!debug_mode) failed_already=1;
00107     }
00108     getFcn = (GetFcnType) PR_FindSymbol(lib, "My_GetValue");
00109     setFcn = (SetFcnType) PR_FindFunctionSymbol(lib, "My_SetValue");
00110     (*setFcn)(888);
00111     value = (*getFcn)();
00112     if (value != 888) {
00113         fprintf(stderr, "Test 1 failed: set value to 888, but got %d\n", value);
00114         if (!debug_mode) failed_already=1;
00115     }
00116     if (debug_mode) printf("Test 1 passed\n");
00117 
00118     /*
00119      * Test 2: get a second handle to the same library (this should increment
00120      * the reference count), look up the symbols, call the functions, and
00121      * check the results.
00122      */
00123 
00124     getFcn = (GetFcnType) PR_FindSymbolAndLibrary("My_GetValue", &lib2);
00125     if (NULL == getFcn || lib != lib2) {
00126         fprintf(stderr, "Test 2 failed: handles for the same library are not "
00127             "equal: handle 1: %p, handle 2: %p\n", lib, lib2);
00128         if (!debug_mode) failed_already=1;
00129     }
00130     setFcn = (SetFcnType) PR_FindSymbol(lib2, "My_SetValue");
00131     value = (*getFcn)();
00132     if (value != 888) {
00133         fprintf(stderr, "Test 2 failed: value should be 888, but got %d\n",
00134             value);
00135         if (!debug_mode) failed_already=1;
00136     }
00137     (*setFcn)(777);
00138     value = (*getFcn)();
00139     if (value != 777) {
00140         fprintf(stderr, "Test 2 failed: set value to 777, but got %d\n", value);
00141         if (!debug_mode) failed_already=1;
00142         goto exit_now;
00143     }
00144     if (debug_mode) printf("Test 2 passed\n");
00145 
00146     /*
00147      * Test 3: unload the library.  The library should still be accessible
00148      * via the second handle.  do the same things as above.
00149      */
00150 
00151     status = PR_UnloadLibrary(lib);
00152     if (PR_FAILURE == status) {
00153         fprintf(stderr, "Test 3 failed: cannot unload library: (%d, %d)\n",
00154             PR_GetError(), PR_GetOSError());
00155         if (!debug_mode) failed_already=1;
00156         goto exit_now;
00157     }
00158     getFcn = (GetFcnType) PR_FindFunctionSymbol(lib2, "My_GetValue");
00159     setFcn = (SetFcnType) PR_FindSymbol(lib2, "My_SetValue");
00160     (*setFcn)(666);
00161     value = (*getFcn)();
00162     if (value != 666) {
00163         fprintf(stderr, "Test 3 failed: set value to 666, but got %d\n", value);
00164         if (!debug_mode) failed_already=1;
00165         goto exit_now;
00166     }
00167     if (debug_mode) printf("Test 3 passed\n");
00168 
00169     /*
00170      * Test 4: unload the library, testing the reference count mechanism.
00171      */
00172 
00173     status = PR_UnloadLibrary(lib2);
00174     if (PR_FAILURE == status) {
00175         fprintf(stderr, "Test 4 failed: cannot unload library: (%d, %d)\n",
00176             PR_GetError(), PR_GetOSError());
00177         if (!debug_mode) failed_already=1;
00178         goto exit_now;
00179     }
00180     getFcn = (GetFcnType) PR_FindFunctionSymbolAndLibrary("My_GetValue", &lib2);
00181     if (NULL != getFcn) {
00182         fprintf(stderr, "Test 4 failed: how can we find a symbol "
00183             "in an already unloaded library?\n");
00184         if (!debug_mode) failed_already=1;
00185         goto exit_now;
00186     }
00187     if (debug_mode) {
00188         printf("Test 4 passed\n");
00189     }
00190 
00191     /*
00192     ** Test 5: LoadStaticLibrary()
00193     */
00194     {
00195         PRStaticLinkTable   slt[10];
00196         PRLibrary           *lib;
00197         
00198         lib = PR_LoadStaticLibrary( "my.dll", slt );
00199         if ( lib == NULL )
00200         {
00201             fprintf(stderr, "Test 5: LoadStatiLibrary() failed\n" );
00202             goto exit_now;
00203         }
00204         if (debug_mode)
00205         {
00206             printf("Test 5 passed\n");
00207         }
00208     }
00209 
00210     goto exit_now;
00211 exit_now: 
00212     PR_Cleanup();
00213 
00214     if (failed_already) {
00215         printf("FAILED\n");
00216         return 1;
00217     } else {
00218         printf("PASSED\n");
00219         return 0;
00220     }
00221 }