From 6c91c92ce531bd77ee8e28214dd2e0fd9f54232a Mon Sep 17 00:00:00 2001 From: Oscar Virot Date: Fri, 17 Jan 2025 17:42:01 +0100 Subject: [PATCH] Add Fail if broken XML Added unittest --- TameMyCerts.Tests/XMLPolicyTests.cs | 26 +++++++++++++++++-- .../Models/CertificateRequestPolicy.cs | 4 ++- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/TameMyCerts.Tests/XMLPolicyTests.cs b/TameMyCerts.Tests/XMLPolicyTests.cs index 9f7bda0..3b27480 100644 --- a/TameMyCerts.Tests/XMLPolicyTests.cs +++ b/TameMyCerts.Tests/XMLPolicyTests.cs @@ -67,7 +67,7 @@ public void Test_Unknown_XML_Element() CertificateRequestPolicyCacheEntry cacheEntry = new CertificateRequestPolicyCacheEntry(filename); - Assert.Empty(cacheEntry.ErrorMessage); + Assert.NotEmpty(cacheEntry.ErrorMessage); Assert.Equal(2, _listener.Events.Count); Assert.Equal(92, _listener.Events[0].EventId); output.WriteLine(_listener.Events[0].Message); @@ -89,7 +89,7 @@ public void Test_Unknown_XML_Element2() CertificateRequestPolicyCacheEntry cacheEntry = new CertificateRequestPolicyCacheEntry(filename); - Assert.Empty(cacheEntry.ErrorMessage); + Assert.NotEmpty(cacheEntry.ErrorMessage); Assert.Equal(2, _listener.Events.Count); Assert.Equal(92, _listener.Events[0].EventId); @@ -145,4 +145,26 @@ public void Broken_XML_Policies() File.Delete(filename); } + [Fact] + public void Test_Broken_XML() + { + var filename = Path.GetTempFileName(); + File.Delete(filename); + + string sampleXML = @" + This should fault + +"; + File.WriteAllText($"{filename}.xml", sampleXML); + _listener.ClearEvents(); + + CertificateRequestPolicyCache cache = new CertificateRequestPolicyCache(Path.GetTempPath()); + var cacheEntry = cache.GetCertificateRequestPolicy(Path.GetFileName(filename)); + + Assert.NotNull(cacheEntry); + Assert.Null(cacheEntry.CertificateRequestPolicy); + + File.Delete($"{filename}.xml"); + } } \ No newline at end of file diff --git a/TameMyCerts/Models/CertificateRequestPolicy.cs b/TameMyCerts/Models/CertificateRequestPolicy.cs index a614474..c76a587 100644 --- a/TameMyCerts/Models/CertificateRequestPolicy.cs +++ b/TameMyCerts/Models/CertificateRequestPolicy.cs @@ -165,13 +165,15 @@ public string SaveToString() } private static void UnknownElementHandler(object sender, XmlElementEventArgs e) { - ETWLogger.Log.TMC_92_Policy_Unknown_XML_Element(e.Element.Name, e.LineNumber, e.LinePosition); + ETWLogger.Log.TMC_92_Policy_Unknown_XML_Element(e.Element.Name, e.LineNumber, e.LinePosition); + throw new XmlException($"Unknown XML element {e.Element.Name} at line {e.LineNumber}, position {e.LinePosition}"); } // Event handler for unknown attributes private static void UnknownAttributeHandler(object sender, XmlAttributeEventArgs e) { ETWLogger.Log.TMC_93_Policy_Unknown_XML_Attribute(e.Attr.Name, e.Attr.Value, e.LineNumber, e.LinePosition); + throw new XmlException($"Unknown XML attribute {e.Attr.Name} with value {e.Attr.Value} at line {e.LineNumber}, position {e.LinePosition}"); } } \ No newline at end of file