Shift section definitions into separate file, add testing.

Change-Id: Idb0b41d7fa2999485580fca770958a27c1086f65
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 0600df9..fcdfa5a 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -67,6 +67,7 @@
   tests/test-utils.cpp
   generator/cper-generate.c
   generator/gen-utils.c 
+  sections/cper-section.c
   ${GeneratorSectionSources} 
   ${EDKSources}
 )
@@ -76,6 +77,7 @@
 target_link_libraries(cper-parse json-c b64c)
 target_link_libraries(cper-convert cper-parse)
 target_compile_options(cper-parse PRIVATE -Wno-address-of-packed-member)
+target_compile_options(cper-tests PRIVATE -fpermissive)
 
 # Copy required specification JSON for command line application.
 add_custom_command(TARGET cper-convert POST_BUILD COMMAND ${CMAKE_COMMAND} -E make_directory bin/specification)
diff --git a/cper-parse.c b/cper-parse.c
index 28c2fbd..5e71ba2 100644
--- a/cper-parse.c
+++ b/cper-parse.c
@@ -11,21 +11,7 @@
 #include "edk/Cper.h"
 #include "cper-parse.h"
 #include "cper-utils.h"
-#include "sections/cper-section-generic.h"
-#include "sections/cper-section-ia32x64.h"
-#include "sections/cper-section-arm.h"
-#include "sections/cper-section-memory.h"
-#include "sections/cper-section-pcie.h"
-#include "sections/cper-section-pci-bus.h"
-#include "sections/cper-section-pci-dev.h"
-#include "sections/cper-section-firmware.h"
-#include "sections/cper-section-dmar-generic.h"
-#include "sections/cper-section-dmar-vtd.h"
-#include "sections/cper-section-dmar-iommu.h"
-#include "sections/cper-section-ccix-per.h"
-#include "sections/cper-section-cxl-protocol.h"
-#include "sections/cper-section-ipf.h"
-#include "sections/cper-section-cxl-component.h"
+#include "sections/cper-section.h"
 
 //Private pre-definitions.
 json_object *cper_header_to_ir(EFI_COMMON_ERROR_RECORD_HEADER *header);
@@ -265,67 +251,14 @@
 			       json_object_new_string(section_type_string));
 
 	//Readable section type, if possible.
-	char *section_type_readable = "Unknown";
-	if (guid_equal(&section_descriptor->SectionType,
-		       &gEfiProcessorGenericErrorSectionGuid))
-		section_type_readable = "Processor Generic";
-	else if (guid_equal(&section_descriptor->SectionType,
-			    &gEfiIa32X64ProcessorErrorSectionGuid))
-		section_type_readable = "IA32/X64";
-	else if (guid_equal(&section_descriptor->SectionType,
-			    &gEfiIpfProcessorErrorSectionGuid))
-		section_type_readable = "IPF";
-	else if (guid_equal(&section_descriptor->SectionType,
-			    &gEfiArmProcessorErrorSectionGuid))
-		section_type_readable = "ARM";
-	else if (guid_equal(&section_descriptor->SectionType,
-			    &gEfiPlatformMemoryErrorSectionGuid) ||
-		 guid_equal(&section_descriptor->SectionType,
-			    &gEfiPlatformMemoryError2SectionGuid))
-		section_type_readable = "Platform Memory";
-	else if (guid_equal(&section_descriptor->SectionType,
-			    &gEfiPcieErrorSectionGuid))
-		section_type_readable = "PCIe";
-	else if (guid_equal(&section_descriptor->SectionType,
-			    &gEfiFirmwareErrorSectionGuid))
-		section_type_readable = "Firmware Error Record Reference";
-	else if (guid_equal(&section_descriptor->SectionType,
-			    &gEfiPciBusErrorSectionGuid))
-		section_type_readable = "PCI/PCI-X Bus";
-	else if (guid_equal(&section_descriptor->SectionType,
-			    &gEfiPciDevErrorSectionGuid))
-		section_type_readable = "PCI Component/Device";
-	else if (guid_equal(&section_descriptor->SectionType,
-			    &gEfiDMArGenericErrorSectionGuid))
-		section_type_readable = "DMAr Generic";
-	else if (guid_equal(&section_descriptor->SectionType,
-			    &gEfiDirectedIoDMArErrorSectionGuid))
-		section_type_readable =
-			"Intel VT for Directed I/O specific DMAr section";
-	else if (guid_equal(&section_descriptor->SectionType,
-			    &gEfiIommuDMArErrorSectionGuid))
-		section_type_readable = "IOMMU specific DMAr section";
-	else if (guid_equal(&section_descriptor->SectionType,
-			    &gEfiCcixPerLogErrorSectionGuid))
-		section_type_readable = "CCIX PER Log Error";
-	else if (guid_equal(&section_descriptor->SectionType,
-			    &gEfiCxlProtocolErrorSectionGuid))
-		section_type_readable = "CXL Protocol Error";
-	else if (guid_equal(&section_descriptor->SectionType,
-			    &gEfiCxlGeneralMediaErrorSectionGuid))
-		section_type_readable = "CXL General Media Component Error";
-	else if (guid_equal(&section_descriptor->SectionType,
-			    &gEfiCxlDramEventErrorSectionGuid))
-		section_type_readable = "CXL DRAM Component Error";
-	else if (guid_equal(&section_descriptor->SectionType,
-			    &gEfiCxlPhysicalSwitchErrorSectionGuid))
-		section_type_readable = "CXL Physical Switch Component Error";
-	else if (guid_equal(&section_descriptor->SectionType,
-			    &gEfiCxlVirtualSwitchErrorSectionGuid))
-		section_type_readable = "CXL Virtual Switch Component Error";
-	else if (guid_equal(&section_descriptor->SectionType,
-			    &gEfiCxlMldPortErrorSectionGuid))
-		section_type_readable = "CXL MLD Port Component Error";
+	const char *section_type_readable = "Unknown";
+	for (int i=0; i<section_definitions_len; i++)
+	{
+		if (guid_equal(section_definitions[i].Guid, &section_descriptor->SectionType)) {
+			section_type_readable = section_definitions[i].ReadableName;
+			break;
+		}
+	}
 
 	json_object_object_add(section_type, "type",
 			       json_object_new_string(section_type_readable));
