Skip to content

Commit

Permalink
PluginManager, Utility: add tests for global state shared across libr…
Browse files Browse the repository at this point in the history
…aries.

In particular, it becomes duplicated when Corrade is built statically and
linked to more than one shared library (or separately to a shared library
and an executable). Fixes following.
  • Loading branch information
mosra committed Sep 1, 2019
1 parent a1b4246 commit 21ec227
Show file tree
Hide file tree
Showing 9 changed files with 351 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/Corrade/PluginManager/Test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,29 @@ set_target_properties(
PluginManagerManagerInitFiniTest
PROPERTIES FOLDER "Corrade/PluginManager/Test")

if(CORRADE_BUILD_STATIC AND NOT CORRADE_TARGET_EMSCRIPTEN AND NOT CORRADE_TARGET_IOS AND NOT CORRADE_TARGET_ANDROID AND NOT CORRADE_TARGET_WINDOWS_RT)
# Yes, adding AbstractAnimal.cpp to both targets is fine in this case.
# The whole Corrade gets duplicated too. Using CorradePluginManagerTestLib
# instead of CorradePluginManager as that's what Canary links to.
add_library(PluginManagerGlobalStateAcrossLibrariesLibrary SHARED
AbstractAnimal.cpp
GlobalStateAcrossLibrariesLibrary.cpp)
target_link_libraries(PluginManagerGlobalStateAcrossLibrariesLibrary
PUBLIC CorradePluginManagerTestLib
PRIVATE Canary)
target_include_directories(PluginManagerGlobalStateAcrossLibrariesLibrary PRIVATE $<TARGET_FILE_DIR:PluginManagerManagerTest>)

corrade_add_test(PluginManagerGlobalStateAcrossLi___Test
AbstractAnimal.cpp
GlobalStateAcrossLibrariesTest.cpp
LIBRARIES PluginManagerGlobalStateAcrossLibrariesLibrary)
target_include_directories(PluginManagerGlobalStateAcrossLi___Test PRIVATE $<TARGET_FILE_DIR:PluginManagerManagerTest>)
set_target_properties(
PluginManagerGlobalStateAcrossLibrariesLibrary
PluginManagerGlobalStateAcrossLi___Test
PROPERTIES FOLDER "Corrade/PluginManager/Test")
endif()

add_subdirectory(animals)
add_subdirectory(init-fini)
if(NOT CORRADE_PLUGINMANAGER_NO_DYNAMIC_PLUGIN_SUPPORT)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
This file is part of Corrade.
Copyright © 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016,
2017, 2018, 2019 Vladimír Vondruš <mosra@centrum.cz>
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/

#include "GlobalStateAcrossLibrariesLibrary.h"

#include <vector>
#include <string>

#include "Corrade/PluginManager/Manager.h"

#include "AbstractAnimal.h"

static void initialize() {
CORRADE_PLUGIN_IMPORT(Canary)
}

