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

Commit b72a51a

Browse files
committed
Make newly enabled file-load tests more robust.
Changed literal \\ path separators to use Path.Combine, and fixed the NotBefore/NotAfter verification to use a DateTime built from UTC, rather than require everyone to set their clocks to Redmond-time.
1 parent d77c30c commit b72a51a

File tree

4 files changed

+15
-13
lines changed

4 files changed

+15
-13
lines changed

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
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
@@ -16,7 +15,7 @@ public static void X509CertTest()
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("TestData\\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);
@@ -56,7 +55,10 @@ public static void X509Cert2Test()
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("TestData\\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);
@@ -121,7 +123,7 @@ public static void X509Cert2ToStringVerbose()
121123
[ActiveIssue(1993, PlatformID.AnyUnix)]
122124
public static void X509Cert2CreateFromPfxFile()
123125
{
124-
using (X509Certificate2 cert2 = new X509Certificate2("TestData\\DummyTcpServer.pfx"))
126+
using (X509Certificate2 cert2 = new X509Certificate2(Path.Combine("TestData", "DummyTcpServer.pfx")))
125127
{
126128
// OID=RSA Encryption
127129
Assert.Equal("1.2.840.113549.1.1.1", cert2.GetKeyAlgorithm());
@@ -132,7 +134,7 @@ public static void X509Cert2CreateFromPfxFile()
132134
[ActiveIssue(1993, PlatformID.AnyUnix)]
133135
public static void X509Cert2CreateFromPfxWithPassword()
134136
{
135-
using (X509Certificate2 cert2 = new X509Certificate2("TestData\\test.pfx", "test"))
137+
using (X509Certificate2 cert2 = new X509Certificate2(Path.Combine("TestData", "test.pfx"), "test"))
136138
{
137139
// OID=RSA Encryption
138140
Assert.Equal("1.2.840.113549.1.1.1", cert2.GetKeyAlgorithm());

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

Lines changed: 2 additions & 2 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
@@ -274,7 +274,7 @@ public static void ImportFromFileTests()
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: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ 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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public static void TestSetPrivateKey()
121121
[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);

0 commit comments

Comments
 (0)