1*a2056e9cSWilly Tu // Copyright 2021 Google LLC 2*a2056e9cSWilly Tu // 3*a2056e9cSWilly Tu // Licensed under the Apache License, Version 2.0 (the "License"); 4*a2056e9cSWilly Tu // you may not use this file except in compliance with the License. 5*a2056e9cSWilly Tu // You may obtain a copy of the License at 6*a2056e9cSWilly Tu // 7*a2056e9cSWilly Tu // http://www.apache.org/licenses/LICENSE-2.0 8*a2056e9cSWilly Tu // 9*a2056e9cSWilly Tu // Unless required by applicable law or agreed to in writing, software 10*a2056e9cSWilly Tu // distributed under the License is distributed on an "AS IS" BASIS, 11*a2056e9cSWilly Tu // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12*a2056e9cSWilly Tu // See the License for the specific language governing permissions and 13*a2056e9cSWilly Tu // limitations under the License. 14*a2056e9cSWilly Tu 150e9aae5dSPatrick Venture #include "commands.hpp" 1607f85150SPatrick Venture #include "entity_name.hpp" 1707f85150SPatrick Venture #include "handler_mock.hpp" 1807f85150SPatrick Venture 1907f85150SPatrick Venture #include <cstdint> 2007f85150SPatrick Venture #include <cstring> 2107f85150SPatrick Venture #include <string> 2207f85150SPatrick Venture #include <vector> 2307f85150SPatrick Venture 2407f85150SPatrick Venture #include <gtest/gtest.h> 2507f85150SPatrick Venture 2607f85150SPatrick Venture #define MAX_IPMI_BUFFER 64 2707f85150SPatrick Venture 2807f85150SPatrick Venture using ::testing::Return; 2907f85150SPatrick Venture 3007f85150SPatrick Venture namespace google 3107f85150SPatrick Venture { 3207f85150SPatrick Venture namespace ipmi 3307f85150SPatrick Venture { 3407f85150SPatrick Venture 3507f85150SPatrick Venture TEST(EntityNameCommandTest, InvalidCommandLength) 3607f85150SPatrick Venture { 3707f85150SPatrick Venture // GetEntityNameRequest is three bytes, let's send 2. 3807f85150SPatrick Venture std::vector<std::uint8_t> request = {SysOEMCommands::SysEntityName, 0x01}; 3907f85150SPatrick Venture size_t dataLen = request.size(); 4007f85150SPatrick Venture std::uint8_t reply[MAX_IPMI_BUFFER]; 4107f85150SPatrick Venture 4207f85150SPatrick Venture HandlerMock hMock; 4307f85150SPatrick Venture EXPECT_EQ(IPMI_CC_REQ_DATA_LEN_INVALID, 4445fad1bbSPatrick Venture getEntityName(request.data(), reply, &dataLen, &hMock)); 4507f85150SPatrick Venture } 4607f85150SPatrick Venture 4707f85150SPatrick Venture TEST(EntityNameCommandTest, ValidRequest) 4807f85150SPatrick Venture { 4907f85150SPatrick Venture std::uint8_t entityId = 3; 5007f85150SPatrick Venture std::uint8_t entityInstance = 5; 5107f85150SPatrick Venture std::vector<std::uint8_t> request = {SysOEMCommands::SysEntityName, 5207f85150SPatrick Venture entityId, entityInstance}; 5307f85150SPatrick Venture size_t dataLen = request.size(); 5407f85150SPatrick Venture std::uint8_t reply[MAX_IPMI_BUFFER]; 5507f85150SPatrick Venture std::string entityName = "asdf"; 5607f85150SPatrick Venture 5707f85150SPatrick Venture HandlerMock hMock; 5807f85150SPatrick Venture EXPECT_CALL(hMock, getEntityName(entityId, entityInstance)) 5907f85150SPatrick Venture .WillOnce(Return(entityName)); 6007f85150SPatrick Venture EXPECT_EQ(IPMI_CC_OK, 6145fad1bbSPatrick Venture getEntityName(request.data(), reply, &dataLen, &hMock)); 6207f85150SPatrick Venture EXPECT_EQ(reply[1], entityName.length()); 6307f85150SPatrick Venture EXPECT_EQ(0, std::memcmp(&reply[2], entityName.c_str(), reply[1])); 6407f85150SPatrick Venture } 6507f85150SPatrick Venture 6607f85150SPatrick Venture } // namespace ipmi 6707f85150SPatrick Venture } // namespace google 68