namespace Corrade { namespace PluginManager { namespace Test {

std::vector<std::string> staticPluginsLoadedInALibrary() {
initialize();

/* Avoid accidentally loading the dynamic plugins as well */
Manager<AbstractAnimal> manager{"nonexistent"};
return manager.pluginList();
}

}}}
45 changes: 45 additions & 0 deletions src/Corrade/PluginManager/Test/GlobalStateAcrossLibrariesLibrary.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#ifndef Corrade_PluginManager_Test_GlobalStateAcrossLibrariesLibrary_h
#define Corrade_PluginManager_Test_GlobalStateAcrossLibrariesLibrary_h
/*
This file is part of Magnum.
Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019
Vladimír Vondruš <mosra@centrum.cz>
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/

#include "Corrade/PluginManager/Manager.h"
#include "Corrade/Utility/StlForwardString.h"
#include "Corrade/Utility/StlForwardVector.h"
#include "Corrade/Utility/VisibilityMacros.h"

#ifdef PluginManagerGlobalStateAcrossLibrariesLibrary_EXPORTS
#define CORRADE_GLOBALSTATEACROSSLIBRARIESLIBRARY_EXPORT CORRADE_VISIBILITY_EXPORT
#else
#define CORRADE_GLOBALSTATEACROSSLIBRARIESLIBRARY_EXPORT CORRADE_VISIBILITY_IMPORT
#endif

namespace Corrade { namespace PluginManager { namespace Test {

CORRADE_GLOBALSTATEACROSSLIBRARIESLIBRARY_EXPORT std::vector<std::string> staticPluginsLoadedInALibrary();

}}}

#endif
59 changes: 59 additions & 0 deletions src/Corrade/PluginManager/Test/GlobalStateAcrossLibrariesTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
This file is part of Corrade.
Copyright © 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016,
2017, 2018, 2019 Vladimír Vondruš <mosra@centrum.cz>
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/

#include <vector>
#include <string>

#include "Corrade/PluginManager/Manager.h"
#include "Corrade/TestSuite/Tester.h"
#include "Corrade/Utility/DebugStl.h"

#include "AbstractAnimal.h"
#include "GlobalStateAcrossLibrariesLibrary.h"

namespace Corrade { namespace PluginManager { namespace Test { namespace {

struct GlobalStateAcrossLibrariesTest: TestSuite::Tester {
explicit GlobalStateAcrossLibrariesTest();

void test();
};

GlobalStateAcrossLibrariesTest::GlobalStateAcrossLibrariesTest() {
addTests({&GlobalStateAcrossLibrariesTest::test});
}

void GlobalStateAcrossLibrariesTest::test() {
/* Canary is linked to the library, but the executable should see it too */
CORRADE_COMPARE(staticPluginsLoadedInALibrary(), std::vector<std::string>{"Canary"});

/* Avoid accidentally loading the dynamic plugins as well */
PluginManager::Manager<AbstractAnimal> manager{"nonexistent"};
CORRADE_COMPARE(manager.pluginList(), std::vector<std::string>{"Canary"});
}

}}}}

CORRADE_TEST_MAIN(Corrade::PluginManager::Test::GlobalStateAcrossLibrariesTest)
4 changes: 4 additions & 0 deletions src/Corrade/PluginManager/Test/animals/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ endif()

corrade_add_static_plugin(Canary ${CMAKE_CURRENT_BINARY_DIR} Canary.conf Canary.cpp)
set_target_properties(Canary Canary-dependencies PROPERTIES FOLDER "Corrade/PluginManager/Test")
if(BUILD_STATIC)
# The plugin gets linked to a shared test lib in a static build
set_target_properties(Canary PROPERTIES POSITION_INDEPENDENT_CODE ON)
endif()

target_link_libraries(Canary PRIVATE CorradePluginManagerTestLib)

Expand Down
15 changes: 15 additions & 0 deletions src/Corrade/Utility/Test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,21 @@ set_target_properties(
ResourceTestNothingData-dependencies
PROPERTIES FOLDER "Corrade/Utility/Test")

if(CORRADE_BUILD_STATIC AND NOT CORRADE_TARGET_EMSCRIPTEN AND NOT CORRADE_TARGET_IOS AND NOT CORRADE_TARGET_ANDROID AND NOT CORRADE_TARGET_WINDOWS_RT)
add_library(UtilityGlobalStateAcrossLibrariesLibrary SHARED
GlobalStateAcrossLibrariesLibrary.cpp
${ResourceTestData})
target_link_libraries(UtilityGlobalStateAcrossLibrariesLibrary PRIVATE CorradeUtility)

corrade_add_test(UtilityGlobalStateAcrossLibrariesTest
GlobalStateAcrossLibrariesTest.cpp
LIBRARIES UtilityGlobalStateAcrossLibrariesLibrary)
set_target_properties(
UtilityGlobalStateAcrossLibrariesLibrary
UtilityGlobalStateAcrossLibrariesTest
PROPERTIES FOLDER "Corrade/Utility/Test")
endif()

