Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit ec73071

Browse files
committed
Merge pull request #2017 from bartonjs/enable-x509-filetests
Enable file-load tests for X509Certificates
2 parents da361f5 + b72a51a commit ec73071

File tree

7 files changed

+65
-29
lines changed

7 files changed

+65
-29
lines changed

src/System.Security.Cryptography.X509Certificates/tests/CertTests.cs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,20 @@
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

44
using System.IO;
5-
using System.Text;
65
using Xunit;
76

87
namespace System.Security.Cryptography.X509Certificates.Tests
98
{
109
public static class CertTests
1110
{
1211
[Fact]
13-
[ActiveIssue(1977, PlatformID.Any)]
12+
[ActiveIssue(1993, PlatformID.AnyUnix)]
1413
public static void X509CertTest()
1514
{
1615
const string CertSubject =
1716
@"CN=Microsoft Corporate Root Authority, OU=ITG, O=Microsoft, L=Redmond, S=WA, C=US, [email protected]";
1817

19-
using (X509Certificate cert = new X509Certificate("microsoft.cer"))
18+
using (X509Certificate cert = new X509Certificate(Path.Combine("TestData", "microsoft.cer")))
2019
{
2120
Assert.Equal(CertSubject, cert.Subject);
2221
Assert.Equal(CertSubject, cert.Issuer);
@@ -50,13 +49,16 @@ public static void X509CertTest()
5049
}
5150

5251
[Fact]
53-
[ActiveIssue(1977, PlatformID.Any)]
52+
[ActiveIssue(1993, PlatformID.AnyUnix)]
5453
public static void X509Cert2Test()
5554
{
5655
const string CertName =
5756
@"[email protected], CN=ABA.ECOM Root CA, O=""ABA.ECOM, INC."", L=Washington, S=DC, C=US";
5857

59-
using (X509Certificate2 cert2 = new X509Certificate2("test.cer"))
58+
DateTime notBefore = new DateTime(1999, 7, 12, 17, 33, 53, DateTimeKind.Utc).ToLocalTime();
59+
DateTime notAfter = new DateTime(2009, 7, 9, 17, 33, 53, DateTimeKind.Utc).ToLocalTime();
60+
61+
using (X509Certificate2 cert2 = new X509Certificate2(Path.Combine("TestData", "test.cer")))
6062
{
6163
Assert.Equal(CertName, cert2.IssuerName.Name);
6264
Assert.Equal(CertName, cert2.SubjectName.Name);
@@ -67,8 +69,8 @@ public static void X509Cert2Test()
6769
Assert.True(pubKey.Key is RSACryptoServiceProvider);
6870
Assert.Equal("RSA", pubKey.Oid.FriendlyName);
6971

70-
Assert.Equal(new DateTime(2009, 7, 9, 10, 33, 53), cert2.NotAfter);
71-
Assert.Equal(new DateTime(1999, 7, 12, 10, 33, 53), cert2.NotBefore);
72+
Assert.Equal(notAfter, cert2.NotAfter);
73+
Assert.Equal(notBefore, cert2.NotBefore);
7274

7375
Assert.Equal("00D01E4090000046520000000100000004", cert2.SerialNumber);
7476
Assert.Equal("1.2.840.113549.1.1.5", cert2.SignatureAlgorithm.Value);
@@ -146,21 +148,21 @@ public static void X509Cert2ToStringVerbose()
146148
}
147149

148150
[Fact]
149-
[ActiveIssue(1977, PlatformID.Any)]
151+
[ActiveIssue(1993, PlatformID.AnyUnix)]
150152
public static void X509Cert2CreateFromPfxFile()
151153
{
152-
using (X509Certificate2 cert2 = new X509Certificate2("DummyTcpServer.pfx"))
154+
using (X509Certificate2 cert2 = new X509Certificate2(Path.Combine("TestData", "DummyTcpServer.pfx")))
153155
{
154156
// OID=RSA Encryption
155157
Assert.Equal("1.2.840.113549.1.1.1", cert2.GetKeyAlgorithm());
156158
}
157159
}
158160

159161
[Fact]
160-
[ActiveIssue(1977, PlatformID.Any)]
162+
[ActiveIssue(1993, PlatformID.AnyUnix)]
161163
public static void X509Cert2CreateFromPfxWithPassword()
162164
{
163-
using (X509Certificate2 cert2 = new X509Certificate2("test.pfx", "test"))
165+
using (X509Certificate2 cert2 = new X509Certificate2(Path.Combine("TestData", "test.pfx"), "test"))
164166
{
165167
// OID=RSA Encryption
166168
Assert.Equal("1.2.840.113549.1.1.1", cert2.GetKeyAlgorithm());

src/System.Security.Cryptography.X509Certificates/tests/CollectionTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
using System.Linq;
55
using System.Collections;
6-
6+
using System.IO;
77
using Xunit;
88

99
namespace System.Security.Cryptography.X509Certificates.Tests
@@ -268,13 +268,13 @@ public static void ImportStoreSavedAsPfxData()
268268
}
269269

270270
[Fact]
271-
[ActiveIssue(1977, PlatformID.Any)]
271+
[ActiveIssue(1993, PlatformID.AnyUnix)]
272272
public static void ImportFromFileTests()
273273
{
274274
using (var pfxCer = new X509Certificate2(TestData.PfxData, TestData.PfxDataPassword))
275275
{
276276
X509Certificate2Collection cc2 = new X509Certificate2Collection();
277-
cc2.Import(@"TestData\My.pfx", TestData.PfxDataPassword, X509KeyStorageFlags.DefaultKeySet);
277+
cc2.Import(Path.Combine("TestData" ,"My.pfx"), TestData.PfxDataPassword, X509KeyStorageFlags.DefaultKeySet);
278278
int count = cc2.Count;
279279
Assert.Equal(1, count);
280280

src/System.Security.Cryptography.X509Certificates/tests/LoadFromFileTests.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace System.Security.Cryptography.X509Certificates.Tests
1010
public static class LoadFromFileTests
1111
{
1212
[Fact]
13-
[ActiveIssue(1977, PlatformID.Any)]
13+
[ActiveIssue(1993, PlatformID.AnyUnix)]
1414
public static void TestIssuer()
1515
{
1616
using (X509Certificate2 c = LoadCertificateFromFile())
@@ -24,7 +24,7 @@ public static void TestIssuer()
2424
}
2525

2626
[Fact]
27-
[ActiveIssue(1977, PlatformID.Any)]
27+
[ActiveIssue(1993, PlatformID.AnyUnix)]
2828
public static void TestSubject()
2929
{
3030
using (X509Certificate2 c = LoadCertificateFromFile())
@@ -38,7 +38,7 @@ public static void TestSubject()
3838
}
3939

4040
[Fact]
41-
[ActiveIssue(1977, PlatformID.Any)]
41+
[ActiveIssue(1993, PlatformID.AnyUnix)]
4242
public static void TestSerial()
4343
{
4444
byte[] expectedSerial = "b00000000100dd9f3bd08b0aaf11b000000033".HexToByteArray();
@@ -51,7 +51,7 @@ public static void TestSerial()
5151
}
5252

5353
[Fact]
54-
[ActiveIssue(1977, PlatformID.Any)]
54+
[ActiveIssue(1993, PlatformID.AnyUnix)]
5555
public static void TestThumbprint()
5656
{
5757
byte[] expectedThumbPrint = "108e2ba23632620c427c570b6d9db51ac31387fe".HexToByteArray();
@@ -64,7 +64,7 @@ public static void TestThumbprint()
6464
}
6565

6666
[Fact]
67-
[ActiveIssue(1977, PlatformID.Any)]
67+
[ActiveIssue(1993, PlatformID.AnyUnix)]
6868
public static void TestGetFormat()
6969
{
7070
using (X509Certificate2 c = LoadCertificateFromFile())
@@ -75,7 +75,7 @@ public static void TestGetFormat()
7575
}
7676

7777
[Fact]
78-
[ActiveIssue(1977, PlatformID.Any)]
78+
[ActiveIssue(1993, PlatformID.AnyUnix)]
7979
public static void TestGetKeyAlgorithm()
8080
{
8181
using (X509Certificate2 c = LoadCertificateFromFile())
@@ -86,7 +86,7 @@ public static void TestGetKeyAlgorithm()
8686
}
8787

8888
[Fact]
89-
[ActiveIssue(1977, PlatformID.Any)]
89+
[ActiveIssue(1993, PlatformID.AnyUnix)]
9090
public static void TestGetKeyAlgorithmParameters()
9191
{
9292
string expected = "0500";
@@ -101,7 +101,7 @@ public static void TestGetKeyAlgorithmParameters()
101101
}
102102

103103
[Fact]
104-
[ActiveIssue(1977, PlatformID.Any)]
104+
[ActiveIssue(1993, PlatformID.AnyUnix)]
105105
public static void TestGetPublicKey()
106106
{
107107
byte[] expectedPublicKey = (
@@ -123,12 +123,12 @@ public static void TestGetPublicKey()
123123
}
124124

125125
[Fact]
126-
[ActiveIssue(1977, PlatformID.Any)]
126+
[ActiveIssue(1993, PlatformID.AnyUnix)]
127127
public static void TestLoadSignedFile()
128128
{
129129
// X509Certificate2 can also extract the certificate from a signed file.
130130

131-
string path = @"TestData\Windows6.1-KB3004361-x64.msu";
131+
string path = Path.Combine("TestData", "Windows6.1-KB3004361-x64.msu");
132132
if (!File.Exists(path))
133133
throw new Exception(string.Format("Test infrastructure failure: Expected to find file \"{0}\".", path));
134134

@@ -152,9 +152,9 @@ public static void TestLoadSignedFile()
152152

153153
private static X509Certificate2 LoadCertificateFromFile()
154154
{
155-
string path = @"TestData\Ms.cer";
155+
string path = Path.Combine("TestData", "Ms.cer");
156156
if (!File.Exists(path))
157-
throw new Exception(String.Format("Test infrastructure failure: Expected to find file \"{0}\".", path));
157+
throw new Exception(string.Format("Test infrastructure failure: Expected to find file \"{0}\".", path));
158158
byte[] data = File.ReadAllBytes(path);
159159
Assert.Equal(TestData.MsCertificate, data);
160160

src/System.Security.Cryptography.X509Certificates/tests/PfxTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,10 @@ public static void TestSetPrivateKey()
118118
}
119119

120120
[Fact]
121-
[ActiveIssue(1977, PlatformID.Any)]
121+
[ActiveIssue(1993, PlatformID.AnyUnix)]
122122
public static void TestContentType()
123123
{
124-
String fileName = @"TestData\My.pfx";
124+
string fileName = Path.Combine("TestData", "My.pfx");
125125
if (!File.Exists(fileName))
126126
throw new Exception("Test infrastructure failure: Expected to find file: \"" + fileName + "\".");
127127
X509ContentType ct = X509Certificate2.GetCertContentType(fileName);

src/System.Security.Cryptography.X509Certificates/tests/System.Security.Cryptography.X509Certificates.Tests.csproj

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,19 @@
3939
<None Include="project.json" />
4040
</ItemGroup>
4141
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
42+
<!-- Temporary until we have new work in build tools to
43+
deploy content from nuget packages -->
44+
<Target Name="CopyX509TestDataToTestDirectory" AfterTargets="CopyTestToTestDirectory" Inputs="$(PackagesDir)$(X509TestPackage)\content" Outputs="$(TestPath)%(TestTargetFramework.Folder)\*.*">
45+
<PropertyGroup>
46+
<X509TestPackage>System.Security.Cryptography.X509Certificates.TestData\1.0.0-prerelease</X509TestPackage>
47+
<TestTargetFrameworkFolder>%(TestTargetFramework.Folder)</TestTargetFrameworkFolder>
48+
</PropertyGroup>
49+
<ItemGroup>
50+
<X509TestDataSrc Include="$(PackagesDir)$(X509TestPackage)\content\**\*.*" />
51+
<X509TestDataDest Include="@(X509TestDataSrc->'$(TestPath)$(TestTargetFrameworkFolder)\%(RecursiveDir)%(Filename)%(Extension)')" />
52+
</ItemGroup>
53+
<Copy SourceFiles="@(X509TestDataSrc)" DestinationFiles="@(X509TestDataDest)" SkipUnchangedFiles="$(SkipCopyUnchangedFiles)" OverwriteReadOnlyFiles="$(OverwriteReadOnlyFiles)" Retries="$(CopyRetryCount)" RetryDelayMilliseconds="$(CopyRetryDelayMilliseconds)" UseHardlinksIfPossible="$(CreateHardLinksForCopyFilesToOutputDirectoryIfPossible)">
54+
<Output TaskParameter="DestinationFiles" ItemName="FileWrites" />
55+
</Copy>
56+
</Target>
4257
</Project>

src/System.Security.Cryptography.X509Certificates/tests/project.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"System.Security.Cryptography.Encoding": "4.0.0-beta-*",
99
"System.Security.Cryptography.Encryption": "4.0.0-beta-*",
1010
"System.Security.Cryptography.RSA": "4.0.0-beta-*",
11+
"System.Security.Cryptography.X509Certificates.TestData": "1.0.0-prerelease",
1112
"xunit": "2.0.0-beta5-build2785",
1213
"xunit.abstractions.netcore": "1.0.0-prerelease",
1314
"xunit.assert": "2.0.0-beta5-build2785",

src/System.Security.Cryptography.X509Certificates/tests/project.lock.json

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,7 @@
313313
"lib/DNXCore50/System.Security.Cryptography.RSA.dll"
314314
]
315315
},
316+
"System.Security.Cryptography.X509Certificates.TestData/1.0.0-prerelease": {},
316317
"System.Text.Encoding/4.0.10-beta-23008": {
317318
"dependencies": {
318319
"System.Runtime": "4.0.0-beta-23008"
@@ -757,6 +758,22 @@
757758
"ref/net46/System.Security.Cryptography.RSA.dll"
758759
]
759760
},
761+
"System.Security.Cryptography.X509Certificates.TestData/1.0.0-prerelease": {
762+
"sha512": "UbpXPyovZcwfKhYg/O85pLjFEO3hkrEjCLGus7P9y45Peoqrfc8XKzdCW5k5WIDCtLw9M1PJHGlOFV/U0D1tkg==",
763+
"files": [
764+
"System.Security.Cryptography.X509Certificates.TestData.1.0.0-prerelease.nupkg",
765+
"System.Security.Cryptography.X509Certificates.TestData.1.0.0-prerelease.nupkg.sha512",
766+
"System.Security.Cryptography.X509Certificates.TestData.nuspec",
767+
"content/TestData/DummyTcpServer.pfx",
768+
"content/TestData/microsoft.cer",
769+
"content/TestData/MS.cer",
770+
"content/TestData/My.cer",
771+
"content/TestData/My.pfx",
772+
"content/TestData/test.cer",
773+
"content/TestData/test.pfx",
774+
"content/TestData/Windows6.1-KB3004361-x64.msu"
775+
]
776+
},
760777
"System.Text.Encoding/4.0.10-beta-23008": {
761778
"sha512": "dDcnfqdj6stUj1o1mNZIxEDTI0er7TVER+CVcng+6rEejErS5HQjIEsuEepQSiAF3VDiZu8ZI89RAUuQbCSjEw==",
762779
"files": [
@@ -952,6 +969,7 @@
952969
"System.Security.Cryptography.Encoding >= 4.0.0-beta-*",
953970
"System.Security.Cryptography.Encryption >= 4.0.0-beta-*",
954971
"System.Security.Cryptography.RSA >= 4.0.0-beta-*",
972+
"System.Security.Cryptography.X509Certificates.TestData >= 1.0.0-prerelease",
955973
"xunit >= 2.0.0-beta5-build2785",
956974
"xunit.abstractions.netcore >= 1.0.0-prerelease",
957975
"xunit.assert >= 2.0.0-beta5-build2785",
@@ -961,4 +979,4 @@
961979
],
962980
"DNXCore,Version=v5.0": []
963981
}
964-
}
982+
}

0 commit comments

Comments
 (0)