summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPhilip Withnall <philip@tecnocode.co.uk>2023-05-11 11:12:10 +0000
committerPhilip Withnall <philip@tecnocode.co.uk>2023-05-11 11:12:10 +0000
commitfde068300efe442822fbbd56dc8ecc1caf4b83a7 (patch)
tree92d262ff02629b86d691328bc09e21801da4a7e1
parenta14803d06d89f5e73bae8acf5d78b365ecfad151 (diff)
parente2973c9a41760df9bbf1823c748e39e48ee8a163 (diff)
downloadglib-fde068300efe442822fbbd56dc8ecc1caf4b83a7.tar.gz
Merge branch 'main' into 'main'
glib/tests/fileutils:fix asserts when running as root See merge request GNOME/glib!3421
-rw-r--r--glib/tests/fileutils.c93
1 files changed, 53 insertions, 40 deletions
diff --git a/glib/tests/fileutils.c b/glib/tests/fileutils.c
index b9149720d..ab5b574cc 100644
--- a/glib/tests/fileutils.c
+++ b/glib/tests/fileutils.c
@@ -723,46 +723,6 @@ test_mkdir_with_parents_1 (const gchar *base)
g_free (p0);
}
-static void
-test_mkdir_with_parents (void)
-{
- gchar *cwd, *new_path;
- if (g_test_verbose())
- g_printerr ("checking g_mkdir_with_parents() in subdir ./hum/");
- test_mkdir_with_parents_1 ("hum");
- g_remove ("hum");
- if (g_test_verbose())
- g_printerr ("checking g_mkdir_with_parents() in subdir ./hii///haa/hee/");
- test_mkdir_with_parents_1 ("./hii///haa/hee///");
- g_remove ("hii/haa/hee");
- g_remove ("hii/haa");
- g_remove ("hii");
- cwd = g_get_current_dir ();
- if (g_test_verbose())
- g_printerr ("checking g_mkdir_with_parents() in cwd: %s", cwd);
- test_mkdir_with_parents_1 (cwd);
-
- new_path = g_build_filename (cwd, "new", NULL);
- g_assert_cmpint (g_mkdir_with_parents (new_path, 0), ==, 0);
- g_assert_cmpint (g_rmdir (new_path), ==, 0);
- g_free (new_path);
- g_free (cwd);
-
- g_assert_cmpint (g_mkdir_with_parents ("./test", 0), ==, 0);
- g_assert_cmpint (g_mkdir_with_parents ("./test", 0), ==, 0);
- g_remove ("./test");
-
-#ifndef G_OS_WIN32
- g_assert_cmpint (g_mkdir_with_parents ("/usr/b/c", 0), ==, -1);
- /* EPERM or EROFS may be returned if the filesystem as a whole is read-only */
- if (errno != EPERM && errno != EROFS)
- g_assert_cmpint (errno, ==, EACCES);
-#endif
-
- g_assert_cmpint (g_mkdir_with_parents (NULL, 0), ==, -1);
- g_assert_cmpint (errno, ==, EINVAL);
-}
-
/*
* check_cap_dac_override:
* @tmpdir: (nullable): A temporary directory in which we can create
@@ -848,6 +808,59 @@ check_cap_dac_override (const char *tmpdir)
#endif
}
+static void
+test_mkdir_with_parents (void)
+{
+ gchar *cwd, *new_path;
+#ifndef G_OS_WIN32
+ gboolean can_override_dac = check_cap_dac_override (NULL);
+#endif
+ if (g_test_verbose())
+ g_printerr ("checking g_mkdir_with_parents() in subdir ./hum/");
+ test_mkdir_with_parents_1 ("hum");
+ g_remove ("hum");
+ if (g_test_verbose())
+ g_printerr ("checking g_mkdir_with_parents() in subdir ./hii///haa/hee/");
+ test_mkdir_with_parents_1 ("./hii///haa/hee///");
+ g_remove ("hii/haa/hee");
+ g_remove ("hii/haa");
+ g_remove ("hii");
+ cwd = g_get_current_dir ();
+ if (g_test_verbose())
+ g_printerr ("checking g_mkdir_with_parents() in cwd: %s", cwd);
+ test_mkdir_with_parents_1 (cwd);
+
+ new_path = g_build_filename (cwd, "new", NULL);
+ g_assert_cmpint (g_mkdir_with_parents (new_path, 0), ==, 0);
+ g_assert_cmpint (g_rmdir (new_path), ==, 0);
+ g_free (new_path);
+ g_free (cwd);
+
+ g_assert_cmpint (g_mkdir_with_parents ("./test", 0), ==, 0);
+ g_assert_cmpint (g_mkdir_with_parents ("./test", 0), ==, 0);
+ g_remove ("./test");
+
+#ifndef G_OS_WIN32
+ if (can_override_dac)
+ {
+ g_assert_cmpint (g_mkdir_with_parents ("/usr/b/c", 0), ==, 0);
+ g_remove ("/usr/b/c");
+ g_remove ("/usr/b");
+ }
+ else
+ {
+ g_assert_cmpint (g_mkdir_with_parents ("/usr/b/c", 0), ==, -1);
+ /* EPERM or EROFS may be returned if the filesystem as a whole is read-only */
+ if (errno != EPERM && errno != EROFS)
+ g_assert_cmpint (errno, ==, EACCES);
+ }
+
+#endif
+
+ g_assert_cmpint (g_mkdir_with_parents (NULL, 0), ==, -1);
+ g_assert_cmpint (errno, ==, EINVAL);
+}
+
/* Reproducer for https://gitlab.gnome.org/GNOME/glib/issues/1852 */
static void
test_mkdir_with_parents_permission (void)