add dynamic library interface to enable testing

Add interface defining the methods for dynamic linking to enable
testing.

Change-Id: If4d090d3cedc019b426435a1f651191803bfc1a9
Signed-off-by: Patrick Venture <venture@google.com>
diff --git a/test/Makefile.am b/test/Makefile.am
index 5e0cdb0..0d36509 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -35,7 +35,9 @@
 	manager_read_unittest \
 	manager_writemeta_unittest \
 	process_unittest \
-	crc_unittest
+	crc_unittest \
+	utils_unittest
+
 TESTS = $(check_PROGRAMS)
 
 ipmi_unittest_SOURCES = ipmi_unittest.cpp
@@ -116,3 +118,6 @@
 
 crc_unittest_SOURCES = crc_unittest.cpp
 crc_unittest_LDADD = $(top_builddir)/crc.o
+
+utils_unittest_SOURCES = utils_unittest.cpp
+utils_unittest_LDADD =  $(top_builddir)/utils.o $(PHOSPHOR_LOGGING_LIBS)
diff --git a/test/dlsys_mock.hpp b/test/dlsys_mock.hpp
new file mode 100644
index 0000000..795b40e
--- /dev/null
+++ b/test/dlsys_mock.hpp
@@ -0,0 +1,23 @@
+#pragma once
+
+#include "internal/sys.hpp"
+
+#include <gmock/gmock.h>
+
+namespace blobs
+{
+namespace internal
+{
+
+class InternalDlSysMock : public DlSysInterface
+{
+  public:
+    virtual ~InternalDlSysMock() = default;
+
+    MOCK_CONST_METHOD0(dlerror, const char*());
+    MOCK_CONST_METHOD2(dlopen, void*(const char*, int));
+    MOCK_CONST_METHOD2(dlsym, void*(void*, const char*));
+};
+
+} // namespace internal
+} // namespace blobs
diff --git a/test/utils_unittest.cpp b/test/utils_unittest.cpp
new file mode 100644
index 0000000..8a0cdca
--- /dev/null
+++ b/test/utils_unittest.cpp
@@ -0,0 +1,96 @@
+#include "dlsys_mock.hpp"
+#include "fs.hpp"
+#include "utils.hpp"
+
+#include <blobs-ipmid/test/blob_mock.hpp>
+#include <blobs-ipmid/test/manager_mock.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