Browse Source
- Add .generated directory for test artifacts - Save key derivation data to key_derivation.json - Save account initialization to account_init.json - Save registration data to registration.json - Save claim details to claim_details.json - Save test environment to test-env.sh - Save contacts data to contacts.json - Add proper error handling for file operations - Improve deeplink test flow with JSON-based data - Add color output and better status messages - Add ADB device detection and fallback to print mode Technical Changes: - Add file system operations with proper error handling - Standardize JSON output format across Python/TypeScript - Update test flow to use generated JSON files - Add proper typing for registration response - Improve error reporting and debug output This improves the test workflow by saving all intermediate data as JSON files that can be used by other test scripts. The deeplink testing now uses this data instead of environment variables for better reliability.pull/127/head
3 changed files with 398 additions and 174 deletions
@ -1,175 +1,138 @@ |
|||||
#!/bin/bash |
#!/bin/bash |
||||
|
|
||||
|
# File header documentation |
||||
|
# @file run-deeplink-tests.sh |
||||
|
# @brief Automated deeplink testing script using generated JSON files and ADB |
||||
|
# @author Matthew Raymer |
||||
|
# @date $(date +%Y-%m-%d) |
||||
|
|
||||
|
# Color definitions |
||||
|
BLUE='\033[0;34m' |
||||
|
GREEN='\033[0;32m' |
||||
|
RED='\033[0;31m' |
||||
|
YELLOW='\033[1;33m' |
||||
|
NC='\033[0m' |
||||
|
|
||||
# Parse command line arguments |
# Parse command line arguments |
||||
GENERATE_ONLY=false |
TIMEOUT=5 |
||||
ALL_TESTS=false |
TEST_MODE="execute" # Default to execute mode |
||||
while getopts "ga" opt; do |
|
||||
|
while getopts "t:p" opt; do |
||||
case $opt in |
case $opt in |
||||
g) GENERATE_ONLY=true ;; |
t) TIMEOUT=$OPTARG ;; |
||||
a) ALL_TESTS=true ;; |
p) TEST_MODE="print" ;; |
||||
\?) echo "Invalid option: -$OPTARG" >&2; exit 1 ;; |
\?) echo "Invalid option: -$OPTARG" >&2; exit 1 ;; |
||||
esac |
esac |
||||
done |
done |
||||
|
|
||||
# Directory for storing generated DIDs |
# Verify .generated directory exists |
||||
mkdir -p .generated |
if [ ! -d .generated ]; then |
||||
|
echo -e "${RED}Error: .generated directory not found${NC}" |
||||
|
echo "Please run the DID generation script first" |
||||
|
exit 1 |
||||
|
fi |
||||
|
|
||||
# Function to validate environment variables |
# Function to read and validate JSON files |
||||
validate_env() { |
validate_json_files() { |
||||
local missing=false |
local required_files=("test-env.sh" "claim_details.json" "contacts.json") |
||||
for var in CONTACT1_DID CONTACT1_KEY CONTACT2_DID CONTACT2_KEY ISSUER_DID ISSUER_KEY; do |
for file in "${required_files[@]}"; do |
||||
if [ -z "${!var}" ]; then |
if [ ! -f ".generated/$file" ]; then |
||||
echo "Error: $var is not set" >&2 |
echo -e "${RED}Error: Missing required file .generated/$file${NC}" |
||||
missing=true |
return 1 |
||||
fi |
fi |
||||
done |
done |
||||
if [ "$missing" = true ]; then |
|
||||
rm -f .generated/test-env.sh # Remove invalid env file |
|
||||
echo "Please regenerate DIDs by running:" >&2 |
|
||||
echo "./test-scripts/run-deeplink-tests.sh -g" >&2 |
|
||||
return 1 |
|
||||
fi |
|
||||
return 0 |
return 0 |
||||
} |
} |
||||
|
|
||||
# Check if we already have generated DIDs |
# Function to URL encode string |
||||
if [ -f .generated/test-env.sh ] && [ "$GENERATE_ONLY" = false ]; then |
urlencode() { |
||||
echo "Using existing DIDs from .generated/test-env.sh" |
local string="${1}" |
||||
source .generated/test-env.sh |
local strlen=${#string} |
||||
validate_env || exit 1 |
local encoded="" |
||||
else |
local pos c o |
||||
# Function to extract DID info from did_generator.sh output |
|
||||
extract_did_info() { |
for (( pos=0 ; pos<strlen ; pos++ )); do |
||||
local output="$1" |
c=${string:$pos:1} |
||||
|
case "$c" in |
||||
# Debug: Show what we received |
[-_.~a-zA-Z0-9] ) o="${c}" ;; |
||||
echo "DEBUG: extract_did_info received: '$output'" >&2 |
* ) printf -v o '%%%02x' "'$c" |
||||
|
esac |
||||
# Parse and validate JSON |
encoded+="${o}" |
||||
if ! printf '%s' "$output" | jq -e 'if type == "object" and .status == "success" then true else false end' >/dev/null 2>&1; then |
done |
||||
echo "Error: DID generation failed" >&2 |
echo "${encoded}" |
||||
echo "Error details:" >&2 |
} |
||||
if ! printf '%s' "$output" | jq -e . >/dev/null 2>&1; then |
|
||||
echo "Invalid JSON output: $output" >&2 |
# Function to execute or print deeplink |
||||
else |
execute_deeplink() { |
||||
printf '%s' "$output" | jq -r '"\(.stage): \(.error // "Unknown error")"' >&2 |
local url="$1" |
||||
fi |
local description="$2" |
||||
|
local encoded_url=$(urlencode "$url") |
||||
|
|
||||
|
echo -e "\n${BLUE}Testing: $description${NC}" |
||||
|
|
||||
|
if [ "$TEST_MODE" = "print" ]; then |
||||
|
echo -e "${YELLOW}Deep Link URL:${NC}" |
||||
|
echo "$url" |
||||
|
echo -e "${YELLOW}Encoded URL:${NC}" |
||||
|
echo "$encoded_url" |
||||
|
echo "---" |
||||
|
else |
||||
|
echo "URL: $url" |
||||
|
if adb shell am start -a android.intent.action.VIEW -d "$encoded_url"; then |
||||
|
echo -e "${GREEN}Success: Deeplink executed${NC}" |
||||
|
sleep "$TIMEOUT" |
||||
|
else |
||||
|
echo -e "${RED}Error: Failed to execute deeplink${NC}" |
||||
return 1 |
return 1 |
||||
fi |
fi |
||||
|
fi |
||||
# Return the successful JSON |
} |
||||
printf '%s' "$output" |
|
||||
} |
|
||||
|
|
||||
echo "Generating first contact DID..." |
# Main test sequence |
||||
# Debug: Show raw command output |
main() { |
||||
DEBUG_OUTPUT=$(DEBUG=0 ./test-scripts/did_generator.sh) |
# Validate environment and files |
||||
GEN_STATUS=$? |
if ! validate_json_files; then |
||||
echo "DEBUG: Raw did_generator.sh output: '$DEBUG_OUTPUT'" >&2 |
|
||||
|
|
||||
if [ $GEN_STATUS -ne 0 ]; then |
|
||||
echo "Error: did_generator.sh failed with status $GEN_STATUS" >&2 |
|
||||
exit 1 |
exit 1 |
||||
fi |
fi |
||||
|
|
||||
CONTACT1_OUTPUT=$(printf '%s' "$DEBUG_OUTPUT" | tr -d '\r') |
# Source environment variables |
||||
echo "DEBUG: After tr command: '$CONTACT1_OUTPUT'" >&2 |
source .generated/test-env.sh |
||||
|
|
||||
# Debug: Show what we're passing to extract_did_info |
# Load JSON data |
||||
echo "DEBUG: Calling extract_did_info with: '$CONTACT1_OUTPUT'" >&2 |
CLAIM_DETAILS=$(cat .generated/claim_details.json) |
||||
CONTACT1_INFO=$(extract_did_info "$CONTACT1_OUTPUT") |
CONTACTS=$(cat .generated/contacts.json) |
||||
EXTRACT_STATUS=$? |
|
||||
echo "DEBUG: extract_did_info returned status: $EXTRACT_STATUS" >&2 |
|
||||
echo "DEBUG: CONTACT1_INFO: '$CONTACT1_INFO'" >&2 |
|
||||
|
|
||||
if [ $EXTRACT_STATUS -ne 0 ]; then |
# 1. Claim-based deeplinks |
||||
echo "DEBUG: extract_did_info failed" >&2 |
execute_deeplink "timesafari://claim-cert/$(jq -r .claim_id <<< "$CLAIM_DETAILS")" \ |
||||
exit 1 |
"Testing claim certificate view" |
||||
fi |
|
||||
CONTACT1_DID=$(printf '%s' "$CONTACT1_INFO" | jq -r .did) |
execute_deeplink "timesafari://claim-add-raw/$(jq -r .claim_id <<< "$CLAIM_DETAILS")" \ |
||||
CONTACT1_KEY=$(printf '%s' "$CONTACT1_INFO" | jq -r .privateKey) |
"Testing raw claim addition" |
||||
|
|
||||
echo "Generating second contact DID (Jordan)..." |
|
||||
DEBUG_OUTPUT=$(DEBUG=0 ./test-scripts/did_generator.sh) |
|
||||
CONTACT2_OUTPUT=$(printf '%s' "$DEBUG_OUTPUT" | tr -d '\r') |
|
||||
CONTACT2_INFO=$(extract_did_info "$CONTACT2_OUTPUT") |
|
||||
if [ $? -ne 0 ]; then |
|
||||
exit 1 |
|
||||
fi |
|
||||
CONTACT2_DID=$(printf '%s' "$CONTACT2_INFO" | jq -r .did) |
|
||||
CONTACT2_KEY=$(printf '%s' "$CONTACT2_INFO" | jq -r .privateKey) |
|
||||
|
|
||||
echo "Generating issuer DID..." |
|
||||
DEBUG_OUTPUT=$(DEBUG=0 ./test-scripts/did_generator.sh) |
|
||||
ISSUER_OUTPUT=$(printf '%s' "$DEBUG_OUTPUT" | tr -d '\r') |
|
||||
ISSUER_INFO=$(extract_did_info "$ISSUER_OUTPUT") |
|
||||
if [ $? -ne 0 ]; then |
|
||||
exit 1 |
|
||||
fi |
|
||||
ISSUER_DID=$(printf '%s' "$ISSUER_INFO" | jq -r .did) |
|
||||
ISSUER_KEY=$(printf '%s' "$ISSUER_INFO" | jq -r .privateKey) |
|
||||
|
|
||||
# Add some visual feedback about the generated DIDs |
|
||||
echo |
|
||||
echo "Generated DIDs:" |
|
||||
echo " Contact 1: ${CONTACT1_DID:0:20}..." |
|
||||
echo " Contact 2: ${CONTACT2_DID:0:20}..." |
|
||||
echo " Issuer: ${ISSUER_DID:0:20}..." |
|
||||
echo |
|
||||
|
|
||||
# Create a temporary env file with the generated DIDs |
|
||||
echo "Creating test-env.sh with generated DIDs..." |
|
||||
cat > .generated/test-env.sh << EOF |
|
||||
#!/bin/bash |
|
||||
# Generated $(date) |
|
||||
export CONTACT1_DID="$CONTACT1_DID" |
|
||||
export CONTACT1_KEY="$CONTACT1_KEY" |
|
||||
export CONTACT2_DID="$CONTACT2_DID" |
|
||||
export CONTACT2_KEY="$CONTACT2_KEY" |
|
||||
export ISSUER_DID="$ISSUER_DID" |
|
||||
export ISSUER_KEY="$ISSUER_KEY" |
|
||||
EOF |
|
||||
|
|
||||
# Debug output |
# 2. DID-based deeplinks |
||||
echo "Contents of generated test-env.sh:" |
execute_deeplink "timesafari://did/$CONTACT1_DID" \ |
||||
cat .generated/test-env.sh |
"Testing DID view" |
||||
|
|
||||
|
execute_deeplink "timesafari://contact-edit/$CONTACT1_DID" \ |
||||
|
"Testing contact editing" |
||||
|
|
||||
# Make sure file is executable |
# 3. JSON-based deeplinks |
||||
chmod +x .generated/test-env.sh |
execute_deeplink "timesafari://contacts/import?contacts=$(jq -r @uri <<< "$CONTACTS")" \ |
||||
|
"Testing contacts import" |
||||
|
|
||||
# Source and validate the newly created env file |
echo -e "\n${GREEN}All deeplink tests completed${NC}" |
||||
echo "Sourcing generated test-env.sh..." |
} |
||||
source .generated/test-env.sh |
|
||||
validate_env || exit 1 |
|
||||
fi |
|
||||
|
|
||||
if [ "$GENERATE_ONLY" = true ]; then |
# Check for adb if not in print mode |
||||
echo "Generated DIDs and created environment file at .generated/test-env.sh" |
if [ "$TEST_MODE" = "execute" ]; then |
||||
echo "To use these DIDs, run:" |
if ! command -v adb >/dev/null 2>&1; then |
||||
echo "source .generated/test-env.sh" |
echo -e "${YELLOW}Warning: adb not found, switching to print mode${NC}" |
||||
exit 0 |
TEST_MODE="print" |
||||
|
elif [ -z "$(adb devices | grep -v List | grep device)" ]; then |
||||
|
echo -e "${YELLOW}Warning: no devices/emulators found, switching to print mode${NC}" |
||||
|
TEST_MODE="print" |
||||
|
fi |
||||
fi |
fi |
||||
|
|
||||
# Create contacts JSON for deep links |
# Execute main test sequence |
||||
CONTACTS_JSON=$(jq -n \ |
main |
||||
--arg did1 "$CONTACT1_DID" \ |
|
||||
--arg did2 "$CONTACT2_DID" \ |
|
||||
'[ |
|
||||
{"did": $did1}, |
|
||||
{ |
|
||||
"did": $did2, |
|
||||
"name": "Jordan", |
|
||||
"nextPubKeyHashB64": "IBfRZfwdzeKOzqCx8b+WlLpMJHOAT9ZknIDJo7F3rZE=", |
|
||||
"publicKeyBase64": "A1eIndfaxgMpVwyD5dYe74DgjuIo5SwPZFCcLdOemjf" |
|
||||
} |
|
||||
]') |
|
||||
|
|
||||
export CONTACTS_JSON |
|
||||
export TEST_MODE=${TEST_MODE:-print} # Use TEST_MODE from environment or default to print |
|
||||
|
|
||||
# Run deep link tests in order |
|
||||
if [ "$ALL_TESTS" = true ]; then |
|
||||
./test-scripts/test-deeplinks.sh -t 5 -a |
|
||||
else |
|
||||
./test-scripts/test-deeplinks.sh -t 5 |
|
||||
fi |
|
Loading…
Reference in new issue