blob: 080671fedf3c0848f09192524ddc189559e5906b [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;
28 statObj.blob_state = blobs::FirmwareBlobHandler::UpdateFlags::ipmi |
29 blobs::FirmwareBlobHandler::UpdateFlags::lpc;
30 statObj.size = 0;
31
32 EXPECT_CALL(blobMock, getBlobList())
Patrick Venture7dad86f2019-05-17 08:52:20 -070033 .WillOnce(
34 Return(std::vector<std::string>({blobs::staticLayoutBlobId})));
35 EXPECT_CALL(blobMock,
36 getStat(TypedEq<const std::string&>(blobs::staticLayoutBlobId)))
Patrick Venture55646de2019-05-16 10:06:26 -070037 .WillOnce(Return(statObj));
38
39 EXPECT_CALL(handlerMock, supportedType())
40 .WillOnce(Return(blobs::FirmwareBlobHandler::UpdateFlags::lpc));
41
42 UpdateHandler updater(&blobMock, &handlerMock);
Patrick Venture7dad86f2019-05-17 08:52:20 -070043 EXPECT_TRUE(updater.checkAvailable(blobs::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>(
56 blobs::FirmwareBlobHandler::UpdateFlags::lpc) |
57 static_cast<std::uint16_t>(blobs::OpenFlags::write);
58 std::uint16_t session = 0xbeef;
59
60 EXPECT_CALL(handlerMock, supportedType())
61 .WillOnce(Return(blobs::FirmwareBlobHandler::UpdateFlags::lpc));
62
Patrick Venture7dad86f2019-05-17 08:52:20 -070063 EXPECT_CALL(blobMock,
64 openBlob(StrEq(blobs::staticLayoutBlobId.c_str()), supported))
Patrick Venture55646de2019-05-16 10:06:26 -070065 .WillOnce(Return(session));
66
67 EXPECT_CALL(handlerMock,
68 sendContents(StrEq(firmwareImage.c_str()), session))
69 .WillOnce(Return(true));
70
71 EXPECT_CALL(blobMock, closeBlob(session)).Times(1);
72
73 UpdateHandler updater(&blobMock, &handlerMock);
Patrick Venture7dad86f2019-05-17 08:52:20 -070074 updater.sendFile(blobs::staticLayoutBlobId, firmwareImage);
Patrick Venture55646de2019-05-16 10:06:26 -070075}
76
77#if 0 /* TODO: fix this up. */
Patrick Ventureaa32a362018-12-13 10:52:33 -080078TEST(UpdaterTest, NormalWalkthroughAllHappy)
79{
80 /* Call updaterMain and have everything respond happily. */
81 DataInterfaceMock handlerMock;
Patrick Venture664c5bc2019-03-07 08:09:45 -080082 ipmiblob::BlobInterfaceMock blobMock;
Patrick Venture55646de2019-05-16 10:06:26 -070083
84 UpdateHandlerMock updaterMock;
85
Patrick Ventureaa32a362018-12-13 10:52:33 -080086 std::string firmwareImage = "image.bin";
87 std::string signatureFile = "image.sig";
88
Patrick Venture7dad86f2019-05-17 08:52:20 -070089 std::vector<std::string> blobList = {blobs::staticLayoutBlobId};
Patrick Venture664c5bc2019-03-07 08:09:45 -080090 ipmiblob::StatResponse statObj;
Patrick Ventureaa32a362018-12-13 10:52:33 -080091 statObj.blob_state = blobs::FirmwareBlobHandler::UpdateFlags::ipmi |
92 blobs::FirmwareBlobHandler::UpdateFlags::lpc;
93 statObj.size = 0;
Patrick Venture664c5bc2019-03-07 08:09:45 -080094 std::uint16_t supported =
95 static_cast<std::uint16_t>(
96 blobs::FirmwareBlobHandler::UpdateFlags::lpc) |
97 static_cast<std::uint16_t>(blobs::OpenFlags::write);
Patrick Ventureaa32a362018-12-13 10:52:33 -080098 std::uint16_t session = 0xbeef;
99
100 EXPECT_CALL(blobMock, getBlobList()).WillOnce(Return(blobList));
101
Patrick Venture7dad86f2019-05-17 08:52:20 -0700102 EXPECT_CALL(blobMock, getStat(TypedEq<const std::string&>(blobs::staticLayoutBlobId)))
Patrick Ventureb58f5612019-05-07 09:22:07 -0700103 .WillOnce(Return(statObj));
Patrick Ventureaa32a362018-12-13 10:52:33 -0800104
Patrick Venture664c5bc2019-03-07 08:09:45 -0800105 EXPECT_CALL(handlerMock, supportedType())
106 .WillOnce(Return(blobs::FirmwareBlobHandler::UpdateFlags::lpc));
Patrick Ventureaa32a362018-12-13 10:52:33 -0800107
Patrick Venture7dad86f2019-05-17 08:52:20 -0700108 EXPECT_CALL(blobMock, openBlob(StrEq(blobs::staticLayoutBlobId.c_str()), Eq(supported)))
Patrick Ventureaa32a362018-12-13 10:52:33 -0800109 .WillOnce(Return(session));
110
111 EXPECT_CALL(handlerMock,
112 sendContents(StrEq(firmwareImage.c_str()), Eq(session)))
113 .WillOnce(Return(true));
114
Patrick Venture7dad86f2019-05-17 08:52:20 -0700115 EXPECT_CALL(blobMock, openBlob(StrEq(blobs::hashBlobId.c_str()), Eq(supported)))
Patrick Venture73528382019-05-14 12:43:37 -0700116 .WillOnce(Return(session));
117
118 EXPECT_CALL(handlerMock,
119 sendContents(StrEq(signatureFile.c_str()), Eq(session)))
120 .WillOnce(Return(true));
121
Patrick Venture7dcca5d2019-05-15 12:32:33 -0700122 EXPECT_CALL(blobMock,
Patrick Venture7dad86f2019-05-17 08:52:20 -0700123 openBlob(StrEq(blobs::verifyBlobId.c_str()), Eq(supported)))
Patrick Venture7dcca5d2019-05-15 12:32:33 -0700124 .WillOnce(Return(session));
125
Patrick Venture55646de2019-05-16 10:06:26 -0700126 EXPECT_CALL(blobMock, commit(session, _)).WillOnce(Return());
Patrick Venture7dcca5d2019-05-15 12:32:33 -0700127
Patrick Ventured61b0ff2019-05-15 15:58:06 -0700128 ipmiblob::StatResponse verificationResponse;
129 verificationResponse.blob_state = supported | blobs::StateFlags::committing;
130 verificationResponse.size = 0;
131 verificationResponse.metadata.push_back(static_cast<std::uint8_t>(
132 blobs::FirmwareBlobHandler::VerifyCheckResponses::success));
133
134 EXPECT_CALL(blobMock, getStat(TypedEq<std::uint16_t>(session)))
135 .WillOnce(Return(verificationResponse));
136
Patrick Ventureaa32a362018-12-13 10:52:33 -0800137 updaterMain(&blobMock, &handlerMock, firmwareImage, signatureFile);
138}
Patrick Venture55646de2019-05-16 10:06:26 -0700139#endif
Patrick Venture9b534f02018-12-13 16:10:02 -0800140
141} // namespace host_tool