Skip to content

Commit 339ebbc

Browse files
authored
Merge pull request #25 from CESNET/sedlak-filter-startswith
Sedlak filter startswith
2 parents b098fa6 + 8477697 commit 339ebbc

6 files changed

Lines changed: 44 additions & 14 deletions

File tree

CMakeModules/unit_tests.cmake

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,33 +25,33 @@ ExternalProject_Get_Property(googletest binary_dir)
2525
set(GTEST_LIBRARY_PATH ${binary_dir}/googlemock/gtest/${CMAKE_FIND_LIBRARY_PREFIXES}gtest.a)
2626
set(GTEST_LIBRARY gtest)
2727
add_library(${GTEST_LIBRARY} UNKNOWN IMPORTED)
28-
set_target_properties(${GTEST_LIBRARY} PROPERTIES
29-
IMPORTED_LOCATION ${GTEST_LIBRARY_PATH}
30-
IMPORTED_LINK_INTERFACE_LIBRARIES ${CMAKE_THREAD_LIBS_INIT})
28+
set_target_properties("${GTEST_LIBRARY}" PROPERTIES
29+
IMPORTED_LOCATION "${GTEST_LIBRARY_PATH}"
30+
IMPORTED_LINK_INTERFACE_LIBRARIES "${CMAKE_THREAD_LIBS_INIT}")
3131
add_dependencies(${GTEST_LIBRARY} googletest)
3232

3333
set(GTEST_MAIN_LIBRARY_PATH ${binary_dir}/googlemock/gtest/${CMAKE_FIND_LIBRARY_PREFIXES}gtest_main.a)
3434
set(GTEST_MAIN_LIBRARY gtest_main)
3535
add_library(${GTEST_MAIN_LIBRARY} UNKNOWN IMPORTED)
36-
set_target_properties(${GTEST_MAIN_LIBRARY} PROPERTIES
37-
IMPORTED_LOCATION ${GTEST_MAIN_LIBRARY_PATH}
38-
IMPORTED_LINK_INTERFACE_LIBRARIES ${CMAKE_THREAD_LIBS_INIT})
36+
set_target_properties("${GTEST_MAIN_LIBRARY}" PROPERTIES
37+
IMPORTED_LOCATION "${GTEST_MAIN_LIBRARY_PATH}"
38+
IMPORTED_LINK_INTERFACE_LIBRARIES "${CMAKE_THREAD_LIBS_INIT}")
3939
add_dependencies(${GTEST_MAIN_LIBRARY} googletest)
4040

4141
set(GMOCK_LIBRARY_PATH ${binary_dir}/googlemock/${CMAKE_FIND_LIBRARY_PREFIXES}gmock.a)
4242
set(GMOCK_LIBRARY gmock)
4343
add_library(${GMOCK_LIBRARY} UNKNOWN IMPORTED)
44-
set_target_properties(${GMOCK_LIBRARY} PROPERTIES
45-
IMPORTED_LOCATION ${GMOCK_LIBRARY_PATH}
46-
IMPORTED_LINK_INTERFACE_LIBRARIES ${CMAKE_THREAD_LIBS_INIT})
44+
set_target_properties("${GMOCK_LIBRARY}" PROPERTIES
45+
IMPORTED_LOCATION "${GMOCK_LIBRARY_PATH}"
46+
IMPORTED_LINK_INTERFACE_LIBRARIES "${CMAKE_THREAD_LIBS_INIT}")
4747
add_dependencies(${GMOCK_LIBRARY} googletest)
4848

4949
set(GMOCK_MAIN_LIBRARY_PATH ${binary_dir}/googlemock/${CMAKE_FIND_LIBRARY_PREFIXES}gmock_main.a)
5050
set(GMOCK_MAIN_LIBRARY gmock_main)
5151
add_library(${GMOCK_MAIN_LIBRARY} UNKNOWN IMPORTED)
52-
set_target_properties(${GMOCK_MAIN_LIBRARY} PROPERTIES
53-
IMPORTED_LOCATION ${GMOCK_MAIN_LIBRARY_PATH}
54-
IMPORTED_LINK_INTERFACE_LIBRARIES ${CMAKE_THREAD_LIBS_INIT})
52+
set_target_properties("${GMOCK_MAIN_LIBRARY}" PROPERTIES
53+
IMPORTED_LOCATION "${GMOCK_MAIN_LIBRARY_PATH}"
54+
IMPORTED_LINK_INTERFACE_LIBRARIES "${CMAKE_THREAD_LIBS_INIT}")
5555
add_dependencies(${GMOCK_MAIN_LIBRARY} ${GTEST_LIBRARY})
5656

