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