mirror of https://github.com/dahall/Vanara.git
Changed name of PrivBlock to ElevPriv to avoid conflict with main lib.
parent
4120d49d5c
commit
e2cbc0a775
|
@ -5,12 +5,12 @@ using static Vanara.PInvoke.Kernel32;
|
||||||
|
|
||||||
namespace Vanara.PInvoke.Tests
|
namespace Vanara.PInvoke.Tests
|
||||||
{
|
{
|
||||||
public class PrivBlock : IDisposable
|
public class ElevPriv : IDisposable
|
||||||
{
|
{
|
||||||
SafeCoTaskMemHandle prevState;
|
SafeCoTaskMemHandle prevState;
|
||||||
SafeHTOKEN tok;
|
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();
|
if (hProc.IsNull) hProc = GetCurrentProcess();
|
||||||
tok = SafeHTOKEN.FromProcess(hProc, access);
|
tok = SafeHTOKEN.FromProcess(hProc, access);
|
||||||
|
|
|
@ -26,7 +26,7 @@ namespace Vanara.PInvoke.Tests
|
||||||
|
|
||||||
// Enable the privilege.
|
// Enable the privilege.
|
||||||
using (var hProc = SafeHPROCESS.Current)
|
using (var hProc = SafeHPROCESS.Current)
|
||||||
using (new PrivBlock("SeLockMemoryPrivilege", hProc))
|
using (new ElevPriv("SeLockMemoryPrivilege", hProc))
|
||||||
{
|
{
|
||||||
// Allocate the physical memory.
|
// Allocate the physical memory.
|
||||||
var aPFNs = new IntPtr[NumberOfPages];
|
var aPFNs = new IntPtr[NumberOfPages];
|
||||||
|
|
|
@ -40,7 +40,7 @@ namespace Vanara.PInvoke.Tests
|
||||||
Assert.That(GetDynamicTimeZoneInformation(out var tz), Is.Not.EqualTo(TZID.TIME_ZONE_ID_INVALID));
|
Assert.That(GetDynamicTimeZoneInformation(out var tz), Is.Not.EqualTo(TZID.TIME_ZONE_ID_INVALID));
|
||||||
Assert.That(tz.StandardName, Is.Not.Null.Or.Empty);
|
Assert.That(tz.StandardName, Is.Not.Null.Or.Empty);
|
||||||
|
|
||||||
using (new PrivBlock("SeTimeZonePrivilege"))
|
using (new ElevPriv("SeTimeZonePrivilege"))
|
||||||
{
|
{
|
||||||
// Set to random
|
// Set to random
|
||||||
Assert.That(SetDynamicTimeZoneInformation(dtz), ResultIs.Successful);
|
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(GetTimeZoneInformation(out var tziOld), Is.Not.EqualTo(TZID.TIME_ZONE_ID_INVALID));
|
||||||
Assert.That(tziOld.StandardName, Is.Not.Null.Or.Empty);
|
Assert.That(tziOld.StandardName, Is.Not.Null.Or.Empty);
|
||||||
|
|
||||||
using (new PrivBlock("SeTimeZonePrivilege"))
|
using (new ElevPriv("SeTimeZonePrivilege"))
|
||||||
{
|
{
|
||||||
// Build new test tz
|
// Build new test tz
|
||||||
var tziNew = new TIME_ZONE_INFORMATION
|
var tziNew = new TIME_ZONE_INFORMATION
|
||||||
|
|
|
@ -290,7 +290,7 @@ namespace Vanara.PInvoke.Tests
|
||||||
[Test]
|
[Test]
|
||||||
public void SetFileIoOverlappedRangeTest()
|
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 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))
|
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.
|
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]
|
[Test]
|
||||||
public void SetFileShortNameTest()
|
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))
|
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);
|
Assert.That(SetFileShortName(tmp.hFile, "SN.TXT"), ResultIs.Successful);
|
||||||
}
|
}
|
||||||
|
|
|
@ -38,7 +38,7 @@ namespace Vanara.PInvoke.Tests
|
||||||
// [Test] This works but suspends the PC
|
// [Test] This works but suspends the PC
|
||||||
public void SetSystemPowerStateTest()
|
public void SetSystemPowerStateTest()
|
||||||
{
|
{
|
||||||
using (new PrivBlock("SeShutdownPrivilege"))
|
using (new ElevPriv("SeShutdownPrivilege"))
|
||||||
Assert.That(SetSystemPowerState(true), ResultIs.Successful);
|
Assert.That(SetSystemPowerState(true), ResultIs.Successful);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -104,7 +104,7 @@ namespace Vanara.PInvoke.Tests
|
||||||
var pval = new PinnedObject(val);
|
var pval = new PinnedObject(val);
|
||||||
VARIABLE_ATTRIBUTE attr = 0U;
|
VARIABLE_ATTRIBUTE attr = 0U;
|
||||||
var guid = Guid.Empty;
|
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(GetFirmwareEnvironmentVariableEx("Test", guid.ToString("B"), pval, 4, out attr), ResultIs.Successful);
|
||||||
Assert.That(SetFirmwareEnvironmentVariableEx("Test", guid.ToString("B"), pval, 4, attr), ResultIs.Failure);
|
Assert.That(SetFirmwareEnvironmentVariableEx("Test", guid.ToString("B"), pval, 4, attr), ResultIs.Failure);
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue