blob: cba8ee2bdbcfbd288ef85182aece4e099a16a1c2 [file] [log] [blame]
Patrick Venturecd8dab42019-01-15 19:57:38 -08001#include "blob_mock.hpp"
Patrick Venturec18e2b62018-11-21 14:19:28 -08002#include "dlsys_mock.hpp"
3#include "fs.hpp"
Patrick Venturecd8dab42019-01-15 19:57:38 -08004#include "manager_mock.hpp"
Patrick Venturec18e2b62018-11-21 14:19:28 -08005#include "utils.hpp"
6
Patrick Venture48eb4022019-03-08 13:08:42 -08007#include <filesystem>
Patrick Venturec18e2b62018-11-21 14:19:28 -08008#include <memory>
9#include <string>
10#include <vector>
11
Patrick Venture99f25be2020-09-28 15:12:21 -070012#include <gmock/gmock.h>
Patrick Venturec18e2b62018-11-21 14:19:28 -080013#include <gtest/gtest.h>
14
Patrick Venture48eb4022019-03-08 13:08:42 -080015namespace fs = std::filesystem;
Patrick Venturec18e2b62018-11-21 14:19:28 -080016
17namespace blobs
18{
19using ::testing::_;
20using ::testing::Return;
21using ::testing::StrEq;
22using ::testing::StrictMock;
23
Patrick Venture99f25be2020-09-28 15:12:21 -070024std::vector<std::string> returnList;
Patrick Venturec18e2b62018-11-21 14:19:28 -080025
26std::vector<std::string> getLibraryList(const std::string& path,
27 PathMatcher check)
28{
Patrick Venture99f25be2020-09-28 15:12:21 -070029 return returnList;
Patrick Venturec18e2b62018-11-21 14:19:28 -080030}
31
32std::unique_ptr<GenericBlobInterface> factoryReturn;
33
34std::unique_ptr<GenericBlobInterface> fakeFactory()
35{
36 return std::move(factoryReturn);
37}
38
Patrick Venture99f25be2020-09-28 15:12:21 -070039class UtilLoadLibraryTest : public ::testing::Test
40{
41 protected:
42 UtilLoadLibraryTest()
43 {
44 returnList = {};
45 }
46};
47
48TEST_F(UtilLoadLibraryTest, NoFilesFound)
Patrick Venturec18e2b62018-11-21 14:19:28 -080049{
50 /* Verify nothing special happens when there are no files found. */
51
52 StrictMock<internal::InternalDlSysMock> dlsys;
53 StrictMock<ManagerMock> manager;
54
55 loadLibraries(&manager, "", &dlsys);
56}
57
Patrick Venture99f25be2020-09-28 15:12:21 -070058TEST_F(UtilLoadLibraryTest, OneFileFoundIsLibrary)
Patrick Venturec18e2b62018-11-21 14:19:28 -080059{
60 /* Verify if it finds a library, and everything works, it'll regsiter it.
61 */
62
Patrick Venture99f25be2020-09-28 15:12:21 -070063 returnList = {"this.fake"};
Patrick Venturec18e2b62018-11-21 14:19:28 -080064
65 StrictMock<internal::InternalDlSysMock> dlsys;
66 StrictMock<ManagerMock> manager;
67 void* handle = reinterpret_cast<void*>(0x01);
68 auto blobMock = std::make_unique<BlobMock>();
69
70 factoryReturn = std::move(blobMock);
71
72 EXPECT_CALL(dlsys, dlopen(_, _)).WillOnce(Return(handle));
73
74 EXPECT_CALL(dlsys, dlerror()).Times(2).WillRepeatedly(Return(nullptr));
75
76 EXPECT_CALL(dlsys, dlsym(handle, StrEq("createHandler")))
77 .WillOnce(Return(reinterpret_cast<void*>(fakeFactory)));
78
79 EXPECT_CALL(manager, registerHandler(_));
80
81 loadLibraries(&manager, "", &dlsys);
82}
83
84TEST(UtilLibraryMatchTest, TestAll)
85{
86 struct LibraryMatch
87 {
88 std::string name;
89 bool expectation;
90 };
91
92 std::vector<LibraryMatch> tests = {
93 {"libblobcmds.0.0.1", false}, {"libblobcmds.0.0", false},
94 {"libblobcmds.0", false}, {"libblobcmds.10", false},
95 {"libblobcmds.a", false}, {"libcmds.so.so.0", true},
William A. Kennington III3ccee072021-06-15 16:51:30 -070096 {"libcmds.so.0", true}, {"libcmds.so", true},
97 {"libcmds.so.0.0.10", true}, {"libblobs.so.1000", true}};
Patrick Venturec18e2b62018-11-21 14:19:28 -080098
99 for (const auto& test : tests)
100 {
101 EXPECT_EQ(test.expectation, matchBlobHandler(test.name));
102 }
103}
104
105} // namespace blobs