]> git.ipfire.org Git - thirdparty/libcgroup.git/commitdiff
tests/gunit: Extend the fuzzer to test cgroup_add_controller()
authorKamalesh Babulal <kamalesh.babulal@oracle.com>
Fri, 24 Feb 2023 05:35:07 +0000 (11:05 +0530)
committerTom Hromatka <tom.hromatka@oracle.com>
Fri, 24 Feb 2023 15:36:39 +0000 (08:36 -0700)
Add fuzzing to the cgroup_add_controller() API, by passing combination
of valid/NULL as arguments to the API.

[----------] 5 tests from APIArgsTest
[ RUN      ] APIArgsTest.API_cgroup_set_permissions
[       OK ] APIArgsTest.API_cgroup_set_permissions (0 ms)
[ RUN      ] APIArgsTest.API_cgroup_new_cgroup
[       OK ] APIArgsTest.API_cgroup_new_cgroup (0 ms)
[ RUN      ] APIArgsTest.API_cgroup_set_value_string
[       OK ] APIArgsTest.API_cgroup_set_value_string (0 ms)
[ RUN      ] APIArgsTest.API_cgroup_get_value_string
[       OK ] APIArgsTest.API_cgroup_get_value_string (0 ms)
[ RUN      ] APIArgsTest.API_cgroup_add_controller
[       OK ] APIArgsTest.API_cgroup_add_controller (0 ms)
[----------] 5 tests from APIArgsTest (0 ms total)

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

tests/gunit/017-API_fuzz_test.cpp

index 2252df34a7ce847cebfd0c5900dd3a1b18cc851f..a959a2fc7d49643c82b38ddca139e1df6d0cdc44 100644 (file)
@@ -170,3 +170,43 @@ TEST_F(APIArgsTest, API_cgroup_get_value_string)
 
        free(value);
 }
+
+/**
+ * Test arguments passed to add controller to a cgroup
+ * @param APIArgsTest googletest test case name
+ * @param API_cgroup_set_value_string test name
+ *
+ * This test will pass a combination of valid and NULL as
+ * arguments to cgroup_add_controller() and check if it
+ * handles it gracefully.
+ */
+TEST_F(APIArgsTest, API_cgroup_add_controller)
+{
+       const char * const cg_name = "FuzzerCgroup";
+       const char * const cg_ctrl = "cpu";
+       const char * const new_cg_ctrl = NULL;
+       struct cgroup_controller *cgc = NULL;
+       struct cgroup *cgroup = NULL;
+       int ret;
+
+       // case 1
+       // cgrp = NULL, name = NULL
+       cgc = cgroup_add_controller(cgroup, new_cg_ctrl);
+       ASSERT_EQ(cgroup, nullptr);
+
+       // case 2
+       // cgrp = NULL, name = valid
+       cgc = cgroup_add_controller(cgroup, cg_ctrl);
+       ASSERT_EQ(cgroup, nullptr);
+
+       cgroup = cgroup_new_cgroup(cg_name);
+       ASSERT_NE(cgroup, nullptr);
+
+       cgc = cgroup_add_controller(cgroup, cg_ctrl);
+       ASSERT_NE(cgroup, nullptr);
+
+       // case 3
+       // cgrp = valid, name = NULL
+       cgc = cgroup_add_controller(cgroup, new_cg_ctrl);
+       ASSERT_EQ(cgc, nullptr);
+}