| 1 | // |
|---|---|
| 2 | // SharedLibraryTest.cpp |
| 3 | // |
| 4 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
| 5 | // and Contributors. |
| 6 | // |
| 7 | // SPDX-License-Identifier: BSL-1.0 |
| 8 | // |
| 9 | |
| 10 | |
| 11 | #include "SharedLibraryTest.h" |
| 12 | #include "Poco/CppUnit/TestCaller.h" |
| 13 | #include "Poco/CppUnit/TestSuite.h" |
| 14 | #include "Poco/SharedLibrary.h" |
| 15 | #include "Poco/Path.h" |
| 16 | #include "Poco/Exception.h" |
| 17 | |
| 18 | |
| 19 | using Poco::SharedLibrary; |
| 20 | using Poco::NotFoundException; |
| 21 | using Poco::LibraryLoadException; |
| 22 | using Poco::LibraryAlreadyLoadedException; |
| 23 | using Poco::Path; |
| 24 | |
| 25 | |
| 26 | typedef int (*GimmeFiveFunc)(); |
| 27 | |
| 28 | |
| 29 | SharedLibraryTest::SharedLibraryTest(const std::string& rName): CppUnit::TestCase(rName) |
| 30 | { |
| 31 | } |
| 32 | |
| 33 | |
| 34 | SharedLibraryTest::~SharedLibraryTest() |
| 35 | { |
| 36 | } |
| 37 | |
| 38 | |
| 39 | void SharedLibraryTest::testSharedLibrary1() |
| 40 | { |
| 41 | std::string self = Path(Path::self()).makeParent().toString(); |
| 42 | std::string path = self + "TestLibrary"; |
| 43 | path.append(SharedLibrary::suffix()); |
| 44 | SharedLibrary sl; |
| 45 | assertTrue (!sl.isLoaded()); |
| 46 | sl.load(path); |
| 47 | assertTrue (sl.getPath() == path); |
| 48 | assertTrue (sl.isLoaded()); |
| 49 | assertTrue (sl.hasSymbol("pocoBuildManifest")); |
| 50 | assertTrue (sl.hasSymbol("pocoInitializeLibrary")); |
| 51 | assertTrue (sl.hasSymbol("pocoUninitializeLibrary")); |
| 52 | assertTrue (sl.hasSymbol("gimmeFive")); |
| 53 | assertTrue (!sl.hasSymbol("fooBar123")); |
| 54 | |
| 55 | void* p1 = sl.getSymbol("pocoBuildManifest"); |
| 56 | assertNotNullPtr(p1); |
| 57 | try |
| 58 | { |
| 59 | p1 = sl.getSymbol("fooBar123"); |
| 60 | failmsg("no such symbol - must throw exception"); |
| 61 | } |
| 62 | catch (NotFoundException&) |
| 63 | { |
| 64 | } |
| 65 | catch (...) |
| 66 | { |
| 67 | failmsg("wrong exception"); |
| 68 | } |
| 69 | sl.unload(); |
| 70 | assertTrue (!sl.isLoaded()); |
| 71 | } |
| 72 | |
| 73 | |
| 74 | void SharedLibraryTest::testSharedLibrary2() |
| 75 | { |
| 76 | std::string self = Path(Path::self()).makeParent().toString(); |
| 77 | std::string path = self + "TestLibrary"; |
| 78 | path.append(SharedLibrary::suffix()); |
| 79 | SharedLibrary sl(path); |
| 80 | assertTrue (sl.getPath() == path); |
| 81 | assertTrue (sl.isLoaded()); |
| 82 | |
| 83 | GimmeFiveFunc gimmeFive = (GimmeFiveFunc) sl.getSymbol("gimmeFive"); |
| 84 | assertTrue (gimmeFive() == 5); |
| 85 | |
| 86 | sl.unload(); |
| 87 | assertTrue (!sl.isLoaded()); |
| 88 | } |
| 89 | |
| 90 | |
| 91 | void SharedLibraryTest::testSharedLibrary3() |
| 92 | { |
| 93 | std::string path = "NonexistentLibrary"; |
| 94 | SharedLibrary sl; |
| 95 | try |
| 96 | { |
| 97 | sl.load(path); |
| 98 | failmsg("no such library - must throw exception"); |
| 99 | } |
| 100 | catch (LibraryLoadException&) |
| 101 | { |
| 102 | } |
| 103 | catch (...) |
| 104 | { |
| 105 | failmsg("wrong exception"); |
| 106 | } |
| 107 | assertTrue (!sl.isLoaded()); |
| 108 | |
| 109 | std::string self = Path(Path::self()).makeParent().toString(); |
| 110 | path = self + "TestLibrary"; |
| 111 | path.append(SharedLibrary::suffix()); |
| 112 | sl.load(path); |
| 113 | assertTrue (sl.isLoaded()); |
| 114 | |
| 115 | try |
| 116 | { |
| 117 | sl.load(path); |
| 118 | failmsg("library already loaded - must throw exception"); |
| 119 | } |
| 120 | catch (LibraryAlreadyLoadedException&) |
| 121 | { |
| 122 | } |
| 123 | catch (...) |
| 124 | { |
| 125 | failmsg("wrong exception"); |
| 126 | } |
| 127 | assertTrue (sl.isLoaded()); |
| 128 | |
| 129 | sl.unload(); |
| 130 | assertTrue (!sl.isLoaded()); |
| 131 | } |
| 132 | |
| 133 | |
| 134 | void SharedLibraryTest::setUp() |
| 135 | { |
| 136 | } |
| 137 | |
| 138 | |
| 139 | void SharedLibraryTest::tearDown() |
| 140 | { |
| 141 | } |
| 142 | |
| 143 | |
| 144 | CppUnit::Test* SharedLibraryTest::suite() |
| 145 | { |
| 146 | CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("SharedLibraryTest"); |
| 147 | |
| 148 | #ifndef _DEBUG // FIXME excluded from the Debug build temporarily for AppVeyor stability |
| 149 | CppUnit_addTest(pSuite, SharedLibraryTest, testSharedLibrary1); |
| 150 | CppUnit_addTest(pSuite, SharedLibraryTest, testSharedLibrary2); |
| 151 | CppUnit_addTest(pSuite, SharedLibraryTest, testSharedLibrary3); |
| 152 | #endif |
| 153 | |
| 154 | return pSuite; |
| 155 | } |
| 156 |