-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
DSACreateTests.cs
109 lines (92 loc) · 3.83 KB
/
DSACreateTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Security.Cryptography.Dsa.Tests;
using Xunit;
namespace System.Security.Cryptography.Tests
{
[SkipOnPlatform(TestPlatforms.Browser | TestPlatforms.iOS | TestPlatforms.tvOS | TestPlatforms.MacCatalyst, "Not supported on Browser/iOS/tvOS/MacCatalyst")]
public static class DSACreateTests
{
public static bool SupportsKeyGeneration => DSAFactory.SupportsKeyGeneration;
public static bool SupportsFips186_3 => DSAFactory.SupportsFips186_3;
[ConditionalTheory(nameof(SupportsKeyGeneration))]
[SkipOnPlatform(TestPlatforms.Android, "Android only supports key sizes that are a multiple of 1024")]
[InlineData(512)]
[InlineData(960)]
public static void CreateWithKeysize_SmallKeys(int keySizeInBits)
{
using (DSA dsa = DSA.Create(keySizeInBits))
{
Assert.Equal(keySizeInBits, dsa.KeySize);
DSAParameters parameters = dsa.ExportParameters(false);
Assert.Equal(keySizeInBits, parameters.Y.Length << 3);
Assert.Equal(keySizeInBits, dsa.KeySize);
}
}
[ConditionalTheory(nameof(SupportsKeyGeneration))]
[InlineData(1024)]
public static void CreateWithKeysize(int keySizeInBits)
{
using (DSA dsa = DSA.Create(keySizeInBits))
{
Assert.Equal(keySizeInBits, dsa.KeySize);
DSAParameters parameters = dsa.ExportParameters(false);
Assert.Equal(keySizeInBits, parameters.Y.Length << 3);
Assert.Equal(keySizeInBits, dsa.KeySize);
}
}
[ConditionalTheory(nameof(SupportsKeyGeneration), nameof(SupportsFips186_3))]
[SkipOnPlatform(TestPlatforms.Android, "Android only supports key sizes that are a multiple of 1024")]
[InlineData(1088)]
public static void CreateWithKeysize_BigKeys(int keySizeInBits)
{
using (DSA dsa = DSA.Create(keySizeInBits))
{
Assert.Equal(keySizeInBits, dsa.KeySize);
DSAParameters parameters = dsa.ExportParameters(false);
Assert.Equal(keySizeInBits, parameters.Y.Length << 3);
Assert.Equal(keySizeInBits, dsa.KeySize);
}
}
[Theory]
[InlineData(0)]
[InlineData(1)]
[InlineData(1023)]
public static void CreateWithKeysize_InvalidKeySize(int keySizeInBits)
{
Assert.Throws<CryptographicException>(() => DSA.Create(keySizeInBits));
}
[Fact]
public static void CreateWithParameters_512()
{
CreateWithParameters(DSATestData.Dsa512Parameters);
}
[Fact]
public static void CreateWithParameters_1024()
{
CreateWithParameters(DSATestData.GetDSA1024Params());
}
[ConditionalFact(nameof(SupportsFips186_3))]
public static void CreateWithParameters_2048()
{
CreateWithParameters(DSATestData.GetDSA2048Params());
}
private static void CreateWithParameters(DSAParameters parameters)
{
DSAParameters exportedPrivate;
using (DSA dsa = DSA.Create(parameters))
{
exportedPrivate = dsa.ExportParameters(true);
}
DSAImportExport.AssertKeyEquals(parameters, exportedPrivate);
}
[Fact]
public static void CreateWithInvalidParameters()
{
DSAParameters parameters = DSATestData.GetDSA1024Params();
parameters.X = null;
parameters.Y = null;
AssertExtensions.Throws<ArgumentException>(null, () => DSA.Create(parameters));
}
}
}