From b503900b1456e8bd4b60d1deb0ef7cc3665676f1 Mon Sep 17 00:00:00 2001 From: Toni Uhlig Date: Sun, 4 Apr 2021 21:59:14 +0200 Subject: First step of cleaning up the Makefile.in hell. The goal is to get rid of the Makefile.in's (replace it with Automake Makefile.am's) as they duplicate lot's of text. That decreases readability and is in general a bad design pattern. It seems appropriate to use Automake for an Autoconf based project. Currently achieved: * using libtool to build the core library (+libtool's semantic versioning) * out-of-source builds should work right now * introducing Automake based Makefile in src/ * removed some (maybe) unused GIT ignored files * provide some small python fixes Signed-off-by: Toni Uhlig --- tests/Makefile.am | 4 +-- tests/dga/Makefile.in | 28 ++++++++++++--------- tests/do-dga.sh | 65 ------------------------------------------------- tests/do-dga.sh.in | 65 +++++++++++++++++++++++++++++++++++++++++++++++++ tests/do-unit.sh | 32 ------------------------ tests/do-unit.sh.in | 20 +++++++++++++++ tests/do.sh.in | 20 +++++++++------ tests/do_valgrind.sh.in | 11 +++++---- tests/unit/Makefile.in | 25 ++++++++++--------- 9 files changed, 134 insertions(+), 136 deletions(-) delete mode 100755 tests/do-dga.sh create mode 100755 tests/do-dga.sh.in delete mode 100755 tests/do-unit.sh create mode 100755 tests/do-unit.sh.in (limited to 'tests') diff --git a/tests/Makefile.am b/tests/Makefile.am index 271516b87..b6d37ddf5 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -1,6 +1,6 @@ -TESTS = do.sh +TESTS = do.sh do-unit.sh -EXTRA_DIST = ossfuzz.sh pcap result +EXTRA_DIST = do_valgrind.sh do-dga.sh ossfuzz.sh pcap result all: @echo -n "" diff --git a/tests/dga/Makefile.in b/tests/dga/Makefile.in index fc94ed7e7..aa2fa5d86 100644 --- a/tests/dga/Makefile.in +++ b/tests/dga/Makefile.in @@ -1,25 +1,24 @@ CC=@CC@ CXX=@CXX@ -SRCHOME=../../src - -CFLAGS=-g -fPIC -DPIC -I$(SRCHOME)/include @JSONC_CFLAGS@ @PCAP_INC@ @CFLAGS@ -LIBNDPI=$(SRCHOME)/lib/libndpi.a -LDFLAGS=$(LIBNDPI) @PCAP_LIB@ @LIBS@ @ADDITIONAL_LIBS@ @JSONC_LIBS@ -lpthread @LDFLAGS@ -HEADERS=$(SRCHOME)/include/ndpi_api.h $(SRCHOME)/include/ndpi_typedefs.h $(SRCHOME)/include/ndpi_protocol_ids.h +BUILD_SRC=../../src +ROOT_SRC=@srcdir@/../../src +ROOT_TESTS_DGA=@srcdir@ + +CFLAGS=-g -fPIC -DPIC -I$(ROOT_SRC)/include -I$(BUILD_SRC)/include @JSONC_CFLAGS@ @PCAP_INC@ @CFLAGS@ +LIBNDPI=$(BUILD_SRC)/.libs/libndpi.a +LDFLAGS=$(LIBNDPI) @PCAP_LIB@ @LIBS@ @JSONC_LIBS@ -lpthread @LDFLAGS@ +HEADERS=$(BUILD_SRC)/include/ndpi_api.h $(ROOT_SRC)/include/ndpi_typedefs.h $(ROOT_SRC)/include/ndpi_protocol_ids.h OBJS=dga_evaluate PREFIX?=@prefix@ all: dga_evaluate -EXECUTABLE_SOURCES := dga_evaluate.c -COMMON_SOURCES := $(filter-out $(EXECUTABLE_SOURCES),$(wildcard *.c )) - dga_evaluate: $(LIBNDPI) dga_evaluate.o $(CC) $(CFLAGS) dga_evaluate.o -o $@ $(LDFLAGS) -%.o: %.c $(HEADERS) Makefile - $(CC) $(CFLAGS) -c $< -o $@ +%.o: $(ROOT_TESTS_DGA)/%.c $(HEADERS) Makefile + $(CC) $(CFLAGS) -c $< -o $(notdir $@) clean: /bin/rm -f *.o dga_evaluate @@ -30,7 +29,12 @@ install: echo "" distdir: - + cp \ + $(ROOT_TESTS_DGA)/dga_evaluate.c \ + $(ROOT_TESTS_DGA)/README.md \ + $(ROOT_TESTS_DGA)/test_dga.csv \ + $(ROOT_TESTS_DGA)/test_non_dga.csv \ + '$(distdir)/' distclean: clean /bin/rm -f Makefile diff --git a/tests/do-dga.sh b/tests/do-dga.sh deleted file mode 100755 index 93408a012..000000000 --- a/tests/do-dga.sh +++ /dev/null @@ -1,65 +0,0 @@ -#!/bin/sh - -cd "$(dirname "${0}")" - -# Baseline performances ------------------------------------------------------------------------------------------------ -# Important notes: BASE values must be integers examples and represents percentage (e.g. 79%, 98%). -BASE_ACCURACY=69 -BASE_PRECISION=89 -BASE_RECALL=41 -# ---------------------------------------------------------------------------------------------------------------------- - -DGA_EVALUATE="./dga/dga_evaluate" -DGA_DATA="dga/test_dga.csv" -NON_DGA_DATA="dga/test_non_dga.csv" -DGA_DATA_SIZE=0 -NON_DGA_DATA_SIZE=0 -DATA_SIZE=0 -RC=0 - -get_evaluation_data_size() { - DGA_DATA_SIZE=`wc -l dga/test_dga.csv | awk '{split($0,a," "); print a[1]}'` - NON_DGA_DATA_SIZE=`wc -l dga/test_non_dga.csv | awk '{split($0,a," "); print a[1]}'` - DATA_SIZE=$(( $NON_DGA_DATA_SIZE + $DGA_DATA_SIZE )) -} - -evaluate_ndpi_dga_detection() { - # DGA detection is a binary classification problem, We evaluate the following metrics: - # Accuracy: (TP + TN) / (TP + TN + FN + FP) - # Precision: TP / (TP + FP) - # Recall: TP / (TP + FN) - - TP=`$DGA_EVALUATE dga/test_dga.csv` - FN=$(( $DGA_DATA_SIZE - $TP )) - FP=`$DGA_EVALUATE dga/test_non_dga.csv` - TN=$(( $NON_DGA_DATA_SIZE - $FP )) - - ACCURACY=`echo "print(int(((${TP} + ${TN})/(${TP} + ${TN} + ${FP} + ${FN}))*100))" | python3` - PRECISION=`echo "print(int(((${TP})/(${TP} + ${FP}))*100))" | python3` - RECALL=`echo "print(int(((${TP})/(${TP} + ${FN}))*100))" | python3` - - # In case modified version of classification algorithm decreases performances, test do not pass. - if [ $ACCURACY -lt $BASE_ACCURACY ]; then - printf "ERROR: Your modifications decreased DGA classifier accuracy: 0.${BASE_ACCURACY} decreased to 0.${ACCURACY}!\n" - RC=1 - fi - if [ $PRECISION -lt $BASE_PRECISION ]; then - printf "ERROR: Your modifications decreased DGA classifier precision: 0.${BASE_PRECISION} decreased to 0.${PRECISION}!\n" - RC=1 - fi - if [ $RECALL -lt $BASE_RECALL ]; then - printf "ERROR: Your modifications decreased DGA classifier recall: 0.${BASE_RECALL} decreased to 0.${RECALL}!\n" - RC=1 - fi - - # Finally we print the current performances, upgrade BASE_ metrics in case you improved it. - echo "DGA detection performances report:" - echo "Accuracy=0.$ACCURACY" - echo "Precision=0.$PRECISION" - echo "Recall=0.$RECALL" -} - -get_evaluation_data_size -evaluate_ndpi_dga_detection - -exit $RC diff --git a/tests/do-dga.sh.in b/tests/do-dga.sh.in new file mode 100755 index 000000000..4c5bd8766 --- /dev/null +++ b/tests/do-dga.sh.in @@ -0,0 +1,65 @@ +#!/bin/sh + +cd "$(dirname "${0}")" + +# Baseline performances ------------------------------------------------------------------------------------------------ +# Important notes: BASE values must be integers examples and represents percentage (e.g. 79%, 98%). +BASE_ACCURACY=69 +BASE_PRECISION=89 +BASE_RECALL=41 +# ---------------------------------------------------------------------------------------------------------------------- + +DGA_EVALUATE="dga/dga_evaluate" +DGA_DATA="@srcdir@/dga/test_dga.csv" +NON_DGA_DATA="@srcdir@/dga/test_non_dga.csv" +DGA_DATA_SIZE=0 +NON_DGA_DATA_SIZE=0 +DATA_SIZE=0 +RC=0 + +get_evaluation_data_size() { + DGA_DATA_SIZE=`wc -l ${DGA_DATA} | awk '{split($0,a," "); print a[1]}'` + NON_DGA_DATA_SIZE=`wc -l ${NON_DGA_DATA} | awk '{split($0,a," "); print a[1]}'` + DATA_SIZE=$(( $NON_DGA_DATA_SIZE + $DGA_DATA_SIZE )) +} + +evaluate_ndpi_dga_detection() { + # DGA detection is a binary classification problem, We evaluate the following metrics: + # Accuracy: (TP + TN) / (TP + TN + FN + FP) + # Precision: TP / (TP + FP) + # Recall: TP / (TP + FN) + + TP=`$DGA_EVALUATE ${DGA_DATA}` + FN=$(( $DGA_DATA_SIZE - $TP )) + FP=`$DGA_EVALUATE ${NON_DGA_DATA}` + TN=$(( $NON_DGA_DATA_SIZE - $FP )) + + ACCURACY=`echo "print(int(((${TP} + ${TN})/(${TP} + ${TN} + ${FP} + ${FN}))*100))" | python3` + PRECISION=`echo "print(int(((${TP})/(${TP} + ${FP}))*100))" | python3` + RECALL=`echo "print(int(((${TP})/(${TP} + ${FN}))*100))" | python3` + + # In case modified version of classification algorithm decreases performances, test do not pass. + if [ $ACCURACY -lt $BASE_ACCURACY ]; then + printf "ERROR: Your modifications decreased DGA classifier accuracy: 0.${BASE_ACCURACY} decreased to 0.${ACCURACY}!\n" + RC=1 + fi + if [ $PRECISION -lt $BASE_PRECISION ]; then + printf "ERROR: Your modifications decreased DGA classifier precision: 0.${BASE_PRECISION} decreased to 0.${PRECISION}!\n" + RC=1 + fi + if [ $RECALL -lt $BASE_RECALL ]; then + printf "ERROR: Your modifications decreased DGA classifier recall: 0.${BASE_RECALL} decreased to 0.${RECALL}!\n" + RC=1 + fi + + # Finally we print the current performances, upgrade BASE_ metrics in case you improved it. + echo "DGA detection performances report:" + echo "Accuracy=0.$ACCURACY" + echo "Precision=0.$PRECISION" + echo "Recall=0.$RECALL" +} + +get_evaluation_data_size +evaluate_ndpi_dga_detection + +exit $RC diff --git a/tests/do-unit.sh b/tests/do-unit.sh deleted file mode 100755 index 937be26ff..000000000 --- a/tests/do-unit.sh +++ /dev/null @@ -1,32 +0,0 @@ -#!/bin/sh - -cd "$(dirname "${0}")" - -UNIT="./unit/unit" - -RC=0 - -check_unit() { - - case "$CXXFLAGS" in - # Skipping tests with sanitizer enabled due to use-of-uninitialized-value in json-c - *sanitize* ) - echo "Skipping unit tests for this environment" - return - ;; - * ) - echo "" - echo "Running unit tests.." - ;; - esac - - $UNIT - UNIT_RC=$? - if [ $UNIT_RC -ne 0 ]; then - RC=1 - fi -} - -check_unit - -exit $RC diff --git a/tests/do-unit.sh.in b/tests/do-unit.sh.in new file mode 100755 index 000000000..cefc8a9ca --- /dev/null +++ b/tests/do-unit.sh.in @@ -0,0 +1,20 @@ +#!/bin/sh + +cd "$(dirname "${0}")" + +UNIT="./unit/unit" + +RC=0 + +check_unit() { + + $UNIT + UNIT_RC=$? + if [ $UNIT_RC -ne 0 ]; then + RC=1 + fi +} + +check_unit + +exit $RC diff --git a/tests/do.sh.in b/tests/do.sh.in index 01fad8497..4c081773b 100755 --- a/tests/do.sh.in +++ b/tests/do.sh.in @@ -1,26 +1,30 @@ #!/bin/sh -cd "$(dirname "${0}")" +BUILD_TESTS="$(realpath $(dirname "${0}"))" +cd "$(dirname "${0}")/@srcdir@" FUZZY_TESTING_ENABLED=@BUILD_FUZZTARGETS@ GCRYPT_ENABLED=@GCRYPT_ENABLED@ GCRYPT_PCAPS="gquic.pcap quic-23.pcap quic-24.pcap quic-27.pcap quic-28.pcap quic-29.pcap quic-mvfst-22.pcap quic-mvfst-27.pcapng quic-mvfst-exp.pcap quic_q50.pcap quic_t50.pcap quic_t51.pcap quic_0RTT.pcap quic_interop_V.pcapng quic-33.pcapng doq.pcapng doq_adguard.pcapng dlt_ppp.pcap os_detected.pcapng quic_frags_ch_out_of_order_same_packet_craziness.pcapng quic_frags_ch_in_multiple_packets.pcapng" -READER="../example/ndpiReader -p ../example/protos.txt -c ../example/categories.txt -r ../example/risky_domains.txt -j ../example/ja3_fingerprints.csv -S ../example/sha1_fingerprints.csv" +READER="${BUILD_TESTS}/../example/ndpiReader -p ../example/protos.txt -c ../example/categories.txt -r ../example/risky_domains.txt -j ../example/ja3_fingerprints.csv -S ../example/sha1_fingerprints.csv" RC=0 PCAPS=`cd pcap; /bin/ls *.pcap *.pcapng` -if [ ! -x "../example/ndpiReader" ]; then - echo "$0: Missing $(realpath ../example/ndpiReader)" - echo "$0: Run ./configure and make first" +if [ ! -x "${BUILD_TESTS}/../example/ndpiReader" ]; then + echo "$0: Missing ${BUILD_TESTS}/../example/ndpiReader)" + echo "$0: Run make first." exit 1 fi fuzzy_testing() { - if [ -f ../fuzz/fuzz_ndpi_reader ]; then - ../fuzz/fuzz_ndpi_reader -max_total_time="${MAX_TOTAL_TIME:-592}" -print_pcs=1 -workers="${FUZZY_WORKERS:-0}" -jobs="${FUZZY_JOBS:-0}" pcap/ - fi + if [ -f "${BUILD_TESTS}/../fuzz/fuzz_ndpi_reader" ]; then + ${BUILD_TESTS}/../fuzz/fuzz_ndpi_reader \ + -max_total_time="${MAX_TOTAL_TIME:-592}" -print_pcs=1 \ + -workers="${FUZZY_WORKERS:-0}" -jobs="${FUZZY_JOBS:-0}" \ + pcap/ + fi } build_results() { diff --git a/tests/do_valgrind.sh.in b/tests/do_valgrind.sh.in index 8ab0509a8..c76cc6e1a 100755 --- a/tests/do_valgrind.sh.in +++ b/tests/do_valgrind.sh.in @@ -1,17 +1,18 @@ #!/bin/sh -cd "$(dirname "${0}")" +BUILD_TESTS="$(realpath $(dirname "${0}"))" +cd "$(dirname "${0}")/@srcdir@" GCRYPT_ENABLED=@GCRYPT_ENABLED@ GCRYPT_PCAPS="gquic.pcap quic-23.pcap quic-24.pcap quic-27.pcap quic-28.pcap quic-29.pcap quic-mvfst-22.pcap quic-mvfst-27.pcap quic-mvfst-exp.pcap quic_q50.pcap quic_t50.pcap quic_t51.pcap quic_0RTT.pcap quic_interop_V.pcapng quic-33.pcapng doq.pcapng doq_adguard.pcapng dlt_ppp.pcap" -READER="valgrind -q --leak-check=full ../example/ndpiReader -p ../example/protos.txt -c ../example/categories.txt" +READER="valgrind -q --leak-check=full ${BUILD_TESTS}/../example/ndpiReader -p ../example/protos.txt -c ../example/categories.txt" RC=0 PCAPS=`cd pcap; /bin/ls *.pcap` -if [ ! -x "../example/ndpiReader" ]; then - echo "$0: Missing $(realpath ../example/ndpiReader)" - echo "$0: Run ./configure and make first" +if [ ! -x "${BUILD_TESTS}/../example/ndpiReader" ]; then + echo "$0: Missing $(realpath ${BUILD_TESTS}/../example/ndpiReader)" + echo "$0: Run make first." exit 1 fi diff --git a/tests/unit/Makefile.in b/tests/unit/Makefile.in index efbd387c8..808a177dd 100644 --- a/tests/unit/Makefile.in +++ b/tests/unit/Makefile.in @@ -1,25 +1,24 @@ CC=@CC@ CXX=@CXX@ -SRCHOME=../../src - -CFLAGS=-g -fPIC -DPIC -I$(SRCHOME)/include @JSONC_CFLAGS@ @PCAP_INC@ @CFLAGS@ -LIBNDPI=$(SRCHOME)/lib/libndpi.a -LDFLAGS=$(LIBNDPI) @PCAP_LIB@ @LIBS@ @ADDITIONAL_LIBS@ @JSONC_LIBS@ -lpthread @LDFLAGS@ -HEADERS=$(SRCHOME)/include/ndpi_api.h $(SRCHOME)/include/ndpi_typedefs.h $(SRCHOME)/include/ndpi_protocol_ids.h +BUILD_SRC=../../src +ROOT_SRC=@srcdir@/../../src +ROOT_TESTS_UNIT=@srcdir@ + +CFLAGS=-g -fPIC -DPIC -I$(ROOT_SRC)/include -I$(BUILD_SRC)/include @JSONC_CFLAGS@ @PCAP_INC@ @CFLAGS@ +LIBNDPI=$(BUILD_SRC)/.libs/libndpi.a +LDFLAGS=$(LIBNDPI) @PCAP_LIB@ @LIBS@ @JSONC_LIBS@ -lpthread @LDFLAGS@ +HEADERS=$(BUILD_SRC)/include/ndpi_api.h $(ROOT_SRC)/include/ndpi_typedefs.h $(ROOT_SRC)/include/ndpi_protocol_ids.h OBJS=unit PREFIX?=@prefix@ all: unit -EXECUTABLE_SOURCES := unit.c -COMMON_SOURCES := $(filter-out $(EXECUTABLE_SOURCES),$(wildcard *.c )) - unit: $(LIBNDPI) unit.o $(CC) $(CFLAGS) unit.o -o $@ $(LDFLAGS) -%.o: %.c $(HEADERS) Makefile - $(CC) $(CFLAGS) -c $< -o $@ +%.o: $(ROOT_TESTS_UNIT)/%.c $(HEADERS) Makefile + $(CC) $(CFLAGS) -c $< -o $(notdir $@) clean: /bin/rm -f *.o unit @@ -30,7 +29,9 @@ install: echo "" distdir: - + cp \ + $(ROOT_TESTS_UNIT)/unit.c \ + '$(distdir)/' distclean: clean /bin/rm -f Makefile -- cgit v1.2.3