From: Kamalesh Babulal Date: Fri, 24 Feb 2023 05:35:07 +0000 (+0530) Subject: tests/gunit: Extend the fuzzer to test cgroup_add_controller() X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=63be4494f31a06b6531bbfd3dceda63ddf4390a5;p=thirdparty%2Flibcgroup.git tests/gunit: Extend the fuzzer to test cgroup_add_controller() 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 Signed-off-by: Tom Hromatka (cherry picked from commit 4bec8844d71e3c7428dddcff29c7006dc378a5ec) --- diff --git a/tests/gunit/017-API_fuzz_test.cpp b/tests/gunit/017-API_fuzz_test.cpp index 2252df34..a959a2fc 100644 --- a/tests/gunit/017-API_fuzz_test.cpp +++ b/tests/gunit/017-API_fuzz_test.cpp @@ -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); +}