test: add state check helper method

Reduce duplicate code by providing a simple helper that will perform the
expectation check of a given state.

Signed-off-by: Patrick Venture <venture@google.com>
Change-Id: I37aae09bebc44149671e1510e815dc724bd60c0b
diff --git a/test/firmware_state_notyetstarted_unittest.cpp b/test/firmware_state_notyetstarted_unittest.cpp
index 4d850aa..f662820 100644
--- a/test/firmware_state_notyetstarted_unittest.cpp
+++ b/test/firmware_state_notyetstarted_unittest.cpp
@@ -90,26 +90,22 @@
  */
 TEST_F(FirmwareHandlerNotYetStartedTest, OpenStaticImageFileVerifyStateChange)
 {
-    auto realHandler = dynamic_cast<FirmwareBlobHandler*>(handler.get());
-
     EXPECT_CALL(imageMock, open(staticLayoutBlobId)).WillOnce(Return(true));
 
     EXPECT_TRUE(handler->open(session, flags, staticLayoutBlobId));
-    EXPECT_EQ(FirmwareBlobHandler::UpdateState::uploadInProgress,
-              realHandler->getCurrentState());
+
+    expectedState(FirmwareBlobHandler::UpdateState::uploadInProgress);
 
     EXPECT_TRUE(handler->canHandleBlob(activeImageBlobId));
 }
 
 TEST_F(FirmwareHandlerNotYetStartedTest, OpenHashFileVerifyStateChange)
 {
-    auto realHandler = dynamic_cast<FirmwareBlobHandler*>(handler.get());
-
     EXPECT_CALL(imageMock, open(hashBlobId)).WillOnce(Return(true));
 
     EXPECT_TRUE(handler->open(session, flags, hashBlobId));
-    EXPECT_EQ(FirmwareBlobHandler::UpdateState::uploadInProgress,
-              realHandler->getCurrentState());
+
+    expectedState(FirmwareBlobHandler::UpdateState::uploadInProgress);
 
     EXPECT_TRUE(handler->canHandleBlob(activeHashBlobId));
 }
diff --git a/test/firmware_state_uploadinprogress_unittest.cpp b/test/firmware_state_uploadinprogress_unittest.cpp
index 237fc44..085f542 100644
--- a/test/firmware_state_uploadinprogress_unittest.cpp
+++ b/test/firmware_state_uploadinprogress_unittest.cpp
@@ -44,11 +44,9 @@
   protected:
     void openToInProgress(const std::string& blobId)
     {
-        auto realHandler = dynamic_cast<FirmwareBlobHandler*>(handler.get());
         EXPECT_CALL(imageMock, open(blobId)).WillOnce(Return(true));
         EXPECT_TRUE(handler->open(session, flags, blobId));
-        EXPECT_EQ(FirmwareBlobHandler::UpdateState::uploadInProgress,
-                  realHandler->getCurrentState());
+        expectedState(FirmwareBlobHandler::UpdateState::uploadInProgress);
     }
 
     std::uint16_t session = 1;
@@ -191,19 +189,11 @@
 TEST_F(FirmwareHandlerUploadInProgressTest,
        ClosingImageFileTransitionsToVerificationPending)
 {
-    auto realHandler = dynamic_cast<FirmwareBlobHandler*>(handler.get());
-
     EXPECT_FALSE(handler->canHandleBlob(verifyBlobId));
+    openToInProgress(staticLayoutBlobId);
 
-    EXPECT_CALL(imageMock, open(staticLayoutBlobId)).WillOnce(Return(true));
-
-    EXPECT_TRUE(handler->open(session, flags, staticLayoutBlobId));
-    EXPECT_EQ(FirmwareBlobHandler::UpdateState::uploadInProgress,
-              realHandler->getCurrentState());
-
-    handler->close(1);
-    EXPECT_EQ(FirmwareBlobHandler::UpdateState::verificationPending,
-              realHandler->getCurrentState());
+    handler->close(session);
+    expectedState(FirmwareBlobHandler::UpdateState::verificationPending);
 
     EXPECT_TRUE(handler->canHandleBlob(verifyBlobId));
 }
