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

s
[----------] 4 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

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

tests/gunit/017-API_fuzz_test.cpp

index 246957973bf2aacd0401fca937385eaf043716ba..2252df34a7ce847cebfd0c5900dd3a1b18cc851f 100644 (file)
@@ -117,3 +117,56 @@ TEST_F(APIArgsTest, API_cgroup_set_value_string)
 
        free(value);
 }
+
+/**
+ * Test arguments passed to get a controller's setting
+ * @param APIArgsTest googletest test case name
+ * @param API_cgroup_get_value_string test name
+ *
+ * This test will pass a combination of valid and NULL as
+ * arguments to cgroup_get_value_string() and check if it
+ * handles it gracefully.
+ */
+TEST_F(APIArgsTest, API_cgroup_get_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_get_value_string(cgc, name, NULL);
+       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_get_value_string(cgc, name, NULL);
+       ASSERT_EQ(ret, 50011);
+
+       name = strdup("cgroup.shares");
+       ASSERT_NE(name, nullptr);
+
+       // case 3
+       // cgc = valid, name = valid, value = NULL
+       ret = cgroup_get_value_string(cgc, name, NULL);
+       ASSERT_EQ(ret, 50011);
+
+       free(name);
+       name = NULL;
+
+       // case 4
+       // cgc = valid, name = valid, value = NULL
+       ret = cgroup_get_value_string(cgc, name, &value);
+       ASSERT_EQ(ret, 50011);
+
+       free(value);
+}