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 | 07f8515 | 2019-03-15 21:36:56 -0700 | [diff] [blame] | 16 | #include "entity_name.hpp" |
| 17 | #include "handler_mock.hpp" |
Patrick Venture | 07f8515 | 2019-03-15 21:36:56 -0700 | [diff] [blame] | 18 | |
| 19 | #include <cstdint> |
| 20 | #include <cstring> |
| 21 | #include <string> |
| 22 | #include <vector> |
| 23 | |
| 24 | #include <gtest/gtest.h> |
| 25 | |
| 26 | #define MAX_IPMI_BUFFER 64 |
| 27 | |
| 28 | using ::testing::Return; |
| 29 | |
| 30 | namespace google |
| 31 | { |
| 32 | namespace ipmi |
| 33 | { |
| 34 | |
| 35 | TEST(EntityNameCommandTest, InvalidCommandLength) |
| 36 | { |
| 37 | // GetEntityNameRequest is three bytes, let's send 2. |
| 38 | std::vector<std::uint8_t> request = {SysOEMCommands::SysEntityName, 0x01}; |
| 39 | size_t dataLen = request.size(); |
| 40 | std::uint8_t reply[MAX_IPMI_BUFFER]; |
| 41 | |
| 42 | HandlerMock hMock; |
| 43 | EXPECT_EQ(IPMI_CC_REQ_DATA_LEN_INVALID, |
Patrick Venture | 45fad1b | 2019-03-18 16:52:14 -0700 | [diff] [blame] | 44 | getEntityName(request.data(), reply, &dataLen, &hMock)); |
Patrick Venture | 07f8515 | 2019-03-15 21:36:56 -0700 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | TEST(EntityNameCommandTest, ValidRequest) |
| 48 | { |
| 49 | std::uint8_t entityId = 3; |
| 50 | std::uint8_t entityInstance = 5; |
| 51 | std::vector<std::uint8_t> request = {SysOEMCommands::SysEntityName, |
| 52 | entityId, entityInstance}; |
| 53 | size_t dataLen = request.size(); |
| 54 | std::uint8_t reply[MAX_IPMI_BUFFER]; |
| 55 | std::string entityName = "asdf"; |
| 56 | |
| 57 | HandlerMock hMock; |
| 58 | EXPECT_CALL(hMock, getEntityName(entityId, entityInstance)) |
| 59 | .WillOnce(Return(entityName)); |
| 60 | EXPECT_EQ(IPMI_CC_OK, |
Patrick Venture | 45fad1b | 2019-03-18 16:52:14 -0700 | [diff] [blame] | 61 | getEntityName(request.data(), reply, &dataLen, &hMock)); |
Patrick Venture | 07f8515 | 2019-03-15 21:36:56 -0700 | [diff] [blame] | 62 | EXPECT_EQ(reply[1], entityName.length()); |
| 63 | EXPECT_EQ(0, std::memcmp(&reply[2], entityName.c_str(), reply[1])); |
| 64 | } |
| 65 | |
| 66 | } // namespace ipmi |
| 67 | } // namespace google |