Introduce mmap_file function
[deliverable/binutils-gdb.git] / gdb / unittests / scoped_mmap-selftests.c
1 /* Self tests for scoped_mmap for GDB, the GNU debugger.
2
3 Copyright (C) 2018 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include "defs.h"
21
22 #include "common/scoped_mmap.h"
23 #include "config.h"
24
25 #if defined(HAVE_SYS_MMAN_H) && defined(HAVE_UNISTD_H)
26
27 #include "selftest.h"
28 #include "common/gdb_unlinker.h"
29
30 #include <unistd.h>
31
32 namespace selftests {
33 namespace scoped_mmap {
34
35 /* Test that the file is unmapped. */
36 static void
37 test_destroy ()
38 {
39 void *mem;
40
41 errno = 0;
42 {
43 ::scoped_mmap smmap (nullptr, sysconf (_SC_PAGESIZE), PROT_WRITE,
44 MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
45
46 mem = smmap.get ();
47 SELF_CHECK (mem != nullptr);
48 }
49
50 SELF_CHECK (msync (mem, sysconf (_SC_PAGESIZE), 0) == -1 && errno == ENOMEM);
51 }
52
53 /* Test that the memory can be released. */
54 static void
55 test_release ()
56 {
57 void *mem;
58
59 errno = 0;
60 {
61 ::scoped_mmap smmap (nullptr, sysconf (_SC_PAGESIZE), PROT_WRITE,
62 MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
63
64 mem = smmap.release ();
65 SELF_CHECK (mem != nullptr);
66 }
67
68 SELF_CHECK (msync (mem, sysconf (_SC_PAGESIZE), 0) == 0 || errno != ENOMEM);
69
70 munmap (mem, sysconf (_SC_PAGESIZE));
71 }
72
73 /* Run selftests. */
74 static void
75 run_tests ()
76 {
77 test_destroy ();
78 test_release ();
79 }
80
81 } /* namespace scoped_mmap */
82
83 namespace mmap_file
84 {
85
86 /* Test the standard usage of mmap_file. */
87 static void
88 test_normal ()
89 {
90 char filename[] = "scoped_mmapped_file-selftest-XXXXXX";
91 int fd = mkstemp (filename);
92 SELF_CHECK (fd >= 0);
93
94 write (fd, "Hello!", 7);
95 close (fd);
96
97 gdb::unlinker unlink_test_file (filename);
98
99 {
100 ::scoped_mmap m = ::mmap_file (filename);
101
102 SELF_CHECK (m.get () != MAP_FAILED);
103 SELF_CHECK (m.size () == 7);
104 SELF_CHECK (0 == strcmp ((char *) m.get (), "Hello!"));
105 }
106 }
107
108 /* Calling mmap_file with a non-existent file should throw an exception. */
109 static void
110 test_invalid_filename ()
111 {
112 bool threw = false;
113
114 try {
115 ::scoped_mmap m = ::mmap_file ("/this/file/should/not/exist");
116 } catch (gdb_exception &e) {
117 threw = true;
118 }
119
120 SELF_CHECK (threw);
121 }
122
123
124 /* Run selftests. */
125 static void
126 run_tests ()
127 {
128 test_normal ();
129 test_invalid_filename ();
130 }
131
132 } /* namespace mmap_file */
133 } /* namespace selftests */
134
135 #endif /* !defined(HAVE_SYS_MMAN_H) || !defined(HAVE_UNISTD_H) */
136
137 void
138 _initialize_scoped_mmap_selftests ()
139 {
140 #if defined(HAVE_SYS_MMAN_H) && defined(HAVE_UNISTD_H)
141 selftests::register_test ("scoped_mmap",
142 selftests::scoped_mmap::run_tests);
143 selftests::register_test ("mmap_file",
144 selftests::mmap_file::run_tests);
145 #endif
146 }
This page took 0.031726 seconds and 4 git commands to generate.