boxes-api/scripts/ImageArchiveTest.bash

73 lines
2.0 KiB
Bash

#!/bin/bash
# Set variables
SERVER_URL="http://localhost:8080" # Replace with your server URL
ENDPOINT="/admin/imagearchive"
OUTPUT_FILE="images.tar.gz"
EXTRACT_DIR="extracted_images"
USERNAME=boxuser
PASSWORD=boxuser
AUTH_TOKEN=$(curl -s -X POST -H "Content-Type: application/json" \
-d "{\"username\":\"$USERNAME\", \"password\":\"$PASSWORD\"}" \
"$SERVER_URL/api/v1/login" | jq -r '.token')
echo $AUTH_TOKEN
# Function to check if a command exists
command_exists () {
type "$1" &> /dev/null ;
}
# Check if required commands exist
if ! command_exists curl || ! command_exists md5sum || ! command_exists tar; then
echo "Error: This script requires curl, md5sum, and tar. Please install them and try again."
exit 1
fi
# Download the archive
echo "Downloading archive..."
HTTP_RESPONSE=$(curl -s -w "HTTPSTATUS:%{http_code}" -o "$OUTPUT_FILE" \
-H "Authorization: Bearer $AUTH_TOKEN" \
"$SERVER_URL/api/v1/login")
HTTP_BODY=$(echo $HTTP_RESPONSE | sed -e 's/HTTPSTATUS\:.*//g')
HTTP_STATUS=$(echo $HTTP_RESPONSE | tr -d '\n' | sed -e 's/.*HTTPSTATUS://')
if [ $HTTP_STATUS -ne 200 ]; then
echo "Error: HTTP Status $HTTP_STATUS"
echo "Response: $HTTP_BODY"
exit 1
fi
# Get MD5 sum from header
HEADER_MD5=$(curl -sI -H "Authorization: Bearer $AUTH_TOKEN" "$SERVER_URL$ENDPOINT" | grep X-Archive-MD5 | cut -d' ' -f2 | tr -d '\r')
# Calculate MD5 sum of downloaded file
CALCULATED_MD5=$(md5sum "$OUTPUT_FILE" | cut -d' ' -f1)
echo "MD5 from header: $HEADER_MD5"
echo "Calculated MD5: $CALCULATED_MD5"
# Compare MD5 sums
if [ "$HEADER_MD5" = "$CALCULATED_MD5" ]; then
echo "MD5 sums match. File integrity verified."
else
echo "MD5 sums do not match. File may be corrupted."
exit 1
fi
# Extract the archive
echo "Extracting archive..."
mkdir -p "$EXTRACT_DIR"
tar -xzvf "$OUTPUT_FILE" -C "$EXTRACT_DIR"
# List contents of extracted directory
echo "Contents of extracted archive:"
find "$EXTRACT_DIR" -type f | sed "s|$EXTRACT_DIR/||"
# Clean up
echo "Cleaning up..."
rm "$OUTPUT_FILE"
rm -r "$EXTRACT_DIR"
echo "Test completed successfully."