test_anastasis_reducer_add_authentication.sh (3096B)
1 #!/bin/bash 2 # This file is in the public domain. 3 4 set -eu 5 6 # Exit, with status code "skip" (no 'real' failure) 7 function exit_skip() { 8 echo " SKIP: $1" 9 exit 77 10 } 11 12 # Exit, with error message (hard failure) 13 function exit_fail() { 14 echo " ERROR: $1" 15 exit 1 16 } 17 18 # Cleanup to run whenever we exit 19 function cleanup() 20 { 21 for n in `jobs -p` 22 do 23 kill $n 2> /dev/null || true 24 done 25 rm -f $TFILE $SFILE 26 wait 27 } 28 29 SFILE=`mktemp test_reducer_stateXXXXXX` 30 TFILE=`mktemp test_reducer_stateXXXXXX` 31 32 # Install cleanup handler (except for kill -9) 33 trap cleanup EXIT 34 35 # Check we can actually run 36 echo -n "Testing for jq" 37 jq -h > /dev/null || exit_skip "jq required" 38 echo " FOUND" 39 40 echo -n "Testing for anastasis-reducer ..." 41 anastasis-reducer -h > /dev/null || exit_skip "anastasis-reducer required" 42 echo " FOUND" 43 44 echo -n "Test add authentication ..." 45 46 # First method 47 anastasis-reducer -a \ 48 '{"authentication_method": { 49 "type": "question", 50 "instructions": "What is your name?", 51 "challenge": "91GPWWR" 52 } }' \ 53 add_authentication resources/03-backup.json $TFILE 54 55 STATE=`jq -r -e .backup_state < $TFILE` 56 if test "$STATE" != "AUTHENTICATIONS_EDITING" 57 then 58 exit_fail "Expected new state to be 'AUTHENTICATIONS_EDITING', got '$STATE'" 59 fi 60 61 ARRAY_LENGTH=`jq -r -e '.authentication_methods | length' < $TFILE` 62 if test $ARRAY_LENGTH != 1 63 then 64 exit_fail "Expected array length to be 1, got '$ARRAY_LENGTH'" 65 fi 66 67 echo -n "." 68 # Second method 69 anastasis-reducer -a \ 70 '{"authentication_method": { 71 "type": "question", 72 "instructions": "How old are you?", 73 "challenge": "64S36" 74 }}' \ 75 add_authentication $TFILE $SFILE 76 77 STATE=`jq -r -e .backup_state < $SFILE` 78 if test "$STATE" != "AUTHENTICATIONS_EDITING" 79 then 80 exit_fail "Expected new state to be 'AUTHENTICATIONS_EDITING', got '$STATE'" 81 fi 82 83 ARRAY_LENGTH=`jq -r -e '.authentication_methods | length' < $SFILE` 84 if test $ARRAY_LENGTH != 2 85 then 86 exit_fail "Expected array length to be 2, got '$ARRAY_LENGTH'" 87 fi 88 89 echo -n "." 90 91 # Third method 92 anastasis-reducer -a \ 93 '{"authentication_method": { 94 "type": "question", 95 "instructions": "Where do you live?", 96 "challenge": "9NGQ4WR" 97 }}' \ 98 add_authentication $SFILE $TFILE 99 100 STATE=`jq -r -e .backup_state < $TFILE` 101 if test "$STATE" != "AUTHENTICATIONS_EDITING" 102 then 103 exit_fail "Expected new state to be 'AUTHENTICATIONS_EDITING', got '$STATE'" 104 fi 105 106 ARRAY_LENGTH=`jq -r -e '.authentication_methods | length' < $TFILE` 107 if test $ARRAY_LENGTH != 3 108 then 109 exit_fail "Expected array length to be 3, got '$ARRAY_LENGTH'" 110 fi 111 112 echo " OK" 113 114 115 echo -n "Test delete authentication ..." 116 117 anastasis-reducer -a \ 118 '{"authentication_method": 2 }' \ 119 delete_authentication $TFILE $SFILE 120 121 STATE=`jq -r -e .backup_state < $SFILE` 122 if test "$STATE" != "AUTHENTICATIONS_EDITING" 123 then 124 exit_fail "Expected new state to be 'AUTHENTICATIONS_EDITING', got '$STATE'" 125 fi 126 127 ARRAY_LENGTH=`jq -r -e '.authentication_methods | length' < $SFILE` 128 if test $ARRAY_LENGTH != 2 129 then 130 exit_fail "Expected array length to be 2, got '$ARRAY_LENGTH'" 131 fi 132 133 echo " OK" 134 135 exit 0