From: Kamalesh Babulal Date: Sat, 4 Mar 2023 06:50:43 +0000 (+0530) Subject: tests/gunit: Extend the fuzzer to test cgroup_get_value_uint64() X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0d906b7662c17e48fa812acfcc5f71eb58874e35;p=thirdparty%2Flibcgroup.git tests/gunit: Extend the fuzzer to test cgroup_get_value_uint64() Add fuzzing to the cgroup_get_value_uint64() API, by passing combination of valid/NULL as arguments to the API. [----------] 11 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) [ RUN ] APIArgsTest.API_cgroup_add_value_string [ OK ] APIArgsTest.API_cgroup_add_value_string (0 ms) [ RUN ] APIArgsTest.API_cgroup_get_uid_gid [ OK ] APIArgsTest.API_cgroup_get_uid_gid (0 ms) [ RUN ] APIArgsTest.API_cgroup_set_value_int64 [ OK ] APIArgsTest.API_cgroup_set_value_int64 (0 ms) [ RUN ] APIArgsTest.API_cgroup_get_value_int64 [ OK ] APIArgsTest.API_cgroup_get_value_int64 (0 ms) [ RUN ] APIArgsTest.API_cgroup_set_value_uint64 [ OK ] APIArgsTest.API_cgroup_set_value_uint64 (0 ms) [ RUN ] APIArgsTest.API_cgroup_get_value_uint64 [ OK ] APIArgsTest.API_cgroup_get_value_uint64 (0 ms) [----------] 11 tests from APIArgsTest (0 ms total) Signed-off-by: Kamalesh Babulal Signed-off-by: Tom Hromatka TJH: Fix comment typo (cherry picked from commit 81f12364c78a8f7f1e90b5ce90e9f02cf057c623) --- diff --git a/tests/gunit/017-API_fuzz_test.cpp b/tests/gunit/017-API_fuzz_test.cpp index d6ceb00f..ba0c8a72 100644 --- a/tests/gunit/017-API_fuzz_test.cpp +++ b/tests/gunit/017-API_fuzz_test.cpp @@ -481,3 +481,59 @@ TEST_F(APIArgsTest, API_cgroup_set_value_uint64) free(name); } + +/** + * Test arguments passed to get a controller's setting + * @param APIArgsTest googletest test case name + * @param API_cgroup_get_value_uint64 test name + * + * This test will pass a combination of valid and NULL as + * arguments to cgroup_get_value_uint64() and check if it + * handles it gracefully. + */ +TEST_F(APIArgsTest, API_cgroup_get_value_uint64) +{ + const char * const cg_name = "FuzzerCgroup"; + struct cgroup_controller * cgc = NULL; + const char * const cg_ctrl = "cpu"; + struct cgroup *cgroup = NULL; + char * name = NULL; + u_int64_t value; + int ret; + + // case 1 + // cgc = NULL, name = NULL, value = NULL + ret = cgroup_get_value_uint64(cgc, name, NULL); + ASSERT_EQ(ret, 50011); + + // case 2 + // cgc = valid, name = NULL, value = NULL + cgroup = cgroup_new_cgroup(cg_name); + ASSERT_NE(cgroup, nullptr); + + cgc = cgroup_add_controller(cgroup, cg_ctrl); + ASSERT_NE(cgroup, nullptr); + + // set cpu.shares, so that cgc->index > 0 + ret = cgroup_set_value_uint64(cgc, "cpu.shares", 1024); + ASSERT_EQ(ret, 0); + + ret = cgroup_get_value_uint64(cgc, name, NULL); + ASSERT_EQ(ret, 50011); + + // case 3 + // cgc = valid, name = valid, value = NULL + name = strdup("cpu.shares"); + ASSERT_NE(name, nullptr); + + ret = cgroup_get_value_uint64(cgc, name, NULL); + ASSERT_EQ(ret, 50011); + + // case 4 + // cgc = valid, name = NULL, value = valid + free(name); + name = NULL; + + ret = cgroup_get_value_uint64(cgc, name, &value); + ASSERT_EQ(ret, 50011); +}