summaryrefslogtreecommitdiffstats
path: root/test/utils_unittest.cpp
blob: b986bd8420f8e6023ade7a0c6c3f86edab0b649b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include "blob_mock.hpp"
#include "dlsys_mock.hpp"
#include "fs.hpp"
#include "manager_mock.hpp"
#include "utils.hpp"

#include <experimental/filesystem>
#include <memory>
#include <string>
#include <vector>

#include <gtest/gtest.h>

namespace fs = std::experimental::filesystem;

namespace blobs
{
using ::testing::_;
using ::testing::Return;
using ::testing::StrEq;
using ::testing::StrictMock;

std::vector<std::string>* returnList = nullptr;

std::vector<std::string> getLibraryList(const std::string& path,
                                        PathMatcher check)
{
    return (returnList) ? *returnList : std::vector<std::string>();
}

std::unique_ptr<GenericBlobInterface> factoryReturn;

std::unique_ptr<GenericBlobInterface> fakeFactory()
{
    return std::move(factoryReturn);
}

TEST(UtilLoadLibraryTest, NoFilesFound)
{
    /* Verify nothing special happens when there are no files found. */

    StrictMock<internal::InternalDlSysMock> dlsys;
    StrictMock<ManagerMock> manager;

    loadLibraries(&manager, "", &dlsys);
}

TEST(UtilLoadLibraryTest, OneFileFoundIsLibrary)
{
    /* Verify if it finds a library, and everything works, it'll regsiter it.
     */

    std::vector<std::string> files = {"this.fake"};
    returnList = &files;

    StrictMock<internal::InternalDlSysMock> dlsys;
    StrictMock<ManagerMock> manager;
    void* handle = reinterpret_cast<void*>(0x01);
    auto blobMock = std::make_unique<BlobMock>();

    factoryReturn = std::move(blobMock);

    EXPECT_CALL(dlsys, dlopen(_, _)).WillOnce(Return(handle));

    EXPECT_CALL(dlsys, dlerror()).Times(2).WillRepeatedly(Return(nullptr));

    EXPECT_CALL(dlsys, dlsym(handle, StrEq("createHandler")))
        .WillOnce(Return(reinterpret_cast<void*>(fakeFactory)));

    EXPECT_CALL(manager, registerHandler(_));

    loadLibraries(&manager, "", &dlsys);
}

TEST(UtilLibraryMatchTest, TestAll)
{
    struct LibraryMatch
    {
        std::string name;
        bool expectation;
    };

    std::vector<LibraryMatch> tests = {
        {"libblobcmds.0.0.1", false}, {"libblobcmds.0.0", false},
        {"libblobcmds.0", false},     {"libblobcmds.10", false},
        {"libblobcmds.a", false},     {"libcmds.so.so.0", true},
        {"libcmds.so.0", true},       {"libcmds.so.0.0", false},
        {"libcmds.so.0.0.10", false}, {"libblobs.so.1000", true}};

    for (const auto& test : tests)
    {
        EXPECT_EQ(test.expectation, matchBlobHandler(test.name));
    }
}

} // namespace blobs
OpenPOWER on IntegriCloud