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; |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame^] | 28 | statObj.blob_state = ipmi_flash::FirmwareBlobHandler::UpdateFlags::ipmi | |
| 29 | ipmi_flash::FirmwareBlobHandler::UpdateFlags::lpc; |
Patrick Venture | 55646de | 2019-05-16 10:06:26 -0700 | [diff] [blame] | 30 | statObj.size = 0; |
| 31 | |
| 32 | EXPECT_CALL(blobMock, getBlobList()) |
Patrick Venture | 7dad86f | 2019-05-17 08:52:20 -0700 | [diff] [blame] | 33 | .WillOnce( |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame^] | 34 | Return(std::vector<std::string>({ipmi_flash::staticLayoutBlobId}))); |
| 35 | EXPECT_CALL(blobMock, getStat(TypedEq<const std::string&>( |
| 36 | ipmi_flash::staticLayoutBlobId))) |
Patrick Venture | 55646de | 2019-05-16 10:06:26 -0700 | [diff] [blame] | 37 | .WillOnce(Return(statObj)); |
| 38 | |
| 39 | EXPECT_CALL(handlerMock, supportedType()) |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame^] | 40 | .WillOnce(Return(ipmi_flash::FirmwareBlobHandler::UpdateFlags::lpc)); |
Patrick Venture | 55646de | 2019-05-16 10:06:26 -0700 | [diff] [blame] | 41 | |
| 42 | UpdateHandler updater(&blobMock, &handlerMock); |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame^] | 43 | EXPECT_TRUE(updater.checkAvailable(ipmi_flash::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>( |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame^] | 56 | ipmi_flash::FirmwareBlobHandler::UpdateFlags::lpc) | |
Patrick Venture | 55646de | 2019-05-16 10:06:26 -0700 | [diff] [blame] | 57 | static_cast<std::uint16_t>(blobs::OpenFlags::write); |
| 58 | std::uint16_t session = 0xbeef; |
| 59 | |
| 60 | EXPECT_CALL(handlerMock, supportedType()) |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame^] | 61 | .WillOnce(Return(ipmi_flash::FirmwareBlobHandler::UpdateFlags::lpc)); |
Patrick Venture | 55646de | 2019-05-16 10:06:26 -0700 | [diff] [blame] | 62 | |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame^] | 63 | EXPECT_CALL( |
| 64 | blobMock, |
| 65 | openBlob(StrEq(ipmi_flash::staticLayoutBlobId.c_str()), supported)) |
Patrick Venture | 55646de | 2019-05-16 10:06:26 -0700 | [diff] [blame] | 66 | .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 Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame^] | 75 | updater.sendFile(ipmi_flash::staticLayoutBlobId, firmwareImage); |
Patrick Venture | 55646de | 2019-05-16 10:06:26 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | #if 0 /* TODO: fix this up. */ |
Patrick Venture | aa32a36 | 2018-12-13 10:52:33 -0800 | [diff] [blame] | 79 | TEST(UpdaterTest, NormalWalkthroughAllHappy) |
| 80 | { |
| 81 | /* Call updaterMain and have everything respond happily. */ |
| 82 | DataInterfaceMock handlerMock; |
Patrick Venture | 664c5bc | 2019-03-07 08:09:45 -0800 | [diff] [blame] | 83 | ipmiblob::BlobInterfaceMock blobMock; |
Patrick Venture | 55646de | 2019-05-16 10:06:26 -0700 | [diff] [blame] | 84 | |
| 85 | UpdateHandlerMock updaterMock; |
| 86 | |
Patrick Venture | aa32a36 | 2018-12-13 10:52:33 -0800 | [diff] [blame] | 87 | std::string firmwareImage = "image.bin"; |
| 88 | std::string signatureFile = "image.sig"; |
| 89 | |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame^] | 90 | std::vector<std::string> blobList = {ipmi_flash::staticLayoutBlobId}; |
Patrick Venture | 664c5bc | 2019-03-07 08:09:45 -0800 | [diff] [blame] | 91 | ipmiblob::StatResponse statObj; |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame^] | 92 | statObj.blob_state = ipmi_flash::FirmwareBlobHandler::UpdateFlags::ipmi | |
| 93 | ipmi_flash::FirmwareBlobHandler::UpdateFlags::lpc; |
Patrick Venture | aa32a36 | 2018-12-13 10:52:33 -0800 | [diff] [blame] | 94 | statObj.size = 0; |
Patrick Venture | 664c5bc | 2019-03-07 08:09:45 -0800 | [diff] [blame] | 95 | std::uint16_t supported = |
| 96 | static_cast<std::uint16_t>( |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame^] | 97 | ipmi_flash::FirmwareBlobHandler::UpdateFlags::lpc) | |
Patrick Venture | 664c5bc | 2019-03-07 08:09:45 -0800 | [diff] [blame] | 98 | static_cast<std::uint16_t>(blobs::OpenFlags::write); |
Patrick Venture | aa32a36 | 2018-12-13 10:52:33 -0800 | [diff] [blame] | 99 | std::uint16_t session = 0xbeef; |
| 100 | |
| 101 | EXPECT_CALL(blobMock, getBlobList()).WillOnce(Return(blobList)); |
| 102 | |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame^] | 103 | EXPECT_CALL(blobMock, getStat(TypedEq<const std::string&>(ipmi_flash::staticLayoutBlobId))) |
Patrick Venture | b58f561 | 2019-05-07 09:22:07 -0700 | [diff] [blame] | 104 | .WillOnce(Return(statObj)); |
Patrick Venture | aa32a36 | 2018-12-13 10:52:33 -0800 | [diff] [blame] | 105 | |
Patrick Venture | 664c5bc | 2019-03-07 08:09:45 -0800 | [diff] [blame] | 106 | EXPECT_CALL(handlerMock, supportedType()) |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame^] | 107 | .WillOnce(Return(ipmi_flash::FirmwareBlobHandler::UpdateFlags::lpc)); |
Patrick Venture | aa32a36 | 2018-12-13 10:52:33 -0800 | [diff] [blame] | 108 | |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame^] | 109 | EXPECT_CALL(blobMock, openBlob(StrEq(ipmi_flash::staticLayoutBlobId.c_str()), Eq(supported))) |
Patrick Venture | aa32a36 | 2018-12-13 10:52:33 -0800 | [diff] [blame] | 110 | .WillOnce(Return(session)); |
| 111 | |
| 112 | EXPECT_CALL(handlerMock, |
| 113 | sendContents(StrEq(firmwareImage.c_str()), Eq(session))) |
| 114 | .WillOnce(Return(true)); |
| 115 | |
Patrick Venture | 7dad86f | 2019-05-17 08:52:20 -0700 | [diff] [blame] | 116 | EXPECT_CALL(blobMock, openBlob(StrEq(blobs::hashBlobId.c_str()), Eq(supported))) |
Patrick Venture | 7352838 | 2019-05-14 12:43:37 -0700 | [diff] [blame] | 117 | .WillOnce(Return(session)); |
| 118 | |
| 119 | EXPECT_CALL(handlerMock, |
| 120 | sendContents(StrEq(signatureFile.c_str()), Eq(session))) |
| 121 | .WillOnce(Return(true)); |
| 122 | |
Patrick Venture | 7dcca5d | 2019-05-15 12:32:33 -0700 | [diff] [blame] | 123 | EXPECT_CALL(blobMock, |
Patrick Venture | 7dad86f | 2019-05-17 08:52:20 -0700 | [diff] [blame] | 124 | openBlob(StrEq(blobs::verifyBlobId.c_str()), Eq(supported))) |
Patrick Venture | 7dcca5d | 2019-05-15 12:32:33 -0700 | [diff] [blame] | 125 | .WillOnce(Return(session)); |
| 126 | |
Patrick Venture | 55646de | 2019-05-16 10:06:26 -0700 | [diff] [blame] | 127 | EXPECT_CALL(blobMock, commit(session, _)).WillOnce(Return()); |
Patrick Venture | 7dcca5d | 2019-05-15 12:32:33 -0700 | [diff] [blame] | 128 | |
Patrick Venture | d61b0ff | 2019-05-15 15:58:06 -0700 | [diff] [blame] | 129 | 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 Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame^] | 133 | ipmi_flash::FirmwareBlobHandler::VerifyCheckResponses::success)); |
Patrick Venture | d61b0ff | 2019-05-15 15:58:06 -0700 | [diff] [blame] | 134 | |
| 135 | EXPECT_CALL(blobMock, getStat(TypedEq<std::uint16_t>(session))) |
| 136 | .WillOnce(Return(verificationResponse)); |
| 137 | |
Patrick Venture | aa32a36 | 2018-12-13 10:52:33 -0800 | [diff] [blame] | 138 | updaterMain(&blobMock, &handlerMock, firmwareImage, signatureFile); |
| 139 | } |
Patrick Venture | 55646de | 2019-05-16 10:06:26 -0700 | [diff] [blame] | 140 | #endif |
Patrick Venture | 9b534f0 | 2018-12-13 16:10:02 -0800 | [diff] [blame] | 141 | |
| 142 | } // namespace host_tool |