]> git.ipfire.org Git - thirdparty/libcgroup.git/commitdiff
tests/gunit: Extend the fuzzer to test cgroup_set_value_int64()
authorKamalesh Babulal <kamalesh.babulal@oracle.com>
Thu, 2 Mar 2023 05:05:04 +0000 (10:35 +0530)
committerTom Hromatka <tom.hromatka@oracle.com>
Thu, 2 Mar 2023 15:51:00 +0000 (08:51 -0700)
Add fuzzing to the cgroup_set_value_int64() API, by passing combination
of valid/NULL as arguments to the API.

[----------] 8 tests from APIArgsTest
[ RUN      ] APIArgsTest.API_cgroup_set_permissions
[       OK ] APIArgsTest.API_cgroup_set_permissions (1 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)
[----------] 8 tests from APIArgsTest (1 ms total)

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

tests/gunit/017-API_fuzz_test.cpp

index 93e1232c3a36a3a8e593fecc2416b210f7b1e35d..8e2334f514b086f45d52e6657c8f20bcd4663e3e 100644 (file)
@@ -315,3 +315,58 @@ TEST_F(APIArgsTest, API_cgroup_get_uid_gid)
        ret = cgroup_get_uid_gid(cgroup, &tasks_uid, &tasks_gid, &control_uid, &control_gid);
        ASSERT_EQ(ret, 0);
 }
+
+/**
+ * Test arguments passed to set a controller's setting
+ * @param APIArgsTest googletest test case name
+ * @param API_cgroup_set_value_int64 test name
+ *
+ * This test will pass a combination of valid and NULL as
+ * arguments to cgroup_set_value_int64() and check if it
+ * handles it gracefully.
+ */
+TEST_F(APIArgsTest, API_cgroup_set_value_int64)
+{
+       const char * const cg_name = "FuzzerCgroup";
+       struct cgroup_controller * cgc = NULL;
+       const char * const cg_ctrl = "cpu";
+       struct cgroup *cgroup = NULL;
+       int64_t value = 1024;
+       char * name = NULL;
+       int ret;
+
+       // case 1
+       // cgc = NULL, name = NULL, value = valid
+       ret = cgroup_set_value_int64(cgc, name, value);
+       ASSERT_EQ(ret, 50011);
+
+       // case 2
+       // cgc = valid, name = NULL, value = valid
+       cgroup = cgroup_new_cgroup(cg_name);
+       ASSERT_NE(cgroup, nullptr);
+
+       cgc = cgroup_add_controller(cgroup, cg_ctrl);
+       ASSERT_NE(cgroup, nullptr);
+
+       // set cpu.shares, so that cgc->index > 0
+       ret = cgroup_set_value_int64(cgc, "cpu.shares", 1024);
+       ASSERT_EQ(ret, 0);
+
+       ret = cgroup_set_value_int64(cgc, name, value);
+       ASSERT_EQ(ret, 50011);
+
+       // case 3
+       // cgc = valid, name = valid, value = valid
+       name = strdup("cpu.shares");
+       ASSERT_NE(name, nullptr);
+
+       ret = cgroup_set_value_int64(cgc, name, value);
+       ASSERT_EQ(ret, 0);
+
+       // check if the value was set right
+       ret = cgroup_get_value_int64(cgc, name, &value);
+       ASSERT_EQ(ret, 0);
+       ASSERT_EQ(value, 1024);
+
+       free(name);
+}