From 1394f24871c2163abd5d124fa0cbae9f4fa09e45 Mon Sep 17 00:00:00 2001 From: Kamalesh Babulal Date: Tue, 21 Feb 2023 13:10:05 +0000 Subject: [PATCH] tests/gunit: Extend the fuzzer to test cgroup_get_value_string() 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 Signed-off-by: Tom Hromatka (cherry picked from commit 689d7fb7105727675ba6487530903b1f7e6ef50a) --- tests/gunit/017-API_fuzz_test.cpp | 53 +++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/tests/gunit/017-API_fuzz_test.cpp b/tests/gunit/017-API_fuzz_test.cpp index 24695797..2252df34 100644 --- a/tests/gunit/017-API_fuzz_test.cpp +++ b/tests/gunit/017-API_fuzz_test.cpp @@ -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); +} -- 2.47.2