blob: 3dfd3c0ec07d65d0681d57d7cc21a050e08b1117 [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
William A. Kennington III29f35bc2020-11-03 23:30:31 -080015#include "commands.hpp"
16#include "errors.hpp"
17#include "handler_mock.hpp"
Willy Tuff3cd8e2021-09-14 22:49:55 -070018#include "helper.hpp"
William A. Kennington III29f35bc2020-11-03 23:30:31 -080019#include "machine_name.hpp"
20
21#include <cstdint>
22#include <cstring>
23#include <string>
24#include <vector>
25
26#include <gtest/gtest.h>
27
28#define MAX_IPMI_BUFFER 64
29
30using ::testing::Return;
31using ::testing::Throw;
32
33namespace google
34{
35namespace ipmi
36{
37
William A. Kennington III29f35bc2020-11-03 23:30:31 -080038TEST(MachineNameCommandTest, InvalidFile)
39{
Willy Tuff3cd8e2021-09-14 22:49:55 -070040 std::vector<std::uint8_t> request = {};
William A. Kennington III29f35bc2020-11-03 23:30:31 -080041 ::testing::StrictMock<HandlerMock> hMock;
42 EXPECT_CALL(hMock, getMachineName()).WillOnce(Throw(IpmiException(5)));
43
Willy Tuff3cd8e2021-09-14 22:49:55 -070044 EXPECT_EQ(::ipmi::response(5), getMachineName(request, &hMock));
William A. Kennington III29f35bc2020-11-03 23:30:31 -080045}
46
47TEST(MachineNameCommandTest, CachesValidRequest)
48{
Willy Tuff3cd8e2021-09-14 22:49:55 -070049 std::vector<std::uint8_t> request = {};
William A. Kennington III29f35bc2020-11-03 23:30:31 -080050 const std::string ret = "Machine";
51
52 ::testing::StrictMock<HandlerMock> hMock;
53 EXPECT_CALL(hMock, getMachineName()).WillOnce(Return(ret));
54
Willy Tuff3cd8e2021-09-14 22:49:55 -070055 auto reply = getMachineName(request, &hMock);
56 auto result = ValidateReply(reply);
57 auto& data = result.second;
William A. Kennington III29f35bc2020-11-03 23:30:31 -080058
Willy Tuff3cd8e2021-09-14 22:49:55 -070059 EXPECT_EQ(sizeof(GetMachineNameReply) + ret.size(), data.size());
60 EXPECT_EQ(SysOEMCommands::SysMachineName, result.first);
61 EXPECT_EQ(ret.length(), data[0]);
62 EXPECT_EQ(ret.data(),
63 std::string(data.begin() + sizeof(struct GetMachineNameReply),
64 data.end()));
William A. Kennington III29f35bc2020-11-03 23:30:31 -080065}
66
67} // namespace ipmi
68} // namespace google