Skip to content

Commit

Permalink
chore(userspace/libsinsp): add small test around sinsp_observer.
Browse files Browse the repository at this point in the history
Signed-off-by: Federico Di Pierro <[email protected]>
  • Loading branch information
FedeDP committed Jan 8, 2025
1 parent da8254f commit 6ba4e77
Showing 1 changed file with 96 additions and 0 deletions.
96 changes: 96 additions & 0 deletions userspace/libsinsp/test/classes/sinsp_observer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// SPDX-License-Identifier: Apache-2.0
/*
Copyright (C) 2025 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 <sinsp_with_test_input.h>
#include "sinsp_observer.h"

class test_observer : public sinsp_observer {

Check warning on line 22 in userspace/libsinsp/test/classes/sinsp_observer.cpp

View check run for this annotation

Codecov / codecov/patch

userspace/libsinsp/test/classes/sinsp_observer.cpp#L22

Added line #L22 was not covered by tests
public:
test_observer(): clone_counter(0), execve_counter(0) {}

void on_read(sinsp_evt* evt,

Check warning on line 26 in userspace/libsinsp/test/classes/sinsp_observer.cpp

View check run for this annotation

Codecov / codecov/patch

userspace/libsinsp/test/classes/sinsp_observer.cpp#L26

Added line #L26 was not covered by tests
int64_t tid,
int64_t fd,
sinsp_fdinfo* fdinfo,
const char* data,
uint32_t original_len,
uint32_t len) override {}

Check warning on line 32 in userspace/libsinsp/test/classes/sinsp_observer.cpp

View check run for this annotation

Codecov / codecov/patch

userspace/libsinsp/test/classes/sinsp_observer.cpp#L32

Added line #L32 was not covered by tests

void on_write(sinsp_evt* evt,

Check warning on line 34 in userspace/libsinsp/test/classes/sinsp_observer.cpp

View check run for this annotation

Codecov / codecov/patch

userspace/libsinsp/test/classes/sinsp_observer.cpp#L34

Added line #L34 was not covered by tests
int64_t tid,
int64_t fd,
sinsp_fdinfo* fdinfo,
const char* data,
uint32_t original_len,
uint32_t len) override {}

Check warning on line 40 in userspace/libsinsp/test/classes/sinsp_observer.cpp

View check run for this annotation

Codecov / codecov/patch

userspace/libsinsp/test/classes/sinsp_observer.cpp#L40

Added line #L40 was not covered by tests

void on_sendfile(sinsp_evt* evt, int64_t fdin, uint32_t len) override {}
void on_connect(sinsp_evt* evt, uint8_t* packed_data) override {}

Check warning on line 43 in userspace/libsinsp/test/classes/sinsp_observer.cpp

View check run for this annotation

Codecov / codecov/patch

userspace/libsinsp/test/classes/sinsp_observer.cpp#L42-L43

Added lines #L42 - L43 were not covered by tests

void on_accept(sinsp_evt* evt,

Check warning on line 45 in userspace/libsinsp/test/classes/sinsp_observer.cpp

View check run for this annotation

Codecov / codecov/patch

userspace/libsinsp/test/classes/sinsp_observer.cpp#L45

Added line #L45 was not covered by tests
int64_t newfd,
uint8_t* packed_data,
sinsp_fdinfo* new_fdinfo) override {}

Check warning on line 48 in userspace/libsinsp/test/classes/sinsp_observer.cpp

View check run for this annotation

Codecov / codecov/patch

userspace/libsinsp/test/classes/sinsp_observer.cpp#L48

Added line #L48 was not covered by tests

void on_file_open(sinsp_evt* evt, const std::string& fullpath, uint32_t flags) override {}
void on_error(sinsp_evt* evt) override {}
void on_erase_fd(erase_fd_params* params) override {}
void on_socket_shutdown(sinsp_evt* evt) override {}

Check warning on line 53 in userspace/libsinsp/test/classes/sinsp_observer.cpp

View check run for this annotation

Codecov / codecov/patch

userspace/libsinsp/test/classes/sinsp_observer.cpp#L50-L53

Added lines #L50 - L53 were not covered by tests
void on_execve(sinsp_evt* evt) override { execve_counter++; }

void on_clone(sinsp_evt* evt, sinsp_threadinfo* newtinfo, int64_t tid_collision) override {
clone_counter++;
}

void on_bind(sinsp_evt* evt) override {}

Check warning on line 60 in userspace/libsinsp/test/classes/sinsp_observer.cpp

View check run for this annotation

Codecov / codecov/patch

userspace/libsinsp/test/classes/sinsp_observer.cpp#L60

Added line #L60 was not covered by tests

bool on_resolve_container(sinsp_container_manager* manager,
sinsp_threadinfo* tinfo,
bool query_os_for_missing_info) override {
return true;
}

void on_socket_status_changed(sinsp_evt* evt) override {}

Check warning on line 68 in userspace/libsinsp/test/classes/sinsp_observer.cpp

View check run for this annotation

Codecov / codecov/patch

userspace/libsinsp/test/classes/sinsp_observer.cpp#L68

Added line #L68 was not covered by tests

int get_clone_counter() const { return clone_counter; }

int get_execve_counter() const { return execve_counter; };

private:
int clone_counter;
int execve_counter;
};

TEST_F(sinsp_with_test_input, sinsp_observer) {
add_default_init_thread();
open_inspector();

test_observer observer;

m_inspector.set_observer(&observer);

/* clone exit event */
generate_clone_x_event(22, INIT_TID, INIT_PID, INIT_PTID);
ASSERT_EQ(observer.get_clone_counter(), 1);
ASSERT_EQ(observer.get_execve_counter(), 0);

/* execve exit event */
generate_execve_enter_and_exit_event(0, 11, 11, 11, 11);
ASSERT_EQ(observer.get_clone_counter(), 1);
ASSERT_EQ(observer.get_execve_counter(), 1);
}

0 comments on commit 6ba4e77

Please sign in to comment.