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" |
Willy Tu | ff3cd8e | 2021-09-14 22:49:55 -0700 | [diff] [blame] | 18 | #include "helper.hpp" |
Patrick Venture | 07f8515 | 2019-03-15 21:36:56 -0700 | [diff] [blame] | 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 | |
| 31 | namespace google |
| 32 | { |
| 33 | namespace ipmi |
| 34 | { |
| 35 | |
| 36 | TEST(EntityNameCommandTest, InvalidCommandLength) |
| 37 | { |
| 38 | // GetEntityNameRequest is three bytes, let's send 2. |
Willy Tu | ff3cd8e | 2021-09-14 22:49:55 -0700 | [diff] [blame] | 39 | std::vector<std::uint8_t> request = {0x01}; |
Patrick Venture | 07f8515 | 2019-03-15 21:36:56 -0700 | [diff] [blame] | 40 | HandlerMock hMock; |
Willy Tu | ff3cd8e | 2021-09-14 22:49:55 -0700 | [diff] [blame] | 41 | |
| 42 | EXPECT_EQ(::ipmi::responseReqDataLenInvalid(), |
| 43 | getEntityName(request, &hMock)); |
Patrick Venture | 07f8515 | 2019-03-15 21:36:56 -0700 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | TEST(EntityNameCommandTest, ValidRequest) |
| 47 | { |
| 48 | std::uint8_t entityId = 3; |
| 49 | std::uint8_t entityInstance = 5; |
Willy Tu | ff3cd8e | 2021-09-14 22:49:55 -0700 | [diff] [blame] | 50 | std::vector<std::uint8_t> request = {entityId, entityInstance}; |
Patrick Venture | 07f8515 | 2019-03-15 21:36:56 -0700 | [diff] [blame] | 51 | std::string entityName = "asdf"; |
| 52 | |
| 53 | HandlerMock hMock; |
| 54 | EXPECT_CALL(hMock, getEntityName(entityId, entityInstance)) |
| 55 | .WillOnce(Return(entityName)); |
Willy Tu | ff3cd8e | 2021-09-14 22:49:55 -0700 | [diff] [blame] | 56 | |
| 57 | auto reply = getEntityName(request, &hMock); |
| 58 | auto result = ValidateReply(reply); |
| 59 | auto& data = result.second; |
| 60 | |
| 61 | EXPECT_EQ(sizeof(GetEntityNameReply) + entityName.size(), data.size()); |
| 62 | EXPECT_EQ(SysOEMCommands::SysEntityName, result.first); |
| 63 | EXPECT_EQ(entityName.length(), data[0]); |
| 64 | EXPECT_EQ(entityName.data(), |
| 65 | std::string(data.begin() + sizeof(struct GetEntityNameReply), |
| 66 | data.end())); |
Patrick Venture | 07f8515 | 2019-03-15 21:36:56 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | } // namespace ipmi |
| 70 | } // namespace google |