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 15 #include "commands.hpp" 16 #include "entity_name.hpp" 17 #include "handler_mock.hpp" 18 #include "helper.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 31 namespace google 32 { 33 namespace ipmi 34 { 35 36 TEST(EntityNameCommandTest, InvalidCommandLength) 37 { 38 // GetEntityNameRequest is three bytes, let's send 2. 39 std::vector<std::uint8_t> request = {0x01}; 40 HandlerMock hMock; 41 42 EXPECT_EQ(::ipmi::responseReqDataLenInvalid(), 43 getEntityName(request, &hMock)); 44 } 45 46 TEST(EntityNameCommandTest, ValidRequest) 47 { 48 std::uint8_t entityId = 3; 49 std::uint8_t entityInstance = 5; 50 std::vector<std::uint8_t> request = {entityId, entityInstance}; 51 std::string entityName = "asdf"; 52 53 HandlerMock hMock; 54 EXPECT_CALL(hMock, getEntityName(entityId, entityInstance)) 55 .WillOnce(Return(entityName)); 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())); 67 } 68 69 } // namespace ipmi 70 } // namespace google 71