357 lines
15 KiB
Bash
Executable File
357 lines
15 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
# Comprehensive C-Relay Test - Test event types and subscriptions
|
|
# Uses nak to generate and publish various event types, then tests subscriptions
|
|
|
|
set -e # Exit on any error
|
|
|
|
# Color constants
|
|
RED='\033[31m'
|
|
GREEN='\033[32m'
|
|
YELLOW='\033[33m'
|
|
BLUE='\033[34m'
|
|
BOLD='\033[1m'
|
|
RESET='\033[0m'
|
|
|
|
# Test configuration
|
|
RELAY_URL="ws://127.0.0.1:8888"
|
|
TEST_PRIVATE_KEY="nsec1j4c6269y9w0q2er2xjw8sv2ehyrtfxq3jwgdlxj6qfn8z4gjsq5qfvfk99"
|
|
|
|
# Print functions
|
|
print_header() {
|
|
echo -e "${BLUE}${BOLD}=== $1 ===${RESET}"
|
|
}
|
|
|
|
print_step() {
|
|
echo -e "${YELLOW}[STEP]${RESET} $1"
|
|
}
|
|
|
|
print_success() {
|
|
echo -e "${GREEN}✓${RESET} $1"
|
|
}
|
|
|
|
print_error() {
|
|
echo -e "${RED}✗${RESET} $1"
|
|
}
|
|
|
|
print_info() {
|
|
echo -e "${BLUE}[INFO]${RESET} $1"
|
|
}
|
|
|
|
print_warning() {
|
|
echo -e "${YELLOW}[WARNING]${RESET} $1"
|
|
}
|
|
|
|
# Global arrays to store event IDs for subscription tests
|
|
declare -a REGULAR_EVENT_IDS=()
|
|
declare -a REPLACEABLE_EVENT_IDS=()
|
|
declare -a EPHEMERAL_EVENT_IDS=()
|
|
declare -a ADDRESSABLE_EVENT_IDS=()
|
|
|
|
# Helper function to publish event and extract ID
|
|
publish_event() {
|
|
local event_json="$1"
|
|
local event_type="$2"
|
|
local description="$3"
|
|
|
|
# Extract event ID
|
|
local event_id=$(echo "$event_json" | jq -r '.id' 2>/dev/null)
|
|
if [[ "$event_id" == "null" || -z "$event_id" ]]; then
|
|
print_error "Could not extract event ID from $description"
|
|
return 1
|
|
fi
|
|
|
|
print_info "Publishing $description..."
|
|
|
|
# Create EVENT message in Nostr format
|
|
local event_message="[\"EVENT\",$event_json]"
|
|
|
|
# Publish to relay
|
|
local response=""
|
|
if command -v websocat &> /dev/null; then
|
|
response=$(echo "$event_message" | timeout 5s websocat "$RELAY_URL" 2>&1 || echo "Connection failed")
|
|
else
|
|
print_error "websocat not found - required for testing"
|
|
return 1
|
|
fi
|
|
|
|
|
|
# Check response
|
|
if [[ "$response" == *"Connection failed"* ]]; then
|
|
print_error "Failed to connect to relay for $description"
|
|
return 1
|
|
elif [[ "$response" == *"true"* ]]; then
|
|
print_success "$description uploaded (ID: ${event_id:0:16}...)"
|
|
|
|
# Store event ID in appropriate array
|
|
case "$event_type" in
|
|
"regular") REGULAR_EVENT_IDS+=("$event_id") ;;
|
|
"replaceable") REPLACEABLE_EVENT_IDS+=("$event_id") ;;
|
|
"ephemeral") EPHEMERAL_EVENT_IDS+=("$event_id") ;;
|
|
"addressable") ADDRESSABLE_EVENT_IDS+=("$event_id") ;;
|
|
esac
|
|
echo # Add blank line for readability
|
|
return 0
|
|
else
|
|
print_warning "$description might have failed: $response"
|
|
echo # Add blank line for readability
|
|
return 1
|
|
fi
|
|
}
|
|
|
|
# Helper function to publish invalid event and expect rejection
|
|
publish_invalid_event() {
|
|
local event_json="$1"
|
|
local description="$2"
|
|
local expected_error="$3"
|
|
|
|
print_info "Publishing invalid $description..."
|
|
|
|
# Create EVENT message in Nostr format
|
|
local event_message="[\"EVENT\",$event_json]"
|
|
|
|
# Publish to relay
|
|
local response=""
|
|
if command -v websocat &> /dev/null; then
|
|
response=$(echo "$event_message" | timeout 5s websocat "$RELAY_URL" 2>&1 || echo "Connection failed")
|
|
else
|
|
print_error "websocat not found - required for testing"
|
|
return 1
|
|
fi
|
|
|
|
# Check response - should contain "false" and error message
|
|
if [[ "$response" == *"Connection failed"* ]]; then
|
|
print_error "Failed to connect to relay for $description"
|
|
return 1
|
|
elif [[ "$response" == *"false"* ]]; then
|
|
# Extract error message
|
|
local error_msg=$(echo "$response" | grep -o '"[^"]*invalid[^"]*"' | head -1 | sed 's/"//g' 2>/dev/null || echo "rejected")
|
|
print_success "$description correctly rejected: $error_msg"
|
|
echo # Add blank line for readability
|
|
return 0
|
|
elif [[ "$response" == *"true"* ]]; then
|
|
print_error "$description was incorrectly accepted (should have been rejected)"
|
|
echo # Add blank line for readability
|
|
return 1
|
|
else
|
|
print_warning "$description response unclear: $response"
|
|
echo # Add blank line for readability
|
|
return 1
|
|
fi
|
|
}
|
|
|
|
# Test subscription with filters
|
|
test_subscription() {
|
|
local sub_id="$1"
|
|
local filter="$2"
|
|
local description="$3"
|
|
local expected_count="$4"
|
|
|
|
print_step "Testing subscription: $description"
|
|
|
|
# Create REQ message
|
|
local req_message="[\"REQ\",\"$sub_id\",$filter]"
|
|
|
|
print_info "Testing filter: $filter"
|
|
|
|
# Send subscription and collect events
|
|
local response=""
|
|
if command -v websocat &> /dev/null; then
|
|
response=$(echo -e "$req_message\n[\"CLOSE\",\"$sub_id\"]" | timeout 3s websocat "$RELAY_URL" 2>/dev/null || echo "")
|
|
fi
|
|
|
|
|
|
# Count EVENT responses (lines containing ["EVENT","sub_id",...])
|
|
local event_count=0
|
|
if [[ -n "$response" ]]; then
|
|
event_count=$(echo "$response" | grep -c "\"EVENT\"" 2>/dev/null || echo "0")
|
|
fi
|
|
|
|
if [[ "$expected_count" == "any" ]]; then
|
|
if [[ $event_count -gt 0 ]]; then
|
|
print_success "$description - Found $event_count events"
|
|
else
|
|
print_warning "$description - No events found"
|
|
fi
|
|
elif [[ $event_count -eq $expected_count ]]; then
|
|
print_success "$description - Found expected $event_count events"
|
|
else
|
|
print_warning "$description - Expected $expected_count events, found $event_count"
|
|
fi
|
|
|
|
# Show a few sample events for verification (first 2)
|
|
if [[ $event_count -gt 0 && "$description" == "All events" ]]; then
|
|
print_info "Sample events (first 2):"
|
|
echo "$response" | grep "\"EVENT\"" | head -2 | while IFS= read -r line; do
|
|
local event_content=$(echo "$line" | jq -r '.[2].content' 2>/dev/null || echo "N/A")
|
|
local event_kind=$(echo "$line" | jq -r '.[2].kind' 2>/dev/null || echo "N/A")
|
|
local event_id=$(echo "$line" | jq -r '.[2].id' 2>/dev/null || echo "N/A")
|
|
echo " - ID: ${event_id:0:16}... Kind: $event_kind Content: ${event_content:0:30}..."
|
|
done
|
|
fi
|
|
|
|
echo # Add blank line for readability
|
|
return 0
|
|
}
|
|
|
|
# Main test function
|
|
run_comprehensive_test() {
|
|
print_header "C-Relay Comprehensive Test"
|
|
|
|
# Check dependencies
|
|
print_step "Checking dependencies..."
|
|
if ! command -v nak &> /dev/null; then
|
|
print_error "nak command not found"
|
|
print_info "Please install nak: go install github.com/fiatjaf/nak@latest"
|
|
return 1
|
|
fi
|
|
if ! command -v websocat &> /dev/null; then
|
|
print_error "websocat command not found"
|
|
print_info "Please install websocat for testing"
|
|
return 1
|
|
fi
|
|
if ! command -v jq &> /dev/null; then
|
|
print_error "jq command not found"
|
|
print_info "Please install jq for JSON processing"
|
|
return 1
|
|
fi
|
|
print_success "All dependencies found"
|
|
|
|
print_header "PHASE 1: Publishing Various Event Types"
|
|
|
|
# Test 1: Regular Events (kind 1)
|
|
print_step "Creating regular events (kind 1)..."
|
|
local regular1=$(nak event --sec "$TEST_PRIVATE_KEY" -c "Regular event #1" -k 1 --ts $(($(date +%s) - 100)) -t "type=regular" -t "test=phase1" 2>/dev/null)
|
|
local regular2=$(nak event --sec "$TEST_PRIVATE_KEY" -c "Regular event #2 with tags" -k 1 --ts $(($(date +%s) - 90)) -e "previous_event_id" -p "test_pubkey" -t "type=regular" -t "test=phase1" 2>/dev/null)
|
|
|
|
publish_event "$regular1" "regular" "Regular event #1"
|
|
publish_event "$regular2" "regular" "Regular event #2"
|
|
|
|
# Test 2: Replaceable Events (kind 0 - metadata)
|
|
print_step "Creating replaceable events (kind 0)..."
|
|
local replaceable1=$(nak event --sec "$TEST_PRIVATE_KEY" -c '{"name":"Test User","about":"Testing C-Relay"}' -k 0 --ts $(($(date +%s) - 80)) -t "type=replaceable" 2>/dev/null)
|
|
local replaceable2=$(nak event --sec "$TEST_PRIVATE_KEY" -c '{"name":"Test User Updated","about":"Updated profile"}' -k 0 --ts $(($(date +%s) - 70)) -t "type=replaceable" 2>/dev/null)
|
|
|
|
publish_event "$replaceable1" "replaceable" "Replaceable event #1 (metadata)"
|
|
publish_event "$replaceable2" "replaceable" "Replaceable event #2 (metadata update)"
|
|
|
|
# Test 3: Ephemeral Events (kind 20000+)
|
|
print_step "Creating ephemeral events (kind 20001)..."
|
|
local ephemeral1=$(nak event --sec "$TEST_PRIVATE_KEY" -c "Ephemeral event - should not be stored permanently" -k 20001 --ts $(date +%s) -t "type=ephemeral" 2>/dev/null)
|
|
|
|
publish_event "$ephemeral1" "ephemeral" "Ephemeral event"
|
|
|
|
# Test 4: Addressable Events (kind 30000+)
|
|
print_step "Creating addressable events (kind 30001)..."
|
|
local addressable1=$(nak event --sec "$TEST_PRIVATE_KEY" -c "Addressable event with d-tag" -k 30001 --ts $(($(date +%s) - 50)) -t "d=test-article" -t "type=addressable" 2>/dev/null)
|
|
local addressable2=$(nak event --sec "$TEST_PRIVATE_KEY" -c "Updated addressable event" -k 30001 --ts $(($(date +%s) - 40)) -t "d=test-article" -t "type=addressable" -t "updated=true" 2>/dev/null)
|
|
|
|
publish_event "$addressable1" "addressable" "Addressable event #1"
|
|
publish_event "$addressable2" "addressable" "Addressable event #2 (update)"
|
|
|
|
# Brief pause to let events settle
|
|
sleep 2
|
|
|
|
print_header "PHASE 2: Testing Invalid Events (NIP-01 Validation)"
|
|
|
|
print_step "Testing various invalid events that should be rejected..."
|
|
|
|
# Test 1: Event with invalid JSON structure (malformed)
|
|
local malformed_event='{"id":"invalid","pubkey":"invalid_pubkey","created_at":"not_a_number","kind":1,"tags":[],"content":"test"}'
|
|
publish_invalid_event "$malformed_event" "malformed event with invalid created_at" "invalid"
|
|
|
|
# Test 2: Event with missing required fields
|
|
local missing_field_event='{"id":"test123","pubkey":"valid_pubkey","kind":1,"tags":[],"content":"test"}'
|
|
publish_invalid_event "$missing_field_event" "event missing created_at and sig" "invalid"
|
|
|
|
# Test 3: Event with invalid pubkey format (not hex)
|
|
local invalid_pubkey_event='{"id":"abc123","pubkey":"not_valid_hex_pubkey","created_at":1234567890,"kind":1,"tags":[],"content":"test","sig":"fake_sig"}'
|
|
publish_invalid_event "$invalid_pubkey_event" "event with invalid pubkey format" "invalid"
|
|
|
|
# Test 4: Event with invalid event ID format
|
|
local invalid_id_event='{"id":"not_64_char_hex","pubkey":"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef","created_at":1234567890,"kind":1,"tags":[],"content":"test","sig":"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"}'
|
|
publish_invalid_event "$invalid_id_event" "event with invalid ID format" "invalid"
|
|
|
|
# Test 5: Event with invalid signature
|
|
local invalid_sig_event='{"id":"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef","pubkey":"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef","created_at":1234567890,"kind":1,"tags":[],"content":"test","sig":"invalid_signature_format"}'
|
|
publish_invalid_event "$invalid_sig_event" "event with invalid signature format" "invalid"
|
|
|
|
# Test 6: Event with invalid kind (negative)
|
|
local invalid_kind_event='{"id":"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef","pubkey":"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef","created_at":1234567890,"kind":-1,"tags":[],"content":"test","sig":"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"}'
|
|
publish_invalid_event "$invalid_kind_event" "event with negative kind" "invalid"
|
|
|
|
# Test 7: Event with invalid tags format (not array)
|
|
local invalid_tags_event='{"id":"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef","pubkey":"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef","created_at":1234567890,"kind":1,"tags":"not_an_array","content":"test","sig":"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"}'
|
|
publish_invalid_event "$invalid_tags_event" "event with invalid tags format" "invalid"
|
|
|
|
print_success "Invalid event tests completed - all should have been rejected"
|
|
|
|
print_header "PHASE 3: Testing Subscriptions and Filters"
|
|
|
|
# Test subscription filters
|
|
print_step "Testing various subscription filters..."
|
|
|
|
# Test 1: Get all events
|
|
test_subscription "test_all" '{}' "All events" "any"
|
|
|
|
# Test 2: Get events by kind
|
|
test_subscription "test_kind1" '{"kinds":[1]}' "Kind 1 events only" "2"
|
|
test_subscription "test_kind0" '{"kinds":[0]}' "Kind 0 events only" "any"
|
|
|
|
# Test 3: Get events by author (pubkey)
|
|
local test_pubkey=$(echo "$regular1" | jq -r '.pubkey' 2>/dev/null)
|
|
test_subscription "test_author" "{\"authors\":[\"$test_pubkey\"]}" "Events by specific author" "any"
|
|
|
|
# Test 4: Get recent events (time-based)
|
|
local recent_timestamp=$(($(date +%s) - 200))
|
|
test_subscription "test_recent" "{\"since\":$recent_timestamp}" "Recent events" "any"
|
|
|
|
# Test 5: Get events with specific tags
|
|
test_subscription "test_tag_type" '{"#type":["regular"]}' "Events with type=regular tag" "any"
|
|
|
|
# Test 6: Multiple kinds
|
|
test_subscription "test_multi_kinds" '{"kinds":[0,1]}' "Multiple kinds (0,1)" "any"
|
|
|
|
# Test 7: Limit results
|
|
test_subscription "test_limit" '{"kinds":[1],"limit":1}' "Limited to 1 event" "1"
|
|
|
|
print_header "PHASE 4: Database Verification"
|
|
|
|
# Check what's actually stored in the database
|
|
print_step "Verifying database contents..."
|
|
|
|
if command -v sqlite3 &> /dev/null; then
|
|
print_info "Events by type in database:"
|
|
sqlite3 db/c_nostr_relay.db "SELECT event_type, COUNT(*) as count FROM events GROUP BY event_type;" | while read line; do
|
|
echo " $line"
|
|
done
|
|
|
|
print_info "Recent events in database:"
|
|
sqlite3 db/c_nostr_relay.db "SELECT substr(id, 1, 16) || '...' as short_id, event_type, kind, substr(content, 1, 30) || '...' as short_content FROM events ORDER BY created_at DESC LIMIT 5;" | while read line; do
|
|
echo " $line"
|
|
done
|
|
|
|
print_success "Database verification complete"
|
|
else
|
|
print_warning "sqlite3 not available for database verification"
|
|
fi
|
|
|
|
return 0
|
|
}
|
|
|
|
# Run the comprehensive test
|
|
print_header "Starting C-Relay Comprehensive Test Suite with NIP-01 Validation"
|
|
echo
|
|
|
|
if run_comprehensive_test; then
|
|
echo
|
|
print_success "All tests completed successfully!"
|
|
print_info "The C-Relay with full NIP-01 validation is working correctly"
|
|
print_info "✅ Event validation, signature verification, and error handling all working"
|
|
echo
|
|
exit 0
|
|
else
|
|
echo
|
|
print_error "Some tests failed"
|
|
exit 1
|
|
fi |