12 const char* map_file_name = "map_file.dat";
14 int test_anon_map_readwrite()
16 void* map = mmap(NULL, 1024, PROT_READ | PROT_WRITE,
17 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
18 if (map == MAP_FAILED)
20 printf("mmap (MAP_ANONYMOUS, PROT_READ | PROT_WRITE) returned unexpected error: %d\n", errno);
24 *((unsigned char*)map) = 1;
26 int result = munmap(map, 1024);
29 printf("munmap (MAP_ANONYMOUS, PROT_READ | PROT_WRITE) returned unexpected error: %d\n", errno);
34 int test_anon_map_readonly()
36 void* map = mmap(NULL, 1024, PROT_READ,
37 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
38 if (map == MAP_FAILED)
40 printf("mmap (MAP_ANONYMOUS, PROT_READ) returned unexpected error: %d\n", errno);
44 *((unsigned char*)map) = 1;
46 int result = munmap(map, 1024);
49 printf("munmap (MAP_ANONYMOUS, PROT_READ) returned unexpected error: %d\n", errno);
54 int test_anon_map_writeonly()
56 void* map = mmap(NULL, 1024, PROT_WRITE,
57 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
58 if (map == MAP_FAILED)
60 printf("mmap (MAP_ANONYMOUS, PROT_WRITE) returned unexpected error: %d\n", errno);
64 *((unsigned char*)map) = 1;
66 int result = munmap(map, 1024);
69 printf("munmap (MAP_ANONYMOUS, PROT_WRITE) returned unexpected error: %d\n", errno);
74 int test_anon_map_readonly_nowrite()
76 void* map = mmap(NULL, 1024, PROT_READ,
77 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
78 if (map == MAP_FAILED)
80 printf("mmap (MAP_ANONYMOUS, PROT_READ) returned unexpected error: %d\n", errno);
84 if (*((unsigned char*)map) != 0)
85 printf("test_anon_map_readonly_nowrite (MAP_ANONYMOUS, PROT_READ) returned unexpected value: %d\n",
86 (int)*((unsigned char*)map));
88 int result = munmap(map, 1024);
91 printf("munmap (MAP_ANONYMOUS, PROT_READ) returned unexpected error: %d\n", errno);
96 int test_file_map_readwrite()
98 mode_t mode = S_IRUSR | S_IWUSR;
99 int o = open(map_file_name, O_TRUNC | O_BINARY | O_RDWR | O_CREAT, mode);
101 void* map = mmap(NULL, 1024, PROT_READ | PROT_WRITE, MAP_PRIVATE, o, 0);
102 if (map == MAP_FAILED)
104 printf("mmap returned unexpected error: %d\n", errno);
108 *((unsigned char*)map) = 1;
110 int result = munmap(map, 1024);
113 printf("munmap returned unexpected error: %d\n", errno);
117 /*TODO: get file info and content and compare it with the sources conditions */
118 unlink(map_file_name);
123 int test_file_map_mlock_munlock()
125 const size_t map_size = 1024;
128 mode_t mode = S_IRUSR | S_IWUSR;
129 int o = open(map_file_name, O_TRUNC | O_BINARY | O_RDWR | O_CREAT, mode);
132 printf("unable to create file %s: %d\n", map_file_name, errno);
136 void* map = mmap(NULL, map_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, o, 0);
137 if (map == MAP_FAILED)
139 printf("mmap returned unexpected error: %d\n", errno);
144 if (mlock(map, map_size) != 0)
146 printf("mlock returned unexpected error: %d\n", errno);
151 *((unsigned char*)map) = 1;
153 if (munlock(map, map_size) != 0)
155 printf("munlock returned unexpected error: %d\n", errno);
160 result = munmap(map, map_size);
163 printf("munmap returned unexpected error: %d\n", errno);
168 unlink(map_file_name);
173 int test_file_map_msync()
175 const size_t map_size = 1024;
178 mode_t mode = S_IRUSR | S_IWUSR;
179 int o = open(map_file_name, O_TRUNC | O_BINARY | O_RDWR | O_CREAT, mode);
182 printf("unable to create file %s: %d\n", map_file_name, errno);
186 void* map = mmap(NULL, map_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, o, 0);
187 if (map == MAP_FAILED)
189 printf("mmap returned unexpected error: %d\n", errno);
194 *((unsigned char*)map) = 1;
196 if (msync(map, map_size, MS_SYNC) != 0)
198 printf("msync returned unexpected error: %d\n", errno);
202 result = munmap(map, map_size);
205 printf("munmap returned unexpected error: %d\n", errno);
210 unlink(map_file_name);
215 #define EXEC_TEST(name) \
216 if (name() != 0) { result = -1; printf( #name ": fail\n"); } \
217 else { printf(#name ": pass\n"); }
223 EXEC_TEST(test_anon_map_readwrite);
224 //NOTE: this test must cause an access violation exception
225 //EXEC_TEST(test_anon_map_readonly);
226 EXEC_TEST(test_anon_map_readonly_nowrite);
227 EXEC_TEST(test_anon_map_writeonly);
229 EXEC_TEST(test_file_map_readwrite);
230 EXEC_TEST(test_file_map_mlock_munlock);
231 EXEC_TEST(test_file_map_msync);
232 //TODO: EXEC_TEST(test_file_map_mprotect);