#!/bin/bash # Test Script for Router Enrollment # This script helps test the enrollment process without actually enrolling a router set -euo pipefail # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' # No Color # Test configuration TEST_API_ENDPOINT="https://your-zitinexus-portal.com/api" TEST_HASH_KEY="a1b2c3d4e5f6789012345678901234567890abcd" log() { local level=$1 shift local message="$*" case $level in "ERROR") echo -e "${RED}[ERROR]${NC} $message" >&2 ;; "SUCCESS") echo -e "${GREEN}[SUCCESS]${NC} $message" ;; "WARNING") echo -e "${YELLOW}[WARNING]${NC} $message" ;; "INFO") echo -e "${BLUE}[INFO]${NC} $message" ;; *) echo "$message" ;; esac } # Test API connectivity test_api_connectivity() { log "INFO" "Testing API connectivity..." local api_endpoint read -p "Enter API endpoint to test [$TEST_API_ENDPOINT]: " api_endpoint api_endpoint="${api_endpoint:-$TEST_API_ENDPOINT}" # Test health endpoint local health_url="${api_endpoint}/router/health" log "INFO" "Testing health endpoint: $health_url" local response=$(curl -s -w "%{http_code}" -o /dev/null "$health_url" 2>/dev/null || echo "000") if [[ "$response" == "200" ]]; then log "SUCCESS" "API health endpoint is accessible" else log "ERROR" "API health endpoint returned HTTP $response" return 1 fi } # Test hash key format validation test_hash_key_validation() { log "INFO" "Testing hash key format validation..." local test_cases=( "a1b2c3d4e5f6789012345678901234567890abcd:VALID" "A1B2C3D4E5F6789012345678901234567890ABCD:VALID" "short:INVALID" "toolongtobeavalidhashkeyfortesting123456789:INVALID" "g1h2i3j4k5l6789012345678901234567890xyz:INVALID" "a1b2c3d4e5f6789012345678901234567890abc:INVALID" ) for test_case in "${test_cases[@]}"; do local hash_key="${test_case%:*}" local expected="${test_case#*:}" if [[ "$hash_key" =~ ^[a-fA-F0-9]{32}$ ]]; then local result="VALID" else local result="INVALID" fi if [[ "$result" == "$expected" ]]; then log "SUCCESS" "Hash key validation: '$hash_key' -> $result ✓" else log "ERROR" "Hash key validation: '$hash_key' -> $result (expected $expected) ✗" fi done } # Test API registration call (dry run) test_api_registration() { log "INFO" "Testing API registration call (dry run)..." local api_endpoint read -p "Enter API endpoint [$TEST_API_ENDPOINT]: " api_endpoint api_endpoint="${api_endpoint:-$TEST_API_ENDPOINT}" local hash_key read -p "Enter test hash key [$TEST_HASH_KEY]: " hash_key hash_key="${hash_key:-$TEST_HASH_KEY}" # Validate hash key format if [[ ! "$hash_key" =~ ^[a-fA-F0-9]{32}$ ]]; then log "ERROR" "Invalid hash key format" return 1 fi local api_url="${api_endpoint}/router/register" local payload="{\"hashKey\":\"$hash_key\"}" log "INFO" "Making API call to: $api_url" log "INFO" "Payload: $payload" local response_file=$(mktemp) local http_code http_code=$(curl -s -w "%{http_code}" -o "$response_file" \ -X POST \ -H "Content-Type: application/json" \ -H "User-Agent: ZitiRouter-TestScript/1.0.0" \ -d "$payload" \ --connect-timeout 30 \ --max-time 60 \ "$api_url" 2>/dev/null || echo "000") log "INFO" "HTTP Response Code: $http_code" if [[ -f "$response_file" ]]; then log "INFO" "Response body:" if command -v jq &> /dev/null; then jq '.' "$response_file" 2>/dev/null || cat "$response_file" else cat "$response_file" fi fi rm -f "$response_file" case $http_code in "200") log "SUCCESS" "API call successful" ;; "400") log "WARNING" "Bad request - check hash key validity" ;; "404") log "ERROR" "Hash key not found or endpoint not available" ;; "429") log "WARNING" "Rate limited - try again later" ;; "000") log "ERROR" "Connection failed - check network connectivity" ;; *) log "ERROR" "Unexpected response code: $http_code" ;; esac } # Test system requirements test_system_requirements() { log "INFO" "Testing system requirements..." # Check if running as root if [[ $EUID -eq 0 ]]; then log "SUCCESS" "Running as root ✓" else log "WARNING" "Not running as root (enrollment script requires sudo)" fi # Check curl if command -v curl &> /dev/null; then local curl_version=$(curl --version | head -n1) log "SUCCESS" "curl available: $curl_version ✓" else log "ERROR" "curl not found ✗" fi # Check jq if command -v jq &> /dev/null; then local jq_version=$(jq --version) log "SUCCESS" "jq available: $jq_version ✓" else log "WARNING" "jq not found (will be installed by enrollment script)" fi # Check systemctl if command -v systemctl &> /dev/null; then log "SUCCESS" "systemctl available ✓" else log "ERROR" "systemctl not found ✗" fi # Check OpenZiti CLI if command -v ziti &> /dev/null; then local ziti_version=$(ziti version 2>/dev/null | head -n1 || echo "unknown") log "SUCCESS" "OpenZiti CLI available: $ziti_version ✓" else log "INFO" "OpenZiti CLI not found (will be installed by enrollment script)" fi # Check internet connectivity if curl -s --connect-timeout 5 https://get.openziti.io >/dev/null 2>&1; then log "SUCCESS" "Internet connectivity ✓" else log "ERROR" "No internet connectivity ✗" fi } # Test directory permissions test_directory_permissions() { log "INFO" "Testing directory permissions..." local test_dirs=( "/etc" "/var/log" "/etc/systemd/system" ) for dir in "${test_dirs[@]}"; do if [[ -d "$dir" ]]; then if [[ -w "$dir" ]]; then log "SUCCESS" "$dir is writable ✓" else if [[ $EUID -eq 0 ]]; then log "ERROR" "$dir is not writable even as root ✗" else log "WARNING" "$dir is not writable (need root access)" fi fi else log "ERROR" "$dir does not exist ✗" fi done } # Main menu show_menu() { echo echo "==============================================" echo " Router Enrollment Test Script" echo "==============================================" echo echo "1. Test API Connectivity" echo "2. Test Hash Key Validation" echo "3. Test API Registration Call" echo "4. Test System Requirements" echo "5. Test Directory Permissions" echo "6. Run All Tests" echo "7. Exit" echo } # Run all tests run_all_tests() { log "INFO" "Running all tests..." echo test_system_requirements echo test_directory_permissions echo test_hash_key_validation echo test_api_connectivity echo log "INFO" "All tests completed" } # Main execution main() { while true; do show_menu read -p "Select an option (1-7): " choice case $choice in 1) test_api_connectivity ;; 2) test_hash_key_validation ;; 3) test_api_registration ;; 4) test_system_requirements ;; 5) test_directory_permissions ;; 6) run_all_tests ;; 7) log "INFO" "Exiting..." exit 0 ;; *) log "ERROR" "Invalid option. Please select 1-7." ;; esac echo read -p "Press Enter to continue..." done } # Run main function main "$@"