Use standard POSIX "ustar" format for the scratch disk.
[pintos-anon] / src / tests / vm / mmap-clean.c
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
10 void
11 test_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 }