test: rework ipmi validate test as table
The tests are now done as a table with inputs and expected outputs
instead of an individual test method for each case. The trade-off is
that if the table fails it may not be immediately obvious what failed.
Change-Id: I18d43c89674fe7b99c04eb1e8bee513fa37478f4
Signed-off-by: Patrick Venture <venture@google.com>
diff --git a/test/ipmi_validate_unittest.cpp b/test/ipmi_validate_unittest.cpp
index 14ada91..f733c5e 100644
--- a/test/ipmi_validate_unittest.cpp
+++ b/test/ipmi_validate_unittest.cpp
@@ -2,54 +2,27 @@
#include <gtest/gtest.h>
-// ipmid.hpp isn't installed where we can grab it and this value is per BMC
-// SoC.
-#define MAX_IPMI_BUFFER 64
-
-TEST(IpmiValidateTest, BoringValidateCheckReturnsTrue)
+TEST(IpmiValidateTest, VerifyCommandMinimumLengths)
{
- // Verify the validate check can return true when it's happy.
- auto cmd = FlashSubCmds::flashStartTransfer;
- size_t dataLen = sizeof(struct StartTx);
- EXPECT_TRUE(validateRequestLength(cmd, dataLen));
-}
+ struct TestCases
+ {
+ FlashSubCmds cmd;
+ size_t len;
+ bool expect;
+ };
-TEST(IpmiValidateTest, InvalidLengthStartTransferReturnsFalse)
-{
- // Verify that the request is sanity checked w.r.t length.
- auto cmd = FlashSubCmds::flashStartTransfer;
- size_t dataLen = sizeof(struct StartTx) - 1;
- EXPECT_FALSE(validateRequestLength(cmd, dataLen));
-}
+ std::vector<TestCases> tests = {
+ {FlashSubCmds::flashStartTransfer, sizeof(struct StartTx), true},
+ {FlashSubCmds::flashStartTransfer, sizeof(struct StartTx) - 1, false},
+ {FlashSubCmds::flashDataBlock, sizeof(struct ChunkHdr) - 1, false},
+ {FlashSubCmds::flashDataBlock, sizeof(struct ChunkHdr), false},
+ {FlashSubCmds::flashStartHash, sizeof(struct StartTx) - 1, false},
+ {FlashSubCmds::flashHashData, sizeof(struct ChunkHdr) - 1, false},
+ };
-TEST(IpmiValidateTest, InvalidLengthDataBlockReturnsFalse)
-{
- // This request isn't large enough to be well-formed.
- auto cmd = FlashSubCmds::flashDataBlock;
- size_t dataLen = sizeof(struct ChunkHdr) - 1;
- EXPECT_FALSE(validateRequestLength(cmd, dataLen));
-}
-
-TEST(IpmiValidateTest, DataBlockNoDataReturnsFalse)
-{
- // If the request has no data, it's invalid, returns failure.
- auto cmd = FlashSubCmds::flashDataBlock;
- size_t dataLen = sizeof(struct ChunkHdr);
- EXPECT_FALSE(validateRequestLength(cmd, dataLen));
-}
-
-TEST(IpmiValidateTest, StartHashInvalidReturnsFalse)
-{
- // Verify the request is sanity checked w.r.t length.
- auto cmd = FlashSubCmds::flashStartHash;
- size_t dataLen = sizeof(struct StartTx) - 1;
- EXPECT_FALSE(validateRequestLength(cmd, dataLen));
-}
-
-TEST(IpmiValidateTest, InvalidLengthHashBlockReturnsFalse)
-{
- // This request isn't large enough to be well-formed.
- auto cmd = FlashSubCmds::flashHashData;
- size_t dataLen = sizeof(struct ChunkHdr) - 1;
- EXPECT_FALSE(validateRequestLength(cmd, dataLen));
+ for (const auto& test : tests)
+ {
+ bool result = validateRequestLength(test.cmd, test.len);
+ EXPECT_EQ(result, test.expect);
+ }
}