try to cleanup the mess in the input code
[pcsx_rearmed.git] / deps / mman / test.c
CommitLineData
81c4586f
PC
1
2#include "mman.h"
3
4#include <errno.h>
5#include <fcntl.h>
6#include <sys/stat.h>
7
8#ifndef NULL
9#define NULL (void*)0
10#endif
11
12const char* map_file_name = "map_file.dat";
13
14int test_anon_map_readwrite()
15{
16 void* map = mmap(NULL, 1024, PROT_READ | PROT_WRITE,
17 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
18 if (map == MAP_FAILED)
19 {
20 printf("mmap (MAP_ANONYMOUS, PROT_READ | PROT_WRITE) returned unexpected error: %d\n", errno);
21 return -1;
22 }
23
24 *((unsigned char*)map) = 1;
25
26 int result = munmap(map, 1024);
27
28 if (result != 0)
29 printf("munmap (MAP_ANONYMOUS, PROT_READ | PROT_WRITE) returned unexpected error: %d\n", errno);
30
31 return result;
32}
33
34int test_anon_map_readonly()
35{
36 void* map = mmap(NULL, 1024, PROT_READ,
37 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
38 if (map == MAP_FAILED)
39 {
40 printf("mmap (MAP_ANONYMOUS, PROT_READ) returned unexpected error: %d\n", errno);
41 return -1;
42 }
43
44 *((unsigned char*)map) = 1;
45
46 int result = munmap(map, 1024);
47
48 if (result != 0)
49 printf("munmap (MAP_ANONYMOUS, PROT_READ) returned unexpected error: %d\n", errno);
50
51 return result;
52}
53
54int test_anon_map_writeonly()
55{
56 void* map = mmap(NULL, 1024, PROT_WRITE,
57 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
58 if (map == MAP_FAILED)
59 {
60 printf("mmap (MAP_ANONYMOUS, PROT_WRITE) returned unexpected error: %d\n", errno);
61 return -1;
62 }
63
64 *((unsigned char*)map) = 1;
65
66 int result = munmap(map, 1024);
67
68 if (result != 0)
69 printf("munmap (MAP_ANONYMOUS, PROT_WRITE) returned unexpected error: %d\n", errno);
70
71 return result;
72}
73
74int test_anon_map_readonly_nowrite()
75{
76 void* map = mmap(NULL, 1024, PROT_READ,
77 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
78 if (map == MAP_FAILED)
79 {
80 printf("mmap (MAP_ANONYMOUS, PROT_READ) returned unexpected error: %d\n", errno);
81 return -1;
82 }
83
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));
87
88 int result = munmap(map, 1024);
89
90 if (result != 0)
91 printf("munmap (MAP_ANONYMOUS, PROT_READ) returned unexpected error: %d\n", errno);
92
93 return result;
94}
95
96int test_file_map_readwrite()
97{
98 mode_t mode = S_IRUSR | S_IWUSR;
99 int o = open(map_file_name, O_TRUNC | O_BINARY | O_RDWR | O_CREAT, mode);
100
101 void* map = mmap(NULL, 1024, PROT_READ | PROT_WRITE, MAP_PRIVATE, o, 0);
102 if (map == MAP_FAILED)
103 {
104 printf("mmap returned unexpected error: %d\n", errno);
105 return -1;
106 }
107
108 *((unsigned char*)map) = 1;
109
110 int result = munmap(map, 1024);
111
112 if (result != 0)
113 printf("munmap returned unexpected error: %d\n", errno);
114
115 close(o);
116
117 /*TODO: get file info and content and compare it with the sources conditions */
118 unlink(map_file_name);
119
120 return result;
121}
122
123int test_file_map_mlock_munlock()
124{
125 const size_t map_size = 1024;
126
127 int result = 0;
128 mode_t mode = S_IRUSR | S_IWUSR;
129 int o = open(map_file_name, O_TRUNC | O_BINARY | O_RDWR | O_CREAT, mode);
130 if (o == -1)
131 {
132 printf("unable to create file %s: %d\n", map_file_name, errno);
133 return -1;
134 }
135
136 void* map = mmap(NULL, map_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, o, 0);
137 if (map == MAP_FAILED)
138 {
139 printf("mmap returned unexpected error: %d\n", errno);
140 result = -1;
141 goto done_close;
142 }
143
144 if (mlock(map, map_size) != 0)
145 {
146 printf("mlock returned unexpected error: %d\n", errno);
147 result = -1;
148 goto done_munmap;
149 }
150
151 *((unsigned char*)map) = 1;
152
153 if (munlock(map, map_size) != 0)
154 {
155 printf("munlock returned unexpected error: %d\n", errno);
156 result = -1;
157 }
158
159done_munmap:
160 result = munmap(map, map_size);
161
162 if (result != 0)
163 printf("munmap returned unexpected error: %d\n", errno);
164
165done_close:
166 close(o);
167
168 unlink(map_file_name);
169done:
170 return result;
171}
172
173int test_file_map_msync()
174{
175 const size_t map_size = 1024;
176
177 int result = 0;
178 mode_t mode = S_IRUSR | S_IWUSR;
179 int o = open(map_file_name, O_TRUNC | O_BINARY | O_RDWR | O_CREAT, mode);
180 if (o == -1)
181 {
182 printf("unable to create file %s: %d\n", map_file_name, errno);
183 return -1;
184 }
185
186 void* map = mmap(NULL, map_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, o, 0);
187 if (map == MAP_FAILED)
188 {
189 printf("mmap returned unexpected error: %d\n", errno);
190 result = -1;
191 goto done_close;
192 }
193
194 *((unsigned char*)map) = 1;
195
196 if (msync(map, map_size, MS_SYNC) != 0)
197 {
198 printf("msync returned unexpected error: %d\n", errno);
199 result = -1;
200 }
201
202 result = munmap(map, map_size);
203
204 if (result != 0)
205 printf("munmap returned unexpected error: %d\n", errno);
206
207done_close:
208 close(o);
209
210 unlink(map_file_name);
211done:
212 return result;
213}
214
215#define EXEC_TEST(name) \
216 if (name() != 0) { result = -1; printf( #name ": fail\n"); } \
217 else { printf(#name ": pass\n"); }
218
219int main()
220{
221 int result = 0;
222
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);
228
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);
233
234 return result;
235}