|
| 1 | +package bwapi; |
| 2 | + |
| 3 | +import com.sun.jna.Pointer; |
| 4 | +import com.sun.jna.platform.win32.Kernel32; |
| 5 | +import com.sun.jna.platform.win32.WinBase; |
| 6 | +import com.sun.jna.platform.win32.WinNT; |
| 7 | +import org.junit.Before; |
| 8 | +import org.junit.Test; |
| 9 | + |
| 10 | +import static org.junit.Assert.assertEquals; |
| 11 | +import static org.junit.Assert.assertNotNull; |
| 12 | +import static org.junit.Assert.assertNull; |
| 13 | +import static org.junit.Assume.assumeTrue; |
| 14 | + |
| 15 | +/** |
| 16 | + * Verifies the Windows mapping binding's JNA signatures against a real shared-memory object. |
| 17 | + * The existing JNA Kernel32 binding creates the mapping and its first view; |
| 18 | + * MappingKernel opens a second view. Reads and writes must agree in both directions. |
| 19 | + * A Unicode name checks Windows string marshalling, and a missing mapping must |
| 20 | + * return null. These tests require Windows but do not require a BWAPI server. |
| 21 | + */ |
| 22 | +public class MappingKernelTest { |
| 23 | + @Before |
| 24 | + public void requireWindows() { |
| 25 | + assumeTrue(System.getProperty("os.name").startsWith("Windows")); |
| 26 | + } |
| 27 | + |
| 28 | + @Test |
| 29 | + public void opensUnicodeNamedMappingAndSharesWritesBetweenViews() { |
| 30 | + // Include Unicode to exercise the same W32API string mapping used by the client. |
| 31 | + String name = "Local\\jbwapi-mapping-\u03bb-" + System.nanoTime(); |
| 32 | + int access = WinNT.SECTION_MAP_READ | WinNT.SECTION_MAP_WRITE; |
| 33 | + WinNT.HANDLE created = Kernel32.INSTANCE.CreateFileMapping( |
| 34 | + WinBase.INVALID_HANDLE_VALUE, null, WinNT.PAGE_READWRITE, 0, 4, name); |
| 35 | + assertNotNull(created); |
| 36 | + WinNT.HANDLE opened = null; |
| 37 | + Pointer originalView = null; |
| 38 | + Pointer clientView = null; |
| 39 | + try { |
| 40 | + originalView = Kernel32.INSTANCE.MapViewOfFile(created, access, 0, 0, 4); |
| 41 | + assertNotNull(originalView); |
| 42 | + originalView.setInt(0, 10003); |
| 43 | + |
| 44 | + opened = ClientConnectionW32.MappingKernel.INSTANCE.OpenFileMapping(access, false, name); |
| 45 | + assertNotNull(opened); |
| 46 | + clientView = ClientConnectionW32.MappingKernel.INSTANCE.MapViewOfFile(opened, access, 0, 0, 4); |
| 47 | + assertNotNull(clientView); |
| 48 | + assertEquals(10003, clientView.getInt(0)); |
| 49 | + |
| 50 | + clientView.setInt(0, 42); |
| 51 | + assertEquals(42, originalView.getInt(0)); |
| 52 | + } finally { |
| 53 | + if (clientView != null) { |
| 54 | + Kernel32.INSTANCE.UnmapViewOfFile(clientView); |
| 55 | + } |
| 56 | + if (originalView != null) { |
| 57 | + Kernel32.INSTANCE.UnmapViewOfFile(originalView); |
| 58 | + } |
| 59 | + if (opened != null) { |
| 60 | + Kernel32.INSTANCE.CloseHandle(opened); |
| 61 | + } |
| 62 | + Kernel32.INSTANCE.CloseHandle(created); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void returnsNullForMissingMapping() { |
| 68 | + assertNull(ClientConnectionW32.MappingKernel.INSTANCE.OpenFileMapping( |
| 69 | + WinNT.SECTION_MAP_READ, false, "Local\\jbwapi-missing-" + System.nanoTime())); |
| 70 | + } |
| 71 | +} |
0 commit comments