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 | |
William A. Kennington III | 29f35bc | 2020-11-03 23:30:31 -0800 | [diff] [blame] | 15 | #include "commands.hpp" |
| 16 | #include "errors.hpp" |
| 17 | #include "handler_mock.hpp" |
| 18 | #include "machine_name.hpp" |
| 19 | |
| 20 | #include <cstdint> |
| 21 | #include <cstring> |
| 22 | #include <string> |
| 23 | #include <vector> |
| 24 | |
| 25 | #include <gtest/gtest.h> |
| 26 | |
| 27 | #define MAX_IPMI_BUFFER 64 |
| 28 | |
| 29 | using ::testing::Return; |
| 30 | using ::testing::Throw; |
| 31 | |
| 32 | namespace google |
| 33 | { |
| 34 | namespace ipmi |
| 35 | { |
| 36 | |
| 37 | TEST(MachineNameCommandTest, InvalidCommandLength) |
| 38 | { |
| 39 | std::vector<std::uint8_t> request = {}; |
| 40 | size_t dataLen = request.size(); |
| 41 | std::uint8_t reply[MAX_IPMI_BUFFER]; |
| 42 | |
| 43 | ::testing::StrictMock<HandlerMock> hMock; |
| 44 | |
| 45 | EXPECT_EQ(IPMI_CC_REQ_DATA_LEN_INVALID, |
| 46 | getMachineName(request.data(), reply, &dataLen, &hMock)); |
| 47 | } |
| 48 | |
| 49 | TEST(MachineNameCommandTest, InvalidFile) |
| 50 | { |
| 51 | std::vector<std::uint8_t> request = {SysOEMCommands::SysMachineName}; |
| 52 | size_t dataLen = request.size(); |
| 53 | std::uint8_t reply[MAX_IPMI_BUFFER]; |
| 54 | |
| 55 | ::testing::StrictMock<HandlerMock> hMock; |
| 56 | EXPECT_CALL(hMock, getMachineName()).WillOnce(Throw(IpmiException(5))); |
| 57 | |
| 58 | EXPECT_EQ(5, getMachineName(request.data(), reply, &dataLen, &hMock)); |
| 59 | } |
| 60 | |
| 61 | TEST(MachineNameCommandTest, CachesValidRequest) |
| 62 | { |
| 63 | std::vector<std::uint8_t> request = {SysOEMCommands::SysMachineName}; |
| 64 | size_t dataLen = request.size(); |
| 65 | std::uint8_t reply[MAX_IPMI_BUFFER]; |
| 66 | const std::string ret = "Machine"; |
| 67 | |
| 68 | ::testing::StrictMock<HandlerMock> hMock; |
| 69 | EXPECT_CALL(hMock, getMachineName()).WillOnce(Return(ret)); |
| 70 | |
| 71 | EXPECT_EQ(IPMI_CC_OK, |
| 72 | getMachineName(request.data(), reply, &dataLen, &hMock)); |
| 73 | EXPECT_EQ(SysOEMCommands::SysMachineName, reply[0]); |
| 74 | EXPECT_EQ(ret.size(), reply[1]); |
| 75 | EXPECT_EQ(0, std::memcmp(&reply[2], ret.data(), ret.size())); |
| 76 | |
| 77 | dataLen = request.size(); |
| 78 | memset(reply, 0, sizeof(reply)); |
| 79 | EXPECT_EQ(IPMI_CC_OK, |
| 80 | getMachineName(request.data(), reply, &dataLen, &hMock)); |
| 81 | EXPECT_EQ(SysOEMCommands::SysMachineName, reply[0]); |
| 82 | EXPECT_EQ(ret.size(), reply[1]); |
| 83 | EXPECT_EQ(0, std::memcmp(&reply[2], ret.data(), ret.size())); |
| 84 | } |
| 85 | |
| 86 | } // namespace ipmi |
| 87 | } // namespace google |