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