tools/pci: add wrapper for libpciaccess

This allows unit testing code that uses libpciaccess.

Signed-off-by: Benjamin Fair <benjaminfair@google.com>
Change-Id: Iec559c72e0e7b6c2e8737a54bb3e88fe1e0f4fdb
diff --git a/tools/Makefile.am b/tools/Makefile.am
index e0a192e..f63a112 100644
--- a/tools/Makefile.am
+++ b/tools/Makefile.am
@@ -22,6 +22,7 @@
 	io.cpp \
 	net.cpp \
 	pci.cpp \
+	pciaccess.cpp \
 	p2a.cpp \
 	progress.cpp
 libupdater_la_LIBADD = $(top_builddir)/libfirmware_common.la
diff --git a/tools/pciaccess.cpp b/tools/pciaccess.cpp
new file mode 100644
index 0000000..8aced6f
--- /dev/null
+++ b/tools/pciaccess.cpp
@@ -0,0 +1,76 @@
+/*
+ * Copyright 2020 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "pciaccess.hpp"
+
+#include <cstdio>
+#include <cstring>
+#include <system_error>
+
+namespace host_tool
+{
+
+struct pci_device_iterator* PciAccessImpl::pci_id_match_iterator_create(
+    const struct pci_id_match* match) const
+{
+    return ::pci_id_match_iterator_create(match);
+}
+
+void PciAccessImpl::pci_iterator_destroy(struct pci_device_iterator* iter) const
+{
+    return ::pci_iterator_destroy(iter);
+}
+
+struct pci_device*
+    PciAccessImpl::pci_device_next(struct pci_device_iterator* iter) const
+{
+    return ::pci_device_next(iter);
+}
+
+int PciAccessImpl::pci_device_probe(struct pci_device* dev) const
+{
+    return ::pci_device_probe(dev);
+}
+
+int PciAccessImpl::pci_device_map_range(struct pci_device* dev, pciaddr_t base,
+                                        pciaddr_t size, unsigned map_flags,
+                                        void** addr) const
+{
+    return ::pci_device_map_range(dev, base, size, map_flags, addr);
+}
+
+int PciAccessImpl::pci_device_unmap_range(struct pci_device* dev, void* memory,
+                                          pciaddr_t size) const
+{
+    return ::pci_device_unmap_range(dev, memory, size);
+}
+
+PciAccessImpl::PciAccessImpl()
+{
+    int ret = ::pci_system_init();
+    if (ret)
+    {
+        throw std::system_error(ret, std::generic_category(),
+                                "Error setting up libpciaccess");
+    }
+}
+
+PciAccessImpl::~PciAccessImpl()
+{
+    ::pci_system_cleanup();
+}
+
+} // namespace host_tool
diff --git a/tools/pciaccess.hpp b/tools/pciaccess.hpp
new file mode 100644
index 0000000..ea7d5b4
--- /dev/null
+++ b/tools/pciaccess.hpp
@@ -0,0 +1,81 @@
+/*
+ * Copyright 2020 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+extern "C"
+{
+#include <pciaccess.h>
+} // extern "C"
+
+namespace host_tool
+{
+
+/**
+ * @class PciAccess
+ * @brief Overridable interface to libpciacess for unit testing
+ */
+class PciAccess
+{
+  public:
+    virtual struct pci_device_iterator* pci_id_match_iterator_create(
+        const struct pci_id_match* match) const = 0;
+    virtual void
+        pci_iterator_destroy(struct pci_device_iterator* iter) const = 0;
+    virtual struct pci_device*
+        pci_device_next(struct pci_device_iterator* iter) const = 0;
+    virtual int pci_device_probe(struct pci_device* dev) const = 0;
+    virtual int pci_device_map_range(struct pci_device* dev, pciaddr_t base,
+                                     pciaddr_t size, unsigned map_flags,
+                                     void** addr) const = 0;
+    virtual int pci_device_unmap_range(struct pci_device* dev, void* memory,
+                                       pciaddr_t size) const = 0;
+
+    virtual ~PciAccess() = default;
+};
+
+/**
+ * @class PciAccessImpl
+ * @brief libpciaccess concrete implementation
+ * @details Passes through all calls to the underlying library.
+ */
+class PciAccessImpl : public PciAccess
+{
+  public:
+    struct pci_device_iterator* pci_id_match_iterator_create(
+        const struct pci_id_match* match) const override;
+    void pci_iterator_destroy(struct pci_device_iterator* iter) const override;
+    struct pci_device*
+        pci_device_next(struct pci_device_iterator* iter) const override;
+    int pci_device_probe(struct pci_device* dev) const override;
+    int pci_device_map_range(struct pci_device* dev, pciaddr_t base,
+                             pciaddr_t size, unsigned map_flags,
+                             void** addr) const override;
+    int pci_device_unmap_range(struct pci_device* dev, void* memory,
+                               pciaddr_t size) const override;
+
+    static PciAccessImpl& getInstance()
+    {
+        static PciAccessImpl instance;
+        return instance;
+    }
+
+  private:
+    PciAccessImpl();
+    virtual ~PciAccessImpl();
+};
+
+} // namespace host_tool
diff --git a/tools/test/Makefile.am b/tools/test/Makefile.am
index 1fe35a0..16c1bb3 100644
--- a/tools/test/Makefile.am
+++ b/tools/test/Makefile.am
@@ -18,6 +18,7 @@
 check_PROGRAMS = \
 	tools_bt_unittest \
 	tools_lpc_unittest \
