v0.3.13 - Working on admin system
This commit is contained in:
@@ -117,7 +117,7 @@ generate_test_keypair() {
|
||||
|
||||
echo "$pubkey" > "$pubkey_file"
|
||||
|
||||
log_info "Generated keypair for $name: pubkey=${pubkey:0:16}..."
|
||||
log_info "Generated keypair for $name: pubkey=$pubkey"
|
||||
|
||||
# Export for use in calling functions
|
||||
eval "${name}_PRIVKEY=\"$privkey\""
|
||||
@@ -135,7 +135,9 @@ encrypt_nip44_content() {
|
||||
return 1
|
||||
fi
|
||||
|
||||
# log_info "DEBUG: Encrypting content: $content"
|
||||
log_info "DEBUG: About to encrypt content: '$content'" >&2
|
||||
log_info "DEBUG: Sender privkey: $sender_privkey" >&2
|
||||
log_info "DEBUG: Receiver pubkey: $receiver_pubkey" >&2
|
||||
|
||||
# Use nak to perform NIP-44 encryption with correct syntax:
|
||||
# nak encrypt --recipient-pubkey <pubkey> --sec <private_key> [plaintext]
|
||||
@@ -145,13 +147,24 @@ encrypt_nip44_content() {
|
||||
if [ $? -ne 0 ] || [ -z "$encrypted_content" ]; then
|
||||
log_error "Failed to encrypt content with NIP-44"
|
||||
log_error "Content: $content"
|
||||
log_error "Sender privkey: ${sender_privkey:0:16}..."
|
||||
log_error "Receiver pubkey: ${receiver_pubkey:0:16}..."
|
||||
log_error "Sender privkey: $sender_privkey"
|
||||
log_error "Receiver pubkey: $receiver_pubkey"
|
||||
return 1
|
||||
fi
|
||||
|
||||
# log_info "DEBUG: Encrypted content: $encrypted_content"
|
||||
# log_info "Successfully encrypted content with NIP-44"
|
||||
# Validate that encrypted content is valid base64 and doesn't contain problematic characters
|
||||
if ! echo "$encrypted_content" | grep -q '^[A-Za-z0-9+/]*=*$'; then
|
||||
log_error "Encrypted content contains invalid characters for JSON: $encrypted_content"
|
||||
return 1
|
||||
fi
|
||||
|
||||
# Check if encrypted content is valid UTF-8/base64
|
||||
if ! echo "$encrypted_content" | base64 -d >/dev/null 2>&1; then
|
||||
log_warning "Encrypted content may not be valid base64: $encrypted_content"
|
||||
fi
|
||||
|
||||
log_info "DEBUG: Encrypted content: $encrypted_content" >&2
|
||||
log_info "Successfully encrypted content with NIP-44" >&2
|
||||
echo "$encrypted_content"
|
||||
return 0
|
||||
}
|
||||
@@ -167,7 +180,7 @@ decrypt_nip44_content() {
|
||||
return 1
|
||||
fi
|
||||
|
||||
log_info "DEBUG: Decrypting content: ${encrypted_content:0:32}..."
|
||||
log_info "DEBUG: Decrypting content: $encrypted_content"
|
||||
|
||||
# Use nak to perform NIP-44 decryption with correct syntax:
|
||||
# nak decrypt --sender-pubkey <pubkey> --sec <private_key> [encrypted_content]
|
||||
@@ -176,9 +189,9 @@ decrypt_nip44_content() {
|
||||
|
||||
if [ $? -ne 0 ] || [ -z "$decrypted_content" ]; then
|
||||
log_error "Failed to decrypt content with NIP-44"
|
||||
log_error "Encrypted content: ${encrypted_content:0:32}..."
|
||||
log_error "Receiver privkey: ${receiver_privkey:0:16}..."
|
||||
log_error "Sender pubkey: ${sender_pubkey:0:16}..."
|
||||
log_error "Encrypted content: $encrypted_content"
|
||||
log_error "Receiver privkey: $receiver_privkey"
|
||||
log_error "Sender pubkey: $sender_pubkey"
|
||||
return 1
|
||||
fi
|
||||
|
||||
@@ -196,7 +209,7 @@ send_websocket_message() {
|
||||
# Use websocat to send message and capture response (following pattern from tests/1_nip_test.sh)
|
||||
local response=""
|
||||
if command -v websocat &> /dev/null; then
|
||||
response=$(echo "$message" | timeout "$timeout" websocat "$RELAY_URL" 2>&1 || echo "Connection failed")
|
||||
response=$(printf '%s\n' "$message" | timeout "$timeout" websocat "$RELAY_URL" 2>&1 || echo "Connection failed")
|
||||
|
||||
# Check if connection failed
|
||||
if [[ "$response" == *"Connection failed"* ]]; then
|
||||
@@ -220,22 +233,54 @@ send_admin_event() {
|
||||
local timeout_seconds="${3:-10}"
|
||||
|
||||
log_info "Sending admin event: $description"
|
||||
log_info "DEBUG: Full event JSON: $event_json"
|
||||
|
||||
# Create EVENT message
|
||||
local event_message="[\"EVENT\",$event_json]"
|
||||
log_info "DEBUG: Full EVENT message: $event_message"
|
||||
# Create EVENT message using jq to properly handle special characters
|
||||
local event_message
|
||||
event_message=$(jq -n --argjson event "$event_json" '["EVENT", $event]')
|
||||
|
||||
# Send event using websocat (following 1_nip_test.sh pattern)
|
||||
# Validate that the event message is valid UTF-8 (temporarily disabled for debugging)
|
||||
# if ! echo "$event_message" | iconv -f utf-8 -t utf-8 >/dev/null 2>&1; then
|
||||
# log_error "Event message contains invalid UTF-8 characters"
|
||||
# return 1
|
||||
# fi
|
||||
|
||||
# Use websocat to send event and capture OK response
|
||||
local response=""
|
||||
if command -v websocat &> /dev/null; then
|
||||
log_info "DEBUG: About to send to relay: $event_message"
|
||||
response=$(echo "$event_message" | timeout "$timeout_seconds" websocat "$RELAY_URL" 2>&1 || echo "Connection failed")
|
||||
log_info "Sending event using websocat..."
|
||||
|
||||
# Check if connection failed
|
||||
if [[ "$response" == *"Connection failed"* ]]; then
|
||||
# Debug: Show what we're sending
|
||||
log_info "DEBUG: Event message being sent: $event_message"
|
||||
|
||||
# Write to temporary file to avoid shell interpretation issues
|
||||
local temp_file="${TEMP_DIR}/event_message_$$"
|
||||
printf '%s\n' "$event_message" > "$temp_file"
|
||||
|
||||
# Send via websocat using file input with delay to receive response
|
||||
response=$(timeout "$timeout_seconds" sh -c "cat '$temp_file'; sleep 0.5" | websocat "$RELAY_URL" 2>&1)
|
||||
local websocat_exit_code=$?
|
||||
|
||||
# Clean up temp file
|
||||
rm -f "$temp_file"
|
||||
|
||||
log_info "DEBUG: Websocat exit code: $websocat_exit_code"
|
||||
log_info "DEBUG: Websocat response: $response"
|
||||
|
||||
# Check for specific websocat errors
|
||||
if [[ "$response" == *"UTF-8 failure"* ]]; then
|
||||
log_error "UTF-8 encoding error in event data for $description"
|
||||
log_error "Event message: $event_message"
|
||||
return 1
|
||||
elif [[ "$response" == *"Connection failed"* ]] || [[ "$response" == *"Connection refused"* ]] || [[ "$response" == *"timeout"* ]]; then
|
||||
log_error "Failed to connect to relay for $description"
|
||||
return 1
|
||||
elif [[ "$response" == *"error running"* ]]; then
|
||||
log_error "Websocat error for $description: $response"
|
||||
return 1
|
||||
elif [ $websocat_exit_code -eq 0 ]; then
|
||||
log_info "Event sent successfully via websocat"
|
||||
else
|
||||
log_warning "Websocat returned exit code $websocat_exit_code"
|
||||
fi
|
||||
|
||||
else
|
||||
@@ -254,8 +299,9 @@ send_admin_query() {
|
||||
|
||||
log_info "Sending admin query: $description"
|
||||
|
||||
# Create EVENT message
|
||||
local event_message="[\"EVENT\",$event_json]"
|
||||
# Create EVENT message using jq to properly handle special characters
|
||||
local event_message
|
||||
event_message=$(jq -n --argjson event "$event_json" '["EVENT", $event]')
|
||||
|
||||
# For queries, we need to also send a REQ to get the response
|
||||
local sub_id="admin_query_$(date +%s)"
|
||||
@@ -265,7 +311,7 @@ send_admin_query() {
|
||||
# Send query event and subscription in sequence
|
||||
local response=""
|
||||
if command -v websocat &> /dev/null; then
|
||||
response=$(echo -e "$event_message\n$req_message\n$close_message" | timeout "$timeout_seconds" websocat "$RELAY_URL" 2>&1 || echo "Connection failed")
|
||||
response=$(printf '%s\n%s\n%s\n' "$event_message" "$req_message" "$close_message" | timeout "$timeout_seconds" websocat "$RELAY_URL" 2>&1 || echo "Connection failed")
|
||||
|
||||
# Check if connection failed
|
||||
if [[ "$response" == *"Connection failed"* ]]; then
|
||||
@@ -321,7 +367,7 @@ send_auth_rule_event() {
|
||||
local pattern_value="$4" # actual pubkey or hash value
|
||||
local description="$5" # optional description
|
||||
|
||||
log_info "Creating auth rule event: $action $rule_type $pattern_type ${pattern_value:0:16}..."
|
||||
log_info "Creating auth rule event: $action $rule_type $pattern_type $pattern_value"
|
||||
|
||||
# Create command array according to README.md API specification
|
||||
# Format: ["blacklist", "pubkey", "abc123..."] or ["whitelist", "pubkey", "def456..."]
|
||||
@@ -430,7 +476,7 @@ test_event_publishing() {
|
||||
log_info "Testing event publishing: $description"
|
||||
|
||||
# Create a simple test event (kind 1 - text note) using nak like NIP-42 test
|
||||
local test_content="Test message from ${test_pubkey:0:16}... at $(date)"
|
||||
local test_content="Test message from $test_pubkey at $(date)"
|
||||
local test_event
|
||||
test_event=$(nak event -k 1 --content "$test_content" --sec "$test_privkey" 2>/dev/null)
|
||||
|
||||
@@ -442,7 +488,7 @@ test_event_publishing() {
|
||||
# Send the event using nak directly (more reliable than websocat)
|
||||
log_info "Publishing test event to relay..."
|
||||
local result
|
||||
result=$(echo "$test_event" | timeout 10s nak event "$RELAY_URL" 2>&1)
|
||||
result=$(printf '%s\n' "$test_event" | timeout 10s nak event "$RELAY_URL" 2>&1)
|
||||
local exit_code=$?
|
||||
|
||||
log_info "Event publishing result: $result"
|
||||
@@ -545,15 +591,17 @@ test_admin_authentication() {
|
||||
return
|
||||
fi
|
||||
|
||||
# Send admin event
|
||||
local message="[\"EVENT\",$config_event]"
|
||||
# Send admin event using the proper admin event function
|
||||
local response
|
||||
response=$(send_websocket_message "$message" 10)
|
||||
response=$(send_admin_event "$config_event" "admin authentication test")
|
||||
local exit_code=$?
|
||||
|
||||
if echo "$response" | grep -q '"OK".*true'; then
|
||||
log_info "Admin authentication result: $response"
|
||||
|
||||
if [ $exit_code -eq 0 ] && echo "$response" | grep -q '"OK".*true'; then
|
||||
pass_test "Admin authentication successful"
|
||||
else
|
||||
fail_test "Admin authentication failed: $response"
|
||||
fail_test "Admin authentication failed: $response (exit code: $exit_code)"
|
||||
fi
|
||||
}
|
||||
|
||||
@@ -786,14 +834,14 @@ test_hash_blacklist() {
|
||||
return
|
||||
fi
|
||||
|
||||
log_info "Testing hash blacklist with event ID: ${event_id:0:16}..."
|
||||
log_info "Testing hash blacklist with event ID: $event_id"
|
||||
|
||||
# Add the event ID to hash blacklist
|
||||
if send_auth_rule_event "add" "blacklist" "hash" "$event_id" "Test hash blacklist"; then
|
||||
# Try to publish the same event using nak - should be blocked
|
||||
log_info "Attempting to publish blacklisted event..."
|
||||
local result
|
||||
result=$(echo "$test_event" | timeout 10s nak event "$RELAY_URL" 2>&1)
|
||||
result=$(printf '%s\n' "$test_event" | timeout 10s nak event "$RELAY_URL" 2>&1)
|
||||
local exit_code=$?
|
||||
|
||||
if [ $exit_code -ne 0 ] || echo "$result" | grep -q -i "blocked\|denied\|rejected\|blacklist"; then
|
||||
@@ -922,7 +970,7 @@ run_all_tests() {
|
||||
|
||||
clear_all_auth_rules
|
||||
|
||||
# test_admin_authentication
|
||||
test_admin_authentication
|
||||
# test_auth_rules_storage_query
|
||||
# test_basic_whitelist
|
||||
# test_basic_blacklist
|
||||
@@ -980,7 +1028,7 @@ main() {
|
||||
echo ""
|
||||
|
||||
# Check if relay is running - using websocat like the working tests
|
||||
if ! echo '["REQ","connection_test",{}]' | timeout 5 websocat "$RELAY_URL" >/dev/null 2>&1; then
|
||||
if ! printf '%s\n' '["REQ","connection_test",{}]' | timeout 5 websocat "$RELAY_URL" >/dev/null 2>&1; then
|
||||
log_error "Cannot connect to relay at $RELAY_URL"
|
||||
log_error "Please ensure the C-Relay server is running in test mode"
|
||||
exit 1
|
||||
|
||||
Reference in New Issue
Block a user