blob: dd83de2eeeddd597801cb42fce1d24622a1c4ded [file] [log] [blame]
Jason Lingc78bfc82020-11-05 18:58:16 -08001#include "version_handler.hpp"
William A. Kennington IIIabf17352020-12-22 21:07:11 -08002#include "version_mock.hpp"
Jason Lingc78bfc82020-11-05 18:58:16 -08003
William A. Kennington IIIabf17352020-12-22 21:07:11 -08004#include <memory>
Jason Lingc78bfc82020-11-05 18:58:16 -08005#include <string>
William A. Kennington IIIabf17352020-12-22 21:07:11 -08006#include <unordered_map>
Jason Lingc78bfc82020-11-05 18:58:16 -08007#include <vector>
8
9#include <gtest/gtest.h>
William A. Kennington IIIabf17352020-12-22 21:07:11 -080010
Jason Lingc78bfc82020-11-05 18:58:16 -080011using ::testing::Return;
William A. Kennington IIIabf17352020-12-22 21:07:11 -080012
Jason Lingc78bfc82020-11-05 18:58:16 -080013namespace ipmi_flash
14{
15
16class VersionOpenBlobTest : public ::testing::Test
17{
18 protected:
19 void SetUp() override
20 {
William A. Kennington IIIabf17352020-12-22 21:07:11 -080021 h = std::make_unique<VersionBlobHandler>(
22 createMockVersionConfigs(blobNames, &im, &tm));
Jason Lingc78bfc82020-11-05 18:58:16 -080023 }
William A. Kennington IIIabf17352020-12-22 21:07:11 -080024
Jason Lingc78bfc82020-11-05 18:58:16 -080025 std::unique_ptr<blobs::GenericBlobInterface> h;
26 std::vector<std::string> blobNames{"blob0", "blob1", "blob2", "blob3"};
27 std::unordered_map<std::string, TriggerMock*> tm;
28 std::unordered_map<std::string, ImageHandlerMock*> im;
29 const std::uint16_t defaultSessionNumber{0};
30};
31
32TEST_F(VersionOpenBlobTest, VerifySingleBlobOpen)
33{
34 EXPECT_CALL(*tm.at("blob0"), trigger()).Times(1).WillOnce(Return(true));
35 EXPECT_TRUE(h->open(defaultSessionNumber, blobs::read, "blob0"));
36}
37
38TEST_F(VersionOpenBlobTest, VerifyMultipleBlobOpens)
39{
William A. Kennington III007c0162020-12-23 16:36:22 -080040 for (const auto& [_, val] : tm)
Jason Lingc78bfc82020-11-05 18:58:16 -080041 {
42 /* set the expectation that every onOpen will be triggered */
William A. Kennington III007c0162020-12-23 16:36:22 -080043 EXPECT_CALL(*val, trigger()).WillOnce(Return(true));
Jason Lingc78bfc82020-11-05 18:58:16 -080044 }
45 int i{defaultSessionNumber};
46 for (const auto& blob : blobNames)
47 {
48 EXPECT_TRUE(h->open(i++, blobs::read, blob));
49 }
50}
51
52TEST_F(VersionOpenBlobTest, VerifyOpenAfterClose)
53{
William A. Kennington III007c0162020-12-23 16:36:22 -080054 EXPECT_CALL(*tm.at("blob0"), trigger()).WillOnce(Return(true));
Jason Lingc78bfc82020-11-05 18:58:16 -080055 EXPECT_TRUE(h->open(defaultSessionNumber, blobs::read, "blob0"));
William A. Kennington III007c0162020-12-23 16:36:22 -080056
57 EXPECT_CALL(*tm.at("blob0"), abort()).Times(1);
Jason Lingc78bfc82020-11-05 18:58:16 -080058 EXPECT_TRUE(h->close(defaultSessionNumber));
William A. Kennington III007c0162020-12-23 16:36:22 -080059
60 EXPECT_CALL(*tm.at("blob0"), trigger()).WillOnce(Return(true));
Jason Lingc78bfc82020-11-05 18:58:16 -080061 EXPECT_TRUE(h->open(defaultSessionNumber, blobs::read, "blob0"));
62}
63
William A. Kennington III9936c452020-12-23 23:31:23 -080064TEST_F(VersionOpenBlobTest, VerifyMultiOpenWorks)
Jason Lingc78bfc82020-11-05 18:58:16 -080065{
William A. Kennington III007c0162020-12-23 16:36:22 -080066 EXPECT_CALL(*tm.at("blob1"), trigger()).WillOnce(Return(true));
Jason Lingc78bfc82020-11-05 18:58:16 -080067 EXPECT_TRUE(h->open(0, blobs::read, "blob1"));
William A. Kennington III9936c452020-12-23 23:31:23 -080068 EXPECT_TRUE(h->open(1, blobs::read, "blob1"));
69 EXPECT_TRUE(h->open(2, blobs::read, "blob1"));
Jason Lingc78bfc82020-11-05 18:58:16 -080070}
71
72TEST_F(VersionOpenBlobTest, VerifyFailedTriggerFails)
73{
William A. Kennington III007c0162020-12-23 16:36:22 -080074 EXPECT_CALL(*tm.at("blob1"), trigger()).WillOnce(Return(false));
Jason Lingc78bfc82020-11-05 18:58:16 -080075 EXPECT_FALSE(h->open(0, blobs::read, "blob1"));
William A. Kennington III007c0162020-12-23 16:36:22 -080076 EXPECT_CALL(*tm.at("blob1"), trigger()).WillOnce(Return(true));
Jason Lingc78bfc82020-11-05 18:58:16 -080077 EXPECT_TRUE(h->open(0, blobs::read, "blob1"));
78}
79
80TEST_F(VersionOpenBlobTest, VerifyUnsupportedOpenFlagsFails)
81{
Jason Lingc78bfc82020-11-05 18:58:16 -080082 EXPECT_FALSE(h->open(0, blobs::write, "blob1"));
William A. Kennington III007c0162020-12-23 16:36:22 -080083 EXPECT_CALL(*tm.at("blob1"), trigger()).WillOnce(Return(true));
Jason Lingc78bfc82020-11-05 18:58:16 -080084 EXPECT_TRUE(h->open(0, blobs::read, "blob1"));
85}
86
87} // namespace ipmi_flash