xref: /openbmc/libpldm/tests/oem/ibm/host.cpp (revision 48761c62)
1 #include <endian.h>
2 #include <libpldm/base.h>
3 #include <libpldm/oem/ibm/host.h>
4 
5 #include <array>
6 #include <cstdint>
7 #include <cstring>
8 #include <vector>
9 
10 #include <gtest/gtest.h>
11 
12 constexpr auto hdrSize = sizeof(pldm_msg_hdr);
13 
14 TEST(GetAlertStatus, testGoodEncodeRequest)
15 {
16     std::array<uint8_t, hdrSize + PLDM_GET_ALERT_STATUS_REQ_BYTES> requestMsg{};
17 
18     uint8_t versionId = 0x0;
19 
20     auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
21     auto rc = encode_get_alert_status_req(0, versionId, request,
22                                           PLDM_GET_ALERT_STATUS_REQ_BYTES);
23     EXPECT_EQ(rc, PLDM_SUCCESS);
24     EXPECT_EQ(versionId, request->payload[0]);
25 }
26 
27 TEST(GetAlertStatus, testBadEncodeRequest)
28 {
29     std::array<uint8_t, hdrSize + PLDM_GET_ALERT_STATUS_REQ_BYTES> requestMsg{};
30 
31     auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
32     auto rc = encode_get_alert_status_req(0, 0x0, request,
33                                           PLDM_GET_ALERT_STATUS_REQ_BYTES + 1);
34     EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
35 }
36 
37 TEST(GetAlertStatus, testGoodDecodeResponse)
38 {
39     uint8_t completionCode = PLDM_SUCCESS;
40     uint32_t rack_entry = 0xff000030;
41     uint32_t pri_cec_node = 0x00008030;
42     std::array<uint8_t, hdrSize + PLDM_GET_ALERT_STATUS_RESP_BYTES>
43         responseMsg{};
44 
45     uint8_t retCompletionCode = 0;
46     uint32_t retRack_entry = 0;
47     uint32_t retPri_cec_node = 0;
48 
49     auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
50     struct pldm_get_alert_status_resp* resp =
51         reinterpret_cast<struct pldm_get_alert_status_resp*>(response->payload);
52     resp->completion_code = completionCode;
53     resp->rack_entry = htole32(rack_entry);
54     resp->pri_cec_node = htole32(pri_cec_node);
55 
56     auto rc = decode_get_alert_status_resp(
57         response, responseMsg.size() - hdrSize, &retCompletionCode,
58         &retRack_entry, &retPri_cec_node);
59     EXPECT_EQ(rc, PLDM_SUCCESS);
60     EXPECT_EQ(retCompletionCode, completionCode);
61     EXPECT_EQ(retRack_entry, rack_entry);
62     EXPECT_EQ(retPri_cec_node, pri_cec_node);
63 }
64 
65 TEST(GetAlertStatus, testBadDecodeResponse)
66 {
67     auto rc = decode_get_alert_status_resp(NULL, 0, NULL, NULL, NULL);
68     EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
69 
70     uint8_t completionCode = PLDM_SUCCESS;
71     uint32_t rack_entry = 0xff000030;
72     uint32_t pri_cec_node = 0x00008030;
73     std::array<uint8_t, hdrSize + PLDM_GET_ALERT_STATUS_RESP_BYTES>
74         responseMsg{};
75 
76     uint8_t retCompletionCode = 0;
77     uint32_t retRack_entry = 0;
78     uint32_t retPri_cec_node = 0;
79 
80     auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
81     struct pldm_get_alert_status_resp* resp =
82         reinterpret_cast<struct pldm_get_alert_status_resp*>(response->payload);
83     resp->completion_code = completionCode;
84     resp->rack_entry = htole32(rack_entry);
85     resp->pri_cec_node = htole32(pri_cec_node);
86 
87     rc = decode_get_alert_status_resp(
88         response, responseMsg.size() - hdrSize + 1, &retCompletionCode,
89         &retRack_entry, &retPri_cec_node);
90     EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
91 }
92 
93 TEST(GetAlertStatus, testGoodEncodeResponse)
94 {
95     uint8_t completionCode = 0;
96     uint32_t rack_entry = 0xff000030;
97     uint32_t pri_cec_node = 0x00008030;
98 
99     std::vector<uint8_t> responseMsg(hdrSize +
100                                      PLDM_GET_ALERT_STATUS_RESP_BYTES);
101     auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
102 
103     auto rc =
104         encode_get_alert_status_resp(0, PLDM_SUCCESS, rack_entry, pri_cec_node,
105                                      response, responseMsg.size() - hdrSize);
106 
107     EXPECT_EQ(rc, PLDM_SUCCESS);
108     struct pldm_get_alert_status_resp* resp =
109         reinterpret_cast<struct pldm_get_alert_status_resp*>(response->payload);
110 
111     EXPECT_EQ(completionCode, resp->completion_code);
112     EXPECT_EQ(rack_entry, le32toh(resp->rack_entry));
113     EXPECT_EQ(pri_cec_node, le32toh(resp->pri_cec_node));
114 }
115 
116 TEST(GetAlertStatus, testBadEncodeResponse)
117 {
118     uint32_t rack_entry = 0xff000030;
119     uint32_t pri_cec_node = 0x00008030;
120 
121     std::vector<uint8_t> responseMsg(hdrSize +
122                                      PLDM_GET_ALERT_STATUS_RESP_BYTES);
123     auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
124 
125     auto rc = encode_get_alert_status_resp(0, PLDM_SUCCESS, rack_entry,
126                                            pri_cec_node, response,
127                                            responseMsg.size() - hdrSize + 1);
128 
129     EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
130 }
131 
132 TEST(GetAlertStatus, testGoodDecodeRequest)
133 {
134     std::array<uint8_t, hdrSize + PLDM_GET_ALERT_STATUS_REQ_BYTES> requestMsg{};
135 
136     uint8_t versionId = 0x0;
137     uint8_t retVersionId;
138 
139     auto req = reinterpret_cast<pldm_msg*>(requestMsg.data());
140 
141     req->payload[0] = versionId;
142 
143     auto rc = decode_get_alert_status_req(req, requestMsg.size() - hdrSize,
144                                           &retVersionId);
145 
146     EXPECT_EQ(rc, PLDM_SUCCESS);
147     EXPECT_EQ(retVersionId, versionId);
148 }
149 
150 TEST(GetAlertStatus, testBadDecodeRequest)
151 {
152     std::array<uint8_t, hdrSize + PLDM_GET_ALERT_STATUS_REQ_BYTES> requestMsg{};
153 
154     uint8_t versionId = 0x0;
155     uint8_t retVersionId;
156 
157     auto req = reinterpret_cast<pldm_msg*>(requestMsg.data());
158 
159     req->payload[0] = versionId;
160 
161     auto rc = decode_get_alert_status_req(req, requestMsg.size() - hdrSize + 1,
162                                           &retVersionId);
163 
164     EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
165 }
166