PEL: Maybe shrink the AdditionalData UD section

If the UserData section created from the AdditionalData event log
property would run the PEL over 16KB, shrink that section enough to make
the PEL fit into 16KB.

Signed-off-by: Matt Spinler <spinler@us.ibm.com>
Change-Id: Ie56f61c9c2eb4a264613388464450d595533bc8c
diff --git a/test/openpower-pels/pel_test.cpp b/test/openpower-pels/pel_test.cpp
index 8832038..ea1842d 100644
--- a/test/openpower-pels/pel_test.cpp
+++ b/test/openpower-pels/pel_test.cpp
@@ -180,6 +180,47 @@
     EXPECT_EQ(udCount, 2); // AD section and sysInfo section
 }
 
+// Test that when the AdditionalData size is over 16KB that
+// the PEL that's created is exactly 16KB since the UserData
+// section that contains all that data was pruned.
+TEST_F(PELTest, CreateTooBigADTest)
+{
+    message::Entry regEntry;
+    uint64_t timestamp = 5;
+
+    regEntry.name = "test";
+    regEntry.subsystem = 5;
+    regEntry.actionFlags = 0xC000;
+    regEntry.src.type = 0xBD;
+    regEntry.src.reasonCode = 0x1234;
+
+    // Over the 16KB max PEL size
+    std::string bigAD{"KEY1="};
+    bigAD += std::string(17000, 'G');
+
+    std::vector<std::string> data{bigAD};
+    AdditionalData ad{data};
+    MockDataInterface dataIface;
+
+    PEL pel{regEntry, 42, timestamp, phosphor::logging::Entry::Level::Error, ad,
+            dataIface};
+
+    EXPECT_TRUE(pel.valid());
+    EXPECT_EQ(pel.size(), 16384);
+
+    // Make sure that there are still 2 UD sections.
+    size_t udCount = 0;
+    for (const auto& section : pel.optionalSections())
+    {
+        if (section->header().id == static_cast<uint16_t>(SectionID::userData))
+        {
+            udCount++;
+        }
+    }
+
+    EXPECT_EQ(udCount, 2); // AD section and sysInfo section
+}
+
 // Test that we'll create Generic optional sections for sections that
 // there aren't explicit classes for.
 TEST_F(PELTest, GenericSectionTest)