blob: 033b2f455497979ec75d985e3d58ff0d7807caa2 [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::_;
12using ::testing::Return;
William A. Kennington IIIabf17352020-12-22 21:07:11 -080013
Jason Lingc78bfc82020-11-05 18:58:16 -080014namespace ipmi_flash
15{
16
17class VersionOpenBlobTest : public ::testing::Test
18{
19 protected:
20 void SetUp() override
21 {
William A. Kennington IIIabf17352020-12-22 21:07:11 -080022 h = std::make_unique<VersionBlobHandler>(
23 createMockVersionConfigs(blobNames, &im, &tm));
Jason Lingc78bfc82020-11-05 18:58:16 -080024 }
William A. Kennington IIIabf17352020-12-22 21:07:11 -080025
Jason Lingc78bfc82020-11-05 18:58:16 -080026 std::unique_ptr<blobs::GenericBlobInterface> h;
27 std::vector<std::string> blobNames{"blob0", "blob1", "blob2", "blob3"};
28 std::unordered_map<std::string, TriggerMock*> tm;
29 std::unordered_map<std::string, ImageHandlerMock*> im;
30 const std::uint16_t defaultSessionNumber{0};
31};
32
33TEST_F(VersionOpenBlobTest, VerifySingleBlobOpen)
34{
35 EXPECT_CALL(*tm.at("blob0"), trigger()).Times(1).WillOnce(Return(true));
36 EXPECT_TRUE(h->open(defaultSessionNumber, blobs::read, "blob0"));
37}
38
39TEST_F(VersionOpenBlobTest, VerifyMultipleBlobOpens)
40{
William A. Kennington III007c0162020-12-23 16:36:22 -080041 for (const auto& [_, val] : tm)
Jason Lingc78bfc82020-11-05 18:58:16 -080042 {
43 /* set the expectation that every onOpen will be triggered */
William A. Kennington III007c0162020-12-23 16:36:22 -080044 EXPECT_CALL(*val, trigger()).WillOnce(Return(true));
Jason Lingc78bfc82020-11-05 18:58:16 -080045 }
46 int i{defaultSessionNumber};
47 for (const auto& blob : blobNames)
48 {
49 EXPECT_TRUE(h->open(i++, blobs::read, blob));
50 }
51}
52
53TEST_F(VersionOpenBlobTest, VerifyOpenAfterClose)
54{
William A. Kennington III007c0162020-12-23 16:36:22 -080055 EXPECT_CALL(*tm.at("blob0"), trigger()).WillOnce(Return(true));
Jason Lingc78bfc82020-11-05 18:58:16 -080056 EXPECT_TRUE(h->open(defaultSessionNumber, blobs::read, "blob0"));
William A. Kennington III007c0162020-12-23 16:36:22 -080057
58 EXPECT_CALL(*tm.at("blob0"), abort()).Times(1);
Jason Lingc78bfc82020-11-05 18:58:16 -080059 EXPECT_TRUE(h->close(defaultSessionNumber));
William A. Kennington III007c0162020-12-23 16:36:22 -080060
61 EXPECT_CALL(*tm.at("blob0"), trigger()).WillOnce(Return(true));
Jason Lingc78bfc82020-11-05 18:58:16 -080062 EXPECT_TRUE(h->open(defaultSessionNumber, blobs::read, "blob0"));
63}
64
Jason Lingc78bfc82020-11-05 18:58:16 -080065TEST_F(VersionOpenBlobTest, VerifyDoubleOpenFails)
66{
William A. Kennington III007c0162020-12-23 16:36:22 -080067 EXPECT_CALL(*tm.at("blob1"), trigger()).WillOnce(Return(true));
Jason Lingc78bfc82020-11-05 18:58:16 -080068 EXPECT_TRUE(h->open(0, blobs::read, "blob1"));
69 EXPECT_FALSE(h->open(2, blobs::read, "blob1"));
William A. Kennington III007c0162020-12-23 16:36:22 -080070 EXPECT_FALSE(h->open(2, blobs::read, "blob1"));
Jason Lingc78bfc82020-11-05 18:58:16 -080071}
72
73TEST_F(VersionOpenBlobTest, VerifyFailedTriggerFails)
74{
William A. Kennington III007c0162020-12-23 16:36:22 -080075 EXPECT_CALL(*tm.at("blob1"), trigger()).WillOnce(Return(false));
Jason Lingc78bfc82020-11-05 18:58:16 -080076 EXPECT_FALSE(h->open(0, blobs::read, "blob1"));
William A. Kennington III007c0162020-12-23 16:36:22 -080077 EXPECT_CALL(*tm.at("blob1"), trigger()).WillOnce(Return(true));
Jason Lingc78bfc82020-11-05 18:58:16 -080078 EXPECT_TRUE(h->open(0, blobs::read, "blob1"));
79}
80
81TEST_F(VersionOpenBlobTest, VerifyUnsupportedOpenFlagsFails)
82{
Jason Lingc78bfc82020-11-05 18:58:16 -080083 EXPECT_FALSE(h->open(0, blobs::write, "blob1"));
William A. Kennington III007c0162020-12-23 16:36:22 -080084 EXPECT_CALL(*tm.at("blob1"), trigger()).WillOnce(Return(true));
Jason Lingc78bfc82020-11-05 18:58:16 -080085 EXPECT_TRUE(h->open(0, blobs::read, "blob1"));
86}
87
88} // namespace ipmi_flash