blob: a939a8aff1767a4889db8948a8afb363a3dea118 [file] [log] [blame]
Willy Tua2056e92021-10-10 13:36:16 -07001// 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 Venture0e9aae52020-08-13 13:07:09 -070015#include "commands.hpp"
Patrick Venture07f85152019-03-15 21:36:56 -070016#include "entity_name.hpp"
17#include "handler_mock.hpp"
Patrick Venture07f85152019-03-15 21:36:56 -070018
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
28using ::testing::Return;
29
30namespace google
31{
32namespace ipmi
33{
34
35TEST(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 Venture45fad1b2019-03-18 16:52:14 -070044 getEntityName(request.data(), reply, &dataLen, &hMock));
Patrick Venture07f85152019-03-15 21:36:56 -070045}
46
47TEST(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 Venture45fad1b2019-03-18 16:52:14 -070061 getEntityName(request.data(), reply, &dataLen, &hMock));
Patrick Venture07f85152019-03-15 21:36:56 -070062 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