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