1 #include "commands.hpp"
2 #include "errors.hpp"
3 #include "handler_mock.hpp"
4 #include "machine_name.hpp"
5 
6 #include <cstdint>
7 #include <cstring>
8 #include <string>
9 #include <vector>
10 
11 #include <gtest/gtest.h>
12 
13 #define MAX_IPMI_BUFFER 64
14 
15 using ::testing::Return;
16 using ::testing::Throw;
17 
18 namespace google
19 {
20 namespace ipmi
21 {
22 
23 TEST(MachineNameCommandTest, InvalidCommandLength)
24 {
25     std::vector<std::uint8_t> request = {};
26     size_t dataLen = request.size();
27     std::uint8_t reply[MAX_IPMI_BUFFER];
28 
29     ::testing::StrictMock<HandlerMock> hMock;
30 
31     EXPECT_EQ(IPMI_CC_REQ_DATA_LEN_INVALID,
32               getMachineName(request.data(), reply, &dataLen, &hMock));
33 }
34 
35 TEST(MachineNameCommandTest, InvalidFile)
36 {
37     std::vector<std::uint8_t> request = {SysOEMCommands::SysMachineName};
38     size_t dataLen = request.size();
39     std::uint8_t reply[MAX_IPMI_BUFFER];
40 
41     ::testing::StrictMock<HandlerMock> hMock;
42     EXPECT_CALL(hMock, getMachineName()).WillOnce(Throw(IpmiException(5)));
43 
44     EXPECT_EQ(5, getMachineName(request.data(), reply, &dataLen, &hMock));
45 }
46 
47 TEST(MachineNameCommandTest, CachesValidRequest)
48 {
49     std::vector<std::uint8_t> request = {SysOEMCommands::SysMachineName};
50     size_t dataLen = request.size();
51     std::uint8_t reply[MAX_IPMI_BUFFER];
52     const std::string ret = "Machine";
53 
54     ::testing::StrictMock<HandlerMock> hMock;
55     EXPECT_CALL(hMock, getMachineName()).WillOnce(Return(ret));
56 
57     EXPECT_EQ(IPMI_CC_OK,
58               getMachineName(request.data(), reply, &dataLen, &hMock));
59     EXPECT_EQ(SysOEMCommands::SysMachineName, reply[0]);
60     EXPECT_EQ(ret.size(), reply[1]);
61     EXPECT_EQ(0, std::memcmp(&reply[2], ret.data(), ret.size()));
62 
63     dataLen = request.size();
64     memset(reply, 0, sizeof(reply));
65     EXPECT_EQ(IPMI_CC_OK,
66               getMachineName(request.data(), reply, &dataLen, &hMock));
67     EXPECT_EQ(SysOEMCommands::SysMachineName, reply[0]);
68     EXPECT_EQ(ret.size(), reply[1]);
69     EXPECT_EQ(0, std::memcmp(&reply[2], ret.data(), ret.size()));
70 }
71 
72 } // namespace ipmi
73 } // namespace google
74