5757
include_directories(

src/filter/operations/str.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,19 @@ contains_str(fds_filter_value_u *big, fds_filter_value_u *little, fds_filter_val
8888
result->b = strnnstr(big->str.chars, little->str.chars, big->str.len, little->str.len) != NULL;
8989
}
9090

91+
void
92+
startswith_str(fds_filter_value_u *big, fds_filter_value_u *little, fds_filter_value_u *result)
93+
{
94+
result->b = (big->str.len >= little->str.len
95+
&& memcmp(big->str.chars, little->str.chars, little->str.len) == 0);
96+
}
9197

92-
98+
void
99+
endswith_str(fds_filter_value_u *big, fds_filter_value_u *little, fds_filter_value_u *result)
100+
{
101+
result->b = (big->str.len >= little->str.len
102+
&& memcmp(&big->str.chars[big->str.len - little->str.len], little->str.chars, little->str.len) == 0);
103+
}
93104

94105
void
95106
str_in_list(fds_filter_value_u *item, fds_filter_value_u *list, fds_filter_value_u *result)
@@ -136,6 +147,8 @@ const fds_filter_op_s str_operations[] = {
136147
FDS_FILTER_DEF_BINARY_OP(FDS_FDT_STR, "!=", FDS_FDT_STR, ne_str, FDS_FDT_BOOL),
137148

138149
FDS_FILTER_DEF_BINARY_OP(FDS_FDT_STR, "contains", FDS_FDT_STR, contains_str, FDS_FDT_BOOL),
150+
FDS_FILTER_DEF_BINARY_OP(FDS_FDT_STR, "startswith", FDS_FDT_STR, startswith_str, FDS_FDT_BOOL),
151+
FDS_FILTER_DEF_BINARY_OP(FDS_FDT_STR, "endswith", FDS_FDT_STR, endswith_str, FDS_FDT_BOOL),
139152

140153
FDS_FILTER_DEF_BINARY_OP(FDS_FDT_STR, "in", FDS_FDT_LIST | FDS_FDT_STR, str_in_list, FDS_FDT_BOOL),
141154

src/filter/parser.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ struct operator_s op_parse_def_table[] = {
8888
{ "exists" , 2, OP_KIND_PREFIX, OP_ASSOC_NONE },
8989
{ "in" , 2, OP_KIND_INFIX , OP_ASSOC_LEFT },
9090
{ "contains", 2, OP_KIND_INFIX , OP_ASSOC_LEFT },
91+
{ "startswith", 2, OP_KIND_INFIX , OP_ASSOC_LEFT },
92+
{ "endswith", 2, OP_KIND_INFIX , OP_ASSOC_LEFT },
9193

9294
{ "" , 1, OP_KIND_INFIX , OP_ASSOC_LEFT },
9395
{ "==" , 1, OP_KIND_INFIX , OP_ASSOC_LEFT },

src/filter/scanner.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ const struct {
7575
// Strings that are treated as symbols
7676
const char *symbols[] = {
7777
"~", "not", "*", "/", "+", "-", "|", "&", "^", "%",
78-
"and", "or", "in", "contains", "exists", "[", "]", "(", ")", ",",
78+
"and", "or", "in", "contains", "startswith", "endswith", "exists",
79+
"[", "]", "(", ")", ",",
7980
"<", ">", "==", "!=", ">=", "<=", "<<", ">>",
8081
"in", "out", "ingress", "egress", "src", "dst"
8182
};

src/ipfix_filter/ipfix_filter.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ get_filter_data_type(const struct fds_iemgr_elem *elem)
177177
return FDS_FDT_MAC;
178178

179179
case FDS_ET_STRING:
180+
case FDS_ET_OCTET_ARRAY:
180181
return FDS_FDT_STR;
181182

182183
case FDS_ET_DATE_TIME_SECONDS:
@@ -386,6 +387,7 @@ read_record_field(struct fds_drec *record, const struct fds_iemgr_elem *field_de
386387
break;
387388

388389
case FDS_ET_STRING:
390+
case FDS_ET_OCTET_ARRAY:
389391
out_value->str.len = size;
390392
out_value->str.chars = (char *) data;
391393
break;

tests/unit_tests/filter/filter.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,18 @@ TEST_F(Filter, string_operations) {
397397
EXPECT_EQ(evaluate("\"hello world!\" contains \"\""), true);
398398
EXPECT_EQ(evaluate("not \"\" contains \"hello\""), true);
399399
EXPECT_EQ(evaluate("not \"hello world!\" contains \"foo\""), true);
400+
EXPECT_EQ(evaluate("\"hello world!\" startswith \"hello\""), true);
401+
EXPECT_EQ(evaluate("\"hello world!\" startswith \"hello world!\""), true);
402+
EXPECT_EQ(evaluate("\"hello world!\" startswith \"hello world!!\""), false);
403+
EXPECT_EQ(evaluate("\"hello world!\" startswith \"ello world!!\""), false);
404+
EXPECT_EQ(evaluate("\"hello world!\" startswith \"\""), true);
405+
EXPECT_EQ(evaluate("\"hello world!\" startswith \"ello\""), false);
406+
EXPECT_EQ(evaluate("\"hello world!\" startswith \"world\""), false);
407+
EXPECT_EQ(evaluate("\"hello world!\" endswith \"world\""), false);
408+
EXPECT_EQ(evaluate("\"hello world!\" endswith \"world!\""), true);
409+
EXPECT_EQ(evaluate("\"hello world!\" endswith \"hello world!\""), true);
410+
EXPECT_EQ(evaluate("\"hello world!\" endswith \"hello\""), false);
411+
EXPECT_EQ(evaluate("\"hello world!\" endswith \"hello world!!\""), false);
400412
}
401413

402414
TEST_F(Filter, bitwise_operations) {

0 commit comments

Comments
 (0)