@@ -211,19 +201,11 @@
 TEST_F(FirmwareHandlerUploadInProgressTest,
        ClosingHashFileTransitionsToVerificationPending)
 {
-    auto realHandler = dynamic_cast<FirmwareBlobHandler*>(handler.get());
-
     EXPECT_FALSE(handler->canHandleBlob(verifyBlobId));
+    openToInProgress(hashBlobId);
 
-    EXPECT_CALL(imageMock, open(hashBlobId)).WillOnce(Return(true));
-
-    EXPECT_TRUE(handler->open(session, flags, hashBlobId));
-    EXPECT_EQ(FirmwareBlobHandler::UpdateState::uploadInProgress,
-              realHandler->getCurrentState());
-
-    handler->close(1);
-    EXPECT_EQ(FirmwareBlobHandler::UpdateState::verificationPending,
-              realHandler->getCurrentState());
+    handler->close(session);
+    expectedState(FirmwareBlobHandler::UpdateState::verificationPending);
 
     EXPECT_TRUE(handler->canHandleBlob(verifyBlobId));
 }
diff --git a/test/firmware_state_verificationcompleted_unittest.cpp b/test/firmware_state_verificationcompleted_unittest.cpp
index f06193e..d514dd0 100644
--- a/test/firmware_state_verificationcompleted_unittest.cpp
+++ b/test/firmware_state_verificationcompleted_unittest.cpp
@@ -49,30 +49,26 @@
         /* The hash was not sent up, as it's technically optional.  Therefore,
          * there is no active hash file.
          */
-        auto realHandler = dynamic_cast<FirmwareBlobHandler*>(handler.get());
         EXPECT_CALL(imageMock, open(staticLayoutBlobId)).WillOnce(Return(true));
         EXPECT_TRUE(handler->open(session, flags, staticLayoutBlobId));
-        EXPECT_EQ(FirmwareBlobHandler::UpdateState::uploadInProgress,
-                  realHandler->getCurrentState());
+        expectedState(FirmwareBlobHandler::UpdateState::uploadInProgress);
+
         EXPECT_CALL(imageMock, close()).WillRepeatedly(Return());
         handler->close(session);
-        EXPECT_EQ(FirmwareBlobHandler::UpdateState::verificationPending,
-                  realHandler->getCurrentState());
+        expectedState(FirmwareBlobHandler::UpdateState::verificationPending);
 
         EXPECT_TRUE(handler->open(session, flags, verifyBlobId));
         EXPECT_CALL(*verifyMockPtr, triggerVerification())
             .WillOnce(Return(true));
 
         EXPECT_TRUE(handler->commit(session, {}));
-        EXPECT_EQ(FirmwareBlobHandler::UpdateState::verificationStarted,
-                  realHandler->getCurrentState());
+        expectedState(FirmwareBlobHandler::UpdateState::verificationStarted);
 
         EXPECT_CALL(*verifyMockPtr, checkVerificationState())
             .WillOnce(Return(checkResponse));
         blobs::BlobMeta meta;
         EXPECT_TRUE(handler->stat(session, &meta));
-        EXPECT_EQ(FirmwareBlobHandler::UpdateState::verificationCompleted,
-                  realHandler->getCurrentState());
+        expectedState(FirmwareBlobHandler::UpdateState::verificationCompleted);
     }
 
     std::uint16_t session = 1;
