From: Kamalesh Babulal Date: Tue, 31 Jan 2023 18:27:22 +0000 (-0700) Subject: tests/gunit: Add a simple fuzzer X-Git-Tag: v3.1.0~224 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=dac22679ee3e888be5dedc50e22e0d001bfceb4a;p=thirdparty%2Flibcgroup.git tests/gunit: Add a simple fuzzer Add a simple fuzzing test case that would exercise API's unlikely code paths by passing NULL/negative values as arguments. Output: -------- [...] [----------] 1 test from APIArgsTest [ RUN ] APIArgsTest.API_cgroup_set_permissions [ OK ] APIArgsTest.API_cgroup_set_permissions (0 ms) [----------] 1 test from APIArgsTest (0 ms total) to begin with, it exercises cgroup_set_permissions() by passing a NULL pointer in place of struct cgroup. This test case will be extended to cover almost all APIs. Signed-off-by: Kamalesh Babulal Signed-off-by: Tom Hromatka --- diff --git a/tests/gunit/017-API_fuzz_test.cpp b/tests/gunit/017-API_fuzz_test.cpp new file mode 100644 index 00000000..2cdd72bd --- /dev/null +++ b/tests/gunit/017-API_fuzz_test.cpp @@ -0,0 +1,46 @@ +/* SPDX-License-Identifier: LGPL-2.1-only */ +/** + * libcgroup googletest for fuzz testing APIs with negative values. + * + * Copyright (c) 2023 Oracle and/or its affiliates. All rights reserved. + * Author: Kamalesh Babulal + */ + +#include + +#include "gtest/gtest.h" + +#include "libcgroup-internal.h" + +class APIArgsTest: public :: testing:: Test { + protected: + + void SetUp() override { + /* Stub */ + } +}; + +/** + * Pass NULL cgroup for setting permissions + * @param APIArgsTest googletest test case name + * @param API_cgroup_set_permissions test name + * + * This test will pass NULL cgroup to the cgroup_set_permissions() + * and check it handles it gracefully. + */ +TEST_F(APIArgsTest, API_cgroup_set_permissions) +{ + mode_t dir_mode, ctrl_mode, task_mode; + struct cgroup * cgroup = NULL; + + dir_mode = (S_IRWXU | S_IXGRP | S_IXOTH); + ctrl_mode = (S_IRUSR | S_IWUSR | S_IRGRP); + task_mode = (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); + + testing::internal::CaptureStdout(); + + cgroup_set_permissions(cgroup, dir_mode, ctrl_mode, task_mode); + + std::string result = testing::internal::GetCapturedStdout(); + ASSERT_EQ(result, "Error: Cgroup, operation not allowed\n"); +} diff --git a/tests/gunit/Makefile.am b/tests/gunit/Makefile.am index ba91039c..353567b4 100644 --- a/tests/gunit/Makefile.am +++ b/tests/gunit/Makefile.am @@ -42,7 +42,9 @@ gtest_SOURCES = gtest.cpp \ 013-cgroup_build_tasks_procs_path.cpp \ 014-cgroupv2_get_subtree_control.cpp \ 015-cgroupv2_controller_enabled.cpp \ - 016-cgset_parse_r_flag.cpp + 016-cgset_parse_r_flag.cpp \ + 017-API_fuzz_test.cpp + gtest_LDFLAGS = -L$(top_srcdir)/googletest/googletest -l:libgtest.so \ -rpath $(abs_top_srcdir)/googletest/googletest