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 "handler_mock.hpp"
17 #include "helper.hpp"
18 #include "psu.hpp"
19 
20 #include <cstdint>
21 #include <cstring>
22 #include <vector>
23 
24 #include <gtest/gtest.h>
25 
26 using ::testing::Return;
27 
28 namespace google
29 {
30 namespace ipmi
31 {
32 
TEST(PsuCommandTest,InvalidRequestLength)33 TEST(PsuCommandTest, InvalidRequestLength)
34 {
35     std::vector<std::uint8_t> request = {};
36     HandlerMock hMock;
37 
38     EXPECT_EQ(::ipmi::responseReqDataLenInvalid(),
39               psuHardReset(request, &hMock));
40 }
41 
TEST(PsuCommandTest,ValidRequest)42 TEST(PsuCommandTest, ValidRequest)
43 {
44     std::uint8_t delayValue = 0x45;
45     struct PsuResetRequest requestContents;
46     requestContents.delay = delayValue;
47     std::vector<std::uint8_t> request(sizeof(requestContents));
48     std::memcpy(request.data(), &requestContents, sizeof(requestContents));
49 
50     HandlerMock hMock;
51     EXPECT_CALL(hMock, psuResetDelay(delayValue));
52 
53     auto reply = psuHardReset(request, &hMock);
54     auto result = ValidateReply(reply, /*hasData=*/false);
55 
56     EXPECT_EQ(SysOEMCommands::SysPsuHardReset, result.first);
57 }
58 
TEST(PsuResetOnShutdownCommandTest,ValidRequest)59 TEST(PsuResetOnShutdownCommandTest, ValidRequest)
60 {
61     std::vector<std::uint8_t> request = {};
62 
63     HandlerMock hMock;
64     EXPECT_CALL(hMock, psuResetOnShutdown());
65 
66     auto reply = psuHardResetOnShutdown(request, &hMock);
67     auto result = ValidateReply(reply, /*hasData=*/false);
68 
69     EXPECT_EQ(SysOEMCommands::SysPsuHardResetOnShutdown, result.first);
70 }
71 
72 } // namespace ipmi
73 } // namespace google
74