diff --git a/test/firmware_state_verificationpending_unittest.cpp b/test/firmware_state_verificationpending_unittest.cpp
index 92b6a2a..c42d83e 100644
--- a/test/firmware_state_verificationpending_unittest.cpp
+++ b/test/firmware_state_verificationpending_unittest.cpp
@@ -47,15 +47,13 @@
   protected:
     void getToVerificationPending(const std::string& blobId)
     {
-        auto realHandler = dynamic_cast<FirmwareBlobHandler*>(handler.get());
         EXPECT_CALL(imageMock, open(blobId)).WillOnce(Return(true));
         EXPECT_TRUE(handler->open(session, flags, blobId));
-        EXPECT_EQ(FirmwareBlobHandler::UpdateState::uploadInProgress,
-                  realHandler->getCurrentState());
+        expectedState(FirmwareBlobHandler::UpdateState::uploadInProgress);
+
         EXPECT_CALL(imageMock, close()).WillRepeatedly(Return());
         handler->close(session);
-        EXPECT_EQ(FirmwareBlobHandler::UpdateState::verificationPending,
-                  realHandler->getCurrentState());
+        expectedState(FirmwareBlobHandler::UpdateState::verificationPending);
     }
 
     std::uint16_t session = 1;
@@ -169,13 +167,11 @@
 
     EXPECT_CALL(imageMock, open(staticLayoutBlobId)).WillOnce(Return(true));
     EXPECT_TRUE(handler->open(session, flags, staticLayoutBlobId));
