blob: 95a5abcc1cf0f3b6adbdac097e2ecbb0233bb6e7 [file] [log] [blame]
Patrick Ventureaa32a362018-12-13 10:52:33 -08001#include "data_interface_mock.hpp"
2#include "updater.hpp"
Patrick Venture55646de2019-05-16 10:06:26 -07003#include "updater_mock.hpp"
Patrick Venture7dad86f2019-05-17 08:52:20 -07004#include "util.hpp"
Patrick Ventureaa32a362018-12-13 10:52:33 -08005
Patrick Venture664c5bc2019-03-07 08:09:45 -08006#include <blobs-ipmid/blobs.hpp>
7#include <ipmiblob/test/blob_interface_mock.hpp>
Patrick Ventureaa32a362018-12-13 10:52:33 -08008#include <string>
9
10#include <gtest/gtest.h>
11
Patrick Venture9b534f02018-12-13 16:10:02 -080012namespace host_tool
13{
14
Patrick Venture7dcca5d2019-05-15 12:32:33 -070015using ::testing::_;
Patrick Ventureaa32a362018-12-13 10:52:33 -080016using ::testing::Eq;
17using ::testing::Return;
18using ::testing::StrEq;
Patrick Ventureb58f5612019-05-07 09:22:07 -070019using ::testing::TypedEq;
Patrick Ventureaa32a362018-12-13 10:52:33 -080020
Patrick Venture55646de2019-05-16 10:06:26 -070021TEST(UpdaterTest, CheckAvailableSuccess)
22{
23 /* Call checkAvailable directly() to make sure it works. */
24 DataInterfaceMock handlerMock;
25 ipmiblob::BlobInterfaceMock blobMock;
26
Patrick Venture55646de2019-05-16 10:06:26 -070027 ipmiblob::StatResponse statObj;
Patrick Venture1d5a31c2019-05-20 11:38:22 -070028 statObj.blob_state = ipmi_flash::FirmwareBlobHandler::UpdateFlags::ipmi |
29 ipmi_flash::FirmwareBlobHandler::UpdateFlags::lpc;
Patrick Venture55646de2019-05-16 10:06:26 -070030 statObj.size = 0;
31
32 EXPECT_CALL(blobMock, getBlobList())
Patrick Venture7dad86f2019-05-17 08:52:20 -070033 .WillOnce(
Patrick Venture1d5a31c2019-05-20 11:38:22 -070034 Return(std::vector<std::string>({ipmi_flash::staticLayoutBlobId})));
35 EXPECT_CALL(blobMock, getStat(TypedEq<const std::string&>(
36 ipmi_flash::staticLayoutBlobId)))
Patrick Venture55646de2019-05-16 10:06:26 -070037 .WillOnce(Return(statObj));
38
39 EXPECT_CALL(handlerMock, supportedType())
Patrick Venture1d5a31c2019-05-20 11:38:22 -070040 .WillOnce(Return(ipmi_flash::FirmwareBlobHandler::UpdateFlags::lpc));
Patrick Venture55646de2019-05-16 10:06:26 -070041
42 UpdateHandler updater(&blobMock, &handlerMock);
Patrick Venture1d5a31c2019-05-20 11:38:22 -070043 EXPECT_TRUE(updater.checkAvailable(ipmi_flash::staticLayoutBlobId));
Patrick Venture55646de2019-05-16 10:06:26 -070044}
45
46TEST(UpdaterTest, SendFileSuccess)
47{
48 /* Call sendFile to verify it does what we expect. */
49 DataInterfaceMock handlerMock;
50 ipmiblob::BlobInterfaceMock blobMock;
51
Patrick Venture55646de2019-05-16 10:06:26 -070052 std::string firmwareImage = "image.bin";
53
54 std::uint16_t supported =
55 static_cast<std::uint16_t>(
Patrick Venture1d5a31c2019-05-20 11:38:22 -070056 ipmi_flash::FirmwareBlobHandler::UpdateFlags::lpc) |
Patrick Venture55646de2019-05-16 10:06:26 -070057 static_cast<std::uint16_t>(blobs::OpenFlags::write);
58 std::uint16_t session = 0xbeef;
59
60 EXPECT_CALL(handlerMock, supportedType())
Patrick Venture1d5a31c2019-05-20 11:38:22 -070061 .WillOnce(Return(ipmi_flash::FirmwareBlobHandler::UpdateFlags::lpc));
Patrick Venture55646de2019-05-16 10:06:26 -070062
Patrick Venture1d5a31c2019-05-20 11:38:22 -070063 EXPECT_CALL(
64 blobMock,
65 openBlob(StrEq(ipmi_flash::staticLayoutBlobId.c_str()), supported))
Patrick Venture55646de2019-05-16 10:06:26 -070066 .WillOnce(Return(session));
67
68 EXPECT_CALL(handlerMock,
69 sendContents(StrEq(firmwareImage.c_str()), session))
70 .WillOnce(Return(true));
71
72 EXPECT_CALL(blobMock, closeBlob(session)).Times(1);
73
74 UpdateHandler updater(&blobMock, &handlerMock);
Patrick Venture1d5a31c2019-05-20 11:38:22 -070075 updater.sendFile(ipmi_flash::staticLayoutBlobId, firmwareImage);
Patrick Venture55646de2019-05-16 10:06:26 -070076}
77
78#if 0 /* TODO: fix this up. */
Patrick Ventureaa32a362018-12-13 10:52:33 -080079TEST(UpdaterTest, NormalWalkthroughAllHappy)
80{
81 /* Call updaterMain and have everything respond happily. */
82 DataInterfaceMock handlerMock;
Patrick Venture664c5bc2019-03-07 08:09:45 -080083 ipmiblob::BlobInterfaceMock blobMock;
Patrick Venture55646de2019-05-16 10:06:26 -070084
85 UpdateHandlerMock updaterMock;
86
Patrick Ventureaa32a362018-12-13 10:52:33 -080087 std::string firmwareImage = "image.bin";
88 std::string signatureFile = "image.sig";
89
Patrick Venture1d5a31c2019-05-20 11:38:22 -070090 std::vector<std::string> blobList = {ipmi_flash::staticLayoutBlobId};
Patrick Venture664c5bc2019-03-07 08:09:45 -080091 ipmiblob::StatResponse statObj;
Patrick Venture1d5a31c2019-05-20 11:38:22 -070092 statObj.blob_state = ipmi_flash::FirmwareBlobHandler::UpdateFlags::ipmi |
93 ipmi_flash::FirmwareBlobHandler::UpdateFlags::lpc;
Patrick Ventureaa32a362018-12-13 10:52:33 -080094 statObj.size = 0;
Patrick Venture664c5bc2019-03-07 08:09:45 -080095 std::uint16_t supported =
96 static_cast<std::uint16_t>(
Patrick Venture1d5a31c2019-05-20 11:38:22 -070097 ipmi_flash::FirmwareBlobHandler::UpdateFlags::lpc) |
Patrick Venture664c5bc2019-03-07 08:09:45 -080098 static_cast<std::uint16_t>(blobs::OpenFlags::write);
Patrick Ventureaa32a362018-12-13 10:52:33 -080099 std::uint16_t session = 0xbeef;
100
101 EXPECT_CALL(blobMock, getBlobList()).WillOnce(Return(blobList));
102
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700103 EXPECT_CALL(blobMock, getStat(TypedEq<const std::string&>(ipmi_flash::staticLayoutBlobId)))
Patrick Ventureb58f5612019-05-07 09:22:07 -0700104 .WillOnce(Return(statObj));
Patrick Ventureaa32a362018-12-13 10:52:33 -0800105
Patrick Venture664c5bc2019-03-07 08:09:45 -0800106 EXPECT_CALL(handlerMock, supportedType())
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700107 .WillOnce(Return(ipmi_flash::FirmwareBlobHandler::UpdateFlags::lpc));
Patrick Ventureaa32a362018-12-13 10:52:33 -0800108
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700109 EXPECT_CALL(blobMock, openBlob(StrEq(ipmi_flash::staticLayoutBlobId.c_str()), Eq(supported)))
Patrick Ventureaa32a362018-12-13 10:52:33 -0800110 .WillOnce(Return(session));
111
112 EXPECT_CALL(handlerMock,
113 sendContents(StrEq(firmwareImage.c_str()), Eq(session)))
114 .WillOnce(Return(true));
115
Patrick Venture7dad86f2019-05-17 08:52:20 -0700116 EXPECT_CALL(blobMock, openBlob(StrEq(blobs::hashBlobId.c_str()), Eq(supported)))
Patrick Venture73528382019-05-14 12:43:37 -0700117 .WillOnce(Return(session));
118
119 EXPECT_CALL(handlerMock,
120 sendContents(StrEq(signatureFile.c_str()), Eq(session)))
121 .WillOnce(Return(true));
122
Patrick Venture7dcca5d2019-05-15 12:32:33 -0700123 EXPECT_CALL(blobMock,
Patrick Venture7dad86f2019-05-17 08:52:20 -0700124 openBlob(StrEq(blobs::verifyBlobId.c_str()), Eq(supported)))
Patrick Venture7dcca5d2019-05-15 12:32:33 -0700125 .WillOnce(Return(session));
126
Patrick Venture55646de2019-05-16 10:06:26 -0700127 EXPECT_CALL(blobMock, commit(session, _)).WillOnce(Return());
Patrick Venture7dcca5d2019-05-15 12:32:33 -0700128
Patrick Ventured61b0ff2019-05-15 15:58:06 -0700129 ipmiblob::StatResponse verificationResponse;
130 verificationResponse.blob_state = supported | blobs::StateFlags::committing;
131 verificationResponse.size = 0;
132 verificationResponse.metadata.push_back(static_cast<std::uint8_t>(
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700133 ipmi_flash::FirmwareBlobHandler::VerifyCheckResponses::success));
Patrick Ventured61b0ff2019-05-15 15:58:06 -0700134
135 EXPECT_CALL(blobMock, getStat(TypedEq<std::uint16_t>(session)))
136 .WillOnce(Return(verificationResponse));
137
Patrick Ventureaa32a362018-12-13 10:52:33 -0800138 updaterMain(&blobMock, &handlerMock, firmwareImage, signatureFile);
139}
Patrick Venture55646de2019-05-16 10:06:26 -0700140#endif
Patrick Venture9b534f02018-12-13 16:10:02 -0800141
142} // namespace host_tool