Blame SOURCES/0043-test-add-test-case-for-recursive-chown-ing.patch

a3e2b5
From b53f89d56a5b7528735ddf335f8b47ab3e1a947a Mon Sep 17 00:00:00 2001
a3e2b5
From: Lennart Poettering <lennart@poettering.net>
a3e2b5
Date: Fri, 19 Oct 2018 11:31:37 +0200
a3e2b5
Subject: [PATCH] test: add test case for recursive chown()ing
a3e2b5
a3e2b5
[msekleta: I removed call to log_test_skipped() and replaced it with older construct log_info() + return EXIT_TEST_SKIP]
a3e2b5
a3e2b5
(cherry-picked from commit cb9e44db36caefcbb8ee7a12e14217305ed69ff2)
a3e2b5
a3e2b5
Related: #1643368
a3e2b5
---
a3e2b5
 src/test/meson.build      |   5 ++
a3e2b5
 src/test/test-chown-rec.c | 162 ++++++++++++++++++++++++++++++++++++++
a3e2b5
 2 files changed, 167 insertions(+)
a3e2b5
 create mode 100644 src/test/test-chown-rec.c
a3e2b5
a3e2b5
diff --git a/src/test/meson.build b/src/test/meson.build
a3e2b5
index 7da7e3a22c..b982251b1f 100644
a3e2b5
--- a/src/test/meson.build
a3e2b5
+++ b/src/test/meson.build
a3e2b5
@@ -60,6 +60,11 @@ tests += [
a3e2b5
           libmount,
a3e2b5
           libblkid]],
a3e2b5
 
