Moving the tests.....still more....
[reactos.git] / rosapps / tests / map_dup_inherit / map_dup_inherit.c
1 #include <stdio.h>
2 #include <windows.h>
3 #include <stdlib.h>
4 #include <string.h>
5
6 /* This tests the ability of the target win32 to create an anonymous file
7 * mapping, create a mapping view with MapViewOfFile, and then realize the
8 * pages with VirtualAlloc.
9 */
10
11 int main( int argc, char **argv ) {
12 HANDLE file_view;
13 void *file_map;
14 int *x;
15
16 fprintf( stderr, "%lu: Starting\n", GetCurrentProcessId() );
17
18 if( argc == 2 ) {
19 file_map = (void *)atoi(argv[1]);
20 } else {
21 file_map = CreateFileMapping( INVALID_HANDLE_VALUE,
22 NULL,
23 PAGE_READWRITE | SEC_RESERVE,
24 0, 0x1000, NULL );
25 if( !SetHandleInformation( file_map,
26 HANDLE_FLAG_INHERIT,
27 HANDLE_FLAG_INHERIT ) ) {
28 fprintf( stderr, "%lu: Could not make handle inheritable.\n",
29 GetCurrentProcessId() );
30 return 100;
31 }
32 }
33
34 if( !file_map ) {
35 fprintf( stderr, "%lu: Could not create anonymous file map.\n",
36 GetCurrentProcessId() );
37 return 1;
38 }
39
40 file_view = MapViewOfFile( file_map,
41 FILE_MAP_WRITE,
42 0,
43 0,
44 0x1000 );
45
46 if( !file_view ) {
47 fprintf( stderr, "%lu: Could not map view of file.\n",
48 GetCurrentProcessId() );
49 return 2;
50 }
51
52 if( !VirtualAlloc( file_view, 0x1000, MEM_COMMIT, PAGE_READWRITE ) ) {
53 fprintf( stderr, "%lu: VirtualAlloc failed to realize the page.\n",
54 GetCurrentProcessId() );
55 return 3;
56 }
57
58 x = (int *)file_view;
59 x[0] = 0x12345678;
60
61 if( x[0] != 0x12345678 ) {
62 fprintf( stderr, "%lu: Can't write to the memory (%08x != 0x12345678)\n",
63 GetCurrentProcessId(), x[0] );
64 return 4;
65 }
66
67 if( argc == 1 ) {
68 STARTUPINFO si;
69 PROCESS_INFORMATION pi;
70 char cmdline[1000];
71
72 memset( &si, 0, sizeof( si ) );
73 memset( &pi, 0, sizeof( pi ) );
74
75 sprintf(cmdline,"%s %d", argv[0], (int)file_map);
76 if( !CreateProcess(NULL, cmdline, NULL, NULL, TRUE, 0, NULL, NULL,
77 &si, &pi ) ) {
78 fprintf( stderr, "%lu: Could not create child process.\n",
79 GetCurrentProcessId() );
80 return 5;
81 }
82
83 if( WaitForSingleObject( pi.hThread, INFINITE ) != WAIT_OBJECT_0 ) {
84 fprintf( stderr, "%lu: Failed to wait for child process to terminate.\n",
85 GetCurrentProcessId() );
86 return 6;
87 }
88 }
89
90 return 0;
91 }