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

Commit 0a131dd

Browse files
authored
Change PlatformDetection.GetFrameworkVersion() to use registry key value instead of minor build number (#26094)
* Change PlatformDetection.GetFrameworkVersion() to use registry key value instead of minor build number * Address PR Feedback to do one liner getters
1 parent 270be00 commit 0a131dd

File tree

15 files changed

+59
-110
lines changed

15 files changed

+59
-110
lines changed

src/Common/tests/System/Security/Cryptography/AlgorithmImplementations/RSA/EncryptDecrypt.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public void NullArray_Throws()
2626

2727
public abstract class EncryptDecrypt
2828
{
29-
private static bool EphemeralKeysAreExportable => !PlatformDetection.IsFullFramework || PlatformDetection.IsNetfx462OrNewer();
29+
private static bool EphemeralKeysAreExportable => !PlatformDetection.IsFullFramework || PlatformDetection.IsNetfx462OrNewer;
3030

3131
protected abstract byte[] Encrypt(RSA rsa, byte[] data, RSAEncryptionPadding padding);
3232
protected abstract byte[] Decrypt(RSA rsa, byte[] data, RSAEncryptionPadding padding);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace System.Security.Cryptography.Rsa.Tests
1010
{
1111
public partial class ImportExport
1212
{
13-
private static bool EphemeralKeysAreExportable => !PlatformDetection.IsFullFramework || PlatformDetection.IsNetfx462OrNewer();
13+
private static bool EphemeralKeysAreExportable => !PlatformDetection.IsFullFramework || PlatformDetection.IsNetfx462OrNewer;
1414

1515
[ConditionalFact(nameof(EphemeralKeysAreExportable))]
1616
public static void ExportAutoKey()

src/Common/tests/System/Security/Cryptography/AlgorithmImplementations/RSA/SignVerify.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ public void NullArray_Throws()
3838

3939
public abstract class SignVerify
4040
{
41-
public static bool BadKeyFormatDoesntThrow => !PlatformDetection.IsFullFramework || PlatformDetection.IsNetfx462OrNewer();
42-
public static bool InvalidKeySizeDoesntThrow => !PlatformDetection.IsFullFramework || PlatformDetection.IsNetfx462OrNewer();
41+
public static bool BadKeyFormatDoesntThrow => !PlatformDetection.IsFullFramework || PlatformDetection.IsNetfx462OrNewer;
42+
public static bool InvalidKeySizeDoesntThrow => !PlatformDetection.IsFullFramework || PlatformDetection.IsNetfx462OrNewer;
4343

4444
protected abstract byte[] SignData(RSA rsa, byte[] data, HashAlgorithmName hashAlgorithm, RSASignaturePadding padding);
4545
protected abstract byte[] SignHash(RSA rsa, byte[] hash, HashAlgorithmName hashAlgorithm, RSASignaturePadding padding);

src/CoreFx.Private.TestUtilities/ref/CoreFx.Private.TestUtilities.cs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ public static partial class PlatformDetection
4040
{
4141
public static bool IsReflectionEmitSupported;
4242
public static bool ClientWebSocketPartialMessagesSupported { get { throw null; } }
43-
public static System.PlatformDetection.Range[] FrameworkRanges { get { throw null; } }
4443
public static bool HasWindowsShell { get { throw null; } }
4544
public static bool IsArmProcess { get { throw null; } }
4645
public static bool IsCentos6 { get { throw null; } }
@@ -54,6 +53,9 @@ public static partial class PlatformDetection
5453
public static bool IsNetBSD { get { throw null; } }
5554
public static bool IsFreeBSD { get { throw null; } }
5655
public static bool IsNetCore { get { throw null; } }
56+
public static bool IsNetfx462OrNewer { get { throw null; } }
57+
public static bool IsNetfx470OrNewer { get { throw null; } }
58+
public static bool IsNetfx471OrNewer { get { throw null; } }
5759
public static bool IsNetNative { get { throw null; } }
5860
public static bool IsNonZeroLowerBoundArraySupported { get { throw null; } }
5961
public static bool IsNotArmProcess { get { throw null; } }
@@ -102,19 +104,7 @@ public static partial class PlatformDetection
102104
public static System.Version OSXVersion { get { throw null; } }
103105
public static int WindowsVersion { get { throw null; } }
104106
public static string GetDistroVersionString() { throw null; }
105-
public static bool IsNetfx462OrNewer() { throw null; }
106-
public static bool IsNetfx470OrNewer() { throw null; }
107-
public static bool IsNetfx471OrNewer() { throw null; }
108107
public static bool TargetsNetFx452OrLower { get { throw null; } }
109-
110-
public partial class Range
111-
{
112-
public Range(System.Version start, System.Version finish, System.Version frameworkVersion) { }
113-
public System.Version Finish { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } }
114-
public System.Version FrameworkVersion { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } }
115-
public System.Version Start { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } }
116-
public bool IsInRange(System.Version version) { throw null; }
117-
}
118108
}
119109
public static partial class TheoryExtensions
120110
{

src/CoreFx.Private.TestUtilities/src/System/PlatformDetection.NetFx.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// See the LICENSE file in the project root for more information.
44

55
using Xunit;
6+
using Microsoft.Win32;
67
using System.Runtime.Versioning;
78

89
namespace System
@@ -16,5 +17,45 @@ public static partial class PlatformDetection
1617
// When we update xunit runner in the future which may target recent framework version, TargetsNetFx452OrLower can start return
1718
// false but we don't expect any code change though.
1819
public static bool TargetsNetFx452OrLower => TargetVersion.CompareTo(new Version(4, 5, 3, 0)) < 0;
20+
21+
public static bool IsNetfx462OrNewer => GetFrameworkVersion() >= new Version(4, 6, 2);
22+
23+
public static bool IsNetfx470OrNewer => GetFrameworkVersion() >= new Version(4, 7, 0);
24+
25+
public static bool IsNetfx471OrNewer => GetFrameworkVersion() >= new Version(4, 7, 1);
26+
27+
// To get the framework version we can do it throught the registry key and getting the Release value under the .NET Framework key.
28+
// the mapping to each version can be found in: https://docs.microsoft.com/en-us/dotnet/framework/migration-guide/how-to-determine-which-versions-are-installed
29+
// everytime we ship a new version this method should be updated to include the new framework version.
30+
private static Version GetFrameworkVersion()
31+
{
32+
using (RegistryKey ndpKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full"))
33+
{
34+
if (ndpKey != null)
35+
{
36+
int value = (int)(ndpKey.GetValue("Release") ?? 0);
37+
if (value >= 461308)
38+
return new Version(4, 7, 1);
39+
if (value >= 460798)
40+
return new Version(4, 7, 0);
41+
if (value >= 394802)
42+
return new Version(4, 6, 2);
43+
if (value >= 394254)
44+
return new Version(4, 6, 1);
45+
if (value >= 393295)
46+
return new Version(4, 6, 0);
47+
if (value >= 379893)
48+
return new Version(4, 5, 2);
49+
if (value >= 378675)
50+
return new Version(4, 5, 1);
51+
if (value >= 378389)
52+
return new Version(4, 5, 0);
53+
54+
throw new NotSupportedException($"No 4.5 or later framework version detected, framework key value: {value}");
55+
}
56+
57+
throw new NotSupportedException(@"No registry key found under 'SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full' to determine running framework version");
58+
}
59+
}
1960
}
2061
}

src/CoreFx.Private.TestUtilities/src/System/PlatformDetection.NonNetFx.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,8 @@ namespace System
99
public static partial class PlatformDetection
1010
{
1111
public static bool TargetsNetFx452OrLower => false;
12+
public static bool IsNetfx462OrNewer => false;
13+
public static bool IsNetfx470OrNewer => false;
14+
public static bool IsNetfx471OrNewer => false;
1215
}
1316
}

src/CoreFx.Private.TestUtilities/src/System/PlatformDetection.Unix.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@ public static partial class PlatformDetection
2121
public static bool IsWindows10Version1703OrGreater => false;
2222
public static bool IsWindows10Version1709OrGreater => false;
2323
public static bool IsNotOneCoreUAP => true;
24-
public static bool IsNetfx462OrNewer() { return false; }
25-
public static bool IsNetfx470OrNewer() { return false; }
26-
public static bool IsNetfx471OrNewer() { return false; }
2724
public static bool IsInAppContainer => false;
2825
public static int WindowsVersion => -1;
2926

src/CoreFx.Private.TestUtilities/src/System/PlatformDetection.Windows.cs

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -69,60 +69,6 @@ public static bool IsWindowsIoTCore
6969
public static bool IsWindows7 => GetWindowsVersion() == 6 && GetWindowsMinorVersion() == 1;
7070
public static bool IsWindows8x => GetWindowsVersion() == 6 && (GetWindowsMinorVersion() == 2 || GetWindowsMinorVersion() == 3);
7171

72-
public static bool IsNetfx462OrNewer()
73-
{
74-
if (!IsFullFramework)
75-
{
76-
return false;
77-
}
78-
79-
Version net462 = new Version(4, 6, 2);
80-
Version runningVersion = GetFrameworkVersion();
81-
return runningVersion != null && runningVersion >= net462;
82-
}
83-
84-
public static bool IsNetfx470OrNewer()
85-
{
86-
if (!IsFullFramework)
87-
{
88-
return false;
89-
}
90-
91-
Version net470 = new Version(4, 7, 0);
92-
Version runningVersion = GetFrameworkVersion();
93-
return runningVersion != null && runningVersion >= net470;
94-
}
95-
96-
public static bool IsNetfx471OrNewer()
97-
{
98-
if (!IsFullFramework)
99-
{
100-
return false;
101-
}
102-
103-
Version net471 = new Version(4, 7, 1);
104-
Version runningVersion = GetFrameworkVersion();
105-
return runningVersion != null && runningVersion >= net471;
106-
}
107-
108-
private static Version GetFrameworkVersion()
109-
{
110-
string[] descriptionArray = RuntimeInformation.FrameworkDescription.Split(' ');
111-
if (descriptionArray.Length < 3)
112-
return null;
113-
114-
if (!Version.TryParse(descriptionArray[2], out Version actualVersion))
115-
return null;
116-
117-
foreach (Range currentRange in FrameworkRanges)
118-
{
119-
if (currentRange.IsInRange(actualVersion))
120-
return currentRange.FrameworkVersion;
121-
}
122-
123-
return null;
124-
}
125-
12672
public static string GetDistroVersionString() { return "ProductType=" + GetWindowsProductType() + "InstallationType=" + GetInstallationType(); }
12773

12874
private static int s_isInAppContainer = -1;

src/CoreFx.Private.TestUtilities/src/System/PlatformDetection.cs

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -102,33 +102,5 @@ public static bool IsNonZeroLowerBoundArraySupported
102102
// System.Security.Cryptography.Xml.XmlDsigXsltTransform.GetOutput() relies on XslCompiledTransform which relies
103103
// heavily on Reflection.Emit
104104
public static bool IsXmlDsigXsltTransformSupported => !PlatformDetection.IsUap;
105-
106-
public static Range[] FrameworkRanges => new Range[]{
107-
new Range(new Version(4, 7, 2500, 0), null, new Version(4, 7, 1)),
108-
new Range(new Version(4, 6, 2000, 0), new Version(4, 7, 2090, 0), new Version(4, 7, 0)),
109-
new Range(new Version(4, 6, 1500, 0), new Version(4, 6, 1999, 0), new Version(4, 6, 2)),
110-
new Range(new Version(4, 6, 1000, 0), new Version(4, 6, 1499, 0), new Version(4, 6, 1)),
111-
new Range(new Version(4, 6, 55, 0), new Version(4, 6, 999, 0), new Version(4, 6, 0)),
112-
new Range(new Version(4, 0, 30319, 0), new Version(4, 0, 52313, 36313), new Version(4, 5, 2))
113-
};
114-
115-
public class Range
116-
{
117-
public Version Start { get; private set; }
118-
public Version Finish { get; private set; }
119-
public Version FrameworkVersion { get; private set; }
120-
121-
public Range(Version start, Version finish, Version frameworkVersion)
122-
{
123-
Start = start;
124-
Finish = finish;
125-
FrameworkVersion = frameworkVersion;
126-
}
127-
128-
public bool IsInRange(Version version)
129-
{
130-
return version >= Start && (Finish == null || version <= Finish);
131-
}
132-
}
133105
}
134106
}

0 commit comments

Comments
 (0)