blob: 9c01aefe40029a58052956ba46073d257d9f4a23 [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
William A. Kennington III993f5412021-06-15 18:19:18 -070026std::vector<std::string> getLibraryList(const std::string&, PathMatcher)
Patrick Venturec18e2b62018-11-21 14:19:28 -080027{
Patrick Venture99f25be2020-09-28 15:12:21 -070028 return returnList;
Patrick Venturec18e2b62018-11-21 14:19:28 -080029}
30
31std::unique_ptr<GenericBlobInterface> factoryReturn;
32
33std::unique_ptr<GenericBlobInterface> fakeFactory()
34{
35 return std::move(factoryReturn);
36}
37
Patrick Venture99f25be2020-09-28 15:12:21 -070038class UtilLoadLibraryTest : public ::testing::Test
39{
40 protected:
41 UtilLoadLibraryTest()
42 {
43 returnList = {};
44 }
45};
46
47TEST_F(UtilLoadLibraryTest, NoFilesFound)
Patrick Venturec18e2b62018-11-21 14:19:28 -080048{
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
Patrick Venture99f25be2020-09-28 15:12:21 -070057TEST_F(UtilLoadLibraryTest, OneFileFoundIsLibrary)
Patrick Venturec18e2b62018-11-21 14:19:28 -080058{
59 /* Verify if it finds a library, and everything works, it'll regsiter it.
60 */
61
Patrick Venture99f25be2020-09-28 15:12:21 -070062 returnList = {"this.fake"};
Patrick Venturec18e2b62018-11-21 14:19:28 -080063
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
83TEST(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},
William A. Kennington III3ccee072021-06-15 16:51:30 -070095 {"libcmds.so.0", true}, {"libcmds.so", true},
96 {"libcmds.so.0.0.10", true}, {"libblobs.so.1000", true}};
Patrick Venturec18e2b62018-11-21 14:19:28 -080097
98 for (const auto& test : tests)
99 {
100 EXPECT_EQ(test.expectation, matchBlobHandler(test.name));
101 }
102}
103
104} // namespace blobs