|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using System; |
| 6 | +using System.Collections.Generic; |
| 7 | +using System.Linq; |
| 8 | +using System.Text; |
| 9 | +using System.Threading.Tasks; |
| 10 | +using System.Xml; |
| 11 | +using Xunit; |
| 12 | +using Xunit.Extensions; |
| 13 | + |
| 14 | +namespace System.Security.Cryptography.Xml.Tests |
| 15 | +{ |
| 16 | + public class CipherDataTests |
| 17 | + { |
| 18 | + [Fact] |
| 19 | + public void Constructor_Empty() |
| 20 | + { |
| 21 | + CipherData cipherData = new CipherData(); |
| 22 | + |
| 23 | + Assert.Null(cipherData.CipherReference); |
| 24 | + Assert.Null(cipherData.CipherValue); |
| 25 | + Assert.Throws<CryptographicException>(() => cipherData.GetXml()); |
| 26 | + } |
| 27 | + |
| 28 | + [Fact] |
| 29 | + public void Constructor_CipherValue_Null() |
| 30 | + { |
| 31 | + Assert.Throws<ArgumentNullException>(() => new CipherData((byte[])null)); |
| 32 | + } |
| 33 | + |
| 34 | + [Theory] |
| 35 | + [InlineData(new byte[0])] |
| 36 | + [InlineData(new byte[] { 1, 2, 3 })] |
| 37 | + public void Constructor_CipherValue(byte[] cipherValue) |
| 38 | + { |
| 39 | + CipherData cipherData = new CipherData(cipherValue); |
| 40 | + |
| 41 | + Assert.Equal(cipherValue, cipherData.CipherValue); |
| 42 | + Assert.Null(cipherData.CipherReference); |
| 43 | + |
| 44 | + XmlElement xmlElement = cipherData.GetXml(); |
| 45 | + Assert.NotNull(xmlElement); |
| 46 | + Assert.Equal( |
| 47 | + $"<CipherData xmlns=\"http://www.w3.org/2001/04/xmlenc#\"><CipherValue>{Convert.ToBase64String(cipherValue)}</CipherValue></CipherData>", |
| 48 | + xmlElement.OuterXml); |
| 49 | + } |
| 50 | + |
| 51 | + [Fact] |
| 52 | + public void Constructor_CipherReference_Null() |
| 53 | + { |
| 54 | + Assert.Throws<ArgumentNullException>(() => new CipherData((CipherReference)null)); |
| 55 | + } |
| 56 | + |
| 57 | + [Theory] |
| 58 | + [MemberData(nameof(Constructor_CipherReference_Source))] |
| 59 | + public void Constructor_CipherReference(CipherReference cipherReference) |
| 60 | + { |
| 61 | + CipherData cipherData = new CipherData(cipherReference); |
| 62 | + |
| 63 | + Assert.Null(cipherData.CipherValue); |
| 64 | + Assert.Equal(cipherReference, cipherData.CipherReference); |
| 65 | + |
| 66 | + XmlElement xmlElement = cipherData.GetXml(); |
| 67 | + Assert.NotNull(xmlElement); |
| 68 | + if (cipherReference.Uri != string.Empty) |
| 69 | + { |
| 70 | + Assert.Equal( |
| 71 | + $"<CipherData xmlns=\"http://www.w3.org/2001/04/xmlenc#\"><CipherReference URI=\"{cipherReference.Uri}\" /></CipherData>", |
| 72 | + xmlElement.OuterXml); |
| 73 | + } |
| 74 | + else |
| 75 | + { |
| 76 | + Assert.Equal( |
| 77 | + "<CipherData xmlns=\"http://www.w3.org/2001/04/xmlenc#\"><CipherReference /></CipherData>", |
| 78 | + xmlElement.OuterXml); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + public static IEnumerable<object[]> Constructor_CipherReference_Source() |
| 83 | + { |
| 84 | + return new object[][] |
| 85 | + { |
| 86 | + new [] { new CipherReference() }, |
| 87 | + new [] { new CipherReference("http://dummy.urionly.io") }, |
| 88 | + new [] { new CipherReference("http://dummy.uri.transform.io", new TransformChain()) }, |
| 89 | + }; |
| 90 | + } |
| 91 | + |
| 92 | + [Fact] |
| 93 | + public void CipherReference_Null() |
| 94 | + { |
| 95 | + CipherData cipherData = new CipherData(); |
| 96 | + Assert.Throws<ArgumentNullException>(() => cipherData.CipherReference = null); |
| 97 | + } |
| 98 | + |
| 99 | + [Fact] |
| 100 | + public void CipherReference_CipherValueSet() |
| 101 | + { |
| 102 | + CipherData cipherData = new CipherData(new byte[0]); |
| 103 | + Assert.Throws<CryptographicException>(() => cipherData.CipherReference = new CipherReference()); |
| 104 | + } |
| 105 | + |
| 106 | + [Fact] |
| 107 | + public void CipherValue_Null() |
| 108 | + { |
| 109 | + CipherData cipherData = new CipherData(new CipherReference()); |
| 110 | + Assert.Throws<ArgumentNullException>(() => cipherData.CipherValue = null); |
| 111 | + } |
| 112 | + |
| 113 | + [Fact] |
| 114 | + public void CipherValue_CipherReferenceSet() |
| 115 | + { |
| 116 | + CipherData cipherData = new CipherData(new CipherReference()); |
| 117 | + Assert.Throws<CryptographicException>(() => cipherData.CipherValue = new byte[0]); |
| 118 | + } |
| 119 | + |
| 120 | + [Fact] |
| 121 | + public void LoadXml_Null() |
| 122 | + { |
| 123 | + CipherData cipherData = new CipherData(); |
| 124 | + Assert.Throws<ArgumentNullException>(() => cipherData.LoadXml(null)); |
| 125 | + } |
| 126 | + |
| 127 | + [Fact] |
| 128 | + public void LoadXml_NoValueOrReference() |
| 129 | + { |
| 130 | + XmlDocument xmlDocument = new XmlDocument(); |
| 131 | + xmlDocument.LoadXml("<root/>"); |
| 132 | + |
| 133 | + CipherData cipherData = new CipherData(); |
| 134 | + |
| 135 | + Assert.Throws<CryptographicException>(() => cipherData.LoadXml(xmlDocument.DocumentElement)); |
| 136 | + } |
| 137 | + |
| 138 | + [Theory] |
| 139 | + [MemberData(nameof(LoadXml_CipherValue_Source))] |
| 140 | + public void LoadXml_CipherValue(XmlElement xmlElement, byte[] cipherValue) |
| 141 | + { |
| 142 | + CipherData cipherData = new CipherData(); |
| 143 | + cipherData.LoadXml(xmlElement); |
| 144 | + |
| 145 | + Assert.Equal(cipherValue, cipherData.CipherValue); |
| 146 | + Assert.Null(cipherData.CipherReference); |
| 147 | + |
| 148 | + XmlElement gotXmlElement = cipherData.GetXml(); |
| 149 | + Assert.NotNull(gotXmlElement); |
| 150 | + Assert.Equal(xmlElement.OuterXml, gotXmlElement.OuterXml); |
| 151 | + } |
| 152 | + |
| 153 | + public static IEnumerable<object[]> LoadXml_CipherValue_Source() |
| 154 | + { |
| 155 | + return new[] |
| 156 | + { |
| 157 | + ToCipherDataTestCase("<root xmlns:enc='{0}'><enc:CipherValue>{1}</enc:CipherValue></root>", new byte[0]), |
| 158 | + ToCipherDataTestCase("<root xmlns:enc='{0}'><enc:CipherValue>{1}</enc:CipherValue></root>", new byte[] { 5, 6, 7 }), |
| 159 | + ToCipherDataTestCase("<root xmlns='{0}'><CipherValue>{1}</CipherValue></root>", new byte[0]), |
| 160 | + }; |
| 161 | + } |
| 162 | + |
| 163 | + public static object[] ToCipherDataTestCase(string xml, byte[] cipherData) |
| 164 | + { |
| 165 | + XmlDocument xmlDocument = new XmlDocument(); |
| 166 | + xmlDocument.LoadXml(string.Format(xml, EncryptedXml.XmlEncNamespaceUrl, Convert.ToBase64String(cipherData))); |
| 167 | + return new object[] { xmlDocument.DocumentElement, cipherData }; |
| 168 | + } |
| 169 | + |
| 170 | + [Theory] |
| 171 | + [MemberData(nameof(LoadXml_CipherReference_Source))] |
| 172 | + public void LoadXml_CipherReference(XmlElement xmlElement, string uri) |
| 173 | + { |
| 174 | + CipherData cipherData = new CipherData(); |
| 175 | + cipherData.LoadXml(xmlElement); |
| 176 | + |
| 177 | + Assert.Equal(uri, cipherData.CipherReference.Uri); |
| 178 | + Assert.Null(cipherData.CipherValue); |
| 179 | + |
| 180 | + XmlElement gotXmlElement = cipherData.GetXml(); |
| 181 | + Assert.NotNull(gotXmlElement); |
| 182 | + Assert.Equal(xmlElement.OuterXml, gotXmlElement.OuterXml); |
| 183 | + } |
| 184 | + |
| 185 | + public static IEnumerable<object[]> LoadXml_CipherReference_Source() |
| 186 | + { |
| 187 | + return new[] |
| 188 | + { |
| 189 | + ToCipherReferenceXmlElement("<root xmlns:enc='{0}'><enc:CipherReference URI=\"{1}\" /></root>", "http://dummy.io"), |
| 190 | + ToCipherReferenceXmlElement("<root xmlns:enc='{0}'><enc:CipherReference URI=\"{1}\" /></root>", "https://encrypted.dummy.io"), |
| 191 | + ToCipherReferenceXmlElement("<root xmlns='{0}'><CipherReference URI=\"{1}\" /></root>", "ftp://wtf.org"), |
| 192 | + }; |
| 193 | + } |
| 194 | + |
| 195 | + public static object[] ToCipherReferenceXmlElement(string xml, string uri) |
| 196 | + { |
| 197 | + XmlDocument xmlDocument = new XmlDocument(); |
| 198 | + xmlDocument.LoadXml(string.Format(xml, EncryptedXml.XmlEncNamespaceUrl, uri)); |
| 199 | + return new object[] { xmlDocument.DocumentElement, uri }; |
| 200 | + } |
| 201 | + } |
| 202 | +} |
0 commit comments