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 <new>
9 #include <vector>
10
11 #include <gmock/gmock.h>
12 #include <gtest/gtest.h>
13
TEST(GetAlertStatus,testGoodEncodeRequest)14 TEST(GetAlertStatus, testGoodEncodeRequest)
15 {
16 PLDM_MSG_DEFINE_P(request, PLDM_GET_ALERT_STATUS_REQ_BYTES);
17
18 uint8_t versionId = 0x0;
19
20 auto rc = encode_get_alert_status_req(0, versionId, request,
21 PLDM_GET_ALERT_STATUS_REQ_BYTES);
22 EXPECT_EQ(rc, PLDM_SUCCESS);
23 EXPECT_EQ(versionId, request->payload[0]);
24 }
25
TEST(GetAlertStatus,testBadEncodeRequest)26 TEST(GetAlertStatus, testBadEncodeRequest)
27 {
28 PLDM_MSG_DEFINE_P(request, PLDM_GET_ALERT_STATUS_REQ_BYTES);
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
TEST(GetAlertStatus,testGoodDecodeResponse)34 TEST(GetAlertStatus, testGoodDecodeResponse)
35 {
36 uint8_t completionCode = PLDM_SUCCESS;
37 uint32_t rack_entry = 0xff000030;
38 uint32_t pri_cec_node = 0x00008030;
39
40 PLDM_MSG_DEFINE_P(response, PLDM_GET_ALERT_STATUS_RESP_BYTES);
41 auto* resp = new (response->payload) pldm_get_alert_status_resp;
42 resp->completion_code = completionCode;
43 resp->rack_entry = htole32(rack_entry);
44 resp->pri_cec_node = htole32(pri_cec_node);
45
46 uint8_t retCompletionCode = 0;
47 uint32_t retRack_entry = 0;
48 uint32_t retPri_cec_node = 0;
49
50 auto rc = decode_get_alert_status_resp(
51 response, PLDM_GET_ALERT_STATUS_RESP_BYTES, &retCompletionCode,
52 &retRack_entry, &retPri_cec_node);
53 EXPECT_EQ(rc, PLDM_SUCCESS);
54 EXPECT_EQ(retCompletionCode, completionCode);
55 EXPECT_EQ(retRack_entry, rack_entry);
56 EXPECT_EQ(retPri_cec_node, pri_cec_node);
57 }
58
TEST(GetAlertStatus,testBadDecodeResponse)59 TEST(GetAlertStatus, testBadDecodeResponse)
60 {
61 auto rc = decode_get_alert_status_resp(NULL, 0, NULL, NULL, NULL);
62 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
63
64 uint8_t completionCode = PLDM_SUCCESS;
65 uint32_t rack_entry = 0xff000030;
66 uint32_t pri_cec_node = 0x00008030;
67
68 PLDM_MSG_DEFINE_P(response, PLDM_GET_ALERT_STATUS_RESP_BYTES);
69 auto* resp = new (response->payload) pldm_get_alert_status_resp;
70 resp->completion_code = completionCode;
71 resp->rack_entry = htole32(rack_entry);
72 resp->pri_cec_node = htole32(pri_cec_node);
73
74 uint8_t retCompletionCode = 0;
75 uint32_t retRack_entry = 0;
76 uint32_t retPri_cec_node = 0;
77
78 rc = decode_get_alert_status_resp(
79 response, PLDM_GET_ALERT_STATUS_RESP_BYTES + 1, &retCompletionCode,
80 &retRack_entry, &retPri_cec_node);
81 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
82 }
83
TEST(GetAlertStatus,testGoodEncodeResponse)84 TEST(GetAlertStatus, testGoodEncodeResponse)
85 {
86 uint32_t rack_entry = 0xff000030;
87 uint32_t pri_cec_node = 0x00008030;
88
89 PLDM_MSG_DEFINE_P(response, PLDM_GET_ALERT_STATUS_RESP_BYTES);
90
91 auto rc = encode_get_alert_status_resp(0, PLDM_SUCCESS, rack_entry,
92 pri_cec_node, response,
93 PLDM_GET_ALERT_STATUS_RESP_BYTES);
94
95 ASSERT_EQ(rc, PLDM_SUCCESS);
96 EXPECT_THAT(response_buf, testing::ElementsAreArray(
97 {0x00, 0x3f, 0xf0, 0x00, 0x30, 0x00, 0x00,
98 0xff, 0x30, 0x80, 0x00, 0x00}));
99 }
100
TEST(GetAlertStatus,testBadEncodeResponse)101 TEST(GetAlertStatus, testBadEncodeResponse)
102 {
103 uint32_t rack_entry = 0xff000030;
104 uint32_t pri_cec_node = 0x00008030;
105
106 PLDM_MSG_DEFINE_P(response, PLDM_GET_ALERT_STATUS_RESP_BYTES);
107
108 auto rc = encode_get_alert_status_resp(
109 0, PLDM_SUCCESS, rack_entry, pri_cec_node, response,
110 PLDM_GET_ALERT_STATUS_RESP_BYTES + 1);
111
112 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
113 }
114
TEST(GetAlertStatus,testGoodDecodeRequest)115 TEST(GetAlertStatus, testGoodDecodeRequest)
116 {
117 uint8_t versionId = 0x0;
118 uint8_t retVersionId;
119
120 PLDM_MSG_DEFINE_P(req, PLDM_GET_ALERT_STATUS_REQ_BYTES);
121 req->payload[0] = versionId;
122
123 auto rc = decode_get_alert_status_req(req, PLDM_GET_ALERT_STATUS_REQ_BYTES,
124 &retVersionId);
125
126 EXPECT_EQ(rc, PLDM_SUCCESS);
127 EXPECT_EQ(retVersionId, versionId);
128 }
129
TEST(GetAlertStatus,testBadDecodeRequest)130 TEST(GetAlertStatus, testBadDecodeRequest)
131 {
132 uint8_t versionId = 0x0;
133 uint8_t retVersionId;
134
135 PLDM_MSG_DEFINE_P(req, PLDM_GET_ALERT_STATUS_REQ_BYTES);
136 req->payload[0] = versionId;
137
138 auto rc = decode_get_alert_status_req(
139 req, PLDM_GET_ALERT_STATUS_REQ_BYTES + 1, &retVersionId);
140
141 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
142 }
143