Skip to content

Commit

Permalink
Add dummy shared lib to test mef::ExternLibrary
Browse files Browse the repository at this point in the history
Basic smoke tests are provided to check
if the dynamic library loading works on the given platform.

Issue #74
  • Loading branch information
rakhimov committed Aug 21, 2017
1 parent b944bd0 commit fba114d
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/expression/extern.cc
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class ExternLibrary::Pimpl {
bool decorate) : lib_handle_(nullptr) {
if (decorate) {
lib_path += ".so";
auto pos = lib_path.find('/');
auto pos = lib_path.rfind('/');
lib_path.insert(pos == std::string::npos ? 0 : (pos + 1), "lib");
}
if (!system || lib_path.find('/') != std::string::npos) {
Expand Down
6 changes: 5 additions & 1 deletion src/expression/extern.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,16 @@ class ExternLibrary : public Element, private boost::noncopyable {
/// @returns The function pointer resolved from the symbol.
///
/// @throws UndefinedElement The symbol is not in the library.
///
/// @note The functionality should fail at compile time (UB in C)
/// if the platform doesn't support
/// object pointer to function pointer casts.
template <typename F>
std::enable_if_t<std::is_pointer<F>::value &&
std::is_function<std::remove_pointer_t<F>>::value,
F>
get(const std::string& symbol) const {
return static_cast<F>(get(symbol.c_str()));
return reinterpret_cast<F>(get(symbol.c_str()));
}

private:
Expand Down
12 changes: 12 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ set(SCRAM_CORE_TEST_SOURCE
"${CMAKE_CURRENT_SOURCE_DIR}/element_tests.cc"
"${CMAKE_CURRENT_SOURCE_DIR}/event_tests.cc"
"${CMAKE_CURRENT_SOURCE_DIR}/expression_tests.cc"
"${CMAKE_CURRENT_SOURCE_DIR}/extern_function_tests.cc"
"${CMAKE_CURRENT_SOURCE_DIR}/ccf_group_tests.cc"
"${CMAKE_CURRENT_SOURCE_DIR}/fault_tree_tests.cc"
"${CMAKE_CURRENT_SOURCE_DIR}/alignment_tests.cc"
Expand Down Expand Up @@ -78,3 +79,14 @@ install(DIRECTORY
DESTINATION share/scram
COMPONENT testing
)

# Create dummy library to test dynamic loading and extern function.
add_library(scram_dummy_extern SHARED "${CMAKE_CURRENT_SOURCE_DIR}/scram_dummy_extern.cc")
if(WIN32)
set(CMAKE_SHARED_LIBRARY_PREFIX "")
endif()
install(
TARGETS scram_dummy_extern
DESTINATION lib/scram/test
COMPONENT testing
)
79 changes: 79 additions & 0 deletions tests/extern_function_tests.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Copyright (C) 2017 Olzhas Rakhimov
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "expression/extern.h"

#include <memory>

#include <gtest/gtest.h>

#include <boost/predef.h>

#if BOOST_OS_LINUX
#include <stdlib.h>
#endif

#include "env.h"

namespace scram {
namespace mef {
namespace test {

const char kLibName[] = "scram_dummy_extern";
const char kLibRelPath[] = "../lib/scram/test/scram_dummy_extern";

#if BOOST_OS_LINUX
const char kLibRelPathLinux[] = "../lib/scram/test/libscram_dummy_extern.so";
#endif

TEST(ExternTest, ExternLibraryLoad) {
const std::string bin_dir = Env::install_dir() + "/bin";
EXPECT_THROW(ExternLibrary("dummy", kLibName, "", false, false), IOError);
EXPECT_THROW(ExternLibrary("dummy", kLibName, "", false, true), IOError);
EXPECT_THROW(ExternLibrary("dummy", kLibName, "", true, true), IOError);
EXPECT_THROW(ExternLibrary("dummy", kLibRelPath, bin_dir, false, false),
IOError);
EXPECT_NO_THROW(ExternLibrary("dummy", kLibRelPath, bin_dir, false, true));
EXPECT_NO_THROW(ExternLibrary("dummy", kLibRelPath, bin_dir, true, true));

#if BOOST_OS_LINUX
EXPECT_NO_THROW(
ExternLibrary("dummy", kLibRelPathLinux, bin_dir, false, false));
// The system search with LD_LIBRARY_PATH must be tested outside.
#endif
}

TEST(ExternTest, ExternLibraryGet) {
const std::string bin_dir = Env::install_dir() + "/bin";

std::unique_ptr<ExternLibrary> library;
ASSERT_NO_THROW(library = std::make_unique<ExternLibrary>(
"dummy", kLibRelPath, bin_dir, false, true));

EXPECT_NO_THROW(library->get<int(*)()>("foo"));
EXPECT_NO_THROW(library->get<double(*)()>("bar"));
EXPECT_NO_THROW(library->get<float(*)()>("baz"));
EXPECT_THROW(library->get<int(*)()>("foobar"), UndefinedElement);

EXPECT_EQ(42, library->get<int(*)()>("foo")());
EXPECT_EQ(42, library->get<double(*)()>("bar")());
EXPECT_EQ(42, library->get<float(*)()>("baz")());
}

} // namespace test
} // namespace mef
} // namespace scram
25 changes: 25 additions & 0 deletions tests/scram_dummy_extern.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright (C) 2017 Olzhas Rakhimov
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

/// @file scram_dummy_extern.cc
/// Dummy library with extern functions to test dynamic loading in MEF extern.

extern "C" {
int foo() { return 42; }
double bar() { return 42; }
float baz() { return 42; }
}

0 comments on commit fba114d

Please sign in to comment.