Skip to content

Commit aec55c5

Browse files
committed
List Detector - Add tests
1 parent c48bde3 commit aec55c5

24 files changed

Lines changed: 325 additions & 0 deletions

CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
2020
option(NM_NG_ENABLE_DOC_DOXYGEN "Enable build of code documentation" OFF)
2121
option(NM_NG_BUILD_WITH_ASAN "Build with Address Sanitizer (only for CMAKE_BUILD_TYPE=Debug)" OFF)
2222
option(NM_NG_BUILD_WITH_UBSAN "Build with Undefined Behavior Sanitizer (only for CMAKE_BUILD_TYPE=Debug)" OFF)
23+
option(NM_NG_ENABLE_TESTS "Build with tests of modules" OFF)
2324

2425
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pedantic -Wall -Wextra -Wunused -Wconversion -Wsign-conversion")
2526
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O3 -Werror")
@@ -41,3 +42,7 @@ add_subdirectory(modules)
4142
add_subdirectory(common)
4243
add_subdirectory(pkg)
4344
add_subdirectory(doc)
45+
46+
if (NM_NG_ENABLE_TESTS)
47+
include(cmake/tests.cmake)
48+
endif()

cmake/tests.cmake

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# File searches for test.sh script in each module subdirectory and runs it if present
2+
3+
file(GLOB MODULE_DIRS RELATIVE ${CMAKE_SOURCE_DIR}/modules ${CMAKE_SOURCE_DIR}/modules/*)
4+
enable_testing()
5+
6+
add_custom_target(tests
7+
COMMAND ctest --output-on-failure
8+
VERBATIM
9+
)
10+
11+
foreach(MODULE ${MODULE_DIRS})
12+
if (NOT IS_DIRECTORY ${CMAKE_SOURCE_DIR}/modules/${MODULE})
13+
continue()
14+
endif()
15+
set(TEST_SCRIPT "${CMAKE_SOURCE_DIR}/modules/${MODULE}/tests/test.sh")
16+
17+
if (EXISTS ${TEST_SCRIPT})
18+
add_test(NAME Test${MODULE} COMMAND bash ${TEST_SCRIPT} ${CMAKE_BINARY_DIR}/modules/${MODULE}/src/${MODULE})
19+
add_dependencies(tests ${MODULE})
20+
else()
21+
add_custom_target(print_missing_test_for_${MODULE}
22+
COMMAND ${CMAKE_COMMAND} -E cmake_echo_color --yellow --bold "No test.sh found for: ${MODULE}. Skipping..."
23+
VERBATIM
24+
)
25+
add_dependencies(tests print_missing_test_for_${MODULE})
26+
endif()
27+
endforeach()

modules/listDetector/tests/test.sh

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#!/bin/bash
2+
3+
function exit_with_error {
4+
pkill logger
5+
pkill logreplay
6+
pkill listDetector
7+
exit 1
8+
}
9+
10+
function process_started {
11+
pid=$1
12+
if ! ps -p $pid > /dev/null
13+
then
14+
echo "Failed to start process"
15+
exit_with_error
16+
fi
17+
}
18+
19+
data_path="$(dirname "$0")/testsData/"
20+
list_detector=$1
21+
22+
set -e
23+
trap 'echo "Command \"$BASH_COMMAND\" failed!"; exit_with_error' ERR
24+
for input_file in $data_path/inputs/*; do
25+
index=$(echo "$input_file" | grep -o '[0-9]\+')
26+
27+
res_file="/tmp/res"
28+
logger -i "u:listDetector" -w $res_file &
29+
logger_pid=$!
30+
sleep 0.1
31+
32+
process_started $logger_pid
33+
34+
$list_detector \
35+
-i "u:lr,u:listDetector" \
36+
-r "$data_path/rules/rule$index.csv" \
37+
-lm blacklist &
38+
39+
detector_pid=$!
40+
sleep 0.1
41+
process_started $detector_pid
42+
43+
logreplay -i "u:lr" -f "$data_path/inputs/input$index.csv" 2>/dev/null &
44+
sleep 0.1
45+
process_started $!
46+
47+
wait $logger_pid
48+
wait $detector_pid
49+
50+
if [ -f "$res_file" ]; then
51+
if ! cmp -s "$data_path/results/res$index.csv" "$res_file"; then
52+
echo "Files results/res$index.csv and $res_file are not equal"
53+
exit_with_error
54+
fi
55+
else
56+
echo "File $res_file not found"
57+
exit_with_error
58+
fi
59+
done
60+
61+
echo "All tests passed"
62+
exit 0
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
ipaddr DST_IP, ipaddr SRC_IP, uint64 BYTES, string STR_ONE, string STR_TWO, uint32 PACKETS
2+
54.175.219.8,54.175.219.8,123,test1,test1,123
3+
54.175.219.24,54.175.29.123,123,rterttest1,trtrttest11,123
4+
54.175.219.77,54.175.21.66,123,test123232,test132322,123
5+
54.175.219.99,54.175.3.17,123,test1222,test11113,123
6+
----------------------------------------------------------
7+
54.175.219.99,54.175.3.17,123,tes1222,tst11114,123
8+
54.175.219.99,54.175.3.17,123,test,test5,123
9+
54.175.219.99,54.175.3.17,123,t,test11116,123
10+
===========================================================
11+
54.175.210.19,54.175.219.16,133,test1,test2,124
12+
54.175.210.19,54.175.219.16,133,test1,test234234,124
13+
54.175.210.19,54.175.219.16,133,test1,test2123123123,124
14+
54.175.210.19,54.175.219.16,133,test1231231,test2,124
15+
-----------------------------------------------------------
16+
54.175.21.19,54.175.219.16,133,test231231,test2,124
17+
54.175.210.19,54.175.219.16,133,tst1231231,test2,124
18+
54.176.210.19,54.175.219.16,133,test,test2,124
19+
54.175.210.19,55.175.219.11,133,test1231231,test,124
20+
========================================================
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
uint16 PORT, string STR1, string STR2, uint16 GARBAGE
2+
1,match1,match2,3
3+
1,match12,match2,3
4+
1,match1,match22,3
5+
1,match2341,match2432,3
6+
7+
2,match1,match2,3
8+
2,match21,ma2tch2,3
9+
10+
3,match1,match2,3
11+
3,match12,match2,3
12+
3,match1,match22,3
13+
3,match12,match22,3
14+
15+
4,match1,match2,3
16+
4,match12,match2,3
17+
4,match1,match22,3
18+
4,match121,matc3h22,3
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
uint16 PORT, string STR1, string STR2, uint16 GARBAGE
2+
3,match1,match2,3
3+
3,match1,match22,3
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
ipaddr IPA, ipaddr IPB, string STRA, string STRB, uint16 FIXEDA, uint8 FIXEDB, string STRC, ipaddr IPC
2+
1.1.1.1,2.2.2.2,string1,string2,1234,56,string3,3.3.3.3
3+
---------------------------------------------------------
4+
1.1.1.2,2.2.2.2,string1,string2,1234,56,string3,3.3.3.3
5+
1.1.1.1,2.2.2.3,string1,string2,1234,56,string3,3.3.3.3
6+
1.1.1.1,2.2.2.2,string1,string2,1234,56,string3,4.3.3.3
7+
1.1.1.1,2.2.2.2,string,string2,1234,56,string3,3.3.3.3
8+
1.1.1.1,2.2.2.2,string1,stri,1234,56,string3,3.3.3.3
9+
1.1.1.1,2.2.2.2,string1,string2,1234,56,sng3,3.3.3.3
10+
1.1.1.1,2.2.2.2,string1,string2,1254,56,string3,3.3.3.3
11+
1.1.1.1,2.2.2.2,string1,string2,1234,66,string3,3.3.3.3
12+
============================================================
13+
1.1.2.1,2.2.3.2,string12,string22,1244,66,string32,3.3.4.3
14+
==============================================================
15+
1.1.3.1,2.2.4.2,string1,string22,1254,76,string32,3.3.4.3
16+
1.1.3.255,2.2.4.255,string1,string22,1254,76,string32,3.3.4.2
17+
1.1.3.22,2.2.4.33,string1,string22,1254,76,string32,3.3.4.1
18+
----------------------------------------------------------
19+
1.1.13.1,2.2.4.2,string1,string22,1254,76,string32,3.3.4.3
20+
1.1.3.255,2.2.4.255,string1,string22,1254,76,string32,3.23.4.2
21+
1.1.3.22,2.2.4.33,string21,string22,1254,76,string32,3.3.4.1
22+
1.1.3.1,2.2.4.2,string1,string22,1254,86,string32,3.3.4.3
23+
1.1.3.255,2.2.4.255,string1,string22,1354,76,string32,3.3.4.2
24+
1.1.3.22,2.2.4.33,string1,string22,1254,76,stringg32,3.3.4.1
25+
1.1.3.22,2.2.4.33,string1,string22,1264,76,string32,3.3.4.1
26+
===============================================================
27+
1.1.4.1,2.2.5.2,string1,str2,1264,86,str,3.3.5.3
28+
1.1.4.255,2.2.5.244,string1,str23434,1264,86,kkkkkkstr,3.3.5.2
29+
1.1.4.100,2.2.5.200,string1,bgfdgdfstr2,1264,86,ssssssstr,3.3.5.1
30+
--------------------------------------------------------------------
31+
1.1.6.1,2.2.5.2,string1,str2,1264,86,str,3.3.5.3
32+
1.1.4.255,2.2.5.244,string1,str23434,1264,86,kkkkkkstr,3.4.5.2
33+
1.1.4.100,2.2.5.200,string1,bgfdgdfstr2,1264,86,sssssssr,3.3.5.1
34+
1.1.4.1,2.2.5.2,string1,str,1264,86,str,3.3.5.3
35+
1.1.4.255,2.2.5.244,string1,str23434,124,6,kkkkkkstr,3.3.5.2
36+
1.1.4.100,2.2.5.200,string,bgfdgdfstr2,1264,86,ssssssstr,3.3.5.1
37+
=================================================================
38+
1.1.5.1,2.2.6.2,strign,strign12,1264,86,stri12,3.3.6.3
39+
1.1.5.234,2.2.6.233,strign,strign66,1264,86,stri33,3.3.6.1
40+
1.1.5.15,2.2.6.20,strign,strign11,1264,86,stri55,3.3.6.2
41+
1.1.5.234,2.2.7.233,strign,strign66,1264,86,stri33,3.3.6.1
42+
1.1.5.15,2.2.6.20,strign,strign11,1264,86,stri55,3.3.7.2
43+
--------------------------------------------------------------
44+
1.1.5.1,2.2.6.2,strin,strign12,1264,86,stri12,3.3.6.3
45+
1.1.5.234,2.2.6.233,strign,strign,1264,86,stri33,3.3.6.1
46+
1.1.5.15,2.2.6.20,strign,strign11,1264,86,stri5,3.3.6.2
47+
1.1.6.1,2.2.6.2,strign,strign12,1264,86,stri12,3.3.6.3
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
ipaddr DST_IP, ipaddr SRC_IP, uint64 BYTES, string STR_ONE, string STR_TWO, uint32 PACKETS
2+
55.175.219.8,54.175.219.8,123,test1,test1,123
3+
55.175.219.24,54.175.29.123,123,rterttest1,trtrttest11,666
4+
55.175.219.77,54.175.21.66,123,test123232,test132322,999
5+
55.175.219.99,54.175.3.17,123,test1222,test11113,111
6+
----------------------------------------------------------
7+
55.175.219.99,54.175.3.17,123,tes1222,tst11114,123
8+
55.175.219.99,54.175.3.17,123,test,test5,123
9+
55.175.219.99,54.175.3.17,123,t,test11116,123
10+
===========================================================
11+
54.175.210.169,54.175.219.16,133,test1,test2,124
12+
54.175.20.19,54.175.219.16,133,test1,test234234,124
13+
54.75.210.19,54.175.219.16,133,test1,test2123123123,124
14+
5.175.210.19,54.175.219.16,133,test1231231,test2,124
15+
-----------------------------------------------------------
16+
54.175.210.19,55.175.219.16,133,tst1231231,test2,124
17+
54.176.210.19,55.175.219.16,133,test,test2,124
18+
54.175.210.19,55.175.219.11,133,test1231231,test,124
19+
========================================================
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
ipaddr IPA, ipaddr IPB, string STRA, string STRB, uint16 FIXEDA, uint8 FIXEDB, string STRC, ipaddr IPC
2+
1.1.1.1,2.2.2.2,string1,string2,1234,56,string3,3.3.3.3
3+
---------------------------------------------------------
4+
1.1.1.2,2.2.2.2,string1,string2,1234,56,string3,3.3.3.3
5+
1.1.1.1,2.2.2.3,string1,string2,1234,56,string3,3.3.3.3
6+
1.1.1.1,2.2.2.2,string,string2,1234,56,string3,3.3.3.3
7+
1.1.1.1,2.2.2.2,string1,stri,1234,56,string3,3.3.3.3
8+
1.1.1.1,2.2.2.2,string1,string2,1234,56,sng3,3.3.3.3
9+
1.1.1.1,2.2.2.2,string1,string2,1254,56,string3,3.3.3.3
10+
1.1.1.1,2.2.2.2,string1,string2,1234,66,string3,3.3.3.3
11+
============================================================
12+
1.1.2.1,2.2.3.2,string12,string22,1244,66,string32,3.3.4.3
13+
==============================================================
14+
15+
----------------------------------------------------------
16+
1.1.13.1,2.2.4.2,string1,string22,1254,76,string32,3.3.4.3
17+
1.1.3.255,2.2.4.255,string1,string22,1254,76,string32,3.23.4.2
18+
1.1.3.1,2.2.4.2,string1,string22,1254,86,string32,3.3.4.3
19+
1.1.3.255,2.2.4.255,string1,string22,1354,76,string32,3.3.4.2
20+
1.1.3.22,2.2.4.33,string1,string22,1254,76,stringg32,3.3.4.1
21+
1.1.3.22,2.2.4.33,string1,string22,1264,76,string32,3.3.4.1
22+
===============================================================
23+
1.1.4.1,2.2.5.2,string1,str2,1264,868,str,3.3.5.3
24+
1.1.4.255,2.2.5.244,string1,str23434,1264,816,kkkkkkstr,3.3.5.2
25+
1.1.4.100,2.2.5.200,string1,bgfdgdfstr2,1264,986,ssssssstr,3.3.5.1
26+
--------------------------------------------------------------------
27+
1.1.6.1,2.2.5.2,string1,str2,1264,86,str,3.3.5.3
28+
1.1.4.255,2.2.5.244,string1,str23434,1264,86,kkkkkkstr,3.4.5.2
29+
1.1.4.100,2.2.5.200,string1,bgfdgdfstr2,1264,86,sssssssr,3.3.5.1
30+
1.1.4.1,2.2.5.2,string1,str,1264,86,str,3.3.5.3
31+
1.1.4.100,2.2.5.200,string,bgfdgdfstr2,1264,86,ssssssstr,3.3.5.1
32+
=================================================================
33+
1.1.5.1,2.2.6.2,strign,strign12,1264,86,stri12,3.3.6.3
34+
1.1.5.234,2.2.6.233,strign,strign66,1264,86,stri33,3.3.6.1
35+
1.1.5.15,2.2.6.20,strign,strign11,1264,86,stri55,3.3.6.2
36+
--------------------------------------------------------------
37+
1.1.5.234,2.2.7.233,strign,strign66,1264,86,stri33,3.3.6.1
38+
1.1.5.1,2.2.6.2,strin,strign12,1264,86,stri12,3.3.6.3
39+
1.1.5.234,2.2.6.233,strign,strign,1264,86,stri33,3.3.6.1
40+
1.1.5.15,2.2.6.20,strign,strign11,1264,86,stri5,3.3.6.2
41+
1.1.6.1,2.2.6.2,strign,strign12,1264,86,stri12,3.3.6.3
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
ipaddr IPA, ipaddr IPB, ipaddr IPC
2+
2001:0000:130F:0000:0000:09C0:876A:130A,2001:0000:130F:0000:0000:09C0:876A:130A,2001:0000:130F:0000:0000:09C0:876A:1301
3+
================================================================================================================================
4+
2101:0000:130F:0000:0000:09C0:876A:000B,2101:0000:130F:0000:0000:09C0:876A:100B,2101:0000:130F:0000:0000:09C0:876A:150B
5+
----------------------------------------------------------------------------------------------------------------------
6+
2101:1000:130F:0000:0000:09C0:876A:000B,2101:0000:130F:0000:0000:09C0:876A:100B,2101:0000:130F:0000:0000:09C0:876A:150B
7+
2101:0000:130F:0000:0000:09C0:876A:000B,2101:0000:130F:0000:0000:09C0:876A:100B,2101:0000:330F:0000:0000:09C0:876A:150B
8+
2101:0000:130F:0000:0000:09C0:876A:000B,4101:0000:130F:0000:0000:09C0:876A:100B,2101:0000:130F:0000:0000:09C0:876A:150B

0 commit comments

Comments
 (0)