Commit | Line | Data |
---|---|---|
e418a61a TT |
1 | /* Self tests for scoped_fd 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/filestuff.h" | |
23 | #include "selftest.h" | |
24 | ||
25 | namespace selftests { | |
26 | namespace mkdir_recursive { | |
27 | ||
28 | /* Try to create DIR using mkdir_recursive and make sure it exists. */ | |
29 | ||
30 | static bool | |
31 | create_dir_and_check (const char *dir) | |
32 | { | |
33 | ::mkdir_recursive (dir); | |
34 | ||
35 | struct stat st; | |
36 | if (stat (dir, &st) != 0) | |
37 | perror_with_name (("stat")); | |
38 | ||
39 | return (st.st_mode & S_IFDIR) != 0; | |
40 | } | |
41 | ||
42 | /* Test mkdir_recursive. */ | |
43 | ||
44 | static void | |
45 | test () | |
46 | { | |
47 | char base[] = "/tmp/gdb-selftests-XXXXXX"; | |
48 | ||
49 | if (mkdtemp (base) == NULL) | |
50 | perror_with_name (("mkdtemp")); | |
51 | ||
52 | /* Try not to leave leftover directories. */ | |
53 | struct cleanup_dirs { | |
54 | cleanup_dirs (const char *base) | |
55 | : m_base (base) | |
56 | {} | |
57 | ||
58 | ~cleanup_dirs () { | |
59 | rmdir (string_printf ("%s/a/b/c/d/e", m_base).c_str ()); | |
60 | rmdir (string_printf ("%s/a/b/c/d", m_base).c_str ()); | |
61 | rmdir (string_printf ("%s/a/b/c", m_base).c_str ()); | |
62 | rmdir (string_printf ("%s/a/b", m_base).c_str ()); | |
63 | rmdir (string_printf ("%s/a", m_base).c_str ()); | |
64 | rmdir (m_base); | |
65 | } | |
66 | ||
67 | private: | |
68 | const char *m_base; | |
69 | } cleanup_dirs (base); | |
70 | ||
71 | std::string dir = string_printf ("%s/a/b", base); | |
72 | SELF_CHECK (create_dir_and_check (dir.c_str ())); | |
73 | ||
74 | dir = string_printf ("%s/a/b/c//d/e/", base); | |
75 | SELF_CHECK (create_dir_and_check (dir.c_str ())); | |
76 | } | |
77 | ||
78 | } | |
79 | } | |
80 | ||
81 | void | |
82 | _initialize_mkdir_recursive_selftests () | |
83 | { | |
84 | #if defined (HAVE_MKDTEMP) | |
85 | selftests::register_test ("mkdir_recursive", | |
86 | selftests::mkdir_recursive::test); | |
87 | #endif | |
88 | } | |
89 |