Deepak Kodihalli | d130e1a | 2020-06-17 05:55:32 -0500 | [diff] [blame] | 1 | #include "common/utils.hpp" |
TOM JOSEPH | d4d97a5 | 2020-03-23 14:36:34 +0530 | [diff] [blame] | 2 | #include "libpldmresponder/event_parser.hpp" |
Deepak Kodihalli | 557dfb0 | 2019-05-12 13:11:17 +0530 | [diff] [blame] | 3 | #include "libpldmresponder/pdr.hpp" |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 4 | #include "libpldmresponder/pdr_utils.hpp" |
Deepak Kodihalli | 557dfb0 | 2019-05-12 13:11:17 +0530 | [diff] [blame] | 5 | #include "libpldmresponder/platform.hpp" |
George Liu | 64d26be | 2020-01-15 10:58:10 +0800 | [diff] [blame] | 6 | #include "libpldmresponder/platform_numeric_effecter.hpp" |
George Liu | 0d7aca8 | 2020-03-30 15:01:36 +0800 | [diff] [blame] | 7 | #include "libpldmresponder/platform_state_effecter.hpp" |
George Liu | 1e44c73 | 2020-02-28 20:20:06 +0800 | [diff] [blame] | 8 | #include "mocked_utils.hpp" |
Deepak Kodihalli | 557dfb0 | 2019-05-12 13:11:17 +0530 | [diff] [blame] | 9 | |
George Liu | 36e8135 | 2020-07-01 14:40:30 +0800 | [diff] [blame] | 10 | #include <sdbusplus/test/sdbus_mock.hpp> |
| 11 | |
Deepak Kodihalli | 557dfb0 | 2019-05-12 13:11:17 +0530 | [diff] [blame] | 12 | #include <iostream> |
| 13 | |
George Liu | 1e44c73 | 2020-02-28 20:20:06 +0800 | [diff] [blame] | 14 | using namespace pldm::utils; |
Deepak Kodihalli | 557dfb0 | 2019-05-12 13:11:17 +0530 | [diff] [blame] | 15 | using namespace pldm::responder; |
Deepak Kodihalli | c682fe2 | 2020-03-04 00:42:54 -0600 | [diff] [blame] | 16 | using namespace pldm::responder::platform; |
| 17 | using namespace pldm::responder::pdr; |
| 18 | using namespace pldm::responder::pdr_utils; |
Deepak Kodihalli | 557dfb0 | 2019-05-12 13:11:17 +0530 | [diff] [blame] | 19 | |
George Liu | 36e8135 | 2020-07-01 14:40:30 +0800 | [diff] [blame] | 20 | using ::testing::_; |
| 21 | using ::testing::Return; |
| 22 | using ::testing::StrEq; |
| 23 | |
Deepak Kodihalli | 557dfb0 | 2019-05-12 13:11:17 +0530 | [diff] [blame] | 24 | TEST(getPDR, testGoodPath) |
| 25 | { |
| 26 | std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_PDR_REQ_BYTES> |
| 27 | requestPayload{}; |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 28 | auto req = reinterpret_cast<pldm_msg*>(requestPayload.data()); |
Deepak Kodihalli | 557dfb0 | 2019-05-12 13:11:17 +0530 | [diff] [blame] | 29 | size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr); |
| 30 | |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 31 | struct pldm_get_pdr_req* request = |
| 32 | reinterpret_cast<struct pldm_get_pdr_req*>(req->payload); |
| 33 | request->request_count = 100; |
| 34 | |
George Liu | 36e8135 | 2020-07-01 14:40:30 +0800 | [diff] [blame] | 35 | MockdBusHandler mockedUtils; |
| 36 | EXPECT_CALL(mockedUtils, getService(StrEq("/foo/bar"), _)) |
| 37 | .Times(5) |
| 38 | .WillRepeatedly(Return("foo.bar")); |
| 39 | |
Deepak Kodihalli | c682fe2 | 2020-03-04 00:42:54 -0600 | [diff] [blame] | 40 | auto pdrRepo = pldm_pdr_init(); |
Deepak Kodihalli | b5c227e | 2020-07-13 06:58:34 -0500 | [diff] [blame] | 41 | Handler handler(&mockedUtils, "./pdr_jsons/state_effecter/good", |
George Liu | 36e8135 | 2020-07-01 14:40:30 +0800 | [diff] [blame] | 42 | "./event_jsons/good", pdrRepo, nullptr, nullptr); |
Deepak Kodihalli | c682fe2 | 2020-03-04 00:42:54 -0600 | [diff] [blame] | 43 | Repo repo(pdrRepo); |
| 44 | ASSERT_EQ(repo.empty(), false); |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 45 | auto response = handler.getPDR(req, requestPayloadLength); |
Deepak Kodihalli | 557dfb0 | 2019-05-12 13:11:17 +0530 | [diff] [blame] | 46 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 47 | |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 48 | struct pldm_get_pdr_resp* resp = |
| 49 | reinterpret_cast<struct pldm_get_pdr_resp*>(responsePtr->payload); |
| 50 | ASSERT_EQ(PLDM_SUCCESS, resp->completion_code); |
| 51 | ASSERT_EQ(2, resp->next_record_handle); |
| 52 | ASSERT_EQ(true, resp->response_count != 0); |
| 53 | |
| 54 | pldm_pdr_hdr* hdr = reinterpret_cast<pldm_pdr_hdr*>(resp->record_data); |
| 55 | ASSERT_EQ(hdr->record_handle, 1); |
| 56 | ASSERT_EQ(hdr->version, 1); |
Deepak Kodihalli | c682fe2 | 2020-03-04 00:42:54 -0600 | [diff] [blame] | 57 | |
| 58 | pldm_pdr_destroy(pdrRepo); |
Deepak Kodihalli | 557dfb0 | 2019-05-12 13:11:17 +0530 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | TEST(getPDR, testShortRead) |
| 62 | { |
| 63 | std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_PDR_REQ_BYTES> |
| 64 | requestPayload{}; |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 65 | auto req = reinterpret_cast<pldm_msg*>(requestPayload.data()); |
Deepak Kodihalli | 557dfb0 | 2019-05-12 13:11:17 +0530 | [diff] [blame] | 66 | size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr); |
| 67 | |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 68 | struct pldm_get_pdr_req* request = |
| 69 | reinterpret_cast<struct pldm_get_pdr_req*>(req->payload); |
| 70 | request->request_count = 1; |
| 71 | |
George Liu | 36e8135 | 2020-07-01 14:40:30 +0800 | [diff] [blame] | 72 | MockdBusHandler mockedUtils; |
| 73 | EXPECT_CALL(mockedUtils, getService(StrEq("/foo/bar"), _)) |
| 74 | .Times(5) |
| 75 | .WillRepeatedly(Return("foo.bar")); |
| 76 | |
Deepak Kodihalli | c682fe2 | 2020-03-04 00:42:54 -0600 | [diff] [blame] | 77 | auto pdrRepo = pldm_pdr_init(); |
Deepak Kodihalli | b5c227e | 2020-07-13 06:58:34 -0500 | [diff] [blame] | 78 | Handler handler(&mockedUtils, "./pdr_jsons/state_effecter/good", |
George Liu | 36e8135 | 2020-07-01 14:40:30 +0800 | [diff] [blame] | 79 | "./event_jsons/good", pdrRepo, nullptr, nullptr); |
Deepak Kodihalli | c682fe2 | 2020-03-04 00:42:54 -0600 | [diff] [blame] | 80 | Repo repo(pdrRepo); |
| 81 | ASSERT_EQ(repo.empty(), false); |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 82 | auto response = handler.getPDR(req, requestPayloadLength); |
Deepak Kodihalli | 557dfb0 | 2019-05-12 13:11:17 +0530 | [diff] [blame] | 83 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 84 | struct pldm_get_pdr_resp* resp = |
| 85 | reinterpret_cast<struct pldm_get_pdr_resp*>(responsePtr->payload); |
| 86 | ASSERT_EQ(PLDM_SUCCESS, resp->completion_code); |
| 87 | ASSERT_EQ(1, resp->response_count); |
Deepak Kodihalli | c682fe2 | 2020-03-04 00:42:54 -0600 | [diff] [blame] | 88 | pldm_pdr_destroy(pdrRepo); |
Deepak Kodihalli | 557dfb0 | 2019-05-12 13:11:17 +0530 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | TEST(getPDR, testBadRecordHandle) |
| 92 | { |
| 93 | std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_PDR_REQ_BYTES> |
| 94 | requestPayload{}; |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 95 | auto req = reinterpret_cast<pldm_msg*>(requestPayload.data()); |
Deepak Kodihalli | 557dfb0 | 2019-05-12 13:11:17 +0530 | [diff] [blame] | 96 | size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr); |
| 97 | |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 98 | struct pldm_get_pdr_req* request = |
| 99 | reinterpret_cast<struct pldm_get_pdr_req*>(req->payload); |
| 100 | request->record_handle = 100000; |
| 101 | request->request_count = 1; |
| 102 | |
George Liu | 36e8135 | 2020-07-01 14:40:30 +0800 | [diff] [blame] | 103 | MockdBusHandler mockedUtils; |
| 104 | EXPECT_CALL(mockedUtils, getService(StrEq("/foo/bar"), _)) |
| 105 | .Times(5) |
| 106 | .WillRepeatedly(Return("foo.bar")); |
| 107 | |
Deepak Kodihalli | c682fe2 | 2020-03-04 00:42:54 -0600 | [diff] [blame] | 108 | auto pdrRepo = pldm_pdr_init(); |
Deepak Kodihalli | b5c227e | 2020-07-13 06:58:34 -0500 | [diff] [blame] | 109 | Handler handler(&mockedUtils, "./pdr_jsons/state_effecter/good", |
George Liu | 36e8135 | 2020-07-01 14:40:30 +0800 | [diff] [blame] | 110 | "./event_jsons/good", pdrRepo, nullptr, nullptr); |
Deepak Kodihalli | c682fe2 | 2020-03-04 00:42:54 -0600 | [diff] [blame] | 111 | Repo repo(pdrRepo); |
| 112 | ASSERT_EQ(repo.empty(), false); |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 113 | auto response = handler.getPDR(req, requestPayloadLength); |
Deepak Kodihalli | 557dfb0 | 2019-05-12 13:11:17 +0530 | [diff] [blame] | 114 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 115 | |
| 116 | ASSERT_EQ(responsePtr->payload[0], PLDM_PLATFORM_INVALID_RECORD_HANDLE); |
Deepak Kodihalli | c682fe2 | 2020-03-04 00:42:54 -0600 | [diff] [blame] | 117 | |
| 118 | pldm_pdr_destroy(pdrRepo); |
Deepak Kodihalli | 557dfb0 | 2019-05-12 13:11:17 +0530 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | TEST(getPDR, testNoNextRecord) |
| 122 | { |
| 123 | std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_PDR_REQ_BYTES> |
| 124 | requestPayload{}; |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 125 | auto req = reinterpret_cast<pldm_msg*>(requestPayload.data()); |
Deepak Kodihalli | 557dfb0 | 2019-05-12 13:11:17 +0530 | [diff] [blame] | 126 | size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr); |
| 127 | |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 128 | struct pldm_get_pdr_req* request = |
| 129 | reinterpret_cast<struct pldm_get_pdr_req*>(req->payload); |
| 130 | request->record_handle = 1; |
| 131 | |
George Liu | 36e8135 | 2020-07-01 14:40:30 +0800 | [diff] [blame] | 132 | MockdBusHandler mockedUtils; |
| 133 | EXPECT_CALL(mockedUtils, getService(StrEq("/foo/bar"), _)) |
| 134 | .Times(5) |
| 135 | .WillRepeatedly(Return("foo.bar")); |
| 136 | |
Deepak Kodihalli | c682fe2 | 2020-03-04 00:42:54 -0600 | [diff] [blame] | 137 | auto pdrRepo = pldm_pdr_init(); |
Deepak Kodihalli | b5c227e | 2020-07-13 06:58:34 -0500 | [diff] [blame] | 138 | Handler handler(&mockedUtils, "./pdr_jsons/state_effecter/good", |
George Liu | 36e8135 | 2020-07-01 14:40:30 +0800 | [diff] [blame] | 139 | "./event_jsons/good", pdrRepo, nullptr, nullptr); |
Deepak Kodihalli | c682fe2 | 2020-03-04 00:42:54 -0600 | [diff] [blame] | 140 | Repo repo(pdrRepo); |
| 141 | ASSERT_EQ(repo.empty(), false); |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 142 | auto response = handler.getPDR(req, requestPayloadLength); |
Deepak Kodihalli | 557dfb0 | 2019-05-12 13:11:17 +0530 | [diff] [blame] | 143 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 144 | struct pldm_get_pdr_resp* resp = |
| 145 | reinterpret_cast<struct pldm_get_pdr_resp*>(responsePtr->payload); |
| 146 | ASSERT_EQ(PLDM_SUCCESS, resp->completion_code); |
| 147 | ASSERT_EQ(2, resp->next_record_handle); |
Deepak Kodihalli | c682fe2 | 2020-03-04 00:42:54 -0600 | [diff] [blame] | 148 | |
| 149 | pldm_pdr_destroy(pdrRepo); |
Deepak Kodihalli | 557dfb0 | 2019-05-12 13:11:17 +0530 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | TEST(getPDR, testFindPDR) |
| 153 | { |
| 154 | std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_PDR_REQ_BYTES> |
| 155 | requestPayload{}; |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 156 | auto req = reinterpret_cast<pldm_msg*>(requestPayload.data()); |
Deepak Kodihalli | 557dfb0 | 2019-05-12 13:11:17 +0530 | [diff] [blame] | 157 | size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr); |
| 158 | |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 159 | struct pldm_get_pdr_req* request = |
| 160 | reinterpret_cast<struct pldm_get_pdr_req*>(req->payload); |
| 161 | request->request_count = 100; |
| 162 | |
George Liu | 36e8135 | 2020-07-01 14:40:30 +0800 | [diff] [blame] | 163 | MockdBusHandler mockedUtils; |
| 164 | EXPECT_CALL(mockedUtils, getService(StrEq("/foo/bar"), _)) |
| 165 | .Times(5) |
| 166 | .WillRepeatedly(Return("foo.bar")); |
| 167 | |
Deepak Kodihalli | c682fe2 | 2020-03-04 00:42:54 -0600 | [diff] [blame] | 168 | auto pdrRepo = pldm_pdr_init(); |
Deepak Kodihalli | b5c227e | 2020-07-13 06:58:34 -0500 | [diff] [blame] | 169 | Handler handler(&mockedUtils, "./pdr_jsons/state_effecter/good", |
George Liu | 36e8135 | 2020-07-01 14:40:30 +0800 | [diff] [blame] | 170 | "./event_jsons/good", pdrRepo, nullptr, nullptr); |
Deepak Kodihalli | c682fe2 | 2020-03-04 00:42:54 -0600 | [diff] [blame] | 171 | Repo repo(pdrRepo); |
| 172 | ASSERT_EQ(repo.empty(), false); |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 173 | auto response = handler.getPDR(req, requestPayloadLength); |
Deepak Kodihalli | 557dfb0 | 2019-05-12 13:11:17 +0530 | [diff] [blame] | 174 | |
| 175 | // Let's try to find a PDR of type stateEffecter (= 11) and entity type = |
| 176 | // 100 |
| 177 | bool found = false; |
| 178 | uint32_t handle = 0; // start asking for PDRs from recordHandle 0 |
Deepak Kodihalli | 557dfb0 | 2019-05-12 13:11:17 +0530 | [diff] [blame] | 179 | while (!found) |
| 180 | { |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 181 | request->record_handle = handle; |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 182 | auto response = handler.getPDR(req, requestPayloadLength); |
Deepak Kodihalli | 557dfb0 | 2019-05-12 13:11:17 +0530 | [diff] [blame] | 183 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 184 | struct pldm_get_pdr_resp* resp = |
| 185 | reinterpret_cast<struct pldm_get_pdr_resp*>(responsePtr->payload); |
| 186 | ASSERT_EQ(PLDM_SUCCESS, resp->completion_code); |
Deepak Kodihalli | 557dfb0 | 2019-05-12 13:11:17 +0530 | [diff] [blame] | 187 | |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 188 | handle = resp->next_record_handle; // point to the next pdr in case |
| 189 | // current is not what we want |
| 190 | |
| 191 | pldm_pdr_hdr* hdr = reinterpret_cast<pldm_pdr_hdr*>(resp->record_data); |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 192 | std::cerr << "PDR next record handle " << handle << "\n"; |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 193 | std::cerr << "PDR type " << hdr->type << "\n"; |
Deepak Kodihalli | 557dfb0 | 2019-05-12 13:11:17 +0530 | [diff] [blame] | 194 | if (hdr->type == PLDM_STATE_EFFECTER_PDR) |
| 195 | { |
| 196 | pldm_state_effecter_pdr* pdr = |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 197 | reinterpret_cast<pldm_state_effecter_pdr*>(resp->record_data); |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 198 | std::cerr << "PDR entity type " << pdr->entity_type << "\n"; |
Deepak Kodihalli | 557dfb0 | 2019-05-12 13:11:17 +0530 | [diff] [blame] | 199 | if (pdr->entity_type == 100) |
| 200 | { |
| 201 | found = true; |
| 202 | // Rest of the PDR can be accessed as need be |
| 203 | break; |
| 204 | } |
| 205 | } |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 206 | if (!resp->next_record_handle) // no more records |
Deepak Kodihalli | 557dfb0 | 2019-05-12 13:11:17 +0530 | [diff] [blame] | 207 | { |
| 208 | break; |
| 209 | } |
| 210 | } |
| 211 | ASSERT_EQ(found, true); |
Deepak Kodihalli | c682fe2 | 2020-03-04 00:42:54 -0600 | [diff] [blame] | 212 | |
| 213 | pldm_pdr_destroy(pdrRepo); |
Deepak Kodihalli | 557dfb0 | 2019-05-12 13:11:17 +0530 | [diff] [blame] | 214 | } |
Sampa Misra | a2fa070 | 2019-05-31 01:28:55 -0500 | [diff] [blame] | 215 | |
Sampa Misra | a2fa070 | 2019-05-31 01:28:55 -0500 | [diff] [blame] | 216 | TEST(setStateEffecterStatesHandler, testGoodRequest) |
| 217 | { |
George Liu | 36e8135 | 2020-07-01 14:40:30 +0800 | [diff] [blame] | 218 | MockdBusHandler mockedUtils; |
| 219 | EXPECT_CALL(mockedUtils, getService(StrEq("/foo/bar"), _)) |
| 220 | .Times(5) |
| 221 | .WillRepeatedly(Return("foo.bar")); |
| 222 | |
Deepak Kodihalli | c682fe2 | 2020-03-04 00:42:54 -0600 | [diff] [blame] | 223 | auto inPDRRepo = pldm_pdr_init(); |
| 224 | auto outPDRRepo = pldm_pdr_init(); |
| 225 | Repo outRepo(outPDRRepo); |
Deepak Kodihalli | b5c227e | 2020-07-13 06:58:34 -0500 | [diff] [blame] | 226 | Handler handler(&mockedUtils, "./pdr_jsons/state_effecter/good", |
George Liu | 36e8135 | 2020-07-01 14:40:30 +0800 | [diff] [blame] | 227 | "./event_jsons/good", inPDRRepo, nullptr, nullptr); |
Deepak Kodihalli | c682fe2 | 2020-03-04 00:42:54 -0600 | [diff] [blame] | 228 | Repo inRepo(inPDRRepo); |
| 229 | getRepoByType(inRepo, outRepo, PLDM_STATE_EFFECTER_PDR); |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 230 | pdr_utils::PdrEntry e; |
Deepak Kodihalli | c682fe2 | 2020-03-04 00:42:54 -0600 | [diff] [blame] | 231 | auto record1 = pdr::getRecordByHandle(outRepo, 1, e); |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 232 | ASSERT_NE(record1, nullptr); |
Sampa Misra | a2fa070 | 2019-05-31 01:28:55 -0500 | [diff] [blame] | 233 | pldm_state_effecter_pdr* pdr = |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 234 | reinterpret_cast<pldm_state_effecter_pdr*>(e.data); |
Sampa Misra | a2fa070 | 2019-05-31 01:28:55 -0500 | [diff] [blame] | 235 | EXPECT_EQ(pdr->hdr.type, PLDM_STATE_EFFECTER_PDR); |
| 236 | |
| 237 | std::vector<set_effecter_state_field> stateField; |
| 238 | stateField.push_back({PLDM_REQUEST_SET, 1}); |
| 239 | stateField.push_back({PLDM_REQUEST_SET, 1}); |
George Liu | 1ec85d4 | 2020-02-12 16:05:32 +0800 | [diff] [blame] | 240 | std::string value = "xyz.openbmc_project.Foo.Bar.V1"; |
George Liu | 1e44c73 | 2020-02-28 20:20:06 +0800 | [diff] [blame] | 241 | PropertyValue propertyValue = value; |
Sampa Misra | a2fa070 | 2019-05-31 01:28:55 -0500 | [diff] [blame] | 242 | |
George Liu | 1ec85d4 | 2020-02-12 16:05:32 +0800 | [diff] [blame] | 243 | DBusMapping dbusMapping{"/foo/bar", "xyz.openbmc_project.Foo.Bar", |
| 244 | "propertyName", "string"}; |
George Liu | 1e44c73 | 2020-02-28 20:20:06 +0800 | [diff] [blame] | 245 | |
George Liu | 36e8135 | 2020-07-01 14:40:30 +0800 | [diff] [blame] | 246 | EXPECT_CALL(mockedUtils, setDbusProperty(dbusMapping, propertyValue)) |
Sampa Misra | a2fa070 | 2019-05-31 01:28:55 -0500 | [diff] [blame] | 247 | .Times(2); |
George Liu | 0d7aca8 | 2020-03-30 15:01:36 +0800 | [diff] [blame] | 248 | auto rc = platform_state_effecter::setStateEffecterStatesHandler< |
George Liu | 36e8135 | 2020-07-01 14:40:30 +0800 | [diff] [blame] | 249 | MockdBusHandler, Handler>(mockedUtils, handler, 0x1, stateField); |
Sampa Misra | a2fa070 | 2019-05-31 01:28:55 -0500 | [diff] [blame] | 250 | ASSERT_EQ(rc, 0); |
Deepak Kodihalli | c682fe2 | 2020-03-04 00:42:54 -0600 | [diff] [blame] | 251 | |
| 252 | pldm_pdr_destroy(inPDRRepo); |
| 253 | pldm_pdr_destroy(outPDRRepo); |
Sampa Misra | a2fa070 | 2019-05-31 01:28:55 -0500 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | TEST(setStateEffecterStatesHandler, testBadRequest) |
| 257 | { |
George Liu | 36e8135 | 2020-07-01 14:40:30 +0800 | [diff] [blame] | 258 | MockdBusHandler mockedUtils; |
| 259 | EXPECT_CALL(mockedUtils, getService(StrEq("/foo/bar"), _)) |
| 260 | .Times(5) |
| 261 | .WillRepeatedly(Return("foo.bar")); |
| 262 | |
Deepak Kodihalli | c682fe2 | 2020-03-04 00:42:54 -0600 | [diff] [blame] | 263 | auto inPDRRepo = pldm_pdr_init(); |
| 264 | auto outPDRRepo = pldm_pdr_init(); |
| 265 | Repo outRepo(outPDRRepo); |
Deepak Kodihalli | b5c227e | 2020-07-13 06:58:34 -0500 | [diff] [blame] | 266 | Handler handler(&mockedUtils, "./pdr_jsons/state_effecter/good", |
George Liu | 36e8135 | 2020-07-01 14:40:30 +0800 | [diff] [blame] | 267 | "./event_jsons/good", inPDRRepo, nullptr, nullptr); |
Deepak Kodihalli | c682fe2 | 2020-03-04 00:42:54 -0600 | [diff] [blame] | 268 | Repo inRepo(inPDRRepo); |
| 269 | getRepoByType(inRepo, outRepo, PLDM_STATE_EFFECTER_PDR); |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 270 | pdr_utils::PdrEntry e; |
Deepak Kodihalli | c682fe2 | 2020-03-04 00:42:54 -0600 | [diff] [blame] | 271 | auto record1 = pdr::getRecordByHandle(outRepo, 1, e); |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 272 | ASSERT_NE(record1, nullptr); |
Sampa Misra | a2fa070 | 2019-05-31 01:28:55 -0500 | [diff] [blame] | 273 | pldm_state_effecter_pdr* pdr = |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 274 | reinterpret_cast<pldm_state_effecter_pdr*>(e.data); |
Sampa Misra | a2fa070 | 2019-05-31 01:28:55 -0500 | [diff] [blame] | 275 | EXPECT_EQ(pdr->hdr.type, PLDM_STATE_EFFECTER_PDR); |
| 276 | |
| 277 | std::vector<set_effecter_state_field> stateField; |
| 278 | stateField.push_back({PLDM_REQUEST_SET, 3}); |
| 279 | stateField.push_back({PLDM_REQUEST_SET, 4}); |
| 280 | |
George Liu | 0d7aca8 | 2020-03-30 15:01:36 +0800 | [diff] [blame] | 281 | auto rc = platform_state_effecter::setStateEffecterStatesHandler< |
George Liu | 36e8135 | 2020-07-01 14:40:30 +0800 | [diff] [blame] | 282 | MockdBusHandler, Handler>(mockedUtils, handler, 0x1, stateField); |
Sampa Misra | a2fa070 | 2019-05-31 01:28:55 -0500 | [diff] [blame] | 283 | ASSERT_EQ(rc, PLDM_PLATFORM_SET_EFFECTER_UNSUPPORTED_SENSORSTATE); |
| 284 | |
George Liu | 0d7aca8 | 2020-03-30 15:01:36 +0800 | [diff] [blame] | 285 | rc = platform_state_effecter::setStateEffecterStatesHandler<MockdBusHandler, |
| 286 | Handler>( |
George Liu | 36e8135 | 2020-07-01 14:40:30 +0800 | [diff] [blame] | 287 | mockedUtils, handler, 0x9, stateField); |
Sampa Misra | a2fa070 | 2019-05-31 01:28:55 -0500 | [diff] [blame] | 288 | ASSERT_EQ(rc, PLDM_PLATFORM_INVALID_EFFECTER_ID); |
| 289 | |
| 290 | stateField.push_back({PLDM_REQUEST_SET, 4}); |
George Liu | 0d7aca8 | 2020-03-30 15:01:36 +0800 | [diff] [blame] | 291 | rc = platform_state_effecter::setStateEffecterStatesHandler<MockdBusHandler, |
| 292 | Handler>( |
George Liu | 36e8135 | 2020-07-01 14:40:30 +0800 | [diff] [blame] | 293 | mockedUtils, handler, 0x1, stateField); |
Sampa Misra | a2fa070 | 2019-05-31 01:28:55 -0500 | [diff] [blame] | 294 | ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 295 | |
Deepak Kodihalli | c682fe2 | 2020-03-04 00:42:54 -0600 | [diff] [blame] | 296 | pldm_pdr_destroy(inPDRRepo); |
| 297 | pldm_pdr_destroy(outPDRRepo); |
Sampa Misra | a2fa070 | 2019-05-31 01:28:55 -0500 | [diff] [blame] | 298 | } |
George Liu | 64d26be | 2020-01-15 10:58:10 +0800 | [diff] [blame] | 299 | |
| 300 | TEST(setNumericEffecterValueHandler, testGoodRequest) |
| 301 | { |
George Liu | 36e8135 | 2020-07-01 14:40:30 +0800 | [diff] [blame] | 302 | MockdBusHandler mockedUtils; |
| 303 | EXPECT_CALL(mockedUtils, getService(StrEq("/foo/bar"), _)) |
| 304 | .Times(5) |
| 305 | .WillRepeatedly(Return("foo.bar")); |
| 306 | |
George Liu | 64d26be | 2020-01-15 10:58:10 +0800 | [diff] [blame] | 307 | auto inPDRRepo = pldm_pdr_init(); |
| 308 | auto numericEffecterPdrRepo = pldm_pdr_init(); |
| 309 | Repo numericEffecterPDRs(numericEffecterPdrRepo); |
Deepak Kodihalli | b5c227e | 2020-07-13 06:58:34 -0500 | [diff] [blame] | 310 | Handler handler(&mockedUtils, "./pdr_jsons/state_effecter/good", "", |
George Liu | 36e8135 | 2020-07-01 14:40:30 +0800 | [diff] [blame] | 311 | inPDRRepo, nullptr, nullptr); |
George Liu | 64d26be | 2020-01-15 10:58:10 +0800 | [diff] [blame] | 312 | Repo inRepo(inPDRRepo); |
| 313 | getRepoByType(inRepo, numericEffecterPDRs, PLDM_NUMERIC_EFFECTER_PDR); |
| 314 | |
| 315 | pdr_utils::PdrEntry e; |
| 316 | auto record3 = pdr::getRecordByHandle(numericEffecterPDRs, 3, e); |
| 317 | ASSERT_NE(record3, nullptr); |
| 318 | |
| 319 | pldm_numeric_effecter_value_pdr* pdr = |
| 320 | reinterpret_cast<pldm_numeric_effecter_value_pdr*>(e.data); |
| 321 | EXPECT_EQ(pdr->hdr.type, PLDM_NUMERIC_EFFECTER_PDR); |
| 322 | |
| 323 | uint16_t effecterId = 3; |
| 324 | uint32_t effecterValue = 2100000000; // 2036-07-18 21:20:00 |
| 325 | PropertyValue propertyValue = static_cast<uint32_t>(effecterValue); |
| 326 | |
George Liu | 64d26be | 2020-01-15 10:58:10 +0800 | [diff] [blame] | 327 | DBusMapping dbusMapping{"/foo/bar", "xyz.openbmc_project.Foo.Bar", |
| 328 | "propertyName", "uint64_t"}; |
George Liu | 36e8135 | 2020-07-01 14:40:30 +0800 | [diff] [blame] | 329 | EXPECT_CALL(mockedUtils, setDbusProperty(dbusMapping, propertyValue)) |
George Liu | 64d26be | 2020-01-15 10:58:10 +0800 | [diff] [blame] | 330 | .Times(1); |
| 331 | |
| 332 | auto rc = platform_numeric_effecter::setNumericEffecterValueHandler< |
| 333 | MockdBusHandler, Handler>( |
George Liu | 36e8135 | 2020-07-01 14:40:30 +0800 | [diff] [blame] | 334 | mockedUtils, handler, effecterId, PLDM_EFFECTER_DATA_SIZE_UINT32, |
George Liu | 64d26be | 2020-01-15 10:58:10 +0800 | [diff] [blame] | 335 | reinterpret_cast<uint8_t*>(&effecterValue), 4); |
| 336 | ASSERT_EQ(rc, 0); |
| 337 | |
| 338 | pldm_pdr_destroy(inPDRRepo); |
| 339 | pldm_pdr_destroy(numericEffecterPdrRepo); |
| 340 | } |
| 341 | |
| 342 | TEST(setNumericEffecterValueHandler, testBadRequest) |
| 343 | { |
George Liu | 36e8135 | 2020-07-01 14:40:30 +0800 | [diff] [blame] | 344 | MockdBusHandler mockedUtils; |
| 345 | EXPECT_CALL(mockedUtils, getService(StrEq("/foo/bar"), _)) |
| 346 | .Times(5) |
| 347 | .WillRepeatedly(Return("foo.bar")); |
| 348 | |
George Liu | 64d26be | 2020-01-15 10:58:10 +0800 | [diff] [blame] | 349 | auto inPDRRepo = pldm_pdr_init(); |
| 350 | auto numericEffecterPdrRepo = pldm_pdr_init(); |
| 351 | Repo numericEffecterPDRs(numericEffecterPdrRepo); |
Deepak Kodihalli | b5c227e | 2020-07-13 06:58:34 -0500 | [diff] [blame] | 352 | Handler handler(&mockedUtils, "./pdr_jsons/state_effecter/good", "", |
George Liu | 36e8135 | 2020-07-01 14:40:30 +0800 | [diff] [blame] | 353 | inPDRRepo, nullptr, nullptr); |
George Liu | 64d26be | 2020-01-15 10:58:10 +0800 | [diff] [blame] | 354 | Repo inRepo(inPDRRepo); |
| 355 | getRepoByType(inRepo, numericEffecterPDRs, PLDM_NUMERIC_EFFECTER_PDR); |
| 356 | |
| 357 | pdr_utils::PdrEntry e; |
| 358 | auto record3 = pdr::getRecordByHandle(numericEffecterPDRs, 3, e); |
| 359 | ASSERT_NE(record3, nullptr); |
| 360 | |
| 361 | pldm_numeric_effecter_value_pdr* pdr = |
| 362 | reinterpret_cast<pldm_numeric_effecter_value_pdr*>(e.data); |
| 363 | EXPECT_EQ(pdr->hdr.type, PLDM_NUMERIC_EFFECTER_PDR); |
| 364 | |
| 365 | uint16_t effecterId = 3; |
| 366 | uint64_t effecterValue = 9876543210; |
George Liu | 64d26be | 2020-01-15 10:58:10 +0800 | [diff] [blame] | 367 | auto rc = platform_numeric_effecter::setNumericEffecterValueHandler< |
| 368 | MockdBusHandler, Handler>( |
George Liu | 36e8135 | 2020-07-01 14:40:30 +0800 | [diff] [blame] | 369 | mockedUtils, handler, effecterId, PLDM_EFFECTER_DATA_SIZE_SINT32, |
George Liu | 64d26be | 2020-01-15 10:58:10 +0800 | [diff] [blame] | 370 | reinterpret_cast<uint8_t*>(&effecterValue), 3); |
| 371 | ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 372 | |
| 373 | pldm_pdr_destroy(inPDRRepo); |
| 374 | pldm_pdr_destroy(numericEffecterPdrRepo); |
| 375 | } |
Tom Joseph | b426860 | 2020-04-17 17:20:45 +0530 | [diff] [blame] | 376 | |
| 377 | TEST(parseStateSensor, allScenarios) |
| 378 | { |
| 379 | // Sample state sensor with SensorID - 1, EntityType - Processor Module(67) |
| 380 | // State Set ID - Operational Running Status(11), Supported States - 3,4 |
| 381 | std::vector<uint8_t> sample1PDR{0x00, 0x00, 0x00, 0x00, 0x01, 0x04, 0x00, |
| 382 | 0x00, 0x17, 0x00, 0x00, 0x00, 0x01, 0x00, |
| 383 | 0x43, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, |
| 384 | 0x00, 0x01, 0x0b, 0x00, 0x01, 0x18}; |
| 385 | |
| 386 | const auto& [terminusHandle1, sensorID1, sensorInfo1] = |
| 387 | parseStateSensorPDR(sample1PDR); |
| 388 | const auto& [containerID1, entityType1, entityInstance1] = |
| 389 | std::get<0>(sensorInfo1); |
| 390 | const auto& states1 = std::get<1>(sensorInfo1); |
| 391 | CompositeSensorStates statesCmp1{{3u, 4u}}; |
| 392 | |
| 393 | ASSERT_EQ(le16toh(terminusHandle1), 0u); |
| 394 | ASSERT_EQ(le16toh(sensorID1), 1u); |
| 395 | ASSERT_EQ(le16toh(containerID1), 0u); |
| 396 | ASSERT_EQ(le16toh(entityType1), 67u); |
| 397 | ASSERT_EQ(le16toh(entityInstance1), 1u); |
| 398 | ASSERT_EQ(states1, statesCmp1); |
| 399 | |
| 400 | // Sample state sensor with SensorID - 2, EntityType - System Firmware(31) |
| 401 | // State Set ID - Availability(2), Supported States - 3,4,9,10,11,13 |
| 402 | std::vector<uint8_t> sample2PDR{0x00, 0x00, 0x00, 0x00, 0x01, 0x04, 0x00, |
| 403 | 0x00, 0x17, 0x00, 0x00, 0x00, 0x02, 0x00, |
| 404 | 0x1F, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, |
| 405 | 0x00, 0x01, 0x02, 0x00, 0x02, 0x18, 0x2E}; |
| 406 | |
| 407 | const auto& [terminusHandle2, sensorID2, sensorInfo2] = |
| 408 | parseStateSensorPDR(sample2PDR); |
| 409 | const auto& [containerID2, entityType2, entityInstance2] = |
| 410 | std::get<0>(sensorInfo2); |
| 411 | const auto& states2 = std::get<1>(sensorInfo2); |
| 412 | CompositeSensorStates statesCmp2{{3u, 4u, 9u, 10u, 11u, 13u}}; |
| 413 | |
| 414 | ASSERT_EQ(le16toh(terminusHandle2), 0u); |
| 415 | ASSERT_EQ(le16toh(sensorID2), 2u); |
| 416 | ASSERT_EQ(le16toh(containerID2), 0u); |
| 417 | ASSERT_EQ(le16toh(entityType2), 31u); |
| 418 | ASSERT_EQ(le16toh(entityInstance2), 1u); |
| 419 | ASSERT_EQ(states2, statesCmp2); |
| 420 | |
| 421 | // Sample state sensor with SensorID - 3, EntityType - Virtual Machine |
| 422 | // Manager(33), Composite State Sensor -2 , State Set ID - Link State(33), |
| 423 | // Supported States - 1,2, State Set ID - Configuration State(15), |
| 424 | // Supported States - 1,2,3,4 |
| 425 | std::vector<uint8_t> sample3PDR{ |
| 426 | 0x00, 0x00, 0x00, 0x00, 0x01, 0x04, 0x00, 0x00, 0x17, 0x00, 0x00, |
| 427 | 0x00, 0x03, 0x00, 0x21, 0x00, 0x02, 0x00, 0x01, 0x00, 0x00, 0x00, |
| 428 | 0x02, 0x21, 0x00, 0x01, 0x06, 0x0F, 0x00, 0x01, 0x1E}; |
| 429 | |
| 430 | const auto& [terminusHandle3, sensorID3, sensorInfo3] = |
| 431 | parseStateSensorPDR(sample3PDR); |
| 432 | const auto& [containerID3, entityType3, entityInstance3] = |
| 433 | std::get<0>(sensorInfo3); |
| 434 | const auto& states3 = std::get<1>(sensorInfo3); |
| 435 | CompositeSensorStates statesCmp3{{1u, 2u}, {1u, 2u, 3u, 4u}}; |
| 436 | |
| 437 | ASSERT_EQ(le16toh(terminusHandle3), 0u); |
| 438 | ASSERT_EQ(le16toh(sensorID3), 3u); |
| 439 | ASSERT_EQ(le16toh(containerID3), 1u); |
| 440 | ASSERT_EQ(le16toh(entityType3), 33u); |
| 441 | ASSERT_EQ(le16toh(entityInstance3), 2u); |
| 442 | ASSERT_EQ(states3, statesCmp3); |
| 443 | } |
TOM JOSEPH | d4d97a5 | 2020-03-23 14:36:34 +0530 | [diff] [blame] | 444 | |
| 445 | TEST(StateSensorHandler, allScenarios) |
| 446 | { |
| 447 | using namespace pldm::responder::events; |
| 448 | |
| 449 | ASSERT_THROW(StateSensorHandler("./event_jsons/malformed1"), |
| 450 | std::exception); |
| 451 | ASSERT_THROW(StateSensorHandler("./event_jsons/malformed2"), |
| 452 | std::exception); |
| 453 | |
| 454 | StateSensorHandler handler{"./event_jsons/good"}; |
| 455 | constexpr uint8_t eventState0 = 0; |
| 456 | constexpr uint8_t eventState1 = 1; |
| 457 | constexpr uint8_t eventState2 = 2; |
| 458 | constexpr uint8_t eventState3 = 3; |
| 459 | |
| 460 | // Event Entry 1 |
| 461 | { |
| 462 | StateSensorEntry entry{1, 64, 1, 0}; |
| 463 | const auto& [dbusMapping, eventStateMap] = handler.getEventInfo(entry); |
| 464 | DBusMapping mapping{"/xyz/abc/def", |
| 465 | "xyz.openbmc_project.example1.value", "value1", |
| 466 | "string"}; |
| 467 | ASSERT_EQ(mapping == dbusMapping, true); |
| 468 | |
| 469 | const auto& propValue0 = eventStateMap.at(eventState0); |
| 470 | const auto& propValue1 = eventStateMap.at(eventState1); |
| 471 | const auto& propValue2 = eventStateMap.at(eventState2); |
| 472 | PropertyValue value0{std::in_place_type<std::string>, |
| 473 | "xyz.openbmc_project.State.Normal"}; |
| 474 | PropertyValue value1{std::in_place_type<std::string>, |
| 475 | "xyz.openbmc_project.State.Critical"}; |
| 476 | PropertyValue value2{std::in_place_type<std::string>, |
| 477 | "xyz.openbmc_project.State.Fatal"}; |
| 478 | ASSERT_EQ(value0 == propValue0, true); |
| 479 | ASSERT_EQ(value1 == propValue1, true); |
| 480 | ASSERT_EQ(value2 == propValue2, true); |
| 481 | } |
| 482 | |
| 483 | // Event Entry 2 |
| 484 | { |
| 485 | StateSensorEntry entry{1, 64, 1, 1}; |
| 486 | const auto& [dbusMapping, eventStateMap] = handler.getEventInfo(entry); |
| 487 | DBusMapping mapping{"/xyz/abc/def", |
| 488 | "xyz.openbmc_project.example2.value", "value2", |
| 489 | "uint8_t"}; |
| 490 | ASSERT_EQ(mapping == dbusMapping, true); |
| 491 | |
| 492 | const auto& propValue0 = eventStateMap.at(eventState2); |
| 493 | const auto& propValue1 = eventStateMap.at(eventState3); |
| 494 | PropertyValue value0{std::in_place_type<uint8_t>, 9}; |
| 495 | PropertyValue value1{std::in_place_type<uint8_t>, 10}; |
| 496 | ASSERT_EQ(value0 == propValue0, true); |
| 497 | ASSERT_EQ(value1 == propValue1, true); |
| 498 | } |
| 499 | |
| 500 | // Event Entry 3 |
| 501 | { |
| 502 | StateSensorEntry entry{2, 67, 2, 0}; |
| 503 | const auto& [dbusMapping, eventStateMap] = handler.getEventInfo(entry); |
| 504 | DBusMapping mapping{"/xyz/abc/ghi", |
| 505 | "xyz.openbmc_project.example3.value", "value3", |
| 506 | "bool"}; |
| 507 | ASSERT_EQ(mapping == dbusMapping, true); |
| 508 | |
| 509 | const auto& propValue0 = eventStateMap.at(eventState0); |
| 510 | const auto& propValue1 = eventStateMap.at(eventState1); |
| 511 | PropertyValue value0{std::in_place_type<bool>, false}; |
| 512 | PropertyValue value1{std::in_place_type<bool>, true}; |
| 513 | ASSERT_EQ(value0 == propValue0, true); |
| 514 | ASSERT_EQ(value1 == propValue1, true); |
| 515 | } |
| 516 | |
| 517 | // Invalid Entry |
| 518 | { |
| 519 | StateSensorEntry entry{0, 0, 0, 0}; |
| 520 | ASSERT_THROW(handler.getEventInfo(entry), std::out_of_range); |
| 521 | } |
| 522 | } |