Skip to content

Commit

Permalink
test(scap): add unit tests for scap_cgroup_prefix_path
Browse files Browse the repository at this point in the history
Signed-off-by: Mauro Ezequiel Moltrasio <mmoltras@redhat.com>
Co-authored-by: Andrea Terzolo <andreaterzolo3@gmail.com>
  • Loading branch information
Molter73 and Andreagit97 committed Sep 21, 2023
1 parent 384665f commit 55008e8
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 1 deletion.
6 changes: 6 additions & 0 deletions test/libscap/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ set(LIBSCAP_TESTS_SOURCES ${USERSPACE_TEST_SUITE})
file(GLOB_RECURSE LIBSCAP_TESTS_UTILS_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/helpers/*cpp")
list(APPEND LIBSCAP_TESTS_SOURCES ${LIBSCAP_TESTS_UTILS_SOURCES})

# Linux specific tests
if (CMAKE_SYSTEM_NAME MATCHES "Linux")
file(GLOB_RECURSE LINUX_TEST_SUITE "${CMAKE_CURRENT_SOURCE_DIR}/test_suites/userspace/linux/*.cpp")
list(APPEND LIBSCAP_TEST_SOURCES ${LINUX_TEST_SUITE})
endif()

# Engine-specific tests
if(BUILD_DRIVER)
file(GLOB_RECURSE KMOD_TEST_SUITE "${CMAKE_CURRENT_SOURCE_DIR}/test_suites/engines/kmod/*.cpp")
Expand Down
77 changes: 77 additions & 0 deletions test/libscap/test_suites/userspace/linux/scap_cgroup.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
Copyright (C) 2023 The Falco Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#include <gtest/gtest.h>
#include <linux/scap_cgroup.h>
#include <linux/scap_cgroup.c>

TEST(cgroups, path_relative)
{
char final_path[4096];
const char* prefix = "/1/2/3";
const char* path = "/../../../init.scope";
size_t prefix_len = 0;
size_t path_strip_len = 0;
ASSERT_EQ(scap_cgroup_prefix_path(prefix, path, &prefix_len, &path_strip_len), SCAP_SUCCESS);
snprintf(final_path, sizeof(final_path), "%.*s%s", (int)prefix_len, prefix, path + path_strip_len);
ASSERT_STREQ(final_path,"/init.scope");
}

TEST(cgroups, path_relative_with_final_slash)
{
char final_path[4096];
const char* prefix = "/1/2/3/";
const char* path = "/../../../init.scope";
size_t prefix_len = 0;
size_t path_strip_len = 0;
ASSERT_EQ(scap_cgroup_prefix_path(prefix, path, &prefix_len, &path_strip_len), SCAP_SUCCESS);
snprintf(final_path, sizeof(final_path), "%.*s%s", (int)prefix_len, prefix, path + path_strip_len);
ASSERT_STREQ(final_path,"/1/init.scope");
}

TEST(cgroups, path_absolute)
{
char final_path[4096];
const char* prefix = "/1/2/3";
const char* path = "/absolute";
size_t prefix_len = 0;
size_t path_strip_len = 0;
ASSERT_EQ(scap_cgroup_prefix_path(prefix, path, &prefix_len, &path_strip_len), SCAP_SUCCESS);
snprintf(final_path, sizeof(final_path), "%.*s%s", (int)prefix_len, prefix, path + path_strip_len);
ASSERT_STREQ(final_path,"/1/2/3/absolute");
}

TEST(cgroups, prefix_empty)
{
const char* prefix = "";
const char* path = "/../../absolute";
size_t prefix_len = 0;
size_t path_strip_len = 0;
ASSERT_EQ(scap_cgroup_prefix_path(prefix, path, &prefix_len, &path_strip_len), SCAP_FAILURE);
}

TEST(cgroups, path_empty)
{
char final_path[4096];
const char* prefix = "/1/2/3";
const char* path = "";
size_t prefix_len = 0;
size_t path_strip_len = 0;
ASSERT_EQ(scap_cgroup_prefix_path(prefix, path, &prefix_len, &path_strip_len), SCAP_SUCCESS);
snprintf(final_path, sizeof(final_path), "%.*s%s", (int)prefix_len, prefix, path + path_strip_len);
ASSERT_STREQ(final_path,"/1/2/3");
}
5 changes: 4 additions & 1 deletion userspace/libscap/linux/scap_cgroup.c
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,9 @@ static const char* scan_back(const char* start, const char* end)
// slash so that we don't end up with doubled slashes (one from the prefix, one from the path)
static int32_t scap_cgroup_prefix_path(const char* prefix, const char* path, size_t* prefix_len, size_t* path_strip_len)
{
ASSERT(prefix != NULL);
ASSERT(path != NULL);

const char* prefix_p = prefix + strlen(prefix);
const char* path_p = path;

Expand Down Expand Up @@ -533,7 +536,7 @@ static int32_t get_cgroup_subsystems_v2(struct scap_cgroup_interface* cgi, struc

if(cgi->m_use_cache)
{
struct scap_cgroup_cache* cached = malloc(sizeof(*cached));
struct scap_cgroup_cache* cached = (struct scap_cgroup_cache*)malloc(sizeof(*cached));
if(cached)
{
int uth_status = SCAP_SUCCESS;
Expand Down

0 comments on commit 55008e8

Please sign in to comment.