From 50bdc3b1716d2769f823803de39eb510775bc389 Mon Sep 17 00:00:00 2001 From: MalinAhlberg Date: Fri, 20 Dec 2024 09:38:47 +0100 Subject: [PATCH] [tests] move checker functions to separate file also clean up minor typos etc --- .github/integration/scripts/checkers.sh | 28 +++++++++++++++ .../integration/tests/10_encrypt_decrypt.sh | 34 +++++------------- .github/integration/tests/20_upload.sh | 36 +++---------------- 3 files changed, 41 insertions(+), 57 deletions(-) create mode 100755 .github/integration/scripts/checkers.sh diff --git a/.github/integration/scripts/checkers.sh b/.github/integration/scripts/checkers.sh new file mode 100755 index 0000000..9a36e25 --- /dev/null +++ b/.github/integration/scripts/checkers.sh @@ -0,0 +1,28 @@ +#!/bin/bash +set -e + +# Function checking that file(s) were encrypted +function check_encrypted_file { + for k do + echo "working with $k" + output=$(head -c 8 "$k") + + if [[ "$output" == "crypt4gh" ]]; then + echo "Encrypted data file: $k" + else + echo "Failed to encrypt file: $k" + exit 1 + fi + done +} + + +# Function checking that a file was uploaded to the S3 backend +function check_uploaded_file { + if s3cmd -c testing/directS3 ls s3://"$1" | grep -q "$2"; then + echo "Uploaded encrypted file to s3 backend" + else + echo "Failed to upload file to s3 backend" + exit 1 + fi +} diff --git a/.github/integration/tests/10_encrypt_decrypt.sh b/.github/integration/tests/10_encrypt_decrypt.sh index a2fae75..4607ce1 100755 --- a/.github/integration/tests/10_encrypt_decrypt.sh +++ b/.github/integration/tests/10_encrypt_decrypt.sh @@ -1,24 +1,7 @@ #!/bin/bash set -e - -# Function checking that a file was encrypted -function check_encypted_file() { - - for k in $1 - do - output=$(head -c 8 "$k") - - if [[ "$output" = "crypt4gh" ]]; then - echo "Encrypted data file" - else - echo "Failed to encrypt file" - exit 1 - fi - done -} - -# inferred from access_key in testing/s3cmd.conf -user=test_dummy.org +test_dir=$(dirname "$0") +source "$test_dir/../scripts/checkers.sh" # Create random file dd if=/dev/urandom of=data_file count=1 bs=1M @@ -34,8 +17,7 @@ fi # Encrypt a file ./sda-cli encrypt -key sda_key.pub.pem data_file -files="data_file.c4gh" -check_encypted_file $files +check_encrypted_file data_file.c4gh # Create folder and encrypt files in it @@ -43,7 +25,7 @@ cp data_file data_file1 mkdir data_files_enc ./sda-cli encrypt -key sda_key.pub.pem -outdir data_files_enc data_file data_file1 -check_encypted_file "data_files_enc/data_file.c4gh data_files_enc/data_file1.c4gh" +check_encrypted_file data_files_enc/data_file.c4gh data_files_enc/data_file1.c4gh # Test multiple pub key encryption @@ -55,7 +37,7 @@ do else echo "Failed to create key pair for encryption" exit 1 -fi + fi done # Create file with concatenated pub keys @@ -67,7 +49,7 @@ cp data_file data_file_keys # Encrypt with multiple key flag calls ./sda-cli encrypt -key sda_key.pub.pem -key sda_key2.pub.pem data_file_keys -check_encypted_file "data_file_keys.c4gh" +check_encrypted_file data_file_keys.c4gh # Decrypt file with both keys, one at the time for key in sda_key sda_key2 do @@ -86,7 +68,7 @@ rm data_file_keys.c4gh # Encrypt with a single key and with a concatenated key file ./sda-cli encrypt -key sda_key.pub.pem -key sda_keys data_file_keys -check_encypted_file "data_file_keys.c4gh" +check_encrypted_file data_file_keys.c4gh # Decrypt file with all three keys for key in sda_key sda_key1 sda_key2 @@ -105,7 +87,7 @@ rm data_file_keys.c4gh # Encrypt with concatenated key file ./sda-cli encrypt -key sda_keys data_file_keys -check_encypted_file "data_file_keys.c4gh" +check_encrypted_file data_file_keys.c4gh # Decrypt file with all keys for key in sda_key1 sda_key2 diff --git a/.github/integration/tests/20_upload.sh b/.github/integration/tests/20_upload.sh index ec929b9..2a16f5a 100755 --- a/.github/integration/tests/20_upload.sh +++ b/.github/integration/tests/20_upload.sh @@ -1,38 +1,12 @@ #!/bin/bash set -e +test_dir=$(dirname "$0") +source "$test_dir/../scripts/checkers.sh" + # inferred from access_key in testing/s3cmd.conf user=test_dummy.org -# Function checking that a file was encrypted -function check_encypted_file() { - - for k in $1 - do - output=$(head -c 8 "$k") - - if [[ "$output" = "crypt4gh" ]]; then - echo "Encrypted data file" - else - echo "Failed to encrypt file" - exit 1 - fi - done -} - -# Function checking that a file was uploaded to the S3 backend -function check_uploaded_file() { - # TODO if emtpy, this will fail silently - output=$(s3cmd -c testing/directS3 ls s3://"$1" | grep -q "$2") - if $output ; then - echo "Uploaded encrypted file to s3 backend" - else - echo "Failed to upload file to s3 backend" - exit 1 - fi -} - - # Create folder with subfolder structure and add some encrypted files mkdir data_files_enc/dir1 data_files_enc/dir1/dir2 cp data_files_enc/data_file.c4gh data_files_enc/data_file3.c4gh @@ -47,7 +21,7 @@ check_uploaded_file "test/$user/data_file.c4gh" data_file.c4gh # Try to upload a file twice with the --force-overwrite flag -output=$(./sda-cli -config testing/s3cmd.conf upload --force-overwrite data_file.c4gh) +./sda-cli -config testing/s3cmd.conf upload --force-overwrite data_file.c4gh # Test upload all files from a folder, one by one @@ -105,7 +79,7 @@ cp data_file data_files_unenc/. && cp data_file data_files_unenc/dir1/data_file1 uploadDir="testEncryptUpload" ./sda-cli -config testing/s3cmd.conf upload -encrypt-with-key sda_key.pub.pem -r data_files_unenc -targetDir "$uploadDir" -check_encypted_file "data_files_unenc/data_file.c4gh" "data_files_unenc/dir1/data_file1.c4gh" +check_encrypted_file data_files_unenc/data_file.c4gh data_files_unenc/dir1/data_file1.c4gh for k in data_files_unenc/data_file.c4gh data_files_unenc/dir1/data_file1.c4gh do