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 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

  build_afl_host_test_mdns:
    if: contains(github.event.pull_request.labels.*.name, 'mdns') || github.event_name == 'push'
    name: Build AFL host test
    strategy:
      matrix:
        idf_ver: ["latest"]
        idf_target: ["esp32"]

    runs-on: ubuntu-22.04
    container: espressif/idf:${{ matrix.idf_ver }}
    steps:
      - name: Checkout esp-protocols
        uses: actions/checkout@v4
        with:
          path: esp-protocols
      - name: Install Necessary Libs
        run: |
          apt-get update -y
          apt-get install -y libbsd-dev
      - name: Build ${{ matrix.example }} with IDF-${{ matrix.idf_ver }} for ${{ matrix.idf_target }}
        env:
          IDF_TARGET: ${{ matrix.idf_target }}
        shell: bash
        run: |
          . ${IDF_PATH}/export.sh
          cd $GITHUB_WORKSPACE/esp-protocols/components/mdns/tests/test_afl_fuzz_host/
          make INSTR=off
