Skip to content

Commit 7532f65

Browse files
authored
Handle platform not supported during test discovery.
When Browser is doing test discovery, it now more eagerly reads some static initialization that we were previously depending on conditionals preventing. This changes the initialization to handle platform not supported in more scenarios.
1 parent 21713d2 commit 7532f65

7 files changed

Lines changed: 70 additions & 7 deletions

File tree

src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/DSA/DSAKeyGeneration.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,16 @@ private static int GetSecondMin(KeySizes[] keySizes)
122122

123123
private static bool GetHasSecondMinSize()
124124
{
125-
using (DSA dsa = DSAFactory.Create())
125+
try
126+
{
127+
using (DSA dsa = DSAFactory.Create())
128+
{
129+
return GetSecondMin(dsa.LegalKeySizes) != int.MaxValue;
130+
}
131+
}
132+
catch (PlatformNotSupportedException)
126133
{
127-
return GetSecondMin(dsa.LegalKeySizes) != int.MaxValue;
134+
return false;
128135
}
129136
}
130137
}

src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/RSA/ImportExport.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -455,9 +455,9 @@ private static bool TestRsa16384()
455455

456456
return true;
457457
}
458-
catch (CryptographicException)
458+
catch (Exception e) when (e is CryptographicException or PlatformNotSupportedException)
459459
{
460-
// The key is too big for this platform.
460+
// The key is too big for this platform or the platform is not supported.
461461
return false;
462462
}
463463
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
4+
using System.Runtime.InteropServices;
5+
6+
namespace System.Security.Cryptography.EcDiffieHellman.Tests
7+
{
8+
public partial class ECDiffieHellmanProvider : IECDiffieHellmanProvider
9+
{
10+
public bool IsCurveValid(Oid oid) => false;
11+
public bool ExplicitCurvesSupported => false;
12+
public bool CanDeriveNewPublicKey => false;
13+
public bool SupportsRawDerivation => false;
14+
public bool SupportsSha3 => false;
15+
}
16+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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+
4+
using System.Runtime.InteropServices;
5+
6+
namespace System.Security.Cryptography.EcDsa.Tests
7+
{
8+
public partial class ECDsaProvider : IECDsaProvider
9+
{
10+
public bool IsCurveValid(Oid oid) => false;
11+
public bool ExplicitCurvesSupported => false;
12+
private static bool IsValueOrFriendlyNameValid(string friendlyNameOrValue) => false;
13+
}
14+
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,14 +388,18 @@
388388
<Compile Include="DefaultECDsaProvider.Windows.cs" />
389389
<Compile Include="DefaultECDiffieHellmanProvider.Windows.cs" />
390390
</ItemGroup>
391-
<ItemGroup Condition="'$(TargetPlatformIdentifier)' != 'windows' and '$(UseAndroidCrypto)' != 'true'">
391+
<ItemGroup Condition="'$(TargetPlatformIdentifier)' != 'windows' and '$(UseAndroidCrypto)' != 'true' and '$(TargetPlatformIdentifier)' != 'browser'">
392392
<Compile Include="DefaultECDsaProvider.Unix.cs" />
393393
<Compile Include="DefaultECDiffieHellmanProvider.Unix.cs" />
394394
<Compile Include="$(CommonPath)Interop\Unix\Interop.Libraries.cs"
395395
Link="Common\Interop\Unix\Interop.Libraries.cs" />
396396
<Compile Include="$(CommonPath)Interop\Unix\System.Security.Cryptography.Native\Interop.Initialization.cs"
397397
Link="Common\Interop\Unix\System.Security.Cryptography.Native\Interop.Initialization.cs" />
398398
</ItemGroup>
399+
<ItemGroup Condition="'$(TargetPlatformIdentifier)' == 'browser'">
400+
<Compile Include="DefaultECDsaProvider.Browser.cs" />
401+
<Compile Include="DefaultECDiffieHellmanProvider.Browser.cs" />
402+
</ItemGroup>
399403
<ItemGroup Condition="'$(UseAndroidCrypto)' == 'true' or '$(UseAppleCrypto)' == 'true'">
400404
<Compile Include="X509Certificates\X509StoreMutableTests.cs" />
401405
</ItemGroup>

src/libraries/System.Security.Cryptography/tests/X509Certificates/CertificateCreation/CertificateRequestChainTests.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,12 @@ private static bool DetectPssSupport()
534534
return false;
535535
}
536536

537+
if (PlatformDetection.IsBrowser)
538+
{
539+
// Browser doesn't support PSS or RSA at all.
540+
return false;
541+
}
542+
537543
using (X509Certificate2 cert = new X509Certificate2(TestData.PfxData, TestData.PfxDataPassword))
538544
using (RSA rsa = cert.GetRSAPrivateKey())
539545
{

src/libraries/System.Security.Cryptography/tests/X509Certificates/SignatureSupport.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,23 @@ public class SignatureSupport
1313
//
1414
// If there's ever a platform that blocks RSASSA+SHA-1 but doesn't block ECDSA or DSA with SHA-1,
1515
// the logic here will need to get more complicated.
16-
public static bool SupportsX509Sha1Signatures { get; } =
17-
System.Security.Cryptography.Tests.SignatureSupport.CanProduceSha1Signature(RSA.Create());
16+
public static bool SupportsX509Sha1Signatures { get; } = GetSupportsX509Sha1Signatures();
17+
18+
19+
private static bool GetSupportsX509Sha1Signatures()
20+
{
21+
RSA rsa;
22+
23+
try
24+
{
25+
rsa = RSA.Create();
26+
}
27+
catch (PlatformNotSupportedException)
28+
{
29+
return false;
30+
}
31+
32+
return System.Security.Cryptography.Tests.SignatureSupport.CanProduceSha1Signature(rsa);
33+
}
1834
}
1935
}

0 commit comments

Comments
 (0)