xref: /netbsd-src/external/gpl3/gdb.old/dist/gdb/unittests/mkdir-recursive-selftests.c (revision d16b7486a53dcb8072b60ec6fcb4373a2d0c27b7)
1 /* Self tests for scoped_fd for GDB, the GNU debugger.
2 
3    Copyright (C) 2018-2020 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 "gdbsupport/filestuff.h"
23 #include "gdbsupport/selftest.h"
24 #include "gdbsupport/byte-vector.h"
25 #include "gdbsupport/pathstuff.h"
26 
27 namespace selftests {
28 namespace mkdir_recursive {
29 
30 /* Try to create DIR using mkdir_recursive and make sure it exists.  */
31 
32 static bool
33 create_dir_and_check (const char *dir)
34 {
35   ::mkdir_recursive (dir);
36 
37   struct stat st;
38   if (stat (dir, &st) != 0)
39     perror_with_name (("stat"));
40 
41   return (st.st_mode & S_IFDIR) != 0;
42 }
43 
44 /* Test mkdir_recursive.  */
45 
46 static void
47 test ()
48 {
49   std::string tmp = get_standard_temp_dir () + "/gdb-selftests";
50   gdb::char_vector base = make_temp_filename (tmp);
51 
52   if (mkdtemp (base.data ()) == NULL)
53     perror_with_name (("mkdtemp"));
54 
55   /* Try not to leave leftover directories.  */
56   struct cleanup_dirs {
57     cleanup_dirs (const char *base)
58       : m_base (base)
59     {}
60 
61     ~cleanup_dirs () {
62       rmdir (string_printf ("%s/a/b/c/d/e", m_base).c_str ());
63       rmdir (string_printf ("%s/a/b/c/d", m_base).c_str ());
64       rmdir (string_printf ("%s/a/b/c", m_base).c_str ());
65       rmdir (string_printf ("%s/a/b", m_base).c_str ());
66       rmdir (string_printf ("%s/a", m_base).c_str ());
67       rmdir (m_base);
68     }
69 
70   private:
71     const char *m_base;
72   } cleanup_dirs (base.data ());
73 
74   std::string dir = string_printf ("%s/a/b", base.data ());
75   SELF_CHECK (create_dir_and_check (dir.c_str ()));
76 
77   dir = string_printf ("%s/a/b/c//d/e/", base.data ());
78   SELF_CHECK (create_dir_and_check (dir.c_str ()));
79 }
80 
81 }
82 }
83 
84 void _initialize_mkdir_recursive_selftests ();
85 void
86 _initialize_mkdir_recursive_selftests ()
87 {
88   selftests::register_test ("mkdir_recursive",
89 			    selftests::mkdir_recursive::test);
90 }
91 
92