test: cleanup: firmware::open() tests

Signed-off-by: Patrick Venture <venture@google.com>
Change-Id: I432cb185ce07c93e34f366bb67c857f589a11be6
diff --git a/test/firmware_open_unittest.cpp b/test/firmware_open_unittest.cpp
index 5148a44..13f802b 100644
--- a/test/firmware_open_unittest.cpp
+++ b/test/firmware_open_unittest.cpp
@@ -4,6 +4,7 @@
 #include "util.hpp"
 #include "verification_mock.hpp"
 
+#include <memory>
 #include <vector>
 
 #include <gmock/gmock.h>
@@ -15,24 +16,57 @@
 using ::testing::Return;
 using ::testing::StrEq;
 
-TEST(FirmwareHandlerOpenTest, OpenWithEverythingValid)
+class FirmwareHandlerOpenTestIpmiOnly : public ::testing::Test
+{
+  protected:
+    ImageHandlerMock imageMock;
+    std::vector<HandlerPack> blobs;
+    std::vector<DataHandlerPack> data;
+    std::unique_ptr<GenericBlobInterface> handler;
+
+    void SetUp() override
+    {
+        blobs = {
+            {hashBlobId, &imageMock},
+            {"asdf", &imageMock},
+        };
+        data = {
+            {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
+        };
+        handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
+            blobs, data, CreateVerifyMock());
+    }
+};
+
+class FirmwareHandlerOpenTestLpc : public ::testing::Test
+{
+  protected:
+    DataHandlerMock dataMock;
+    ImageHandlerMock imageMock;
+    std::vector<HandlerPack> blobs;
+    std::vector<DataHandlerPack> data;
+    std::unique_ptr<GenericBlobInterface> handler;
+
+    void SetUp() override
+    {
+        blobs = {
+            {hashBlobId, &imageMock},
+            {"asdf", &imageMock},
+        };
+        data = {
+            {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
+            {FirmwareBlobHandler::UpdateFlags::lpc, &dataMock},
+        };
+
+        handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
+            blobs, data, CreateVerifyMock());
+    }
+};
+
+TEST_F(FirmwareHandlerOpenTestIpmiOnly, OpenWithEverythingValid)
 {
     /* The client passes write set, and a transport that's supported, and a
      * firmware image blob_id that's supported. */
-
-    ImageHandlerMock imageMock;
-
-    std::vector<HandlerPack> blobs = {
-        {hashBlobId, &imageMock},
-        {"asdf", &imageMock},
-    };
-    std::vector<DataHandlerPack> data = {
-        {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
-    };
-
-    auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
-        blobs, data, CreateVerifyMock());
-
     EXPECT_CALL(imageMock, open("asdf")).WillOnce(Return(true));
 
     EXPECT_TRUE(handler->open(
@@ -45,22 +79,9 @@
                             activeImageBlobId));
 }
 
-TEST(FirmwareHandlerOpenTest, OpenWithEverythingValidHashFile)
+TEST_F(FirmwareHandlerOpenTestIpmiOnly, OpenWithEverythingValidHashFile)
 {
     /* Open the hash file by blob_id. */
-    ImageHandlerMock imageMock;
-
-    std::vector<HandlerPack> blobs = {
-        {hashBlobId, &imageMock},
-        {"asdf", &imageMock},
-    };
-    std::vector<DataHandlerPack> data = {
-        {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
-    };
-
-    auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
-        blobs, data, CreateVerifyMock());
-
     EXPECT_CALL(imageMock, open(StrEq(hashBlobId))).WillOnce(Return(true));
 
     EXPECT_TRUE(handler->open(
@@ -74,26 +95,11 @@
                             activeHashBlobId));
 }
 
-TEST(FirmwareHandlerOpenTest, OpenWithDataHandlerAllSucceeds)
+TEST_F(FirmwareHandlerOpenTestLpc, OpenWithDataHandlerAllSucceeds)
 {
     /* Attempting to open a file that has an active handler, and use that active
      * handler method.
      */
-    DataHandlerMock dataMock;
-    ImageHandlerMock imageMock;
-
-    std::vector<HandlerPack> blobs = {
-        {hashBlobId, &imageMock},
-        {"asdf", &imageMock},
-    };
-    std::vector<DataHandlerPack> data = {
-        {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
-        {FirmwareBlobHandler::UpdateFlags::lpc, &dataMock},
-    };
-
-    auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
-        blobs, data, CreateVerifyMock());
-
     EXPECT_CALL(dataMock, open()).WillOnce(Return(true));
     EXPECT_CALL(imageMock, open(StrEq(hashBlobId))).WillOnce(Return(true));
 
@@ -108,24 +114,9 @@
                             activeHashBlobId));
 }
 
