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