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 // (C) 2002 Motus Technologies Inc. (http://www.motus.com)
6 // (C) 2004 Novell  http://www.novell.com
7 
8 using Xunit;
9 
10 namespace System.Security.Cryptography.Algorithms.Tests
11 {
12     public class SignatureDescriptionTests
13     {
14         [Fact]
Constructor_Default()15         public void Constructor_Default()
16         {
17             SignatureDescription sig = new SignatureDescription();
18             Assert.Null(sig.KeyAlgorithm);
19             Assert.Null(sig.DigestAlgorithm);
20             Assert.Null(sig.FormatterAlgorithm);
21             Assert.Null(sig.DeformatterAlgorithm);
22         }
23 
24         [Fact]
Constructor_Null()25         public void Constructor_Null()
26         {
27             AssertExtensions.Throws<ArgumentNullException>("el", () => new SignatureDescription(null));
28         }
29 
30         [Fact]
Constructor_SecurityElement_Empty()31         public void Constructor_SecurityElement_Empty()
32         {
33             SecurityElement se = new SecurityElement("xml");
34             SignatureDescription sig = new SignatureDescription(se);
35             Assert.Null(sig.KeyAlgorithm);
36             Assert.Null(sig.DigestAlgorithm);
37             Assert.Null(sig.FormatterAlgorithm);
38             Assert.Null(sig.DeformatterAlgorithm);
39         }
40 
41         [Fact]
Constructor_SecurityElement_DSA()42         public void Constructor_SecurityElement_DSA()
43         {
44             SecurityElement se = new SecurityElement("DSASignature");
45             se.AddChild(new SecurityElement("Key", "System.Security.Cryptography.DSACryptoServiceProvider"));
46             se.AddChild(new SecurityElement("Digest", "System.Security.Cryptography.SHA1CryptoServiceProvider"));
47             se.AddChild(new SecurityElement("Formatter", "System.Security.Cryptography.DSASignatureFormatter"));
48             se.AddChild(new SecurityElement("Deformatter", "System.Security.Cryptography.DSASignatureDeformatter"));
49 
50             SignatureDescription sig = new SignatureDescription(se);
51             Assert.Equal("System.Security.Cryptography.DSACryptoServiceProvider", sig.KeyAlgorithm);
52             Assert.Equal("System.Security.Cryptography.SHA1CryptoServiceProvider", sig.DigestAlgorithm);
53             Assert.Equal("System.Security.Cryptography.DSASignatureFormatter", sig.FormatterAlgorithm);
54             Assert.Equal("System.Security.Cryptography.DSASignatureDeformatter", sig.DeformatterAlgorithm);
55         }
56 
57         [Fact]
Constructor_SecurityElement_RSA()58         public void Constructor_SecurityElement_RSA()
59         {
60             SecurityElement se = new SecurityElement("RSASignature");
61             se.AddChild(new SecurityElement("Key", "System.Security.Cryptography.RSACryptoServiceProvider"));
62             se.AddChild(new SecurityElement("Digest", "System.Security.Cryptography.SHA1CryptoServiceProvider"));
63             se.AddChild(new SecurityElement("Formatter", "System.Security.Cryptography.RSAPKCS1SignatureFormatter"));
64             se.AddChild(new SecurityElement("Deformatter", "System.Security.Cryptography.RSAPKCS1SignatureDeformatter"));
65 
66             SignatureDescription sig = new SignatureDescription(se);
67             Assert.Equal("System.Security.Cryptography.RSACryptoServiceProvider", sig.KeyAlgorithm);
68             Assert.Equal("System.Security.Cryptography.SHA1CryptoServiceProvider", sig.DigestAlgorithm);
69             Assert.Equal("System.Security.Cryptography.RSAPKCS1SignatureFormatter", sig.FormatterAlgorithm);
70             Assert.Equal("System.Security.Cryptography.RSAPKCS1SignatureDeformatter", sig.DeformatterAlgorithm);
71         }
72 
73         [Fact]
Properties()74         public void Properties()
75         {
76             const string invalid = "invalid";
77             SignatureDescription sig = new SignatureDescription();
78 
79             sig.DeformatterAlgorithm = invalid;
80             Assert.NotNull(sig.DeformatterAlgorithm);
81             Assert.Equal(invalid, sig.DeformatterAlgorithm);
82             sig.DeformatterAlgorithm = null;
83             Assert.Null(sig.DeformatterAlgorithm);
84 
85             sig.DigestAlgorithm = invalid;
86             Assert.NotNull(sig.DigestAlgorithm);
87             Assert.Equal(invalid, sig.DigestAlgorithm);
88             sig.DigestAlgorithm = null;
89             Assert.Null(sig.DigestAlgorithm);
90 
91             sig.FormatterAlgorithm = invalid;
92             Assert.NotNull(sig.FormatterAlgorithm);
93             Assert.Equal(invalid, sig.FormatterAlgorithm);
94             sig.FormatterAlgorithm = null;
95             Assert.Null(sig.FormatterAlgorithm);
96 
97             sig.KeyAlgorithm = invalid;
98             Assert.NotNull(sig.KeyAlgorithm);
99             Assert.Equal(invalid, sig.KeyAlgorithm);
100             sig.KeyAlgorithm = null;
101             Assert.Null(sig.KeyAlgorithm);
102         }
103 
104         [Fact]
Deformatter()105         public void Deformatter()
106         {
107             AsymmetricSignatureDeformatter def;
108             SignatureDescription sig = new SignatureDescription();
109             DSA dsa = DSA.Create();
110 
111             // Deformatter with all properties null
112             AssertExtensions.Throws<ArgumentNullException>("name", () => sig.CreateDeformatter(dsa));
113 
114             // Deformatter with invalid DeformatterAlgorithm property
115             sig.DeformatterAlgorithm = "DSA";
116             Assert.ThrowsAny<Exception>(() => def = sig.CreateDeformatter(dsa));
117 
118             // Deformatter with valid DeformatterAlgorithm property
119             sig.DeformatterAlgorithm = "DSASignatureDeformatter";
120             Assert.Throws<NullReferenceException>(() => def = sig.CreateDeformatter(dsa));
121 
122             // Deformatter with valid DeformatterAlgorithm property
123             sig.KeyAlgorithm = "DSA";
124             sig.DigestAlgorithm = "SHA1";
125             sig.DeformatterAlgorithm = "DSASignatureDeformatter";
126             Assert.Throws<NullReferenceException>(() => def = sig.CreateDeformatter(dsa));
127         }
128 
129         [Fact]
130         [PlatformSpecific(TestPlatforms.Windows)]  // Operation is not supported on Unix
Digest()131         public void Digest()
132         {
133             bool rightClass = false;
134             HashAlgorithm hash = null;
135             SignatureDescription sig = new SignatureDescription();
136 
137             // null hash
138             AssertExtensions.Throws<ArgumentNullException>("name", () => hash = sig.CreateDigest());
139 
140             sig.DigestAlgorithm = "SHA1";
141             hash = sig.CreateDigest();
142             Assert.NotNull(hash);
143             rightClass = (hash.ToString().IndexOf(sig.DigestAlgorithm) > 0);
144             Assert.True(rightClass, "CreateDigest(SHA1)");
145 
146             sig.DigestAlgorithm = "MD5";
147             hash = sig.CreateDigest();
148             Assert.NotNull(hash);
149             rightClass = (hash.ToString().IndexOf(sig.DigestAlgorithm) > 0);
150             Assert.True(rightClass, "CreateDigest(MD5)");
151 
152             sig.DigestAlgorithm = "SHA256";
153             hash = sig.CreateDigest();
154             Assert.NotNull(hash);
155             rightClass = (hash.ToString().IndexOf(sig.DigestAlgorithm) > 0);
156             Assert.True(rightClass, "CreateDigest(SHA256)");
157 
158             sig.DigestAlgorithm = "SHA384";
159             hash = sig.CreateDigest();
160             Assert.NotNull(hash);
161             rightClass = (hash.ToString().IndexOf(sig.DigestAlgorithm) > 0);
162             Assert.True(rightClass, "CreateDigest(SHA384)");
163 
164             sig.DigestAlgorithm = "SHA512";
165             hash = sig.CreateDigest();
166             Assert.NotNull(hash);
167             rightClass = (hash.ToString().IndexOf(sig.DigestAlgorithm) > 0);
168             Assert.True(rightClass, "CreateDigest(SHA512)");
169 
170             sig.DigestAlgorithm = "bad";
171             hash = sig.CreateDigest();
172             Assert.Null(hash);
173         }
174 
175         [Fact]
Formatter()176         public void Formatter()
177         {
178             SignatureDescription sig = new SignatureDescription();
179             DSA dsa = DSA.Create();
180 
181             // Formatter with all properties null
182             AssertExtensions.Throws<ArgumentNullException>("name", () => sig.CreateFormatter(dsa));
183 
184             // Formatter with invalid FormatterAlgorithm property
185             AsymmetricSignatureFormatter fmt = null;
186             sig.FormatterAlgorithm = "DSA";
187             Assert.ThrowsAny<Exception>(() => fmt = sig.CreateFormatter(dsa));
188 
189             // Formatter with valid FormatterAlgorithm property
190             sig.FormatterAlgorithm = "DSASignatureFormatter";
191             Assert.Throws<NullReferenceException>(() => sig.CreateFormatter(dsa));
192 
193             // Deformatter with valid DeformatterAlgorithm property
194             sig.KeyAlgorithm = "DSA";
195             sig.DigestAlgorithm = "SHA1";
196             sig.FormatterAlgorithm = "DSASignatureFormatter";
197             Assert.Throws<NullReferenceException>(() => sig.CreateFormatter(dsa));
198         }
199     }
200 }
201