stem-rs 1.1.2

A complete Rust library for Tor control protocol — build privacy-focused applications with type-safe, async-first APIs
Documentation
name: Tests

on:
  push:
  pull_request:
  workflow_dispatch:
  workflow_call:

env:
  CARGO_TERM_COLOR: always
  TOR_VERSION: "14.0.4"
  RUST_BACKTRACE: 1

jobs:
  unit-tests:
    name: Unit Tests
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      
      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@stable
        with:
          components: clippy, rustfmt
      
      - name: Cache cargo registry
        uses: actions/cache@v4
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
            target
          key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
          restore-keys: |
            ${{ runner.os }}-cargo-
      
      - name: Run clippy
        run: cargo clippy --all-targets --all-features -- -D warnings
      
      - name: Run unit tests
        run: cargo test --lib --verbose
      
      - name: Run doc tests
        run: cargo test --doc --verbose

  integration-tests:
    name: Integration Tests
    runs-on: ubuntu-latest
    needs: unit-tests
    steps:
      - uses: actions/checkout@v4
      
      - name: Install system dependencies
        run: sudo apt-get update && sudo apt-get install -y libevent-2.1-7
      
      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@stable
      
      - name: Cache cargo registry
        uses: actions/cache@v4
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
            target
          key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
          restore-keys: |
            ${{ runner.os }}-cargo-
      
      - name: Download and extract Tor Expert Bundle
        run: |
          mkdir -p tor-bundle
          cd tor-bundle
          curl -fsSL -o tor-expert-bundle.tar.gz \
            "https://archive.torproject.org/tor-package-archive/torbrowser/${{ env.TOR_VERSION }}/tor-expert-bundle-linux-x86_64-${{ env.TOR_VERSION }}.tar.gz"
          tar -xzf tor-expert-bundle.tar.gz
          chmod +x tor/tor
          mkdir -p tor/data
      
      - name: Create Tor configuration
        run: |
          cat > tor-bundle/torrc << 'EOF'
          ControlPort 9051
          CookieAuthentication 1
          CookieAuthFile ${{ github.workspace }}/tor-bundle/tor/control_auth_cookie
          DataDirectory ${{ github.workspace }}/tor-bundle/tor/data
          Log notice file ${{ github.workspace }}/tor-bundle/tor/tor.log
          SocksPort 9050
          DisableNetwork 0
          EOF
      
      - name: Start Tor in background
        run: |
          ./tor-bundle/tor/tor -f tor-bundle/torrc &
          echo $! > tor.pid
          echo "Tor PID: $(cat tor.pid)"
      
      - name: Wait for Tor bootstrap
        run: |
          echo "Waiting for Tor to bootstrap..."
          for i in {1..60}; do
            if [ -f tor-bundle/tor/control_auth_cookie ]; then
              echo "Cookie file found after ${i} seconds"
              break
            fi
            sleep 1
          done
          
          for i in {1..360}; do
            if grep -q "Bootstrapped 100%" tor-bundle/tor/tor.log 2>/dev/null; then
              echo "Tor bootstrapped successfully after ${i} seconds"
              break
            fi
            if [ $i -eq 120 ]; then
              echo "Tor bootstrap timeout"
              cat tor-bundle/tor/tor.log || true
              exit 1
            fi
            sleep 1
          done
      
      - name: Run integration tests
        env:
          TOR_CONTROL_PORT: 9051
          TOR_COOKIE_PATH: ${{ github.workspace }}/tor-bundle/tor/control_auth_cookie
          TOR_SOCKS_PORT: 9050
        run: cargo test --features integration --verbose -- --test-threads=1
      
      - name: Stop Tor
        if: always()
        run: |
          if [ -f tor.pid ]; then
            kill $(cat tor.pid) 2>/dev/null || true
            rm tor.pid
          fi
      
      - name: Upload Tor logs on failure
        if: failure()
        uses: actions/upload-artifact@v4
        with:
          name: tor-logs
          path: |
            tor-bundle/tor/tor.log
            tor-bundle/tor/data/