flash-ipmi: implement flashHashFinish

Change-Id: I44ff6494064c11071cc9e71402e5eebab8a6a1de
Signed-off-by: Patrick Venture <venture@google.com>
diff --git a/flash-ipmi.cpp b/flash-ipmi.cpp
index 8fc9a52..dda9580 100644
--- a/flash-ipmi.cpp
+++ b/flash-ipmi.cpp
@@ -176,7 +176,12 @@
 
 bool FlashUpdate::hashFinish()
 {
-    /* TODO: implement. */
+    if (hashFd)
+    {
+        closeFile(&hashFd);
+        return true;
+    }
+
     return false;
 }
 
diff --git a/test/Makefile.am b/test/Makefile.am
index 1fc2d92..bed24e6 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -24,7 +24,8 @@
 	flash_flashdata_unittest \
 	flash_flashfinish_unittest \
 	flash_hashstart_unittest \
-	flash_hashdata_unittest
+	flash_hashdata_unittest \
+	flash_hashfinish_unittest
 
 TESTS = $(check_PROGRAMS)
 
@@ -72,3 +73,6 @@
 
 flash_hashdata_unittest_SOURCES = flash_hashdata_unittest.cpp
 flash_hashdata_unittest_LDADD = $(top_builddir)/flash-ipmi.o $(SDBUSPLUS_LIBS)
+
+flash_hashfinish_unittest_SOURCES = flash_hashfinish_unittest.cpp
+flash_hashfinish_unittest_LDADD = $(top_builddir)/flash-ipmi.o $(SDBUSPLUS_LIBS)
diff --git a/test/flash_hashfinish_unittest.cpp b/test/flash_hashfinish_unittest.cpp
new file mode 100644
index 0000000..de785ae
--- /dev/null
+++ b/test/flash_hashfinish_unittest.cpp
@@ -0,0 +1,77 @@
+#include "flash-ipmi.hpp"
+
+#include <cstdio>
+#include <cstring>
+#include <gtest/gtest.h>
+#include <string>
+#include <vector>
+
+#define THIRTYTWO_MIB 33554432
+
+class FlashIpmiHashDataTest : public ::testing::Test
+{
+  protected:
+    FlashIpmiHashDataTest() = default;
+
+    void SetUp() override
+    {
+        name = std::tmpnam(nullptr);
+        name2 = std::tmpnam(nullptr);
+    }
+    void TearDown() override
+    {
+        (void)std::remove(name.c_str());
+        (void)std::remove(name2.c_str());
+    }
+
+    std::string name;
+    std::string name2;
+};
+
+TEST_F(FlashIpmiHashDataTest, CalledOutOfSequenceFails)
+{
+    // Verify that there is sanity checking
+    std::vector<uint8_t> bytes = {0xaa, 0x55};
+
+    FlashUpdate updater(name, name2);
+    EXPECT_FALSE(updater.hashFinish());
+
+    // Verify the file doesn't exist.
+    auto file = std::fopen(name2.c_str(), "r");
+    EXPECT_FALSE(file);
+}
+
+TEST_F(FlashIpmiHashDataTest, CalledInSequenceSucceeds)
+{
+    // Verify that under normal usage it closes the file.
+    std::vector<uint8_t> bytes = {0xaa, 0x55};
+
+    FlashUpdate updater(name, name2);
+    EXPECT_TRUE(updater.start(THIRTYTWO_MIB));
+    EXPECT_TRUE(updater.startHash(THIRTYTWO_MIB));
+    EXPECT_TRUE(updater.hashFinish());
+
+    // Verify we can open the file, so we know it didn't get deleted.
+    auto file = std::fopen(name2.c_str(), "r");
+    EXPECT_TRUE(file);
+    std::fclose(file);
+}
+
+TEST_F(FlashIpmiHashDataTest, CalledTwiceFails)
+{
+    // Verify that under normal usage it closes the file, and therefore cannot
+    // be closed twice.
+    std::vector<uint8_t> bytes = {0xaa, 0x55};
+
+    FlashUpdate updater(name, name2);
+    EXPECT_TRUE(updater.start(THIRTYTWO_MIB));
+    EXPECT_TRUE(updater.startHash(THIRTYTWO_MIB));
+    EXPECT_TRUE(updater.hashFinish());
+
+    EXPECT_FALSE(updater.hashFinish());
+
+    // Verify we can open the file, so we know it didn't get deleted.
+    auto file = std::fopen(name2.c_str(), "r");
+    EXPECT_TRUE(file);
+    std::fclose(file);
+}