blob: 1010bd85335f213f18e365266d9bdfe8e2428ce8 [file] [log] [blame]
Sampa Misra0db1dfa2019-03-19 00:15:31 -05001#include <string.h>
2
3#include <array>
4
5#include "libpldm/base.h"
6#include "libpldm/platform.h"
7
8#include <gtest/gtest.h>
9
Zahed Hossain223a73d2019-07-04 12:46:18 -050010constexpr auto hdrSize = sizeof(pldm_msg_hdr);
11
Sampa Misra0db1dfa2019-03-19 00:15:31 -050012TEST(SetStateEffecterStates, testEncodeResponse)
13{
vkaverapa6575b82019-04-03 05:33:52 -050014 std::array<uint8_t,
15 sizeof(pldm_msg_hdr) + PLDM_SET_STATE_EFFECTER_STATES_RESP_BYTES>
16 responseMsg{};
17 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
Sampa Misra0db1dfa2019-03-19 00:15:31 -050018 uint8_t completionCode = 0;
19
vkaverapa6575b82019-04-03 05:33:52 -050020 auto rc = encode_set_state_effecter_states_resp(0, PLDM_SUCCESS, response);
Sampa Misra0db1dfa2019-03-19 00:15:31 -050021
22 ASSERT_EQ(rc, PLDM_SUCCESS);
vkaverapa6575b82019-04-03 05:33:52 -050023 ASSERT_EQ(completionCode, response->payload[0]);
Sampa Misra0db1dfa2019-03-19 00:15:31 -050024}
25
vkaverap98a2c192019-04-03 05:33:52 -050026TEST(SetStateEffecterStates, testEncodeRequest)
27{
vkaverapa6575b82019-04-03 05:33:52 -050028 std::array<uint8_t,
29 sizeof(pldm_msg_hdr) + PLDM_SET_STATE_EFFECTER_STATES_REQ_BYTES>
30 requestMsg{};
31 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
32
vkaverap98a2c192019-04-03 05:33:52 -050033 uint16_t effecterId = 0x0A;
34 uint8_t compEffecterCnt = 0x2;
35 std::array<set_effecter_state_field, 8> stateField{};
36 stateField[0] = {PLDM_REQUEST_SET, 2};
37 stateField[1] = {PLDM_REQUEST_SET, 3};
38
vkaverap98a2c192019-04-03 05:33:52 -050039 auto rc = encode_set_state_effecter_states_req(
vkaverapa6575b82019-04-03 05:33:52 -050040 0, effecterId, compEffecterCnt, stateField.data(), request);
vkaverap98a2c192019-04-03 05:33:52 -050041
42 ASSERT_EQ(rc, PLDM_SUCCESS);
vkaverapa6575b82019-04-03 05:33:52 -050043 ASSERT_EQ(effecterId, request->payload[0]);
44 ASSERT_EQ(compEffecterCnt, request->payload[sizeof(effecterId)]);
45 ASSERT_EQ(stateField[0].set_request,
46 request->payload[sizeof(effecterId) + sizeof(compEffecterCnt)]);
47 ASSERT_EQ(stateField[0].effecter_state,
48 request->payload[sizeof(effecterId) + sizeof(compEffecterCnt) +
49 sizeof(stateField[0].set_request)]);
50 ASSERT_EQ(stateField[1].set_request,
51 request->payload[sizeof(effecterId) + sizeof(compEffecterCnt) +
52 sizeof(stateField[0])]);
53 ASSERT_EQ(stateField[1].effecter_state,
54 request->payload[sizeof(effecterId) + sizeof(compEffecterCnt) +
55 sizeof(stateField[0]) +
56 sizeof(stateField[1].set_request)]);
vkaverap98a2c192019-04-03 05:33:52 -050057}
58
59TEST(SetStateEffecterStates, testGoodDecodeResponse)
60{
Zahed Hossain223a73d2019-07-04 12:46:18 -050061 std::array<uint8_t, hdrSize + PLDM_SET_STATE_EFFECTER_STATES_RESP_BYTES>
vkaverap98a2c192019-04-03 05:33:52 -050062 responseMsg{};
63
vkaverap98a2c192019-04-03 05:33:52 -050064 uint8_t completion_code = 0xA0;
65
66 uint8_t retcompletion_code = 0;
67
Zahed Hossain223a73d2019-07-04 12:46:18 -050068 memcpy(responseMsg.data() + hdrSize, &completion_code,
69 sizeof(completion_code));
70
71 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
vkaverap98a2c192019-04-03 05:33:52 -050072
vkaverapa6575b82019-04-03 05:33:52 -050073 auto rc = decode_set_state_effecter_states_resp(
Zahed Hossain223a73d2019-07-04 12:46:18 -050074 response, responseMsg.size() - hdrSize, &retcompletion_code);
vkaverap98a2c192019-04-03 05:33:52 -050075
76 ASSERT_EQ(rc, PLDM_SUCCESS);
77 ASSERT_EQ(completion_code, retcompletion_code);
78}
79
Sampa Misra0db1dfa2019-03-19 00:15:31 -050080TEST(SetStateEffecterStates, testGoodDecodeRequest)
81{
Zahed Hossain223a73d2019-07-04 12:46:18 -050082 std::array<uint8_t, hdrSize + PLDM_SET_STATE_EFFECTER_STATES_REQ_BYTES>
83 requestMsg{};
Sampa Misra0db1dfa2019-03-19 00:15:31 -050084
Sampa Misra0db1dfa2019-03-19 00:15:31 -050085 uint16_t effecterId = 0x32;
86 uint8_t compEffecterCnt = 0x2;
87
88 std::array<set_effecter_state_field, 8> stateField{};
89 stateField[0] = {PLDM_REQUEST_SET, 3};
90 stateField[1] = {PLDM_REQUEST_SET, 4};
91
92 uint16_t retEffecterId = 0;
93 uint8_t retCompEffecterCnt = 0;
94
95 std::array<set_effecter_state_field, 8> retStateField{};
96
Zahed Hossain223a73d2019-07-04 12:46:18 -050097 memcpy(requestMsg.data() + hdrSize, &effecterId, sizeof(effecterId));
98 memcpy(requestMsg.data() + sizeof(effecterId) + hdrSize, &compEffecterCnt,
Sampa Misra0db1dfa2019-03-19 00:15:31 -050099 sizeof(compEffecterCnt));
Zahed Hossain223a73d2019-07-04 12:46:18 -0500100 memcpy(requestMsg.data() + sizeof(effecterId) + sizeof(compEffecterCnt) +
101 hdrSize,
Sampa Misra0db1dfa2019-03-19 00:15:31 -0500102 &stateField, sizeof(stateField));
103
Zahed Hossain223a73d2019-07-04 12:46:18 -0500104 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
105
Sampa Misra0db1dfa2019-03-19 00:15:31 -0500106 auto rc = decode_set_state_effecter_states_req(
Zahed Hossain223a73d2019-07-04 12:46:18 -0500107 request, requestMsg.size() - hdrSize, &retEffecterId,
vkaverapa6575b82019-04-03 05:33:52 -0500108 &retCompEffecterCnt, retStateField.data());
Sampa Misra0db1dfa2019-03-19 00:15:31 -0500109
110 ASSERT_EQ(rc, PLDM_SUCCESS);
111 ASSERT_EQ(effecterId, retEffecterId);
112 ASSERT_EQ(retCompEffecterCnt, compEffecterCnt);
113 ASSERT_EQ(retStateField[0].set_request, stateField[0].set_request);
114 ASSERT_EQ(retStateField[0].effecter_state, stateField[0].effecter_state);
115 ASSERT_EQ(retStateField[1].set_request, stateField[1].set_request);
116 ASSERT_EQ(retStateField[1].effecter_state, stateField[1].effecter_state);
117}
118
119TEST(SetStateEffecterStates, testBadDecodeRequest)
120{
Zahed Hossain223a73d2019-07-04 12:46:18 -0500121 const struct pldm_msg* msg = NULL;
Sampa Misra0db1dfa2019-03-19 00:15:31 -0500122
vkaverapa6575b82019-04-03 05:33:52 -0500123 auto rc = decode_set_state_effecter_states_req(msg, sizeof(*msg), NULL,
124 NULL, NULL);
Sampa Misra0db1dfa2019-03-19 00:15:31 -0500125
126 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
127}
vkaverap98a2c192019-04-03 05:33:52 -0500128
129TEST(SetStateEffecterStates, testBadDecodeResponse)
130{
131 std::array<uint8_t, PLDM_SET_STATE_EFFECTER_STATES_RESP_BYTES>
132 responseMsg{};
133
Zahed Hossain223a73d2019-07-04 12:46:18 -0500134 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
135
136 auto rc = decode_set_state_effecter_states_resp(response,
vkaverapa6575b82019-04-03 05:33:52 -0500137 responseMsg.size(), NULL);
vkaverap98a2c192019-04-03 05:33:52 -0500138
139 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
140}