Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new(tests): introduce a new test helper #2181

Merged
merged 1 commit into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 4 additions & 15 deletions userspace/libsinsp/test/filterchecks/evt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,36 +23,25 @@ TEST_F(sinsp_with_test_input, EVT_FILTER_is_open_create) {

open_inspector();

std::string path = "/home/file.txt";
int64_t fd = 3;

// In the enter event we don't send the `PPM_O_F_CREATED`
sinsp_evt* evt = add_event_advance_ts(increasing_ts(),
1,
PPME_SYSCALL_OPEN_E,
3,
path.c_str(),
sinsp_test_input::open_params::default_path,
(uint32_t)PPM_O_RDWR | PPM_O_CREAT,
(uint32_t)0);
ASSERT_EQ(get_field_as_string(evt, "evt.is_open_create"), "false");

// The `fdinfo` is not populated in the enter event
ASSERT_FALSE(evt->get_fd_info());

evt = add_event_advance_ts(increasing_ts(),
1,
PPME_SYSCALL_OPEN_X,
6,
fd,
path.c_str(),
(uint32_t)PPM_O_RDWR | PPM_O_CREAT | PPM_O_F_CREATED,
(uint32_t)0,
(uint32_t)5,
(uint64_t)123);
uint32_t flags = PPM_O_RDWR | PPM_O_CREAT | PPM_O_F_CREATED;
evt = generate_open_x_event(sinsp_test_input::open_params{.flags = flags});
ASSERT_EQ(get_field_as_string(evt, "evt.is_open_create"), "true");
ASSERT_TRUE(evt->get_fd_info());

ASSERT_EQ(evt->get_fd_info()->m_openflags, PPM_O_RDWR | PPM_O_CREAT | PPM_O_F_CREATED);
ASSERT_EQ(evt->get_fd_info()->m_openflags, flags);
}

TEST_F(sinsp_with_test_input, EVT_FILTER_is_lower_layer) {
Expand Down
14 changes: 14 additions & 0 deletions userspace/libsinsp/test/sinsp_with_test_input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,20 @@ sinsp_evt* sinsp_with_test_input::generate_getcwd_failed_entry_event(int64_t tid
path.c_str());
}

sinsp_evt* sinsp_with_test_input::generate_open_x_event(sinsp_test_input::open_params params,
int64_t tid_caller) {
return add_event_advance_ts(increasing_ts(),
tid_caller,
PPME_SYSCALL_OPEN_X,
6,
params.fd,
params.path,
params.flags,
params.mode,
params.dev,
params.ino);
}

//=============================== PROCESS GENERATION ===========================

void sinsp_with_test_input::add_thread(const scap_threadinfo& tinfo,
Expand Down
19 changes: 19 additions & 0 deletions userspace/libsinsp/test/sinsp_with_test_input.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,23 @@ limitations under the License.
#define INIT_PID INIT_TID
#define INIT_PTID 0

namespace sinsp_test_input {
struct open_params {
static constexpr int32_t default_fd = 4;
static constexpr const char* default_path = "/home/file.txt";
// Used for some filter checks
static constexpr const char* default_directory = "/home";
static constexpr const char* default_filename = "file.txt";

int32_t fd = default_fd;
const char* path = default_path;
uint32_t flags = 0;
uint32_t mode = 0;
uint32_t dev = 0;
uint64_t ino = 0;
};
} // namespace sinsp_test_input

class sinsp_with_test_input : public ::testing::Test {
protected:
sinsp_with_test_input();
Expand Down Expand Up @@ -192,6 +209,8 @@ class sinsp_with_test_input : public ::testing::Test {
sinsp_evt* generate_proc_exit_event(int64_t tid_to_remove, int64_t reaper_tid);
sinsp_evt* generate_random_event(int64_t tid_caller = INIT_TID);
sinsp_evt* generate_getcwd_failed_entry_event(int64_t tid_caller = INIT_TID);
sinsp_evt* generate_open_x_event(sinsp_test_input::open_params params = {},
int64_t tid_caller = INIT_TID);

//=============================== PROCESS GENERATION ===========================

Expand Down
Loading