summaryrefslogtreecommitdiffstats
path: root/Source/kwsys/testDynamicLoader.cxx
blob: c7ddc2b5e0224cae8ea744bfc11de6c5a48bf2ad (plain)
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
/*=========================================================================

  Program:   KWSys - Kitware System Library
  Module:    $RCSfile$

  Copyright (c) Kitware, Inc., Insight Consortium.  All rights reserved.
  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notices for more information.

=========================================================================*/
#include "kwsysPrivate.h"

#include KWSYS_HEADER(DynamicLoader.hxx)
#include KWSYS_HEADER(ios/iostream)
#include KWSYS_HEADER(stl/string)

#if defined(__BEOS__)
#include <be/kernel/OS.h>  /* disable_debugger() API. */
#endif

// Work-around CMake dependency scanning limitation.  This must
// duplicate the above list of headers.
#if 0
# include "DynamicLoader.hxx.in"
# include "kwsys_ios_iostream.h.in"
# include "kwsys_stl_string.hxx.in"
#endif

// Include with <> instead of "" to avoid getting any in-source copy
// left on disk.
#include <testSystemTools.h>

kwsys_stl::string GetLibName(const char* lname)
{
  // Construct proper name of lib
  kwsys_stl::string slname;
  slname = EXECUTABLE_OUTPUT_PATH;
#ifdef CMAKE_INTDIR
  slname += "/";
  slname += CMAKE_INTDIR;
#endif
  slname += "/";
  slname += kwsys::DynamicLoader::LibPrefix();
  slname += lname;
  slname += kwsys::DynamicLoader::LibExtension();

  return slname;
}

/* libname = Library name (proper prefix, proper extension)
 * System  = symbol to lookup in libname
 * r1: should OpenLibrary succeed ?
 * r2: should GetSymbolAddress succeed ?
 * r3: should CloseLibrary succeed ?
 */
int TestDynamicLoader(const char* libname, const char* symbol, int r1, int r2, int r3)
{
  kwsys_ios::cerr << "Testing: " << libname << kwsys_ios::endl;
  kwsys::DynamicLoader::LibraryHandle l
    = kwsys::DynamicLoader::OpenLibrary(libname);
  // If result is incompatible with expectation just fails (xor):
  if( (r1 && !l) || (!r1 && l) )
    {
    kwsys_ios::cerr
      << kwsys::DynamicLoader::LastError() << kwsys_ios::endl;
    return 1;
    }
  kwsys::DynamicLoader::SymbolPointer f
    = kwsys::DynamicLoader::GetSymbolAddress(l, symbol);
  if( (r2 && !f) || (!r2 && f) )
    {
    kwsys_ios::cerr
      << kwsys::DynamicLoader::LastError() << kwsys_ios::endl;
    return 1;
    }
#ifndef __APPLE__
  int s = kwsys::DynamicLoader::CloseLibrary(l);
  if( (r3 && !s) || (!r3 && s) )
    {
    kwsys_ios::cerr
      << kwsys::DynamicLoader::LastError() << kwsys_ios::endl;
    return 1;
    }
#else
  (void)r3;
#endif
  return 0;
}

int testDynamicLoader(int argc, char *argv[])
{
#if defined(_WIN32)
  SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX);
#elif defined(__BEOS__)
  disable_debugger(1);
#endif
  int res;
  if( argc == 3 )
    {
    // User specify a libname and symbol to check.
    res = TestDynamicLoader(argv[1], argv[2],1,1,1);
    return res;
    }
  // Make sure that inexistant lib is giving correct result
  res = TestDynamicLoader("azerty_", "foo_bar",0,0,0);
  // Make sure that random binary file cannnot be assimilated as dylib
  res += TestDynamicLoader(TEST_SYSTEMTOOLS_BIN_FILE, "wp",0,0,0);
#ifdef __linux__
  // This one is actually fun to test, since dlopen is by default loaded...wonder why :)
  res += TestDynamicLoader("foobar.lib", "dlopen",0,1,0);
  res += TestDynamicLoader("libdl.so", "dlopen",1,1,1);
  res += TestDynamicLoader("libdl.so", "TestDynamicLoader",1,0,1);
#endif
  // Now try on the generated library
  kwsys_stl::string libname = GetLibName(KWSYS_NAMESPACE_STRING "TestDynload");
  res += TestDynamicLoader(libname.c_str(), "dummy",1,0,1);
  res += TestDynamicLoader(libname.c_str(), "TestDynamicLoaderSymbolPointer",1,1,1);
  res += TestDynamicLoader(libname.c_str(), "_TestDynamicLoaderSymbolPointer",1,0,1);
  res += TestDynamicLoader(libname.c_str(), "TestDynamicLoaderData",1,1,1);
  res += TestDynamicLoader(libname.c_str(), "_TestDynamicLoaderData",1,0,1);

  return res;
}