From: Kamalesh Babulal Date: Tue, 21 Feb 2023 13:13:25 +0000 (+0000) Subject: tests/gunit: Extend the fuzzer to test cgroup_set_value_string() X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=39691997889d961554a67a09d5bc620f51667cd2;p=thirdparty%2Flibcgroup.git tests/gunit: Extend the fuzzer to test cgroup_set_value_string() 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 Signed-off-by: Tom Hromatka (cherry picked from commit c259bb79641582f08a5ddd3e1f85aa7a018ba716) --- diff --git a/tests/gunit/017-API_fuzz_test.cpp b/tests/gunit/017-API_fuzz_test.cpp index 27afcb84..24695797 100644 --- a/tests/gunit/017-API_fuzz_test.cpp +++ b/tests/gunit/017-API_fuzz_test.cpp @@ -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); +}