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 "errors.hpp"
17 #include "handler_mock.hpp"
18 #include "helper.hpp"
19 #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 
30 using ::testing::Return;
31 using ::testing::Throw;
32 
33 namespace google
34 {
35 namespace ipmi
36 {
37 
TEST(MachineNameCommandTest,InvalidFile)38 TEST(MachineNameCommandTest, InvalidFile)
39 {
40     std::vector<std::uint8_t> request = {};
41     ::testing::StrictMock<HandlerMock> hMock;
42     EXPECT_CALL(hMock, getMachineName()).WillOnce(Throw(IpmiException(5)));
43 
44     EXPECT_EQ(::ipmi::response(5), getMachineName(request, &hMock));
45 }
46 
TEST(MachineNameCommandTest,CachesValidRequest)47 TEST(MachineNameCommandTest, CachesValidRequest)
48 {
49     std::vector<std::uint8_t> request = {};
50     const std::string ret = "Machine";
51 
52     ::testing::StrictMock<HandlerMock> hMock;
53     EXPECT_CALL(hMock, getMachineName()).WillOnce(Return(ret));
54 
55     auto reply = getMachineName(request, &hMock);
56     auto result = ValidateReply(reply);
57     auto& data = result.second;
58 
59     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()));
65 }
66 
67 } // namespace ipmi
68 } // namespace google
69