1*c18e2b64SPatrick Venture #include "dlsys_mock.hpp"
2*c18e2b64SPatrick Venture #include "fs.hpp"
3*c18e2b64SPatrick Venture #include "utils.hpp"
4*c18e2b64SPatrick Venture 
5*c18e2b64SPatrick Venture #include <blobs-ipmid/test/blob_mock.hpp>
6*c18e2b64SPatrick Venture #include <blobs-ipmid/test/manager_mock.hpp>
7*c18e2b64SPatrick Venture #include <experimental/filesystem>
8*c18e2b64SPatrick Venture #include <memory>
9*c18e2b64SPatrick Venture #include <string>
10*c18e2b64SPatrick Venture #include <vector>
11*c18e2b64SPatrick Venture 
12*c18e2b64SPatrick Venture #include <gtest/gtest.h>
13*c18e2b64SPatrick Venture 
14*c18e2b64SPatrick Venture namespace fs = std::experimental::filesystem;
15*c18e2b64SPatrick Venture 
16*c18e2b64SPatrick Venture namespace blobs
17*c18e2b64SPatrick Venture {
18*c18e2b64SPatrick Venture using ::testing::_;
19*c18e2b64SPatrick Venture using ::testing::Return;
20*c18e2b64SPatrick Venture using ::testing::StrEq;
21*c18e2b64SPatrick Venture using ::testing::StrictMock;
22*c18e2b64SPatrick Venture 
23*c18e2b64SPatrick Venture std::vector<std::string>* returnList = nullptr;
24*c18e2b64SPatrick Venture 
25*c18e2b64SPatrick Venture std::vector<std::string> getLibraryList(const std::string& path,
26*c18e2b64SPatrick Venture                                         PathMatcher check)
27*c18e2b64SPatrick Venture {
28*c18e2b64SPatrick Venture     return (returnList) ? *returnList : std::vector<std::string>();
29*c18e2b64SPatrick Venture }
30*c18e2b64SPatrick Venture 
31*c18e2b64SPatrick Venture std::unique_ptr<GenericBlobInterface> factoryReturn;
32*c18e2b64SPatrick Venture 
33*c18e2b64SPatrick Venture std::unique_ptr<GenericBlobInterface> fakeFactory()
34*c18e2b64SPatrick Venture {
35*c18e2b64SPatrick Venture     return std::move(factoryReturn);
36*c18e2b64SPatrick Venture }
37*c18e2b64SPatrick Venture 
38*c18e2b64SPatrick Venture TEST(UtilLoadLibraryTest, NoFilesFound)
39*c18e2b64SPatrick Venture {
40*c18e2b64SPatrick Venture     /* Verify nothing special happens when there are no files found. */
41*c18e2b64SPatrick Venture 
42*c18e2b64SPatrick Venture     StrictMock<internal::InternalDlSysMock> dlsys;
43*c18e2b64SPatrick Venture     StrictMock<ManagerMock> manager;
44*c18e2b64SPatrick Venture 
45*c18e2b64SPatrick Venture     loadLibraries(&manager, "", &dlsys);
46*c18e2b64SPatrick Venture }
47*c18e2b64SPatrick Venture 
48*c18e2b64SPatrick Venture TEST(UtilLoadLibraryTest, OneFileFoundIsLibrary)
49*c18e2b64SPatrick Venture {
50*c18e2b64SPatrick Venture     /* Verify if it finds a library, and everything works, it'll regsiter it.
51*c18e2b64SPatrick Venture      */
52*c18e2b64SPatrick Venture 
53*c18e2b64SPatrick Venture     std::vector<std::string> files = {"this.fake"};
54*c18e2b64SPatrick Venture     returnList = &files;
55*c18e2b64SPatrick Venture 
56*c18e2b64SPatrick Venture     StrictMock<internal::InternalDlSysMock> dlsys;
57*c18e2b64SPatrick Venture     StrictMock<ManagerMock> manager;
58*c18e2b64SPatrick Venture     void* handle = reinterpret_cast<void*>(0x01);
59*c18e2b64SPatrick Venture     auto blobMock = std::make_unique<BlobMock>();
60*c18e2b64SPatrick Venture 
61*c18e2b64SPatrick Venture     factoryReturn = std::move(blobMock);
62*c18e2b64SPatrick Venture 
63*c18e2b64SPatrick Venture     EXPECT_CALL(dlsys, dlopen(_, _)).WillOnce(Return(handle));
64*c18e2b64SPatrick Venture 
65*c18e2b64SPatrick Venture     EXPECT_CALL(dlsys, dlerror()).Times(2).WillRepeatedly(Return(nullptr));
66*c18e2b64SPatrick Venture 
67*c18e2b64SPatrick Venture     EXPECT_CALL(dlsys, dlsym(handle, StrEq("createHandler")))
68*c18e2b64SPatrick Venture         .WillOnce(Return(reinterpret_cast<void*>(fakeFactory)));
69*c18e2b64SPatrick Venture 
70*c18e2b64SPatrick Venture     EXPECT_CALL(manager, registerHandler(_));
71*c18e2b64SPatrick Venture 
72*c18e2b64SPatrick Venture     loadLibraries(&manager, "", &dlsys);
73*c18e2b64SPatrick Venture }
74*c18e2b64SPatrick Venture 
75*c18e2b64SPatrick Venture TEST(UtilLibraryMatchTest, TestAll)
76*c18e2b64SPatrick Venture {
77*c18e2b64SPatrick Venture     struct LibraryMatch
78*c18e2b64SPatrick Venture     {
79*c18e2b64SPatrick Venture         std::string name;
80*c18e2b64SPatrick Venture         bool expectation;
81*c18e2b64SPatrick Venture     };
82*c18e2b64SPatrick Venture 
83*c18e2b64SPatrick Venture     std::vector<LibraryMatch> tests = {
84*c18e2b64SPatrick Venture         {"libblobcmds.0.0.1", false}, {"libblobcmds.0.0", false},
85*c18e2b64SPatrick Venture         {"libblobcmds.0", false},     {"libblobcmds.10", false},
86*c18e2b64SPatrick Venture         {"libblobcmds.a", false},     {"libcmds.so.so.0", true},
87*c18e2b64SPatrick Venture         {"libcmds.so.0", true},       {"libcmds.so.0.0", false},
88*c18e2b64SPatrick Venture         {"libcmds.so.0.0.10", false}, {"libblobs.so.1000", true}};
89*c18e2b64SPatrick Venture 
90*c18e2b64SPatrick Venture     for (const auto& test : tests)
91*c18e2b64SPatrick Venture     {
92*c18e2b64SPatrick Venture         EXPECT_EQ(test.expectation, matchBlobHandler(test.name));
93*c18e2b64SPatrick Venture     }
94*c18e2b64SPatrick Venture }
95*c18e2b64SPatrick Venture 
96*c18e2b64SPatrick Venture } // namespace blobs
97