Debian Codegraph @

subprojects/libglnx/tests/test-libglnx-shutil.c

raw
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
2 *
3 * Copyright © 2017-2019 Endless OS Foundation LLC
4 * Copyright © 2024 Collabora Ltd.
5 * SPDX-License-Identifier: LGPL-2.0-or-later
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library 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 GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
21 */
22
23 #include "libglnx-config.h"
24 #include "libglnx.h"
25 #include <glib.h>
26 #include <stdlib.h>
27 #include <gio/gio.h>
28 #include <err.h>
29 #include <string.h>
30
31 #include "libglnx-testlib.h"
32
33 static void
34 test_mkdir_p_parent_unsuitable (void)
35 {
36 _GLNX_TEST_SCOPED_TEMP_DIR;
37 _GLNX_TEST_DECLARE_ERROR(local_error, error);
38 glnx_autofd int dfd = -1;
39
40 if (!glnx_ensure_dir (AT_FDCWD, "test", 0755, error))
41 return;
42 if (!glnx_opendirat (AT_FDCWD, "test", FALSE, &dfd, error))
43 return;
44
45 if (!glnx_file_replace_contents_at (dfd, "file",
46 (const guint8 *) "", 0,
47 GLNX_FILE_REPLACE_NODATASYNC,
48 NULL, error))
49 return;
50
51 if (symlinkat ("nosuchtarget", dfd, "link") < 0)
52 {
53 glnx_throw_errno_prefix (error, "symlinkat");
54 return;
55 }
56
57 glnx_shutil_mkdir_p_at (dfd, "file/baz", 0755, NULL, error);
58 g_test_message ("mkdir %s -> %s", "file/baz",
59 local_error ? local_error->message : "success");
60 g_assert_error (local_error, G_IO_ERROR, G_IO_ERROR_NOT_DIRECTORY);
61 g_clear_error (&local_error);
62
63 glnx_shutil_mkdir_p_at (dfd, "link/baz", 0755, NULL, error);
64 g_test_message ("mkdir %s -> %s", "link/baz",
65 local_error ? local_error->message : "success");
66 g_assert_error (local_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND);
67 g_clear_error (&local_error);
68 }
69
70 static void
71 test_mkdir_p_enoent (void)
72 {
73 _GLNX_TEST_SCOPED_TEMP_DIR;
74 _GLNX_TEST_DECLARE_ERROR(local_error, error);
75 glnx_autofd int dfd = -1;
76
77 if (!glnx_ensure_dir (AT_FDCWD, "test", 0755, error))
78 return;
79 if (!glnx_opendirat (AT_FDCWD, "test", FALSE, &dfd, error))
80 return;
81 if (rmdir ("test") < 0)
82 return (void) glnx_throw_errno_prefix (error, "rmdir(%s)", "test");
83
84 /* This should fail with ENOENT. */
85 glnx_shutil_mkdir_p_at (dfd, "blah/baz", 0755, NULL, error);
86 g_assert_error (local_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND);
87 g_clear_error (&local_error);
88 }
89
90 int
91 main (int argc,
92 char **argv)
93 {
94 int ret;
95
96 g_test_init (&argc, &argv, NULL);
97
98 g_test_add_func ("/mkdir-p/enoent", test_mkdir_p_enoent);
99 g_test_add_func ("/mkdir-p/parent-unsuitable", test_mkdir_p_parent_unsuitable);
100
101 ret = g_test_run();
102
103 return ret;
104 }