# Unix-specific / non-RT-Windows-specific functionality. Also Emscripten.
if(CORRADE_TARGET_UNIX OR (CORRADE_TARGET_WINDOWS AND NOT CORRADE_TARGET_WINDOWS_RT) OR CORRADE_TARGET_EMSCRIPTEN)
corrade_add_test(UtilityFileWatcherTest FileWatcherTest.cpp)
Expand Down
39 changes: 39 additions & 0 deletions src/Corrade/Utility/Test/GlobalStateAcrossLibrariesLibrary.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
This file is part of Corrade.
Copyright © 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016,
2017, 2018, 2019 Vladimír Vondruš <mosra@centrum.cz>
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/

#include <string>

#include "GlobalStateAcrossLibrariesLibrary.h"

#include "Corrade/Utility/Debug.h"
#include "Corrade/Utility/Resource.h"

namespace Corrade { namespace Utility { namespace Test {

std::ostream* debugOutputFromALibrary() { return Debug::output(); }

bool libraryHasATestResourceGroup(){ return Resource::hasGroup("test"); }

}}}
46 changes: 46 additions & 0 deletions src/Corrade/Utility/Test/GlobalStateAcrossLibrariesLibrary.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#ifndef Corrade_Utility_Test_GlobalStateAcrossLibrariesLibrary_h
#define Corrade_Utility_Test_GlobalStateAcrossLibrariesLibrary_h
/*
This file is part of Magnum.
Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019
Vladimír Vondruš <mosra@centrum.cz>
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/

#include <iosfwd>

#include <Corrade/Utility/VisibilityMacros.h>

#ifdef UtilityGlobalStateAcrossLibrariesLibrary_EXPORTS
#define CORRADE_GLOBALSTATEACROSSLIBRARIESLIBRARY_EXPORT CORRADE_VISIBILITY_EXPORT
#else
#define CORRADE_GLOBALSTATEACROSSLIBRARIESLIBRARY_EXPORT CORRADE_VISIBILITY_IMPORT
#endif

namespace Corrade { namespace Utility { namespace Test {

CORRADE_GLOBALSTATEACROSSLIBRARIESLIBRARY_EXPORT std::ostream* debugOutputFromALibrary();

CORRADE_GLOBALSTATEACROSSLIBRARIESLIBRARY_EXPORT bool libraryHasATestResourceGroup();

}}}

#endif
71 changes: 71 additions & 0 deletions src/Corrade/Utility/Test/GlobalStateAcrossLibrariesTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
This file is part of Corrade.
Copyright © 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016,
2017, 2018, 2019 Vladimír Vondruš <mosra@centrum.cz>
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/

#include <sstream>

#include "Corrade/TestSuite/Tester.h"
#include "Corrade/Utility/Resource.h"

#include "GlobalStateAcrossLibrariesLibrary.h"

namespace Corrade { namespace Utility { namespace Test { namespace {

struct GlobalStateAcrossLibrariesTest: TestSuite::Tester {
explicit GlobalStateAcrossLibrariesTest();

void debug();
void resource();
};

GlobalStateAcrossLibrariesTest::GlobalStateAcrossLibrariesTest() {
addTests({&GlobalStateAcrossLibrariesTest::debug,
&GlobalStateAcrossLibrariesTest::resource});
}

void GlobalStateAcrossLibrariesTest::debug() {
std::ostringstream out;

std::ostream* current = Debug::output();
CORRADE_COMPARE(debugOutputFromALibrary(), current);

{
Debug redirectOutput{&out};
CORRADE_COMPARE(debugOutputFromALibrary(), &out);
}

CORRADE_COMPARE(debugOutputFromALibrary(), current);
}

void GlobalStateAcrossLibrariesTest::resource() {
/* The resource is complied into the the library, but the executable should
see it too */
CORRADE_VERIFY(libraryHasATestResourceGroup());

CORRADE_VERIFY(Utility::Resource::hasGroup("test"));
}

}}}}

CORRADE_TEST_MAIN(Corrade::Utility::Test::GlobalStateAcrossLibrariesTest)

0 comments on commit 21ec227

Please sign in to comment.