]> git.ipfire.org Git - thirdparty/libcgroup.git/commitdiff
tests/gunit: Extend the fuzzer to test cgroup_get_value_bool()
authorKamalesh Babulal <kamalesh.babulal@oracle.com>
Wed, 8 Mar 2023 15:26:38 +0000 (20:56 +0530)
committerTom Hromatka <tom.hromatka@oracle.com>
Thu, 9 Mar 2023 17:15:33 +0000 (10:15 -0700)
Add fuzzing to the cgroup_get_value_bool() API, by passing combination
of valid/NULL as arguments to the API.

[----------] 13 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)
[ RUN      ] APIArgsTest.API_cgroup_add_value_string
[       OK ] APIArgsTest.API_cgroup_add_value_string (0 ms)
[ RUN      ] APIArgsTest.API_cgroup_get_uid_gid
[       OK ] APIArgsTest.API_cgroup_get_uid_gid (0 ms)
[ RUN      ] APIArgsTest.API_cgroup_set_value_int64
[       OK ] APIArgsTest.API_cgroup_set_value_int64 (0 ms)
[ RUN      ] APIArgsTest.API_cgroup_get_value_int64
[       OK ] APIArgsTest.API_cgroup_get_value_int64 (0 ms)
[ RUN      ] APIArgsTest.API_cgroup_set_value_uint64
[       OK ] APIArgsTest.API_cgroup_set_value_uint64 (0 ms)
[ RUN      ] APIArgsTest.API_cgroup_get_value_uint64
[       OK ] APIArgsTest.API_cgroup_get_value_uint64 (0 ms)
[ RUN      ] APIArgsTest.API_cgroup_set_value_bool
[       OK ] APIArgsTest.API_cgroup_set_value_bool (0 ms)
[ RUN      ] APIArgsTest.API_cgroup_get_value_bool
[       OK ] APIArgsTest.API_cgroup_get_value_bool (0 ms)
[----------] 13 tests from APIArgsTest (1 ms total)

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

tests/gunit/017-API_fuzz_test.cpp

index 71cb130d52f187a0e078f056ae66030286978e69..969cfdb63248b8a1d2a2fcfa9e1fe92ffef8fbaf 100644 (file)
@@ -592,3 +592,59 @@ TEST_F(APIArgsTest, API_cgroup_set_value_bool)
 
        free(name);
 }
+
+/**
+ * Test arguments passed to get a controller's setting
+ * @param APIArgsTest googletest test case name
+ * @param API_cgroup_get_value_bool test name
+ *
+ * This test will pass a combination of valid and NULL as
+ * arguments to cgroup_get_value_bool() and check if it
+ * handles it gracefully.
+ */
+TEST_F(APIArgsTest, API_cgroup_get_value_bool)
+{
+       const char * const cg_name = "FuzzerCgroup";
+       struct cgroup_controller * cgc = NULL;
+       const char * const cg_ctrl = "cpuset";
+       struct cgroup *cgroup = NULL;
+       char * name = NULL;
+       bool value;
+       int ret;
+
+       // case 1
+       // cgc = NULL, name = NULL, value = NULL
+       ret = cgroup_get_value_bool(cgc, name, NULL);
+       ASSERT_EQ(ret, 50011);
+
+       // case 2
+       // cgc = valid, name = NULL, value = NULL
+       cgroup = cgroup_new_cgroup(cg_name);
+       ASSERT_NE(cgroup, nullptr);
+
+       cgc = cgroup_add_controller(cgroup, cg_ctrl);
+       ASSERT_NE(cgroup, nullptr);
+
+       // set cpuset.cpu_exclusive, so that cgc->index > 0
+       ret = cgroup_set_value_bool(cgc, "cpuset.cpu_exclusive", 0);
+       ASSERT_EQ(ret, 0);
+
+       ret = cgroup_get_value_bool(cgc, name, NULL);
+       ASSERT_EQ(ret, 50011);
+
+       // case 3
+       // cgc = valid, name = valid, value = NULL
+       name = strdup("cpuset.cpu_exclusive");
+       ASSERT_NE(name, nullptr);
+
+       ret = cgroup_get_value_bool(cgc, name, NULL);
+       ASSERT_EQ(ret, 50011);
+
+       // case 4
+       // cgc = valid, name = NULL, value = valid
+       free(name);
+       name = NULL;
+
+       ret = cgroup_get_value_bool(cgc, name, &value);
+       ASSERT_EQ(ret, 50011);
+}