mirror of https://github.com/dahall/Vanara.git
Changed AdvApiTests.PrivBlock references to shared ElevPriv.
parent
2af1e65730
commit
b7cedf436f
|
@ -14,7 +14,7 @@ namespace Vanara.PInvoke.Tests
|
|||
|
||||
static AclApiTests()
|
||||
{
|
||||
using (new PrivBlock("SeSecurityPrivilege"))
|
||||
using (new ElevPriv("SeSecurityPrivilege"))
|
||||
pSd = AdvApi32Tests.GetSD(AdvApi32Tests.fn, SecInfoAll);
|
||||
}
|
||||
|
||||
|
@ -134,7 +134,7 @@ namespace Vanara.PInvoke.Tests
|
|||
public void TreeResetNamedSecurityInfoTest()
|
||||
{
|
||||
var counter = 0;
|
||||
using (new PrivBlock("SeSecurityPrivilege"))
|
||||
using (new ElevPriv("SeSecurityPrivilege"))
|
||||
{
|
||||
Assert.That(GetNamedSecurityInfo(AdvApi32Tests.fn, SE_OBJECT_TYPE.SE_FILE_OBJECT, SecInfoAll, out var pOwnSid, out var pGrpSid, out var dacl, out var sacl, out var plsd), ResultIs.Successful);
|
||||
Assert.That(TreeResetNamedSecurityInfo(@"C:\Temp\Temp\", SE_OBJECT_TYPE.SE_FILE_OBJECT, SecInfoAll, pOwnSid, pGrpSid, dacl, sacl, false, OnProgress, PROG_INVOKE_SETTING.ProgressInvokeEveryObject), ResultIs.Successful);
|
||||
|
@ -148,7 +148,7 @@ namespace Vanara.PInvoke.Tests
|
|||
public void TreeSetNamedSecurityInfoTest()
|
||||
{
|
||||
var counter = 0;
|
||||
using (new PrivBlock("SeSecurityPrivilege"))
|
||||
using (new ElevPriv("SeSecurityPrivilege"))
|
||||
{
|
||||
Assert.That(GetNamedSecurityInfo(AdvApi32Tests.fn, SE_OBJECT_TYPE.SE_FILE_OBJECT, SecInfoAll, out var pOwnSid, out var pGrpSid, out var dacl, out var sacl, out var plsd), ResultIs.Successful);
|
||||
Assert.That(TreeSetNamedSecurityInfo(@"C:\Temp\Temp\", SE_OBJECT_TYPE.SE_FILE_OBJECT, SecInfoAll, pOwnSid, pGrpSid, dacl, sacl, TREE_SEC_INFO.TREE_SEC_INFO_SET, OnProgress, PROG_INVOKE_SETTING.ProgressInvokeEveryObject), ResultIs.Successful);
|
||||
|
|
|
@ -13,7 +13,7 @@ namespace Vanara.PInvoke.Tests
|
|||
private static readonly Guid objAccAudit = new Guid("6997984a-797a-11d9-bed3-505054503030");
|
||||
private static readonly Guid regAudit = new Guid("0cce921e-69ae-11d9-bed3-505054503030");
|
||||
private static SafePSID pCurSid;
|
||||
private PrivBlock secPriv;
|
||||
private ElevPriv secPriv;
|
||||
|
||||
public static IEnumerable<Guid> Categories => AuditEnumerateCategories();
|
||||
|
||||
|
@ -28,7 +28,7 @@ namespace Vanara.PInvoke.Tests
|
|||
[OneTimeSetUp]
|
||||
public void _SetupTests()
|
||||
{
|
||||
secPriv = new PrivBlock("SeSecurityPrivilege");
|
||||
secPriv = new ElevPriv("SeSecurityPrivilege");
|
||||
}
|
||||
|
||||
[OneTimeTearDown]
|
||||
|
|
|
@ -9,7 +9,7 @@ namespace Vanara.PInvoke.Tests
|
|||
[Test]
|
||||
public void ConvertSecurityDescriptorToStringSecurityDescriptorTest()
|
||||
{
|
||||
using (new PrivBlock("SeSecurityPrivilege"))
|
||||
using (new ElevPriv("SeSecurityPrivilege"))
|
||||
{
|
||||
var si = SECURITY_INFORMATION.OWNER_SECURITY_INFORMATION | SECURITY_INFORMATION.GROUP_SECURITY_INFORMATION | SECURITY_INFORMATION.DACL_SECURITY_INFORMATION | SECURITY_INFORMATION.SACL_SECURITY_INFORMATION;
|
||||
Assert.That(GetNamedSecurityInfo(@"C:\Temp\help.ico", SE_OBJECT_TYPE.SE_FILE_OBJECT, si, out _, out _, out _, out _, out var sd), ResultIs.Successful);
|
||||
|
|
|
@ -282,7 +282,7 @@ namespace Vanara.PInvoke.Tests
|
|||
public void ConvertToAutoInheritPrivateObjectSecurityTest()
|
||||
{
|
||||
const SECURITY_INFORMATION si = SECURITY_INFORMATION.DACL_SECURITY_INFORMATION | SECURITY_INFORMATION.OWNER_SECURITY_INFORMATION | SECURITY_INFORMATION.GROUP_SECURITY_INFORMATION;
|
||||
using (new PrivBlock("SeSecurityPrivilege"))
|
||||
using (new ElevPriv("SeSecurityPrivilege"))
|
||||
using (var pParentSD = AdvApi32Tests.GetSD(System.IO.Path.GetDirectoryName(AdvApi32Tests.fn), si))
|
||||
using (var pSD = AdvApi32Tests.GetSD(AdvApi32Tests.fn, si))
|
||||
{
|
||||
|
@ -295,7 +295,7 @@ namespace Vanara.PInvoke.Tests
|
|||
[Test]
|
||||
public void CreateGetSetPrivateObjectSecurityExTest()
|
||||
{
|
||||
using (new PrivBlock("SeSecurityPrivilege"))
|
||||
using (new ElevPriv("SeSecurityPrivilege"))
|
||||
using (var pParentSD = AdvApi32Tests.GetSD(System.IO.Path.GetDirectoryName(AdvApi32Tests.fn)))
|
||||
using (var hTok = SafeHTOKEN.FromProcess(GetCurrentProcess(), TokenAccess.TOKEN_IMPERSONATE | TokenAccess.TOKEN_DUPLICATE | TokenAccess.TOKEN_QUERY).Duplicate(SECURITY_IMPERSONATION_LEVEL.SecurityImpersonation))
|
||||
{
|
||||
|
@ -318,7 +318,7 @@ namespace Vanara.PInvoke.Tests
|
|||
[Test]
|
||||
public void CreatePrivateObjectSecurityTest()
|
||||
{
|
||||
using (new PrivBlock("SeSecurityPrivilege"))
|
||||
using (new ElevPriv("SeSecurityPrivilege"))
|
||||
using (var pParentSD = AdvApi32Tests.GetSD(System.IO.Path.GetDirectoryName(AdvApi32Tests.fn)))
|
||||
using (var hTok = SafeHTOKEN.FromProcess(GetCurrentProcess(), TokenAccess.TOKEN_IMPERSONATE | TokenAccess.TOKEN_DUPLICATE | TokenAccess.TOKEN_QUERY).Duplicate(SECURITY_IMPERSONATION_LEVEL.SecurityImpersonation))
|
||||
{
|
||||
|
@ -330,7 +330,7 @@ namespace Vanara.PInvoke.Tests
|
|||
[Test]
|
||||
public void CreatePrivateObjectSecurityWithMultipleInheritanceTest()
|
||||
{
|
||||
using (new PrivBlock("SeSecurityPrivilege"))
|
||||
using (new ElevPriv("SeSecurityPrivilege"))
|
||||
using (var pParentSD = AdvApi32Tests.GetSD(System.IO.Path.GetDirectoryName(AdvApi32Tests.fn)))
|
||||
using (var hTok = SafeHTOKEN.FromProcess(GetCurrentProcess(), TokenAccess.TOKEN_IMPERSONATE | TokenAccess.TOKEN_DUPLICATE | TokenAccess.TOKEN_QUERY).Duplicate(SECURITY_IMPERSONATION_LEVEL.SecurityImpersonation))
|
||||
{
|
||||
|
@ -342,7 +342,7 @@ namespace Vanara.PInvoke.Tests
|
|||
[Test]
|
||||
public void CreateRestrictedTokenTest()
|
||||
{
|
||||
using (new PrivBlock("SeSecurityPrivilege"))
|
||||
using (new ElevPriv("SeSecurityPrivilege"))
|
||||
using (var hTok = SafeHTOKEN.FromProcess(GetCurrentProcess(), TokenAccess.TOKEN_ALL_ACCESS))
|
||||
{
|
||||
Assert.That(IsTokenRestricted(hTok), Is.False);
|
||||
|
@ -453,7 +453,7 @@ namespace Vanara.PInvoke.Tests
|
|||
public void GetSetKernelObjectSecurityTest()
|
||||
{
|
||||
HANDLE hProc = (IntPtr)GetCurrentProcess();
|
||||
using (new PrivBlock("SeSecurityPrivilege"))
|
||||
using (new ElevPriv("SeSecurityPrivilege"))
|
||||
using (var pSD = new SafePSECURITY_DESCRIPTOR(2048))
|
||||
{
|
||||
// Get self-relative SD with DACL
|
||||
|
@ -526,7 +526,7 @@ namespace Vanara.PInvoke.Tests
|
|||
[Test]
|
||||
public void GetSecurityDescriptorSaclTest()
|
||||
{
|
||||
using (new PrivBlock("SeSecurityPrivilege"))
|
||||
using (new ElevPriv("SeSecurityPrivilege"))
|
||||
using (var pSD = AdvApi32Tests.GetSD(AdvApi32Tests.fn, SECURITY_INFORMATION.SACL_SECURITY_INFORMATION))
|
||||
{
|
||||
Assert.That(GetSecurityDescriptorSacl(pSD, out var present, out var pSacl, out var def), ResultIs.Successful);
|
||||
|
@ -557,7 +557,7 @@ namespace Vanara.PInvoke.Tests
|
|||
}
|
||||
}
|
||||
|
||||
using (new PrivBlock("SeSecurityPrivilege"))
|
||||
using (new ElevPriv("SeSecurityPrivilege"))
|
||||
using (var t = SafeHTOKEN.FromThread(GetCurrentThread(), TokenAccess.TOKEN_ALL_ACCESS))
|
||||
using (var mem = new SafeHGlobalHandle(8192))
|
||||
{
|
||||
|
|
|
@ -12,7 +12,7 @@ namespace Vanara.PInvoke.Tests
|
|||
public void ThreadWaitChainSessionTest()
|
||||
{
|
||||
RegisterWaitChainCOMCallback();
|
||||
using (new PrivBlock("SeDebugPrivilege"))
|
||||
using (new ElevPriv("SeDebugPrivilege"))
|
||||
using (var hWct = OpenThreadWaitChainSession(WaitChainSessionType.WCT_SYNC_OPEN_FLAG))
|
||||
{
|
||||
Assert.That(hWct, ResultIs.ValidHandle);
|
||||
|
|
|
@ -311,7 +311,7 @@ namespace Vanara.PInvoke.Tests
|
|||
{
|
||||
const string eventSource = "TestEventSource";
|
||||
var guid = Guid.NewGuid();
|
||||
using (var srcReg = new SafeAUTHZ_SOURCE_SCHEMA_REGISTRATION { szEventSourceName = eventSource, szEventAccessStringsFile = @"%SystemRoot%\System32\MsObjs.dll", szObjectTypeName = "Obj1" , pProviderGuid = guid })
|
||||
using (var srcReg = new SafeAUTHZ_SOURCE_SCHEMA_REGISTRATION { szEventSourceName = eventSource, szEventAccessStringsFile = @"%SystemRoot%\System32\MsObjs.dll", szObjectTypeName = "Obj1", pProviderGuid = guid })
|
||||
{
|
||||
var nSrc = (AUTHZ_SOURCE_SCHEMA_REGISTRATION)srcReg;
|
||||
Assert.That(nSrc.szEventSourceName.ToString(), Is.EqualTo(eventSource));
|
||||
|
@ -335,7 +335,7 @@ namespace Vanara.PInvoke.Tests
|
|||
{
|
||||
const string eventSource = "TestEventSource";
|
||||
|
||||
using (new PrivBlock("SeAuditPrivilege"))
|
||||
using (new ElevPriv("SeAuditPrivilege"))
|
||||
{
|
||||
var srcReg = new SafeAUTHZ_SOURCE_SCHEMA_REGISTRATION { szEventSourceName = eventSource, szEventAccessStringsFile = @"%SystemRoot%\System32\MsObjs.dll", szObjectTypeName = "Obj1", pProviderGuid = Guid.NewGuid() };
|
||||
Assert.That(AuthzInstallSecurityEventSource(0, srcReg), Is.True);
|
||||
|
@ -383,26 +383,4 @@ namespace Vanara.PInvoke.Tests
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal class PrivBlock : IDisposable
|
||||
{
|
||||
SafeCoTaskMemHandle prevState;
|
||||
SafeHTOKEN tok;
|
||||
|
||||
public PrivBlock(string priv)
|
||||
{
|
||||
tok = SafeHTOKEN.FromProcess(GetCurrentProcess(), TokenAccess.TOKEN_ADJUST_PRIVILEGES | TokenAccess.TOKEN_QUERY);
|
||||
var newPriv = new PTOKEN_PRIVILEGES(LUID.FromName(priv), PrivilegeAttributes.SE_PRIVILEGE_ENABLED);
|
||||
prevState = PTOKEN_PRIVILEGES.GetAllocatedAndEmptyInstance();
|
||||
if (!AdjustTokenPrivileges(tok, false, newPriv, (uint)prevState.Size, prevState, out var retLen))
|
||||
Win32Error.ThrowLastError();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
AdjustTokenPrivileges(tok, false, prevState);
|
||||
prevState.Dispose();
|
||||
tok.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue