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 | aa32a36 | 2018-12-13 10:52:33 -0800 | [diff] [blame] | 4 | |
Patrick Venture | 664c5bc | 2019-03-07 08:09:45 -0800 | [diff] [blame] | 5 | #include <blobs-ipmid/blobs.hpp> |
| 6 | #include <ipmiblob/test/blob_interface_mock.hpp> |
Patrick Venture | aa32a36 | 2018-12-13 10:52:33 -0800 | [diff] [blame] | 7 | #include <string> |
| 8 | |
| 9 | #include <gtest/gtest.h> |
| 10 | |
Patrick Venture | 9b534f0 | 2018-12-13 16:10:02 -0800 | [diff] [blame] | 11 | namespace host_tool |
| 12 | { |
| 13 | |
Patrick Venture | 7dcca5d | 2019-05-15 12:32:33 -0700 | [diff] [blame] | 14 | using ::testing::_; |
Patrick Venture | aa32a36 | 2018-12-13 10:52:33 -0800 | [diff] [blame] | 15 | using ::testing::Eq; |
| 16 | using ::testing::Return; |
| 17 | using ::testing::StrEq; |
Patrick Venture | b58f561 | 2019-05-07 09:22:07 -0700 | [diff] [blame] | 18 | using ::testing::TypedEq; |
Patrick Venture | aa32a36 | 2018-12-13 10:52:33 -0800 | [diff] [blame] | 19 | |
Patrick Venture | 55646de | 2019-05-16 10:06:26 -0700 | [diff] [blame^] | 20 | TEST(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 | |
| 45 | TEST(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 Venture | aa32a36 | 2018-12-13 10:52:33 -0800 | [diff] [blame] | 77 | TEST(UpdaterTest, NormalWalkthroughAllHappy) |
| 78 | { |
| 79 | /* Call updaterMain and have everything respond happily. */ |
| 80 | DataInterfaceMock handlerMock; |
Patrick Venture | 664c5bc | 2019-03-07 08:09:45 -0800 | [diff] [blame] | 81 | ipmiblob::BlobInterfaceMock blobMock; |
Patrick Venture | 55646de | 2019-05-16 10:06:26 -0700 | [diff] [blame^] | 82 | |
| 83 | UpdateHandlerMock updaterMock; |
| 84 | |
Patrick Venture | aa32a36 | 2018-12-13 10:52:33 -0800 | [diff] [blame] | 85 | std::string firmwareImage = "image.bin"; |
| 86 | std::string signatureFile = "image.sig"; |
Patrick Venture | 339dece | 2018-12-14 18:32:04 -0800 | [diff] [blame] | 87 | std::string expectedBlob = "/flash/image"; |
Patrick Venture | 7352838 | 2019-05-14 12:43:37 -0700 | [diff] [blame] | 88 | std::string expectedHash = "/flash/hash"; |
Patrick Venture | 7dcca5d | 2019-05-15 12:32:33 -0700 | [diff] [blame] | 89 | std::string expectedVerify = "/flash/verify"; |
Patrick Venture | aa32a36 | 2018-12-13 10:52:33 -0800 | [diff] [blame] | 90 | |
Patrick Venture | 339dece | 2018-12-14 18:32:04 -0800 | [diff] [blame] | 91 | std::vector<std::string> blobList = {expectedBlob}; |
Patrick Venture | 664c5bc | 2019-03-07 08:09:45 -0800 | [diff] [blame] | 92 | ipmiblob::StatResponse statObj; |
Patrick Venture | aa32a36 | 2018-12-13 10:52:33 -0800 | [diff] [blame] | 93 | statObj.blob_state = blobs::FirmwareBlobHandler::UpdateFlags::ipmi | |
| 94 | blobs::FirmwareBlobHandler::UpdateFlags::lpc; |
| 95 | statObj.size = 0; |
Patrick Venture | 664c5bc | 2019-03-07 08:09:45 -0800 | [diff] [blame] | 96 | 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 Venture | aa32a36 | 2018-12-13 10:52:33 -0800 | [diff] [blame] | 100 | std::uint16_t session = 0xbeef; |
| 101 | |
| 102 | EXPECT_CALL(blobMock, getBlobList()).WillOnce(Return(blobList)); |
| 103 | |
Patrick Venture | b58f561 | 2019-05-07 09:22:07 -0700 | [diff] [blame] | 104 | EXPECT_CALL(blobMock, getStat(TypedEq<const std::string&>(expectedBlob))) |
| 105 | .WillOnce(Return(statObj)); |
Patrick Venture | aa32a36 | 2018-12-13 10:52:33 -0800 | [diff] [blame] | 106 | |
Patrick Venture | 664c5bc | 2019-03-07 08:09:45 -0800 | [diff] [blame] | 107 | EXPECT_CALL(handlerMock, supportedType()) |
| 108 | .WillOnce(Return(blobs::FirmwareBlobHandler::UpdateFlags::lpc)); |
Patrick Venture | aa32a36 | 2018-12-13 10:52:33 -0800 | [diff] [blame] | 109 | |
Patrick Venture | 339dece | 2018-12-14 18:32:04 -0800 | [diff] [blame] | 110 | EXPECT_CALL(blobMock, openBlob(StrEq(expectedBlob.c_str()), Eq(supported))) |
Patrick Venture | aa32a36 | 2018-12-13 10:52:33 -0800 | [diff] [blame] | 111 | .WillOnce(Return(session)); |
| 112 | |
| 113 | EXPECT_CALL(handlerMock, |
| 114 | sendContents(StrEq(firmwareImage.c_str()), Eq(session))) |
| 115 | .WillOnce(Return(true)); |
| 116 | |
Patrick Venture | 7352838 | 2019-05-14 12:43:37 -0700 | [diff] [blame] | 117 | 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 Venture | 7dcca5d | 2019-05-15 12:32:33 -0700 | [diff] [blame] | 124 | EXPECT_CALL(blobMock, |
| 125 | openBlob(StrEq(expectedVerify.c_str()), Eq(supported))) |
| 126 | .WillOnce(Return(session)); |
| 127 | |
Patrick Venture | 55646de | 2019-05-16 10:06:26 -0700 | [diff] [blame^] | 128 | EXPECT_CALL(blobMock, commit(session, _)).WillOnce(Return()); |
Patrick Venture | 7dcca5d | 2019-05-15 12:32:33 -0700 | [diff] [blame] | 129 | |
Patrick Venture | d61b0ff | 2019-05-15 15:58:06 -0700 | [diff] [blame] | 130 | 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 Venture | aa32a36 | 2018-12-13 10:52:33 -0800 | [diff] [blame] | 139 | updaterMain(&blobMock, &handlerMock, firmwareImage, signatureFile); |
| 140 | } |
Patrick Venture | 55646de | 2019-05-16 10:06:26 -0700 | [diff] [blame^] | 141 | #endif |
Patrick Venture | 9b534f0 | 2018-12-13 16:10:02 -0800 | [diff] [blame] | 142 | |
| 143 | } // namespace host_tool |