summaryrefslogtreecommitdiffstats
path: root/tests/vm/mmap-clean.c
diff options
context:
space:
mode:
authormanuel <manuel@mausz.at>2012-03-27 11:51:08 +0200
committermanuel <manuel@mausz.at>2012-03-27 11:51:08 +0200
commit4f670845ff9ab6c48bcb5f7bf4d4ef6dc3c3064b (patch)
tree868c52e06f207b5ec8a3cc141f4b8b2bdfcc165c /tests/vm/mmap-clean.c
parenteae0bd57f0a26314a94785061888d193d186944a (diff)
downloadprogos-4f670845ff9ab6c48bcb5f7bf4d4ef6dc3c3064b.tar.gz
progos-4f670845ff9ab6c48bcb5f7bf4d4ef6dc3c3064b.tar.bz2
progos-4f670845ff9ab6c48bcb5f7bf4d4ef6dc3c3064b.zip
reorganize file structure to match the upstream requirements
Diffstat (limited to 'tests/vm/mmap-clean.c')
-rw-r--r--tests/vm/mmap-clean.c53
1 files changed, 53 insertions, 0 deletions
diff --git a/tests/vm/mmap-clean.c b/tests/vm/mmap-clean.c
new file mode 100644
index 0000000..ea1dc9c
--- /dev/null
+++ b/tests/vm/mmap-clean.c
@@ -0,0 +1,53 @@
1/* Verifies that mmap'd regions are only written back on munmap
2 if the data was actually modified in memory. */
3
4#include <string.h>
5#include <syscall.h>
6#include "tests/vm/sample.inc"
7#include "tests/lib.h"
8#include "tests/main.h"
9
10void
11test_main (void)
12{
13 static const char overwrite[] = "Now is the time for all good...";
14 static char buffer[sizeof sample - 1];
15 char *actual = (char *) 0x54321000;
16 int handle;
17 mapid_t map;
18
19 /* Open file, map, verify data. */
20 CHECK ((handle = open ("sample.txt")) > 1, "open \"sample.txt\"");
21 CHECK ((map = mmap (handle, actual)) != MAP_FAILED, "mmap \"sample.txt\"");
22 if (memcmp (actual, sample, strlen (sample)))
23 fail ("read of mmap'd file reported bad data");
24
25 /* Modify file. */
26 CHECK (write (handle, overwrite, strlen (overwrite))
27 == (int) strlen (overwrite),
28 "write \"sample.txt\"");
29
30 /* Close mapping. Data should not be written back, because we
31 didn't modify it via the mapping. */
32 msg ("munmap \"sample.txt\"");
33 munmap (map);
34
35 /* Read file back. */
36 msg ("seek \"sample.txt\"");
37 seek (handle, 0);
38 CHECK (read (handle, buffer, sizeof buffer) == sizeof buffer,
39 "read \"sample.txt\"");
40
41 /* Verify that file overwrite worked. */
42 if (memcmp (buffer, overwrite, strlen (overwrite))
43 || memcmp (buffer + strlen (overwrite), sample + strlen (overwrite),
44 strlen (sample) - strlen (overwrite)))
45 {
46 if (!memcmp (buffer, sample, strlen (sample)))
47 fail ("munmap wrote back clean page");
48 else
49 fail ("read surprising data from file");
50 }
51 else
52 msg ("file change was retained after munmap");
53}