Willy Tu | a2056e9 | 2021-10-10 13:36:16 -0700 | [diff] [blame] | 1 | // Copyright 2021 Google LLC |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
Patrick Venture | 0e9aae5 | 2020-08-13 13:07:09 -0700 | [diff] [blame] | 15 | #include "commands.hpp" |
Patrick Venture | aa37412 | 2019-03-15 15:09:10 -0700 | [diff] [blame] | 16 | #include "handler_mock.hpp" |
Patrick Venture | aa37412 | 2019-03-15 15:09:10 -0700 | [diff] [blame] | 17 | #include "psu.hpp" |
| 18 | |
| 19 | #include <cstdint> |
| 20 | #include <cstring> |
| 21 | #include <vector> |
| 22 | |
| 23 | #include <gtest/gtest.h> |
| 24 | |
| 25 | #define MAX_IPMI_BUFFER 64 |
| 26 | |
| 27 | using ::testing::Return; |
| 28 | |
| 29 | namespace google |
| 30 | { |
| 31 | namespace ipmi |
| 32 | { |
| 33 | |
| 34 | TEST(PsuCommandTest, InvalidRequestLength) |
| 35 | { |
| 36 | std::vector<std::uint8_t> request = {SysOEMCommands::SysPsuHardReset}; |
| 37 | size_t dataLen = request.size(); |
| 38 | std::uint8_t reply[MAX_IPMI_BUFFER]; |
| 39 | |
| 40 | HandlerMock hMock; |
| 41 | EXPECT_EQ(IPMI_CC_REQ_DATA_LEN_INVALID, |
Patrick Venture | 45fad1b | 2019-03-18 16:52:14 -0700 | [diff] [blame] | 42 | psuHardReset(request.data(), reply, &dataLen, &hMock)); |
Patrick Venture | aa37412 | 2019-03-15 15:09:10 -0700 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | TEST(PsuCommandTest, ValidRequest) |
| 46 | { |
| 47 | std::uint32_t delayValue = 0x45; |
| 48 | struct PsuResetRequest requestContents; |
| 49 | requestContents.subcommand = SysOEMCommands::SysPsuHardReset; |
| 50 | requestContents.delay = delayValue; |
| 51 | |
| 52 | std::vector<std::uint8_t> request(sizeof(requestContents)); |
| 53 | std::memcpy(request.data(), &requestContents, sizeof(requestContents)); |
| 54 | size_t dataLen = request.size(); |
| 55 | std::uint8_t reply[MAX_IPMI_BUFFER]; |
| 56 | |
| 57 | HandlerMock hMock; |
| 58 | EXPECT_CALL(hMock, psuResetDelay(delayValue)); |
| 59 | EXPECT_EQ(IPMI_CC_OK, |
Patrick Venture | 45fad1b | 2019-03-18 16:52:14 -0700 | [diff] [blame] | 60 | psuHardReset(request.data(), reply, &dataLen, &hMock)); |
Patrick Venture | aa37412 | 2019-03-15 15:09:10 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Shounak Mitra | ac4a16f | 2021-02-02 11:11:44 -0800 | [diff] [blame] | 63 | TEST(PsuResetOnShutdownCommandTest, InvalidRequestLength) |
| 64 | { |
| 65 | std::vector<std::uint8_t> request; |
| 66 | size_t dataLen = request.size(); |
| 67 | std::uint8_t reply[MAX_IPMI_BUFFER]; |
| 68 | |
| 69 | HandlerMock hMock; |
| 70 | EXPECT_EQ(IPMI_CC_REQ_DATA_LEN_INVALID, |
| 71 | psuHardResetOnShutdown(request.data(), reply, &dataLen, &hMock)); |
| 72 | } |
| 73 | |
| 74 | TEST(PsuResetOnShutdownCommandTest, ValidRequest) |
| 75 | { |
| 76 | struct PsuResetOnShutdownRequest requestContents; |
| 77 | requestContents.subcommand = SysOEMCommands::SysPsuHardReset; |
| 78 | |
| 79 | std::vector<std::uint8_t> request(sizeof(requestContents)); |
| 80 | std::memcpy(request.data(), &requestContents, sizeof(requestContents)); |
| 81 | size_t dataLen = request.size(); |
| 82 | std::uint8_t reply[MAX_IPMI_BUFFER]; |
| 83 | |
| 84 | HandlerMock hMock; |
| 85 | EXPECT_CALL(hMock, psuResetOnShutdown()); |
| 86 | EXPECT_EQ(IPMI_CC_OK, |
| 87 | psuHardResetOnShutdown(request.data(), reply, &dataLen, &hMock)); |
| 88 | } |
| 89 | |
Patrick Venture | aa37412 | 2019-03-15 15:09:10 -0700 | [diff] [blame] | 90 | } // namespace ipmi |
| 91 | } // namespace google |