Deepak Kodihalli | 557dfb0 | 2019-05-12 13:11:17 +0530 | [diff] [blame^] | 1 | #include "libpldmresponder/pdr.hpp" |
| 2 | #include "libpldmresponder/platform.hpp" |
| 3 | |
| 4 | #include <iostream> |
| 5 | |
| 6 | #include <gtest/gtest.h> |
| 7 | |
| 8 | using namespace pldm::responder; |
| 9 | |
| 10 | TEST(getPDR, testGoodPath) |
| 11 | { |
| 12 | std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_PDR_REQ_BYTES> |
| 13 | requestPayload{}; |
| 14 | auto request = reinterpret_cast<pldm_msg*>(requestPayload.data()); |
| 15 | size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr); |
| 16 | |
| 17 | uint8_t* start = request->payload; |
| 18 | start += sizeof(uint32_t) + sizeof(uint32_t) + sizeof(uint8_t); |
| 19 | uint16_t* reqCount = reinterpret_cast<uint16_t*>(start); |
| 20 | *reqCount = 100; |
| 21 | using namespace pdr; |
| 22 | Repo& pdrRepo = get("./pdr_jsons/state_effecter/good"); |
| 23 | ASSERT_EQ(pdrRepo.empty(), false); |
| 24 | auto response = getPDR(request, requestPayloadLength); |
| 25 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 26 | |
| 27 | ASSERT_EQ(responsePtr->payload[0], PLDM_SUCCESS); |
| 28 | start = responsePtr->payload; |
| 29 | start += sizeof(uint8_t); |
| 30 | uint32_t* nextRecordHandle = reinterpret_cast<uint32_t*>(start); |
| 31 | ASSERT_EQ(*nextRecordHandle, 2); |
| 32 | start += sizeof(uint32_t) + sizeof(uint32_t) + sizeof(uint8_t); |
| 33 | uint16_t* recordCount = reinterpret_cast<uint16_t*>(start); |
| 34 | ASSERT_EQ(*recordCount != 0, true); |
| 35 | start += sizeof(uint16_t); |
| 36 | // Check a bit of the PDR common header |
| 37 | pldm_state_effecter_pdr* pdr = |
| 38 | reinterpret_cast<pldm_state_effecter_pdr*>(start); |
| 39 | ASSERT_EQ(pdr->hdr.record_handle, 1); |
| 40 | ASSERT_EQ(pdr->hdr.version, 1); |
| 41 | } |
| 42 | |
| 43 | TEST(getPDR, testShortRead) |
| 44 | { |
| 45 | std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_PDR_REQ_BYTES> |
| 46 | requestPayload{}; |
| 47 | auto request = reinterpret_cast<pldm_msg*>(requestPayload.data()); |
| 48 | size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr); |
| 49 | |
| 50 | uint8_t* start = request->payload; |
| 51 | start += sizeof(uint32_t) + sizeof(uint32_t) + sizeof(uint8_t); |
| 52 | uint16_t* reqCount = reinterpret_cast<uint16_t*>(start); |
| 53 | // Read 1 byte of PDR |
| 54 | *reqCount = 1; |
| 55 | using namespace pdr; |
| 56 | Repo& pdrRepo = get("./pdr_jsons/state_effecter/good"); |
| 57 | ASSERT_EQ(pdrRepo.empty(), false); |
| 58 | auto response = getPDR(request, requestPayloadLength); |
| 59 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 60 | |
| 61 | ASSERT_EQ(responsePtr->payload[0], PLDM_SUCCESS); |
| 62 | start = responsePtr->payload; |
| 63 | start += |
| 64 | sizeof(uint8_t) + sizeof(uint32_t) + sizeof(uint32_t) + sizeof(uint8_t); |
| 65 | uint16_t* recordCount = reinterpret_cast<uint16_t*>(start); |
| 66 | ASSERT_EQ(*recordCount, 1); |
| 67 | } |
| 68 | |
| 69 | TEST(getPDR, testBadRecordHandle) |
| 70 | { |
| 71 | std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_PDR_REQ_BYTES> |
| 72 | requestPayload{}; |
| 73 | auto request = reinterpret_cast<pldm_msg*>(requestPayload.data()); |
| 74 | size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr); |
| 75 | |
| 76 | uint8_t* start = request->payload; |
| 77 | uint32_t* recordHandle = reinterpret_cast<uint32_t*>(start); |
| 78 | *recordHandle = 100000; |
| 79 | start += sizeof(uint32_t) + sizeof(uint32_t) + sizeof(uint8_t); |
| 80 | uint16_t* reqCount = reinterpret_cast<uint16_t*>(start); |
| 81 | *reqCount = 1; |
| 82 | using namespace pdr; |
| 83 | Repo& pdrRepo = get("./pdr_jsons/state_effecter/good"); |
| 84 | ASSERT_EQ(pdrRepo.empty(), false); |
| 85 | auto response = getPDR(request, requestPayloadLength); |
| 86 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 87 | |
| 88 | ASSERT_EQ(responsePtr->payload[0], PLDM_PLATFORM_INVALID_RECORD_HANDLE); |
| 89 | } |
| 90 | |
| 91 | TEST(getPDR, testNoNextRecord) |
| 92 | { |
| 93 | std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_PDR_REQ_BYTES> |
| 94 | requestPayload{}; |
| 95 | auto request = reinterpret_cast<pldm_msg*>(requestPayload.data()); |
| 96 | size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr); |
| 97 | |
| 98 | uint8_t* start = request->payload; |
| 99 | uint32_t* recordHandle = reinterpret_cast<uint32_t*>(start); |
| 100 | *recordHandle = 3; |
| 101 | using namespace pdr; |
| 102 | Repo& pdrRepo = get("./pdr_jsons/state_effecter/good"); |
| 103 | ASSERT_EQ(pdrRepo.empty(), false); |
| 104 | auto response = getPDR(request, requestPayloadLength); |
| 105 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 106 | |
| 107 | ASSERT_EQ(responsePtr->payload[0], PLDM_SUCCESS); |
| 108 | start = responsePtr->payload; |
| 109 | start += sizeof(uint8_t); |
| 110 | uint32_t* nextRecordHandle = reinterpret_cast<uint32_t*>(start); |
| 111 | ASSERT_EQ(*nextRecordHandle, 0); |
| 112 | } |
| 113 | |
| 114 | TEST(getPDR, testFindPDR) |
| 115 | { |
| 116 | std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_PDR_REQ_BYTES> |
| 117 | requestPayload{}; |
| 118 | auto request = reinterpret_cast<pldm_msg*>(requestPayload.data()); |
| 119 | size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr); |
| 120 | |
| 121 | uint8_t* start = request->payload; |
| 122 | start += sizeof(uint32_t) + sizeof(uint32_t) + sizeof(uint8_t); |
| 123 | uint16_t* reqCount = reinterpret_cast<uint16_t*>(start); |
| 124 | *reqCount = 100; |
| 125 | using namespace pdr; |
| 126 | Repo& pdrRepo = get("./pdr_jsons/state_effecter/good"); |
| 127 | ASSERT_EQ(pdrRepo.empty(), false); |
| 128 | auto response = getPDR(request, requestPayloadLength); |
| 129 | |
| 130 | // Let's try to find a PDR of type stateEffecter (= 11) and entity type = |
| 131 | // 100 |
| 132 | bool found = false; |
| 133 | uint32_t handle = 0; // start asking for PDRs from recordHandle 0 |
| 134 | uint32_t* recordHandle = nullptr; |
| 135 | while (!found) |
| 136 | { |
| 137 | start = request->payload; |
| 138 | recordHandle = reinterpret_cast<uint32_t*>(start); |
| 139 | *recordHandle = handle; |
| 140 | auto response = getPDR(request, requestPayloadLength); |
| 141 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 142 | |
| 143 | ASSERT_EQ(responsePtr->payload[0], PLDM_SUCCESS); |
| 144 | start = responsePtr->payload; |
| 145 | start += sizeof(uint8_t); |
| 146 | uint32_t* nextRecordHandle = reinterpret_cast<uint32_t*>(start); |
| 147 | handle = *nextRecordHandle; // point to the next pdr in case current is |
| 148 | // not what we want |
| 149 | start += sizeof(uint32_t) + sizeof(uint32_t) + sizeof(uint8_t) + |
| 150 | sizeof(uint16_t); |
| 151 | pldm_pdr_hdr* hdr = reinterpret_cast<pldm_pdr_hdr*>(start); |
| 152 | uint32_t intType = hdr->type; |
| 153 | std::cerr << "PDR next record handle " << handle << std::endl; |
| 154 | std::cerr << "PDR type " << intType << std::endl; |
| 155 | if (hdr->type == PLDM_STATE_EFFECTER_PDR) |
| 156 | { |
| 157 | pldm_state_effecter_pdr* pdr = |
| 158 | reinterpret_cast<pldm_state_effecter_pdr*>(start); |
| 159 | std::cerr << "PDR entity type " << pdr->entity_type << std::endl; |
| 160 | if (pdr->entity_type == 100) |
| 161 | { |
| 162 | found = true; |
| 163 | // Rest of the PDR can be accessed as need be |
| 164 | break; |
| 165 | } |
| 166 | } |
| 167 | if (!*nextRecordHandle) // no more records |
| 168 | { |
| 169 | break; |
| 170 | } |
| 171 | } |
| 172 | ASSERT_EQ(found, true); |
| 173 | } |