1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
|
///////////////////////////////////////////////////////////////////////////////
// Name: tests/misc/dynamiclib.cpp
// Purpose: Test wxDynamicLibrary
// Author: Francesco Montorsi (extracted from console sample)
// Created: 2010-06-13
// Copyright: (c) 2010 wxWidgets team
///////////////////////////////////////////////////////////////////////////////
// ----------------------------------------------------------------------------
// headers
// ----------------------------------------------------------------------------
#include "testprec.h"
#include "wx/dynlib.h"
#ifndef __WINDOWS__
#include "wx/dir.h"
#include "wx/filename.h"
#endif
// ----------------------------------------------------------------------------
// test class
// ----------------------------------------------------------------------------
TEST_CASE("DynamicLibrary::Load", "[dynlib]")
{
#if defined(__WINDOWS__)
static const char* const LIB_NAME = "kernel32.dll";
static const char* const FUNC_NAME = "lstrlenA";
#elif defined(__CYGWIN__)
static const char* const LIB_NAME = "cygwin1.dll";
static const char* const FUNC_NAME = "strlen";
#elif defined(__DARWIN__)
// Under macOS 12+ we can actually load the libc dylib even though the
// corresponding file doesn't exist on disk, so we have to handle it
// differently.
static const char* const LIB_NAME = "/usr/lib/libc.dylib";
static const char* const FUNC_NAME = "strlen";
#else // other Unix
static const char* const candidateDirs[] =
{
"/lib/x86_64-linux-gnu",
"/lib",
"/lib64",
"/usr/lib",
};
static const char* const candidateVersions[] = { "6", "7", "6.1", "0.3", "0.1" };
wxString LIB_NAME;
wxArrayString allMatches;
for ( size_t n = 0; n < WXSIZEOF(candidateDirs); ++n )
{
const wxString dir(candidateDirs[n]);
if ( !wxDir::Exists(dir) )
continue;
for ( size_t m = 0; m < WXSIZEOF(candidateVersions); ++m )
{
const wxString candidate = wxString::Format
(
"%s/libc.so.%s",
dir, candidateVersions[m]
);
if ( wxFileName::Exists(candidate) )
{
LIB_NAME = candidate;
break;
}
}
if ( !LIB_NAME.empty() )
break;
wxDir::GetAllFiles(dir, &allMatches, "libc.*", wxDIR_FILES);
}
if ( LIB_NAME.empty() )
{
WARN("Couldn't find libc.so, skipping DynamicLibrary::Load() test.");
if ( !allMatches.empty() )
{
WARN("Possible candidates:\n" << wxJoin(allMatches, '\n'));
}
return;
}
static const char* const FUNC_NAME = "strlen";
#endif // OS
wxDynamicLibrary lib(LIB_NAME);
REQUIRE( lib.IsLoaded() );
SECTION("strlen")
{
typedef int (wxSTDCALL *wxStrlenType)(const char *);
wxStrlenType pfnStrlen = (wxStrlenType)lib.GetSymbol(FUNC_NAME);
if ( pfnStrlen )
{
// Call the function dynamically loaded
CHECK( pfnStrlen("foo") == 3 );
}
else
{
FAIL(FUNC_NAME << " wasn't found in " << LIB_NAME);
}
}
#ifdef __WINDOWS__
SECTION("A/W")
{
static const char* const FUNC_NAME_AW = "lstrlen";
typedef int (wxSTDCALL *wxStrlenTypeAorW)(const wxChar *);
wxStrlenTypeAorW
pfnStrlenAorW = (wxStrlenTypeAorW)lib.GetSymbolAorW(FUNC_NAME_AW);
if ( pfnStrlenAorW )
{
CHECK( pfnStrlenAorW(wxT("foobar")) == 6 );
}
else
{
FAIL(FUNC_NAME_AW << " wasn't found in " << LIB_NAME);
}
}
#endif // __WINDOWS__
}
|