]> git.ipfire.org Git - thirdparty/libcgroup.git/commitdiff
tests/gunit: Add a simple fuzzer
authorKamalesh Babulal <kamalesh.babulal@oracle.com>
Tue, 31 Jan 2023 18:30:56 +0000 (11:30 -0700)
committerTom Hromatka <tom.hromatka@oracle.com>
Tue, 31 Jan 2023 18:31:03 +0000 (11:31 -0700)
Add a simple fuzzing test case that would exercise API's unlikely code
paths by passing NULL/negative values as arguments.

Output:
--------
[...]

[----------] 1 test from APIArgsTest
[ RUN      ] APIArgsTest.API_cgroup_set_permissions
[       OK ] APIArgsTest.API_cgroup_set_permissions (0 ms)
[----------] 1 test from APIArgsTest (0 ms total)

to begin with, it exercises cgroup_set_permissions() by passing a NULL
pointer in place of struct cgroup. This test case will be extended to
cover almost all APIs.

Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
(cherry picked from commit dac22679ee3e888be5dedc50e22e0d001bfceb4a)

tests/gunit/017-API_fuzz_test.cpp [new file with mode: 0644]
tests/gunit/Makefile.am

diff --git a/tests/gunit/017-API_fuzz_test.cpp b/tests/gunit/017-API_fuzz_test.cpp
new file mode 100644 (file)
index 0000000..2cdd72b
--- /dev/null
@@ -0,0 +1,46 @@
+/* SPDX-License-Identifier: LGPL-2.1-only */
+/**
+ * libcgroup googletest for fuzz testing APIs with negative values.
+ *
+ * Copyright (c) 2023 Oracle and/or its affiliates.  All rights reserved.
+ * Author: Kamalesh Babulal <kamalesh.babulal@oracle.com>
+ */
+
+#include <sys/stat.h>
+
+#include "gtest/gtest.h"
+
+#include "libcgroup-internal.h"
+
+class APIArgsTest: public :: testing:: Test {
+       protected:
+
+       void SetUp() override {
+               /* Stub */
+       }
+};
+
+/**
+ * Pass NULL cgroup for setting permissions
+ * @param APIArgsTest googletest test case name
+ * @param API_cgroup_set_permissions test name
+ *
+ * This test will pass NULL cgroup to the cgroup_set_permissions()
+ * and check it handles it gracefully.
+ */
+TEST_F(APIArgsTest, API_cgroup_set_permissions)
+{
+       mode_t dir_mode, ctrl_mode, task_mode;
+       struct cgroup * cgroup = NULL;
+
+       dir_mode = (S_IRWXU | S_IXGRP | S_IXOTH);
+       ctrl_mode = (S_IRUSR | S_IWUSR | S_IRGRP);
+       task_mode = (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
+
+       testing::internal::CaptureStdout();
+
+       cgroup_set_permissions(cgroup, dir_mode, ctrl_mode, task_mode);
+
+       std::string result = testing::internal::GetCapturedStdout();
+       ASSERT_EQ(result, "Error: Cgroup, operation not allowed\n");
+}
index ba91039c192bc450a03268fa961fb86e60da4326..353567b47ea1e3aecc62d3c5f92447b9782235e3 100644 (file)
@@ -42,7 +42,9 @@ gtest_SOURCES = gtest.cpp \
                013-cgroup_build_tasks_procs_path.cpp \
                014-cgroupv2_get_subtree_control.cpp \
                015-cgroupv2_controller_enabled.cpp \
-               016-cgset_parse_r_flag.cpp
+               016-cgset_parse_r_flag.cpp \
+               017-API_fuzz_test.cpp
+
 gtest_LDFLAGS = -L$(top_srcdir)/googletest/googletest -l:libgtest.so \
                -rpath $(abs_top_srcdir)/googletest/googletest