-
-    auto realHandler = dynamic_cast<FirmwareBlobHandler*>(handler.get());
-    EXPECT_EQ(FirmwareBlobHandler::UpdateState::uploadInProgress,
-              realHandler->getCurrentState());
+    expectedState(FirmwareBlobHandler::UpdateState::uploadInProgress);
 
     expectedBlobs.erase(
-        std::remove(expectedBlobs.begin(), expectedBlobs.end(), verifyBlobId));
+        std::remove(expectedBlobs.begin(), expectedBlobs.end(), verifyBlobId),
+        expectedBlobs.end());
 
     /* Verify the active blob ID was not added to the list twice and
      * verifyBlobId is removed
@@ -188,19 +184,17 @@
  * close(session)
  */
 TEST_F(FirmwareHandlerVerificationPendingTest,
-       ClosingVerifyBlobDoesNotChangeState)
+       ClosingVerifyBlobWithoutCommitDoesNotChangeState)
 {
+    /* commit() will change the state, closing post-commit is part of
+     * verificationStarted testing.
+     */
     getToVerificationPending(staticLayoutBlobId);
     EXPECT_TRUE(handler->open(session, flags, verifyBlobId));
-
-    auto realHandler = dynamic_cast<FirmwareBlobHandler*>(handler.get());
-    EXPECT_EQ(FirmwareBlobHandler::UpdateState::verificationPending,
-              realHandler->getCurrentState());
+    expectedState(FirmwareBlobHandler::UpdateState::verificationPending);
 
     handler->close(session);
-
-    EXPECT_EQ(FirmwareBlobHandler::UpdateState::verificationPending,
-              realHandler->getCurrentState());
+    expectedState(FirmwareBlobHandler::UpdateState::verificationPending);
 }
 
 /*
@@ -214,10 +208,7 @@
     EXPECT_CALL(*verifyMockPtr, triggerVerification()).WillOnce(Return(true));
 
     EXPECT_TRUE(handler->commit(session, {}));
-
-    auto realHandler = dynamic_cast<FirmwareBlobHandler*>(handler.get());
-    EXPECT_EQ(FirmwareBlobHandler::UpdateState::verificationStarted,
-              realHandler->getCurrentState());
+    expectedState(FirmwareBlobHandler::UpdateState::verificationStarted);
 }
 
 /*
diff --git a/test/firmware_state_verificationstarted_unittest.cpp b/test/firmware_state_verificationstarted_unittest.cpp
index b09e9ce..e5f57e5 100644
--- a/test/firmware_state_verificationstarted_unittest.cpp
+++ b/test/firmware_state_verificationstarted_unittest.cpp
@@ -44,23 +44,20 @@
   protected:
     void getToVerificationStarted(const std::string& blobId)
     {
-        auto realHandler = dynamic_cast<FirmwareBlobHandler*>(handler.get());
         EXPECT_CALL(imageMock, open(blobId)).WillOnce(Return(true));
         EXPECT_TRUE(handler->open(session, flags, blobId));
-        EXPECT_EQ(FirmwareBlobHandler::UpdateState::uploadInProgress,
-                  realHandler->getCurrentState());
+        expectedState(FirmwareBlobHandler::UpdateState::uploadInProgress);
+
         EXPECT_CALL(imageMock, close()).WillRepeatedly(Return());
         handler->close(session);
-        EXPECT_EQ(FirmwareBlobHandler::UpdateState::verificationPending,
-                  realHandler->getCurrentState());
+        expectedState(FirmwareBlobHandler::UpdateState::verificationPending);
 
         EXPECT_TRUE(handler->open(session, flags, verifyBlobId));
         EXPECT_CALL(*verifyMockPtr, triggerVerification())
             .WillOnce(Return(true));
 
         EXPECT_TRUE(handler->commit(session, {}));
-        EXPECT_EQ(FirmwareBlobHandler::UpdateState::verificationStarted,
-                  realHandler->getCurrentState());
+        expectedState(FirmwareBlobHandler::UpdateState::verificationStarted);
     }
 
     std::uint16_t session = 1;
@@ -104,12 +101,9 @@
     expectedMeta.metadata.push_back(
         static_cast<std::uint8_t>(VerifyCheckResponses::failed));
 
-    auto realHandler = dynamic_cast<FirmwareBlobHandler*>(handler.get());
     EXPECT_TRUE(handler->stat(session, &meta));
     EXPECT_EQ(expectedMeta, meta);
-
-    EXPECT_EQ(FirmwareBlobHandler::UpdateState::verificationCompleted,
-              realHandler->getCurrentState());
+    expectedState(FirmwareBlobHandler::UpdateState::verificationCompleted);
 }
 
 TEST_F(FirmwareHandlerVerificationStartedTest,
@@ -128,12 +122,9 @@
     expectedMeta.metadata.push_back(
         static_cast<std::uint8_t>(VerifyCheckResponses::success));
 
-    auto realHandler = dynamic_cast<FirmwareBlobHandler*>(handler.get());
     EXPECT_TRUE(handler->stat(session, &meta));
     EXPECT_EQ(expectedMeta, meta);
-
-    EXPECT_EQ(FirmwareBlobHandler::UpdateState::verificationCompleted,
-              realHandler->getCurrentState());
+    expectedState(FirmwareBlobHandler::UpdateState::verificationCompleted);
 }
 
 /* TODO: Once verificationCompleted is the state, canHandleBlob should accept
@@ -253,9 +244,7 @@
 {
     getToVerificationStarted(staticLayoutBlobId);
     EXPECT_TRUE(handler->commit(session, {}));
-    auto realHandler = dynamic_cast<FirmwareBlobHandler*>(handler.get());
-    EXPECT_EQ(FirmwareBlobHandler::UpdateState::verificationStarted,
-              realHandler->getCurrentState());
+    expectedState(FirmwareBlobHandler::UpdateState::verificationStarted);
 }
 
 /*
diff --git a/test/firmware_unittest.hpp b/test/firmware_unittest.hpp
index d2eaa2c..2427b82 100644
--- a/test/firmware_unittest.hpp
+++ b/test/firmware_unittest.hpp
@@ -33,6 +33,12 @@
             blobs, data, std::move(verifyMock), CreateUpdateMock());
     }
 
+    void expectedState(FirmwareBlobHandler::UpdateState state)
+    {
+        auto realHandler = dynamic_cast<FirmwareBlobHandler*>(handler.get());
+        EXPECT_EQ(state, realHandler->getCurrentState());
+    }
+
     ImageHandlerMock imageMock;
     std::vector<HandlerPack> blobs;
     std::vector<DataHandlerPack> data = {