1 /* Self tests for scoped_mmap for GDB, the GNU debugger.
3 Copyright (C) 2018 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/>. */
22 #include "common/scoped_mmap.h"
25 #if defined(HAVE_SYS_MMAN_H)
28 #include "common/gdb_unlinker.h"
33 namespace scoped_mmap
{
35 /* Test that the file is unmapped. */
43 ::scoped_mmap
smmap (nullptr, sysconf (_SC_PAGESIZE
), PROT_WRITE
,
44 MAP_ANONYMOUS
| MAP_PRIVATE
, 0, 0);
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. */
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
));
81 } /* namespace scoped_mmap */
86 /* Test the standard usage of mmap_file. */
90 char filename
[] = "scoped_mmapped_file-selftest-XXXXXX";
91 int fd
= mkstemp (filename
);
94 SELF_CHECK (write (fd
, "Hello!", 7) == 7);
97 gdb::unlinker
unlink_test_file (filename
);
100 ::scoped_mmap m
= ::mmap_file (filename
);
102 SELF_CHECK (m
.get () != MAP_FAILED
);
103 SELF_CHECK (m
.size () == 7);
104 SELF_CHECK (0 == strcmp ((char *) m
.get (), "Hello!"));
108 /* Calling mmap_file with a non-existent file should throw an exception. */
110 test_invalid_filename ()
115 ::scoped_mmap m
= ::mmap_file ("/this/file/should/not/exist");
116 } catch (gdb_exception
&e
) {
129 test_invalid_filename ();
132 } /* namespace mmap_file */
133 } /* namespace selftests */
135 #endif /* !defined(HAVE_SYS_MMAN_H) */
138 _initialize_scoped_mmap_selftests ()
140 #if defined(HAVE_SYS_MMAN_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
);
This page took 0.032709 seconds and 4 git commands to generate.