mirror of https://github.com/dahall/Vanara.git
Completed unit tests and updates to SID functions in SecurityBaseApi.h
parent
871ad3b8ce
commit
38ab24c904
|
@ -26,7 +26,7 @@ namespace Vanara.PInvoke
|
|||
|
||||
/// <summary>Initializes a new instance of the <see cref="SafePSID"/> class.</summary>
|
||||
/// <param name="size">The size of memory to allocate, in bytes.</param>
|
||||
public SafePSID(int size) : base(size)
|
||||
public SafePSID(SizeT size) : base(size)
|
||||
{
|
||||
}
|
||||
|
||||
|
|
|
@ -210,9 +210,42 @@ namespace Vanara.PInvoke
|
|||
/// </remarks>
|
||||
// https://docs.microsoft.com/en-us/windows/desktop/api/securitybaseapi/nf-securitybaseapi-getsididentifierauthority
|
||||
// PSID_IDENTIFIER_AUTHORITY GetSidIdentifierAuthority( PSID pSid );
|
||||
[DllImport(Lib.AdvApi32, SetLastError = true, ExactSpelling = true)]
|
||||
[DllImport(Lib.AdvApi32, SetLastError = true, EntryPoint = "GetSidIdentifierAuthority")]
|
||||
[PInvokeData("securitybaseapi.h", MSDNShortId = "67a06e7b-775f-424c-ab36-0fc9b93b801a")]
|
||||
public static extern PSID_IDENTIFIER_AUTHORITY GetSidIdentifierAuthority(PSID pSid);
|
||||
internal static extern IntPtr InternalGetSidIdentifierAuthority(PSID pSid);
|
||||
|
||||
/// <summary>
|
||||
/// <para>
|
||||
/// The <c>GetSidIdentifierAuthority</c> function returns a pointer to the SID_IDENTIFIER_AUTHORITY structure in a specified security
|
||||
/// identifier (SID).
|
||||
/// </para>
|
||||
/// </summary>
|
||||
/// <param name="pSid">
|
||||
/// <para>A pointer to the SID structure for which a pointer to the SID_IDENTIFIER_AUTHORITY structure is returned.</para>
|
||||
/// <para>
|
||||
/// This function does not handle SID structures that are not valid. Call the IsValidSid function to verify that the <c>SID</c>
|
||||
/// structure is valid before you call this function.
|
||||
/// </para>
|
||||
/// </param>
|
||||
/// <returns>
|
||||
/// <para>
|
||||
/// If the function succeeds, the return value is a pointer to the SID_IDENTIFIER_AUTHORITY structure for the specified SID structure.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// If the function fails, the return value is undefined. The function fails if the SID structure pointed to by the pSid parameter is
|
||||
/// not valid. To get extended error information, call GetLastError.
|
||||
/// </para>
|
||||
/// </returns>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// This function uses a 32-bit RID value. For applications that require a larger RID value, use CreateWellKnownSid and related functions.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
// https://docs.microsoft.com/en-us/windows/desktop/api/securitybaseapi/nf-securitybaseapi-getsididentifierauthority
|
||||
// PSID_IDENTIFIER_AUTHORITY GetSidIdentifierAuthority( PSID pSid );
|
||||
[PInvokeData("securitybaseapi.h", MSDNShortId = "67a06e7b-775f-424c-ab36-0fc9b93b801a")]
|
||||
public static PSID_IDENTIFIER_AUTHORITY GetSidIdentifierAuthority(PSID pSid) =>
|
||||
new PSID_IDENTIFIER_AUTHORITY(InternalGetSidIdentifierAuthority(pSid));
|
||||
|
||||
/// <summary>
|
||||
/// <para>
|
||||
|
@ -256,9 +289,34 @@ namespace Vanara.PInvoke
|
|||
/// index value specified by the nSubAuthority parameter is out of bounds.
|
||||
/// </para>
|
||||
/// </returns>
|
||||
[DllImport(Lib.AdvApi32, ExactSpelling = true, SetLastError = true)]
|
||||
[DllImport(Lib.AdvApi32, EntryPoint = "GetSidSubAuthority", SetLastError = true)]
|
||||
[PInvokeData("securitybaseapi.h", MSDNShortId = "aa446657")]
|
||||
public static extern IntPtr GetSidSubAuthority(PSID pSid, uint nSubAuthority);
|
||||
internal static extern IntPtr InternalGetSidSubAuthority(PSID pSid, uint nSubAuthority);
|
||||
|
||||
/// <summary>
|
||||
/// The GetSidSubAuthority function returns a pointer to a specified subauthority in a security identifier (SID). The subauthority
|
||||
/// value is a relative identifier (RID).
|
||||
/// </summary>
|
||||
/// <param name="pSid">A pointer to the SID structure from which a pointer to a subauthority is to be returned.</param>
|
||||
/// <param name="nSubAuthority">
|
||||
/// Specifies an index value identifying the subauthority array element whose address the function will return. The function performs
|
||||
/// no validation tests on this value. An application can call the GetSidSubAuthorityCount function to discover the range of
|
||||
/// acceptable values.
|
||||
/// </param>
|
||||
/// <returns>
|
||||
/// On success, the return value is the specified SID subauthority.
|
||||
/// <para>
|
||||
/// If the function fails, an exception is thrown. The function fails if the specified SID structure is not valid or if the index
|
||||
/// value specified by the nSubAuthority parameter is out of bounds.
|
||||
/// </para>
|
||||
/// </returns>
|
||||
[PInvokeData("securitybaseapi.h", MSDNShortId = "aa446657")]
|
||||
public static uint GetSidSubAuthority(PSID pSid, uint nSubAuthority)
|
||||
{
|
||||
var ptr = InternalGetSidSubAuthority(pSid, nSubAuthority);
|
||||
Win32Error.GetLastError().ThrowIfFailed();
|
||||
return unchecked((uint)Marshal.ReadInt32(ptr));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The <c>GetSidSubAuthorityCount</c> function returns a pointer to the member in a security identifier (SID) structure that
|
||||
|
@ -284,9 +342,41 @@ namespace Vanara.PInvoke
|
|||
/// </remarks>
|
||||
// https://docs.microsoft.com/en-us/windows/desktop/api/securitybaseapi/nf-securitybaseapi-getsidsubauthoritycount PUCHAR
|
||||
// GetSidSubAuthorityCount( PSID pSid );
|
||||
[DllImport(Lib.AdvApi32, SetLastError = true, ExactSpelling = true)]
|
||||
[DllImport(Lib.AdvApi32, SetLastError = true, EntryPoint = "GetSidSubAuthorityCount")]
|
||||
[PInvokeData("securitybaseapi.h", MSDNShortId = "ca81fb91-f5a1-4dc6-83ec-eadb62a37805")]
|
||||
public static extern IntPtr GetSidSubAuthorityCount(PSID pSid);
|
||||
internal static extern IntPtr InternalGetSidSubAuthorityCount(PSID pSid);
|
||||
|
||||
/// <summary>
|
||||
/// The <c>GetSidSubAuthorityCount</c> function returns a pointer to the member in a security identifier (SID) structure that
|
||||
/// contains the subauthority count.
|
||||
/// </summary>
|
||||
/// <param name="pSid">
|
||||
/// <para>A pointer to the SID structure from which a pointer to the subauthority count is returned.</para>
|
||||
/// <para>
|
||||
/// This function does not handle SID structures that are not valid. Call the IsValidSid function to verify that the <c>SID</c>
|
||||
/// structure is valid before you call this function.
|
||||
/// </para>
|
||||
/// </param>
|
||||
/// <returns>
|
||||
/// <para>If the function succeeds, the return value is the subauthority count for the specified SID structure.</para>
|
||||
/// <para>
|
||||
/// If the function fails, an exception is thrown. The function fails if the specified SID structure is not valid. To get
|
||||
/// extended error information, call GetLastError.
|
||||
/// </para>
|
||||
/// </returns>
|
||||
/// <remarks>
|
||||
/// The SID structure specified in pSid uses a 32-bit value. For applications that require longer RID values, use CreateWellKnownSid
|
||||
/// and related functions.
|
||||
/// </remarks>
|
||||
// https://docs.microsoft.com/en-us/windows/desktop/api/securitybaseapi/nf-securitybaseapi-getsidsubauthoritycount PUCHAR
|
||||
// GetSidSubAuthorityCount( PSID pSid );
|
||||
[PInvokeData("securitybaseapi.h", MSDNShortId = "ca81fb91-f5a1-4dc6-83ec-eadb62a37805")]
|
||||
public static byte GetSidSubAuthorityCount(PSID pSid)
|
||||
{
|
||||
var ptr = InternalGetSidSubAuthorityCount(pSid);
|
||||
Win32Error.GetLastError().ThrowIfFailed();
|
||||
return Marshal.ReadByte(ptr);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The <c>GetWindowsAccountDomainSid</c> function receives a security identifier (SID) and returns a SID representing the domain of
|
||||
|
@ -308,6 +398,29 @@ namespace Vanara.PInvoke
|
|||
[return: MarshalAs(UnmanagedType.Bool)]
|
||||
public static extern bool GetWindowsAccountDomainSid(PSID pSid, SafePSID pDomainSid, ref uint cbDomainSid);
|
||||
|
||||
/// <summary>
|
||||
/// The <c>GetWindowsAccountDomainSid</c> function receives a security identifier (SID) and returns a SID representing the domain of
|
||||
/// that SID.
|
||||
/// </summary>
|
||||
/// <param name="pSid">A pointer to the SID to examine.</param>
|
||||
/// <param name="pDomainSid">An allocated safe pointer to a SID representing the domain.</param>
|
||||
/// <returns>
|
||||
/// <para>Returns <see langword="true"/> if successful.</para>
|
||||
/// <para>Otherwise, returns <see langword="false"/>. For extended error information, call GetLastError.</para>
|
||||
/// </returns>
|
||||
[PInvokeData("securitybaseapi.h", MSDNShortId = "ee2ba1b4-1bef-4d79-bb18-512705e2c378")]
|
||||
public static bool GetWindowsAccountDomainSid(PSID pSid, out SafePSID pDomainSid)
|
||||
{
|
||||
uint sz = 0;
|
||||
if (!GetWindowsAccountDomainSid(pSid, SafePSID.Null, ref sz) && sz == 0)
|
||||
{
|
||||
pDomainSid = SafePSID.Null;
|
||||
return false;
|
||||
}
|
||||
pDomainSid = new SafePSID(sz);
|
||||
return GetWindowsAccountDomainSid(pSid, pDomainSid, ref sz);
|
||||
}
|
||||
|
||||
/// <summary>The <c>InitializeSid</c> function initializes a security identifier (SID).</summary>
|
||||
/// <param name="Sid">A pointer to a SID structure to be initialized.</param>
|
||||
/// <param name="pIdentifierAuthority">A pointer to a SID_IDENTIFIER_AUTHORITY structure to set in the SID structure.</param>
|
||||
|
|
|
@ -4282,7 +4282,7 @@ namespace Vanara.PInvoke
|
|||
{
|
||||
/// <summary>An array of 6 bytes specifying a SID's top-level authority.</summary>
|
||||
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 6)]
|
||||
public byte[] Value;
|
||||
public byte[] Value = new byte[6];
|
||||
|
||||
/// <summary>Initializes a new instance of the <see cref="SID_IDENTIFIER_AUTHORITY"/> struct.</summary>
|
||||
/// <param name="value">The value.</param>
|
||||
|
@ -4291,7 +4291,6 @@ namespace Vanara.PInvoke
|
|||
{
|
||||
if (value == null || value.Length != 6)
|
||||
throw new ArgumentOutOfRangeException(nameof(value));
|
||||
Value = new byte[6];
|
||||
Array.Copy(value, Value, 6);
|
||||
}
|
||||
|
||||
|
@ -4299,10 +4298,17 @@ namespace Vanara.PInvoke
|
|||
/// <param name="value">The value.</param>
|
||||
public PSID_IDENTIFIER_AUTHORITY(long value)
|
||||
{
|
||||
Value = new byte[6];
|
||||
LongValue = value;
|
||||
}
|
||||
|
||||
internal PSID_IDENTIFIER_AUTHORITY(IntPtr existingPtr)
|
||||
{
|
||||
if (existingPtr == IntPtr.Zero)
|
||||
Value = existingPtr.ToArray<byte>(6);
|
||||
}
|
||||
|
||||
private PSID_IDENTIFIER_AUTHORITY() { }
|
||||
|
||||
/// <summary>Gets or sets the long value.</summary>
|
||||
/// <value>The long value.</value>
|
||||
public long LongValue
|
||||
|
@ -4311,10 +4317,7 @@ namespace Vanara.PInvoke
|
|||
{
|
||||
long nAuthority = 0;
|
||||
for (var i = 0; i <= 5; i++)
|
||||
{
|
||||
nAuthority <<= 8;
|
||||
nAuthority |= Value[i];
|
||||
}
|
||||
nAuthority |= (Value[i] << (8 * i));
|
||||
return nAuthority;
|
||||
}
|
||||
set
|
||||
|
@ -4334,6 +4337,21 @@ namespace Vanara.PInvoke
|
|||
/// <param name="sia">The sia.</param>
|
||||
/// <returns>The result of the conversion.</returns>
|
||||
public static implicit operator PSID_IDENTIFIER_AUTHORITY(SID_IDENTIFIER_AUTHORITY sia) => new PSID_IDENTIFIER_AUTHORITY(sia.Value);
|
||||
|
||||
/// <summary>Implements the operator !=.</summary>
|
||||
/// <param name="h1">The first handle.</param>
|
||||
/// <param name="h2">The second handle.</param>
|
||||
/// <returns>The result of the operator.</returns>
|
||||
public static bool operator !=(PSID_IDENTIFIER_AUTHORITY h1, PSID_IDENTIFIER_AUTHORITY h2) => !(h1 == h2);
|
||||
|
||||
/// <summary>Implements the operator ==.</summary>
|
||||
/// <param name="h1">The first handle.</param>
|
||||
/// <param name="h2">The second handle.</param>
|
||||
/// <returns>The result of the operator.</returns>
|
||||
public static bool operator ==(PSID_IDENTIFIER_AUTHORITY h1, PSID_IDENTIFIER_AUTHORITY h2) => h1.Equals(h2);
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override bool Equals(object obj) => obj is PSID_IDENTIFIER_AUTHORITY h ? h.LongValue == h.LongValue : false;
|
||||
}
|
||||
|
||||
/// <summary>The TOKEN_PRIVILEGES structure contains information about a set of privileges for an access token.</summary>
|
||||
|
|
|
@ -44,31 +44,6 @@ namespace Vanara.PInvoke.Tests
|
|||
}
|
||||
}
|
||||
|
||||
[Test()]
|
||||
public void AllocateAndInitializeSidTest()
|
||||
{
|
||||
var b = AllocateAndInitializeSid(KnownSIDAuthority.SECURITY_WORLD_SID_AUTHORITY, 1, KnownSIDRelativeID.SECURITY_WORLD_RID, 0, 0, 0, 0, 0, 0, 0, out var pSid);
|
||||
Assert.That(b);
|
||||
var everyone = new SecurityIdentifier(WellKnownSidType.WorldSid, null);
|
||||
var esid = new byte[everyone.BinaryLength];
|
||||
everyone.GetBinaryForm(esid, 0);
|
||||
var peSid = new SafeByteArray(esid);
|
||||
Assert.That(EqualSid(pSid, (IntPtr)peSid));
|
||||
ConvertStringSidToSid("S-1-2-0", out var lsid);
|
||||
Assert.That(EqualSid(pSid, (IntPtr)lsid), Is.False);
|
||||
string s = null;
|
||||
Assert.That(IsValidSid(pSid), Is.True);
|
||||
Assert.That(() => s = ConvertSidToStringSid(pSid), Throws.Nothing);
|
||||
Assert.That(s, Is.EqualTo("S-1-1-0"));
|
||||
var saptr = GetSidSubAuthority(pSid, 0);
|
||||
Assert.That(Marshal.ReadInt32(saptr), Is.EqualTo(0));
|
||||
var len = GetLengthSid(pSid);
|
||||
var p2 = new SafePSID(len);
|
||||
b = CopySid(len, (IntPtr)p2, pSid);
|
||||
Assert.That(EqualSid(p2, pSid));
|
||||
Assert.That(b);
|
||||
}
|
||||
|
||||
[Test()]
|
||||
[PrincipalPermission(SecurityAction.Assert, Role = "Administrators")]
|
||||
public void ChangeAndQueryServiceConfigTest()
|
||||
|
|
|
@ -4,6 +4,7 @@ using System.Linq;
|
|||
using System.Runtime.InteropServices;
|
||||
using System.Security.Principal;
|
||||
using Vanara.Extensions;
|
||||
using Vanara.InteropServices;
|
||||
using static Vanara.PInvoke.AdvApi32;
|
||||
|
||||
namespace Vanara.PInvoke.Tests
|
||||
|
@ -23,32 +24,72 @@ namespace Vanara.PInvoke.Tests
|
|||
[TestFixture()]
|
||||
public class PSIDTests
|
||||
{
|
||||
public static SafePSID GetCurrentSid() => new SafePSID(WindowsIdentity.GetCurrent().User.GetBytes());
|
||||
[Test()]
|
||||
public void AllocateAndInitializeSidTest()
|
||||
{
|
||||
var b = AllocateAndInitializeSid(KnownSIDAuthority.SECURITY_WORLD_SID_AUTHORITY, 1, KnownSIDRelativeID.SECURITY_WORLD_RID, 0, 0, 0, 0, 0, 0, 0, out var pSid);
|
||||
Assert.That(b);
|
||||
var everyone = new SecurityIdentifier(WellKnownSidType.WorldSid, null);
|
||||
var esid = new byte[everyone.BinaryLength];
|
||||
everyone.GetBinaryForm(esid, 0);
|
||||
var peSid = new SafeByteArray(esid);
|
||||
Assert.That(EqualSid(pSid, (IntPtr)peSid));
|
||||
ConvertStringSidToSid("S-1-2-0", out var lsid);
|
||||
Assert.That(EqualSid(pSid, (IntPtr)lsid), Is.False);
|
||||
string s = null;
|
||||
Assert.That(IsValidSid(pSid), Is.True);
|
||||
Assert.That(() => s = ConvertSidToStringSid(pSid), Throws.Nothing);
|
||||
Assert.That(s, Is.EqualTo("S-1-1-0"));
|
||||
Assert.That(GetSidSubAuthority(pSid, 0), Is.EqualTo(0));
|
||||
var len = GetLengthSid(pSid);
|
||||
var p2 = new SafePSID(len);
|
||||
b = CopySid(len, (IntPtr)p2, pSid);
|
||||
Assert.That(EqualSid(p2, pSid));
|
||||
Assert.That(b);
|
||||
}
|
||||
|
||||
[Test()]
|
||||
public void CloneTest()
|
||||
{
|
||||
var sid = GetCurrentSid();
|
||||
var sid2 = sid.Clone();
|
||||
using (var sid = SafePSID.Current)
|
||||
using (var sid2 = sid.Clone())
|
||||
{
|
||||
Assert.That(sid2.IsValidSid);
|
||||
Assert.That(sid, Is.EqualTo(sid2));
|
||||
}
|
||||
}
|
||||
|
||||
[Test()]
|
||||
public void CopyTest()
|
||||
{
|
||||
var sid = GetCurrentSid();
|
||||
using (var sid = SafePSID.Current)
|
||||
{
|
||||
Assert.That(!sid.IsInvalid);
|
||||
Assert.That(sid.IsValidSid);
|
||||
Assert.That(sid.ToString(), Does.StartWith("S-1-5"));
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void EqualDomainSidTest()
|
||||
{
|
||||
Assert.That(EqualDomainSid(SafePSID.Current, SafePSID.Current, out var eq), ResultIs.Successful);
|
||||
Assert.That(eq, Is.True);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void EqualPrefixSidTest()
|
||||
{
|
||||
Assert.That(EqualPrefixSid(SafePSID.Current, SafePSID.Everyone), Is.False);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void EqualsTest()
|
||||
{
|
||||
var ssid = new SafePSID("S-1-1-0");
|
||||
var esid = SafePSID.Everyone;
|
||||
var mesid = SafePSID.Current;
|
||||
using (var ssid = new SafePSID("S-1-1-0"))
|
||||
using (var esid = SafePSID.Everyone)
|
||||
using (var mesid = SafePSID.Current)
|
||||
{
|
||||
Assert.That(ssid == esid, Is.True);
|
||||
Assert.That(ssid != mesid, Is.True);
|
||||
Assert.That(ssid.Equals(null), Is.False);
|
||||
|
@ -60,46 +101,99 @@ namespace Vanara.PInvoke.Tests
|
|||
Assert.That(ssid.Equals((object)(IntPtr)esid), Is.True);
|
||||
Assert.That(ssid.Equals((object)54), Is.False);
|
||||
}
|
||||
}
|
||||
|
||||
[Test()]
|
||||
public void GetBinaryForm()
|
||||
{
|
||||
var sid = new SafePSID("S-1-1-0");
|
||||
using (var sid = new SafePSID("S-1-1-0"))
|
||||
Assert.That(sid.GetBinaryForm(), Is.EquivalentTo(new SecurityIdentifier(WellKnownSidType.WorldSid, null).GetBytes()));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetSidIdentifierAuthorityTest()
|
||||
{
|
||||
Assert.That(GetSidIdentifierAuthority(SafePSID.Everyone), Is.EqualTo(KnownSIDAuthority.SECURITY_WORLD_SID_AUTHORITY));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetSidLengthRequiredTest()
|
||||
{
|
||||
Assert.That(GetSidLengthRequired(6), ResultIs.Not.Value(0));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetSidSubAuthorityTest()
|
||||
{
|
||||
Assert.That(GetSidSubAuthority(SafePSID.Everyone, 0), ResultIs.Value(0));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetSidSubAuthorityCountTest()
|
||||
{
|
||||
Assert.That(GetSidSubAuthorityCount(SafePSID.Everyone), ResultIs.Value(1));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetWindowsAccountDomainSidTest()
|
||||
{
|
||||
Assert.That(GetWindowsAccountDomainSid(SafePSID.Current, out var pDomSid), ResultIs.Successful);
|
||||
Assert.That(pDomSid.IsValidSid);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InitializeSidTest()
|
||||
{
|
||||
using (var pSid = new SafePSID(32))
|
||||
Assert.That(InitializeSid(pSid, KnownSIDAuthority.SECURITY_LOCAL_SID_AUTHORITY, 2), ResultIs.Successful);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void IsWellKnownSidTest()
|
||||
{
|
||||
Assert.That(IsWellKnownSid(SafePSID.Everyone, WELL_KNOWN_SID_TYPE.WinWorldSid), Is.True);
|
||||
}
|
||||
|
||||
[Test()]
|
||||
public void InitTest()
|
||||
{
|
||||
var sid = GetCurrentSid();
|
||||
using (var sid = SafePSID.Current)
|
||||
{
|
||||
var sidStr = sid.ToString();
|
||||
Assert.That(sidStr, Does.StartWith("S-1-5-"));
|
||||
var ssid = sid.ToString().Substring(6).Split('-').Select(int.Parse).ToArray();
|
||||
var i = ssid[0];
|
||||
var dest = new int[ssid.Length - 1];
|
||||
Array.Copy(ssid, 1, dest, 0, ssid.Length - 1);
|
||||
var sid2 = SafePSID.Init(KnownSIDAuthority.SECURITY_NT_AUTHORITY, i, dest);
|
||||
using (var sid2 = SafePSID.Init(KnownSIDAuthority.SECURITY_NT_AUTHORITY, i, dest))
|
||||
{
|
||||
Assert.That(sid2.IsValidSid);
|
||||
Assert.That(sid, Is.EqualTo(sid2));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Test()]
|
||||
public void PSIDTest()
|
||||
{
|
||||
var sid = GetCurrentSid();
|
||||
using (var sid = SafePSID.Current)
|
||||
{
|
||||
Assert.That(!sid.IsInvalid);
|
||||
Assert.That(sid.IsValidSid);
|
||||
Assert.That(sid.ToString(), Does.StartWith("S-1-5"));
|
||||
|
||||
var sid2 = new SafePSID(sid);
|
||||
using (var sid2 = new SafePSID(sid))
|
||||
{
|
||||
Assert.That(!sid2.IsInvalid);
|
||||
Assert.That(sid2.ToString(), Is.EqualTo(sid.ToString()));
|
||||
|
||||
var sid3 = new SafePSID("S-1-1-0");
|
||||
using (var sid3 = new SafePSID("S-1-1-0"))
|
||||
{
|
||||
var id2 = new SecurityIdentifier((IntPtr)sid3);
|
||||
Assert.That(id2.IsWellKnown(WellKnownSidType.WorldSid));
|
||||
|
||||
var sid4 = new SafePSID(100);
|
||||
using (var sid4 = new SafePSID(100))
|
||||
{
|
||||
Assert.That(!sid4.IsClosed);
|
||||
Assert.That(!sid4.IsValidSid);
|
||||
Assert.That((int)sid4.Size, Is.EqualTo(100));
|
||||
|
@ -110,11 +204,16 @@ namespace Vanara.PInvoke.Tests
|
|||
Assert.That(sid.Equals("X"), Is.False);
|
||||
Assert.That(sid.Equals(sid3), Is.False);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Test()]
|
||||
public void ToStringTest()
|
||||
{
|
||||
var sid = SafePSID.Everyone;
|
||||
using (var sid = SafePSID.Everyone)
|
||||
{
|
||||
const string sddl = "S-1-1-0";
|
||||
Assert.That(sid.ToString(), Is.EqualTo(sddl));
|
||||
Assert.That(sid.ToString(null), Is.EqualTo(sddl));
|
||||
|
@ -128,6 +227,7 @@ namespace Vanara.PInvoke.Tests
|
|||
|
||||
Assert.That(new SafePSID(new byte[] { 12, 255 }).ToString(), Is.EqualTo("Invalid"));
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SafePSIDArrayCtorTest()
|
||||
|
|
|
@ -46,7 +46,7 @@ namespace Vanara.PInvoke.Tests
|
|||
|
||||
public static SafeAUTHZ_CLIENT_CONTEXT_HANDLE GetCurrentUserAuthContext(SafeAUTHZ_RESOURCE_MANAGER_HANDLE hResMgr)
|
||||
{
|
||||
var b = AuthzInitializeContextFromSid(AuthzContextFlags.DEFAULT, PSIDTests.GetCurrentSid(), hResMgr, IntPtr.Zero, new LUID(), IntPtr.Zero, out var hCtx);
|
||||
var b = AuthzInitializeContextFromSid(AuthzContextFlags.DEFAULT, SafePSID.Current, hResMgr, IntPtr.Zero, new LUID(), IntPtr.Zero, out var hCtx);
|
||||
if (!b) TestContext.WriteLine($"AuthzInitializeContextFromSid:{Win32Error.GetLastError()}");
|
||||
Assert.That(b);
|
||||
Assert.That(!hCtx.IsInvalid);
|
||||
|
|
Loading…
Reference in New Issue