image: debian:stable

stages:
  - build_and_test

before_script:
  - export DEBIAN_FRONTEND=noninteractive
  - apt-get update -qq
  - >
      apt-get install -y -qq \
        coreutils sudo \
        build-essential make cmake binutils gcc clang autoconf automake \
        libtool pkg-config git wget unzip \
        libpcap-dev libgpg-error-dev libjson-c-dev zlib1g-dev \
        netcat-openbsd python3 python3-jsonschema tree lcov iproute2

after_script:
  - test -r /tmp/nDPIsrvd.log && cat /tmp/nDPIsrvd.log
  - test -r /tmp/nDPId.log && cat /tmp/nDPId.log

build_and_test_static_libndpi_tsan:
  script:
    # test for NETWORK_BUFFER_MAX_SIZE C and Python value equality
    - C_VAL=$(cat config.h | sed -n 's/^#define\s\+NETWORK_BUFFER_MAX_SIZE\s\+\([0-9]\+\).*$/\1/gp')
    - PY_VAL=$(cat dependencies/nDPIsrvd.py | sed -n 's/^NETWORK_BUFFER_MAX_SIZE = \([0-9]\+\).*$/\1/gp')
    - test ${C_VAL} = ${PY_VAL}
    # test for nDPId_PACKETS_PLEN_MAX C and Python value equality
    - C_VAL=$(cat config.h | sed -n 's/^#define\s\+nDPId_PACKETS_PLEN_MAX\s\+\([0-9]\+\).*$/\1/gp')
    - PY_VAL=$(cat dependencies/nDPIsrvd.py | sed -n 's/^nDPId_PACKETS_PLEN_MAX = \([0-9]\+\).*$/\1/gp')
    - test ${C_VAL} = ${PY_VAL}
    # static linked build
    - mkdir build-clang-tsan
    - cd build-clang-tsan
    - env CMAKE_C_FLAGS='-Werror' CMAKE_C_COMPILER='clang' cmake .. -DBUILD_EXAMPLES=ON -DBUILD_NDPI=ON -DBUILD_NDPI_FORCE_GIT_UPDATE=ON -DENABLE_SANITIZER_THREAD=ON -DENABLE_ZLIB=ON
    - make clean-libnDPI
    - make libnDPI
    - tree libnDPI
    - make install VERBOSE=1 DESTDIR="$(realpath ../_install)"
    - cd ..
    - ./_install/usr/local/bin/nDPId-test
    - ./test/run_tests.sh ./libnDPI ./_install/usr/local/bin/nDPId-test
  artifacts:
    expire_in: 1 week
    paths:
      - _install/
  stage: build_and_test

build_and_test_static_libndpi:
  script:
    - mkdir build-cmake-submodule
    - cd build-cmake-submodule
    - env CMAKE_C_FLAGS='-Werror' cmake .. -DENABLE_SYSTEMD=ON -DBUILD_EXAMPLES=ON -DBUILD_NDPI=ON -DBUILD_NDPI_FORCE_GIT_UPDATE=ON -DENABLE_ZLIB=ON
    - make clean-libnDPI
    - make libnDPI
    - tree libnDPI
    - make install VERBOSE=1 DESTDIR="$(realpath ../_install)"
    - cpack -G DEB
    - sudo dpkg -i nDPId-*.deb
    - cd ..
    - test -x /bin/systemctl && sudo systemctl daemon-reload
    - test -x /bin/systemctl && sudo systemctl enable ndpid@lo
    - test -x /bin/systemctl && sudo systemctl start ndpid@lo
    - test -x /bin/systemctl && sudo systemctl status ndpisrvd.service ndpid@lo.service
    - test -x /bin/systemctl && sudo systemctl stop ndpid@lo
    - ./build-cmake-submodule/nDPId-test
    - ./test/run_tests.sh ./libnDPI ./build-cmake-submodule/nDPId-test
    - >
      if ldd ./build-cmake-submodule/nDPId | grep -qoEi libndpi; then \
      echo 'nDPId linked against a static libnDPI should not contain a shared linked libnDPI.' >&2; false; fi
    - cc nDPId.c nio.c utils.c -I./build-cmake-submodule/libnDPI/include/ndpi -I. -I./dependencies -I./dependencies/jsmn -I./dependencies/uthash/include -o /tmp/a.out -lpcap ./build-cmake-submodule/libnDPI/lib/libndpi.a -pthread -lm -lz
  artifacts:
    expire_in: 1 week
    paths:
      - build-cmake-submodule/*.deb
      - _install/
  stage: build_and_test

build_and_test_static_libndpi_coverage:
  script:
    - mkdir build-cmake-submodule
    - cd build-cmake-submodule
    - env CMAKE_C_FLAGS='-Werror' cmake .. -DENABLE_SYSTEMD=ON -DENABLE_COVERAGE=ON -DBUILD_EXAMPLES=ON -DBUILD_NDPI=ON -DBUILD_NDPI_FORCE_GIT_UPDATE=ON -DENABLE_SANITIZER=ON -DENABLE_ZLIB=ON
    - make clean-libnDPI
    - make libnDPI
    - tree libnDPI
    - make install VERBOSE=1 DESTDIR="$(realpath ../_install)"
    - cd ..
    - ./build-cmake-submodule/nDPId-test
    - ./test/run_tests.sh ./libnDPI ./build-cmake-submodule/nDPId-test
    # generate coverage report
    - make -C ./build-cmake-submodule coverage || true
    - >
      if ldd build/nDPId | grep -qoEi libndpi; then \
      echo 'nDPId linked against a static libnDPI should not contain a shared linked libnDPI.' >&2; false; fi
  artifacts:
    expire_in: 1 week
    paths:
      - build-cmake-submodule/coverage_report
      - _install/
  stage: build_and_test

build_dynamic_libndpi:
  script:
    # pkg-config dynamic linked build
    - git clone https://github.com/ntop/nDPI.git
    - cd nDPI
    - ./autogen.sh --prefix="$(realpath ../_install)" --enable-option-checking=fatal
    - make install V=s
    - cd ..
    - tree ./_install
    - mkdir build
    - cd build
    - export CMAKE_PREFIX_PATH="$(realpath ../_install)"
    - env CMAKE_C_FLAGS='-Werror' cmake .. -DBUILD_EXAMPLES=ON -DENABLE_SANITIZER=ON -DENABLE_MEMORY_PROFILING=ON -DENABLE_ZLIB=ON
    - make all VERBOSE=1
    - make install VERBOSE=1 DESTDIR="$(realpath ../_install)"
    - cd ..
    - tree ./_install
    - ./build/nDPId-test
    - ./build/nDPId -h || test $? -eq 1
    - ./build/nDPIsrvd -h || test $? -eq 1
    # dameon start/stop test
    - NUSER=nobody make -C ./build daemon VERBOSE=1
    - NUSER=nobody make -C ./build daemon VERBOSE=1
    # make dist
    - make -C ./build dist
  artifacts:
    expire_in: 1 week
    paths:
      - _install/
  stage: build_and_test
