aboutsummaryrefslogtreecommitdiff
path: root/deps/mman/test.c
blob: 9374b9f15034713aec86405dab36fa9f4601bb3c (plain)
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#include "mman.h"

#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>

#ifndef NULL
#define NULL    (void*)0
#endif

const char* map_file_name = "map_file.dat";

int test_anon_map_readwrite()
{    
    void* map = mmap(NULL, 1024, PROT_READ | PROT_WRITE,
 		      MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
    if (map == MAP_FAILED)
    {
        printf("mmap (MAP_ANONYMOUS, PROT_READ | PROT_WRITE) returned unexpected error: %d\n", errno);
        return -1;
    }
        
    *((unsigned char*)map) = 1;
    
    int result = munmap(map, 1024);
    
    if (result != 0)
        printf("munmap (MAP_ANONYMOUS, PROT_READ | PROT_WRITE) returned unexpected error: %d\n", errno);
        
    return result;
}

int test_anon_map_readonly()
{    
    void* map = mmap(NULL, 1024, PROT_READ,
 		      MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
    if (map == MAP_FAILED)
    {
        printf("mmap (MAP_ANONYMOUS, PROT_READ) returned unexpected error: %d\n", errno);
        return -1;
    }
        
    *((unsigned char*)map) = 1;
    
    int result = munmap(map, 1024);
    
    if (result != 0)
        printf("munmap (MAP_ANONYMOUS, PROT_READ) returned unexpected error: %d\n", errno);
        
    return result;
}

int test_anon_map_writeonly()
{    
    void* map = mmap(NULL, 1024, PROT_WRITE,
 		      MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
    if (map == MAP_FAILED)
    {
        printf("mmap (MAP_ANONYMOUS, PROT_WRITE) returned unexpected error: %d\n", errno);
        return -1;
    }
        
    *((unsigned char*)map) = 1;
    
    int result = munmap(map, 1024);
    
    if (result != 0)
        printf("munmap (MAP_ANONYMOUS, PROT_WRITE) returned unexpected error: %d\n", errno);
        
    return result;
}

int test_anon_map_readonly_nowrite()
{    
    void* map = mmap(NULL, 1024, PROT_READ,
 		      MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
    if (map == MAP_FAILED)
    {
        printf("mmap (MAP_ANONYMOUS, PROT_READ) returned unexpected error: %d\n", errno);
        return -1;
    }
    
    if (*((unsigned char*)map) != 0)
        printf("test_anon_map_readonly_nowrite (MAP_ANONYMOUS, PROT_READ) returned unexpected value: %d\n", 
                (int)*((unsigned char*)map));
        
    int result = munmap(map, 1024);
    
    if (result != 0)
        printf("munmap (MAP_ANONYMOUS, PROT_READ) returned unexpected error: %d\n", errno);
        
    return result;
}

int test_file_map_readwrite()
{
    mode_t mode = S_IRUSR | S_IWUSR;
    int o = open(map_file_name, O_TRUNC | O_BINARY | O_RDWR | O_CREAT, mode);

    void* map = mmap(NULL, 1024, PROT_READ | PROT_WRITE, MAP_PRIVATE, o, 0);
    if (map == MAP_FAILED)
    {
        printf("mmap returned unexpected error: %d\n", errno);
        return -1;
    }

    *((unsigned char*)map) = 1;
    
    int result = munmap(map, 1024);
    
    if (result != 0)
        printf("munmap returned unexpected error: %d\n", errno);
    
    close(o);
    
    /*TODO: get file info and content and compare it with the sources conditions */
    unlink(map_file_name);
    
    return result;
}

int test_file_map_mlock_munlock()
{
    const size_t map_size = 1024;
    
    int result = 0;
    mode_t mode = S_IRUSR | S_IWUSR;
    int o = open(map_file_name, O_TRUNC | O_BINARY | O_RDWR | O_CREAT, mode);
    if (o == -1)
    {
        printf("unable to create file %s: %d\n", map_file_name, errno);
        return -1;
    }

    void* map = mmap(NULL, map_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, o, 0);
    if (map == MAP_FAILED)
    {
        printf("mmap returned unexpected error: %d\n", errno);
        result = -1;
        goto done_close;
    }
     
    if (mlock(map, map_size) != 0)
    {
        printf("mlock returned unexpected error: %d\n", errno);
        result = -1;
        goto done_munmap;        
    }
    
    *((unsigned char*)map) = 1;
    
    if (munlock(map, map_size) != 0)
    {
        printf("munlock returned unexpected error: %d\n", errno);
        result = -1;
    }
    
done_munmap:
    result = munmap(map, map_size);
    
    if (result != 0)
        printf("munmap returned unexpected error: %d\n", errno);
        
done_close:
    close(o);
    
    unlink(map_file_name);
done:
    return result;
}

int test_file_map_msync()
{
    const size_t map_size = 1024;
    
    int result = 0;
    mode_t mode = S_IRUSR | S_IWUSR;
    int o = open(map_file_name, O_TRUNC | O_BINARY | O_RDWR | O_CREAT, mode);
    if (o == -1)
    {
        printf("unable to create file %s: %d\n", map_file_name, errno);
        return -1;
    }

    void* map = mmap(NULL, map_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, o, 0);
    if (map == MAP_FAILED)
    {
        printf("mmap returned unexpected error: %d\n", errno);
        result = -1;
        goto done_close;
    }
     
    *((unsigned char*)map) = 1;

    if (msync(map, map_size, MS_SYNC) != 0)
    {
        printf("msync returned unexpected error: %d\n", errno);
        result = -1;
    }
    
    result = munmap(map, map_size);
    
    if (result != 0)
        printf("munmap returned unexpected error: %d\n", errno);
        
done_close:
    close(o);
    
    unlink(map_file_name);
done:
    return result;
}

#define EXEC_TEST(name) \
    if (name() != 0) { result = -1; printf( #name ": fail\n"); } \
    else { printf(#name ": pass\n"); }

int main()
{
    int result = 0;
    
    EXEC_TEST(test_anon_map_readwrite);
    //NOTE: this test must cause an access violation exception
    //EXEC_TEST(test_anon_map_readonly);
    EXEC_TEST(test_anon_map_readonly_nowrite);
    EXEC_TEST(test_anon_map_writeonly);
    
    EXEC_TEST(test_file_map_readwrite);
    EXEC_TEST(test_file_map_mlock_munlock);
    EXEC_TEST(test_file_map_msync);
    //TODO: EXEC_TEST(test_file_map_mprotect);
    
    return result;
}