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" |
Willy Tu | ff3cd8e | 2021-09-14 22:49:55 -0700 | [diff] [blame] | 18 | #include "helper.hpp" |
William A. Kennington III | 29f35bc | 2020-11-03 23:30:31 -0800 | [diff] [blame] | 19 | #include "machine_name.hpp" |
| 20 | |
| 21 | #include <cstdint> |
| 22 | #include <cstring> |
| 23 | #include <string> |
| 24 | #include <vector> |
| 25 | |
| 26 | #include <gtest/gtest.h> |
| 27 | |
| 28 | #define MAX_IPMI_BUFFER 64 |
| 29 | |
| 30 | using ::testing::Return; |
| 31 | using ::testing::Throw; |
| 32 | |
| 33 | namespace google |
| 34 | { |
| 35 | namespace ipmi |
| 36 | { |
| 37 | |
William A. Kennington III | 29f35bc | 2020-11-03 23:30:31 -0800 | [diff] [blame] | 38 | TEST(MachineNameCommandTest, InvalidFile) |
| 39 | { |
Willy Tu | ff3cd8e | 2021-09-14 22:49:55 -0700 | [diff] [blame] | 40 | std::vector<std::uint8_t> request = {}; |
William A. Kennington III | 29f35bc | 2020-11-03 23:30:31 -0800 | [diff] [blame] | 41 | ::testing::StrictMock<HandlerMock> hMock; |
| 42 | EXPECT_CALL(hMock, getMachineName()).WillOnce(Throw(IpmiException(5))); |
| 43 | |
Willy Tu | ff3cd8e | 2021-09-14 22:49:55 -0700 | [diff] [blame] | 44 | EXPECT_EQ(::ipmi::response(5), getMachineName(request, &hMock)); |
William A. Kennington III | 29f35bc | 2020-11-03 23:30:31 -0800 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | TEST(MachineNameCommandTest, CachesValidRequest) |
| 48 | { |
Willy Tu | ff3cd8e | 2021-09-14 22:49:55 -0700 | [diff] [blame] | 49 | std::vector<std::uint8_t> request = {}; |
William A. Kennington III | 29f35bc | 2020-11-03 23:30:31 -0800 | [diff] [blame] | 50 | const std::string ret = "Machine"; |
| 51 | |
| 52 | ::testing::StrictMock<HandlerMock> hMock; |
| 53 | EXPECT_CALL(hMock, getMachineName()).WillOnce(Return(ret)); |
| 54 | |
Willy Tu | ff3cd8e | 2021-09-14 22:49:55 -0700 | [diff] [blame] | 55 | auto reply = getMachineName(request, &hMock); |
| 56 | auto result = ValidateReply(reply); |
| 57 | auto& data = result.second; |
William A. Kennington III | 29f35bc | 2020-11-03 23:30:31 -0800 | [diff] [blame] | 58 | |
Willy Tu | ff3cd8e | 2021-09-14 22:49:55 -0700 | [diff] [blame] | 59 | EXPECT_EQ(sizeof(GetMachineNameReply) + ret.size(), data.size()); |
| 60 | EXPECT_EQ(SysOEMCommands::SysMachineName, result.first); |
| 61 | EXPECT_EQ(ret.length(), data[0]); |
| 62 | EXPECT_EQ(ret.data(), |
| 63 | std::string(data.begin() + sizeof(struct GetMachineNameReply), |
| 64 | data.end())); |
William A. Kennington III | 29f35bc | 2020-11-03 23:30:31 -0800 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | } // namespace ipmi |
| 68 | } // namespace google |