Add translations for various sub-directories
[binutils-gdb.git] / gdb / unittests / scoped_mmap-selftests.c
blob09a94ed6590e649ff26ec05c9286eb5899a7dab2
1 /* Self tests for scoped_mmap for GDB, the GNU debugger.
3 Copyright (C) 2018-2024 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
21 #include "gdbsupport/filestuff.h"
22 #include "gdbsupport/scoped_mmap.h"
23 #include "config.h"
25 #if defined(HAVE_SYS_MMAN_H)
27 #include "gdbsupport/selftest.h"
28 #include "gdbsupport/gdb_unlinker.h"
30 #include <unistd.h>
32 namespace selftests {
33 namespace scoped_mmap {
35 /* Test that the file is unmapped. */
36 static void
37 test_destroy ()
39 void *mem;
41 errno = 0;
43 ::scoped_mmap smmap (nullptr, sysconf (_SC_PAGESIZE), PROT_WRITE,
44 MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
46 mem = smmap.get ();
47 SELF_CHECK (mem != nullptr);
50 SELF_CHECK (msync (mem, sysconf (_SC_PAGESIZE), 0) == -1 && errno == ENOMEM);
53 /* Test that the memory can be released. */
54 static void
55 test_release ()
57 void *mem;
59 errno = 0;
61 ::scoped_mmap smmap (nullptr, sysconf (_SC_PAGESIZE), PROT_WRITE,
62 MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
64 mem = smmap.release ();
65 SELF_CHECK (mem != nullptr);
68 SELF_CHECK (msync (mem, sysconf (_SC_PAGESIZE), 0) == 0 || errno != ENOMEM);
70 munmap (mem, sysconf (_SC_PAGESIZE));
73 /* Run selftests. */
74 static void
75 run_tests ()
77 test_destroy ();
78 test_release ();
81 } /* namespace scoped_mmap */
83 namespace mmap_file
86 /* Test the standard usage of mmap_file. */
87 static void
88 test_normal ()
90 char filename[] = "scoped_mmapped_file-selftest-XXXXXX";
92 scoped_fd fd = gdb_mkostemp_cloexec (filename);
93 SELF_CHECK (fd.get () >= 0);
95 SELF_CHECK (write (fd.get (), "Hello!", 7) == 7);
98 gdb::unlinker unlink_test_file (filename);
101 ::scoped_mmap m = ::mmap_file (filename);
103 SELF_CHECK (m.get () != MAP_FAILED);
104 SELF_CHECK (m.size () == 7);
105 SELF_CHECK (0 == strcmp ((char *) m.get (), "Hello!"));
109 /* Calling mmap_file with a non-existent file should throw an exception. */
110 static void
111 test_invalid_filename ()
113 bool threw = false;
115 try {
116 ::scoped_mmap m = ::mmap_file ("/this/file/should/not/exist");
117 } catch (const gdb_exception &e) {
118 threw = true;
121 SELF_CHECK (threw);
125 /* Run selftests. */
126 static void
127 run_tests ()
129 test_normal ();
130 test_invalid_filename ();
133 } /* namespace mmap_file */
134 } /* namespace selftests */
136 #endif /* !defined(HAVE_SYS_MMAN_H) */
138 void _initialize_scoped_mmap_selftests ();
139 void
140 _initialize_scoped_mmap_selftests ()
142 #if defined(HAVE_SYS_MMAN_H)
143 selftests::register_test ("scoped_mmap",
144 selftests::scoped_mmap::run_tests);
145 selftests::register_test ("mmap_file",
146 selftests::mmap_file::run_tests);
147 #endif