| Patrick Venture | cd8dab4 | 2019-01-15 19:57:38 -0800 | [diff] [blame] | 1 | #include "blob_mock.hpp" | 
| Patrick Venture | c18e2b6 | 2018-11-21 14:19:28 -0800 | [diff] [blame] | 2 | #include "dlsys_mock.hpp" | 
 | 3 | #include "fs.hpp" | 
| Patrick Venture | cd8dab4 | 2019-01-15 19:57:38 -0800 | [diff] [blame] | 4 | #include "manager_mock.hpp" | 
| Patrick Venture | c18e2b6 | 2018-11-21 14:19:28 -0800 | [diff] [blame] | 5 | #include "utils.hpp" | 
 | 6 |  | 
| Patrick Venture | 48eb402 | 2019-03-08 13:08:42 -0800 | [diff] [blame] | 7 | #include <filesystem> | 
| Patrick Venture | c18e2b6 | 2018-11-21 14:19:28 -0800 | [diff] [blame] | 8 | #include <memory> | 
 | 9 | #include <string> | 
 | 10 | #include <vector> | 
 | 11 |  | 
| Patrick Venture | 99f25be | 2020-09-28 15:12:21 -0700 | [diff] [blame] | 12 | #include <gmock/gmock.h> | 
| Patrick Venture | c18e2b6 | 2018-11-21 14:19:28 -0800 | [diff] [blame] | 13 | #include <gtest/gtest.h> | 
 | 14 |  | 
| Patrick Venture | 48eb402 | 2019-03-08 13:08:42 -0800 | [diff] [blame] | 15 | namespace fs = std::filesystem; | 
| Patrick Venture | c18e2b6 | 2018-11-21 14:19:28 -0800 | [diff] [blame] | 16 |  | 
 | 17 | namespace blobs | 
 | 18 | { | 
 | 19 | using ::testing::_; | 
 | 20 | using ::testing::Return; | 
 | 21 | using ::testing::StrEq; | 
 | 22 | using ::testing::StrictMock; | 
 | 23 |  | 
| Patrick Venture | 99f25be | 2020-09-28 15:12:21 -0700 | [diff] [blame] | 24 | std::vector<std::string> returnList; | 
| Patrick Venture | c18e2b6 | 2018-11-21 14:19:28 -0800 | [diff] [blame] | 25 |  | 
 | 26 | std::vector<std::string> getLibraryList(const std::string& path, | 
 | 27 |                                         PathMatcher check) | 
 | 28 | { | 
| Patrick Venture | 99f25be | 2020-09-28 15:12:21 -0700 | [diff] [blame] | 29 |     return returnList; | 
| Patrick Venture | c18e2b6 | 2018-11-21 14:19:28 -0800 | [diff] [blame] | 30 | } | 
 | 31 |  | 
 | 32 | std::unique_ptr<GenericBlobInterface> factoryReturn; | 
 | 33 |  | 
 | 34 | std::unique_ptr<GenericBlobInterface> fakeFactory() | 
 | 35 | { | 
 | 36 |     return std::move(factoryReturn); | 
 | 37 | } | 
 | 38 |  | 
| Patrick Venture | 99f25be | 2020-09-28 15:12:21 -0700 | [diff] [blame] | 39 | class UtilLoadLibraryTest : public ::testing::Test | 
 | 40 | { | 
 | 41 |   protected: | 
 | 42 |     UtilLoadLibraryTest() | 
 | 43 |     { | 
 | 44 |         returnList = {}; | 
 | 45 |     } | 
 | 46 | }; | 
 | 47 |  | 
 | 48 | TEST_F(UtilLoadLibraryTest, NoFilesFound) | 
| Patrick Venture | c18e2b6 | 2018-11-21 14:19:28 -0800 | [diff] [blame] | 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 |  | 
| Patrick Venture | 99f25be | 2020-09-28 15:12:21 -0700 | [diff] [blame] | 58 | TEST_F(UtilLoadLibraryTest, OneFileFoundIsLibrary) | 
| Patrick Venture | c18e2b6 | 2018-11-21 14:19:28 -0800 | [diff] [blame] | 59 | { | 
 | 60 |     /* Verify if it finds a library, and everything works, it'll regsiter it. | 
 | 61 |      */ | 
 | 62 |  | 
| Patrick Venture | 99f25be | 2020-09-28 15:12:21 -0700 | [diff] [blame] | 63 |     returnList = {"this.fake"}; | 
| Patrick Venture | c18e2b6 | 2018-11-21 14:19:28 -0800 | [diff] [blame] | 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}, | 
| William A. Kennington III | 3ccee07 | 2021-06-15 16:51:30 -0700 | [diff] [blame^] | 96 |         {"libcmds.so.0", true},       {"libcmds.so", true}, | 
 | 97 |         {"libcmds.so.0.0.10", true},  {"libblobs.so.1000", true}}; | 
| Patrick Venture | c18e2b6 | 2018-11-21 14:19:28 -0800 | [diff] [blame] | 98 |  | 
 | 99 |     for (const auto& test : tests) | 
 | 100 |     { | 
 | 101 |         EXPECT_EQ(test.expectation, matchBlobHandler(test.name)); | 
 | 102 |     } | 
 | 103 | } | 
 | 104 |  | 
 | 105 | } // namespace blobs |