-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
Utilities.cs
456 lines (392 loc) · 15.8 KB
/
Utilities.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
//
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Loader;
using System.Security;
using System.Text;
using System.Threading;
using Xunit;
namespace TestLibrary
{
public static partial class Utilities
{
static volatile bool verbose;
static volatile bool verboseSet = false;
const char HIGH_SURROGATE_START = '\ud800';
const char HIGH_SURROGATE_END = '\udbff';
const char LOW_SURROGATE_START = '\udc00';
const char LOW_SURROGATE_END = '\udfff';
static string sTestDirectory = string.Empty;
public static string TestDirectory
{
get
{
return sTestDirectory;
}
set
{
sTestDirectory = value;
}
}
public static bool Verbose
{
get
{
if (!verboseSet)
{
verboseSet = true;
verbose = false;
}
return (bool)verbose;
}
set
{
verbose = value;
}
}
public static bool IsX86 => (RuntimeInformation.ProcessArchitecture == Architecture.X86);
public static bool IsNotX86 => !IsX86;
public static bool IsX64 => (RuntimeInformation.ProcessArchitecture == Architecture.X64);
public static bool IsArm => (RuntimeInformation.ProcessArchitecture == Architecture.Arm);
public static bool IsArm64 => (RuntimeInformation.ProcessArchitecture == Architecture.Arm64);
public static bool IsWindows => OperatingSystem.IsWindows();
public static bool IsLinux => OperatingSystem.IsLinux();
public static bool IsMacOSX => OperatingSystem.IsMacOS();
public static bool IsWindows7 => IsWindows && Environment.OSVersion.Version.Major == 6 && Environment.OSVersion.Version.Minor == 1;
public static bool IsWindowsNanoServer => (!IsWindowsIoTCore && GetInstallationType().Equals("Nano Server", StringComparison.OrdinalIgnoreCase));
// Windows 10 October 2018 Update
public static bool IsWindows10Version1809OrGreater =>
IsWindows && GetWindowsVersion() == 10 && GetWindowsMinorVersion() == 0 && GetWindowsBuildNumber() >= 17763;
public static bool IsWindowsIoTCore
{
get
{
if (IsWindows)
{
int productType = GetWindowsProductType();
return productType == Kernel32.PRODUCT_IOTUAPCOMMERCIAL
|| productType == Kernel32.PRODUCT_IOTUAP;
}
return false;
}
}
// return whether or not the OS is a 64 bit OS
public static bool Is64 => (IntPtr.Size == 8);
public static bool IsMonoRuntime => Type.GetType("Mono.RuntimeStructs") != null;
public static bool IsNotMonoRuntime => !IsMonoRuntime;
public static bool IsNativeAot => IsNotMonoRuntime && !IsReflectionEmitSupported;
#if NETCOREAPP
public static bool IsReflectionEmitSupported => RuntimeFeature.IsDynamicCodeSupported;
public static bool IsNotReflectionEmitSupported => !IsReflectionEmitSupported;
#else
public static bool IsReflectionEmitSupported => true;
#endif
public static bool SupportsExceptionInterop => IsWindows && IsNotMonoRuntime && !IsNativeAot; // matches definitions in clr.featuredefines.props
public static bool IsGCStress => (Environment.GetEnvironmentVariable("COMPlus_GCStress") != null) || (Environment.GetEnvironmentVariable("DOTNET_GCStress") != null);
public static string ByteArrayToString(byte[] bytes)
{
StringBuilder sb = new StringBuilder();
foreach (byte b in bytes)
{
sb.Append(b.ToString("x2"));
sb.Append(", ");
}
if (bytes.Length > 0)
sb.Remove(sb.Length - 2, 2);
return sb.ToString();
}
public static bool CompareBytes(byte[] arr1, byte[] arr2)
{
if (arr1 == null) return (arr2 == null);
if (arr2 == null) return false;
if (arr1.Length != arr2.Length) return false;
for (int i = 0; i < arr1.Length; i++) if (arr1[i] != arr2[i]) return false;
return true;
}
// Given a string, display the unicode characters in hex format, optionally displaying each
// characters unicode category
public static string FormatHexStringFromUnicodeString(string string1, bool includeUnicodeCategory)
{
string returnString = "";
if (null == string1)
{
return null;
}
foreach (char x in string1)
{
string tempString = FormatHexStringFromUnicodeChar(x, includeUnicodeCategory);
if (!returnString.Equals("") && !includeUnicodeCategory)
{
returnString = returnString + " " + tempString;
}
else
{
returnString += tempString;
}
}
return returnString;
}
// Given a character, display its unicode value in hex format.
public static string FormatHexStringFromUnicodeChar(char char1, bool includeUnicodeCategory)
{
if (includeUnicodeCategory)
throw new Exception("Win8P does not support Char.UnicodeCategory");
return ((int)char1).ToString("X4");
}
public static CultureInfo CurrentCulture
{
get { return System.Globalization.CultureInfo.CurrentCulture; }
set
{
System.Globalization.CultureInfo.DefaultThreadCurrentCulture = value;
}
}
[MethodImpl(MethodImplOptions.NoInlining)]
private static int GetWindowsProductType()
{
if (!Kernel32.GetProductInfo(Environment.OSVersion.Version.Major, Environment.OSVersion.Version.Minor, 0, 0, out int productType))
{
return Kernel32.PRODUCT_UNDEFINED;
}
return productType;
}
private static string GetInstallationType()
{
if (IsWindows)
{
return GetInstallationTypeForWindows();
}
return string.Empty;
}
private static string GetInstallationTypeForWindows()
{
try
{
string key = @"SOFTWARE\Microsoft\Windows NT\CurrentVersion";
string value = "InstallationType";
return GetRegistryValueString(key, value);
}
catch (Exception e)
{
Console.WriteLine($"{nameof(GetInstallationTypeForWindows)} exception: {e}");
return string.Empty;
}
}
internal static uint GetWindowsVersion()
{
if (!IsWindows)
{
return 0;
}
Assert.Equal(0, Ntdll.RtlGetVersionEx(out Ntdll.RTL_OSVERSIONINFOEX osvi));
return osvi.dwMajorVersion;
}
internal static uint GetWindowsMinorVersion()
{
if (!IsWindows)
{
return 0;
}
Assert.Equal(0, Ntdll.RtlGetVersionEx(out Ntdll.RTL_OSVERSIONINFOEX osvi));
return osvi.dwMinorVersion;
}
internal static uint GetWindowsBuildNumber()
{
if (!IsWindows)
{
return 0;
}
Assert.Equal(0, Ntdll.RtlGetVersionEx(out Ntdll.RTL_OSVERSIONINFOEX osvi));
return osvi.dwBuildNumber;
}
[MethodImpl(MethodImplOptions.NoInlining)]
private static string GetRegistryValueString(string key, string value)
{
int dataSize = 0;
Advapi32.RType type;
int result = Advapi32.RegGetValueW(
Advapi32.HKEY_LOCAL_MACHINE,
key,
value,
Advapi32.RFlags.RRF_RT_REG_SZ,
out type,
IntPtr.Zero,
ref dataSize);
if (result != 0 || type != Advapi32.RType.RegSz)
{
throw new Exception($"Invalid {nameof(Advapi32.RegGetValueW)} result: 0x{result:x} type: {type}");
}
IntPtr data = Marshal.AllocCoTaskMem(dataSize + 1);
result = Advapi32.RegGetValueW(
Advapi32.HKEY_LOCAL_MACHINE,
key,
value,
Advapi32.RFlags.RRF_RT_REG_SZ,
out type,
data,
ref dataSize);
if (result != 0 || type != Advapi32.RType.RegSz)
{
throw new Exception($"Invalid {nameof(Advapi32.RegGetValueW)} result: 0x{result:x} type: {type}");
}
string stringValue = Marshal.PtrToStringUni(data);
Marshal.FreeCoTaskMem(data);
return stringValue;
}
private static class Ntdll
{
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
internal unsafe struct RTL_OSVERSIONINFOEX
{
internal uint dwOSVersionInfoSize;
internal uint dwMajorVersion;
internal uint dwMinorVersion;
internal uint dwBuildNumber;
internal uint dwPlatformId;
internal fixed char szCSDVersion[128];
}
[DllImport(nameof(Ntdll), ExactSpelling=true)]
private static extern int RtlGetVersion(ref RTL_OSVERSIONINFOEX lpVersionInformation);
internal static unsafe int RtlGetVersionEx(out RTL_OSVERSIONINFOEX osvi)
{
osvi = new RTL_OSVERSIONINFOEX();
osvi.dwOSVersionInfoSize = (uint)sizeof(RTL_OSVERSIONINFOEX);
return RtlGetVersion(ref osvi);
}
internal static unsafe string RtlGetVersion()
{
const string Version = "Microsoft Windows";
if (RtlGetVersionEx(out RTL_OSVERSIONINFOEX osvi) == 0)
{
return osvi.szCSDVersion[0] != '\0' ?
string.Format("{0} {1}.{2}.{3} {4}", Version, osvi.dwMajorVersion, osvi.dwMinorVersion, osvi.dwBuildNumber, new string(&(osvi.szCSDVersion[0]))) :
string.Format("{0} {1}.{2}.{3}", Version, osvi.dwMajorVersion, osvi.dwMinorVersion, osvi.dwBuildNumber);
}
else
{
return Version;
}
}
}
private sealed class Kernel32
{
public const int PRODUCT_UNDEFINED = 0;
public const int PRODUCT_IOTUAP = 0x0000007B;
public const int PRODUCT_IOTUAPCOMMERCIAL = 0x00000083;
public const int PRODUCT_CORE = 0x00000065;
public const int PRODUCT_CORE_COUNTRYSPECIFIC = 0x00000063;
public const int PRODUCT_CORE_N = 0x00000062;
public const int PRODUCT_CORE_SINGLELANGUAGE = 0x00000064;
public const int PRODUCT_HOME_BASIC = 0x00000002;
public const int PRODUCT_HOME_BASIC_N = 0x00000005;
public const int PRODUCT_HOME_PREMIUM = 0x00000003;
public const int PRODUCT_HOME_PREMIUM_N = 0x0000001A;
/// <summary>
/// https://docs.microsoft.com/en-us/windows/desktop/api/sysinfoapi/nf-sysinfoapi-getproductinfo
/// </summary>
[DllImport(nameof(Kernel32), SetLastError = false)]
public static extern bool GetProductInfo(
int dwOSMajorVersion,
int dwOSMinorVersion,
int dwSpMajorVersion,
int dwSpMinorVersion,
out int pdwReturnedProductType);
}
private sealed class Advapi32
{
/// <summary>
/// http://msdn.microsoft.com/en-us/library/windows/desktop/ms724884(v=vs.85).aspx
/// </summary>
public enum RFlags
{
/// <summary>
/// Any
/// </summary>
Any = 0xffff,
/// <summary>
/// A null-terminated string.
/// This will be either a Unicode or an ANSI string, depending on whether you use the Unicode or ANSI function.
/// </summary>
RRF_RT_REG_SZ = 2,
}
/// <summary>
/// http://msdn.microsoft.com/en-us/library/windows/desktop/ms724884(v=vs.85).aspx
/// </summary>
public enum RType
{
/// <summary>
/// No defined value type
/// </summary>
RegNone = 0,
/// <summary>
/// A null-terminated string.
/// This will be either a Unicode or an ANSI string, depending on whether you use the Unicode or ANSI function.
/// </summary>
RegSz = 1,
}
[DllImport(nameof(Advapi32), CharSet = CharSet.Unicode, SetLastError = true)]
public static extern int RegGetValueW(
IntPtr hkey,
string lpSubKey,
string lpValue,
RFlags dwFlags,
out RType pdwType,
IntPtr pvData,
ref int pcbData);
public static IntPtr HKEY_LOCAL_MACHINE => new IntPtr(unchecked((int)0x80000002));
}
class TestAssemblyLoadContext : AssemblyLoadContext
{
public TestAssemblyLoadContext() : base(isCollectible: true)
{
}
protected override Assembly Load(AssemblyName assemblyName)
{
return null;
}
}
[MethodImpl(MethodImplOptions.NoInlining)]
static int ExecuteAndUnloadInternal(string assemblyPath, string[] args, Action<AssemblyLoadContext> unloadingCallback, out WeakReference alcWeakRef)
{
TestAssemblyLoadContext alc = new TestAssemblyLoadContext();
if (unloadingCallback != null)
{
alc.Unloading += unloadingCallback;
}
alcWeakRef = new WeakReference(alc);
Assembly a = alc.LoadFromAssemblyPath(assemblyPath);
object[] argsObjArray = (a.EntryPoint.GetParameters().Length != 0) ? new object[] { args } : null;
object res = a.EntryPoint.Invoke(null, argsObjArray);
alc.Unload();
return (a.EntryPoint.ReturnType == typeof(void)) ? Environment.ExitCode : Convert.ToInt32(res);
}
public static int ExecuteAndUnload(string assemblyPath, string[] args, Action<AssemblyLoadContext> unloadingCallback = null)
{
WeakReference alcWeakRef;
int exitCode;
exitCode = ExecuteAndUnloadInternal(assemblyPath, args, unloadingCallback, out alcWeakRef);
for (int i = 0; i < 8 && alcWeakRef.IsAlive; i++)
{
GC.Collect();
GC.WaitForPendingFinalizers();
}
if (alcWeakRef.IsAlive)
{
exitCode += 100;
Console.WriteLine($"Unload failed - {assemblyPath}");
}
else
{
Console.WriteLine($"Unload succeeded - {assemblyPath}");
}
return exitCode;
}
}
}