test_anastasis_reducer_enter_secret.sh (10489B)
1 #!/bin/bash 2 # This file is in the public domain. 3 4 # shellcheck disable=SC2317 5 ## Coloring style Text shell script 6 COLOR='\033[0;35m' 7 NOCOLOR='\033[0m' 8 BOLD="$(tput bold)" 9 NORM="$(tput sgr0)" 10 11 set -eu 12 13 # Replace with 0 for nexus... 14 USE_FAKEBANK=1 15 if [ 1 = "$USE_FAKEBANK" ] 16 then 17 ACCOUNT="exchange-account-2" 18 WIRE_METHOD="x-taler-bank" 19 BANK_FLAGS="-f -d $WIRE_METHOD -u $ACCOUNT" 20 BANK_URL="http://localhost:18082/" 21 MERCHANT_PAYTO="payto://x-taler-bank/localhost/anastasis?receiver-name=anastasis" 22 else 23 ACCOUNT="exchange-account-1" 24 WIRE_METHOD="iban" 25 BANK_FLAGS="-ns -d $WIRE_METHOD -u $ACCOUNT" 26 BANK_URL="http://localhost:18082/" 27 MERCHANT_PAYTO="payto://iban/SANDBOXX/DE648226?receiver-name=anastasis" 28 fi 29 30 # Check we can actually run 31 echo -n "Testing for jq" 32 jq -h > /dev/null || exit_skip "jq required" 33 echo " FOUND" 34 echo -n "Testing for anastasis-reducer ..." 35 anastasis-reducer -h > /dev/null || exit_skip "anastasis-reducer required" 36 echo " FOUND" 37 38 echo -n "Testing for taler" 39 taler-exchange-httpd -h > /dev/null || exit_skip " taler-exchange required" 40 taler-merchant-httpd -h > /dev/null || exit_skip " taler-merchant required" 41 echo " FOUND" 42 43 echo -n "Testing for taler-wallet-cli" 44 taler-wallet-cli -v >/dev/null </dev/null || exit_skip " MISSING" 45 echo " FOUND" 46 47 echo -n "Testing for anastasis-httpd" 48 anastasis-httpd -h >/dev/null </dev/null || exit_skip " MISSING" 49 echo " FOUND" 50 51 . setup.sh 52 # Launch exchange, merchant and bank. 53 # shellcheck disable=SC2086 54 setup -c "test_reducer.conf" \ 55 -emw \ 56 -r merchant-exchange-default \ 57 $BANK_FLAGS 58 59 60 # Cleanup to run whenever we exit 61 function cleanup() 62 { 63 exit_cleanup 64 for n in $(jobs -p) 65 do 66 kill "$n" 2> /dev/null || true 67 done 68 rm -rf "$CONF" "$WALLET_DB" "$TFILE" "$UFILE" "$TMP_DIR" 69 wait 70 } 71 72 CONF_1="test_anastasis_reducer_1.conf" 73 CONF_2="test_anastasis_reducer_2.conf" 74 CONF_3="test_anastasis_reducer_3.conf" 75 CONF_4="test_anastasis_reducer_4.conf" 76 77 # Exchange configuration file will be edited, so we create one 78 # from the template. 79 CONF="test_reducer.conf.edited" 80 81 TMP_DIR=$(mktemp -p "${TMPDIR:-/tmp}" -d keys-tmp-XXXXXX) 82 WALLET_DB=$(mktemp -p "${TMPDIR:-/tmp}" test_reducer_wallet.json-XXXXXX) 83 TFILE=$(mktemp -p "${TMPDIR:-/tmp}" test_reducer_statePPXXXXXX) 84 UFILE=$(mktemp -p "${TMPDIR:-/tmp}" test_reducer_stateBFXXXXXX) 85 86 # Install cleanup handler (except for kill -9) 87 trap cleanup EXIT 88 89 90 echo -n "Initialize anastasis databases ..." 91 # Name of the Postgres database we will use for the script. 92 # Will be dropped, do NOT use anything that might be used 93 # elsewhere 94 TARGET_DB_1=$(anastasis-config -c "$CONF_1" -s stasis-postgres -o CONFIG | sed -e "s/^postgres:\/\/\///") 95 TARGET_DB_2=$(anastasis-config -c "$CONF_2" -s stasis-postgres -o CONFIG | sed -e "s/^postgres:\/\/\///") 96 TARGET_DB_3=$(anastasis-config -c "$CONF_3" -s stasis-postgres -o CONFIG | sed -e "s/^postgres:\/\/\///") 97 TARGET_DB_4=$(anastasis-config -c "$CONF_4" -s stasis-postgres -o CONFIG | sed -e "s/^postgres:\/\/\///") 98 99 dropdb "$TARGET_DB_1" >/dev/null 2>/dev/null || true 100 createdb "$TARGET_DB_1" || exit_skip "Could not create database $TARGET_DB_1" 101 anastasis-dbinit -c "$CONF_1" 2> anastasis-dbinit_1.log 102 dropdb "$TARGET_DB_2" >/dev/null 2>/dev/null || true 103 createdb "$TARGET_DB_2" || exit_skip "Could not create database $TARGET_DB_2" 104 anastasis-dbinit -c "$CONF_2" 2> anastasis-dbinit_2.log 105 dropdb "$TARGET_DB_3" >/dev/null 2>/dev/null || true 106 createdb "$TARGET_DB_3" || exit_skip "Could not create database $TARGET_DB_3" 107 anastasis-dbinit -c "$CONF_3" 2> anastasis-dbinit_3.log 108 dropdb "$TARGET_DB_4" >/dev/null 2>/dev/null || true 109 createdb "$TARGET_DB_4" || exit_skip "Could not create database $TARGET_DB_4" 110 anastasis-dbinit -c "$CONF_4" 2> anastasis-dbinit_4.log 111 112 echo " OK" 113 114 echo -n "Launching anastasis services ..." 115 PREFIX="" #valgrind 116 $PREFIX anastasis-httpd -c $CONF_1 2> anastasis-httpd_1.log & 117 $PREFIX anastasis-httpd -c $CONF_2 2> anastasis-httpd_2.log & 118 $PREFIX anastasis-httpd -c $CONF_3 2> anastasis-httpd_3.log & 119 $PREFIX anastasis-httpd -c $CONF_4 2> anastasis-httpd_4.log & 120 121 echo -n "Waiting for anastasis services ..." 122 # Wait for anastasis services to be available 123 for n in $(seq 1 50) 124 do 125 echo -n "." 126 sleep 0.1 127 OK=0 128 # anastasis_01 129 wget --tries=1 --timeout=1 http://localhost:8086/ -o /dev/null -O /dev/null >/dev/null || continue 130 # anastasis_02 131 wget --tries=1 --timeout=1 http://localhost:8087/ -o /dev/null -O /dev/null >/dev/null || continue 132 # anastasis_03 133 wget --tries=1 --timeout=1 http://localhost:8088/ -o /dev/null -O /dev/null >/dev/null || continue 134 # anastasis_04 135 wget --tries=1 --timeout=1 http://localhost:8089/ -o /dev/null -O /dev/null >/dev/null || continue 136 OK=1 137 break 138 done 139 140 if [ 1 != "$OK" ] 141 then 142 exit_skip "Failed to launch anastasis services" 143 fi 144 echo "OK" 145 146 echo -n "Configuring merchant instance ..." 147 # Setup merchant 148 149 curl -H "Content-Type: application/json" -X POST -d '{"auth":{"method":"external"},"id":"admin","name":"default","user_type":"business","address":{},"jurisdiction":{},"use_stefan":true,"default_wire_transfer_delay":{"d_us" : 3600000000},"default_pay_delay":{"d_us": 3600000000}}' http://localhost:9966/management/instances 150 151 152 curl -H "Content-Type: application/json" -X POST -d '{"payto_uri":"'"$MERCHANT_PAYTO"'"}' http://localhost:9966/private/accounts 153 154 155 156 echo " DONE" 157 158 echo -en "${COLOR}${BOLD}Test enter secret in a backup state ...${NORM}${NOCOLOR}" 159 160 $PREFIX anastasis-reducer -a \ 161 '{"secret": { "value" : "veryhardtoguesssecret", "mime" : "text/plain" } }' \ 162 enter_secret resources/06-backup.json "$TFILE" 163 164 STATE=$(jq -r -e .backup_state < "$TFILE") 165 if [ "$STATE" != "SECRET_EDITING" ] 166 then 167 jq -e . "$TFILE" 168 exit_fail "Expected new state to be 'SECRET_EDITING', got '$STATE'" 169 fi 170 171 echo " DONE" 172 echo -en "${COLOR}${BOLD}Test expiration change ...${NORM}${NOCOLOR}" 173 174 SECS=$(date '+%s') 175 # Use 156 days into the future to get 1 year 176 SECS=$(( SECS + 13478400 )) 177 178 $PREFIX anastasis-reducer -a \ 179 "$(jq -n ' 180 {"expiration": { "t_s" : $SEC } }' \ 181 --argjson SEC "$SECS" 182 )" \ 183 update_expiration "$TFILE" "$UFILE" 184 185 STATE=$(jq -r -e .backup_state < "$UFILE") 186 if test "$STATE" != "SECRET_EDITING" 187 then 188 jq -e . "$UFILE" 189 exit_fail "Expected new state to be 'SECRET_EDITING', got '$STATE'" 190 fi 191 192 FEES=$(jq -r -e '.upload_fees[0].fee' < "$UFILE") 193 # 4x 4.99 for the annual fee of each of the four providers in the state, 194 # plus 3x0.01 for the truth uploads: 06-backup.json has three authentication 195 # methods and the reducer places each of them at a provider of its own. 196 if [ "$FEES" != "TESTKUDOS:19.99" ] 197 then 198 jq -e . "$UFILE" 199 exit_fail "Expected upload fees to be 'TESTKUDOS:19.99', got '$FEES'" 200 fi 201 202 203 echo " DONE" 204 echo -en "${COLOR}${BOLD}Test advance to payment ...${NORM}${NOCOLOR}" 205 206 $PREFIX anastasis-reducer next "$UFILE" "$TFILE" 207 208 STATE=$(jq -r -e .backup_state < "$TFILE") 209 if [ "$STATE" != "TRUTHS_PAYING" ] 210 then 211 jq -e . "$TFILE" 212 exit_fail "Expected new state to be 'TRUTHS_PAYING', got '$STATE'" 213 fi 214 215 # FIXME: this test is specific to how the 216 # C reducer stores state (redundantly!), should converge eventually! 217 218 #TMETHOD=$(jq -r -e '.policies[0].methods[0].truth.type' < $TFILE) 219 #if test $TMETHOD != "question" 220 #then 221 # exit_fail "Expected method to be >='question', got $TMETHOD" 222 #fi 223 # 224 #echo " OK" 225 226 227 #Pay 228 229 echo -en "${COLOR}${BOLD}Withdrawing amount to wallet ...${NORM}${NOCOLOR}" 230 231 EXCHANGE_URL="$(taler-exchange-config -c "$CONF" -s exchange -o BASE_URL)" 232 233 rm "$WALLET_DB" 234 taler-wallet-cli \ 235 --no-throttle \ 236 --wallet-db="$WALLET_DB" \ 237 api \ 238 --expect-success 'withdrawTestBalance' \ 239 "$(jq -n ' 240 { 241 amount: "TESTKUDOS:40", 242 corebankApiBaseUrl: $BANK_URL, 243 exchangeBaseUrl: $EXCHANGE_URL 244 }' \ 245 --arg BANK_URL "${BANK_URL}" \ 246 --arg EXCHANGE_URL "${EXCHANGE_URL}" 247 )" 2>wallet-withdraw.err \ 248 >wallet-withdraw.log 249 taler-wallet-cli \ 250 --no-throttle \ 251 --wallet-db="$WALLET_DB" \ 252 run-until-done \ 253 2>wallet-withdraw-finish.err \ 254 >wallet-withdraw-finish.log 255 256 echo " OK" 257 258 echo -en "${COLOR}${BOLD}Making payments for truth uploads ... ${NORM}${NOCOLOR}" 259 OBJECT_SIZE=$(jq -r -e '.payments | length' < "$TFILE") 260 for ((INDEX=0; INDEX < "$OBJECT_SIZE"; INDEX++)) 261 do 262 PAY_URI=$(jq --argjson INDEX $INDEX -r -e '.payments[$INDEX]' < "$TFILE") 263 # run wallet CLI 264 echo -n "$INDEX" 265 taler-wallet-cli \ 266 --no-throttle \ 267 --wallet-db="$WALLET_DB" \ 268 handle-uri "${PAY_URI}" \ 269 -y \ 270 2>wallet-pay1.err \ 271 >wallet-pay1.log 272 echo -n "," 273 done 274 echo " OK" 275 echo -e "${COLOR}${BOLD}Running wallet run-until-done...${NORM}${NOCOLOR}" 276 taler-wallet-cli \ 277 --wallet-db="$WALLET_DB" \ 278 run-until-done \ 279 2>wallet-pay-finish.err \ 280 >wallet-pay-finish.log 281 echo -e "${COLOR}${BOLD}Payments done${NORM}${NOCOLOR}" 282 283 284 echo -en "${COLOR}${BOLD}Try to upload again ...${NORM}${NOCOLOR}" 285 $PREFIX anastasis-reducer pay "$TFILE" "$UFILE" 286 mv "$UFILE" "$TFILE" 287 echo " OK" 288 289 290 STATE="$(jq -r -e .backup_state < "$TFILE")" 291 if [ "$STATE" != "POLICIES_PAYING" ] 292 then 293 exit_fail "Expected new state to be 'POLICIES_PAYING', got '$STATE'" 294 fi 295 296 echo -en "${COLOR}${BOLD}Making payments for policy uploads ... ${NORM}${NOCOLOR}" 297 OBJECT_SIZE="$(jq -r -e '.policy_payment_requests | length' < "$TFILE")" 298 for ((INDEX=0; INDEX < "$OBJECT_SIZE"; INDEX++)) 299 do 300 PAY_URI="$(jq --argjson INDEX "$INDEX" -r -e '.policy_payment_requests[$INDEX].payto' < "$TFILE")" 301 # run wallet CLI 302 export PAY_URI 303 echo -n "$INDEX" 304 taler-wallet-cli \ 305 --wallet-db="$WALLET_DB" \ 306 handle-uri "$PAY_URI" \ 307 -y \ 308 2>"wallet-pay2-$INDEX.err" \ 309 >"wallet-pay2-$INDEX.log" 310 echo -n "," 311 done 312 echo " OK" 313 echo -e "${COLOR}${BOLD}Running wallet run-until-done...${NORM}${NOCOLOR}" 314 taler-wallet-cli \ 315 --wallet-db="$WALLET_DB" \ 316 run-until-done \ 317 2>wallet-pay2-finish.err \ 318 >wallet-pay2-finish.log 319 echo -e "${COLOR}${BOLD}Payments done${NORM}${NOCOLOR}" 320 321 echo -en "${COLOR}${BOLD}Try to upload again ...${NORM}${NOCOLOR}" 322 $PREFIX anastasis-reducer pay "$TFILE" "$UFILE" 323 324 echo " OK" 325 326 echo -n "Final checks ..." 327 328 STATE=$(jq -r -e .backup_state < "$UFILE") 329 if [ "$STATE" != "BACKUP_FINISHED" ] 330 then 331 exit_fail "Expected new state to be BACKUP_FINISHED, got $STATE" 332 fi 333 334 jq -r -e .core_secret \ 335 < "$UFILE" \ 336 > /dev/null \ 337 && exit_fail "'core_secret' was not cleared upon success" 338 339 echo " OK" 340 exit 0