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 "cpld.hpp"
17 #include "handler_mock.hpp"
18 #include "helper.hpp"
19 
20 #include <cstdint>
21 #include <tuple>
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(CpldCommandTest,RequestTooSmall)33 TEST(CpldCommandTest, RequestTooSmall)
34 {
35     std::vector<std::uint8_t> request = {};
36     HandlerMock hMock;
37 
38     EXPECT_EQ(::ipmi::responseReqDataLenInvalid(),
39               cpldVersion(request, &hMock));
40 }
41 
TEST(CpldCommandTest,ValidRequestReturnsHappy)42 TEST(CpldCommandTest, ValidRequestReturnsHappy)
43 {
44     std::vector<std::uint8_t> request = {0x04};
45 
46     std::uint8_t expectedMaj = 0x5;
47     std::uint8_t expectedMin = 0x3;
48     std::uint8_t expectedPt = 0x7;
49     std::uint8_t expectedSbPtr = 0x9;
50 
51     HandlerMock hMock;
52     EXPECT_CALL(hMock, getCpldVersion(0x04))
53         .WillOnce(Return(std::make_tuple(expectedMaj, expectedMin, expectedPt,
54                                          expectedSbPtr)));
55 
56     // Reply is in the form of
57     // std::tuple<ipmi::Cc, std::optional<std::tuple<RetTypes...>>>
58     auto reply = cpldVersion(request, &hMock);
59     auto result = ValidateReply(reply);
60     auto& data = result.second;
61 
62     EXPECT_EQ(sizeof(struct CpldReply), data.size());
63     EXPECT_EQ(SysOEMCommands::SysCpldVersion, result.first);
64     EXPECT_EQ(expectedMaj, data[0]);
65     EXPECT_EQ(expectedMin, data[1]);
66     EXPECT_EQ(expectedPt, data[2]);
67     EXPECT_EQ(expectedSbPtr, data[3]);
68 }
69 
70 } // namespace ipmi
71 } // namespace google
72