test_anastasis_reducer_enter_secret.sh (10317B)
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_walletXXXXXX.json) 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 annual fees, plus 4x0.01 for truth uploads 194 if [ "$FEES" != "TESTKUDOS:20" ] 195 then 196 jq -e . "$UFILE" 197 exit_fail "Expected upload fees to be 'TESTKUDOS:20', got '$FEES'" 198 fi 199 200 201 echo " DONE" 202 echo -en "${COLOR}${BOLD}Test advance to payment ...${NORM}${NOCOLOR}" 203 204 $PREFIX anastasis-reducer next "$UFILE" "$TFILE" 205 206 STATE=$(jq -r -e .backup_state < "$TFILE") 207 if [ "$STATE" != "TRUTHS_PAYING" ] 208 then 209 jq -e . "$TFILE" 210 exit_fail "Expected new state to be 'TRUTHS_PAYING', got '$STATE'" 211 fi 212 213 # FIXME: this test is specific to how the 214 # C reducer stores state (redundantly!), should converge eventually! 215 216 #TMETHOD=$(jq -r -e '.policies[0].methods[0].truth.type' < $TFILE) 217 #if test $TMETHOD != "question" 218 #then 219 # exit_fail "Expected method to be >='question', got $TMETHOD" 220 #fi 221 # 222 #echo " OK" 223 224 225 #Pay 226 227 echo -en "${COLOR}${BOLD}Withdrawing amount to wallet ...${NORM}${NOCOLOR}" 228 229 EXCHANGE_URL="$(taler-exchange-config -c "$CONF" -s exchange -o BASE_URL)" 230 231 rm "$WALLET_DB" 232 taler-wallet-cli \ 233 --no-throttle \ 234 --wallet-db="$WALLET_DB" \ 235 api \ 236 --expect-success 'withdrawTestBalance' \ 237 "$(jq -n ' 238 { 239 amount: "TESTKUDOS:40", 240 corebankApiBaseUrl: $BANK_URL, 241 exchangeBaseUrl: $EXCHANGE_URL 242 }' \ 243 --arg BANK_URL "${BANK_URL}" \ 244 --arg EXCHANGE_URL "${EXCHANGE_URL}" 245 )" 2>wallet-withdraw.err \ 246 >wallet-withdraw.log 247 taler-wallet-cli \ 248 --no-throttle \ 249 --wallet-db="$WALLET_DB" \ 250 run-until-done \ 251 2>wallet-withdraw-finish.err \ 252 >wallet-withdraw-finish.log 253 254 echo " OK" 255 256 echo -en "${COLOR}${BOLD}Making payments for truth uploads ... ${NORM}${NOCOLOR}" 257 OBJECT_SIZE=$(jq -r -e '.payments | length' < "$TFILE") 258 for ((INDEX=0; INDEX < "$OBJECT_SIZE"; INDEX++)) 259 do 260 PAY_URI=$(jq --argjson INDEX $INDEX -r -e '.payments[$INDEX]' < "$TFILE") 261 # run wallet CLI 262 echo -n "$INDEX" 263 taler-wallet-cli \ 264 --no-throttle \ 265 --wallet-db="$WALLET_DB" \ 266 handle-uri "${PAY_URI}" \ 267 -y \ 268 2>wallet-pay1.err \ 269 >wallet-pay1.log 270 echo -n "," 271 done 272 echo " OK" 273 echo -e "${COLOR}${BOLD}Running wallet run-until-done...${NORM}${NOCOLOR}" 274 taler-wallet-cli \ 275 --wallet-db="$WALLET_DB" \ 276 run-until-done \ 277 2>wallet-pay-finish.err \ 278 >wallet-pay-finish.log 279 echo -e "${COLOR}${BOLD}Payments done${NORM}${NOCOLOR}" 280 281 282 echo -en "${COLOR}${BOLD}Try to upload again ...${NORM}${NOCOLOR}" 283 $PREFIX anastasis-reducer pay "$TFILE" "$UFILE" 284 mv "$UFILE" "$TFILE" 285 echo " OK" 286 287 288 STATE="$(jq -r -e .backup_state < "$TFILE")" 289 if [ "$STATE" != "POLICIES_PAYING" ] 290 then 291 exit_fail "Expected new state to be 'POLICIES_PAYING', got '$STATE'" 292 fi 293 294 echo -en "${COLOR}${BOLD}Making payments for policy uploads ... ${NORM}${NOCOLOR}" 295 OBJECT_SIZE="$(jq -r -e '.policy_payment_requests | length' < "$TFILE")" 296 for ((INDEX=0; INDEX < "$OBJECT_SIZE"; INDEX++)) 297 do 298 PAY_URI="$(jq --argjson INDEX "$INDEX" -r -e '.policy_payment_requests[$INDEX].payto' < "$TFILE")" 299 # run wallet CLI 300 export PAY_URI 301 echo -n "$INDEX" 302 taler-wallet-cli \ 303 --wallet-db="$WALLET_DB" \ 304 handle-uri "$PAY_URI" \ 305 -y \ 306 2>"wallet-pay2-$INDEX.err" \ 307 >"wallet-pay2-$INDEX.log" 308 echo -n "," 309 done 310 echo " OK" 311 echo -e "${COLOR}${BOLD}Running wallet run-until-done...${NORM}${NOCOLOR}" 312 taler-wallet-cli \ 313 --wallet-db="$WALLET_DB" \ 314 run-until-done \ 315 2>wallet-pay2-finish.err \ 316 >wallet-pay2-finish.log 317 echo -e "${COLOR}${BOLD}Payments done${NORM}${NOCOLOR}" 318 319 echo -en "${COLOR}${BOLD}Try to upload again ...${NORM}${NOCOLOR}" 320 $PREFIX anastasis-reducer pay "$TFILE" "$UFILE" 321 322 echo " OK" 323 324 echo -n "Final checks ..." 325 326 STATE=$(jq -r -e .backup_state < "$UFILE") 327 if [ "$STATE" != "BACKUP_FINISHED" ] 328 then 329 exit_fail "Expected new state to be BACKUP_FINISHED, got $STATE" 330 fi 331 332 jq -r -e .core_secret \ 333 < "$UFILE" \ 334 > /dev/null \ 335 && exit_fail "'core_secret' was not cleared upon success" 336 337 echo " OK" 338 exit 0