1 #include "blob_mock.hpp" 2 #include "dlsys_mock.hpp" 3 #include "fs.hpp" 4 #include "manager_mock.hpp" 5 #include "utils.hpp" 6 7 #include <filesystem> 8 #include <memory> 9 #include <string> 10 #include <vector> 11 12 #include <gtest/gtest.h> 13 14 namespace fs = std::filesystem; 15 16 namespace blobs 17 { 18 using ::testing::_; 19 using ::testing::Return; 20 using ::testing::StrEq; 21 using ::testing::StrictMock; 22 23 std::vector<std::string>* returnList = nullptr; 24 25 std::vector<std::string> getLibraryList(const std::string& path, 26 PathMatcher check) 27 { 28 return (returnList) ? *returnList : std::vector<std::string>(); 29 } 30 31 std::unique_ptr<GenericBlobInterface> factoryReturn; 32 33 std::unique_ptr<GenericBlobInterface> fakeFactory() 34 { 35 return std::move(factoryReturn); 36 } 37 38 TEST(UtilLoadLibraryTest, NoFilesFound) 39 { 40 /* Verify nothing special happens when there are no files found. */ 41 42 StrictMock<internal::InternalDlSysMock> dlsys; 43 StrictMock<ManagerMock> manager; 44 45 loadLibraries(&manager, "", &dlsys); 46 } 47 48 TEST(UtilLoadLibraryTest, OneFileFoundIsLibrary) 49 { 50 /* Verify if it finds a library, and everything works, it'll regsiter it. 51 */ 52 53 std::vector<std::string> files = {"this.fake"}; 54 returnList = &files; 55 56 StrictMock<internal::InternalDlSysMock> dlsys; 57 StrictMock<ManagerMock> manager; 58 void* handle = reinterpret_cast<void*>(0x01); 59 auto blobMock = std::make_unique<BlobMock>(); 60 61 factoryReturn = std::move(blobMock); 62 63 EXPECT_CALL(dlsys, dlopen(_, _)).WillOnce(Return(handle)); 64 65 EXPECT_CALL(dlsys, dlerror()).Times(2).WillRepeatedly(Return(nullptr)); 66 67 EXPECT_CALL(dlsys, dlsym(handle, StrEq("createHandler"))) 68 .WillOnce(Return(reinterpret_cast<void*>(fakeFactory))); 69 70 EXPECT_CALL(manager, registerHandler(_)); 71 72 loadLibraries(&manager, "", &dlsys); 73 } 74 75 TEST(UtilLibraryMatchTest, TestAll) 76 { 77 struct LibraryMatch 78 { 79 std::string name; 80 bool expectation; 81 }; 82 83 std::vector<LibraryMatch> tests = { 84 {"libblobcmds.0.0.1", false}, {"libblobcmds.0.0", false}, 85 {"libblobcmds.0", false}, {"libblobcmds.10", false}, 86 {"libblobcmds.a", false}, {"libcmds.so.so.0", true}, 87 {"libcmds.so.0", true}, {"libcmds.so.0.0", false}, 88 {"libcmds.so.0.0.10", false}, {"libblobs.so.1000", true}}; 89 90 for (const auto& test : tests) 91 { 92 EXPECT_EQ(test.expectation, matchBlobHandler(test.name)); 93 } 94 } 95 96 } // namespace blobs 97