Add zero write and verify

This code implements the zero verify and zero write dbus interface. The
goal is to fill the whole block device with zeros, then check to make
sure the operation worked correctly.

Tested:
$ systemctl stop emmc.service
$ ./eStoraged -b /dev/mmcblk0 &
$ time busctl call xyz.openbmc_project.eStoraged.mmcblk0 /xyz/openbmc_project/storage/mmcblk0 xyz.openbmc_project.Inventory.Item.Volume Erase s
xyz.openbmc_project.Inventory.Item.Volume.EraseMethod.ZeroOverWrite --timeout=1200
Erasing encrypted eMMC                                                                                                                                                   <6> Starting erase

real    5m59.695s
user    0m0.000s
sys     0m0.030s

root@ytbaz20-nfd01:~/jebr# hexdump /dev/mmcblk0
0000000 0000 0000 0000 0000 0000 0000 0000 0000
*
$ time busctl call xyz.openbmc_project.eStoraged.mmcblk0 /xyz/openbmc_project/storage/mmcblk0 xyz.openbmc_project.Inventory.Item.Volume Erase s
xyz.openbmc_project.Inventory.Item.Volume.EraseMethod.ZeroVerify  --timeout=1200
Erasing encrypted eMMC
<6> Starting erase

real    5m46.920s
user    0m0.010s
sys     0m0.010s
$ echo "not zero" > /dev/mmcblk0
$ time busctl call xyz.openbmc_project.eStoraged.mmcblk0 /xyz/openbmc_project/storage/mmcblk0 xyz.openbmc_project.Inventory.Item.Volume Erase s
xyz.openbmc_project.Inventory.Item.Volume.EraseMethod.ZeroVerify  --timeout=1200
Erasing encrypted eMMC
<6> Starting erase
<3> Estoraged erase zeros block is not zero
Call failed: The operation failed internally.

real    0m0.022s
user    0m0.000s
sys     0m0.020s

Change-Id: Ie78ad427de1aa75472fc7ddd72d094866fe14b66
Signed-off-by: John Edward Broadbent <jebr@google.com>
diff --git a/src/test/erase/zero_test.cpp b/src/test/erase/zero_test.cpp
new file mode 100644
index 0000000..b3b91ec
--- /dev/null
+++ b/src/test/erase/zero_test.cpp
@@ -0,0 +1,99 @@
+#include "estoraged_conf.hpp"
+#include "zero.hpp"
+
+#include <fcntl.h>
+#include <unistd.h>
+
+#include <stdplus/fd/create.hpp>
+#include <stdplus/fd/managed.hpp>
+#include <xyz/openbmc_project/Common/error.hpp>
+
+#include <system_error>
+
+#include <gmock/gmock-matchers.h>
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+
+using estoraged::Zero;
+using sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
+using stdplus::fd::ManagedFd;
+
+namespace estoraged_test
+{
+
+TEST(Zeros, zeroPass)
+{
+    uint64_t size = 4096;
+    int fds[2];
+    Zero pass("fileName");
+    if (pipe(fds))
+    {
+        FAIL();
+    }
+    ManagedFd writeFd(std::move(fds[1]));
+    EXPECT_NO_THROW(pass.writeZero(size, writeFd));
+    ManagedFd verifyFd(std::move(fds[0]));
+    EXPECT_NO_THROW(pass.verifyZero(size, verifyFd));
+}
+
+/* This test that pattern writes the correct number of bytes even if
+ * size of the drive is not divisable by the block size
+ */
+TEST(Zeros, notDivisible)
+{
+    uint64_t size = 4097;
+    // 4097 is not divisible by the block size, and we expect no errors
+    int fds[2];
+    Zero pass("fileName");
+    if (pipe(fds))
+    {
+        FAIL();
+    }
+    ManagedFd writeFd(std::move(fds[1]));
+    EXPECT_NO_THROW(pass.writeZero(size, writeFd));
+    ManagedFd verifyFd(std::move(fds[0]));
+    EXPECT_NO_THROW(pass.verifyZero(size, verifyFd));
+}
+
+TEST(Zeros, notZeroStart)
+{
+    uint64_t size = 4096;
+    int fds[2];
+    Zero pass("fileName");
+    if (pipe(fds))
+    {
+        FAIL();
+    }
+    int dummyValue = 88;
+    if (::write(fds[1], &dummyValue, sizeof(dummyValue)) != sizeof(dummyValue))
+    {
+        FAIL();
+    }
+    ManagedFd writeFd(std::move(fds[1]));
+    EXPECT_NO_THROW(pass.writeZero(size - sizeof(dummyValue), writeFd));
+    ManagedFd verifyFd(std::move(fds[0]));
+    EXPECT_THROW(pass.verifyZero(size, verifyFd), InternalFailure);
+}
+
+TEST(Zeros, notZeroEnd)
+{
+    uint64_t size = 4096;
+    int fds[2];
+    Zero pass("fileName");
+    if (pipe(fds))
+    {
+        FAIL();
+    }
+    int dummyValue = 88;
+    int tmpFd = fds[1];
+    ManagedFd writeFd(std::move(tmpFd));
+    EXPECT_NO_THROW(pass.writeZero(size - sizeof(dummyValue), writeFd));
+    if (::write(fds[1], &dummyValue, sizeof(dummyValue)) != sizeof(dummyValue))
+    {
+        FAIL();
+    }
+    ManagedFd verifyFd(std::move(fds[0]));
+    EXPECT_THROW(pass.verifyZero(size, verifyFd), InternalFailure);
+}
+
+} // namespace estoraged_test