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