-TEST(FirmwareHandlerOpenTest, OpenWithDataHandlerReturnsFailure)
+TEST_F(FirmwareHandlerOpenTestLpc, OpenWithDataHandlerReturnsFailure)
 {
     /* The data handler call returns failure on open, therefore open fails. */
-    DataHandlerMock dataMock;
-    ImageHandlerMock imageMock;
-
-    std::vector<HandlerPack> blobs = {
-        {hashBlobId, &imageMock},
-        {"asdf", &imageMock},
-    };
-    std::vector<DataHandlerPack> data = {
-        {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
-        {FirmwareBlobHandler::UpdateFlags::lpc, &dataMock},
-    };
-
-    auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
-        blobs, data, CreateVerifyMock());
-
     EXPECT_CALL(dataMock, open()).WillOnce(Return(false));
 
     EXPECT_FALSE(handler->open(
@@ -137,25 +128,13 @@
     EXPECT_EQ(3, currentBlobs.size());
 }
 
-TEST(FirmwareHandlerOpenTest, OpenEverythingSucceedsVerifyOpenFileCheck)
+TEST_F(FirmwareHandlerOpenTestIpmiOnly,
+       OpenEverythingSucceedsVerifyOpenFileCheck)
 {
     /* Verify only one file can be open at a time by opening a file, trying
      * again, then closing, and trying again.
      */
-    ImageHandlerMock imageMock1, imageMock2;
-
-    std::vector<HandlerPack> blobs = {
-        {hashBlobId, &imageMock1},
-        {"asdf", &imageMock2},
-    };
-    std::vector<DataHandlerPack> data = {
-        {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
-    };
-
-    auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
-        blobs, data, CreateVerifyMock());
-
-    EXPECT_CALL(imageMock2, open("asdf")).WillOnce(Return(true));
+    EXPECT_CALL(imageMock, open("asdf")).WillOnce(Return(true));
 
     EXPECT_TRUE(handler->open(
         0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi, "asdf"));
@@ -173,33 +152,20 @@
 
     EXPECT_TRUE(handler->close(0));
 
-    EXPECT_CALL(imageMock1, open(StrEq(hashBlobId))).WillOnce(Return(true));
+    EXPECT_CALL(imageMock, open(StrEq(hashBlobId))).WillOnce(Return(true));
 
     EXPECT_TRUE(handler->open(
         1, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi,
         hashBlobId));
 }
 
-TEST(FirmwareHandlerOpenTest, OpenEverythingSucceedsOpenActiveFails)
+TEST_F(FirmwareHandlerOpenTestIpmiOnly, OpenEverythingSucceedsOpenActiveFails)
 {
     /* Attempting to open the active image blob, when it's present will fail.
      *
      * TODO: We'll need another test that closes first because you can only have
      * one file open at a time.
      */
-    ImageHandlerMock imageMock;
-
-    std::vector<HandlerPack> blobs = {
-        {hashBlobId, &imageMock},
-        {"asdf", &imageMock},
-    };
-    std::vector<DataHandlerPack> data = {
-        {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
-    };
-
-    auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
-        blobs, data, CreateVerifyMock());
-
     EXPECT_CALL(imageMock, open("asdf")).WillOnce(Return(true));
 
     EXPECT_TRUE(handler->open(
@@ -221,24 +187,11 @@
         activeImageBlobId));
 }
 
-TEST(FirmwareHandlerOpenTest, OpenWithEverythingValidImageHandlerFails)
+TEST_F(FirmwareHandlerOpenTestIpmiOnly,
+       OpenWithEverythingValidImageHandlerFails)
 {
     /* The image handler for a specific type of image is allowed to return
      * failure on open.  let's simulate that. */
-
-    ImageHandlerMock imageMock;
-
-    std::vector<HandlerPack> blobs = {
-        {hashBlobId, &imageMock},
-        {"asdf", &imageMock},
-    };
-    std::vector<DataHandlerPack> data = {
-        {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
-    };
-
-    auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
-        blobs, data, CreateVerifyMock());
-
     EXPECT_CALL(imageMock, open("asdf")).WillOnce(Return(false));
 
     EXPECT_FALSE(handler->open(
@@ -249,65 +202,23 @@
     EXPECT_EQ(3, currentBlobs.size());
 }
 
-TEST(FirmwareHandlerOpenTest, OpenWithoutWriteFails)
+TEST_F(FirmwareHandlerOpenTestIpmiOnly, OpenWithoutWriteFails)
 {
     /* The client must set the file write bit. */
-
-    ImageHandlerMock imageMock;
-
-    std::vector<HandlerPack> blobs = {
-        {hashBlobId, &imageMock},
-        {"asdf", &imageMock},
-    };
-    std::vector<DataHandlerPack> data = {
-        {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
-    };
-
-    auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
-        blobs, data, CreateVerifyMock());
-
     EXPECT_FALSE(
         handler->open(0, FirmwareBlobHandler::UpdateFlags::ipmi, "asdf"));
 }
 
-TEST(FirmwareHandlerOpenTest, OpenWithInvalidTransportBit)
+TEST_F(FirmwareHandlerOpenTestIpmiOnly, OpenWithInvalidTransportBit)
 {
     /* The client sends a request with a transport mechanism not supported. */
-
-    ImageHandlerMock imageMock;
-
-    std::vector<HandlerPack> blobs = {
-        {hashBlobId, &imageMock},
-        {"asdf", &imageMock},
-    };
-    std::vector<DataHandlerPack> data = {
-        {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
-    };
-
-    auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
-        blobs, data, CreateVerifyMock());
-
     EXPECT_FALSE(
         handler->open(0, FirmwareBlobHandler::UpdateFlags::lpc, "asdf"));
 }
 
-TEST(FirmwareHandlerOpenTest, OpenWithInvalidImageBlobId)
+TEST_F(FirmwareHandlerOpenTestIpmiOnly, OpenWithInvalidImageBlobId)
 {
     /* The client sends a request with an invalid image blob_id. */
-
-    ImageHandlerMock imageMock;
-
-    std::vector<HandlerPack> blobs = {
-        {hashBlobId, &imageMock},
-        {"asdf", &imageMock},
-    };
-    std::vector<DataHandlerPack> data = {
-        {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
-    };
-
-    auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
-        blobs, data, CreateVerifyMock());
-
     EXPECT_FALSE(handler->open(
         0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi, "bcdf"));
 }