1cd8dab49SPatrick Venture #include "blob_mock.hpp"
2c18e2b64SPatrick Venture #include "dlsys_mock.hpp"
3c18e2b64SPatrick Venture #include "fs.hpp"
4cd8dab49SPatrick Venture #include "manager_mock.hpp"
5c18e2b64SPatrick Venture #include "utils.hpp"
6c18e2b64SPatrick Venture
748eb4029SPatrick Venture #include <filesystem>
8c18e2b64SPatrick Venture #include <memory>
9c18e2b64SPatrick Venture #include <string>
10c18e2b64SPatrick Venture #include <vector>
11c18e2b64SPatrick Venture
1299f25bebSPatrick Venture #include <gmock/gmock.h>
13c18e2b64SPatrick Venture #include <gtest/gtest.h>
14c18e2b64SPatrick Venture
1548eb4029SPatrick Venture namespace fs = std::filesystem;
16c18e2b64SPatrick Venture
17c18e2b64SPatrick Venture namespace blobs
18c18e2b64SPatrick Venture {
19c18e2b64SPatrick Venture using ::testing::_;
20c18e2b64SPatrick Venture using ::testing::Return;
21c18e2b64SPatrick Venture using ::testing::StrEq;
22c18e2b64SPatrick Venture using ::testing::StrictMock;
23c18e2b64SPatrick Venture
2499f25bebSPatrick Venture std::vector<std::string> returnList;
25c18e2b64SPatrick Venture
getLibraryList(const std::string &,PathMatcher)26*993f5419SWilliam A. Kennington III std::vector<std::string> getLibraryList(const std::string&, PathMatcher)
27c18e2b64SPatrick Venture {
2899f25bebSPatrick Venture return returnList;
29c18e2b64SPatrick Venture }
30c18e2b64SPatrick Venture
31c18e2b64SPatrick Venture std::unique_ptr<GenericBlobInterface> factoryReturn;
32c18e2b64SPatrick Venture
fakeFactory()33c18e2b64SPatrick Venture std::unique_ptr<GenericBlobInterface> fakeFactory()
34c18e2b64SPatrick Venture {
35c18e2b64SPatrick Venture return std::move(factoryReturn);
36c18e2b64SPatrick Venture }
37c18e2b64SPatrick Venture
3899f25bebSPatrick Venture class UtilLoadLibraryTest : public ::testing::Test
3999f25bebSPatrick Venture {
4099f25bebSPatrick Venture protected:
UtilLoadLibraryTest()4199f25bebSPatrick Venture UtilLoadLibraryTest()
4299f25bebSPatrick Venture {
4399f25bebSPatrick Venture returnList = {};
4499f25bebSPatrick Venture }
4599f25bebSPatrick Venture };
4699f25bebSPatrick Venture
TEST_F(UtilLoadLibraryTest,NoFilesFound)4799f25bebSPatrick Venture TEST_F(UtilLoadLibraryTest, NoFilesFound)
48c18e2b64SPatrick Venture {
49c18e2b64SPatrick Venture /* Verify nothing special happens when there are no files found. */
50c18e2b64SPatrick Venture
51c18e2b64SPatrick Venture StrictMock<internal::InternalDlSysMock> dlsys;
52c18e2b64SPatrick Venture StrictMock<ManagerMock> manager;
53c18e2b64SPatrick Venture
54c18e2b64SPatrick Venture loadLibraries(&manager, "", &dlsys);
55c18e2b64SPatrick Venture }
56c18e2b64SPatrick Venture
TEST_F(UtilLoadLibraryTest,OneFileFoundIsLibrary)5799f25bebSPatrick Venture TEST_F(UtilLoadLibraryTest, OneFileFoundIsLibrary)
58c18e2b64SPatrick Venture {
59c18e2b64SPatrick Venture /* Verify if it finds a library, and everything works, it'll regsiter it.
60c18e2b64SPatrick Venture */
61c18e2b64SPatrick Venture
6299f25bebSPatrick Venture returnList = {"this.fake"};
63c18e2b64SPatrick Venture
64c18e2b64SPatrick Venture StrictMock<internal::InternalDlSysMock> dlsys;
65c18e2b64SPatrick Venture StrictMock<ManagerMock> manager;
66c18e2b64SPatrick Venture void* handle = reinterpret_cast<void*>(0x01);
67c18e2b64SPatrick Venture auto blobMock = std::make_unique<BlobMock>();
68c18e2b64SPatrick Venture
69c18e2b64SPatrick Venture factoryReturn = std::move(blobMock);
70c18e2b64SPatrick Venture
71c18e2b64SPatrick Venture EXPECT_CALL(dlsys, dlopen(_, _)).WillOnce(Return(handle));
72c18e2b64SPatrick Venture
73c18e2b64SPatrick Venture EXPECT_CALL(dlsys, dlerror()).Times(2).WillRepeatedly(Return(nullptr));
74c18e2b64SPatrick Venture
75c18e2b64SPatrick Venture EXPECT_CALL(dlsys, dlsym(handle, StrEq("createHandler")))
76c18e2b64SPatrick Venture .WillOnce(Return(reinterpret_cast<void*>(fakeFactory)));
77c18e2b64SPatrick Venture
78c18e2b64SPatrick Venture EXPECT_CALL(manager, registerHandler(_));
79c18e2b64SPatrick Venture
80c18e2b64SPatrick Venture loadLibraries(&manager, "", &dlsys);
81c18e2b64SPatrick Venture }
82c18e2b64SPatrick Venture
TEST(UtilLibraryMatchTest,TestAll)83c18e2b64SPatrick Venture TEST(UtilLibraryMatchTest, TestAll)
84c18e2b64SPatrick Venture {
85c18e2b64SPatrick Venture struct LibraryMatch
86c18e2b64SPatrick Venture {
87c18e2b64SPatrick Venture std::string name;
88c18e2b64SPatrick Venture bool expectation;
89c18e2b64SPatrick Venture };
90c18e2b64SPatrick Venture
91c18e2b64SPatrick Venture std::vector<LibraryMatch> tests = {
92c18e2b64SPatrick Venture {"libblobcmds.0.0.1", false}, {"libblobcmds.0.0", false},
93c18e2b64SPatrick Venture {"libblobcmds.0", false}, {"libblobcmds.10", false},
94c18e2b64SPatrick Venture {"libblobcmds.a", false}, {"libcmds.so.so.0", true},
953ccee07bSWilliam A. Kennington III {"libcmds.so.0", true}, {"libcmds.so", true},
963ccee07bSWilliam A. Kennington III {"libcmds.so.0.0.10", true}, {"libblobs.so.1000", true}};
97c18e2b64SPatrick Venture
98c18e2b64SPatrick Venture for (const auto& test : tests)
99c18e2b64SPatrick Venture {
100c18e2b64SPatrick Venture EXPECT_EQ(test.expectation, matchBlobHandler(test.name));
101c18e2b64SPatrick Venture }
102c18e2b64SPatrick Venture }
103c18e2b64SPatrick Venture
104c18e2b64SPatrick Venture } // namespace blobs
105