blob: 31c8d558efc2d2cee0f54b61590e14de5bfbafd1 [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 Ventureaa32a362018-12-13 10:52:33 -08004
Patrick Venture664c5bc2019-03-07 08:09:45 -08005#include <blobs-ipmid/blobs.hpp>
6#include <ipmiblob/test/blob_interface_mock.hpp>
Patrick Ventureaa32a362018-12-13 10:52:33 -08007#include <string>
8
9#include <gtest/gtest.h>
10
Patrick Venture9b534f02018-12-13 16:10:02 -080011namespace host_tool
12{
13
Patrick Venture7dcca5d2019-05-15 12:32:33 -070014using ::testing::_;
Patrick Ventureaa32a362018-12-13 10:52:33 -080015using ::testing::Eq;
16using ::testing::Return;
17using ::testing::StrEq;
Patrick Ventureb58f5612019-05-07 09:22:07 -070018using ::testing::TypedEq;
Patrick Ventureaa32a362018-12-13 10:52:33 -080019
Patrick Venture55646de2019-05-16 10:06:26 -070020TEST(UpdaterTest, CheckAvailableSuccess)
21{
22 /* Call checkAvailable directly() to make sure it works. */
23 DataInterfaceMock handlerMock;
24 ipmiblob::BlobInterfaceMock blobMock;
25
26 std::string expectedBlob = "/flash/image";
27
28 ipmiblob::StatResponse statObj;
29 statObj.blob_state = blobs::FirmwareBlobHandler::UpdateFlags::ipmi |
30 blobs::FirmwareBlobHandler::UpdateFlags::lpc;
31 statObj.size = 0;
32
33 EXPECT_CALL(blobMock, getBlobList())
34 .WillOnce(Return(std::vector<std::string>({expectedBlob})));
35 EXPECT_CALL(blobMock, getStat(TypedEq<const std::string&>(expectedBlob)))
36 .WillOnce(Return(statObj));
37
38 EXPECT_CALL(handlerMock, supportedType())
39 .WillOnce(Return(blobs::FirmwareBlobHandler::UpdateFlags::lpc));
40
41 UpdateHandler updater(&blobMock, &handlerMock);
42 EXPECT_TRUE(updater.checkAvailable(expectedBlob));
43}
44
45TEST(UpdaterTest, SendFileSuccess)
46{
47 /* Call sendFile to verify it does what we expect. */
48 DataInterfaceMock handlerMock;
49 ipmiblob::BlobInterfaceMock blobMock;
50
51 std::string expectedBlob = "/flash/image";
52 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
63 EXPECT_CALL(blobMock, openBlob(StrEq(expectedBlob.c_str()), supported))
64 .WillOnce(Return(session));
65
66 EXPECT_CALL(handlerMock,
67 sendContents(StrEq(firmwareImage.c_str()), session))
68 .WillOnce(Return(true));
69
70 EXPECT_CALL(blobMock, closeBlob(session)).Times(1);
71
72 UpdateHandler updater(&blobMock, &handlerMock);
73 updater.sendFile(expectedBlob, firmwareImage);
74}
75
76#if 0 /* TODO: fix this up. */
Patrick Ventureaa32a362018-12-13 10:52:33 -080077TEST(UpdaterTest, NormalWalkthroughAllHappy)
78{
79 /* Call updaterMain and have everything respond happily. */
80 DataInterfaceMock handlerMock;
Patrick Venture664c5bc2019-03-07 08:09:45 -080081 ipmiblob::BlobInterfaceMock blobMock;
Patrick Venture55646de2019-05-16 10:06:26 -070082
83 UpdateHandlerMock updaterMock;
84
Patrick Ventureaa32a362018-12-13 10:52:33 -080085 std::string firmwareImage = "image.bin";
86 std::string signatureFile = "image.sig";
Patrick Venture339dece2018-12-14 18:32:04 -080087 std::string expectedBlob = "/flash/image";
Patrick Venture73528382019-05-14 12:43:37 -070088 std::string expectedHash = "/flash/hash";
Patrick Venture7dcca5d2019-05-15 12:32:33 -070089 std::string expectedVerify = "/flash/verify";
Patrick Ventureaa32a362018-12-13 10:52:33 -080090
Patrick Venture339dece2018-12-14 18:32:04 -080091 std::vector<std::string> blobList = {expectedBlob};
Patrick Venture664c5bc2019-03-07 08:09:45 -080092 ipmiblob::StatResponse statObj;
Patrick Ventureaa32a362018-12-13 10:52:33 -080093 statObj.blob_state = blobs::FirmwareBlobHandler::UpdateFlags::ipmi |
94 blobs::FirmwareBlobHandler::UpdateFlags::lpc;
95 statObj.size = 0;
Patrick Venture664c5bc2019-03-07 08:09:45 -080096 std::uint16_t supported =
97 static_cast<std::uint16_t>(
98 blobs::FirmwareBlobHandler::UpdateFlags::lpc) |
99 static_cast<std::uint16_t>(blobs::OpenFlags::write);
Patrick Ventureaa32a362018-12-13 10:52:33 -0800100 std::uint16_t session = 0xbeef;
101
102 EXPECT_CALL(blobMock, getBlobList()).WillOnce(Return(blobList));
103
Patrick Ventureb58f5612019-05-07 09:22:07 -0700104 EXPECT_CALL(blobMock, getStat(TypedEq<const std::string&>(expectedBlob)))
105 .WillOnce(Return(statObj));
Patrick Ventureaa32a362018-12-13 10:52:33 -0800106
Patrick Venture664c5bc2019-03-07 08:09:45 -0800107 EXPECT_CALL(handlerMock, supportedType())
108 .WillOnce(Return(blobs::FirmwareBlobHandler::UpdateFlags::lpc));
Patrick Ventureaa32a362018-12-13 10:52:33 -0800109
Patrick Venture339dece2018-12-14 18:32:04 -0800110 EXPECT_CALL(blobMock, openBlob(StrEq(expectedBlob.c_str()), Eq(supported)))
Patrick Ventureaa32a362018-12-13 10:52:33 -0800111 .WillOnce(Return(session));
112
113 EXPECT_CALL(handlerMock,
114 sendContents(StrEq(firmwareImage.c_str()), Eq(session)))
115 .WillOnce(Return(true));
116
Patrick Venture73528382019-05-14 12:43:37 -0700117 EXPECT_CALL(blobMock, openBlob(StrEq(expectedHash.c_str()), Eq(supported)))
118 .WillOnce(Return(session));
119
120 EXPECT_CALL(handlerMock,
121 sendContents(StrEq(signatureFile.c_str()), Eq(session)))
122 .WillOnce(Return(true));
123
Patrick Venture7dcca5d2019-05-15 12:32:33 -0700124 EXPECT_CALL(blobMock,
125 openBlob(StrEq(expectedVerify.c_str()), Eq(supported)))
126 .WillOnce(Return(session));
127
Patrick Venture55646de2019-05-16 10:06:26 -0700128 EXPECT_CALL(blobMock, commit(session, _)).WillOnce(Return());
Patrick Venture7dcca5d2019-05-15 12:32:33 -0700129
Patrick Ventured61b0ff2019-05-15 15:58:06 -0700130 ipmiblob::StatResponse verificationResponse;
131 verificationResponse.blob_state = supported | blobs::StateFlags::committing;
132 verificationResponse.size = 0;
133 verificationResponse.metadata.push_back(static_cast<std::uint8_t>(
134 blobs::FirmwareBlobHandler::VerifyCheckResponses::success));
135
136 EXPECT_CALL(blobMock, getStat(TypedEq<std::uint16_t>(session)))
137 .WillOnce(Return(verificationResponse));
138
Patrick Ventureaa32a362018-12-13 10:52:33 -0800139 updaterMain(&blobMock, &handlerMock, firmwareImage, signatureFile);
140}
Patrick Venture55646de2019-05-16 10:06:26 -0700141#endif
Patrick Venture9b534f02018-12-13 16:10:02 -0800142
143} // namespace host_tool