]> git.ipfire.org Git - thirdparty/libcgroup.git/commitdiff
gunit: Add unit tests for cgroup_chown_chmod_tasks()
authorTom Hromatka <tom.hromatka@oracle.com>
Wed, 15 Jul 2020 18:36:07 +0000 (12:36 -0600)
committerTom Hromatka <tom.hromatka@oracle.com>
Thu, 30 Jul 2020 15:24:47 +0000 (09:24 -0600)
This commit adds unit tests for cgroup_chown_chmod_tasks().

[----------] 1 test from ChownChmodTasksTest
[ RUN      ] ChownChmodTasksTest.SuccessfulChownChmod
[       OK ] ChownChmodTasksTest.SuccessfulChownChmod (0 ms)
[----------] 1 test from ChownChmodTasksTest (0 ms total)

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
gunit/010-cgroup_chown_chmod_tasks.cpp [new file with mode: 0644]
gunit/Makefile.am

diff --git a/gunit/010-cgroup_chown_chmod_tasks.cpp b/gunit/010-cgroup_chown_chmod_tasks.cpp
new file mode 100644 (file)
index 0000000..964d99d
--- /dev/null
@@ -0,0 +1,96 @@
+/**
+ * libcgroup googletest for cgroup_chown_chmod_tasks()
+ *
+ * Copyright (c) 2020 Oracle and/or its affiliates.
+ * Author: Tom Hromatka <tom.hromatka@oracle.com>
+ */
+
+/*
+ * This library is free software; you can redistribute it and/or modify it
+ * under the terms of version 2.1 of the GNU Lesser General Public License as
+ * published by the Free Software Foundation.
+ *
+ * This library is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library; if not, see <http://www.gnu.org/licenses>.
+ */
+
+#include <ftw.h>
+
+#include "gtest/gtest.h"
+
+#include "libcgroup-internal.h"
+
+static const char * const PARENT_DIR = "test010cgroup";
+static const mode_t MODE = S_IRWXU | S_IRWXG | S_IRWXO;
+
+class ChownChmodTasksTest : public ::testing::Test {
+       protected:
+
+       void SetUp() override {
+               char tasks_path[FILENAME_MAX];
+               int ret;
+               FILE *f;
+
+               ret = mkdir(PARENT_DIR, MODE);
+               ASSERT_EQ(ret, 0);
+
+               memset(tasks_path, 0, sizeof(tasks_path));
+               ret = snprintf(tasks_path, FILENAME_MAX - 1, "%s/tasks",
+                              PARENT_DIR);
+               ASSERT_GT(ret, 0);
+
+               f = fopen(tasks_path, "w");
+               fclose(f);
+       }
+
+       /*
+        * https://stackoverflow.com/questions/5467725/how-to-delete-a-directory-and-its-contents-in-posix-c
+        */
+       static int unlink_cb(const char *fpath, const struct stat *sb, int typeflag,
+                     struct FTW *ftwbuf)
+       {
+               return remove(fpath);
+       }
+
+       int rmrf(const char * const path)
+       {
+               return nftw(path, unlink_cb, 64, FTW_DEPTH | FTW_PHYS);
+       }
+
+       void TearDown() override {
+               int ret;
+
+               ret = rmrf(PARENT_DIR);
+               ASSERT_EQ(ret, 0);
+       }
+};
+
+TEST_F(ChownChmodTasksTest, SuccessfulChownChmod)
+{
+       mode_t mode = S_IRUSR | S_IWUSR | S_IWGRP | S_IROTH;
+       char tasks_path[FILENAME_MAX];
+       uid_t uid = getuid();
+       gid_t gid = getgid();
+       struct stat statbuf;
+       int ret;
+
+       ret = cgroup_chown_chmod_tasks(PARENT_DIR, uid, gid, mode);
+       ASSERT_EQ(ret, 0);
+
+       memset(tasks_path, 0, sizeof(tasks_path));
+       ret = snprintf(tasks_path, FILENAME_MAX - 1, "%s/tasks",
+                      PARENT_DIR);
+       ASSERT_GT(ret, 0);
+
+       ret = stat(tasks_path, &statbuf);
+       ASSERT_EQ(ret, 0);
+
+       ASSERT_EQ(statbuf.st_uid, uid);
+       ASSERT_EQ(statbuf.st_gid, gid);
+       ASSERT_EQ(statbuf.st_mode & 0777, mode);
+}
index fb4e1d1705de50e790c13be300f80e5d0aa60a3b..43daba32579b9edfaaf8e6c4f8f36c5afb4d8fa2 100644 (file)
@@ -46,6 +46,7 @@ gtest_SOURCES = gtest.cpp \
                006-cgroup_get_cgroup.cpp \
                007-cgroup_process_v1_mount.cpp \
                008-cgroup_process_v2_mount.cpp \
-               009-cgroup_set_values_recursive.cpp
+               009-cgroup_set_values_recursive.cpp \
+               010-cgroup_chown_chmod_tasks.cpp
 gtest_LDFLAGS = -L$(top_builddir)/googletest/googletest -l:libgtest.so \
                -rpath $(abs_top_builddir)/googletest/googletest