@@ -17,6 +17,7 @@ limitations under the License.
1717
1818#include < falco/app/actions/helpers.h>
1919#include < falco/configuration.h>
20+ #include < libsinsp/utils.h>
2021#include < gtest/gtest.h>
2122
2223TEST (Capture, generate_scap_file_path_realistic_scenario) {
@@ -79,6 +80,7 @@ TEST(Capture, capture_config_disabled_by_default) {
7980 EXPECT_EQ (config.m_capture_path_prefix , " /tmp/falco" );
8081 EXPECT_EQ (config.m_capture_mode , capture_mode_t ::RULES);
8182 EXPECT_EQ (config.m_capture_default_duration_ns , 5000 * 1000000LL ); // 5 seconds in ns
83+ EXPECT_EQ (config.m_capture_max_file_size_mb , 0u );
8284}
8385
8486TEST (Capture, capture_config_enabled_rules_mode) {
@@ -132,3 +134,92 @@ TEST(Capture, capture_config_invalid_mode) {
132134 // Should throw an exception for invalid mode
133135 EXPECT_THROW (res = config.init_from_content (config_content, {}), std::logic_error);
134136}
137+
138+ TEST (Capture, capture_config_with_max_file_size_mb) {
139+ std::string config_content = R"(
140+ capture:
141+ enabled: true
142+ max_file_size_mb: 100
143+ )" ;
144+
145+ falco_configuration config;
146+ config_loaded_res res;
147+ ASSERT_NO_THROW (res = config.init_from_content (config_content, {}));
148+
149+ EXPECT_EQ (config.m_capture_max_file_size_mb , 100u );
150+ }
151+
152+ TEST (Capture, capture_config_with_all_limits) {
153+ std::string config_content = R"(
154+ capture:
155+ enabled: true
156+ default_duration: 15000
157+ max_file_size_mb: 500
158+ )" ;
159+
160+ falco_configuration config;
161+ config_loaded_res res;
162+ ASSERT_NO_THROW (res = config.init_from_content (config_content, {}));
163+
164+ EXPECT_EQ (config.m_capture_default_duration_ns , 15000 * 1000000LL );
165+ EXPECT_EQ (config.m_capture_max_file_size_mb , 500u );
166+ }
167+
168+ TEST (Capture, capture_config_rejects_out_of_range_max_file_size_mb) {
169+ // Exceeds the schema maximum (1 TB in MB)
170+ std::string config_content = R"(
171+ capture:
172+ enabled: true
173+ max_file_size_mb: 99999999999
174+ )" ;
175+
176+ falco_configuration config;
177+ config_loaded_res res;
178+ ASSERT_NO_THROW (res = config.init_from_content (config_content, {}));
179+ for (const auto & pair : res) {
180+ EXPECT_TRUE (sinsp_utils::startswith (pair.second , yaml_helper::validation_failed))
181+ << pair.second ;
182+ }
183+ }
184+
185+ TEST (Capture, check_capture_stop_no_stop) {
186+ // Deadline in the future, no size cap
187+ EXPECT_EQ (falco::app::actions::check_capture_stop (100 , 200 , 0 , 0 ),
188+ falco::app::actions::capture_stop_reason::NONE);
189+ // Deadline in the future, size under cap
190+ EXPECT_EQ (falco::app::actions::check_capture_stop (100 , 200 , 1024 , 1 ),
191+ falco::app::actions::capture_stop_reason::NONE);
192+ }
193+
194+ TEST (Capture, check_capture_stop_time_deadline) {
195+ // Exactly at deadline stops (>=)
196+ EXPECT_EQ (falco::app::actions::check_capture_stop (200 , 200 , 0 , 0 ),
197+ falco::app::actions::capture_stop_reason::TIME_DEADLINE);
198+ // Past deadline
199+ EXPECT_EQ (falco::app::actions::check_capture_stop (201 , 200 , 0 , 0 ),
200+ falco::app::actions::capture_stop_reason::TIME_DEADLINE);
201+ }
202+
203+ TEST (Capture, check_capture_stop_size_limit) {
204+ // 1 MB cap, written exactly 1 MB -> stops
205+ EXPECT_EQ (falco::app::actions::check_capture_stop (100 , 200 , 1024 * 1024 , 1 ),
206+ falco::app::actions::capture_stop_reason::SIZE_LIMIT);
207+ // 1 MB cap, written over
208+ EXPECT_EQ (falco::app::actions::check_capture_stop (100 , 200 , 2 * 1024 * 1024 , 1 ),
209+ falco::app::actions::capture_stop_reason::SIZE_LIMIT);
210+ // 1 MB cap, written just under -> no stop
211+ EXPECT_EQ (falco::app::actions::check_capture_stop (100 , 200 , 1024 * 1024 - 1 , 1 ),
212+ falco::app::actions::capture_stop_reason::NONE);
213+ }
214+
215+ TEST (Capture, check_capture_stop_zero_means_unlimited) {
216+ // 0 MB cap means unlimited, even with huge written_bytes
217+ EXPECT_EQ (falco::app::actions::check_capture_stop (100 , 200 , UINT64_MAX, 0 ),
218+ falco::app::actions::capture_stop_reason::NONE);
219+ }
220+
221+ TEST (Capture, check_capture_stop_time_wins_when_both_tripped) {
222+ // Both conditions met: time is reported first (matches check order)
223+ EXPECT_EQ (falco::app::actions::check_capture_stop (200 , 200 , 10 * 1024 * 1024 , 1 ),
224+ falco::app::actions::capture_stop_reason::TIME_DEADLINE);
225+ }
0 commit comments