+	tools_pci_unittest \
 	tools_net_unittest \
 	tools_updater_unittest \
 	tools_helper_unittest
@@ -30,6 +31,9 @@
 tools_lpc_unittest_SOURCES = tools_lpc_unittest.cpp
 tools_lpc_unittest_LDADD = $(top_builddir)/tools/libupdater.la
 
+tools_pci_unittest_SOURCES = tools_pci_unittest.cpp
+tools_pci_unittest_LDADD = $(top_builddir)/tools/libupdater.la
+
 tools_net_unittest_SOURCES = tools_net_unittest.cpp
 tools_net_unittest_LDADD = $(top_builddir)/tools/libupdater.la
 
diff --git a/tools/test/pciaccess_mock.hpp b/tools/test/pciaccess_mock.hpp
new file mode 100644
index 0000000..e717c74
--- /dev/null
+++ b/tools/test/pciaccess_mock.hpp
@@ -0,0 +1,25 @@
+#pragma once
+
+#include "pciaccess.hpp"
+
+#include <gmock/gmock.h>
+
+namespace host_tool
+{
+
+class PciAccessMock : public PciAccess
+{
+  public:
+    MOCK_CONST_METHOD1(pci_id_match_iterator_create,
+                       struct pci_device_iterator*(const struct pci_id_match*));
+    MOCK_CONST_METHOD1(pci_iterator_destroy, void(struct pci_device_iterator*));
+    MOCK_CONST_METHOD1(pci_device_next,
+                       struct pci_device*(struct pci_device_iterator*));
+    MOCK_CONST_METHOD1(pci_device_probe, int(struct pci_device*));
+    MOCK_CONST_METHOD5(pci_device_map_range, int(struct pci_device*, pciaddr_t,
+                                                 pciaddr_t, unsigned, void**));
+    MOCK_CONST_METHOD3(pci_device_unmap_range,
+                       int(struct pci_device*, void*, pciaddr_t));
+};
+
+} // namespace host_tool
diff --git a/tools/test/tools_pci_unittest.cpp b/tools/test/tools_pci_unittest.cpp
new file mode 100644
index 0000000..cc67926
--- /dev/null
+++ b/tools/test/tools_pci_unittest.cpp
@@ -0,0 +1,19 @@
+#include "internal_sys_mock.hpp"
+#include "pciaccess_mock.hpp"
+
+#include <gtest/gtest.h>
+
+namespace host_tool
+{
+namespace
+{
+
+TEST(PciHandleTest, empty)
+{
+    PciAccessMock pciMock;
+
+    (void)pciMock;
+}
+
+} // namespace
+} // namespace host_tool