Load 57 into trunk.
[nativeclient.git] / tests / mmap / mmap_test.cc
blob7b889cc9870cc5fdf751c3fd84062c68d8f9226a
1 /*
2 * Copyright 2008, Google Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
14 * distribution.
15 * * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include <errno.h>
35 #include <stdlib.h>
36 #include <stdio.h>
37 #include <string.h>
38 #include <sys/mman.h>
40 #define PRINT_HEADER 0
41 #define TEXT_LINE_SIZE 1024
45 * function failed(testname, msg)
46 * print failure message and exit with a return code of -1
49 bool failed(const char *testname, const char *msg) {
50 printf("TEST FAILED: %s: %s\n", testname, msg);
51 return false;
56 * function passed(testname, msg)
57 * print success message
60 bool passed(const char *testname, const char *msg) {
61 printf("TEST PASSED: %s: %s\n", testname, msg);
62 return true;
68 * function test*()
70 * Simple tests follow below. Each test may call one or more
71 * of the functions above. They all have a boolean return value
72 * to indicate success (all tests passed) or failure (one or more
73 * tests failed) Order matters - the parent should call
74 * test1() before test2(), and so on.
77 bool test1()
79 int size = 64 * 1024; /* we need 64K */
80 void *zeroes;
81 // test simple mmap
82 void *res = NULL;
83 int rv;
85 printf("test1\n");
87 res = mmap(res, size, PROT_READ, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
88 if (0 >= res) /* define MAP_FAILED */
89 return false;
90 printf("mmap done\n");
91 zeroes = malloc(size);
92 memset(zeroes, 0, size);
93 if (memcmp(res, zeroes, size)) {
94 printf("memcmp failed\n");
95 return false;
98 rv = munmap(res, 1024);
99 if (rv != 0) {
100 printf("munmap failed\n");
101 return false;
103 printf("munmap good\n");
104 return true;
110 * Verify that munmap of executable text pages will fail.
113 bool test2()
115 int rv;
117 printf("test2\n");
119 rv = munmap((void *) (1<<16), (size_t) (1<<16)); /* text starts at 64K */
122 * if the munmap succeeds, we probably won't be able to continue to
123 * run....
125 printf("munmap returned %d\n", rv);
127 if (0 != rv) {
128 printf("munmap good (failed as expected)\n");
129 return true;
131 return false;
137 * Verify that mmap into the NULL pointer guard page will fail.
140 bool test3()
142 void *res;
144 printf("test3\n");
145 res = mmap((void *) 0, (size_t) (1 << 16),
146 PROT_READ, MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, 0, 0);
147 printf("res = %p\n", res);
148 if (MAP_FAILED == res) {
149 printf("mmap okay\n");
150 return true;
151 } else {
152 printf("mmap should not have succeeded\n");
153 return false;
159 * function testSuite()
161 * Run through a complete sequence of file tests.
163 * returns true if all tests succeed. false if one or more fail.
166 bool testSuite()
168 bool ret = true;
169 // The order of executing these tests matters!
170 ret &= test1();
171 ret &= test2();
172 ret &= test3();
173 return ret;
178 * main entry point.
180 * run all tests and call system exit with appropriate value
181 * 0 - success, all tests passed.
182 * -1 - one or more tests failed.
185 int main(const int argc, const char *argv[])
187 bool passed;
189 // run the full test suite
190 passed = testSuite();
192 if (passed) {
193 printf("All tests PASSED\n");
194 exit(0);
195 } else {
196 printf("One or more tests FAILED\n");
197 exit(-1);