test: use common test object
Many tests only set up an IPMI data handler, or one with LPC in
addition, therefore this code can be more easily managed in one
location.
Signed-off-by: Patrick Venture <venture@google.com>
Change-Id: Ie42a74d59bbd74c077c3cf7794546c71e92d2ef7
diff --git a/test/firmware_close_unittest.cpp b/test/firmware_close_unittest.cpp
index f7f729a..2b40d6d 100644
--- a/test/firmware_close_unittest.cpp
+++ b/test/firmware_close_unittest.cpp
@@ -1,5 +1,6 @@
#include "data_mock.hpp"
#include "firmware_handler.hpp"
+#include "firmware_unittest.hpp"
#include "image_mock.hpp"
#include "util.hpp"
#include "verification_mock.hpp"
@@ -16,28 +17,8 @@
using ::testing::Return;
using ::testing::StrEq;
-class FirmwareHandlerCloseTest : public ::testing::Test
+class FirmwareHandlerCloseTest : public FakeLpcFirmwareTest
{
- protected:
- DataHandlerMock dataMock;
- ImageHandlerMock imageMock;
-
- std::vector<HandlerPack> blobs = {
- {hashBlobId, &imageMock},
- {"asdf", &imageMock},
- };
- std::vector<DataHandlerPack> data = {
- {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
- {FirmwareBlobHandler::UpdateFlags::lpc, &dataMock},
- };
-
- std::unique_ptr<GenericBlobInterface> handler;
-
- void SetUp() override
- {
- handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
- blobs, data, CreateVerifyMock());
- }
};
TEST_F(FirmwareHandlerCloseTest, CloseSucceedsWithDataHandler)
diff --git a/test/firmware_open_unittest.cpp b/test/firmware_open_unittest.cpp
index 13f802b..c8c93c0 100644
--- a/test/firmware_open_unittest.cpp
+++ b/test/firmware_open_unittest.cpp
@@ -1,5 +1,6 @@
#include "data_mock.hpp"
#include "firmware_handler.hpp"
+#include "firmware_unittest.hpp"
#include "image_mock.hpp"
#include "util.hpp"
#include "verification_mock.hpp"
@@ -16,51 +17,12 @@
using ::testing::Return;
using ::testing::StrEq;
-class FirmwareHandlerOpenTestIpmiOnly : public ::testing::Test
+class FirmwareHandlerOpenTestIpmiOnly : public IpmiOnlyFirmwareTest
{
- 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
+class FirmwareHandlerOpenTestLpc : public FakeLpcFirmwareTest
{
- 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)
diff --git a/test/firmware_sessionstat_unittest.cpp b/test/firmware_sessionstat_unittest.cpp
index f8cc727..d3d35f9 100644
--- a/test/firmware_sessionstat_unittest.cpp
+++ b/test/firmware_sessionstat_unittest.cpp
@@ -1,5 +1,6 @@
#include "data_mock.hpp"
#include "firmware_handler.hpp"
+#include "firmware_unittest.hpp"
#include "image_mock.hpp"
#include "util.hpp"
#include "verification_mock.hpp"
@@ -13,40 +14,26 @@
using ::testing::Eq;
using ::testing::Return;
-class FirmwareSessionStateTest : public ::testing::Test
+class FirmwareSessionStateTestIpmiOnly : public IpmiOnlyFirmwareTest
{
- protected:
- ImageHandlerMock imageMock1, imageMock2;
- std::vector<HandlerPack> blobs;
-
- void SetUp() override
- {
- blobs = {
- {hashBlobId, &imageMock1},
- {"asdf", &imageMock2},
- };
- }
};
-TEST_F(FirmwareSessionStateTest, DataTypeIpmiNoMetadata)
+class FirmwareSessionStateTestLpc : public FakeLpcFirmwareTest
+{
+};
+
+TEST_F(FirmwareSessionStateTestIpmiOnly, DataTypeIpmiNoMetadata)
{
/* Verifying running stat if the type of data session is IPMI returns no
* metadata.
*/
- 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"));
int size = 512;
- EXPECT_CALL(imageMock2, getSize()).WillOnce(Return(size));
+ EXPECT_CALL(imageMock, getSize()).WillOnce(Return(size));
struct BlobMeta meta;
EXPECT_TRUE(handler->stat(0, &meta));
@@ -56,30 +43,20 @@
EXPECT_EQ(meta.metadata.size(), 0);
}
-TEST_F(FirmwareSessionStateTest, DataTypeP2AReturnsMetadata)
+TEST_F(FirmwareSessionStateTestLpc, DataTypeP2AReturnsMetadata)
{
/* Really any type that isn't IPMI can return metadata, but we only expect
* P2A to for now. Later, LPC may have reason to provide data, and can by
* simply implementing read().
*/
- DataHandlerMock dataMock;
-
- 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(imageMock2, open("asdf")).WillOnce(Return(true));
+ EXPECT_CALL(imageMock, open("asdf")).WillOnce(Return(true));
EXPECT_TRUE(handler->open(
0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::lpc, "asdf"));
int size = 512;
- EXPECT_CALL(imageMock2, getSize()).WillOnce(Return(size));
+ EXPECT_CALL(imageMock, getSize()).WillOnce(Return(size));
std::vector<std::uint8_t> mBytes = {0x01, 0x02};
EXPECT_CALL(dataMock, readMeta()).WillOnce(Return(mBytes));
diff --git a/test/firmware_unittest.hpp b/test/firmware_unittest.hpp
new file mode 100644
index 0000000..0eea768
--- /dev/null
+++ b/test/firmware_unittest.hpp
@@ -0,0 +1,61 @@
+#pragma once
+
+#include "data_mock.hpp"
+#include "firmware_handler.hpp"
+#include "image_mock.hpp"
+#include "verification_mock.hpp"
+
+#include <memory>
+#include <vector>
+
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+
+namespace blobs
+{
+
+class IpmiOnlyFirmwareTest : public ::testing::Test
+{
+ protected:
+ ImageHandlerMock imageMock;
+ std::vector<HandlerPack> blobs;
+ std::vector<DataHandlerPack> data = {
+ {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr}};
+ std::unique_ptr<GenericBlobInterface> handler;
+
+ void SetUp() override
+ {
+ blobs = {
+ {hashBlobId, &imageMock},
+ {"asdf", &imageMock},
+ };
+ handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
+ blobs, data, CreateVerifyMock());
+ }
+};
+
+class FakeLpcFirmwareTest : 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());
+ }
+};
+
+} // namespace blobs
diff --git a/test/firmware_write_unittest.cpp b/test/firmware_write_unittest.cpp
index 48c3217..77f6326 100644
--- a/test/firmware_write_unittest.cpp
+++ b/test/firmware_write_unittest.cpp
@@ -1,5 +1,6 @@
#include "data_mock.hpp"
#include "firmware_handler.hpp"
+#include "firmware_unittest.hpp"
#include "image_mock.hpp"
#include "util.hpp"
#include "verification_mock.hpp"
@@ -15,57 +16,33 @@
using ::testing::Eq;
using ::testing::Return;
-class FirmwareHandlerWriteTest : public ::testing::Test
+class FirmwareHandlerWriteTestIpmiOnly : public IpmiOnlyFirmwareTest
{
- protected:
- ImageHandlerMock imageMock1, imageMock2;
- std::vector<HandlerPack> blobs;
-
- void SetUp() override
- {
- blobs = {
- {hashBlobId, &imageMock1},
- {"asdf", &imageMock2},
- };
- }
};
-TEST_F(FirmwareHandlerWriteTest, DataTypeIpmiWriteSuccess)
+class FirmwareHandlerWriteTestLpc : public FakeLpcFirmwareTest
+{
+};
+
+TEST_F(FirmwareHandlerWriteTestIpmiOnly, DataTypeIpmiWriteSuccess)
{
/* Verify if data type ipmi, it calls write with the bytes. */
- 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"));
std::vector<std::uint8_t> bytes = {0xaa, 0x55};
- EXPECT_CALL(imageMock2, write(0, Eq(bytes))).WillOnce(Return(true));
+ EXPECT_CALL(imageMock, write(0, Eq(bytes))).WillOnce(Return(true));
EXPECT_TRUE(handler->write(0, 0, bytes));
}
-TEST_F(FirmwareHandlerWriteTest, DataTypeNonIpmiWriteSuccess)
+TEST_F(FirmwareHandlerWriteTestLpc, DataTypeNonIpmiWriteSuccess)
{
/* Verify if data type non-ipmi, it calls write with the length. */
- DataHandlerMock dataMock;
-
- 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(imageMock2, open("asdf")).WillOnce(Return(true));
+ EXPECT_CALL(imageMock, open("asdf")).WillOnce(Return(true));
EXPECT_TRUE(handler->open(
0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::lpc, "asdf"));
@@ -79,26 +56,16 @@
std::vector<std::uint8_t> bytes = {0x01, 0x02, 0x03, 0x04};
EXPECT_CALL(dataMock, copyFrom(request.length)).WillOnce(Return(bytes));
- EXPECT_CALL(imageMock2, write(0, Eq(bytes))).WillOnce(Return(true));
+ EXPECT_CALL(imageMock, write(0, Eq(bytes))).WillOnce(Return(true));
EXPECT_TRUE(handler->write(0, 0, ipmiRequest));
}
-TEST_F(FirmwareHandlerWriteTest, DataTypeNonIpmiWriteFailsBadRequest)
+TEST_F(FirmwareHandlerWriteTestLpc, DataTypeNonIpmiWriteFailsBadRequest)
{
/* Verify the data type non-ipmi, if the request's structure doesn't match,
* return failure. */
- DataHandlerMock dataMock;
-
- 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(imageMock2, open("asdf")).WillOnce(Return(true));
+ EXPECT_CALL(imageMock, open("asdf")).WillOnce(Return(true));
EXPECT_TRUE(handler->open(
0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::lpc, "asdf"));
diff --git a/test/firmware_writemeta_unittest.cpp b/test/firmware_writemeta_unittest.cpp
index 93887d2..0ca5c62 100644
--- a/test/firmware_writemeta_unittest.cpp
+++ b/test/firmware_writemeta_unittest.cpp
@@ -1,5 +1,6 @@
#include "data_mock.hpp"
#include "firmware_handler.hpp"
+#include "firmware_unittest.hpp"
#include "image_mock.hpp"
#include "util.hpp"
#include "verification_mock.hpp"
@@ -14,33 +15,13 @@
using ::testing::Eq;
using ::testing::Return;
-class FirmwareHandlerWriteMetaTest : public ::testing::Test
+class FirmwareHandlerWriteMetaTest : public FakeLpcFirmwareTest
{
- protected:
- DataHandlerMock dataMock;
- ImageHandlerMock imageMock1, imageMock2;
- std::vector<HandlerPack> blobs;
- std::vector<DataHandlerPack> data;
- std::unique_ptr<GenericBlobInterface> handler;
-
- void SetUp() override
- {
- blobs = {
- {hashBlobId, &imageMock1},
- {"asdf", &imageMock2},
- };
- data = {
- {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
- {FirmwareBlobHandler::UpdateFlags::lpc, &dataMock},
- };
- handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
- blobs, data, CreateVerifyMock());
- }
};
TEST_F(FirmwareHandlerWriteMetaTest, WriteConfigParametersFailIfOverIPMI)
{
- 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"));
@@ -53,7 +34,7 @@
TEST_F(FirmwareHandlerWriteMetaTest, WriteConfigParametersPassedThrough)
{
EXPECT_CALL(dataMock, open()).WillOnce(Return(true));
- 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::lpc, "asdf"));