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