blob: 8a0cdca1c73111861939ed9436b1f106a0175605 [file] [log] [blame]
Patrick Venturec18e2b62018-11-21 14:19:28 -08001#include "dlsys_mock.hpp"
2#include "fs.hpp"
3#include "utils.hpp"
4
5#include <blobs-ipmid/test/blob_mock.hpp>
6#include <blobs-ipmid/test/manager_mock.hpp>
7#include <experimental/filesystem>
8#include <memory>
9#include <string>
10#include <vector>
11
12#include <gtest/gtest.h>
13
14namespace fs = std::experimental::filesystem;
15
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