Rohit PAI | e891884 | 2025-06-10 09:46:33 +0530 | [diff] [blame] | 1 | #include "NvidiaGpuMctpVdm.hpp" |
| 2 | #include "OcpMctpVdm.hpp" |
| 3 | |
| 4 | #include <array> |
| 5 | #include <cstdint> |
| 6 | #include <string> |
| 7 | #include <variant> |
| 8 | #include <vector> |
| 9 | |
| 10 | #include <gtest/gtest.h> |
| 11 | |
| 12 | using namespace gpu; |
| 13 | |
| 14 | TEST(NvidiaGpuMctpVdmTest, EncodeGetInventoryInformationRequest) |
| 15 | { |
| 16 | std::array<uint8_t, 256> buf{}; |
| 17 | uint8_t instanceId = 1; |
| 18 | uint8_t propertyId = |
| 19 | static_cast<uint8_t>(InventoryPropertyId::BOARD_PART_NUMBER); |
| 20 | |
| 21 | auto rc = encodeGetInventoryInformationRequest(instanceId, propertyId, buf); |
| 22 | EXPECT_EQ(rc, 0); |
| 23 | |
| 24 | auto* msg = reinterpret_cast<GetInventoryInformationRequest*>(buf.data()); |
| 25 | EXPECT_EQ(msg->hdr.command, |
| 26 | static_cast<uint8_t>( |
| 27 | PlatformEnvironmentalCommands::GET_INVENTORY_INFORMATION)); |
| 28 | EXPECT_EQ(msg->hdr.data_size, sizeof(propertyId)); |
| 29 | EXPECT_EQ(msg->property_id, propertyId); |
| 30 | } |
| 31 | |
| 32 | TEST(NvidiaGpuMctpVdmTest, DecodeInventoryString) |
| 33 | { |
| 34 | std::array<uint8_t, 256> buf{}; |
| 35 | auto* response = |
| 36 | reinterpret_cast<ocp::accelerator_management::CommonResponse*>( |
| 37 | buf.data()); |
| 38 | |
| 39 | // Fill header |
| 40 | response->msgHdr.hdr.pci_vendor_id = htobe16(0x10DE); // NVIDIA vendor ID |
| 41 | response->msgHdr.hdr.instance_id = 0x01; // Instance ID |
| 42 | response->msgHdr.hdr.ocp_version = 0x89; // OCP version and type |
| 43 | response->msgHdr.hdr.ocp_accelerator_management_msg_type = |
| 44 | static_cast<uint8_t>( |
| 45 | ocp::accelerator_management::MessageType::RESPONSE); |
| 46 | |
| 47 | response->command = static_cast<uint8_t>( |
| 48 | PlatformEnvironmentalCommands::GET_INVENTORY_INFORMATION); |
| 49 | response->completion_code = static_cast<uint8_t>( |
| 50 | ocp::accelerator_management::CompletionCode::SUCCESS); |
| 51 | response->reserved = 0; |
| 52 | response->data_size = htole16(5); // 5 bytes for "TEST1" |
| 53 | |
| 54 | const char* testStr = "TEST1"; |
| 55 | memcpy(buf.data() + sizeof(ocp::accelerator_management::CommonResponse), |
| 56 | testStr, 5); |
| 57 | |
| 58 | ocp::accelerator_management::CompletionCode cc; |
| 59 | uint16_t reasonCode; |
| 60 | InventoryInfo info; |
| 61 | |
| 62 | auto rc = decodeGetInventoryInformationResponse( |
| 63 | buf, cc, reasonCode, InventoryPropertyId::BOARD_PART_NUMBER, info); |
| 64 | EXPECT_EQ(rc, 0); |
| 65 | EXPECT_EQ(cc, ocp::accelerator_management::CompletionCode::SUCCESS); |
| 66 | EXPECT_EQ(reasonCode, 0); |
| 67 | EXPECT_TRUE(std::holds_alternative<std::string>(info)); |
| 68 | EXPECT_EQ(std::get<std::string>(info), "TEST1"); |
| 69 | } |
| 70 | |
| 71 | TEST(NvidiaGpuMctpVdmTest, DecodeInventoryDeviceGuid) |
| 72 | { |
| 73 | std::array<uint8_t, 256> buf{}; |
| 74 | auto* response = |
| 75 | reinterpret_cast<ocp::accelerator_management::CommonResponse*>( |
| 76 | buf.data()); |
| 77 | |
| 78 | // Fill header |
| 79 | response->msgHdr.hdr.pci_vendor_id = htobe16(0x10DE); // NVIDIA vendor ID |
| 80 | response->msgHdr.hdr.instance_id = 0x01; // Instance ID |
| 81 | response->msgHdr.hdr.ocp_version = 0x89; // OCP version and type |
| 82 | response->msgHdr.hdr.ocp_accelerator_management_msg_type = |
| 83 | static_cast<uint8_t>( |
| 84 | ocp::accelerator_management::MessageType::RESPONSE); |
| 85 | |
| 86 | response->command = static_cast<uint8_t>( |
| 87 | PlatformEnvironmentalCommands::GET_INVENTORY_INFORMATION); |
| 88 | response->completion_code = static_cast<uint8_t>( |
| 89 | ocp::accelerator_management::CompletionCode::SUCCESS); |
| 90 | response->reserved = 0; |
| 91 | response->data_size = htole16(8); // 8 bytes for DEVICE_GUID |
| 92 | |
| 93 | std::vector<uint8_t> dummyGuid = {0xDE, 0xAD, 0xBE, 0xEF, |
| 94 | 0x01, 0x23, 0x45, 0x67}; |
| 95 | memcpy(buf.data() + sizeof(ocp::accelerator_management::CommonResponse), |
| 96 | dummyGuid.data(), dummyGuid.size()); |
| 97 | |
| 98 | ocp::accelerator_management::CompletionCode cc; |
| 99 | uint16_t reasonCode; |
| 100 | InventoryInfo info; |
| 101 | |
| 102 | auto rc = decodeGetInventoryInformationResponse( |
| 103 | buf, cc, reasonCode, InventoryPropertyId::DEVICE_GUID, info); |
| 104 | EXPECT_EQ(rc, 0); |
| 105 | EXPECT_EQ(cc, ocp::accelerator_management::CompletionCode::SUCCESS); |
| 106 | EXPECT_EQ(reasonCode, 0); |
| 107 | EXPECT_TRUE(std::holds_alternative<std::vector<uint8_t>>(info)); |
| 108 | EXPECT_EQ(std::get<std::vector<uint8_t>>(info), dummyGuid); |
| 109 | } |