blob: a9f440e06456db4e341fae48efb4f8af115ce7da [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 III9936c452020-12-23 23:31:23 -08006#include <string_view>
Jason Lingc78bfc82020-11-05 18:58:16 -08007#include <vector>
8
9#include <gtest/gtest.h>
William A. Kennington III9936c452020-12-23 23:31:23 -080010
Jason Lingc78bfc82020-11-05 18:58:16 -080011using ::testing::_;
William A. Kennington III9936c452020-12-23 23:31:23 -080012using ::testing::DoAll;
13using ::testing::ElementsAreArray;
14using ::testing::Ge;
Jason Lingc78bfc82020-11-05 18:58:16 -080015using ::testing::IsEmpty;
16using ::testing::Return;
William A. Kennington III9936c452020-12-23 23:31:23 -080017
Jason Lingc78bfc82020-11-05 18:58:16 -080018namespace ipmi_flash
19{
20
21class VersionReadBlobTest : public ::testing::Test
22{
23 protected:
24 void SetUp() override
25 {
William A. Kennington IIIabf17352020-12-22 21:07:11 -080026 h = std::make_unique<VersionBlobHandler>(
27 createMockVersionConfigs(blobNames, &im, &tm));
Jason Lingc78bfc82020-11-05 18:58:16 -080028 }
29 std::unique_ptr<blobs::GenericBlobInterface> h;
30 std::vector<std::string> blobNames{"blob0", "blob1", "blob2", "blob3"};
31 std::unordered_map<std::string, TriggerMock*> tm;
32 std::unordered_map<std::string, ImageHandlerMock*> im;
33 const std::uint16_t defaultSessionNumber{200};
34 std::vector<uint8_t> vector1{0xDE, 0xAD, 0xBE, 0xEF,
35 0xBA, 0xDF, 0xEE, 0x0D};
William A. Kennington III9936c452020-12-23 23:31:23 -080036 std::vector<uint8_t> vector2{0xCE, 0xAD, 0xDE, 0xFF};
Jason Lingc78bfc82020-11-05 18:58:16 -080037};
38
39TEST_F(VersionReadBlobTest, VerifyValidRead)
40{
William A. Kennington III9936c452020-12-23 23:31:23 -080041 testing::InSequence seq;
42 EXPECT_CALL(*tm.at("blob0"), trigger())
43 .WillOnce(DoAll([&]() { tm.at("blob0")->cb(*tm.at("blob0")); },
44 Return(true)));
Jason Lingc78bfc82020-11-05 18:58:16 -080045 EXPECT_CALL(*tm.at("blob0"), status())
William A. Kennington III9936c452020-12-23 23:31:23 -080046 .WillOnce(Return(ActionStatus::success));
47 EXPECT_CALL(*im.at("blob0"), open(_, std::ios::in)).WillOnce(Return(true));
48 EXPECT_CALL(*im.at("blob0"), read(0, Ge(vector1.size())))
49 .WillOnce(Return(vector1));
50 EXPECT_CALL(*im.at("blob0"), close()).Times(1);
Jason Lingc78bfc82020-11-05 18:58:16 -080051 EXPECT_TRUE(h->open(defaultSessionNumber, blobs::read, "blob0"));
Jason Lingc78bfc82020-11-05 18:58:16 -080052
William A. Kennington III9936c452020-12-23 23:31:23 -080053 std::basic_string_view<uint8_t> vectorS(vector1.data(), vector1.size());
54 EXPECT_THAT(h->read(defaultSessionNumber, 0, 7),
55 ElementsAreArray(vectorS.substr(0, 7)));
56 EXPECT_THAT(h->read(defaultSessionNumber, 2, 10),
57 ElementsAreArray(vectorS.substr(2, 6)));
58 EXPECT_THAT(h->read(defaultSessionNumber, 10, 0), IsEmpty());
59}
60
61TEST_F(VersionReadBlobTest, VerifyMultipleSession)
62{
63 testing::InSequence seq;
64 EXPECT_CALL(*tm.at("blob0"), trigger()).WillOnce(Return(true));
65 EXPECT_TRUE(h->open(0, blobs::read, "blob0"));
66 EXPECT_TRUE(h->open(1, blobs::read, "blob0"));
67
68 EXPECT_CALL(*tm.at("blob0"), status())
69 .WillOnce(Return(ActionStatus::success));
70 EXPECT_CALL(*im.at("blob0"), open(_, std::ios::in)).WillOnce(Return(true));
71 EXPECT_CALL(*im.at("blob0"), read(0, Ge(vector1.size())))
72 .WillOnce(Return(vector1));
73 EXPECT_CALL(*im.at("blob0"), close()).Times(1);
74 tm.at("blob0")->cb(*tm.at("blob0"));
75
76 EXPECT_CALL(*tm.at("blob0"), trigger()).WillOnce(Return(true));
77 EXPECT_TRUE(h->open(2, blobs::read, "blob0"));
78
79 EXPECT_CALL(*tm.at("blob0"), status())
80 .WillOnce(Return(ActionStatus::success));
81 EXPECT_CALL(*im.at("blob0"), open(_, std::ios::in)).WillOnce(Return(true));
82 EXPECT_CALL(*im.at("blob0"), read(0, Ge(vector2.size())))
83 .WillOnce(Return(vector2));
84 EXPECT_CALL(*im.at("blob0"), close()).Times(1);
85 tm.at("blob0")->cb(*tm.at("blob0"));
86
87 EXPECT_THAT(h->read(0, 0, 10), ElementsAreArray(vector1));
88 EXPECT_THAT(h->read(1, 0, 10), ElementsAreArray(vector1));
89 EXPECT_THAT(h->read(2, 0, 10), ElementsAreArray(vector2));
90}
91
92TEST_F(VersionReadBlobTest, VerifyReadEarlyFails)
93{
94 EXPECT_CALL(*tm.at("blob0"), trigger()).WillOnce(Return(true));
95
96 EXPECT_TRUE(h->open(defaultSessionNumber, blobs::read, "blob0"));
William A. Kennington III0674a6d2021-01-12 14:25:41 -080097 EXPECT_THROW(h->read(defaultSessionNumber, 0, 10), std::runtime_error);
Jason Lingc78bfc82020-11-05 18:58:16 -080098}
99
Jason Lingc78bfc82020-11-05 18:58:16 -0800100TEST_F(VersionReadBlobTest, VerifyTriggerFailureReadFails)
101{
William A. Kennington III9936c452020-12-23 23:31:23 -0800102 EXPECT_CALL(*tm.at("blob0"), trigger())
103 .WillOnce(DoAll([&]() { tm.at("blob0")->cb(*tm.at("blob0")); },
104 Return(true)));
Jason Lingc78bfc82020-11-05 18:58:16 -0800105 EXPECT_CALL(*tm.at("blob0"), status())
Jason Lingc78bfc82020-11-05 18:58:16 -0800106 .WillOnce(Return(ActionStatus::failed));
Jason Lingc78bfc82020-11-05 18:58:16 -0800107 EXPECT_TRUE(h->open(defaultSessionNumber, blobs::read, "blob0"));
William A. Kennington III0674a6d2021-01-12 14:25:41 -0800108 EXPECT_THROW(h->read(defaultSessionNumber, 0, 10), std::runtime_error);
Jason Lingc78bfc82020-11-05 18:58:16 -0800109}
110
William A. Kennington III9936c452020-12-23 23:31:23 -0800111TEST_F(VersionReadBlobTest, VerifyReadFailsOnFileOpenFailure)
Jason Lingc78bfc82020-11-05 18:58:16 -0800112{
William A. Kennington III9936c452020-12-23 23:31:23 -0800113 EXPECT_CALL(*tm.at("blob0"), trigger())
114 .WillOnce(DoAll([&]() { tm.at("blob0")->cb(*tm.at("blob0")); },
115 Return(true)));
Jason Lingc78bfc82020-11-05 18:58:16 -0800116 EXPECT_CALL(*tm.at("blob0"), status())
Jason Lingc78bfc82020-11-05 18:58:16 -0800117 .WillOnce(Return(ActionStatus::success));
William A. Kennington III9936c452020-12-23 23:31:23 -0800118 EXPECT_CALL(*im.at("blob0"), open(_, std::ios::in)).WillOnce(Return(false));
Jason Lingc78bfc82020-11-05 18:58:16 -0800119
120 EXPECT_TRUE(h->open(defaultSessionNumber, blobs::read, "blob0"));
William A. Kennington III0674a6d2021-01-12 14:25:41 -0800121 EXPECT_THROW(h->read(defaultSessionNumber, 0, 10), std::runtime_error);
Jason Lingc78bfc82020-11-05 18:58:16 -0800122}
123
William A. Kennington III9936c452020-12-23 23:31:23 -0800124TEST_F(VersionReadBlobTest, VerifyReadFailsOnFileReadFailure)
Jason Lingc78bfc82020-11-05 18:58:16 -0800125{
William A. Kennington III9936c452020-12-23 23:31:23 -0800126 EXPECT_CALL(*tm.at("blob0"), trigger())
127 .WillOnce(DoAll([&]() { tm.at("blob0")->cb(*tm.at("blob0")); },
128 Return(true)));
Jason Lingc78bfc82020-11-05 18:58:16 -0800129 EXPECT_CALL(*tm.at("blob0"), status())
Jason Lingc78bfc82020-11-05 18:58:16 -0800130 .WillOnce(Return(ActionStatus::success));
William A. Kennington III9936c452020-12-23 23:31:23 -0800131 EXPECT_CALL(*im.at("blob0"), open(_, std::ios::in)).WillOnce(Return(true));
132 EXPECT_CALL(*im.at("blob0"), read(_, _)).WillOnce(Return(std::nullopt));
133 EXPECT_CALL(*im.at("blob0"), close()).Times(1);
Jason Lingc78bfc82020-11-05 18:58:16 -0800134
135 EXPECT_TRUE(h->open(defaultSessionNumber, blobs::read, "blob0"));
William A. Kennington III0674a6d2021-01-12 14:25:41 -0800136 EXPECT_THROW(h->read(defaultSessionNumber, 0, 10), std::runtime_error);
Jason Lingc78bfc82020-11-05 18:58:16 -0800137}
138
139} // namespace ipmi_flash