name: Fuzz

on:
  push:
    branches: ['main']
  pull_request:
  # Allow manual trigger
  workflow_dispatch:
  # Run fuzz tests daily
  schedule:
    - cron: '0 0 * * *'

permissions:
  contents: read

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  fuzz:
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        target:
          - fuzz_dtls_packet
          - fuzz_record_parse
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@nightly
      - name: Install cargo-fuzz
        run: cargo install cargo-fuzz
      - name: Cache fuzz corpus
        uses: actions/cache@v4
        with:
          path: fuzz/corpus
          key: fuzz-corpus-${{ matrix.target }}-${{ github.sha }}
          restore-keys: |
            fuzz-corpus-${{ matrix.target }}-
      - name: Run fuzz target
        run: |
          cargo fuzz run ${{ matrix.target }} -- \
            -max_total_time=60 \
            -max_len=65536
      - name: Upload crash artifacts
        if: failure()
        uses: actions/upload-artifact@v4
        with:
          name: crash-${{ matrix.target }}
          path: fuzz/artifacts/${{ matrix.target }}/
