Changed name of PrivBlock to ElevPriv to avoid conflict with main lib.

pull/83/head
David Hall 2019-07-30 11:31:21 -06:00
parent 4120d49d5c
commit e2cbc0a775
7 changed files with 32 additions and 9 deletions

View File

@ -5,12 +5,12 @@ using static Vanara.PInvoke.Kernel32;
namespace Vanara.PInvoke.Tests
{
public class PrivBlock : IDisposable
public class ElevPriv : IDisposable
{
SafeCoTaskMemHandle prevState;
SafeHTOKEN tok;
public PrivBlock(string priv, HPROCESS hProc = default, TokenAccess access = TokenAccess.TOKEN_ADJUST_PRIVILEGES | TokenAccess.TOKEN_QUERY)
public ElevPriv(string priv, HPROCESS hProc = default, TokenAccess access = TokenAccess.TOKEN_ADJUST_PRIVILEGES | TokenAccess.TOKEN_QUERY)
{
if (hProc.IsNull) hProc = GetCurrentProcess();
tok = SafeHTOKEN.FromProcess(hProc, access);

View File

@ -26,7 +26,7 @@ namespace Vanara.PInvoke.Tests
// Enable the privilege.
using (var hProc = SafeHPROCESS.Current)
using (new PrivBlock("SeLockMemoryPrivilege", hProc))
using (new ElevPriv("SeLockMemoryPrivilege", hProc))
{
// Allocate the physical memory.
var aPFNs = new IntPtr[NumberOfPages];

View File

@ -40,7 +40,7 @@ namespace Vanara.PInvoke.Tests
Assert.That(GetDynamicTimeZoneInformation(out var tz), Is.Not.EqualTo(TZID.TIME_ZONE_ID_INVALID));
Assert.That(tz.StandardName, Is.Not.Null.Or.Empty);
using (new PrivBlock("SeTimeZonePrivilege"))
using (new ElevPriv("SeTimeZonePrivilege"))
{
// Set to random
Assert.That(SetDynamicTimeZoneInformation(dtz), ResultIs.Successful);
@ -56,7 +56,7 @@ namespace Vanara.PInvoke.Tests
Assert.That(GetTimeZoneInformation(out var tziOld), Is.Not.EqualTo(TZID.TIME_ZONE_ID_INVALID));
Assert.That(tziOld.StandardName, Is.Not.Null.Or.Empty);
using (new PrivBlock("SeTimeZonePrivilege"))
using (new ElevPriv("SeTimeZonePrivilege"))
{
// Build new test tz
var tziNew = new TIME_ZONE_INFORMATION

View File

@ -290,7 +290,7 @@ namespace Vanara.PInvoke.Tests
[Test]
public void SetFileIoOverlappedRangeTest()
{
using (var priv = new PrivBlock("SeLockMemoryPrivilege"))
using (var priv = new ElevPriv("SeLockMemoryPrivilege"))
using (var tmp = new TempFile(FileAccess.FILE_READ_ATTRIBUTES | FileAccess.GENERIC_READ, FileShare.Read, FileMode.Create, FileFlagsAndAttributes.FILE_FLAG_OVERLAPPED | FileFlagsAndAttributes.FILE_FLAG_NO_BUFFERING))
using (var mem = new AlignedMemory<HGlobalMemoryMethods>(1024, 1024))
Assert.That(SetFileIoOverlappedRange(tmp.hFile, mem, (uint)mem.Size), ResultIs.Failure); // Not sure why I'm having permissions problems.
@ -299,7 +299,7 @@ namespace Vanara.PInvoke.Tests
[Test]
public void SetFileShortNameTest()
{
using (new PrivBlock("SeRestorePrivilege"))
using (new ElevPriv("SeRestorePrivilege"))
using (var tmp = new TempFile(FileAccess.GENERIC_ALL, FileShare.ReadWrite, dwFlagsAndAttributes: FileFlagsAndAttributes.FILE_FLAG_BACKUP_SEMANTICS))
Assert.That(SetFileShortName(tmp.hFile, "SN.TXT"), ResultIs.Successful);
}

View File

@ -38,7 +38,7 @@ namespace Vanara.PInvoke.Tests
// [Test] This works but suspends the PC
public void SetSystemPowerStateTest()
{
using (new PrivBlock("SeShutdownPrivilege"))
using (new ElevPriv("SeShutdownPrivilege"))
Assert.That(SetSystemPowerState(true), ResultIs.Successful);
}
}

View File

@ -104,7 +104,7 @@ namespace Vanara.PInvoke.Tests
var pval = new PinnedObject(val);
VARIABLE_ATTRIBUTE attr = 0U;
var guid = Guid.Empty;
using (new PrivBlock("SeSystemEnvironmentPrivilege"))
using (new ElevPriv("SeSystemEnvironmentPrivilege"))
{
Assert.That(GetFirmwareEnvironmentVariableEx("Test", guid.ToString("B"), pval, 4, out attr), ResultIs.Successful);
Assert.That(SetFirmwareEnvironmentVariableEx("Test", guid.ToString("B"), pval, 4, attr), ResultIs.Failure);

View File

@ -0,0 +1,23 @@
using NUnit.Framework;
using System;
using System.Runtime.InteropServices;
using System.Text;
using Vanara.InteropServices;
using static Vanara.PInvoke.AdvApi32;
using static Vanara.PInvoke.Kernel32;
namespace Vanara.PInvoke.Tests
{
[TestFixture]
public class WinNlsTests
{
[Test]
public void EnumCalendarInfoTest()
{
if (!EnumCalendarInfo(Proc, LOCALE_USER_DEFAULT, ENUM_ALL_CALENDARS, CALTYPE.CAL_RETURN_NUMBER | CALTYPE.CAL_ITWODIGITYEARMAX))
Win32Error.ThrowLastError();
bool Proc(string lpCalendarInfoString) => true;
}
}
}