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
getLibraryList(const std::string &,PathMatcher)26 std::vector<std::string> getLibraryList(const std::string&, PathMatcher)
27 {
28 return returnList;
29 }
30
31 std::unique_ptr<GenericBlobInterface> factoryReturn;
32
fakeFactory()33 std::unique_ptr<GenericBlobInterface> fakeFactory()
34 {
35 return std::move(factoryReturn);
36 }
37
38 class UtilLoadLibraryTest : public ::testing::Test
39 {
40 protected:
UtilLoadLibraryTest()41 UtilLoadLibraryTest()
42 {
43 returnList = {};
44 }
45 };
46
TEST_F(UtilLoadLibraryTest,NoFilesFound)47 TEST_F(UtilLoadLibraryTest, NoFilesFound)
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
TEST_F(UtilLoadLibraryTest,OneFileFoundIsLibrary)57 TEST_F(UtilLoadLibraryTest, OneFileFoundIsLibrary)
58 {
59 /* Verify if it finds a library, and everything works, it'll regsiter it.
60 */
61
62 returnList = {"this.fake"};
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
TEST(UtilLibraryMatchTest,TestAll)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},
95 {"libcmds.so.0", true}, {"libcmds.so", true},
96 {"libcmds.so.0.0.10", true}, {"libblobs.so.1000", true}};
97
98 for (const auto& test : tests)
99 {
100 EXPECT_EQ(test.expectation, matchBlobHandler(test.name));
101 }
102 }
103
104 } // namespace blobs
105