Skip to content

Commit

Permalink
Improve/fix unittest for Psapi#QueryWorkingSetEx
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiasblaesing committed Aug 11, 2022
1 parent 04ae48f commit 45647d5
Showing 1 changed file with 55 additions and 20 deletions.
75 changes: 55 additions & 20 deletions contrib/platform/test/com/sun/jna/platform/win32/PsapiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertEquals;

import java.util.LinkedList;
import java.util.List;
Expand Down Expand Up @@ -281,37 +282,71 @@ public void testEnumProcesses() {

@Test
public void testQueryWorkingSetEx() {
Win32Exception we = null;
HANDLE selfHandle = Kernel32.INSTANCE.GetCurrentProcess();
MEMORY_BASIC_INFORMATION mbi = new MEMORY_BASIC_INFORMATION();
try {
SIZE_T bytesRead = Kernel32.INSTANCE.VirtualQueryEx(selfHandle, Pointer.NULL, mbi, new SIZE_T(mbi.size()));
assertNotEquals("Kernel should be able to read this Process' Bytes", bytesRead.intValue(), 0);
Psapi.PSAPI_WORKING_SET_EX_INFORMATION pswsi = new Psapi.PSAPI_WORKING_SET_EX_INFORMATION();
pswsi.VirtualAddress = mbi.baseAddress;
if (!Psapi.INSTANCE.QueryWorkingSetEx(selfHandle, pswsi.VirtualAddress, pswsi.size())) {
throw new Win32Exception(Native.getLastError());
}
assertTrue("Virual Attributes should not be null", pswsi.VirtualAttributes != null);
if (Psapi.INSTANCE.QueryWorkingSetEx(new HANDLE(), pswsi.VirtualAddress, pswsi.size())) {
throw new Win32Exception(Native.getLastError());
}
assertFalse("This line should never be called", true);
} catch (Win32Exception e) {
we = e;
throw we; // re-throw to invoke finally block
pswsi.VirtualAddress = mbi.getPointer();

// QueryWorkingSetEx expects an array of PSAPI_WORKING_SET_EX_INFORMATION
// structures as second parameter, for the single element case we can
// pass in just the pointer to the structure. As structure is passed
// as a raw pointer it needs to be read and writte explicitly
pswsi.write();
assertTrue("Failed to invoke QueryWorkingSetEx (1)", Psapi.INSTANCE.QueryWorkingSetEx(selfHandle, pswsi.getPointer(), pswsi.size()));
pswsi.read();

assertTrue("Virtual Attributes should not be null (1)", pswsi.VirtualAttributes != null);
assertEquals("Virtual Address should not change before and after call (1)", pswsi.VirtualAddress, mbi.getPointer());
assertTrue("Data was invalid (1)", pswsi.isValid());
assertFalse("Data was reported as bad (1)", pswsi.isBad());
assertEquals("Data indicates sharing (1)", pswsi.getShareCount(), 0);
assertEquals("Data indicated that protection does not match PAGE_READWRITE (1)",
pswsi.getWin32Protection(), WinNT.PAGE_READWRITE);
assertFalse("Data was reported as shared (1)", pswsi.isShared());
assertFalse("Data was reported as locked (1)", pswsi.isLocked());
assertFalse("Data was reported as large pages (1)", pswsi.isLargePage());

// Lock the page we used into memory - this should be reflected in the reported flags now
assertTrue(Kernel32.INSTANCE.VirtualLock(mbi.getPointer(), new SIZE_T(4096)));

pswsi.write();
assertTrue("Failed to invoke QueryWorkingSetEx (2)", Psapi.INSTANCE.QueryWorkingSetEx(selfHandle, pswsi.getPointer(), pswsi.size()));
pswsi.read();

assertTrue("Virtual Attributes should not be null (2)", pswsi.VirtualAttributes != null);
assertEquals("Virtual Address should not change before and after call (2)", pswsi.VirtualAddress, mbi.getPointer());
assertTrue("Data was invalid (2)", pswsi.isValid());
assertFalse("Data was reported as bad (2)", pswsi.isBad());
assertEquals("Data indicates sharing (2)", pswsi.getShareCount(), 0);
assertEquals("Data indicated that protection does not match PAGE_READWRITE (2)",
pswsi.getWin32Protection(), WinNT.PAGE_READWRITE);
assertFalse("Data was reported as shared (2)", pswsi.isShared());
assertTrue("Data was reported as locked (2)", pswsi.isLocked());
assertFalse("Data was reported as large pages (2)", pswsi.isLargePage());

// Check that a query against an invalid target succeeds, but report
// invalid data
pswsi.VirtualAddress = null;
pswsi.write();
assertTrue("Failed to invoke QueryWorkingSetEx (3)", Psapi.INSTANCE.QueryWorkingSetEx(WinBase.INVALID_HANDLE_VALUE, pswsi.getPointer(), pswsi.size()));
pswsi.read();

assertTrue("Virtual Attributes should not be null (3)", pswsi.VirtualAttributes != null);
assertTrue("Virtual Address should not change before and after call (3)", pswsi.VirtualAddress == null);
assertFalse("Data was reported valid, but expected to be invalid (3)", pswsi.isValid());
} finally {
try {
Kernel32Util.closeHandle(selfHandle);
} catch (Win32Exception e) {
if (we == null) {
we = e;
} else {
we.addSuppressedReflected(e);
}
// Ignore
}
if (we != null) {
throw we;
try {
Kernel32.INSTANCE.VirtualUnlock(mbi.getPointer(), new SIZE_T(4096));
} catch (Win32Exception e) {
// Ignore
}
}
}
Expand Down

0 comments on commit 45647d5

Please sign in to comment.