|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +/* |
| 3 | +Copyright (C) 2025 The Falco Authors. |
| 4 | +
|
| 5 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +you may not use this file except in compliance with the License. |
| 7 | +You may obtain a copy of the License at |
| 8 | +
|
| 9 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +
|
| 11 | +Unless required by applicable law or agreed to in writing, software |
| 12 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +See the License for the specific language governing permissions and |
| 15 | +limitations under the License. |
| 16 | +
|
| 17 | +*/ |
| 18 | + |
| 19 | +#include <sinsp_with_test_input.h> |
| 20 | +#include "sinsp_observer.h" |
| 21 | + |
| 22 | +class test_observer : public sinsp_observer { |
| 23 | +public: |
| 24 | + test_observer(): clone_counter(0), execve_counter(0) {} |
| 25 | + |
| 26 | + void on_read(sinsp_evt* evt, |
| 27 | + int64_t tid, |
| 28 | + int64_t fd, |
| 29 | + sinsp_fdinfo* fdinfo, |
| 30 | + const char* data, |
| 31 | + uint32_t original_len, |
| 32 | + uint32_t len) override {} |
| 33 | + |
| 34 | + void on_write(sinsp_evt* evt, |
| 35 | + int64_t tid, |
| 36 | + int64_t fd, |
| 37 | + sinsp_fdinfo* fdinfo, |
| 38 | + const char* data, |
| 39 | + uint32_t original_len, |
| 40 | + uint32_t len) override {} |
| 41 | + |
| 42 | + void on_sendfile(sinsp_evt* evt, int64_t fdin, uint32_t len) override {} |
| 43 | + void on_connect(sinsp_evt* evt, uint8_t* packed_data) override {} |
| 44 | + |
| 45 | + void on_accept(sinsp_evt* evt, |
| 46 | + int64_t newfd, |
| 47 | + uint8_t* packed_data, |
| 48 | + sinsp_fdinfo* new_fdinfo) override {} |
| 49 | + |
| 50 | + void on_file_open(sinsp_evt* evt, const std::string& fullpath, uint32_t flags) override {} |
| 51 | + void on_error(sinsp_evt* evt) override {} |
| 52 | + void on_erase_fd(erase_fd_params* params) override {} |
| 53 | + void on_socket_shutdown(sinsp_evt* evt) override {} |
| 54 | + void on_execve(sinsp_evt* evt) override { execve_counter++; } |
| 55 | + |
| 56 | + void on_clone(sinsp_evt* evt, sinsp_threadinfo* newtinfo, int64_t tid_collision) override { |
| 57 | + clone_counter++; |
| 58 | + } |
| 59 | + |
| 60 | + void on_bind(sinsp_evt* evt) override {} |
| 61 | + |
| 62 | + bool on_resolve_container(sinsp_container_manager* manager, |
| 63 | + sinsp_threadinfo* tinfo, |
| 64 | + bool query_os_for_missing_info) override { |
| 65 | + return true; |
| 66 | + } |
| 67 | + |
| 68 | + void on_socket_status_changed(sinsp_evt* evt) override {} |
| 69 | + |
| 70 | + int get_clone_counter() const { return clone_counter; } |
| 71 | + |
| 72 | + int get_execve_counter() const { return execve_counter; }; |
| 73 | + |
| 74 | +private: |
| 75 | + int clone_counter; |
| 76 | + int execve_counter; |
| 77 | +}; |
| 78 | + |
| 79 | +TEST_F(sinsp_with_test_input, sinsp_observer) { |
| 80 | + add_default_init_thread(); |
| 81 | + open_inspector(); |
| 82 | + |
| 83 | + test_observer observer; |
| 84 | + |
| 85 | + m_inspector.set_observer(&observer); |
| 86 | + |
| 87 | + /* clone exit event */ |
| 88 | + generate_clone_x_event(22, INIT_TID, INIT_PID, INIT_PTID); |
| 89 | + ASSERT_EQ(observer.get_clone_counter(), 1); |
| 90 | + ASSERT_EQ(observer.get_execve_counter(), 0); |
| 91 | + |
| 92 | + /* execve exit event */ |
| 93 | + generate_execve_enter_and_exit_event(0, 11, 11, 11, 11); |
| 94 | + ASSERT_EQ(observer.get_clone_counter(), 1); |
| 95 | + ASSERT_EQ(observer.get_execve_counter(), 1); |
| 96 | +} |
0 commit comments