diff --git a/SafeExamBrowser.Configuration.UnitTests/DataFormats/XmlParserTests.cs b/SafeExamBrowser.Configuration.UnitTests/DataFormats/XmlParserTests.cs index 55e78fba..82ff4b35 100644 --- a/SafeExamBrowser.Configuration.UnitTests/DataFormats/XmlParserTests.cs +++ b/SafeExamBrowser.Configuration.UnitTests/DataFormats/XmlParserTests.cs @@ -41,6 +41,7 @@ namespace SafeExamBrowser.Configuration.UnitTests.DataFormats Assert.IsFalse(sut.CanParse(null)); Assert.IsFalse(sut.CanParse(new MemoryStream(Encoding.UTF8.GetBytes("someKey1")))); Assert.IsFalse(sut.CanParse(new MemoryStream(Encoding.UTF8.GetBytes("")))); + Assert.IsFalse(sut.CanParse(new MemoryStream(Encoding.UTF8.GetBytes("")))); } @@ -59,6 +60,7 @@ namespace SafeExamBrowser.Configuration.UnitTests.DataFormats Assert.AreEqual(9876, result.RawData["someInteger"]); Assert.IsTrue(Convert.FromBase64String("/5vlF76sb+5vgkhjiNTOn7l1SN3Ho2UAMJD3TtLo49M=").SequenceEqual(result.RawData["someData"] as IEnumerable)); Assert.AreEqual(true, result.RawData["someBoolean"]); + Assert.AreEqual(false, result.RawData["anotherBoolean"]); Assert.AreEqual(12.34, result.RawData["someReal"]); Assert.AreEqual(new DateTime(2019, 02, 20, 12, 30, 00, 123), result.RawData["someDate"]); @@ -78,6 +80,71 @@ namespace SafeExamBrowser.Configuration.UnitTests.DataFormats Assert.AreEqual(4, (dictThree["dictThreeKeyTwo"] as List)[3]); } + [TestMethod] + public void MustCheckForRootDictionary() + { + var xml = ""; + var xml2 = "someKey5"; + var xml3 = ""; + var result = sut.TryParse(new MemoryStream(Encoding.UTF8.GetBytes(xml))); + + Assert.AreEqual(LoadStatus.InvalidData, result.Status); + + result = sut.TryParse(new MemoryStream(Encoding.UTF8.GetBytes(xml2))); + Assert.AreEqual(LoadStatus.InvalidData, result.Status); + + result = sut.TryParse(new MemoryStream(Encoding.UTF8.GetBytes(xml3))); + Assert.AreEqual(LoadStatus.InvalidData, result.Status); + } + + [TestMethod] + public void MustParseEmptyXml() + { + var xml = ""; + var result = sut.TryParse(new MemoryStream(Encoding.UTF8.GetBytes(xml))); + + Assert.AreEqual(LoadStatus.Success, result.Status); + Assert.AreEqual(0, result.RawData.Count); + } + + [TestMethod] + public void MustDetectInvalidKey() + { + var xml = "blubb"; + var result = sut.TryParse(new MemoryStream(Encoding.UTF8.GetBytes(xml))); + + Assert.AreEqual(LoadStatus.InvalidData, result.Status); + } + + [TestMethod] + public void MustDetectInvalidValueType() + { + var xml = "blubb1234"; + var result = sut.TryParse(new MemoryStream(Encoding.UTF8.GetBytes(xml))); + + Assert.AreEqual(LoadStatus.InvalidData, result.Status); + } + + [TestMethod] + public void MustAllowEmptyArray() + { + var xml = "value"; + var result = sut.TryParse(new MemoryStream(Encoding.UTF8.GetBytes(xml))); + + Assert.AreEqual(LoadStatus.Success, result.Status); + Assert.IsInstanceOfType(result.RawData["value"], typeof(IList)); + } + + [TestMethod] + public void MustAllowEmptyDictionary() + { + var xml = "value"; + var result = sut.TryParse(new MemoryStream(Encoding.UTF8.GetBytes(xml))); + + Assert.AreEqual(LoadStatus.Success, result.Status); + Assert.IsInstanceOfType(result.RawData["value"], typeof(IDictionary)); + } + private Stream LoadTestData() { var path = $"{nameof(SafeExamBrowser)}.{nameof(Configuration)}.{nameof(UnitTests)}.{nameof(DataFormats)}.XmlTestData.xml"; diff --git a/SafeExamBrowser.Configuration.UnitTests/DataFormats/XmlTestData.xml b/SafeExamBrowser.Configuration.UnitTests/DataFormats/XmlTestData.xml index 97098a4d..10dcebae 100644 --- a/SafeExamBrowser.Configuration.UnitTests/DataFormats/XmlTestData.xml +++ b/SafeExamBrowser.Configuration.UnitTests/DataFormats/XmlTestData.xml @@ -1,4 +1,4 @@ - + someString @@ -35,6 +35,8 @@ someBoolean + anotherBoolean + someReal 12.34 someDate diff --git a/SafeExamBrowser.Configuration/DataFormats/XmlParser.cs b/SafeExamBrowser.Configuration/DataFormats/XmlParser.cs index 96873661..43639898 100644 --- a/SafeExamBrowser.Configuration/DataFormats/XmlParser.cs +++ b/SafeExamBrowser.Configuration/DataFormats/XmlParser.cs @@ -176,7 +176,6 @@ namespace SafeExamBrowser.Configuration.DataFormats return LoadStatus.InvalidData; } - reader.Read(); reader.MoveToContent(); var status = ParseElement(reader, out object value);