@@ -382,66 +315,18 @@
 
 	//Parse section to IR based on GUID.
 	json_object *result = NULL;
-	if (guid_equal(&descriptor->SectionType,
-		       &gEfiProcessorGenericErrorSectionGuid))
-		result = cper_section_generic_to_ir(section, descriptor);
-	else if (guid_equal(&descriptor->SectionType,
-			    &gEfiIa32X64ProcessorErrorSectionGuid))
-		result = cper_section_ia32x64_to_ir(section, descriptor);
-	else if (guid_equal(&descriptor->SectionType,
-			    &gEfiIpfProcessorErrorSectionGuid))
-		result = cper_section_ipf_to_ir(section, descriptor);
-	else if (guid_equal(&descriptor->SectionType,
-			    &gEfiArmProcessorErrorSectionGuid))
-		result = cper_section_arm_to_ir(section, descriptor);
-	else if (guid_equal(&descriptor->SectionType,
-			    &gEfiPlatformMemoryErrorSectionGuid))
-		result =
-			cper_section_platform_memory_to_ir(section, descriptor);
-	else if (guid_equal(&descriptor->SectionType,
-			    &gEfiPlatformMemoryError2SectionGuid))
-		result = cper_section_platform_memory2_to_ir(section,
-							     descriptor);
-	else if (guid_equal(&descriptor->SectionType,
-			    &gEfiPcieErrorSectionGuid))
-		result = cper_section_pcie_to_ir(section, descriptor);
-	else if (guid_equal(&descriptor->SectionType,
-			    &gEfiFirmwareErrorSectionGuid))
-		result = cper_section_firmware_to_ir(section, descriptor);
-	else if (guid_equal(&descriptor->SectionType,
-			    &gEfiPciBusErrorSectionGuid))
-		result = cper_section_pci_bus_to_ir(section, descriptor);
-	else if (guid_equal(&descriptor->SectionType,
-			    &gEfiPciDevErrorSectionGuid))
-		result = cper_section_pci_dev_to_ir(section, descriptor);
-	else if (guid_equal(&descriptor->SectionType,
-			    &gEfiDMArGenericErrorSectionGuid))
-		result = cper_section_dmar_generic_to_ir(section, descriptor);
-	else if (guid_equal(&descriptor->SectionType,
-			    &gEfiDirectedIoDMArErrorSectionGuid))
-		result = cper_section_dmar_vtd_to_ir(section, descriptor);
-	else if (guid_equal(&descriptor->SectionType,
-			    &gEfiIommuDMArErrorSectionGuid))
-		result = cper_section_dmar_iommu_to_ir(section, descriptor);
-	else if (guid_equal(&descriptor->SectionType,
-			    &gEfiCcixPerLogErrorSectionGuid))
-		result = cper_section_ccix_per_to_ir(section, descriptor);
-	else if (guid_equal(&descriptor->SectionType,
-			    &gEfiCxlProtocolErrorSectionGuid))
-		result = cper_section_cxl_protocol_to_ir(section, descriptor);
-	else if (guid_equal(&descriptor->SectionType,
-			    &gEfiCxlGeneralMediaErrorSectionGuid) ||
-		 guid_equal(&descriptor->SectionType,
-			    &gEfiCxlDramEventErrorSectionGuid) ||
-		 guid_equal(&descriptor->SectionType,
-			    &gEfiCxlPhysicalSwitchErrorSectionGuid) ||
-		 guid_equal(&descriptor->SectionType,
-			    &gEfiCxlVirtualSwitchErrorSectionGuid) ||
-		 guid_equal(&descriptor->SectionType,
-			    &gEfiCxlMldPortErrorSectionGuid)) {
-		result = cper_section_cxl_component_to_ir(section, descriptor);
-	} else {
-		//Failed read, unknown GUID.
+	int section_converted = 0;
+	for (int i=0; i<section_definitions_len; i++)
+	{
+		if (guid_equal(section_definitions[i].Guid, &descriptor->SectionType) && section_definitions[i].ToIR != NULL) {
+			result = section_definitions[i].ToIR(section, descriptor);
+			section_converted = 1;
+			break;
+		}
+	}
+
+	//Was it an unknown GUID/failed read?
+	if (!section_converted) {
 		//Output the data as formatted base64.
 		result = json_object_new_object();
 		char *encoded = b64_encode((unsigned char *)section,
diff --git a/ir-parse.c b/ir-parse.c
index 1748de6..5d9e6f2 100644
--- a/ir-parse.c
+++ b/ir-parse.c
@@ -11,21 +11,7 @@
 #include "edk/Cper.h"
 #include "cper-parse.h"
 #include "cper-utils.h"
-#include "sections/cper-section-generic.h"
-#include "sections/cper-section-ia32x64.h"
-#include "sections/cper-section-arm.h"
-#include "sections/cper-section-memory.h"
-#include "sections/cper-section-pcie.h"
-#include "sections/cper-section-pci-bus.h"
-#include "sections/cper-section-pci-dev.h"
-#include "sections/cper-section-firmware.h"
-#include "sections/cper-section-dmar-generic.h"
-#include "sections/cper-section-dmar-vtd.h"
-#include "sections/cper-section-dmar-iommu.h"
-#include "sections/cper-section-ccix-per.h"
-#include "sections/cper-section-cxl-protocol.h"
-#include "sections/cper-section-ipf.h"
-#include "sections/cper-section-cxl-component.h"
+#include "sections/cper-section.h"
 
 //Private pre-declarations.
 void ir_header_to_cper(json_object *header_ir,
@@ -165,63 +151,18 @@
 			EFI_ERROR_SECTION_DESCRIPTOR *descriptor, FILE *out)
 {
 	//Find the correct section type, and parse.
-	if (guid_equal(&descriptor->SectionType,
-		       &gEfiProcessorGenericErrorSectionGuid))
-		ir_section_generic_to_cper(section, out);
-	else if (guid_equal(&descriptor->SectionType,
-			    &gEfiIa32X64ProcessorErrorSectionGuid))
-		ir_section_ia32x64_to_cper(section, out);
-	// else if (guid_equal(&descriptor->SectionType, &gEfiIpfProcessorErrorSectionGuid))
-	//     ir_section_ipf_to_cper(section, out);
-	else if (guid_equal(&descriptor->SectionType,
-			    &gEfiArmProcessorErrorSectionGuid))
-		ir_section_arm_to_cper(section, out);
-	else if (guid_equal(&descriptor->SectionType,
-			    &gEfiPlatformMemoryErrorSectionGuid))
-		ir_section_memory_to_cper(section, out);
-	else if (guid_equal(&descriptor->SectionType,
-			    &gEfiPlatformMemoryError2SectionGuid))
-		ir_section_memory2_to_cper(section, out);
-	else if (guid_equal(&descriptor->SectionType,
-			    &gEfiPcieErrorSectionGuid))
-		ir_section_pcie_to_cper(section, out);
-	else if (guid_equal(&descriptor->SectionType,
-			    &gEfiFirmwareErrorSectionGuid))
-		ir_section_firmware_to_cper(section, out);
-	else if (guid_equal(&descriptor->SectionType,
-			    &gEfiPciBusErrorSectionGuid))
-		ir_section_pci_bus_to_cper(section, out);
-	else if (guid_equal(&descriptor->SectionType,
-			    &gEfiPciDevErrorSectionGuid))
-		ir_section_pci_dev_to_cper(section, out);
-	else if (guid_equal(&descriptor->SectionType,
-			    &gEfiDMArGenericErrorSectionGuid))
-		ir_section_dmar_generic_to_cper(section, out);
-	else if (guid_equal(&descriptor->SectionType,
-			    &gEfiDirectedIoDMArErrorSectionGuid))
-		ir_section_dmar_vtd_to_cper(section, out);
-	else if (guid_equal(&descriptor->SectionType,
-			    &gEfiIommuDMArErrorSectionGuid))
-		ir_section_dmar_iommu_to_cper(section, out);
-	else if (guid_equal(&descriptor->SectionType,
-			    &gEfiCcixPerLogErrorSectionGuid))
-		ir_section_ccix_per_to_cper(section, out);
-	else if (guid_equal(&descriptor->SectionType,
-			    &gEfiCxlProtocolErrorSectionGuid))
-		ir_section_cxl_protocol_to_cper(section, out);
-	else if (guid_equal(&descriptor->SectionType,
-			    &gEfiCxlGeneralMediaErrorSectionGuid) ||
-		 guid_equal(&descriptor->SectionType,
-			    &gEfiCxlDramEventErrorSectionGuid) ||
-		 guid_equal(&descriptor->SectionType,
-			    &gEfiCxlPhysicalSwitchErrorSectionGuid) ||
-		 guid_equal(&descriptor->SectionType,
-			    &gEfiCxlVirtualSwitchErrorSectionGuid) ||
-		 guid_equal(&descriptor->SectionType,
-			    &gEfiCxlMldPortErrorSectionGuid)) {
-		ir_section_cxl_component_to_cper(section, out);
-	} else {
-		//Unknown GUID, so read as a base64 unknown section.
+	int section_converted = 0;
+	for (int i=0; i<section_definitions_len; i++)
+	{
+		if (guid_equal(section_definitions[i].Guid, &descriptor->SectionType) && section_definitions[i].ToCPER != NULL) {
+			section_definitions[i].ToCPER(section, out);
+			section_converted = 1;
+			break;
+		}
+	}
+
+	//If unknown GUID, so read as a base64 unknown section.
+	if (!section_converted) {
 		json_object *encoded = json_object_object_get(section, "data");
 		UINT8 *decoded =
 			b64_decode(json_object_get_string(encoded),
diff --git a/sections/cper-section.c b/sections/cper-section.c
new file mode 100644
index 0000000..82a44a5
--- /dev/null
+++ b/sections/cper-section.c
@@ -0,0 +1,48 @@
+/**
+ * Describes available sections to the CPER parser.
+ * 
+ * Author: Lawrence.Tang@arm.com
+ **/
+#include "../edk/Cper.h"
+#include "cper-section.h"
+#include "cper-section-arm.h"
+#include "cper-section-generic.h"
+#include "cper-section-ia32x64.h"
+#include "cper-section-ipf.h"
+#include "cper-section-memory.h"
+#include "cper-section-pcie.h"
+#include "cper-section-firmware.h"
+#include "cper-section-pci-bus.h"
+#include "cper-section-pci-dev.h"
+#include "cper-section-dmar-generic.h"
+#include "cper-section-dmar-vtd.h"
+#include "cper-section-dmar-iommu.h"
+#include "cper-section-ccix-per.h"
+#include "cper-section-cxl-protocol.h"
+#include "cper-section-cxl-component.h"
+
+//Definitions of all sections available to the CPER parser.
+CPER_SECTION_DEFINITION section_definitions[] = {
+    {&gEfiProcessorGenericErrorSectionGuid, "Processor Generic", cper_section_generic_to_ir, ir_section_generic_to_cper},
+    {&gEfiIa32X64ProcessorErrorSectionGuid, "IA32/X64", cper_section_ia32x64_to_ir, ir_section_ia32x64_to_cper},
+    {&gEfiIpfProcessorErrorSectionGuid, "IPF", NULL, NULL},
+    {&gEfiArmProcessorErrorSectionGuid, "ARM", cper_section_arm_to_ir, ir_section_arm_to_cper},
+    {&gEfiPlatformMemoryErrorSectionGuid, "Platform Memory", cper_section_platform_memory_to_ir, ir_section_memory_to_cper},
+    {&gEfiPlatformMemoryError2SectionGuid, "Platform Memory 2", cper_section_platform_memory2_to_ir, ir_section_memory2_to_cper},
+    {&gEfiPcieErrorSectionGuid, "PCIe", cper_section_pcie_to_ir, ir_section_pcie_to_cper},
+    {&gEfiFirmwareErrorSectionGuid, "Firmware Error Record Reference", cper_section_firmware_to_ir, ir_section_firmware_to_cper},
+    {&gEfiPciBusErrorSectionGuid, "PCI/PCI-X Bus", cper_section_pci_bus_to_ir, ir_section_pci_bus_to_cper},
+    {&gEfiPciDevErrorSectionGuid, "PCI Component/Device", cper_section_pci_dev_to_ir, ir_section_pci_dev_to_cper},
+    {&gEfiDMArGenericErrorSectionGuid, "DMAr Generic", cper_section_dmar_generic_to_ir, ir_section_dmar_generic_to_cper},
+    {&gEfiDirectedIoDMArErrorSectionGuid, "Intel VT for Directed I/O Specific DMAr", cper_section_dmar_vtd_to_ir, ir_section_dmar_vtd_to_cper},
+    {&gEfiIommuDMArErrorSectionGuid, "IOMMU Specific DMAr", cper_section_dmar_iommu_to_ir, ir_section_dmar_iommu_to_cper},
+    {&gEfiCcixPerLogErrorSectionGuid, "CCIX PER Log Error", cper_section_ccix_per_to_ir, ir_section_ccix_per_to_cper},
+    {&gEfiCxlProtocolErrorSectionGuid, "CXL Protocol Error", cper_section_cxl_protocol_to_ir, ir_section_cxl_protocol_to_cper},
+    {&gEfiCxlGeneralMediaErrorSectionGuid, "CXL General Media Component Error", cper_section_cxl_component_to_ir, ir_section_cxl_component_to_cper},
+    {&gEfiCxlDramEventErrorSectionGuid, "CXL DRAM Component Error", cper_section_cxl_component_to_ir, ir_section_cxl_component_to_cper},
+    {&gEfiCxlMemoryModuleErrorSectionGuid, "CXL Memory Module Component Error", cper_section_cxl_component_to_ir, ir_section_cxl_component_to_cper},
+    {&gEfiCxlPhysicalSwitchErrorSectionGuid, "CXL Physical Switch Component Error", cper_section_cxl_component_to_ir, ir_section_cxl_component_to_cper},
+    {&gEfiCxlVirtualSwitchErrorSectionGuid, "CXL Virtual Switch Component Error", cper_section_cxl_component_to_ir, ir_section_cxl_component_to_cper},
+    {&gEfiCxlMldPortErrorSectionGuid, "CXL MLD Port Component Error", cper_section_cxl_component_to_ir, ir_section_cxl_component_to_cper},
+};
+const size_t section_definitions_len = sizeof(section_definitions) / sizeof(CPER_SECTION_DEFINITION);
\ No newline at end of file
diff --git a/sections/cper-section.h b/sections/cper-section.h
new file mode 100644
index 0000000..4379f91
--- /dev/null
+++ b/sections/cper-section.h
@@ -0,0 +1,20 @@
+#ifndef CPER_SECTION_H
+#define CPER_SECTION_H
+
+#include <json.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include "../edk/Cper.h"
+
+//Definition structure for a single CPER section type.
+typedef struct {
+    EFI_GUID* Guid;
+    const char* ReadableName;
+    json_object* (*ToIR)(void*, EFI_ERROR_SECTION_DESCRIPTOR*);
+    void (*ToCPER)(json_object*, FILE*);
+} CPER_SECTION_DEFINITION;
+
+extern CPER_SECTION_DEFINITION section_definitions[];
+extern const size_t section_definitions_len;
+
+#endif
\ No newline at end of file
diff --git a/tests/ir-tests.cpp b/tests/ir-tests.cpp
index a961e40..1017e75 100644
--- a/tests/ir-tests.cpp
+++ b/tests/ir-tests.cpp
@@ -11,6 +11,7 @@
 #include "../cper-parse.h"
 #include "../json-schema.h"
 #include "../generator/cper-generate.h"
+#include "../sections/cper-section.h"
 }
 
 /*
@@ -98,6 +99,22 @@
 }
 
 /*
+* Non-single section assertions.
+*/
+TEST(CompileTimeAssertions, TwoWayConversion)
+{
+	for (int i=0; i<section_definitions_len; i++)
+	{
+		//If a conversion one way exists, a conversion the other way must exist.
+		std::string err = "If a CPER conversion exists one way, there must be an equivalent method in reverse.";
+		if (section_definitions[i].ToCPER != NULL)
+			ASSERT_NE(section_definitions[i].ToIR, NULL) << err;
+		if (section_definitions[i].ToIR != NULL)
+			ASSERT_NE(section_definitions[i].ToCPER, NULL) << err;
+	}
+}
+
+/*
 * Single section tests.
 */