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