1a2056e9cSWilly Tu // Copyright 2021 Google LLC
2a2056e9cSWilly Tu //
3a2056e9cSWilly Tu // Licensed under the Apache License, Version 2.0 (the "License");
4a2056e9cSWilly Tu // you may not use this file except in compliance with the License.
5a2056e9cSWilly Tu // You may obtain a copy of the License at
6a2056e9cSWilly Tu //
7a2056e9cSWilly Tu //      http://www.apache.org/licenses/LICENSE-2.0
8a2056e9cSWilly Tu //
9a2056e9cSWilly Tu // Unless required by applicable law or agreed to in writing, software
10a2056e9cSWilly Tu // distributed under the License is distributed on an "AS IS" BASIS,
11a2056e9cSWilly Tu // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12a2056e9cSWilly Tu // See the License for the specific language governing permissions and
13a2056e9cSWilly Tu // limitations under the License.
14a2056e9cSWilly Tu 
150e9aae5dSPatrick Venture #include "commands.hpp"
1607f85150SPatrick Venture #include "entity_name.hpp"
1707f85150SPatrick Venture #include "handler_mock.hpp"
18*ff3cd8e9SWilly Tu #include "helper.hpp"
1907f85150SPatrick Venture 
2007f85150SPatrick Venture #include <cstdint>
2107f85150SPatrick Venture #include <cstring>
2207f85150SPatrick Venture #include <string>
2307f85150SPatrick Venture #include <vector>
2407f85150SPatrick Venture 
2507f85150SPatrick Venture #include <gtest/gtest.h>
2607f85150SPatrick Venture 
2707f85150SPatrick Venture #define MAX_IPMI_BUFFER 64
2807f85150SPatrick Venture 
2907f85150SPatrick Venture using ::testing::Return;
3007f85150SPatrick Venture 
3107f85150SPatrick Venture namespace google
3207f85150SPatrick Venture {
3307f85150SPatrick Venture namespace ipmi
3407f85150SPatrick Venture {
3507f85150SPatrick Venture 
TEST(EntityNameCommandTest,InvalidCommandLength)3607f85150SPatrick Venture TEST(EntityNameCommandTest, InvalidCommandLength)
3707f85150SPatrick Venture {
3807f85150SPatrick Venture     // GetEntityNameRequest is three bytes, let's send 2.
39*ff3cd8e9SWilly Tu     std::vector<std::uint8_t> request = {0x01};
4007f85150SPatrick Venture     HandlerMock hMock;
41*ff3cd8e9SWilly Tu 
42*ff3cd8e9SWilly Tu     EXPECT_EQ(::ipmi::responseReqDataLenInvalid(),
43*ff3cd8e9SWilly Tu               getEntityName(request, &hMock));
4407f85150SPatrick Venture }
4507f85150SPatrick Venture 
TEST(EntityNameCommandTest,ValidRequest)4607f85150SPatrick Venture TEST(EntityNameCommandTest, ValidRequest)
4707f85150SPatrick Venture {
4807f85150SPatrick Venture     std::uint8_t entityId = 3;
4907f85150SPatrick Venture     std::uint8_t entityInstance = 5;
50*ff3cd8e9SWilly Tu     std::vector<std::uint8_t> request = {entityId, entityInstance};
5107f85150SPatrick Venture     std::string entityName = "asdf";
5207f85150SPatrick Venture 
5307f85150SPatrick Venture     HandlerMock hMock;
5407f85150SPatrick Venture     EXPECT_CALL(hMock, getEntityName(entityId, entityInstance))
5507f85150SPatrick Venture         .WillOnce(Return(entityName));
56*ff3cd8e9SWilly Tu 
57*ff3cd8e9SWilly Tu     auto reply = getEntityName(request, &hMock);
58*ff3cd8e9SWilly Tu     auto result = ValidateReply(reply);
59*ff3cd8e9SWilly Tu     auto& data = result.second;
60*ff3cd8e9SWilly Tu 
61*ff3cd8e9SWilly Tu     EXPECT_EQ(sizeof(GetEntityNameReply) + entityName.size(), data.size());
62*ff3cd8e9SWilly Tu     EXPECT_EQ(SysOEMCommands::SysEntityName, result.first);
63*ff3cd8e9SWilly Tu     EXPECT_EQ(entityName.length(), data[0]);
64*ff3cd8e9SWilly Tu     EXPECT_EQ(entityName.data(),
65*ff3cd8e9SWilly Tu               std::string(data.begin() + sizeof(struct GetEntityNameReply),
66*ff3cd8e9SWilly Tu                           data.end()));
6707f85150SPatrick Venture }
6807f85150SPatrick Venture 
6907f85150SPatrick Venture } // namespace ipmi
7007f85150SPatrick Venture } // namespace google
71