platform-mc: Fix sensor aux name pdr parsing
This fixes an issue with counting the null terminator twice:
both on the name length and on the pdr pointer increment.
This caused to_bytes to throw an exception on sensors with composite
sensor count larger than 1.
Change-Id: I85c2c12854698402b213fe2dd66a42ac8c299902
Signed-off-by: tal-yac <talycb8@gmail.com>
Signed-off-by: Thu Nguyen <thu@os.amperecomputing.com>
diff --git a/platform-mc/terminus.cpp b/platform-mc/terminus.cpp
index 5b2ab3c..004158a 100644
--- a/platform-mc/terminus.cpp
+++ b/platform-mc/terminus.cpp
@@ -283,8 +283,7 @@
}
memcpy(alignedBuffer, ptr, u16NameStringLen * sizeof(uint16_t));
std::u16string u16NameString(alignedBuffer, u16NameStringLen);
- ptr += (u16NameString.size() + sizeof(nullTerminator)) *
- sizeof(uint16_t);
+ ptr += u16NameString.size() * sizeof(uint16_t);
std::transform(u16NameString.cbegin(), u16NameString.cend(),
u16NameString.begin(),
[](uint16_t utf16) { return be16toh(utf16); });
diff --git a/platform-mc/test/terminus_test.cpp b/platform-mc/test/terminus_test.cpp
index 925f284..e4f00f9 100644
--- a/platform-mc/test/terminus_test.cpp
+++ b/platform-mc/test/terminus_test.cpp
@@ -108,6 +108,246 @@
EXPECT_EQ("S0", t1.getTerminusName());
}
+TEST(TerminusTest, parseSensorAuxiliaryMultiNamesPDRTest)
+{
+ auto t1 = pldm::platform_mc::Terminus(1,
+ 1 << PLDM_BASE | 1 << PLDM_PLATFORM);
+ std::vector<uint8_t> pdr1{
+ 0x0,
+ 0x0,
+ 0x0,
+ 0x1, // record handle
+ 0x1, // PDRHeaderVersion
+ PLDM_SENSOR_AUXILIARY_NAMES_PDR, // PDRType
+ 0x0,
+ 0x0, // recordChangeNumber
+ 0x0,
+ 53, // dataLength
+ 0,
+ 0x0, // PLDMTerminusHandle
+ 0x1,
+ 0x0, // sensorID
+ 0x1, // sensorCount
+ 0x3, // nameStringCount
+ 'e',
+ 'n',
+ 0x0, // nameLanguageTag
+ 0x0,
+ 'T',
+ 0x0,
+ 'E',
+ 0x0,
+ 'M',
+ 0x0,
+ 'P',
+ 0x0,
+ '1',
+ 0x0,
+ 0x0, // sensorName Temp1
+ 'f',
+ 'r',
+ 0x0, // nameLanguageTag
+ 0x0,
+ 'T',
+ 0x0,
+ 'E',
+ 0x0,
+ 'M',
+ 0x0,
+ 'P',
+ 0x0,
+ '2',
+ 0x0,
+ 0x0, // sensorName Temp2
+ 'f',
+ 'r',
+ 0x0, // nameLanguageTag
+ 0x0,
+ 'T',
+ 0x0,
+ 'E',
+ 0x0,
+ 'M',
+ 0x0,
+ 'P',
+ 0x0,
+ '1',
+ 0x0,
+ '2',
+ 0x0,
+ 0x0 // sensorName Temp12
+ };
+
+ std::vector<uint8_t> pdr2{
+ 0x1, 0x0, 0x0,
+ 0x0, // record handle
+ 0x1, // PDRHeaderVersion
+ PLDM_ENTITY_AUXILIARY_NAMES_PDR, // PDRType
+ 0x1,
+ 0x0, // recordChangeNumber
+ 0x11,
+ 0, // dataLength
+ /* Entity Auxiliary Names PDR Data*/
+ 3,
+ 0x80, // entityType system software
+ 0x1,
+ 0x0, // Entity instance number =1
+ 0,
+ 0, // Overal system
+ 0, // shared Name Count one name only
+ 01, // nameStringCount
+ 0x65, 0x6e, 0x00,
+ 0x00, // Language Tag "en"
+ 0x53, 0x00, 0x30, 0x00,
+ 0x00 // Entity Name "S0"
+ };
+
+ t1.pdrs.emplace_back(pdr1);
+ t1.pdrs.emplace_back(pdr2);
+ t1.parseTerminusPDRs();
+
+ auto sensorAuxNames = t1.getSensorAuxiliaryNames(0);
+ EXPECT_EQ(nullptr, sensorAuxNames);
+
+ sensorAuxNames = t1.getSensorAuxiliaryNames(1);
+ EXPECT_NE(nullptr, sensorAuxNames);
+
+ const auto& [sensorId, sensorCnt, names] = *sensorAuxNames;
+ EXPECT_EQ(1, sensorId);
+ EXPECT_EQ(1, sensorCnt);
+ EXPECT_EQ(1, names.size());
+ EXPECT_EQ(3, names[0].size());
+ EXPECT_EQ("en", names[0][0].first);
+ EXPECT_EQ("TEMP1", names[0][0].second);
+ EXPECT_EQ("fr", names[0][1].first);
+ EXPECT_EQ("TEMP2", names[0][1].second);
+ EXPECT_EQ("fr", names[0][2].first);
+ EXPECT_EQ("TEMP12", names[0][2].second);
+ EXPECT_EQ(2, t1.pdrs.size());
+ EXPECT_EQ("S0", t1.getTerminusName());
+}
+
+TEST(TerminusTest, parseSensorAuxiliaryNamesMultiSensorsPDRTest)
+{
+ auto t1 = pldm::platform_mc::Terminus(1,
+ 1 << PLDM_BASE | 1 << PLDM_PLATFORM);
+ std::vector<uint8_t> pdr1{
+ 0x0,
+ 0x0,
+ 0x0,
+ 0x1, // record handle
+ 0x1, // PDRHeaderVersion
+ PLDM_SENSOR_AUXILIARY_NAMES_PDR, // PDRType
+ 0x0,
+ 0x0, // recordChangeNumber
+ 0x0,
+ 54, // dataLength
+ 0,
+ 0x0, // PLDMTerminusHandle
+ 0x1,
+ 0x0, // sensorID
+ 0x2, // sensorCount
+ 0x1, // nameStringCount
+ 'e',
+ 'n',
+ 0x0, // nameLanguageTag
+ 0x0,
+ 'T',
+ 0x0,
+ 'E',
+ 0x0,
+ 'M',
+ 0x0,
+ 'P',
+ 0x0,
+ '1',
+ 0x0,
+ 0x0, // sensorName Temp1
+ 0x2, // nameStringCount
+ 'f',
+ 'r',
+ 0x0, // nameLanguageTag
+ 0x0,
+ 'T',
+ 0x0,
+ 'E',
+ 0x0,
+ 'M',
+ 0x0,
+ 'P',
+ 0x0,
+ '2',
+ 0x0,
+ 0x0, // sensorName Temp2
+ 'f',
+ 'r',
+ 0x0, // nameLanguageTag
+ 0x0,
+ 'T',
+ 0x0,
+ 'E',
+ 0x0,
+ 'M',
+ 0x0,
+ 'P',
+ 0x0,
+ '1',
+ 0x0,
+ '2',
+ 0x0,
+ 0x0 // sensorName Temp12
+ };
+
+ std::vector<uint8_t> pdr2{
+ 0x1, 0x0, 0x0,
+ 0x0, // record handle
+ 0x1, // PDRHeaderVersion
+ PLDM_ENTITY_AUXILIARY_NAMES_PDR, // PDRType
+ 0x1,
+ 0x0, // recordChangeNumber
+ 0x11,
+ 0, // dataLength
+ /* Entity Auxiliary Names PDR Data*/
+ 3,
+ 0x80, // entityType system software
+ 0x1,
+ 0x0, // Entity instance number =1
+ 0,
+ 0, // Overal system
+ 0, // shared Name Count one name only
+ 01, // nameStringCount
+ 0x65, 0x6e, 0x00,
+ 0x00, // Language Tag "en"
+ 0x53, 0x00, 0x30, 0x00,
+ 0x00 // Entity Name "S0"
+ };
+
+ t1.pdrs.emplace_back(pdr1);
+ t1.pdrs.emplace_back(pdr2);
+ t1.parseTerminusPDRs();
+
+ auto sensorAuxNames = t1.getSensorAuxiliaryNames(0);
+ EXPECT_EQ(nullptr, sensorAuxNames);
+
+ sensorAuxNames = t1.getSensorAuxiliaryNames(1);
+ EXPECT_NE(nullptr, sensorAuxNames);
+
+ const auto& [sensorId, sensorCnt, names] = *sensorAuxNames;
+ EXPECT_EQ(1, sensorId);
+ EXPECT_EQ(2, sensorCnt);
+ EXPECT_EQ(2, names.size());
+ EXPECT_EQ(1, names[0].size());
+ EXPECT_EQ("en", names[0][0].first);
+ EXPECT_EQ("TEMP1", names[0][0].second);
+ EXPECT_EQ(2, names[1].size());
+ EXPECT_EQ("fr", names[1][0].first);
+ EXPECT_EQ("TEMP2", names[1][0].second);
+ EXPECT_EQ("fr", names[1][1].first);
+ EXPECT_EQ("TEMP12", names[1][1].second);
+ EXPECT_EQ(2, t1.pdrs.size());
+ EXPECT_EQ("S0", t1.getTerminusName());
+}
+
TEST(TerminusTest, parsePDRTestNoSensorPDR)
{
auto t1 = pldm::platform_mc::Terminus(1,