Patrick Venture | aa32a36 | 2018-12-13 10:52:33 -0800 | [diff] [blame] | 1 | #include "data_interface_mock.hpp" |
| 2 | #include "updater.hpp" |
Patrick Venture | 55646de | 2019-05-16 10:06:26 -0700 | [diff] [blame] | 3 | #include "updater_mock.hpp" |
Patrick Venture | 7dad86f | 2019-05-17 08:52:20 -0700 | [diff] [blame] | 4 | #include "util.hpp" |
Patrick Venture | aa32a36 | 2018-12-13 10:52:33 -0800 | [diff] [blame] | 5 | |
Patrick Venture | 664c5bc | 2019-03-07 08:09:45 -0800 | [diff] [blame] | 6 | #include <blobs-ipmid/blobs.hpp> |
| 7 | #include <ipmiblob/test/blob_interface_mock.hpp> |
Patrick Venture | aa32a36 | 2018-12-13 10:52:33 -0800 | [diff] [blame] | 8 | #include <string> |
| 9 | |
| 10 | #include <gtest/gtest.h> |
| 11 | |
Patrick Venture | 9b534f0 | 2018-12-13 16:10:02 -0800 | [diff] [blame] | 12 | namespace host_tool |
| 13 | { |
| 14 | |
Patrick Venture | 7dcca5d | 2019-05-15 12:32:33 -0700 | [diff] [blame] | 15 | using ::testing::_; |
Patrick Venture | aa32a36 | 2018-12-13 10:52:33 -0800 | [diff] [blame] | 16 | using ::testing::Eq; |
| 17 | using ::testing::Return; |
| 18 | using ::testing::StrEq; |
Patrick Venture | b58f561 | 2019-05-07 09:22:07 -0700 | [diff] [blame] | 19 | using ::testing::TypedEq; |
Patrick Venture | aa32a36 | 2018-12-13 10:52:33 -0800 | [diff] [blame] | 20 | |
Patrick Venture | 55646de | 2019-05-16 10:06:26 -0700 | [diff] [blame] | 21 | TEST(UpdaterTest, CheckAvailableSuccess) |
| 22 | { |
| 23 | /* Call checkAvailable directly() to make sure it works. */ |
| 24 | DataInterfaceMock handlerMock; |
| 25 | ipmiblob::BlobInterfaceMock blobMock; |
| 26 | |
Patrick Venture | 55646de | 2019-05-16 10:06:26 -0700 | [diff] [blame] | 27 | 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 Venture | 7dad86f | 2019-05-17 08:52:20 -0700 | [diff] [blame] | 33 | .WillOnce( |
| 34 | Return(std::vector<std::string>({blobs::staticLayoutBlobId}))); |
| 35 | EXPECT_CALL(blobMock, |
| 36 | getStat(TypedEq<const std::string&>(blobs::staticLayoutBlobId))) |
Patrick Venture | 55646de | 2019-05-16 10:06:26 -0700 | [diff] [blame] | 37 | .WillOnce(Return(statObj)); |
| 38 | |
| 39 | EXPECT_CALL(handlerMock, supportedType()) |
| 40 | .WillOnce(Return(blobs::FirmwareBlobHandler::UpdateFlags::lpc)); |
| 41 | |
| 42 | UpdateHandler updater(&blobMock, &handlerMock); |
Patrick Venture | 7dad86f | 2019-05-17 08:52:20 -0700 | [diff] [blame] | 43 | EXPECT_TRUE(updater.checkAvailable(blobs::staticLayoutBlobId)); |
Patrick Venture | 55646de | 2019-05-16 10:06:26 -0700 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | TEST(UpdaterTest, SendFileSuccess) |
| 47 | { |
| 48 | /* Call sendFile to verify it does what we expect. */ |
| 49 | DataInterfaceMock handlerMock; |
| 50 | ipmiblob::BlobInterfaceMock blobMock; |
| 51 | |
Patrick Venture | 55646de | 2019-05-16 10:06:26 -0700 | [diff] [blame] | 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 | |
Patrick Venture | 7dad86f | 2019-05-17 08:52:20 -0700 | [diff] [blame] | 63 | EXPECT_CALL(blobMock, |
| 64 | openBlob(StrEq(blobs::staticLayoutBlobId.c_str()), supported)) |
Patrick Venture | 55646de | 2019-05-16 10:06:26 -0700 | [diff] [blame] | 65 | .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 Venture | 7dad86f | 2019-05-17 08:52:20 -0700 | [diff] [blame] | 74 | updater.sendFile(blobs::staticLayoutBlobId, firmwareImage); |
Patrick Venture | 55646de | 2019-05-16 10:06:26 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | #if 0 /* TODO: fix this up. */ |
Patrick Venture | aa32a36 | 2018-12-13 10:52:33 -0800 | [diff] [blame] | 78 | TEST(UpdaterTest, NormalWalkthroughAllHappy) |
| 79 | { |
| 80 | /* Call updaterMain and have everything respond happily. */ |
| 81 | DataInterfaceMock handlerMock; |
Patrick Venture | 664c5bc | 2019-03-07 08:09:45 -0800 | [diff] [blame] | 82 | ipmiblob::BlobInterfaceMock blobMock; |
Patrick Venture | 55646de | 2019-05-16 10:06:26 -0700 | [diff] [blame] | 83 | |
| 84 | UpdateHandlerMock updaterMock; |
| 85 | |
Patrick Venture | aa32a36 | 2018-12-13 10:52:33 -0800 | [diff] [blame] | 86 | std::string firmwareImage = "image.bin"; |
| 87 | std::string signatureFile = "image.sig"; |
| 88 | |
Patrick Venture | 7dad86f | 2019-05-17 08:52:20 -0700 | [diff] [blame] | 89 | std::vector<std::string> blobList = {blobs::staticLayoutBlobId}; |
Patrick Venture | 664c5bc | 2019-03-07 08:09:45 -0800 | [diff] [blame] | 90 | ipmiblob::StatResponse statObj; |
Patrick Venture | aa32a36 | 2018-12-13 10:52:33 -0800 | [diff] [blame] | 91 | statObj.blob_state = blobs::FirmwareBlobHandler::UpdateFlags::ipmi | |
| 92 | blobs::FirmwareBlobHandler::UpdateFlags::lpc; |
| 93 | statObj.size = 0; |
Patrick Venture | 664c5bc | 2019-03-07 08:09:45 -0800 | [diff] [blame] | 94 | 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 Venture | aa32a36 | 2018-12-13 10:52:33 -0800 | [diff] [blame] | 98 | std::uint16_t session = 0xbeef; |
| 99 | |
| 100 | EXPECT_CALL(blobMock, getBlobList()).WillOnce(Return(blobList)); |
| 101 | |
Patrick Venture | 7dad86f | 2019-05-17 08:52:20 -0700 | [diff] [blame] | 102 | EXPECT_CALL(blobMock, getStat(TypedEq<const std::string&>(blobs::staticLayoutBlobId))) |
Patrick Venture | b58f561 | 2019-05-07 09:22:07 -0700 | [diff] [blame] | 103 | .WillOnce(Return(statObj)); |
Patrick Venture | aa32a36 | 2018-12-13 10:52:33 -0800 | [diff] [blame] | 104 | |
Patrick Venture | 664c5bc | 2019-03-07 08:09:45 -0800 | [diff] [blame] | 105 | EXPECT_CALL(handlerMock, supportedType()) |
| 106 | .WillOnce(Return(blobs::FirmwareBlobHandler::UpdateFlags::lpc)); |
Patrick Venture | aa32a36 | 2018-12-13 10:52:33 -0800 | [diff] [blame] | 107 | |
Patrick Venture | 7dad86f | 2019-05-17 08:52:20 -0700 | [diff] [blame] | 108 | EXPECT_CALL(blobMock, openBlob(StrEq(blobs::staticLayoutBlobId.c_str()), Eq(supported))) |
Patrick Venture | aa32a36 | 2018-12-13 10:52:33 -0800 | [diff] [blame] | 109 | .WillOnce(Return(session)); |
| 110 | |
| 111 | EXPECT_CALL(handlerMock, |
| 112 | sendContents(StrEq(firmwareImage.c_str()), Eq(session))) |
| 113 | .WillOnce(Return(true)); |
| 114 | |
Patrick Venture | 7dad86f | 2019-05-17 08:52:20 -0700 | [diff] [blame] | 115 | EXPECT_CALL(blobMock, openBlob(StrEq(blobs::hashBlobId.c_str()), Eq(supported))) |
Patrick Venture | 7352838 | 2019-05-14 12:43:37 -0700 | [diff] [blame] | 116 | .WillOnce(Return(session)); |
| 117 | |
| 118 | EXPECT_CALL(handlerMock, |
| 119 | sendContents(StrEq(signatureFile.c_str()), Eq(session))) |
| 120 | .WillOnce(Return(true)); |
| 121 | |
Patrick Venture | 7dcca5d | 2019-05-15 12:32:33 -0700 | [diff] [blame] | 122 | EXPECT_CALL(blobMock, |
Patrick Venture | 7dad86f | 2019-05-17 08:52:20 -0700 | [diff] [blame] | 123 | openBlob(StrEq(blobs::verifyBlobId.c_str()), Eq(supported))) |
Patrick Venture | 7dcca5d | 2019-05-15 12:32:33 -0700 | [diff] [blame] | 124 | .WillOnce(Return(session)); |
| 125 | |
Patrick Venture | 55646de | 2019-05-16 10:06:26 -0700 | [diff] [blame] | 126 | EXPECT_CALL(blobMock, commit(session, _)).WillOnce(Return()); |
Patrick Venture | 7dcca5d | 2019-05-15 12:32:33 -0700 | [diff] [blame] | 127 | |
Patrick Venture | d61b0ff | 2019-05-15 15:58:06 -0700 | [diff] [blame] | 128 | 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 Venture | aa32a36 | 2018-12-13 10:52:33 -0800 | [diff] [blame] | 137 | updaterMain(&blobMock, &handlerMock, firmwareImage, signatureFile); |
| 138 | } |
Patrick Venture | 55646de | 2019-05-16 10:06:26 -0700 | [diff] [blame] | 139 | #endif |
Patrick Venture | 9b534f0 | 2018-12-13 16:10:02 -0800 | [diff] [blame] | 140 | |
| 141 | } // namespace host_tool |