diff --git a/UnitTests/CSharpRunner/TestHelper.cs b/UnitTests/CSharpRunner/TestHelper.cs index 08136d7a..88cf74a8 100644 --- a/UnitTests/CSharpRunner/TestHelper.cs +++ b/UnitTests/CSharpRunner/TestHelper.cs @@ -6,9 +6,13 @@ using System.Diagnostics; using System.Linq; using System.Net; using System.Runtime.InteropServices.ComTypes; +using System.Security.AccessControl; +using System.Security.Principal; using System.Threading; using Vanara.Extensions; using Vanara.InteropServices; +using Vanara.Windows.Shell; +using static Vanara.PInvoke.AdvApi32; namespace Vanara.PInvoke.Tests { @@ -23,10 +27,30 @@ namespace Vanara.PInvoke.Tests new GenJsonConverter(i => new SizeT(i), o => o.Value), new GenJsonConverter(dt => dt.ToFileTimeStruct(), ft => ft.ToDateTime()), new GenJsonConverter(i => IPAddress.Parse(i), o => o.ToString()), + new GenJsonConverter(i => new RawSecurityDescriptor(i), o => o.GetSddlForm(AccessControlSections.All)), + new GenJsonConverter(i => new(i), o => o.ToString()), + new GenJsonConverter(i => new(i), o => o.ToString()), }, ReferenceLoopHandling = ReferenceLoopHandling.Serialize }); + /// Gets a value indicating whether the current process is elevated. + /// if the current process is elevated; otherwise, . + public static bool IsElevated + { + get + { + try + { + // Open the access token of the current process with TOKEN_QUERY. + using var hObject = SafeHTOKEN.FromProcess(Process.GetCurrentProcess(), TokenAccess.TOKEN_QUERY | TokenAccess.TOKEN_DUPLICATE); + return hObject.IsElevated; + } + catch { } + return false; + } + } + public static Process RunThrottleApp() => Process.Start(testApp); public static void SetThrottle(string type, bool on) @@ -106,6 +130,9 @@ Simple: case SafeAllocatedMemoryHandleBase mem: return mem.Dump; + case System.Security.AccessControl.GenericSecurityDescriptor sd: + return sd.GetSddlForm(System.Security.AccessControl.AccessControlSections.All); + default: try { return JsonConvert.SerializeObject(value, Formatting.Indented, jsonSet.Value); } catch (Exception e) { return e.ToString(); } @@ -116,8 +143,8 @@ Simple: private class GenJsonConverter : JsonConverter { - Func rdr; - Func wtr; + private readonly Func rdr; + private readonly Func wtr; public GenJsonConverter(Func r, Func w) {