a3e2b5
+        [['src/test/test-chown-rec.c'],
a3e2b5
+         [libcore,
a3e2b5
+          libshared],
a3e2b5
+         []],
a3e2b5
+
a3e2b5
         [['src/test/test-job-type.c'],
a3e2b5
          [libcore,
a3e2b5
           libshared],
a3e2b5
diff --git a/src/test/test-chown-rec.c b/src/test/test-chown-rec.c
a3e2b5
new file mode 100644
a3e2b5
index 0000000000..f16d4d4ba2
a3e2b5
--- /dev/null
a3e2b5
+++ b/src/test/test-chown-rec.c
a3e2b5
@@ -0,0 +1,162 @@
a3e2b5
+/* SPDX-License-Identifier: LGPL-2.1+ */
a3e2b5
+
a3e2b5
+#include <sys/xattr.h>
a3e2b5
+
a3e2b5
+#include "alloc-util.h"
a3e2b5
+#include "chown-recursive.h"
a3e2b5
+#include "fileio.h"
a3e2b5
+#include "log.h"
a3e2b5
+#include "rm-rf.h"
a3e2b5
+#include "string-util.h"
a3e2b5
+#include "tests.h"
a3e2b5
+
a3e2b5
+static const uint8_t acl[] = {
a3e2b5
+        0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x07, 0x00,
a3e2b5
+        0xff, 0xff, 0xff, 0xff, 0x02, 0x00, 0x07, 0x00,
a3e2b5
+        0x02, 0x00, 0x00, 0x00, 0x04, 0x00, 0x07, 0x00,
a3e2b5
+        0xff, 0xff, 0xff, 0xff, 0x10, 0x00, 0x07, 0x00,
a3e2b5
+        0xff, 0xff, 0xff, 0xff, 0x20, 0x00, 0x05, 0x00,
a3e2b5
+        0xff, 0xff, 0xff, 0xff,
a3e2b5
+};
a3e2b5
+
a3e2b5
+static const uint8_t default_acl[] = {
a3e2b5
+        0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x07, 0x00,
a3e2b5
+        0xff, 0xff, 0xff, 0xff, 0x04, 0x00, 0x07, 0x00,
a3e2b5
+        0xff, 0xff, 0xff, 0xff, 0x08, 0x00, 0x07, 0x00,
a3e2b5
+        0x04, 0x00, 0x00, 0x00, 0x10, 0x00, 0x07, 0x00,
a3e2b5
+        0xff, 0xff, 0xff, 0xff, 0x20, 0x00, 0x05, 0x00,
a3e2b5
+        0xff, 0xff, 0xff, 0xff,
a3e2b5
+};
a3e2b5
+
a3e2b5
+static bool has_xattr(const char *p) {
a3e2b5
+        char buffer[sizeof(acl) * 4];
a3e2b5
+
a3e2b5
+        if (lgetxattr(p, "system.posix_acl_access", buffer, sizeof(buffer)) < 0) {
a3e2b5
+                if (IN_SET(errno, EOPNOTSUPP, ENOTTY, ENODATA, ENOSYS))
a3e2b5
+                        return false;
a3e2b5
+        }
a3e2b5
+
a3e2b5
+        return true;
a3e2b5
+}
a3e2b5
+
a3e2b5
+static void test_chown_recursive(void) {
a3e2b5
+        _cleanup_(rm_rf_physical_and_freep) char *t = NULL;
a3e2b5
+        struct stat st;
a3e2b5
+        const char *p;
a3e2b5
+
a3e2b5
+        umask(022);
a3e2b5
+        assert_se(mkdtemp_malloc(NULL, &t) >= 0);
a3e2b5
+
a3e2b5
+        p = strjoina(t, "/dir");
a3e2b5
+        assert_se(mkdir(p, 0777) >= 0);
a3e2b5
+        assert_se(lstat(p, &st) >= 0);
a3e2b5
+        assert_se(S_ISDIR(st.st_mode));
a3e2b5
+        assert_se((st.st_mode & 07777) == 0755);
a3e2b5
+        assert_se(st.st_uid == 0);
a3e2b5
+        assert_se(st.st_gid == 0);
a3e2b5
+        assert_se(!has_xattr(p));
a3e2b5
+
a3e2b5
+        p = strjoina(t, "/dir/symlink");
a3e2b5
+        assert_se(symlink("../../", p) >= 0);
a3e2b5
+        assert_se(lstat(p, &st) >= 0);
a3e2b5
+        assert_se(S_ISLNK(st.st_mode));
a3e2b5
+        assert_se((st.st_mode & 07777) == 0777);
a3e2b5
+        assert_se(st.st_uid == 0);
a3e2b5
+        assert_se(st.st_gid == 0);
a3e2b5
+        assert_se(!has_xattr(p));
a3e2b5
+
a3e2b5
+        p = strjoina(t, "/dir/reg");
a3e2b5
+        assert_se(mknod(p, S_IFREG|0777, 0) >= 0);
a3e2b5
+        assert_se(lstat(p, &st) >= 0);
a3e2b5
+        assert_se(S_ISREG(st.st_mode));
a3e2b5
+        assert_se((st.st_mode & 07777) == 0755);
a3e2b5
+        assert_se(st.st_uid == 0);
a3e2b5
+        assert_se(st.st_gid == 0);
a3e2b5
+        assert_se(!has_xattr(p));
a3e2b5
+
a3e2b5
+        p = strjoina(t, "/dir/sock");
a3e2b5
+        assert_se(mknod(p, S_IFSOCK|0777, 0) >= 0);
a3e2b5
+        assert_se(lstat(p, &st) >= 0);
a3e2b5
+        assert_se(S_ISSOCK(st.st_mode));
a3e2b5
+        assert_se((st.st_mode & 07777) == 0755);
a3e2b5
+        assert_se(st.st_uid == 0);
a3e2b5
+        assert_se(st.st_gid == 0);
a3e2b5
+        assert_se(!has_xattr(p));
a3e2b5
+
a3e2b5
+        p = strjoina(t, "/dir/fifo");
a3e2b5
+        assert_se(mknod(p, S_IFIFO|0777, 0) >= 0);
a3e2b5
+        assert_se(lstat(p, &st) >= 0);
a3e2b5
+        assert_se(S_ISFIFO(st.st_mode));
a3e2b5
+        assert_se((st.st_mode & 07777) == 0755);
a3e2b5
+        assert_se(st.st_uid == 0);
a3e2b5
+        assert_se(st.st_gid == 0);
a3e2b5
+        assert_se(!has_xattr(p));
a3e2b5
+
a3e2b5
+        /* We now apply an xattr to the dir, and check it again */
a3e2b5
+        p = strjoina(t, "/dir");
a3e2b5
+        assert_se(setxattr(p, "system.posix_acl_access", acl, sizeof(acl), 0) >= 0);
a3e2b5
+        assert_se(setxattr(p, "system.posix_acl_default", default_acl, sizeof(default_acl), 0) >= 0);
a3e2b5
+        assert_se(lstat(p, &st) >= 0);
a3e2b5
+        assert_se(S_ISDIR(st.st_mode));
a3e2b5
+        assert_se((st.st_mode & 07777) == 0775); /* acl change changed the mode too */
a3e2b5
+        assert_se(st.st_uid == 0);
a3e2b5
+        assert_se(st.st_gid == 0);
a3e2b5
+        assert_se(has_xattr(p));
a3e2b5
+
a3e2b5
+        assert_se(path_chown_recursive(t, 1, 2) >= 0);
a3e2b5
+
a3e2b5
+        p = strjoina(t, "/dir");
a3e2b5
+        assert_se(lstat(p, &st) >= 0);
a3e2b5
+        assert_se(S_ISDIR(st.st_mode));
a3e2b5
+        assert_se((st.st_mode & 07777) == 0775);
a3e2b5
+        assert_se(st.st_uid == 1);
a3e2b5
+        assert_se(st.st_gid == 2);
a3e2b5
+        assert_se(!has_xattr(p));
a3e2b5
+
a3e2b5
+        p = strjoina(t, "/dir/symlink");
a3e2b5
+        assert_se(lstat(p, &st) >= 0);
a3e2b5
+        assert_se(S_ISLNK(st.st_mode));
a3e2b5
+        assert_se((st.st_mode & 07777) == 0777);
a3e2b5
+        assert_se(st.st_uid == 1);
a3e2b5
+        assert_se(st.st_gid == 2);
a3e2b5
+        assert_se(!has_xattr(p));
a3e2b5
+
a3e2b5
+        p = strjoina(t, "/dir/reg");
a3e2b5
+        assert_se(lstat(p, &st) >= 0);
a3e2b5
+        assert_se(S_ISREG(st.st_mode));
a3e2b5
+        assert_se((st.st_mode & 07777) == 0755);
a3e2b5
+        assert_se(st.st_uid == 1);
a3e2b5
+        assert_se(st.st_gid == 2);
a3e2b5
+        assert_se(!has_xattr(p));
a3e2b5
+
a3e2b5
+        p = strjoina(t, "/dir/sock");
a3e2b5
+        assert_se(lstat(p, &st) >= 0);
a3e2b5
+        assert_se(S_ISSOCK(st.st_mode));
a3e2b5
+        assert_se((st.st_mode & 07777) == 0755);
a3e2b5
+        assert_se(st.st_uid == 1);
a3e2b5
+        assert_se(st.st_gid == 2);
a3e2b5
+        assert_se(!has_xattr(p));
a3e2b5
+
a3e2b5
+        p = strjoina(t, "/dir/fifo");
a3e2b5
+        assert_se(lstat(p, &st) >= 0);
a3e2b5
+        assert_se(S_ISFIFO(st.st_mode));
a3e2b5
+        assert_se((st.st_mode & 07777) == 0755);
a3e2b5
+        assert_se(st.st_uid == 1);
a3e2b5
+        assert_se(st.st_gid == 2);
a3e2b5
+        assert_se(!has_xattr(p));
a3e2b5
+}
a3e2b5
+
a3e2b5
+int main(int argc, char *argv[]) {
a3e2b5
+        log_set_max_level(LOG_DEBUG);
a3e2b5
+        log_parse_environment();
a3e2b5
+        log_open();
a3e2b5
+
a3e2b5
+        if (geteuid() != 0) {
a3e2b5
+                log_info("not running as root");
a3e2b5
+                return EXIT_TEST_SKIP;
a3e2b5
+        }
a3e2b5
+
a3e2b5
+        test_chown_recursive();
a3e2b5
+
a3e2b5
+        return EXIT_SUCCESS;
a3e2b5
+}