]> git.ipfire.org Git - thirdparty/libcgroup.git/commitdiff
tests/gunit: Extend the fuzzer to test cgroup_set_value_string()
authorKamalesh Babulal <kamalesh.babulal@oracle.com>
Tue, 21 Feb 2023 13:13:25 +0000 (13:13 +0000)
committerTom Hromatka <tom.hromatka@oracle.com>
Wed, 22 Feb 2023 16:43:53 +0000 (09:43 -0700)
Add fuzzing to the cgroup_set_value_string() API, by passing combination
of valid/NULL as arguments to the API.

[----------] 3 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 (19 ms)
[----------] 3 tests from APIArgsTest (19 ms total)

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

tests/gunit/017-API_fuzz_test.cpp

index 27afcb8449304032161e4bc0c78cf616882bf807..246957973bf2aacd0401fca937385eaf043716ba 100644 (file)
@@ -61,3 +61,59 @@ TEST_F(APIArgsTest, API_cgroup_new_cgroup)
        cgroup = cgroup_new_cgroup(name);
        ASSERT_EQ(cgroup, nullptr);
 }
+
+/**
+ * Test arguments passed to set a controller's setting
+ * @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_set_value_string() and check if it
+ * handles it gracefully.
+ */
+TEST_F(APIArgsTest, API_cgroup_set_value_string)
+{
+       const char * const cg_name = "FuzzerCgroup";
+       struct cgroup_controller * cgc = NULL;
+       const char * const cg_ctrl = "cpu";
+       char * name = NULL, *value = NULL;
+       struct cgroup *cgroup = NULL;
+       int ret;
+
+       // case 1
+       // cgc = NULL, name = NULL, value = NULL
+       ret = cgroup_set_value_string(cgc, name, value);
+       ASSERT_EQ(ret, 50011);
+
+       cgroup = cgroup_new_cgroup(cg_name);
+       ASSERT_NE(cgroup, nullptr);
+
+       cgc = cgroup_add_controller(cgroup, cg_ctrl);
+       ASSERT_NE(cgroup, nullptr);
+
+       // case 2
+       // cgc = valid, name = NULL, value = NULL
+       ret = cgroup_set_value_string(cgc, name, value);
+       ASSERT_EQ(ret, 50011);
+
+       name = strdup("cgroup.shares");
+       ASSERT_NE(name, nullptr);
+
+       // case 3
+       // cgc = valid, name = valid, value = NULL
+       ret = cgroup_set_value_string(cgc, name, value);
+       ASSERT_EQ(ret, 50011);
+
+       free(name);
+       name = NULL;
+
+       value = strdup("1024");
+       ASSERT_NE(value, nullptr);
+
+       // case 4
+       // cgc = valid, name = NULL, value = valid
+       ret = cgroup_set_value_string(cgc, name, value);
+       ASSERT_EQ(ret, 50011);
+
+       free(value);
+}