test: firmware notYetStarted: open(tarball)

Verify the tarball opening transitions to uploadInProgress to avoid any
future regressions.

Signed-off-by: Patrick Venture <venture@google.com>
Change-Id: I2d6230ae012e688854f9cecef8cc5466527df313
diff --git a/test/Makefile.am b/test/Makefile.am
index 4cde675..ca85bb9 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -40,7 +40,8 @@
 	firmware_state_verificationcompleted_unittest \
 	firmware_state_updatepending_unittest \
 	firmware_state_updatestarted_unittest \
-	firmware_state_updatecompleted_unittest
+	firmware_state_updatecompleted_unittest \
+	firmware_state_notyetstarted_tarball_unittest
 
 if BUILD_HOST_TOOL
 check_PROGRAMS += \
@@ -111,6 +112,9 @@
 firmware_state_updatecompleted_unittest_SOURCES = firmware_state_updatecompleted_unittest.cpp
 firmware_state_updatecompleted_unittest_LDADD = $(top_builddir)/libfirmwareblob_common.la
 
+firmware_state_notyetstarted_tarball_unittest_SOURCES = firmware_state_notyetstarted_tarball_unittest.cpp
+firmware_state_notyetstarted_tarball_unittest_LDADD = $(top_builddir)/libfirmwareblob_common.la
+
 if BUILD_HOST_TOOL
 tools_bt_unittest_SOURCES = tools_bt_unittest.cpp
 tools_bt_unittest_LDADD = $(top_builddir)/tools/libupdater.la
diff --git a/test/firmware_state_notyetstarted_tarball_unittest.cpp b/test/firmware_state_notyetstarted_tarball_unittest.cpp
new file mode 100644
index 0000000..b0e631c
--- /dev/null
+++ b/test/firmware_state_notyetstarted_tarball_unittest.cpp
@@ -0,0 +1,82 @@
+/**
+ * The goal of these tests is to verify opening the ubi tarball changes state
+ * as expected and does not regress.
+ */
+#include "firmware_handler.hpp"
+#include "firmware_unittest.hpp"
+
+#include <string>
+#include <vector>
+
+#include <gtest/gtest.h>
+
+namespace ipmi_flash
+{
+namespace
+{
+
+using ::testing::UnorderedElementsAreArray;
+
+class FirmwareHandlerNotYetStartedUbitTest : public ::testing::Test
+{
+  protected:
+    void SetUp() override
+    {
+        blobs = {
+            {hashBlobId, &imageMock},
+            {ubiTarballBlobId, &imageMock},
+        };
+
+        std::unique_ptr<TriggerableActionInterface> verifyMock =
+            std::make_unique<TriggerMock>();
+        verifyMockPtr = reinterpret_cast<TriggerMock*>(verifyMock.get());
+
+        std::unique_ptr<TriggerableActionInterface> updateMock =
+            std::make_unique<TriggerMock>();
+        updateMockPtr = reinterpret_cast<TriggerMock*>(updateMock.get());
+
+        handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
+            blobs, data, std::move(verifyMock), std::move(updateMock));
+    }
+
+    void expectedState(FirmwareBlobHandler::UpdateState state)
+    {
+        auto realHandler = dynamic_cast<FirmwareBlobHandler*>(handler.get());
+        EXPECT_EQ(state, realHandler->getCurrentState());
+    }
+
+    void openToInProgress(const std::string& blobId)
+    {
+        EXPECT_CALL(imageMock, open(blobId)).WillOnce(Return(true));
+        EXPECT_TRUE(handler->open(session, flags, blobId));
+        expectedState(FirmwareBlobHandler::UpdateState::uploadInProgress);
+    }
+
+    ImageHandlerMock imageMock;
+    std::vector<HandlerPack> blobs;
+    std::vector<DataHandlerPack> data = {
+        {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr}};
+    std::unique_ptr<blobs::GenericBlobInterface> handler;
+    TriggerMock* verifyMockPtr;
+    TriggerMock* updateMockPtr;
+
+    std::uint16_t session = 1;
+    std::uint16_t flags =
+        blobs::OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi;
+};
+
+TEST_F(FirmwareHandlerNotYetStartedUbitTest,
+       OpeningTarballMovesToUploadInProgress)
+{
+    expectedState(FirmwareBlobHandler::UpdateState::notYetStarted);
+
+    std::vector<std::string> expected = {hashBlobId, ubiTarballBlobId};
+    EXPECT_THAT(handler->getBlobIds(), UnorderedElementsAreArray(expected));
+
+    openToInProgress(ubiTarballBlobId);
+    expected = {hashBlobId, ubiTarballBlobId, activeImageBlobId};
+    EXPECT_THAT(handler->getBlobIds(), UnorderedElementsAreArray(expected));
+}
+
+} // namespace
+} // namespace ipmi_flash