flash-ipmi: implement flashDataFinish

Change-Id: I0706df68bedd4ebceb50b10682e5e056fc7937f1
Signed-off-by: Patrick Venture <venture@google.com>
diff --git a/flash-ipmi.cpp b/flash-ipmi.cpp
index 07b9b9a..42333dd 100644
--- a/flash-ipmi.cpp
+++ b/flash-ipmi.cpp
@@ -129,7 +129,13 @@
 
 bool FlashUpdate::flashFinish()
 {
-    /* TODO: implement. */
+    /* If it's open, close it. */
+    if (flashFd)
+    {
+        closeFile(&flashFd);
+        return true;
+    }
+
     return false;
 }
 
diff --git a/test/Makefile.am b/test/Makefile.am
index 43040a1..d0b8b2a 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -21,7 +21,8 @@
 	ipmi_validate_unittest \
 	ipmi_command_unittest \
 	flash_start_unittest \
-	flash_flashdata_unittest
+	flash_flashdata_unittest \
+	flash_flashfinish_unittest
 
 TESTS = $(check_PROGRAMS)
 
@@ -60,3 +61,6 @@
 
 flash_flashdata_unittest_SOURCES = flash_flashdata_unittest.cpp
 flash_flashdata_unittest_LDADD = $(top_builddir)/flash-ipmi.o $(SDBUSPLUS_LIBS)
+
+flash_flashfinish_unittest_SOURCES = flash_flashfinish_unittest.cpp
+flash_flashfinish_unittest_LDADD = $(top_builddir)/flash-ipmi.o $(SDBUSPLUS_LIBS)
diff --git a/test/flash_flashfinish_unittest.cpp b/test/flash_flashfinish_unittest.cpp
new file mode 100644
index 0000000..bdddd5a
--- /dev/null
+++ b/test/flash_flashfinish_unittest.cpp
@@ -0,0 +1,72 @@
+#include "flash-ipmi.hpp"
+
+#include <cstdio>
+#include <cstring>
+#include <gtest/gtest.h>
+#include <string>
+#include <vector>
+
+#define THIRTYTWO_MIB 33554432
+
+class FlashIpmiFlashDataTest : public ::testing::Test
+{
+  protected:
+    FlashIpmiFlashDataTest() = default;
+
+    void SetUp() override
+    {
+        name = std::tmpnam(nullptr);
+    }
+    void TearDown() override
+    {
+        (void)std::remove(name.c_str());
+    }
+
+    std::string name;
+};
+
+TEST_F(FlashIpmiFlashDataTest, CalledOutOfSequenceFails)
+{
+    // Verify that there is sanity checking
+    std::vector<uint8_t> bytes = {0xaa, 0x55};
+
+    FlashUpdate updater(name);
+    EXPECT_FALSE(updater.flashFinish());
+
+    // Verify the file doesn't exist.
+    auto file = std::fopen(name.c_str(), "r");
+    EXPECT_FALSE(file);
+}
+
+TEST_F(FlashIpmiFlashDataTest, CalledInSequenceSucceeds)
+{
+    // Verify that under normal usage it closes the file.
+    std::vector<uint8_t> bytes = {0xaa, 0x55};
+
+    FlashUpdate updater(name);
+    updater.start(THIRTYTWO_MIB);
+    EXPECT_TRUE(updater.flashFinish());
+
+    // Verify we can open the file, so we know it didn't get deleted.
+    auto file = std::fopen(name.c_str(), "r");
+    EXPECT_TRUE(file);
+    std::fclose(file);
+}
+
+TEST_F(FlashIpmiFlashDataTest, 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);
+    updater.start(THIRTYTWO_MIB);
+    EXPECT_TRUE(updater.flashFinish());
+
+    EXPECT_FALSE(updater.flashFinish());
+
+    // Verify we can open the file, so we know it didn't get deleted.
+    auto file = std::fopen(name.c_str(), "r");
+    EXPECT_TRUE(file);
+    std::fclose(file);
+}