|  | 
|  | 1 | +#!/usr/bin/env bats | 
|  | 2 | + | 
|  | 3 | +setup() { | 
|  | 4 | +  # Set up the environment for each test | 
|  | 5 | +  export home="/tmp/borgwarehouse" | 
|  | 6 | +  mkdir -p /tmp/borgwarehouse | 
|  | 7 | +  mkdir -p /tmp/borgwarehouse/repos | 
|  | 8 | +  mkdir -p /tmp/borgwarehouse/.ssh | 
|  | 9 | +  touch /tmp/borgwarehouse/.ssh/authorized_keys | 
|  | 10 | + | 
|  | 11 | +  # SSH keys samples for testing | 
|  | 12 | +  export SSH_KEY_ED25519="ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAICtujwncNGgdNxWOCQedMCnrhRZT4B7eUyyFJNryvQj9 publickey" | 
|  | 13 | +  export SSH_KEY_ED25519_SK="sk-ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAICtujwncNGgdNxWOCQedMCnrhRZT4B7eUyyFJNryvQj9 publickey" | 
|  | 14 | +  export SSH_KEY_RSA="ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDf8SFSuWqPtPYKjoXL8aowdKYfeKFKbE6w4CvqXPSRtgwKGWJva/UVF8Q/jGClwsVpTJfZnA76fnih76cE4ZiucPtDM2dyDILHNSZo/8rwUVkNB4P3aaCxV6lVMurmIgF4ibWQFBdyWKCJM7nQjO71TlMw/HfqpeYXXdjL1MBlMzqOZYLDrPoiJEiAfKheVeCONMlo8HMfEPxiu7bwfF7vQqYstcbZ55RN1t7RYaxlCTZaj0GOxIGKLmmHTDGzQQIaOGSr3+8Gk1I/MFle2/dYKbBEi97NrJowRO4a4pVbVso0YKyESL3U40uZly1bzoNx4DvMBbFwYSE1IJbs/AQIfB6KH4yLtQTmfb4qPRLCS1CBWBZKeKJ304p6rxKuv+CjagsFwdG5cS7cCosfdEU43QuWngnYQGUwMKskxX/7rPm+WZItN7XiNoMRmzaC+T0cIRXH7Cl7VFE3cbTzgmqJPVeUpccjTP/BdDahHKFqyVhAFvyI7JM4ct1/tU8o015TM1EXzMBeJOxalj6RsuDIFjjEaMN5pZmlHGBFBmcRgY7TqYAwr02maKb9BtcPOGIPgpI3AMzqNX+LjFssI0AuqBGTYN8v6OBr2NmTHfZlnClucjoAw71QPeQySABqrX0p9xieX15Ly1z9oMH9lapW6X9e0JnQBMnz1N2eaq1qAQ== publickey" | 
|  | 15 | +} | 
|  | 16 | + | 
|  | 17 | +teardown() { | 
|  | 18 | +  # Clean up the environment after each test | 
|  | 19 | +  rm -rf /tmp/borgwarehouse | 
|  | 20 | +} | 
|  | 21 | + | 
|  | 22 | +@test "Test createRepo.sh with missing arguments" { | 
|  | 23 | +  run bash /test/scripts/createRepo.sh | 
|  | 24 | +  [ "$status" -eq 1 ] | 
|  | 25 | +  [ "$output" == "This shell takes 3 arguments : SSH Public Key, Quota in Go [e.g. : 10], Append only mode [true|false]" ] | 
|  | 26 | +} | 
|  | 27 | + | 
|  | 28 | +@test "Test createRepo.sh with missing Quota and append-only mode arguments" { | 
|  | 29 | +  run bash /test/scripts/createRepo.sh "$SSH_KEY_ED25519" | 
|  | 30 | +  [ "$status" -eq 1 ] | 
|  | 31 | +  [ "$output" == "This shell takes 3 arguments : SSH Public Key, Quota in Go [e.g. : 10], Append only mode [true|false]" ] | 
|  | 32 | +} | 
|  | 33 | + | 
|  | 34 | +@test "Test createRepo.sh with missing Append-only mode argument" { | 
|  | 35 | +  run bash /test/scripts/createRepo.sh "$SSH_KEY_ED25519" 10 | 
|  | 36 | +  echo $output | 
|  | 37 | +  cat ${home}/.ssh/authorized_keys | 
|  | 38 | +  [ "$status" -eq 1 ] | 
|  | 39 | +  [ "$output" == "This shell takes 3 arguments : SSH Public Key, Quota in Go [e.g. : 10], Append only mode [true|false]" ] | 
|  | 40 | +} | 
|  | 41 | + | 
|  | 42 | +@test "Test createRepo.sh with invalid SSH key format" { | 
|  | 43 | +  run bash /test/scripts/createRepo.sh "invalid-key" 10 true | 
|  | 44 | +  [ "$status" -eq 2 ] | 
|  | 45 | +  [ "$output" == "Invalid public SSH KEY format. Provide a key in OpenSSH format (rsa, ed25519, ed25519-sk)" ] | 
|  | 46 | +} | 
|  | 47 | + | 
|  | 48 | +@test "Test createRepo.sh with invalid Quota format" { | 
|  | 49 | +  run bash /test/scripts/createRepo.sh "$SSH_KEY_ED25519" "AA" true | 
|  | 50 | +  [ "$status" -eq 1 ] | 
|  | 51 | +  [ "$output" == "This shell takes 3 arguments : SSH Public Key, Quota in Go [e.g. : 10], Append only mode [true|false]" ] | 
|  | 52 | +} | 
|  | 53 | + | 
|  | 54 | +@test "Test createRepo.sh with invalid Append-only mode format" { | 
|  | 55 | +  run bash /test/scripts/createRepo.sh "$SSH_KEY_ED25519" 10 blabla | 
|  | 56 | +  [ "$status" -eq 1 ] | 
|  | 57 | +  [ "$output" == "This shell takes 3 arguments : SSH Public Key, Quota in Go [e.g. : 10], Append only mode [true|false]" ] | 
|  | 58 | +} | 
|  | 59 | + | 
|  | 60 | + | 
|  | 61 | +@test "Test createRepo.sh if authorized_keys is missing" { | 
|  | 62 | +  rm /tmp/borgwarehouse/.ssh/authorized_keys | 
|  | 63 | +  run bash /test/scripts/createRepo.sh "$SSH_KEY_ED25519" 10 true | 
|  | 64 | +  [ "$status" -eq 5 ] | 
|  | 65 | +  [ "$output" == "${home}/.ssh/authorized_keys must be present" ] | 
|  | 66 | +} | 
|  | 67 | + | 
|  | 68 | +@test "Test createRepo.sh if SSH key is already present in authorized_keys" { | 
|  | 69 | +  # Add a key | 
|  | 70 | +  echo "$SSH_KEY_ED25519" > /tmp/borgwarehouse/.ssh/authorized_keys | 
|  | 71 | +  # Try to re-add the same key | 
|  | 72 | +  run bash /test/scripts/createRepo.sh "$SSH_KEY_ED25519" 10 true | 
|  | 73 | +  [ "$status" -eq 3 ] | 
|  | 74 | +  [ "$output" == "SSH pub key already present in authorized_keys" ] | 
|  | 75 | +} | 
|  | 76 | + | 
|  | 77 | +@test "Test createRepo.sh repository name generation" { | 
|  | 78 | +  run bash /test/scripts/createRepo.sh "$SSH_KEY_ED25519" 10 false | 
|  | 79 | +  [[ "$output" =~ ^[0-9a-f]{8}$ ]]  # Must return a 8 characters hexa string | 
|  | 80 | +} | 
|  | 81 | + | 
|  | 82 | +@test "Test createRepo.sh key ED25519 insertion in authorized_keys" { | 
|  | 83 | +  run bash /test/scripts/createRepo.sh "$SSH_KEY_ED25519" 10 false | 
|  | 84 | +  expected_line="command=\"cd ${home}/repos;borg serve --restrict-to-path ${home}/repos/${output} --storage-quota 10G\",restrict $SSH_KEY_ED25519" | 
|  | 85 | +  grep -qF "$expected_line" /tmp/borgwarehouse/.ssh/authorized_keys | 
|  | 86 | +} | 
|  | 87 | + | 
|  | 88 | +@test "Test createRepo.sh key ED25519-SK insertion in authorized_keys" { | 
|  | 89 | +  run bash /test/scripts/createRepo.sh "$SSH_KEY_ED25519_SK" 10 false | 
|  | 90 | +  expected_line="command=\"cd ${home}/repos;borg serve --restrict-to-path ${home}/repos/${output} --storage-quota 10G\",restrict $SSH_KEY_ED25519_SK" | 
|  | 91 | +  grep -qF "$expected_line" /tmp/borgwarehouse/.ssh/authorized_keys | 
|  | 92 | +} | 
|  | 93 | + | 
|  | 94 | +@test "Test createRepo.sh key RSA insertion in authorized_keys" { | 
|  | 95 | +  run bash /test/scripts/createRepo.sh "$SSH_KEY_RSA" 10 false | 
|  | 96 | +  expected_line="command=\"cd ${home}/repos;borg serve --restrict-to-path ${home}/repos/${output} --storage-quota 10G\",restrict $SSH_KEY_RSA" | 
|  | 97 | +  grep -qF "$expected_line" /tmp/borgwarehouse/.ssh/authorized_keys | 
|  | 98 | +} | 
|  | 99 | + | 
|  | 100 | +@test "Test createRepo.sh key ED25519 insertion in authorized_keys with append only mode" { | 
|  | 101 | +  run bash /test/scripts/createRepo.sh "$SSH_KEY_ED25519" 10 true | 
|  | 102 | +  expected_line="command=\"cd ${home}/repos;borg serve --append-only --restrict-to-path ${home}/repos/${output} --storage-quota 10G\",restrict $SSH_KEY_ED25519" | 
|  | 103 | +  grep -qF "$expected_line" /tmp/borgwarehouse/.ssh/authorized_keys | 
|  | 104 | +} | 
0 commit comments