name: "mdns: host-tests"

on:
  push:
    branches:
      - master
  pull_request:
    types: [opened, synchronize, reopened, labeled]

jobs:
  host_test_mdns:
    if: contains(github.event.pull_request.labels.*.name, 'mdns') || github.event_name == 'push'
    name: Host test build
    runs-on: ubuntu-22.04
    container: espressif/idf:release-v5.3

    steps:
      - name: Checkout esp-protocols
        uses: actions/checkout@v4
        with:
          path: protocols

      - name: Build and Test
        shell: bash
        run: |
          . ${IDF_PATH}/export.sh
          python -m pip install idf-build-apps==2.10.0 dnspython pytest pytest-embedded pytest-embedded-serial-esp pytest-embedded-idf
          cd $GITHUB_WORKSPACE/protocols
          # Build host tests app (with all configs and targets supported)
          python ./ci/build_apps.py components/mdns/tests/host_test/
          cd components/mdns/tests/host_test
          # First run the linux_app and send a quick A query and a reverse query
          ./build_linux_app/mdns_host.elf &
          python dnsfixture.py A myesp.local --ip_only | xargs python dnsfixture.py X
          # Next we run the pytest (using the console app)
          pytest

  host_compat_checks:
    if: contains(github.event.pull_request.labels.*.name, 'mdns') || github.event_name == 'push'
    name: Set of compatibility checks
    strategy:
      matrix:
        idf_ver: ["latest"]

    runs-on: ubuntu-22.04
    container: espressif/idf:${{ matrix.idf_ver }}
    steps:
      - name: Checkout esp-protocols
        uses: actions/checkout@v4
      - name: Install Necessary Libs
        run: |
          apt-get update -y
          apt-get install -y libbsd-dev
      - name: Test AFL compat build
        shell: bash
        run: |
          . ${IDF_PATH}/export.sh
          cd components/mdns/tests/test_afl_fuzz_host/
          make INSTR=off
      - name: Test no malloc functions
        shell: bash
        run: |
          cd components/mdns
          for file in $(ls *.c); do
            cp $file /tmp
            echo -n "Checking that $file does not call any std allocations directly..."
            python mem_prefix_script.py $file
            diff -q $file /tmp/$file || exit 1
            echo "OK"
          done
