Breaking fixes on heap functions. Added helper methods and prevented use of raw pointer for heap allocation to prevent memory leaks. Fixed bugs on function definitions.

pull/60/head
David Hall 2019-06-17 13:47:28 -06:00
parent 8250d509ee
commit ceff427cb9
4 changed files with 591 additions and 226 deletions

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,91 @@
using NUnit.Framework;
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using Vanara.Extensions;
using Vanara.InteropServices;
using static Vanara.PInvoke.AdvApi32;
using static Vanara.PInvoke.Kernel32;
namespace Vanara.PInvoke.Tests
{
[TestFixture]
public class HeapApiTests
{
[Test]
public void GetProcessHeapTest()
{
Assert.That(GetProcessHeap().DangerousGetHandle(), Is.Not.EqualTo(IntPtr.Zero));
}
[Test]
public void GetProcessHeapsTest()
{
Assert.That(GetProcessHeaps(), Is.Not.Empty);
}
[Test]
public void HeapCreateAllocReallocFreeDestroyValidateTest()
{
using (var h = HeapCreate(HeapFlags.HEAP_REALLOC_IN_PLACE_ONLY, 512, 2048))
{
Assert.That(h.IsInvalid, Is.False);
using (var hb = HeapAlloc(h, 0, 256))
{
Assert.That(hb.IsInvalid, Is.False);
Assert.That((uint)HeapSize(h, HeapFlags.HEAP_REALLOC_IN_PLACE_ONLY, hb), Is.EqualTo(256));
using (var hrb = HeapReAlloc(h, HeapFlags.HEAP_REALLOC_IN_PLACE_ONLY, hb, 1024))
{
Assert.That(hrb.IsInvalid, Is.False);
Assert.That(hb.IsClosed, Is.True);
Assert.That(HeapValidate(h, HeapFlags.HEAP_REALLOC_IN_PLACE_ONLY, hrb), Is.True);
}
}
}
}
[Test]
public void HeapLockUnlockTest()
{
using (var h = HeapCreate(HeapFlags.HEAP_REALLOC_IN_PLACE_ONLY, 512, 2048))
{
Assert.That(h.IsInvalid, Is.False);
Assert.That(HeapLock(h), Is.True);
Assert.That(HeapUnlock(h), Is.True);
}
}
[Test]
public void HeapQuerySetInformationTest()
{
using (var h = HeapCreate(HeapFlags.HEAP_REALLOC_IN_PLACE_ONLY, 512, 2048))
{
Assert.That(h.IsInvalid, Is.False);
Assert.That(() => HeapSetInformation(h, HEAP_INFORMATION_CLASS.HeapOptimizeResources, new HEAP_OPTIMIZE_RESOURCES_INFORMATION(0)), Throws.Nothing);
// Not possible under debugger
// Assert.That(() => HeapSetInformation(h, HEAP_INFORMATION_CLASS.HeapCompatibilityInformation, HeapCompatibility.HEAP_STANDARD), Throws.Nothing);
Assert.That(HeapSetInformation(h, HEAP_INFORMATION_CLASS.HeapEnableTerminationOnCorruption, default, 0), Is.True);
Assert.That(() =>
{
var t = HeapQueryInformation<HeapCompatibility>(h, HEAP_INFORMATION_CLASS.HeapCompatibilityInformation);
Assert.That((uint)t, Is.LessThanOrEqualTo(2));
}, Throws.Nothing);
}
}
[Test]
public void HeapSummaryTest()
{
var summary = HEAP_SUMMARY.Default;
Assert.That(HeapSummary(HHEAP.FromProcess(), 0, ref summary), Is.True);
TestContext.WriteLine($"{summary.cbAllocated} : {summary.cbCommitted} : {summary.cbReserved} : {summary.cbMaxReserve}");
}
[Test]
public void HeapWalkTest()
{
Assert.That(HeapWalk(HHEAP.FromProcess()), Is.Not.Empty);
}
}
}

View File

@ -37,6 +37,7 @@
<Reference Include="System" />
</ItemGroup>
<ItemGroup>
<Compile Include="HeapApiTests.cs" />
<Compile Include="FileApiTests.cs" />
<Compile Include="FibersApiTests.cs" />
<Compile Include="ErrHandlingApiTests.cs" />

View File

@ -143,6 +143,13 @@ namespace Vanara.PInvoke.Tests
Assert.That(i, Is.Not.EqualTo(0));
}
[Test]
public void GetGamingDeviceModelInformationTest()
{
Assert.That(GetGamingDeviceModelInformation(out var i), Is.EqualTo((HRESULT)0));
Assert.That(i.deviceId == GAMING_DEVICE_DEVICE_ID.GAMING_DEVICE_DEVICE_ID_NONE);
}
[Test]
public void GetModuleFileNameTest()
{
@ -239,24 +246,6 @@ namespace Vanara.PInvoke.Tests
TestContext.WriteLine(sb);
}
[Test]
public void HeapTest()
{
var ph = new SafeHeapBlock(512);
var fw = new WIN32_FIND_DATA { ftCreationTime = DateTime.Today.ToFileTimeStruct(), cFileName = "test.txt", dwFileAttributes = FileAttributes.Normal };
Marshal.StructureToPtr(fw, ph.DangerousGetHandle(), false);
Assert.That(Marshal.ReadInt32(ph.DangerousGetHandle()), Is.EqualTo((int)FileAttributes.Normal));
Assert.That(ph.Size, Is.EqualTo(512));
using (var hh = HeapCreate(0, 0, 0))
{
var hb = hh.GetBlock(512);
Marshal.StructureToPtr(fw, hb.DangerousGetHandle(), false);
Assert.That(Marshal.ReadInt32(hb.DangerousGetHandle()), Is.EqualTo((int)FileAttributes.Normal));
Assert.That(hb.Size, Is.EqualTo(512));
}
}
[Test]
public void PowerRequestTest()
{