diff --git a/src/System.Security.Cryptography.Xml/tests/CipherDataTests.cs b/src/System.Security.Cryptography.Xml/tests/CipherDataTests.cs new file mode 100644 index 000000000000..5b7e24e30149 --- /dev/null +++ b/src/System.Security.Cryptography.Xml/tests/CipherDataTests.cs @@ -0,0 +1,202 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml; +using Xunit; +using Xunit.Extensions; + +namespace System.Security.Cryptography.Xml.Tests +{ + public class CipherDataTests + { + [Fact] + public void Constructor_Empty() + { + CipherData cipherData = new CipherData(); + + Assert.Null(cipherData.CipherReference); + Assert.Null(cipherData.CipherValue); + Assert.Throws(() => cipherData.GetXml()); + } + + [Fact] + public void Constructor_CipherValue_Null() + { + Assert.Throws(() => new CipherData((byte[])null)); + } + + [Theory] + [InlineData(new byte[0])] + [InlineData(new byte[] { 1, 2, 3 })] + public void Constructor_CipherValue(byte[] cipherValue) + { + CipherData cipherData = new CipherData(cipherValue); + + Assert.Equal(cipherValue, cipherData.CipherValue); + Assert.Null(cipherData.CipherReference); + + XmlElement xmlElement = cipherData.GetXml(); + Assert.NotNull(xmlElement); + Assert.Equal( + $"{Convert.ToBase64String(cipherValue)}", + xmlElement.OuterXml); + } + + [Fact] + public void Constructor_CipherReference_Null() + { + Assert.Throws(() => new CipherData((CipherReference)null)); + } + + [Theory] + [MemberData(nameof(Constructor_CipherReference_Source))] + public void Constructor_CipherReference(CipherReference cipherReference) + { + CipherData cipherData = new CipherData(cipherReference); + + Assert.Null(cipherData.CipherValue); + Assert.Equal(cipherReference, cipherData.CipherReference); + + XmlElement xmlElement = cipherData.GetXml(); + Assert.NotNull(xmlElement); + if (cipherReference.Uri != string.Empty) + { + Assert.Equal( + $"", + xmlElement.OuterXml); + } + else + { + Assert.Equal( + "", + xmlElement.OuterXml); + } + } + + public static IEnumerable Constructor_CipherReference_Source() + { + return new object[][] + { + new [] { new CipherReference() }, + new [] { new CipherReference("http://dummy.urionly.io") }, + new [] { new CipherReference("http://dummy.uri.transform.io", new TransformChain()) }, + }; + } + + [Fact] + public void CipherReference_Null() + { + CipherData cipherData = new CipherData(); + Assert.Throws(() => cipherData.CipherReference = null); + } + + [Fact] + public void CipherReference_CipherValueSet() + { + CipherData cipherData = new CipherData(new byte[0]); + Assert.Throws(() => cipherData.CipherReference = new CipherReference()); + } + + [Fact] + public void CipherValue_Null() + { + CipherData cipherData = new CipherData(new CipherReference()); + Assert.Throws(() => cipherData.CipherValue = null); + } + + [Fact] + public void CipherValue_CipherReferenceSet() + { + CipherData cipherData = new CipherData(new CipherReference()); + Assert.Throws(() => cipherData.CipherValue = new byte[0]); + } + + [Fact] + public void LoadXml_Null() + { + CipherData cipherData = new CipherData(); + Assert.Throws(() => cipherData.LoadXml(null)); + } + + [Fact] + public void LoadXml_NoValueOrReference() + { + XmlDocument xmlDocument = new XmlDocument(); + xmlDocument.LoadXml(""); + + CipherData cipherData = new CipherData(); + + Assert.Throws(() => cipherData.LoadXml(xmlDocument.DocumentElement)); + } + + [Theory] + [MemberData(nameof(LoadXml_CipherValue_Source))] + public void LoadXml_CipherValue(XmlElement xmlElement, byte[] cipherValue) + { + CipherData cipherData = new CipherData(); + cipherData.LoadXml(xmlElement); + + Assert.Equal(cipherValue, cipherData.CipherValue); + Assert.Null(cipherData.CipherReference); + + XmlElement gotXmlElement = cipherData.GetXml(); + Assert.NotNull(gotXmlElement); + Assert.Equal(xmlElement.OuterXml, gotXmlElement.OuterXml); + } + + public static IEnumerable LoadXml_CipherValue_Source() + { + return new[] + { + ToCipherDataTestCase("{1}", new byte[0]), + ToCipherDataTestCase("{1}", new byte[] { 5, 6, 7 }), + ToCipherDataTestCase("{1}", new byte[0]), + }; + } + + public static object[] ToCipherDataTestCase(string xml, byte[] cipherData) + { + XmlDocument xmlDocument = new XmlDocument(); + xmlDocument.LoadXml(string.Format(xml, EncryptedXml.XmlEncNamespaceUrl, Convert.ToBase64String(cipherData))); + return new object[] { xmlDocument.DocumentElement, cipherData }; + } + + [Theory] + [MemberData(nameof(LoadXml_CipherReference_Source))] + public void LoadXml_CipherReference(XmlElement xmlElement, string uri) + { + CipherData cipherData = new CipherData(); + cipherData.LoadXml(xmlElement); + + Assert.Equal(uri, cipherData.CipherReference.Uri); + Assert.Null(cipherData.CipherValue); + + XmlElement gotXmlElement = cipherData.GetXml(); + Assert.NotNull(gotXmlElement); + Assert.Equal(xmlElement.OuterXml, gotXmlElement.OuterXml); + } + + public static IEnumerable LoadXml_CipherReference_Source() + { + return new[] + { + ToCipherReferenceXmlElement("", "http://dummy.io"), + ToCipherReferenceXmlElement("", "https://encrypted.dummy.io"), + ToCipherReferenceXmlElement("", "ftp://wtf.org"), + }; + } + + public static object[] ToCipherReferenceXmlElement(string xml, string uri) + { + XmlDocument xmlDocument = new XmlDocument(); + xmlDocument.LoadXml(string.Format(xml, EncryptedXml.XmlEncNamespaceUrl, uri)); + return new object[] { xmlDocument.DocumentElement, uri }; + } + } +} diff --git a/src/System.Security.Cryptography.Xml/tests/System.Security.Cryptography.Xml.Tests.csproj b/src/System.Security.Cryptography.Xml/tests/System.Security.Cryptography.Xml.Tests.csproj index e6b91efe4bbf..fb3172ba333e 100644 --- a/src/System.Security.Cryptography.Xml/tests/System.Security.Cryptography.Xml.Tests.csproj +++ b/src/System.Security.Cryptography.Xml/tests/System.Security.Cryptography.Xml.Tests.csproj @@ -6,6 +6,7 @@ {3C32659A-6DB9-410A-8E24-BE91BFF4C024} +