Skip to content

Commit 774db6b

Browse files
LucaGuerrapoiana
authored andcommitted
update(tests): add tests for numeric compare
Signed-off-by: Luca Guerra <[email protected]>
1 parent 8ff0321 commit 774db6b

File tree

4 files changed

+77
-0
lines changed

4 files changed

+77
-0
lines changed

userspace/libsinsp/test/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ set(LIBSINSP_UNIT_TESTS_SOURCES
107107
filter_parser.ut.cpp
108108
filter_op_bcontains.ut.cpp
109109
filter_op_pmatch.ut.cpp
110+
filter_op_numeric_compare.ut.cpp
110111
filter_compiler.ut.cpp
111112
filter_transformer.ut.cpp
112113
user.ut.cpp
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
/*
3+
Copyright (C) 2024 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 <libsinsp/sinsp.h>
20+
#include <gtest/gtest.h>
21+
22+
#include <sinsp_with_test_input.h>
23+
24+
TEST_F(sinsp_with_test_input, signed_int_compare)
25+
{
26+
add_default_init_thread();
27+
28+
open_inspector();
29+
30+
sinsp_evt * evt = add_event_advance_ts(increasing_ts(), 1, PPME_SYSCALL_EPOLL_CREATE_X, 1, (uint64_t)-22);
31+
32+
EXPECT_EQ(get_field_as_string(evt, "evt.cpu"), "1");
33+
34+
EXPECT_TRUE(eval_filter(evt, "evt.cpu < 300"));
35+
EXPECT_FALSE(eval_filter(evt, "evt.cpu > 300"));
36+
EXPECT_TRUE(eval_filter(evt, "evt.cpu < 2"));
37+
EXPECT_TRUE(eval_filter(evt, "evt.cpu > -500"));
38+
EXPECT_TRUE(eval_filter(evt, "evt.cpu < 500"));
39+
EXPECT_TRUE(eval_filter(evt, "evt.cpu <= 500"));
40+
41+
EXPECT_TRUE(eval_filter(evt, "evt.cpu <= 1025"));
42+
EXPECT_FALSE(eval_filter(evt, "evt.cpu >= 1025"));
43+
44+
EXPECT_FALSE(eval_filter(evt, "evt.rawarg.res > 0"));
45+
EXPECT_TRUE(eval_filter(evt, "evt.rawarg.res < 0"));
46+
EXPECT_FALSE(eval_filter(evt, "evt.rawarg.res > 4294967295"));
47+
EXPECT_TRUE(eval_filter(evt, "evt.rawarg.res < -1"));
48+
EXPECT_TRUE(eval_filter(evt, "evt.rawarg.res > -65535"));
49+
50+
evt = add_event_advance_ts(increasing_ts(), 1, PPME_SYSCALL_OPEN_E, 3, "/tmp/the_file", PPM_O_NONE, 0666);
51+
evt = add_event_advance_ts(increasing_ts(), 1, PPME_SYSCALL_OPEN_X, 6, (int64_t)(-1), "/tmp/the_file", PPM_O_NONE, 0666, 123, (uint64_t)456);
52+
53+
EXPECT_FALSE(eval_filter(evt, "fd.num >= 0"));
54+
EXPECT_FALSE(eval_filter(evt, "fd.num > 0"));
55+
EXPECT_TRUE(eval_filter(evt, "fd.num < 0"));
56+
EXPECT_FALSE(eval_filter(evt, "fd.num > 4294967295"));
57+
EXPECT_FALSE(eval_filter(evt, "fd.num < -1"));
58+
EXPECT_TRUE(eval_filter(evt, "fd.num > -65535"));
59+
}

userspace/libsinsp/test/sinsp_with_test_input.cpp

+14
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,20 @@ std::string sinsp_with_test_input::get_field_as_string(sinsp_evt* evt, std::stri
497497
return result;
498498
}
499499

500+
bool sinsp_with_test_input::eval_filter(sinsp_evt* evt, std::string filter_str)
501+
{
502+
auto factory = std::make_shared<sinsp_filter_factory>(&m_inspector, m_default_filterlist);
503+
sinsp_filter_compiler compiler(factory, filter_str);
504+
505+
auto filter = compiler.compile();
506+
if (!filter)
507+
{
508+
throw sinsp_exception(std::string("could not compile filter ") + filter_str);
509+
}
510+
511+
return filter->run(evt);
512+
}
513+
500514
sinsp_evt* sinsp_with_test_input::next_event()
501515
{
502516
sinsp_evt* evt;

userspace/libsinsp/test/sinsp_with_test_input.h

+3
Original file line numberDiff line numberDiff line change
@@ -192,12 +192,15 @@ class sinsp_with_test_input : public ::testing::Test
192192
void add_default_init_thread();
193193
void add_simple_thread(int64_t tid, int64_t pid, int64_t ptid, const std::string& comm = "random");
194194
uint64_t increasing_ts();
195+
195196
bool field_exists(sinsp_evt*, std::string_view field_name);
196197
bool field_exists(sinsp_evt*, std::string_view field_name, filter_check_list&);
197198
bool field_has_value(sinsp_evt*, std::string_view field_name);
198199
bool field_has_value(sinsp_evt*, std::string_view field_name, filter_check_list&);
199200
std::string get_field_as_string(sinsp_evt*, std::string_view field_name);
200201
std::string get_field_as_string(sinsp_evt*, std::string_view field_name, filter_check_list&);
202+
bool eval_filter(sinsp_evt* evt, std::string filter);
203+
201204
sinsp_evt* next_event();
202205

203206
scap_test_input_data m_test_data;

0 commit comments

Comments
 (0)