diff --git a/pluginlib/test/utest.cpp b/pluginlib/test/utest.cpp index 772a858..b011f51 100644 --- a/pluginlib/test/utest.cpp +++ b/pluginlib/test/utest.cpp @@ -28,9 +28,16 @@ #include +#include +#include +#include #include +#include +#include #include // NOLINT +#include "rcpputils/shared_library.hpp" // NOLINT +#include "rcutils/env.h" // NOLINT #include "rcutils/logging_macros.h" // NOLINT #include @@ -191,6 +198,184 @@ TEST(PluginlibTest, Splitting) { EXPECT_EQ(pl.getName(""), ""); } +TEST(PluginlibTest, classInformation) { + pluginlib::ClassLoader pl("test_pluginlib", "test_base::Fubar"); + + EXPECT_EQ("test_base::Fubar", pl.getBaseClassType()); + + // Only the classes deriving from test_base::Fubar are declared. + std::vector declared = pl.getDeclaredClasses(); + std::sort(declared.begin(), declared.end()); + EXPECT_EQ( + (std::vector{ + "test_pluginlib/bar", "test_pluginlib/foo", "test_pluginlib/none"}), + declared); + + EXPECT_TRUE(pl.isClassAvailable("test_pluginlib/foo")); + EXPECT_FALSE(pl.isClassAvailable("test_pluginlib/foo_with_ctor")); + EXPECT_EQ("test_plugins::Foo", pl.getClassType("test_pluginlib/foo")); + EXPECT_EQ("This is a foo plugin.", pl.getClassDescription("test_pluginlib/foo")); + EXPECT_EQ("test_pluginlib", pl.getClassPackage("test_pluginlib/foo")); + + const std::vector xml_paths = pl.getPluginXmlPaths(); + ASSERT_EQ(1u, xml_paths.size()); + EXPECT_EQ("test_plugins.xml", std::filesystem::path(xml_paths[0]).filename()); + EXPECT_EQ(xml_paths[0], pl.getPluginManifestPath("test_pluginlib/foo")); + + const std::string library_path = pl.getClassLibraryPath("test_pluginlib/foo"); + EXPECT_TRUE(std::filesystem::exists(library_path)) << library_path; +} + +TEST(PluginlibTest, unknownClassInformation) { + pluginlib::ClassLoader pl("test_pluginlib", "test_base::Fubar"); + const std::string unknown = "test_pluginlib/unknown"; + + EXPECT_FALSE(pl.isClassAvailable(unknown)); + EXPECT_FALSE(pl.isClassLoaded(unknown)); + EXPECT_EQ("", pl.getClassType(unknown)); + EXPECT_EQ("", pl.getClassDescription(unknown)); + EXPECT_EQ("", pl.getClassPackage(unknown)); + EXPECT_EQ("", pl.getPluginManifestPath(unknown)); + EXPECT_THROW(pl.getClassLibraryPath(unknown), pluginlib::LibraryLoadException); + EXPECT_THROW(pl.loadLibraryForClass(unknown), pluginlib::LibraryLoadException); + EXPECT_THROW(pl.unloadLibraryForClass(unknown), pluginlib::LibraryUnloadException); +} + +TEST(PluginlibTest, unloadClassNotLoaded) { + pluginlib::ClassLoader pl("test_pluginlib", "test_base::Fubar"); + EXPECT_THROW( + pl.unloadLibraryForClass("test_pluginlib/foo"), pluginlib::LibraryUnloadException); +} + +TEST(PluginlibTest, registeredLibraries) { + pluginlib::ClassLoader pl("test_pluginlib", "test_base::Fubar"); + EXPECT_TRUE(pl.getRegisteredLibraries().empty()); + + pl.loadLibraryForClass("test_pluginlib/foo"); + EXPECT_TRUE(pl.isClassLoaded("test_pluginlib/foo")); + EXPECT_EQ( + std::vector{pl.getClassLibraryPath("test_pluginlib/foo")}, + pl.getRegisteredLibraries()); + + pl.unloadLibraryForClass("test_pluginlib/foo"); + EXPECT_TRUE(pl.getRegisteredLibraries().empty()); +} + +TEST(PluginlibTest, classMissingFromLibrary) { + // The library of 'test_pluginlib/none' loads, but it does not export the class. + pluginlib::ClassLoader pl("test_pluginlib", "test_base::Fubar"); + EXPECT_THROW( + pl.createUnmanagedInstance("test_pluginlib/none"), pluginlib::CreateClassException); +} + +TEST(PluginlibTest, refreshDeclaredClasses) { + pluginlib::ClassLoader pl("test_pluginlib", "test_base::Fubar"); + std::vector declared = pl.getDeclaredClasses(); + + // Refreshing without any loaded library keeps the same classes. + pl.refreshDeclaredClasses(); + EXPECT_EQ(declared, pl.getDeclaredClasses()); + + // Refreshing with a loaded library keeps the classes usable. + std::shared_ptr foo = pl.createSharedInstance("test_pluginlib/foo"); + pl.refreshDeclaredClasses(); + EXPECT_EQ(declared, pl.getDeclaredClasses()); + ASSERT_TRUE(pl.isClassLoaded("test_pluginlib/foo")); + foo = pl.createSharedInstance("test_pluginlib/foo"); + foo->initialize(10.0); + EXPECT_EQ(100.0, foo->result()); +} + +// Writes 'content' to 'path', creating the parent directories. +static void writeFile(const std::filesystem::path & path, const std::string & content) +{ + std::filesystem::create_directories(path.parent_path()); + std::ofstream(path) << content; +} + +// Writes a plugin description XML, next to a package.xml for 'package_name', +// in a fresh temporary directory. Returns the path to the plugin XML. +static std::string writePluginXml( + const std::string & test_name, const std::string & package_name, const std::string & content) +{ + const std::filesystem::path dir = + std::filesystem::temp_directory_path() / ("pluginlib_utest_" + test_name); + std::filesystem::remove_all(dir); + writeFile(dir / "package.xml", + "" + package_name + ""); + writeFile(dir / "plugins.xml", content); + return (dir / "plugins.xml").string(); +} + +TEST(PluginlibTest, libraryNameWithLibPrefix) { + // 'libtest_plugins' is found as 'test_plugins' on every platform. + const std::string xml_path = writePluginXml( + "libraryNameWithLibPrefix", "test_pluginlib", + "" + "" + ""); + pluginlib::ClassLoader pl( + "test_pluginlib", "test_base::Fubar", "plugin", {xml_path}); + + std::shared_ptr foo = pl.createSharedInstance("test_pluginlib/foo"); + foo->initialize(10.0); + EXPECT_EQ(100.0, foo->result()); +} + +TEST(PluginlibTest, libraryNotFound) { + const std::string xml_path = writePluginXml( + "libraryNotFound", "test_pluginlib", + "" + "" + ""); + pluginlib::ClassLoader pl( + "test_pluginlib", "test_base::Fubar", "plugin", {xml_path}); + + EXPECT_THROW(pl.getClassLibraryPath("test_pluginlib/foo"), pluginlib::LibraryLoadException); + EXPECT_THROW(pl.createSharedInstance("test_pluginlib/foo"), pluginlib::LibraryLoadException); +} + +TEST(PluginlibTest, libraryFailsToLoad) { + // Install a package 'broken_pkg' whose plugin library is not a valid shared library. + const std::filesystem::path prefix = + std::filesystem::temp_directory_path() / "pluginlib_utest_libraryFailsToLoad_prefix"; + std::filesystem::remove_all(prefix); + writeFile(prefix / "share" / "ament_index" / "resource_index" / "packages" / "broken_pkg", ""); + const std::string library_name = rcpputils::get_platform_library_name("broken_plugins"); + writeFile(prefix / "lib" / library_name, "not a shared library"); + writeFile(prefix / "bin" / library_name, "not a shared library"); + + const char * ament_prefix_path = nullptr; + ASSERT_EQ(nullptr, rcutils_get_env("AMENT_PREFIX_PATH", &ament_prefix_path)); + const std::string original_ament_prefix_path = ament_prefix_path; + const char separator = std::filesystem::path::preferred_separator == '\\' ? ';' : ':'; + const std::string new_ament_prefix_path = + prefix.string() + separator + original_ament_prefix_path; + ASSERT_TRUE(rcutils_set_env("AMENT_PREFIX_PATH", new_ament_prefix_path.c_str())); + + const std::string xml_path = writePluginXml( + "libraryFailsToLoad", "broken_pkg", + "" + "" + ""); + { + pluginlib::ClassLoader pl( + "test_pluginlib", "test_base::Fubar", "plugin", {xml_path}); + try { + pl.createSharedInstance("broken_pkg/foo"); + ADD_FAILURE() << "Expected pluginlib::LibraryLoadException"; + } catch (const pluginlib::LibraryLoadException & ex) { + EXPECT_NE(std::string::npos, std::string(ex.what()).find("Failed to load library")) + << ex.what(); + } + } + + EXPECT_TRUE(rcutils_set_env("AMENT_PREFIX_PATH", original_ament_prefix_path.c_str())); +} + // Run all the tests that were declared with TEST() int main(int argc, char ** argv) {