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