1 ///////////////////////////////////////////////////////////////////////////////
2 // Name:        tests/misc/dynamiclib.cpp
3 // Purpose:     Test wxDynamicLibrary
4 // Author:      Francesco Montorsi (extracted from console sample)
5 // Created:     2010-06-13
6 // Copyright:   (c) 2010 wxWidgets team
7 ///////////////////////////////////////////////////////////////////////////////
8 
9 // ----------------------------------------------------------------------------
10 // headers
11 // ----------------------------------------------------------------------------
12 
13 #include "testprec.h"
14 
15 #include "wx/dynlib.h"
16 
17 #ifdef __UNIX__
18     #include "wx/filename.h"
19     #include "wx/log.h"
20 #endif
21 
22 // ----------------------------------------------------------------------------
23 // test class
24 // ----------------------------------------------------------------------------
25 
26 class DynamicLibraryTestCase : public CppUnit::TestCase
27 {
28 public:
DynamicLibraryTestCase()29     DynamicLibraryTestCase() { }
30 
31 private:
32     CPPUNIT_TEST_SUITE( DynamicLibraryTestCase );
33         CPPUNIT_TEST( Load );
34     CPPUNIT_TEST_SUITE_END();
35 
36     void Load();
37 
38     wxDECLARE_NO_COPY_CLASS(DynamicLibraryTestCase);
39 };
40 
41 // register in the unnamed registry so that these tests are run by default
42 CPPUNIT_TEST_SUITE_REGISTRATION( DynamicLibraryTestCase );
43 
44 // also include in its own registry so that these tests can be run alone
45 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( DynamicLibraryTestCase, "DynamicLibraryTestCase" );
46 
Load()47 void DynamicLibraryTestCase::Load()
48 {
49 #if defined(__WINDOWS__)
50     static const wxChar *LIB_NAME = wxT("kernel32.dll");
51     static const wxChar *FUNC_NAME = wxT("lstrlenA");
52 #elif defined(__UNIX__)
53 #ifdef __DARWIN__
54     static const wxChar *LIB_NAME = wxT("/usr/lib/libc.dylib");
55 #else
56     // weird: using just libc.so does *not* work!
57     static const wxChar *LIB_NAME = wxT("/lib/libc.so.6");
58 #endif
59     static const wxChar *FUNC_NAME = wxT("strlen");
60 
61     if ( !wxFileName::Exists(LIB_NAME) )
62     {
63         wxLogWarning("Shared library \"%s\" doesn't exist, "
64                      "skipping DynamicLibraryTestCase::Load() test.");
65         return;
66     }
67 #else
68     #error "don't know how to test wxDllLoader on this platform"
69 #endif
70 
71     wxDynamicLibrary lib(LIB_NAME);
72     CPPUNIT_ASSERT( lib.IsLoaded() );
73 
74     typedef int (wxSTDCALL *wxStrlenType)(const char *);
75     wxStrlenType pfnStrlen = (wxStrlenType)lib.GetSymbol(FUNC_NAME);
76 
77     wxString errMsg = wxString::Format("ERROR: function '%s' wasn't found in '%s'.\n",
78                                        FUNC_NAME, LIB_NAME);
79     CPPUNIT_ASSERT_MESSAGE( errMsg.ToStdString(), (pfnStrlen != NULL) );
80 
81     // Call the function dynamically loaded
82     CPPUNIT_ASSERT( pfnStrlen("foo") == 3 );
83 
84 #ifdef __WINDOWS__
85     static const wxChar *FUNC_NAME_AW = wxT("lstrlen");
86 
87     typedef int (wxSTDCALL *wxStrlenTypeAorW)(const wxChar *);
88     wxStrlenTypeAorW
89         pfnStrlenAorW = (wxStrlenTypeAorW)lib.GetSymbolAorW(FUNC_NAME_AW);
90 
91     wxString errMsg2 = wxString::Format("ERROR: function '%s' wasn't found in '%s'.\n",
92                                        FUNC_NAME_AW, LIB_NAME);
93     CPPUNIT_ASSERT_MESSAGE( errMsg2.ToStdString(), (pfnStrlenAorW != NULL) );
94 
95     CPPUNIT_ASSERT( pfnStrlenAorW(wxT("foobar")) == 6 );
96 #endif // __WINDOWS__
97 }
98