#!/bin/bash # UPnP IGD Integration Test # # This test verifies that UPnP IGD works correctly for: # 1. SSDP multicast discovery of the gateway # 2. SOAP GetExternalIPAddress # 3. SOAP AddPortMapping # 4. Full MWP address discovery through UPnP # # NAT-PMP is DISABLED on the router so the mwp-demo falls through to UPnP. set -e cd "$(dirname "$0")" echo "==========================================" echo " UPnP IGD Integration Test" echo "==========================================" # Cleanup function cleanup() { echo "" echo "Cleaning up..." docker compose down -v --remove-orphans 2>/dev/null || true } trap cleanup EXIT # Build and start echo "" echo "[1/4] Building containers..." docker compose build echo "" echo "[2/4] Starting NAT router (UPnP enabled, NAT-PMP disabled)..." docker compose up -d router sleep 3 # Check router is running echo "" echo "[3/4] Verifying UPnP daemon..." docker compose exec router pgrep miniupnpd > /dev/null || { echo "ERROR: miniupnpd not running!" docker compose logs router exit 1 } echo "miniupnpd is running (UPnP mode)" # Show router state echo "" echo "Router NAT table:" docker compose exec router iptables-legacy -t nat -L -n | head -20 echo "" echo "[4/4] Running peer tests..." # Start peer-a in background echo "" echo "--- Starting peer-a (alice) ---" docker compose run -d --name test-peer-a peer-a /test.sh --connect-to bob sleep 2 # Get alice's DHT ID from logs ALICE_DHT=$(docker logs test-peer-a 2>&1 | grep -o 'DHT Node ID: [0-9a-f]*' | head -1 | cut -d' ' -f4) if [ -z "$ALICE_DHT" ]; then echo "ERROR: Could not get Alice's DHT ID" docker logs test-peer-a exit 1 fi echo "Alice DHT ID: $ALICE_DHT" # Check if UPnP worked for alice echo "" echo "Alice's UPnP status:" docker logs test-peer-a 2>&1 | grep -i "upnp" || echo "(no UPnP messages yet)" # Start peer-b connecting to alice echo "" echo "--- Starting peer-b (bob) ---" docker compose run --name test-peer-b peer-b /test.sh \ --connect-to alice \ --bootstrap 10.100.0.10:18100 \ --dht-id "$ALICE_DHT" & # Wait for connection echo "" echo "Waiting for MWP connection..." sleep 15 # Check results echo "" echo "==========================================" echo " Test Results" echo "==========================================" echo "" echo "--- Alice logs ---" docker logs test-peer-a 2>&1 | tail -30 echo "" echo "--- Bob logs ---" docker logs test-peer-b 2>&1 | tail -30 # Check for success indicators ALICE_UPNP=$(docker logs test-peer-a 2>&1 | grep -c "UPnP" || true) BOB_UPNP=$(docker logs test-peer-b 2>&1 | grep -c "UPnP" || true) ALICE_NATPMP=$(docker logs test-peer-a 2>&1 | grep -c "Using NAT-PMP:" || true) BOB_NATPMP=$(docker logs test-peer-b 2>&1 | grep -c "Using NAT-PMP:" || true) CONNECTION=$(docker logs test-peer-b 2>&1 | grep -c "CONNECTION ESTABLISHED" || true) echo "" echo "==========================================" echo " Summary" echo "==========================================" echo "Alice UPnP messages: $ALICE_UPNP" echo "Bob UPnP messages: $BOB_UPNP" echo "Alice NAT-PMP used: $([ $ALICE_NATPMP -gt 0 ] && echo 'YES (unexpected!)' || echo 'NO (correct)')" echo "Bob NAT-PMP used: $([ $BOB_NATPMP -gt 0 ] && echo 'YES (unexpected!)' || echo 'NO (correct)')" echo "Connection established: $([ $CONNECTION -gt 0 ] && echo 'YES' || echo 'NO')" # Check for UPnP external address discovery if docker logs test-peer-a 2>&1 | grep -q "Using UPnP IGD:"; then echo "" echo "SUCCESS: UPnP IGD address discovery working!" EXTERNAL=$(docker logs test-peer-a 2>&1 | grep "Using UPnP IGD:" | head -1) echo "$EXTERNAL" else echo "" echo "NOTE: UPnP IGD address discovery not confirmed in mwp-demo output" # Check lower-level UPnP messages if docker logs test-peer-a 2>&1 | grep -q "UPnP: External address is"; then echo "But UPnP SOAP GetExternalIPAddress succeeded:" docker logs test-peer-a 2>&1 | grep "UPnP: External address is" | head -1 fi fi # Verify NAT-PMP was NOT used (since it's disabled) if [ $ALICE_NATPMP -gt 0 ] || [ $BOB_NATPMP -gt 0 ]; then echo "" echo "WARNING: NAT-PMP was used despite being disabled on the router!" echo "This suggests the NAT-PMP disable config is not working correctly." fi echo "" # Final pass/fail ALICE_CONNECTED=$(docker logs test-peer-a 2>&1 | grep -c "CONNECTION ESTABLISHED" || true) if [ $ALICE_CONNECTED -gt 0 ] && [ $CONNECTION -gt 0 ]; then echo "PASS: Both peers established connection through NAT" elif [ $ALICE_CONNECTED -gt 0 ] || [ $CONNECTION -gt 0 ]; then echo "PASS: At least one peer established connection through NAT" else echo "FAIL: No connection established" exit 1 fi