From 62dcad6a60a3a74ff8933e11965f450c1ad5952c Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Mon, 17 Jul 2023 16:18:14 +0200 Subject: [PATCH 1/3] -work on auditor test --- src/auditor/generate-auditor-basedb.sh | 2 + src/auditor/test-auditor.sh | 1177 ++++++++++++++---------- 2 files changed, 700 insertions(+), 479 deletions(-) diff --git a/src/auditor/generate-auditor-basedb.sh b/src/auditor/generate-auditor-basedb.sh index 2dc2b2a48..3dae047c5 100755 --- a/src/auditor/generate-auditor-basedb.sh +++ b/src/auditor/generate-auditor-basedb.sh @@ -75,6 +75,8 @@ mkdir -p "$(dirname "$BASEDB")" echo "Dumping database ${BASEDB}.sql" pg_dump -O "auditor-basedb" | sed -e '/AS integer/d' > "${BASEDB}.sql" +cp "${CONF}.edited" "${BASEDB}.conf" +cp "$(taler-config -s exchange-offline -o MASTER_PRIV_FILE -f)" "${BASEDB}.mpriv" # clean up echo -n "Final clean up ..." diff --git a/src/auditor/test-auditor.sh b/src/auditor/test-auditor.sh index da9a90e02..b9b7792d6 100755 --- a/src/auditor/test-auditor.sh +++ b/src/auditor/test-auditor.sh @@ -15,6 +15,10 @@ # TALER; see the file COPYING. If not, If not, see # # +# shellcheck disable=SC2317 +# shellcheck disable=SC1091 +# +# # Setup database which was generated from a perfectly normal # exchange-wallet interaction and run the auditor against it. # @@ -26,7 +30,7 @@ set -eu # Set of numbers for all the testcases. # When adding new tests, increase the last number: -ALL_TESTS=`seq 0 33` +ALL_TESTS=$(seq 0 33) # $TESTS determines which tests we should run. # This construction is used to make it easy to @@ -57,19 +61,19 @@ function stop_libeufin() echo "Stopping libeufin..." if test -f ${MYDIR:-/}/libeufin-sandbox.pid then - PID=`cat ${MYDIR}/libeufin-sandbox.pid 2> /dev/null` + PID=$(cat ${MYDIR}/libeufin-sandbox.pid 2> /dev/null) echo "Killing libeufin sandbox $PID" - rm ${MYDIR}/libeufin-sandbox.pid - kill $PID 2> /dev/null || true - wait $PID || true + rm "${MYDIR}/libeufin-sandbox.pid" + kill "$PID" 2> /dev/null || true + wait "$PID" || true fi if test -f ${MYDIR:-/}/libeufin-nexus.pid then - PID=`cat ${MYDIR}/libeufin-nexus.pid 2> /dev/null` + PID=$(cat ${MYDIR}/libeufin-nexus.pid 2> /dev/null) echo "Killing libeufin nexus $PID" - rm ${MYDIR}/libeufin-nexus.pid - kill $PID 2> /dev/null || true - wait $PID || true + rm "${MYDIR}/libeufin-nexus.pid" + kill "$PID" 2> /dev/null || true + wait "$PID" || true fi echo "Stopping libeufin DONE" } @@ -80,8 +84,8 @@ function cleanup() if test ! -z "${EPID:-}" then echo -n "Stopping exchange $EPID..." - kill -TERM $EPID - wait $EPID || true + kill -TERM "$EPID" + wait "$EPID" || true echo "DONE" unset EPID fi @@ -95,12 +99,17 @@ function exit_cleanup() if test ! -z "${POSTGRES_PATH:-}" then echo "Stopping Postgres at ${POSTGRES_PATH}" - ${POSTGRES_PATH}/pg_ctl -D $TMPDIR -l /dev/null stop &> /dev/null || true + "${POSTGRES_PATH}/pg_ctl" \ + -D "$TMPDIR" \ + -l /dev/null \ + stop \ + &> /dev/null \ + || true fi cleanup - for n in `jobs -p` + for n in $(jobs -p) do - kill $n 2> /dev/null || true + kill "$n" 2> /dev/null || true done wait || true echo "DONE" @@ -110,29 +119,33 @@ function exit_cleanup() trap exit_cleanup EXIT function launch_libeufin () { - cd $MYDIR - export LIBEUFIN_SANDBOX_DB_CONNECTION="jdbc:sqlite:${DB}-sandbox.sqlite3" + cd "$MYDIR" +# shellcheck disable=SC2016 + export LIBEUFIN_SANDBOX_DB_CONNECTION='jdbc:postgresql://localhost/auditor-basedb?socketFactory=org.newsclub.net.unix.AFUNIXSocketFactory$FactoryArg&socketFactoryArg=/var/run/postgresql/.s.PGSQL.5432' libeufin-sandbox serve --no-auth --port 18082 \ - > ${MYDIR}/libeufin-sandbox-stdout.log \ - 2> ${MYDIR}/libeufin-sandbox-stderr.log & - echo $! > ${MYDIR}/libeufin-sandbox.pid - export LIBEUFIN_NEXUS_DB_CONNECTION="jdbc:sqlite:${DB}-nexus.sqlite3" + > "${MYDIR}/libeufin-sandbox-stdout.log" \ + 2> "${MYDIR}/libeufin-sandbox-stderr.log" & + echo $! > "${MYDIR}/libeufin-sandbox.pid" +# shellcheck disable=SC2016 + export LIBEUFIN_NEXUS_DB_CONNECTION='jdbc:postgresql://localhost/auditor-basedb?socketFactory=org.newsclub.net.unix.AFUNIXSocketFactory$FactoryArg&socketFactoryArg=/var/run/postgresql/.s.PGSQL.5432' libeufin-nexus serve --port 8082 \ - 2> ${MYDIR}/libeufin-nexus-stderr.log \ - > ${MYDIR}/libeufin-nexus-stdout.log & - echo $! > ${MYDIR}/libeufin-nexus.pid - cd $ORIGIN + 2> "${MYDIR}/libeufin-nexus-stderr.log" \ + > "${MYDIR}/libeufin-nexus-stdout.log" & + echo $! > "${MYDIR}/libeufin-nexus.pid" + cd "$ORIGIN" } # Downloads new transactions from the bank. function nexus_fetch_transactions () { - export LIBEUFIN_NEXUS_USERNAME=exchange - export LIBEUFIN_NEXUS_PASSWORD=x - export LIBEUFIN_NEXUS_URL=http://localhost:8082/ - cd $MY_TMP_DIR + export LIBEUFIN_NEXUS_USERNAME="exchange" + export LIBEUFIN_NEXUS_PASSWORD="x" + export LIBEUFIN_NEXUS_URL="http://localhost:8082/" + cd "$MY_TMP_DIR" libeufin-cli accounts fetch-transactions \ - --range-type since-last --level report exchange-nexus > /dev/null - cd $ORIGIN + --range-type since-last \ + --level report \ + exchange-nexus > /dev/null + cd "$ORIGIN" unset LIBEUFIN_NEXUS_USERNAME unset LIBEUFIN_NEXUS_PASSWORD unset LIBEUFIN_NEXUS_URL @@ -142,12 +155,13 @@ function nexus_fetch_transactions () { # Instruct Nexus to all the prepared payments (= those # POSTed to /transfer by the exchange). function nexus_submit_to_sandbox () { - export LIBEUFIN_NEXUS_USERNAME=exchange - export LIBEUFIN_NEXUS_PASSWORD=x - export LIBEUFIN_NEXUS_URL=http://localhost:8082/ - cd $MY_TMP_DIR - libeufin-cli accounts submit-payments exchange-nexus - cd $ORIGIN + export LIBEUFIN_NEXUS_USERNAME="exchange" + export LIBEUFIN_NEXUS_PASSWORD="x" + export LIBEUFIN_NEXUS_URL="http://localhost:8082/" + cd "$MY_TMP_DIR" + libeufin-cli accounts submit-payments\ + exchange-nexus + cd "$ORIGIN" unset LIBEUFIN_NEXUS_USERNAME unset LIBEUFIN_NEXUS_PASSWORD unset LIBEUFIN_NEXUS_URL @@ -158,44 +172,67 @@ function nexus_submit_to_sandbox () { function pre_audit () { # Launch bank echo -n "Launching bank" - EXCHANGE_URL=`taler-config -c $CONF -s EXCHANGE -o BASE_URL` launch_libeufin - for n in `seq 1 80` + for n in $(seq 1 80) do echo -n "." sleep 0.1 OK=1 - wget http://localhost:18082/ -o /dev/null -O /dev/null >/dev/null && break + wget http://localhost:18082/ \ + -o /dev/null \ + -O /dev/null \ + >/dev/null \ + && break OK=0 done - if [ 1 != $OK ] + if [ 1 != "$OK" ] then exit_skip "Failed to launch Sandbox" fi - sleep $LIBEUFIN_SETTLE_TIME - for n in `seq 1 80` + sleep "$LIBEUFIN_SETTLE_TIME" + for n in $(seq 1 80) do echo -n "." sleep 0.1 OK=1 - wget http://localhost:8082/ -o /dev/null -O /dev/null >/dev/null && break + wget http://localhost:8082/ \ + -o /dev/null \ + -O /dev/null \ + >/dev/null \ + && break OK=0 done - if [ 1 != $OK ] + if [ 1 != "$OK" ] then exit_skip "Failed to launch Nexus" fi echo " DONE" - if test ${1:-no} = "aggregator" + if test "${1:-no}" = "aggregator" then echo -n "Running exchange aggregator ..." - taler-exchange-aggregator -y -L INFO -t -c $CONF 2> ${MY_TMP_DIR}/aggregator.log || exit_fail "FAIL" + taler-exchange-aggregator \ + -y \ + -L INFO \ + -t \ + -c "$CONF" \ + 2> "${MY_TMP_DIR}/aggregator.log" \ + || exit_fail "FAIL" echo " DONE" echo -n "Running exchange closer ..." - taler-exchange-closer -L INFO -t -c $CONF 2> ${MY_TMP_DIR}/closer.log || exit_fail "FAIL" + taler-exchange-closer \ + -L INFO\ + -t \ + -c "$CONF" \ + 2> "${MY_TMP_DIR}/closer.log" \ + || exit_fail "FAIL" echo " DONE" echo -n "Running exchange transfer ..." - taler-exchange-transfer -L INFO -t -c $CONF 2> ${MY_TMP_DIR}/transfer.log || exit_fail "FAIL" + taler-exchange-transfer \ + -L INFO \ + -t \ + -c "$CONF" \ + 2> "${MY_TMP_DIR}/transfer.log" \ + || exit_fail "FAIL" echo " DONE" echo -n "Running Nexus payment submitter ..." nexus_submit_to_sandbox @@ -213,32 +250,97 @@ function audit_only () { echo -n "Running audit(s) ..." # Restart so that first run is always fresh, and second one is incremental - taler-auditor-dbinit -r -c $CONF - $VALGRIND taler-helper-auditor-aggregation -L DEBUG -c $CONF -m $MASTER_PUB > test-audit-aggregation.json 2> ${MY_TMP_DIR}/test-audit-aggregation.log || exit_fail "aggregation audit failed" + taler-auditor-dbinit \ + -r \ + -c "$CONF" + $VALGRIND taler-helper-auditor-aggregation \ + -L DEBUG \ + -c "$CONF" \ + -m "$MASTER_PUB" \ + > test-audit-aggregation.json \ + 2> "${MY_TMP_DIR}/test-audit-aggregation.log" \ + || exit_fail "aggregation audit failed" echo -n "." - $VALGRIND taler-helper-auditor-aggregation -L DEBUG -c $CONF -m $MASTER_PUB > test-audit-aggregation-inc.json 2> ${MY_TMP_DIR}/test-audit-aggregation-inc.log || exit_fail "incremental aggregation audit failed" + $VALGRIND taler-helper-auditor-aggregation \ + -L DEBUG \ + -c "$CONF" \ + -m "$MASTER_PUB" \ + > test-audit-aggregation-inc.json \ + 2> "${MY_TMP_DIR}/test-audit-aggregation-inc.log" \ + || exit_fail "incremental aggregation audit failed" echo -n "." - $VALGRIND taler-helper-auditor-coins -L DEBUG -c $CONF -m $MASTER_PUB > test-audit-coins.json 2> ${MY_TMP_DIR}/test-audit-coins.log || exit_fail "coin audit failed" + $VALGRIND taler-helper-auditor-coins \ + -L DEBUG \ + -c "$CONF" \ + -m "$MASTER_PUB" \ + > test-audit-coins.json \ + 2> "${MY_TMP_DIR}/test-audit-coins.log" \ + || exit_fail "coin audit failed" echo -n "." - $VALGRIND taler-helper-auditor-coins -L DEBUG -c $CONF -m $MASTER_PUB > test-audit-coins-inc.json 2> ${MY_TMP_DIR}/test-audit-coins-inc.log || exit_fail "incremental coin audit failed" + $VALGRIND taler-helper-auditor-coins \ + -L DEBUG \ + -c "$CONF" \ + -m "$MASTER_PUB" \ + > test-audit-coins-inc.json \ + 2> "${MY_TMP_DIR}/test-audit-coins-inc.log" \ + || exit_fail "incremental coin audit failed" echo -n "." - $VALGRIND taler-helper-auditor-deposits -L DEBUG -c $CONF -m $MASTER_PUB > test-audit-deposits.json 2> ${MY_TMP_DIR}/test-audit-deposits.log || exit_fail "deposits audit failed" + $VALGRIND taler-helper-auditor-deposits \ + -L DEBUG \ + -c "$CONF" \ + -m "$MASTER_PUB" \ + > test-audit-deposits.json \ + 2> "${MY_TMP_DIR}/test-audit-deposits.log" \ + || exit_fail "deposits audit failed" echo -n "." - $VALGRIND taler-helper-auditor-deposits -L DEBUG -c $CONF -m $MASTER_PUB > test-audit-deposits-inc.json 2> ${MY_TMP_DIR}/test-audit-deposits-inc.log || exit_fail "incremental deposits audit failed" + $VALGRIND taler-helper-auditor-deposits \ + -L DEBUG \ + -c "$CONF" \ + -m "$MASTER_PUB" \ + > test-audit-deposits-inc.json \ + 2> "${MY_TMP_DIR}/test-audit-deposits-inc.log" \ + || exit_fail "incremental deposits audit failed" echo -n "." - $VALGRIND taler-helper-auditor-reserves -i -L DEBUG -c $CONF -m $MASTER_PUB > test-audit-reserves.json 2> ${MY_TMP_DIR}/test-audit-reserves.log || exit_fail "reserves audit failed" + $VALGRIND taler-helper-auditor-reserves \ + -i \ + -L DEBUG \ + -c "$CONF" \ + -m "$MASTER_PUB" \ + > test-audit-reserves.json \ + 2> "${MY_TMP_DIR}/test-audit-reserves.log" \ + || exit_fail "reserves audit failed" echo -n "." - $VALGRIND taler-helper-auditor-reserves -i -L DEBUG -c $CONF -m $MASTER_PUB > test-audit-reserves-inc.json 2> ${MY_TMP_DIR}/test-audit-reserves-inc.log || exit_fail "incremental reserves audit failed" + $VALGRIND taler-helper-auditor-reserves \ + -i \ + -L DEBUG \ + -c "$CONF" \ + -m "$MASTER_PUB" \ + > test-audit-reserves-inc.json \ + 2> "${MY_TMP_DIR}/test-audit-reserves-inc.log" \ + || exit_fail "incremental reserves audit failed" echo -n "." - rm -f ${MY_TMP_DIR}/test-wire-audit.log + rm -f "${MY_TMP_DIR}/test-wire-audit.log" thaw() { - $VALGRIND taler-helper-auditor-wire -i -L DEBUG -c $CONF -m $MASTER_PUB > test-audit-wire.json 2>> ${MY_TMP_DIR}/test-wire-audit.log + $VALGRIND taler-helper-auditor-wire \ + -i \ + -L DEBUG \ + -c "$CONF" \ + -m "$MASTER_PUB" \ + > test-audit-wire.json \ + 2>> "${MY_TMP_DIR}/test-wire-audit.log" } - thaw || ( echo -e " FIRST CALL TO taler-helper-auditor-wire FAILED,\nRETRY AFTER TWO SECONDS..." | tee -a ${MY_TMP_DIR}/test-wire-audit.log + thaw || ( echo -e " FIRST CALL TO taler-helper-auditor-wire FAILED,\nRETRY AFTER TWO SECONDS..." | tee -a "${MY_TMP_DIR}/test-wire-audit.log" sleep 2 thaw || exit_fail "wire audit failed" ) echo -n "." - $VALGRIND taler-helper-auditor-wire -i -L DEBUG -c $CONF -m $MASTER_PUB > test-audit-wire-inc.json 2> ${MY_TMP_DIR}/test-wire-audit-inc.log || exit_fail "wire audit inc failed" + $VALGRIND taler-helper-auditor-wire \ + -i \ + -L DEBUG \ + -c "$CONF" \ + -m "$MASTER_PUB" \ + > test-audit-wire-inc.json \ + 2> "${MY_TMP_DIR}/test-wire-audit-inc.log" \ + || exit_fail "wire audit inc failed" echo -n "." echo " DONE" @@ -247,16 +349,30 @@ function audit_only () { # Cleanup to run after the auditor function post_audit () { - taler-exchange-dbinit -c $CONF -g || exit_fail "exchange DB GC failed" + taler-exchange-dbinit \ + -c "$CONF" \ + -g \ + || exit_fail "exchange DB GC failed" cleanup echo -n "TeXing ." - taler-helper-auditor-render.py test-audit-aggregation.json test-audit-coins.json test-audit-deposits.json test-audit-reserves.json test-audit-wire.json < ../../contrib/auditor-report.tex.j2 > test-report.tex || exit_fail "Renderer failed" + taler-helper-auditor-render.py \ + test-audit-aggregation.json \ + test-audit-coins.json \ + test-audit-deposits.json \ + test-audit-reserves.json \ + test-audit-wire.json \ + < ../../contrib/auditor-report.tex.j2 \ + > test-report.tex \ + || exit_fail "Renderer failed" echo -n "." - timeout 10 pdflatex test-report.tex >/dev/null || exit_fail "pdflatex failed" + timeout 10 pdflatex test-report.tex \ + >/dev/null \ + || exit_fail "pdflatex failed" echo -n "." - timeout 10 pdflatex test-report.tex >/dev/null + timeout 10 pdflatex test-report.tex \ + >/dev/null echo " DONE" } @@ -267,21 +383,28 @@ function post_audit () { # before auditor (to trigger pending wire transfers). # Pass "drain" as $2 to run a drain operation as well. function run_audit () { - pre_audit ${1:-no} - if test ${2:-no} = "drain" + pre_audit "${1:-no}" + if test "${2:-no}" = "drain" then echo -n "Starting exchange..." - taler-exchange-httpd -c "${CONF}" -L INFO 2> ${MYDIR}/exchange-httpd-drain.err & + taler-exchange-httpd \ + -c "${CONF}" \ + -L INFO \ + 2> "${MYDIR}/exchange-httpd-drain.err" & EPID=$! # Wait for all services to be available - for n in `seq 1 50` + for n in $(seq 1 50) do echo -n "." sleep 0.1 OK=0 # exchange - wget http://localhost:8081/seed -o /dev/null -O /dev/null >/dev/null || continue + wget "http://localhost:8081/seed" \ + -o /dev/null \ + -O /dev/null \ + >/dev/null \ + || continue OK=1 break done @@ -290,40 +413,61 @@ function run_audit () { echo -n "Running taler-exchange-offline drain " - taler-exchange-offline -L DEBUG -c "${CONF}" \ - drain TESTKUDOS:0.1 exchange-account-1 payto://iban/SANDBOXX/DE360679?receiver-name=Exchange+Drain \ - upload \ - 2> ${MY_TMP_DIR}/taler-exchange-offline-drain.log || exit_fail "offline draining failed" - kill -TERM $EPID - wait $EPID || true + taler-exchange-offline \ + -L DEBUG \ + -c "${CONF}" \ + drain TESTKUDOS:0.1 \ + exchange-account-1 payto://iban/SANDBOXX/DE360679?receiver-name=Exchange+Drain \ + upload \ + 2> "${MY_TMP_DIR}/taler-exchange-offline-drain.log" \ + || exit_fail "offline draining failed" + kill -TERM "$EPID" + wait "$EPID" || true unset EPID echo -n "Running taler-exchange-drain ..." - echo "\n" | taler-exchange-drain -L DEBUG -c $CONF 2> ${MY_TMP_DIR}/taler-exchange-drain.log || exit_fail "FAIL" + printf "\n" | taler-exchange-drain \ + -L DEBUG \ + -c "$CONF" \ + 2> "${MY_TMP_DIR}/taler-exchange-drain.log" \ + || exit_fail "FAIL" echo " DONE" echo -n "Running taler-exchange-transfer ..." - taler-exchange-transfer -L INFO -t -c $CONF 2> ${MY_TMP_DIR}/drain-transfer.log || exit_fail "FAIL" + taler-exchange-transfer \ + -L INFO \ + -t \ + -c "$CONF" \ + 2> "${MY_TMP_DIR}/drain-transfer.log" \ + || exit_fail "FAIL" echo " DONE" - export LIBEUFIN_NEXUS_USERNAME=exchange - export LIBEUFIN_NEXUS_PASSWORD=x - export LIBEUFIN_NEXUS_URL=http://localhost:8082/ - cd $MY_TMP_DIR - PAIN_UUID=`libeufin-cli accounts list-payments exchange-nexus | jq .initiatedPayments[] | jq 'select(.submitted==false)' | jq -r .paymentInitiationId` + export LIBEUFIN_NEXUS_USERNAME="exchange" + export LIBEUFIN_NEXUS_PASSWORD="x" + export LIBEUFIN_NEXUS_URL="http://localhost:8082/" + cd "$MY_TMP_DIR" + PAIN_UUID=$(libeufin-cli accounts list-payments exchange-nexus | jq .initiatedPayments[] | jq 'select(.submitted==false)' | jq -r .paymentInitiationId) if test -z "${PAIN_UUID}" then echo -n "Payment likely already submitted, running submit-payments without UUID anyway ..." - libeufin-cli accounts submit-payments exchange-nexus + libeufin-cli accounts \ + submit-payments \ + exchange-nexus else echo -n "Running payment submission for transaction ${PAIN_UUID} ..." - libeufin-cli accounts submit-payments --payment-uuid ${PAIN_UUID} exchange-nexus + libeufin-cli accounts \ + submit-payments \ + --payment-uuid "${PAIN_UUID}" \ + exchange-nexus fi echo " DONE" echo -n "Import outgoing transactions..." - libeufin-cli accounts fetch-transactions \ - --range-type since-last --level report exchange-nexus + libeufin-cli accounts \ + fetch-transactions \ + --range-type since-last \ + --level report \ + exchange-nexus echo " DONE" - cd $ORIGIN + cd "$ORIGIN" fi audit_only post_audit @@ -334,21 +478,19 @@ function run_audit () { function full_reload() { echo "Doing full reload of the database ($BASEDB - $DB)... " - dropdb $DB 2> /dev/null || true - createdb -T template0 $DB || exit_skip "could not create database $DB (at $PGHOST)" + dropdb "$DB" 2> /dev/null || true + createdb -T template0 "$DB" \ + || exit_skip "could not create database $DB (at $PGHOST)" # Import pre-generated database, -q(ietly) using single (-1) transaction - psql -Aqt $DB -q -1 -f ${BASEDB}.sql > /dev/null || exit_skip "Failed to load database $DB from ${BASEDB}.sql" + psql -Aqt "$DB" \ + -q \ + -1 \ + -f "${BASEDB}.sql" \ + > /dev/null \ + || exit_skip "Failed to load database $DB from ${BASEDB}.sql" echo "DONE" # Technically, this call shouldn't be needed as libeufin should already be stopped here... stop_libeufin - cd $MYDIR - rm -f ${DB}-nexus.sqlite3 ${DB}-sandbox.sqlite3 2> /dev/null || true # libeufin - echo -n "Loading libeufin Nexus basedb: ${BASEDB}-libeufin-nexus.sql " - sqlite3 ${DB}-nexus.sqlite3 < ${BASEDB}-libeufin-nexus.sql || exit_skip "Failed to load Nexus database" - echo "DONE" - echo -n "Loading libeufin Sandbox basedb: ${BASEDB}-libeufin-sandbox.sql " - sqlite3 ${DB}-sandbox.sqlite3 < ${BASEDB}-libeufin-sandbox.sql || exit_skip "Failed to load Sandbox database" - cd $ORIGIN echo "DONE" } @@ -382,78 +524,78 @@ function test_0() { echo PASS - LOSS=`jq -r .total_bad_sig_loss < test-audit-aggregation.json` - if test $LOSS != "TESTKUDOS:0" + LOSS=$(jq -r .total_bad_sig_loss < test-audit-aggregation.json) + if [ "$LOSS" != "TESTKUDOS:0" ] then exit_fail "Wrong total bad sig loss from aggregation, got unexpected loss of $LOSS" fi - LOSS=`jq -r .irregular_loss < test-audit-coins.json` - if test $LOSS != "TESTKUDOS:0" + LOSS=$(jq -r .irregular_loss < test-audit-coins.json) + if [ "$LOSS" != "TESTKUDOS:0" ] then exit_fail "Wrong total bad sig loss from coins, got unexpected loss of $LOSS" fi - LOSS=`jq -r .total_bad_sig_loss < test-audit-reserves.json` - if test $LOSS != "TESTKUDOS:0" + LOSS=$(jq -r .total_bad_sig_loss < test-audit-reserves.json) + if [ "$LOSS" != "TESTKUDOS:0" ] then exit_fail "Wrong total bad sig loss from reserves, got unexpected loss of $LOSS" fi echo -n "Test for wire amounts... " - WIRED=`jq -r .total_wire_in_delta_plus < test-audit-wire.json` - if test $WIRED != "TESTKUDOS:0" + WIRED=$(jq -r .total_wire_in_delta_plus < test-audit-wire.json) + if [ "$WIRED" != "TESTKUDOS:0" ] then exit_fail "Expected total wire delta plus wrong, got $WIRED" fi - WIRED=`jq -r .total_wire_in_delta_minus < test-audit-wire.json` - if test $WIRED != "TESTKUDOS:0" + WIRED=$(jq -r .total_wire_in_delta_minus < test-audit-wire.json) + if [ "$WIRED" != "TESTKUDOS:0" ] then exit_fail "Expected total wire delta minus wrong, got $WIRED" fi - WIRED=`jq -r .total_wire_out_delta_plus < test-audit-wire.json` - if test $WIRED != "TESTKUDOS:0" + WIRED=$(jq -r .total_wire_out_delta_plus < test-audit-wire.json) + if [ "$WIRED" != "TESTKUDOS:0" ] then exit_fail "Expected total wire delta plus wrong, got $WIRED" fi - WIRED=`jq -r .total_wire_out_delta_minus < test-audit-wire.json` - if test $WIRED != "TESTKUDOS:0" + WIRED=$(jq -r .total_wire_out_delta_minus < test-audit-wire.json) + if [ "$WIRED" != "TESTKUDOS:0" ] then exit_fail "Expected total wire delta minus wrong, got $WIRED" fi - WIRED=`jq -r .total_misattribution_in < test-audit-wire.json` - if test $WIRED != "TESTKUDOS:0" + WIRED=$(jq -r .total_misattribution_in < test-audit-wire.json) + if [ "$WIRED" != "TESTKUDOS:0" ] then exit_fail "Expected total misattribution in wrong, got $WIRED" fi - echo PASS + echo "PASS" echo -n "Checking for unexpected arithmetic differences " - LOSS=`jq -r .total_arithmetic_delta_plus < test-audit-aggregation.json` - if test $LOSS != "TESTKUDOS:0" + LOSS=$(jq -r .total_arithmetic_delta_plus < test-audit-aggregation.json) + if [ "$LOSS" != "TESTKUDOS:0" ] then exit_fail "Wrong arithmetic delta from aggregations, got unexpected plus of $LOSS" fi - LOSS=`jq -r .total_arithmetic_delta_minus < test-audit-aggregation.json` - if test $LOSS != "TESTKUDOS:0" + LOSS=$(jq -r .total_arithmetic_delta_minus < test-audit-aggregation.json) + if [ "$LOSS" != "TESTKUDOS:0" ] then exit_fail "Wrong arithmetic delta from aggregation, got unexpected minus of $LOSS" fi - LOSS=`jq -r .total_arithmetic_delta_plus < test-audit-coins.json` - if test $LOSS != "TESTKUDOS:0" + LOSS=$(jq -r .total_arithmetic_delta_plus < test-audit-coins.json) + if [ "$LOSS" != "TESTKUDOS:0" ] then exit_fail "Wrong arithmetic delta from coins, got unexpected plus of $LOSS" fi - LOSS=`jq -r .total_arithmetic_delta_minus < test-audit-coins.json` - if test $LOSS != "TESTKUDOS:0" + LOSS=$(jq -r .total_arithmetic_delta_minus < test-audit-coins.json) + if [ "$LOSS" != "TESTKUDOS:0" ] then exit_fail "Wrong arithmetic delta from coins, got unexpected minus of $LOSS" fi - LOSS=`jq -r .total_arithmetic_delta_plus < test-audit-reserves.json` - if test $LOSS != "TESTKUDOS:0" + LOSS=$(jq -r .total_arithmetic_delta_plus < test-audit-reserves.json) + if [ "$LOSS" != "TESTKUDOS:0" ] then exit_fail "Wrong arithmetic delta from reserves, got unexpected plus of $LOSS" fi - LOSS=`jq -r .total_arithmetic_delta_minus < test-audit-reserves.json` - if test $LOSS != "TESTKUDOS:0" + LOSS=$(jq -r .total_arithmetic_delta_minus < test-audit-reserves.json) + if [ "$LOSS" != "TESTKUDOS:0" ] then exit_fail "Wrong arithmetic delta from reserves, got unexpected minus of $LOSS" fi @@ -461,11 +603,11 @@ function test_0() { jq -e .amount_arithmetic_inconsistencies[0] < test-audit-aggregation.json > /dev/null && exit_fail "Unexpected arithmetic inconsistencies from aggregations detected in ordinary run" jq -e .amount_arithmetic_inconsistencies[0] < test-audit-coins.json > /dev/null && exit_fail "Unexpected arithmetic inconsistencies from coins detected in ordinary run" jq -e .amount_arithmetic_inconsistencies[0] < test-audit-reserves.json > /dev/null && exit_fail "Unexpected arithmetic inconsistencies from reserves detected in ordinary run" - echo PASS + echo "PASS" echo -n "Checking for unexpected wire out differences " jq -e .wire_out_inconsistencies[0] < test-audit-aggregation.json > /dev/null && exit_fail "Unexpected wire out inconsistencies detected in ordinary run" - echo PASS + echo "PASS" # cannot easily undo aggregator, hence full reload full_reload @@ -483,22 +625,48 @@ function test_1() { echo "Checking output" # if an emergency was detected, that is a bug and we should fail echo -n "Test for emergencies... " - jq -e .emergencies[0] < test-audit-coins.json > /dev/null && exit_fail "Unexpected emergency detected in ordinary run" || echo PASS + jq -e .emergencies[0] \ + < test-audit-coins.json \ + > /dev/null \ + && exit_fail "Unexpected emergency detected in ordinary run"; + echo "PASS" echo -n "Test for emergencies by count... " - jq -e .emergencies_by_count[0] < test-audit-coins.json > /dev/null && exit_fail "Unexpected emergency by count detected in ordinary run" || echo PASS + jq -e .emergencies_by_count[0] \ + < test-audit-coins.json \ + > /dev/null \ + && exit_fail "Unexpected emergency by count detected in ordinary run" + echo "PASS" echo -n "Test for wire inconsistencies... " - jq -e .wire_out_amount_inconsistencies[0] < test-audit-wire.json > /dev/null && exit_fail "Unexpected wire out inconsistency detected in ordinary run" - jq -e .reserve_in_amount_inconsistencies[0] < test-audit-wire.json > /dev/null && exit_fail "Unexpected reserve in inconsistency detected in ordinary run" - jq -e .misattribution_inconsistencies[0] < test-audit-wire.json > /dev/null && exit_fail "Unexpected misattribution inconsistency detected in ordinary run" - jq -e .row_inconsistencies[0] < test-audit-wire.json > /dev/null && exit_fail "Unexpected row inconsistency detected in ordinary run" - jq -e .row_minor_inconsistencies[0] < test-audit-wire.json > /dev/null && exit_fail "Unexpected minor row inconsistency detected in ordinary run" - jq -e .wire_format_inconsistencies[0] < test-audit-wire.json > /dev/null && exit_fail "Unexpected wire format inconsistencies detected in ordinary run" + jq -e .wire_out_amount_inconsistencies[0] \ + < test-audit-wire.json \ + > /dev/null \ + && exit_fail "Unexpected wire out inconsistency detected in ordinary run" + jq -e .reserve_in_amount_inconsistencies[0] \ + < test-audit-wire.json \ + > /dev/null \ + && exit_fail "Unexpected reserve in inconsistency detected in ordinary run" + jq -e .misattribution_inconsistencies[0] \ + < test-audit-wire.json \ + > /dev/null \ + && exit_fail "Unexpected misattribution inconsistency detected in ordinary run" + jq -e .row_inconsistencies[0] \ + < test-audit-wire.json \ + > /dev/null \ + && exit_fail "Unexpected row inconsistency detected in ordinary run" + jq -e .row_minor_inconsistencies[0] \ + < test-audit-wire.json \ + > /dev/null \ + && exit_fail "Unexpected minor row inconsistency detected in ordinary run" + jq -e .wire_format_inconsistencies[0] \ + < test-audit-wire.json \ + > /dev/null \ + && exit_fail "Unexpected wire format inconsistencies detected in ordinary run" # TODO: check operation balances are correct (once we have all transaction types and wallet is deterministic) # TODO: check revenue summaries are correct (once we have all transaction types and wallet is deterministic) - echo PASS + echo "PASS" echo -n "Check for lag detection... " @@ -507,10 +675,13 @@ function test_1() { # re-generating the test database as we do not # report lag of less than 1h (see GRACE_PERIOD in # taler-helper-auditor-wire.c) - jq -e .lag_details[0] < test-audit-wire.json > /dev/null || exit_fail "Lag not detected in run without aggregator" + jq -e .lag_details[0] \ + < test-audit-wire.json \ + > /dev/null \ + || exit_fail "Lag not detected in run without aggregator" - LAG=`jq -r .total_amount_lag < test-audit-wire.json` - if test $LAG = "TESTKUDOS:0" + LAG=$(jq -r .total_amount_lag < test-audit-wire.json) + if [ "$LAG" = "TESTKUDOS:0" ] then exit_fail "Expected total lag to be non-zero" fi @@ -518,28 +689,28 @@ function test_1() { echo -n "Test for wire amounts... " - WIRED=`jq -r .total_wire_in_delta_plus < test-audit-wire.json` - if test $WIRED != "TESTKUDOS:0" + WIRED=$(jq -r .total_wire_in_delta_plus < test-audit-wire.json) + if [ "$WIRED" != "TESTKUDOS:0" ] then exit_fail "Expected total wire delta plus wrong, got $WIRED" fi - WIRED=`jq -r .total_wire_in_delta_minus < test-audit-wire.json` - if test $WIRED != "TESTKUDOS:0" + WIRED=$(jq -r .total_wire_in_delta_minus < test-audit-wire.json) + if [ "$WIRED" != "TESTKUDOS:0" ] then exit_fail "Expected total wire delta minus wrong, got $WIRED" fi - WIRED=`jq -r .total_wire_out_delta_plus < test-audit-wire.json` - if test $WIRED != "TESTKUDOS:0" + WIRED=$(jq -r .total_wire_out_delta_plus < test-audit-wire.json) + if [ "$WIRED" != "TESTKUDOS:0" ] then exit_fail "Expected total wire delta plus wrong, got $WIRED" fi - WIRED=`jq -r .total_wire_out_delta_minus < test-audit-wire.json` - if test $WIRED != "TESTKUDOS:0" + WIRED=$(jq -r .total_wire_out_delta_minus < test-audit-wire.json) + if [ "$WIRED" != "TESTKUDOS:0" ] then exit_fail "Expected total wire delta minus wrong, got $WIRED" fi - WIRED=`jq -r .total_misattribution_in < test-audit-wire.json` - if test $WIRED != "TESTKUDOS:0" + WIRED=$(jq -r .total_misattribution_in < test-audit-wire.json) + if [ "$WIRED" != "TESTKUDOS:0" ] then exit_fail "Expected total misattribution in wrong, got $WIRED" fi @@ -552,41 +723,43 @@ function test_1() { function test_2() { echo "===========2: reserves_in inconsistency ===========" - echo "UPDATE exchange.reserves_in SET credit_val=5 WHERE reserve_in_serial_id=1" | psql -At $DB + echo "UPDATE exchange.reserves_in SET credit_val=5 WHERE reserve_in_serial_id=1" \ + | psql -At "$DB" run_audit echo -n "Testing inconsistency detection... " - ROW=`jq .reserve_in_amount_inconsistencies[0].row < test-audit-wire.json` - if test $ROW != 1 + ROW=$(jq .reserve_in_amount_inconsistencies[0].row < test-audit-wire.json) + if [ "$ROW" != 1 ] then exit_fail "Row $ROW is wrong" fi - WIRED=`jq -r .reserve_in_amount_inconsistencies[0].amount_wired < test-audit-wire.json` - if test $WIRED != "TESTKUDOS:10" + WIRED=$(jq -r .reserve_in_amount_inconsistencies[0].amount_wired < test-audit-wire.json) + if [ "$WIRED" != "TESTKUDOS:10" ] then exit_fail "Amount wrong" fi - EXPECTED=`jq -r .reserve_in_amount_inconsistencies[0].amount_exchange_expected < test-audit-wire.json` - if test $EXPECTED != "TESTKUDOS:5" + EXPECTED=$(jq -r .reserve_in_amount_inconsistencies[0].amount_exchange_expected < test-audit-wire.json) + if [ "$EXPECTED" != "TESTKUDOS:5" ] then exit_fail "Expected amount wrong" fi - WIRED=`jq -r .total_wire_in_delta_minus < test-audit-wire.json` - if test $WIRED != "TESTKUDOS:0" + WIRED=$(jq -r .total_wire_in_delta_minus < test-audit-wire.json) + if [ "$WIRED" != "TESTKUDOS:0" ] then exit_fail "Wrong total wire_in_delta_minus, got $WIRED" fi - DELTA=`jq -r .total_wire_in_delta_plus < test-audit-wire.json` - if test $DELTA != "TESTKUDOS:5" + DELTA=$(jq -r .total_wire_in_delta_plus < test-audit-wire.json) + if [ "$DELTA" != "TESTKUDOS:5" ] then exit_fail "Expected total wire delta plus wrong, got $DELTA" fi - echo PASS + echo "PASS" # Undo database modification - echo "UPDATE exchange.reserves_in SET credit_val=10 WHERE reserve_in_serial_id=1" | psql -Aqt $DB + echo "UPDATE exchange.reserves_in SET credit_val=10 WHERE reserve_in_serial_id=1" \ + | psql -Aqt "$DB" } @@ -596,60 +769,61 @@ function test_2() { function test_3() { echo "===========3: reserves_in inconsistency===========" - echo "UPDATE exchange.reserves_in SET credit_val=15 WHERE reserve_in_serial_id=1" | psql -Aqt $DB + echo "UPDATE exchange.reserves_in SET credit_val=15 WHERE reserve_in_serial_id=1" \ + | psql -Aqt "$DB" run_audit - EXPECTED=`jq -r .reserve_balance_summary_wrong_inconsistencies[0].auditor < test-audit-reserves.json` - if test $EXPECTED != "TESTKUDOS:5.01" + EXPECTED=$(jq -r .reserve_balance_summary_wrong_inconsistencies[0].auditor < test-audit-reserves.json) + if [ "$EXPECTED" != "TESTKUDOS:5.01" ] then exit_fail "Expected reserve balance summary amount wrong, got $EXPECTED (auditor)" fi - EXPECTED=`jq -r .reserve_balance_summary_wrong_inconsistencies[0].exchange < test-audit-reserves.json` - if test $EXPECTED != "TESTKUDOS:0.01" + EXPECTED=$(jq -r .reserve_balance_summary_wrong_inconsistencies[0].exchange < test-audit-reserves.json) + if [ "$EXPECTED" != "TESTKUDOS:0.01" ] then exit_fail "Expected reserve balance summary amount wrong, got $EXPECTED (exchange)" fi - WIRED=`jq -r .total_irregular_loss < test-audit-reserves.json` - if test $WIRED != "TESTKUDOS:0" + WIRED=$(jq -r .total_irregular_loss < test-audit-reserves.json) + if [ "$WIRED" != "TESTKUDOS:0" ] then exit_fail "Wrong total loss from insufficient balance, got $WIRED" fi - ROW=`jq -e .reserve_in_amount_inconsistencies[0].row < test-audit-wire.json` - if test $ROW != 1 + ROW=$(jq -e .reserve_in_amount_inconsistencies[0].row < test-audit-wire.json) + if [ "$ROW" != 1 ] then exit_fail "Row wrong, got $ROW" fi - WIRED=`jq -r .reserve_in_amount_inconsistencies[0].amount_exchange_expected < test-audit-wire.json` - if test $WIRED != "TESTKUDOS:15" + WIRED=$(jq -r .reserve_in_amount_inconsistencies[0].amount_exchange_expected < test-audit-wire.json) + if [ "$WIRED" != "TESTKUDOS:15" ] then exit_fail "Wrong amount_exchange_expected, got $WIRED" fi - WIRED=`jq -r .reserve_in_amount_inconsistencies[0].amount_wired < test-audit-wire.json` - if test $WIRED != "TESTKUDOS:10" + WIRED=$(jq -r .reserve_in_amount_inconsistencies[0].amount_wired < test-audit-wire.json) + if [ "$WIRED" != "TESTKUDOS:10" ] then exit_fail "Wrong amount_wired, got $WIRED" fi - WIRED=`jq -r .total_wire_in_delta_minus < test-audit-wire.json` - if test $WIRED != "TESTKUDOS:5" + WIRED=$(jq -r .total_wire_in_delta_minus < test-audit-wire.json) + if [ "$WIRED" != "TESTKUDOS:5" ] then exit_fail "Wrong total wire_in_delta_minus, got $WIRED" fi - WIRED=`jq -r .total_wire_in_delta_plus < test-audit-wire.json` - if test $WIRED != "TESTKUDOS:0" + WIRED=$(jq -r .total_wire_in_delta_plus < test-audit-wire.json) + if [ "$WIRED" != "TESTKUDOS:0" ] then exit_fail "Wrong total wire_in_delta_plus, got $WIRED" fi # Undo database modification - echo "UPDATE exchange.reserves_in SET credit_val=10 WHERE reserve_in_serial_id=1" | psql -Aqt $DB + echo "UPDATE exchange.reserves_in SET credit_val=10 WHERE reserve_in_serial_id=1" | psql -Aqt "$DB" } @@ -660,10 +834,16 @@ function test_4() { echo "===========4: deposit wire target wrong=================" # Original target bank account was 43, changing to 44 - SERIAL=`echo "SELECT deposit_serial_id FROM exchange.deposits WHERE amount_with_fee_val=3 AND amount_with_fee_frac=0 ORDER BY deposit_serial_id LIMIT 1" | psql $DB -Aqt` - OLD_WIRE_ID=`echo "SELECT wire_target_h_payto FROM exchange.deposits WHERE deposit_serial_id=${SERIAL};" | psql $DB -Aqt` - NEW_WIRE_ID=`echo "INSERT INTO exchange.wire_targets (payto_uri, wire_target_h_payto) VALUES ('payto://x-taler-bank/localhost/testuser-xxlargtp', '\x1e8f31936b3cee8f8afd3aac9e38b5db42d45b721ffc4eb1e5b9ddaf1565660b');" | psql $DB -Aqt` - echo "UPDATE exchange.deposits SET wire_target_h_payto='\x1e8f31936b3cee8f8afd3aac9e38b5db42d45b721ffc4eb1e5b9ddaf1565660b' WHERE deposit_serial_id=${SERIAL}" | psql -Aqt $DB + SERIAL=$(echo "SELECT deposit_serial_id FROM exchange.deposits WHERE amount_with_fee_val=3 AND amount_with_fee_frac=0 ORDER BY deposit_serial_id LIMIT 1" | psql "$DB" -Aqt) + OLD_WIRE_ID=$(echo "SELECT wire_target_h_payto FROM exchange.deposits WHERE deposit_serial_id=${SERIAL};" | psql "$DB" -Aqt) +# shellcheck disable=SC2028 + echo "INSERT INTO exchange.wire_targets (payto_uri, wire_target_h_payto) VALUES ('payto://x-taler-bank/localhost/testuser-xxlargtp', '\x1e8f31936b3cee8f8afd3aac9e38b5db42d45b721ffc4eb1e5b9ddaf1565660b');" \ + | psql "$DB" \ + -Aqt \ + > /dev/null +# shellcheck disable=SC2028 + echo "UPDATE exchange.deposits SET wire_target_h_payto='\x1e8f31936b3cee8f8afd3aac9e38b5db42d45b721ffc4eb1e5b9ddaf1565660b' WHERE deposit_serial_id=${SERIAL}" \ + | psql -Aqt "$DB" run_audit @@ -671,33 +851,33 @@ function test_4() { jq -e .bad_sig_losses[0] < test-audit-coins.json > /dev/null || exit_fail "Bad signature not detected" - ROW=`jq -e .bad_sig_losses[0].row < test-audit-coins.json` - if test $ROW != ${SERIAL} + ROW=$(jq -e .bad_sig_losses[0].row < test-audit-coins.json) + if [ "$ROW" != "${SERIAL}" ] then exit_fail "Row wrong, got $ROW" fi - LOSS=`jq -r .bad_sig_losses[0].loss < test-audit-coins.json` - if test $LOSS != "TESTKUDOS:3" + LOSS=$(jq -r .bad_sig_losses[0].loss < test-audit-coins.json) + if [ "$LOSS" != "TESTKUDOS:3" ] then exit_fail "Wrong deposit bad signature loss, got $LOSS" fi - OP=`jq -r .bad_sig_losses[0].operation < test-audit-coins.json` - if test $OP != "deposit" + OP=$(jq -r .bad_sig_losses[0].operation < test-audit-coins.json) + if [ "$OP" != "deposit" ] then exit_fail "Wrong operation, got $OP" fi - LOSS=`jq -r .irregular_loss < test-audit-coins.json` - if test $LOSS != "TESTKUDOS:3" + LOSS=$(jq -r .irregular_loss < test-audit-coins.json) + if [ "$LOSS" != "TESTKUDOS:3" ] then exit_fail "Wrong total bad sig loss, got $LOSS" fi echo PASS # Undo: - echo "UPDATE exchange.deposits SET wire_target_h_payto='$OLD_WIRE_ID' WHERE deposit_serial_id=${SERIAL}" | psql -Aqt $DB + echo "UPDATE exchange.deposits SET wire_target_h_payto='$OLD_WIRE_ID' WHERE deposit_serial_id=${SERIAL}" | psql -Aqt "$DB" } @@ -708,40 +888,42 @@ function test_4() { function test_5() { echo "===========5: deposit contract hash wrong=================" # Modify h_wire hash, so it is inconsistent with 'wire' - SERIAL=`echo "SELECT deposit_serial_id FROM exchange.deposits WHERE amount_with_fee_val=3 AND amount_with_fee_frac=0 ORDER BY deposit_serial_id LIMIT 1" | psql $DB -Aqt` - OLD_H=`echo "SELECT h_contract_terms FROM exchange.deposits WHERE deposit_serial_id=$SERIAL;" | psql $DB -Aqt` - echo "UPDATE exchange.deposits SET h_contract_terms='\x12bb676444955c98789f219148aa31899d8c354a63330624d3d143222cf3bb8b8e16f69accd5a8773127059b804c1955696bf551dd7be62719870613332aa8d5' WHERE deposit_serial_id=$SERIAL" | psql -Aqt $DB + SERIAL=$(echo "SELECT deposit_serial_id FROM exchange.deposits WHERE amount_with_fee_val=3 AND amount_with_fee_frac=0 ORDER BY deposit_serial_id LIMIT 1" | psql "$DB" -Aqt) + OLD_H=$(echo "SELECT h_contract_terms FROM exchange.deposits WHERE deposit_serial_id=$SERIAL;" | psql "$DB" -Aqt) +# shellcheck disable=SC2028 + echo "UPDATE exchange.deposits SET h_contract_terms='\x12bb676444955c98789f219148aa31899d8c354a63330624d3d143222cf3bb8b8e16f69accd5a8773127059b804c1955696bf551dd7be62719870613332aa8d5' WHERE deposit_serial_id=$SERIAL" \ + | psql -Aqt "$DB" run_audit echo -n "Checking bad signature detection... " - ROW=`jq -e .bad_sig_losses[0].row < test-audit-coins.json` - if test $ROW != $SERIAL + ROW=$(jq -e .bad_sig_losses[0].row < test-audit-coins.json) + if [ "$ROW" != "$SERIAL" ] then exit_fail "Row wrong, got $ROW" fi - LOSS=`jq -r .bad_sig_losses[0].loss < test-audit-coins.json` - if test $LOSS != "TESTKUDOS:3" + LOSS=$(jq -r .bad_sig_losses[0].loss < test-audit-coins.json) + if [ "$LOSS" != "TESTKUDOS:3" ] then exit_fail "Wrong deposit bad signature loss, got $LOSS" fi - OP=`jq -r .bad_sig_losses[0].operation < test-audit-coins.json` - if test $OP != "deposit" + OP=$(jq -r .bad_sig_losses[0].operation < test-audit-coins.json) + if [ "$OP" != "deposit" ] then exit_fail "Wrong operation, got $OP" fi - LOSS=`jq -r .irregular_loss < test-audit-coins.json` - if test $LOSS != "TESTKUDOS:3" + LOSS=$(jq -r .irregular_loss < test-audit-coins.json) + if [ "$LOSS" != "TESTKUDOS:3" ] then exit_fail "Wrong total bad sig loss, got $LOSS" fi echo PASS # Undo: - echo "UPDATE exchange.deposits SET h_contract_terms='${OLD_H}' WHERE deposit_serial_id=$SERIAL" | psql -Aqt $DB + echo "UPDATE exchange.deposits SET h_contract_terms='${OLD_H}' WHERE deposit_serial_id=$SERIAL" | psql -Aqt "$DB" } @@ -751,38 +933,40 @@ function test_5() { function test_6() { echo "===========6: known_coins signature wrong=================" # Modify denom_sig, so it is wrong - OLD_SIG=`echo 'SELECT denom_sig FROM exchange.known_coins LIMIT 1;' | psql $DB -Aqt` - COIN_PUB=`echo "SELECT coin_pub FROM exchange.known_coins WHERE denom_sig='$OLD_SIG';" | psql $DB -Aqt` - echo "UPDATE exchange.known_coins SET denom_sig='\x0000000100000000287369672d76616c200a2028727361200a2020287320233542383731423743393036444643303442424430453039353246413642464132463537303139374131313437353746324632323332394644443146324643333445393939413336363430334233413133324444464239413833353833464536354442374335434445304441453035374438363336434541423834463843323843344446304144363030343430413038353435363039373833434431333239393736423642433437313041324632414132414435413833303432434346314139464635394244434346374436323238344143354544364131373739463430353032323241373838423837363535453434423145443831364244353638303232413123290a2020290a20290b' WHERE coin_pub='$COIN_PUB'" | psql -Aqt $DB + OLD_SIG=$(echo 'SELECT denom_sig FROM exchange.known_coins LIMIT 1;' | psql "$DB" -Aqt) + COIN_PUB=$(echo "SELECT coin_pub FROM exchange.known_coins WHERE denom_sig='$OLD_SIG';" | psql "$DB" -Aqt) +# shellcheck disable=SC2028 + echo "UPDATE exchange.known_coins SET denom_sig='\x0000000100000000287369672d76616c200a2028727361200a2020287320233542383731423743393036444643303442424430453039353246413642464132463537303139374131313437353746324632323332394644443146324643333445393939413336363430334233413133324444464239413833353833464536354442374335434445304441453035374438363336434541423834463843323843344446304144363030343430413038353435363039373833434431333239393736423642433437313041324632414132414435413833303432434346314139464635394244434346374436323238344143354544364131373739463430353032323241373838423837363535453434423145443831364244353638303232413123290a2020290a20290b' WHERE coin_pub='$COIN_PUB'" \ + | psql -Aqt "$DB" run_audit - ROW=`jq -e .bad_sig_losses[0].row < test-audit-coins.json` - if test $ROW != "1" + ROW=$(jq -e .bad_sig_losses[0].row < test-audit-coins.json) + if [ "$ROW" != "1" ] then exit_fail "Row wrong, got $ROW" fi - LOSS=`jq -r .bad_sig_losses[0].loss < test-audit-coins.json` - if test $LOSS == "TESTKUDOS:0" + LOSS=$(jq -r .bad_sig_losses[0].loss < test-audit-coins.json) + if [ "$LOSS" == "TESTKUDOS:0" ] then exit_fail "Wrong deposit bad signature loss, got $LOSS" fi - OP=`jq -r .bad_sig_losses[0].operation < test-audit-coins.json` - if test $OP != "melt" + OP=$(jq -r .bad_sig_losses[0].operation < test-audit-coins.json) + if [ "$OP" != "melt" ] then exit_fail "Wrong operation, got $OP" fi - LOSS=`jq -r .irregular_loss < test-audit-coins.json` - if test $LOSS == "TESTKUDOS:0" + LOSS=$(jq -r .irregular_loss < test-audit-coins.json) + if [ "$LOSS" == "TESTKUDOS:0" ] then exit_fail "Wrong total bad sig loss, got $LOSS" fi # Undo - echo "UPDATE exchange.known_coins SET denom_sig='$OLD_SIG' WHERE coin_pub='$COIN_PUB'" | psql -Aqt $DB + echo "UPDATE exchange.known_coins SET denom_sig='$OLD_SIG' WHERE coin_pub='$COIN_PUB'" | psql -Aqt "$DB" } @@ -792,49 +976,51 @@ function test_6() { function test_7() { echo "===========7: reserves_out signature wrong=================" # Modify reserve_sig, so it is bogus - HBE=`echo 'SELECT h_blind_ev FROM exchange.reserves_out LIMIT 1;' | psql $DB -Aqt` - OLD_SIG=`echo "SELECT reserve_sig FROM exchange.reserves_out WHERE h_blind_ev='$HBE';" | psql $DB -Aqt` - A_VAL=`echo "SELECT amount_with_fee_val FROM exchange.reserves_out WHERE h_blind_ev='$HBE';" | psql $DB -Aqt` - A_FRAC=`echo "SELECT amount_with_fee_frac FROM exchange.reserves_out WHERE h_blind_ev='$HBE';" | psql $DB -Aqt` + HBE=$(echo 'SELECT h_blind_ev FROM exchange.reserves_out LIMIT 1;' | psql "$DB" -Aqt) + OLD_SIG=$(echo "SELECT reserve_sig FROM exchange.reserves_out WHERE h_blind_ev='$HBE';" | psql "$DB" -Aqt) + A_VAL=$(echo "SELECT amount_with_fee_val FROM exchange.reserves_out WHERE h_blind_ev='$HBE';" | psql "$DB" -Aqt) + A_FRAC=$(echo "SELECT amount_with_fee_frac FROM exchange.reserves_out WHERE h_blind_ev='$HBE';" | psql "$DB" -Aqt) # Normalize, we only deal with cents in this test-case - A_FRAC=`expr $A_FRAC / 1000000 || true` - echo "UPDATE exchange.reserves_out SET reserve_sig='\x9ef381a84aff252646a157d88eded50f708b2c52b7120d5a232a5b628f9ced6d497e6652d986b581188fb014ca857fd5e765a8ccc4eb7e2ce9edcde39accaa4b' WHERE h_blind_ev='$HBE'" | psql -Aqt $DB + A_FRAC=$(( A_FRAC / 1000000)) +# shellcheck disable=SC2028 + echo "UPDATE exchange.reserves_out SET reserve_sig='\x9ef381a84aff252646a157d88eded50f708b2c52b7120d5a232a5b628f9ced6d497e6652d986b581188fb014ca857fd5e765a8ccc4eb7e2ce9edcde39accaa4b' WHERE h_blind_ev='$HBE'" \ + | psql -Aqt "$DB" run_audit - OP=`jq -r .bad_sig_losses[0].operation < test-audit-reserves.json` - if test $OP != "withdraw" + OP=$(jq -r .bad_sig_losses[0].operation < test-audit-reserves.json) + if [ "$OP" != "withdraw" ] then exit_fail "Wrong operation, got $OP" fi - LOSS=`jq -r .bad_sig_losses[0].loss < test-audit-reserves.json` - LOSS_TOTAL=`jq -r .total_bad_sig_loss < test-audit-reserves.json` - if test $LOSS != $LOSS_TOTAL + LOSS=$(jq -r .bad_sig_losses[0].loss < test-audit-reserves.json) + LOSS_TOTAL=$(jq -r .total_bad_sig_loss < test-audit-reserves.json) + if [ "$LOSS" != "$LOSS_TOTAL" ] then exit_fail "Expected loss $LOSS and total loss $LOSS_TOTAL do not match" fi - if test $A_FRAC != 0 + if [ "$A_FRAC" != 0 ] then - if [ $A_FRAC -lt 10 ] + if [ "$A_FRAC" -lt 10 ] then A_PREV="0" else A_PREV="" fi - if test $LOSS != "TESTKUDOS:$A_VAL.$A_PREV$A_FRAC" + if [ "$LOSS" != "TESTKUDOS:$A_VAL.$A_PREV$A_FRAC" ] then exit_fail "Expected loss TESTKUDOS:$A_VAL.$A_PREV$A_FRAC but got $LOSS" fi else - if test $LOSS != "TESTKUDOS:$A_VAL" + if [ "$LOSS" != "TESTKUDOS:$A_VAL" ] then exit_fail "Expected loss TESTKUDOS:$A_VAL but got $LOSS" fi fi # Undo: - echo "UPDATE exchange.reserves_out SET reserve_sig='$OLD_SIG' WHERE h_blind_ev='$HBE'" | psql -Aqt $DB + echo "UPDATE exchange.reserves_out SET reserve_sig='$OLD_SIG' WHERE h_blind_ev='$HBE'" | psql -Aqt "$DB" } @@ -845,71 +1031,76 @@ function test_8() { echo "===========8: wire-transfer-subject disagreement===========" # Technically, this call shouldn't be needed, as libeufin should already be stopped here. stop_libeufin - cd $MYDIR - OLD_ID=`echo "SELECT id FROM NexusBankTransactions WHERE amount='10' AND currency='TESTKUDOS' ORDER BY id LIMIT 1;" | sqlite3 ${DB}-nexus.sqlite3` || exit_fail "Failed to SELECT FROM NexusBankTransactions nexus DB!" - OLD_WTID=`echo "SELECT reservePublicKey FROM TalerIncomingPayments WHERE payment='$OLD_ID';" | sqlite3 ${DB}-nexus.sqlite3` + cd "$MYDIR" + OLD_ID=$(echo "SELECT id FROM NexusBankTransactions WHERE amount='10' AND currency='TESTKUDOS' ORDER BY id LIMIT 1;" | psql "${DB}") \ + || exit_fail "Failed to SELECT FROM NexusBankTransactions nexus DB!" + OLD_WTID=$(echo "SELECT reservePublicKey FROM TalerIncomingPayments WHERE payment='$OLD_ID';" \ + | pqsl "${DB}") NEW_WTID="CK9QBFY972KR32FVA1MW958JWACEB6XCMHHKVFMCH1A780Q12SVG" - echo "UPDATE TalerIncomingPayments SET reservePublicKey='$NEW_WTID' WHERE payment='$OLD_ID';" | sqlite3 ${DB}-nexus.sqlite3 || exit_fail "Failed to update TalerIncomingPayments" - cd $ORIGIN + echo "UPDATE TalerIncomingPayments SET reservePublicKey='$NEW_WTID' WHERE payment='$OLD_ID';" \ + | psql "${DB}" \ + || exit_fail "Failed to update TalerIncomingPayments" + cd "$ORIGIN" run_audit echo -n "Testing inconsistency detection... " - DIAG=`jq -r .reserve_in_amount_inconsistencies[0].diagnostic < test-audit-wire.json` - if test "x$DIAG" != "xwire subject does not match" + DIAG=$(jq -r .reserve_in_amount_inconsistencies[0].diagnostic < test-audit-wire.json) + if [ "x$DIAG" != "xwire subject does not match" ] then exit_fail "Diagnostic wrong: $DIAG (0)" fi - WTID=`jq -r .reserve_in_amount_inconsistencies[0].reserve_pub < test-audit-wire.json` - if test x$WTID != x"$OLD_WTID" -a x$WTID != x"$NEW_WTID" + WTID=$(jq -r .reserve_in_amount_inconsistencies[0].reserve_pub < test-audit-wire.json) + if [ "x$WTID" != x"$OLD_WTID" ] && [ "x$WTID" != "x$NEW_WTID" ] then exit_fail "WTID reported wrong: $WTID" fi - EX_A=`jq -r .reserve_in_amount_inconsistencies[0].amount_exchange_expected < test-audit-wire.json` - if test x$WTID = x$OLD_WTID -a x$EX_A != x"TESTKUDOS:10" + EX_A=$(jq -r .reserve_in_amount_inconsistencies[0].amount_exchange_expected < test-audit-wire.json) + if [ "$WTID" = "$OLD_WTID" ] && [ "$EX_A" != "TESTKUDOS:10" ] then exit_fail "Amount reported wrong: $EX_A" fi - if test x$WTID = x$NEW_WTID -a x$EX_A != x"TESTKUDOS:0" + if [ "$WTID" = "$NEW_WTID" ] && [ "$EX_A" != "TESTKUDOS:0" ] then exit_fail "Amount reported wrong: $EX_A" fi - DIAG=`jq -r .reserve_in_amount_inconsistencies[1].diagnostic < test-audit-wire.json` - if test "x$DIAG" != "xwire subject does not match" + DIAG=$(jq -r .reserve_in_amount_inconsistencies[1].diagnostic < test-audit-wire.json) + if [ "$DIAG" != "wire subject does not match" ] then exit_fail "Diagnostic wrong: $DIAG (1)" fi - WTID=`jq -r .reserve_in_amount_inconsistencies[1].reserve_pub < test-audit-wire.json` - if test $WTID != "$OLD_WTID" -a $WTID != "$NEW_WTID" + WTID=$(jq -r .reserve_in_amount_inconsistencies[1].reserve_pub < test-audit-wire.json) + if [ "$WTID" != "$OLD_WTID" ] && [ "$WTID" != "$NEW_WTID" ] then exit_fail "WTID reported wrong: $WTID (wanted: $NEW_WTID or $OLD_WTID)" fi - EX_A=`jq -r .reserve_in_amount_inconsistencies[1].amount_exchange_expected < test-audit-wire.json` - if test $WTID = "$OLD_WTID" -a $EX_A != "TESTKUDOS:10" + EX_A=$(jq -r .reserve_in_amount_inconsistencies[1].amount_exchange_expected < test-audit-wire.json) + if [ "$WTID" = "$OLD_WTID" ] && [ "$EX_A" != "TESTKUDOS:10" ] then exit_fail "Amount reported wrong: $EX_A" fi - if test $WTID = "$NEW_WTID" -a $EX_A != "TESTKUDOS:0" + if [ "$WTID" = "$NEW_WTID" ] && [ "$EX_A" != "TESTKUDOS:0" ] then exit_fail "Amount reported wrong: $EX_A" fi - WIRED=`jq -r .total_wire_in_delta_minus < test-audit-wire.json` - if test $WIRED != "TESTKUDOS:10" + WIRED=$(jq -r .total_wire_in_delta_minus < test-audit-wire.json) + if [ "$WIRED" != "TESTKUDOS:10" ] then exit_fail "Wrong total wire_in_delta_minus, got $WIRED" fi - DELTA=`jq -r .total_wire_in_delta_plus < test-audit-wire.json` - if test $DELTA != "TESTKUDOS:10" + DELTA=$(jq -r .total_wire_in_delta_plus < test-audit-wire.json) + if [ "$DELTA" != "TESTKUDOS:10" ] then exit_fail "Expected total wire delta plus wrong, got $DELTA" fi - echo PASS + echo "PASS" # Undo database modification - cd $MYDIR - echo "UPDATE TalerIncomingPayments SET reservePublicKey='$OLD_WTID' WHERE payment='$OLD_ID';" | sqlite3 ${DB}-nexus.sqlite3 - cd $ORIGIN + cd "$MYDIR" + echo "UPDATE TalerIncomingPayments SET reservePublicKey='$OLD_WTID' WHERE payment='$OLD_ID';" \ + | psql "${DB}" + cd "$ORIGIN" } @@ -920,19 +1111,20 @@ function test_9() { echo "===========9: wire-origin disagreement===========" # Technically, this call shouldn't be needed, as libeufin should already be stopped here. stop_libeufin - OLD_ID=`echo "SELECT id FROM NexusBankTransactions WHERE amount='10' AND currency='TESTKUDOS' ORDER BY id LIMIT 1;" | sqlite3 ${MYDIR}/${DB}-nexus.sqlite3` - OLD_ACC=`echo "SELECT incomingPaytoUri FROM TalerIncomingPayments WHERE payment='$OLD_ID';" | sqlite3 ${MYDIR}/${DB}-nexus.sqlite3` - echo "UPDATE TalerIncomingPayments SET incomingPaytoUri='payto://iban/SANDBOXX/DE144373?receiver-name=New+Exchange+Company' WHERE payment='$OLD_ID';" | sqlite3 ${MYDIR}/${DB}-nexus.sqlite3 + OLD_ID=$(echo "SELECT id FROM NexusBankTransactions WHERE amount='10' AND currency='TESTKUDOS' ORDER BY id LIMIT 1;" | psql "${DB}") + OLD_ACC=$(echo "SELECT incomingPaytoUri FROM TalerIncomingPayments WHERE payment='$OLD_ID';" | psql "${DB}") + echo "UPDATE TalerIncomingPayments SET incomingPaytoUri='payto://iban/SANDBOXX/DE144373?receiver-name=New+Exchange+Company' WHERE payment='$OLD_ID';" \ + | psql "${DB}" run_audit echo -n "Testing inconsistency detection... " - AMOUNT=`jq -r .misattribution_in_inconsistencies[0].amount < test-audit-wire.json` + AMOUNT=$(jq -r .misattribution_in_inconsistencies[0].amount < test-audit-wire.json) if test "x$AMOUNT" != "xTESTKUDOS:10" then exit_fail "Reported amount wrong: $AMOUNT" fi - AMOUNT=`jq -r .total_misattribution_in < test-audit-wire.json` + AMOUNT=$(jq -r .total_misattribution_in < test-audit-wire.json) if test "x$AMOUNT" != "xTESTKUDOS:10" then exit_fail "Reported total amount wrong: $AMOUNT" @@ -940,30 +1132,31 @@ function test_9() { echo PASS # Undo database modification - echo "UPDATE TalerIncomingPayments SET incomingPaytoUri='$OLD_ACC' WHERE payment='$OLD_ID';" | sqlite3 ${MYDIR}/${DB}-nexus.sqlite3 + echo "UPDATE TalerIncomingPayments SET incomingPaytoUri='$OLD_ACC' WHERE payment='$OLD_ID';" \ + | psql "${DB}" } # Test wire_in timestamp disagreement! function test_10() { - NOW_MS=`date +%s`000 + NOW_MS=$(date +%s)000 echo "===========10: wire-timestamp disagreement===========" # Technically, this call shouldn't be needed, as libeufin should already be stopped here. stop_libeufin - OLD_ID=`echo "SELECT id FROM NexusBankTransactions WHERE amount='10' AND currency='TESTKUDOS' ORDER BY id LIMIT 1;" | sqlite3 ${MYDIR}/${DB}-nexus.sqlite3` - OLD_DATE=`echo "SELECT timestampMs FROM TalerIncomingPayments WHERE payment='$OLD_ID';" | sqlite3 ${MYDIR}/${DB}-nexus.sqlite3` - echo "UPDATE TalerIncomingPayments SET timestampMs=$NOW_MS WHERE payment=$OLD_ID;" | sqlite3 ${MYDIR}/${DB}-nexus.sqlite3 + OLD_ID=$(echo "SELECT id FROM NexusBankTransactions WHERE amount='10' AND currency='TESTKUDOS' ORDER BY id LIMIT 1;" | psql "${DB}") + OLD_DATE=$(echo "SELECT timestampMs FROM TalerIncomingPayments WHERE payment='$OLD_ID';" | psql "${DB}") + echo "UPDATE TalerIncomingPayments SET timestampMs=$NOW_MS WHERE payment=$OLD_ID;" | psql "${DB}" run_audit echo -n "Testing inconsistency detection... " - DIAG=`jq -r .row_minor_inconsistencies[0].diagnostic < test-audit-wire.json` + DIAG=$(jq -r .row_minor_inconsistencies[0].diagnostic < test-audit-wire.json) if test "x$DIAG" != "xexecution date mismatch" then exit_fail "Reported diagnostic wrong: $DIAG" fi - TABLE=`jq -r .row_minor_inconsistencies[0].table < test-audit-wire.json` + TABLE=$(jq -r .row_minor_inconsistencies[0].table < test-audit-wire.json) if test "x$TABLE" != "xreserves_in" then exit_fail "Reported table wrong: $TABLE" @@ -971,7 +1164,7 @@ function test_10() { echo PASS # Undo database modification - echo "UPDATE TalerIncomingPayments SET timestampMs='$OLD_DATE' WHERE payment=$OLD_ID;" | sqlite3 ${MYDIR}/${DB}-nexus.sqlite3 + echo "UPDATE TalerIncomingPayments SET timestampMs='$OLD_DATE' WHERE payment=$OLD_ID;" | psql "${DB}" } @@ -983,91 +1176,94 @@ function test_11() { echo "===========11: spurious outgoing transfer ===========" # Technically, this call shouldn't be needed, as libeufin should already be stopped here. stop_libeufin - OLD_ID=`echo "SELECT id FROM NexusBankTransactions WHERE amount='10' AND currency='TESTKUDOS' ORDER BY id LIMIT 1;" | sqlite3 ${MYDIR}/${DB}-nexus.sqlite3` - OLD_TX=`echo "SELECT transactionJson FROM NexusBankTransactions WHERE id='$OLD_ID';" | sqlite3 ${MYDIR}/${DB}-nexus.sqlite3` + OLD_ID=$(echo "SELECT id FROM NexusBankTransactions WHERE amount='10' AND currency='TESTKUDOS' ORDER BY id LIMIT 1;" | psql "${DB}") + OLD_TX=$(echo "SELECT transactionJson FROM NexusBankTransactions WHERE id='$OLD_ID';" | psql "${DB}") # Change wire transfer to be FROM the exchange (#2) to elsewhere! # (Note: this change also causes a missing incoming wire transfer, but # this test is only concerned about the outgoing wire transfer # being detected as such, and we simply ignore the other # errors being reported.) - OTHER_IBAN=`echo -e "SELECT iban FROM BankAccounts WHERE label='fortytwo'" | sqlite3 ${MYDIR}/${DB}-sandbox.sqlite3` - NEW_TX=$(echo "$OLD_TX" | jq .batches[0].batchTransactions[0].details.creditDebitIndicator='"DBIT"' | jq 'del(.batches[0].batchTransactions[0].details.debtor)' | jq 'del(.batches[0].batchTransactions[0].details.debtorAccount)' | jq 'del(.batches[0].batchTransactions[0].details.debtorAgent)' | jq '.batches[0].batchTransactions[0].details.creditor'='{"name": "Forty Two"}' | jq .batches[0].batchTransactions[0].details.creditorAccount='{"iban": "'$OTHER_IBAN'"}' | jq .batches[0].batchTransactions[0].details.creditorAgent='{"bic": "SANDBOXX"}' | jq .batches[0].batchTransactions[0].details.unstructuredRemittanceInformation='"CK9QBFY972KR32FVA1MW958JWACEB6XCMHHKVFMCH1A780Q12SVG http://exchange.example.com/"') - echo -e "UPDATE NexusBankTransactions SET transactionJson='"$NEW_TX"' WHERE id=$OLD_ID" | sqlite3 ${MYDIR}/${DB}-nexus.sqlite3 + OTHER_IBAN=$(echo -e "SELECT iban FROM BankAccounts WHERE label='fortytwo'" | psql "${DB}") + NEW_TX=$(echo "$OLD_TX" | jq .batches[0].batchTransactions[0].details.creditDebitIndicator='"DBIT"' | jq 'del(.batches[0].batchTransactions[0].details.debtor)' | jq 'del(.batches[0].batchTransactions[0].details.debtorAccount)' | jq 'del(.batches[0].batchTransactions[0].details.debtorAgent)' | jq '.batches[0].batchTransactions[0].details.creditor'='{"name": "Forty Two"}' | jq .batches[0].batchTransactions[0].details.creditorAccount='{"iban": "'"$OTHER_IBAN"'"}' | jq .batches[0].batchTransactions[0].details.creditorAgent='{"bic": "SANDBOXX"}' | jq .batches[0].batchTransactions[0].details.unstructuredRemittanceInformation='"CK9QBFY972KR32FVA1MW958JWACEB6XCMHHKVFMCH1A780Q12SVG http://exchange.example.com/"') + echo -e "UPDATE NexusBankTransactions SET transactionJson='""$NEW_TX""' WHERE id=$OLD_ID" \ + | psql "${DB}" # Now fake that the exchange prepared this payment (= it POSTed to /transfer) # This step is necessary, because the TWG table that accounts for outgoing # payments needs it. Worth noting here is the column 'rawConfirmation' that # points to the transaction from the main Nexus ledger; without that column set, # a prepared payment won't appear as actually outgoing. - echo -e "INSERT INTO PaymentInitiations (bankAccount,preparationDate,submissionDate,sum,currency,endToEndId,paymentInformationId,instructionId,subject,creditorIban,creditorBic,creditorName,submitted,messageId,rawConfirmation) VALUES (1,1,1,10,'TESTKUDOS','NOTGIVEN','unused','unused','CK9QBFY972KR32FVA1MW958JWACEB6XCMHHKVFMCH1A780Q12SVG http://exchange.example.com/','"$OTHER_IBAN"','SANDBOXX','Forty Two','unused',1,$OLD_ID)" | sqlite3 ${MYDIR}/${DB}-nexus.sqlite3 + echo -e "INSERT INTO PaymentInitiations (bankAccount,preparationDate,submissionDate,sum,currency,endToEndId,paymentInformationId,instructionId,subject,creditorIban,creditorBic,creditorName,submitted,messageId,rawConfirmation) VALUES (1,1,1,10,'TESTKUDOS','NOTGIVEN','unused','unused','CK9QBFY972KR32FVA1MW958JWACEB6XCMHHKVFMCH1A780Q12SVG http://exchange.example.com/','""$OTHER_IBAN""','SANDBOXX','Forty Two','unused',1,$OLD_ID)" \ + | psql "${DB}" # Now populate the TWG table that accounts for outgoing payments, in # order to let /history/outgoing return one result. - echo -e "INSERT INTO TalerRequestedPayments (facade,payment,requestUid,amount,exchangeBaseUrl,wtid,creditAccount) VALUES (1,1,'unused','TESTKUDOS:10','http://exchange.example.com/','CK9QBFY972KR32FVA1MW958JWACEB6XCMHHKVFMCH1A780Q12SVG','payto://iban/SANDBOXX/"$OTHER_IBAN"?receiver-name=Forty+Two')" | sqlite3 ${MYDIR}/${DB}-nexus.sqlite3 + echo -e "INSERT INTO TalerRequestedPayments (facade,payment,requestUid,amount,exchangeBaseUrl,wtid,creditAccount) VALUES (1,1,'unused','TESTKUDOS:10','http://exchange.example.com/','CK9QBFY972KR32FVA1MW958JWACEB6XCMHHKVFMCH1A780Q12SVG','payto://iban/SANDBOXX/""$OTHER_IBAN""?receiver-name=Forty+Two')" \ + | psql "${DB}" run_audit echo -n "Testing inconsistency detection... " - AMOUNT=`jq -r .wire_out_amount_inconsistencies[0].amount_wired < test-audit-wire.json` - if test "x$AMOUNT" != "xTESTKUDOS:10" + AMOUNT=$(jq -r .wire_out_amount_inconsistencies[0].amount_wired < test-audit-wire.json) + if [ "x$AMOUNT" != "xTESTKUDOS:10" ] then exit_fail "Reported wired amount wrong: $AMOUNT" fi - AMOUNT=`jq -r .total_wire_out_delta_plus < test-audit-wire.json` - if test "x$AMOUNT" != "xTESTKUDOS:10" + AMOUNT=$(jq -r .total_wire_out_delta_plus < test-audit-wire.json) + if [ "x$AMOUNT" != "xTESTKUDOS:10" ] then exit_fail "Reported total plus amount wrong: $AMOUNT" fi - AMOUNT=`jq -r .total_wire_out_delta_minus < test-audit-wire.json` - if test "x$AMOUNT" != "xTESTKUDOS:0" + AMOUNT=$(jq -r .total_wire_out_delta_minus < test-audit-wire.json) + if [ "x$AMOUNT" != "xTESTKUDOS:0" ] then exit_fail "Reported total minus amount wrong: $AMOUNT" fi - AMOUNT=`jq -r .wire_out_amount_inconsistencies[0].amount_justified < test-audit-wire.json` - if test "x$AMOUNT" != "xTESTKUDOS:0" + AMOUNT=$(jq -r .wire_out_amount_inconsistencies[0].amount_justified < test-audit-wire.json) + if [ "x$AMOUNT" != "xTESTKUDOS:0" ] then exit_fail "Reported justified amount wrong: $AMOUNT" fi - DIAG=`jq -r .wire_out_amount_inconsistencies[0].diagnostic < test-audit-wire.json` - if test "x$DIAG" != "xjustification for wire transfer not found" + DIAG=$(jq -r .wire_out_amount_inconsistencies[0].diagnostic < test-audit-wire.json) + if [ "x$DIAG" != "xjustification for wire transfer not found" ] then exit_fail "Reported diagnostic wrong: $DIAG" fi - echo PASS + echo "PASS" # Undo database modification - echo -e "UPDATE NexusBankTransactions SET transactionJson='"$OLD_TX"' WHERE id=$OLD_ID;" | sqlite3 ${MYDIR}/${DB}-nexus.sqlite3 + echo -e "UPDATE NexusBankTransactions SET transactionJson='$OLD_TX' WHERE id=$OLD_ID;" \ + | psql "${DB}" # No other prepared payment should exist at this point, # so OK to remove the number 1. - echo -e "DELETE FROM PaymentInitiations WHERE id=1" | sqlite3 ${MYDIR}/${DB}-nexus.sqlite3 - echo -e "DELETE FROM TalerRequestedPayments WHERE id=1" | sqlite3 ${MYDIR}/${DB}-nexus.sqlite3 + echo -e "DELETE FROM PaymentInitiations WHERE id=1" \ + | psql "${DB}" + echo -e "DELETE FROM TalerRequestedPayments WHERE id=1" \ + | psql "${DB}" } # Test for hanging/pending refresh. function test_12() { echo "===========12: incomplete refresh ===========" - OLD_ACC=`echo "DELETE FROM exchange.refresh_revealed_coins;" | psql $DB -Aqt` + OLD_ACC=$(echo "DELETE FROM exchange.refresh_revealed_coins;" | psql "$DB" -Aqt) run_audit echo -n "Testing hung refresh detection... " - HANG=`jq -er .refresh_hanging[0].amount < test-audit-coins.json` - TOTAL_HANG=`jq -er .total_refresh_hanging < test-audit-coins.json` - if test x$HANG = TESTKUDOS:0 + HANG=$(jq -er .refresh_hanging[0].amount < test-audit-coins.json) + TOTAL_HANG=$(jq -er .total_refresh_hanging < test-audit-coins.json) + if [ "$HANG" = "TESTKUDOS:0" ] then exit_fail "Hanging amount zero" fi - if test x$TOTAL_HANG = TESTKUDOS:0 + if [ "$TOTAL_HANG" = "TESTKUDOS:0" ] then exit_fail "Total hanging amount zero" fi - - echo PASS - + echo "PASS" # cannot easily undo DELETE, hence full reload full_reload - } @@ -1076,23 +1272,23 @@ function test_13() { echo "===========13: wrong melt signature ===========" # Modify denom_sig, so it is wrong - COIN_PUB=`echo "SELECT old_coin_pub FROM exchange.refresh_commitments LIMIT 1;" | psql $DB -Aqt` - OLD_SIG=`echo "SELECT old_coin_sig FROM exchange.refresh_commitments WHERE old_coin_pub='$COIN_PUB';" | psql $DB -Aqt` + COIN_PUB=$(echo "SELECT old_coin_pub FROM exchange.refresh_commitments LIMIT 1;" | psql "$DB" -Aqt) + OLD_SIG=$(echo "SELECT old_coin_sig FROM exchange.refresh_commitments WHERE old_coin_pub='$COIN_PUB';" | psql "$DB" -Aqt) NEW_SIG="\xba588af7c13c477dca1ac458f65cc484db8fba53b969b873f4353ecbd815e6b4c03f42c0cb63a2b609c2d726e612fd8e0c084906a41f409b6a23a08a83c89a02" - echo "UPDATE exchange.refresh_commitments SET old_coin_sig='$NEW_SIG' WHERE old_coin_pub='$COIN_PUB'" | psql -Aqt $DB + echo "UPDATE exchange.refresh_commitments SET old_coin_sig='$NEW_SIG' WHERE old_coin_pub='$COIN_PUB'" | psql -Aqt "$DB" run_audit echo -n "Testing inconsistency detection... " - OP=`jq -er .bad_sig_losses[0].operation < test-audit-coins.json` + OP=$(jq -er .bad_sig_losses[0].operation < test-audit-coins.json) if test x$OP != xmelt then exit_fail "Operation wrong, got $OP" fi - LOSS=`jq -er .bad_sig_losses[0].loss < test-audit-coins.json` - TOTAL_LOSS=`jq -er .irregular_loss < test-audit-coins.json` + LOSS=$(jq -er .bad_sig_losses[0].loss < test-audit-coins.json) + TOTAL_LOSS=$(jq -er .irregular_loss < test-audit-coins.json) if test x$LOSS != x$TOTAL_LOSS then exit_fail "Loss inconsistent, got $LOSS and $TOTAL_LOSS" @@ -1118,17 +1314,17 @@ function test_14() { # actual outgoing wire transfers, so we need to run the # aggregator here. pre_audit aggregator - echo "UPDATE exchange.wire_fee SET wire_fee_frac=100;" | psql -Aqt $DB + echo "UPDATE exchange.wire_fee SET wire_fee_frac=100;" | psql -Aqt "$DB" audit_only post_audit echo -n "Testing inconsistency detection... " - TABLE=`jq -r .row_inconsistencies[0].table < test-audit-aggregation.json` + TABLE=$(jq -r .row_inconsistencies[0].table < test-audit-aggregation.json) if test "x$TABLE" != "xwire-fee" then exit_fail "Reported table wrong: $TABLE" fi - DIAG=`jq -r .row_inconsistencies[0].diagnostic < test-audit-aggregation.json` + DIAG=$(jq -r .row_inconsistencies[0].diagnostic < test-audit-aggregation.json) if test "x$DIAG" != "xwire fee signature invalid at given time" then exit_fail "Reported diagnostic wrong: $DIAG" @@ -1145,13 +1341,15 @@ function test_15() { echo "===========15: deposit wire salt wrong=================" # Modify wire_salt hash, so it is inconsistent - SALT=`echo "SELECT wire_salt FROM exchange.deposits WHERE deposit_serial_id=1;" | psql -Aqt $DB` - echo "UPDATE exchange.deposits SET wire_salt='\x1197cd7f7b0e13ab1905fedb36c536a2' WHERE deposit_serial_id=1;" | psql -Aqt $DB + SALT=$(echo "SELECT wire_salt FROM exchange.deposits WHERE deposit_serial_id=1;" | psql -Aqt "$DB") +# shellcheck disable=SC2028 + echo "UPDATE exchange.deposits SET wire_salt='\x1197cd7f7b0e13ab1905fedb36c536a2' WHERE deposit_serial_id=1;" \ + | psql -Aqt "$DB" run_audit echo -n "Testing inconsistency detection... " - OP=`jq -r .bad_sig_losses[0].operation < test-audit-coins.json` + OP=$(jq -r .bad_sig_losses[0].operation < test-audit-coins.json) if test "x$OP" != "xdeposit" then exit_fail "Reported operation wrong: $OP" @@ -1159,7 +1357,7 @@ function test_15() { echo PASS # Restore DB - echo "UPDATE exchange.deposits SET wire_salt='$SALT' WHERE deposit_serial_id=1;" | psql -Aqt $DB + echo "UPDATE exchange.deposits SET wire_salt='$SALT' WHERE deposit_serial_id=1;" | psql -Aqt "$DB" } @@ -1175,30 +1373,30 @@ function test_16() { pre_audit aggregator stop_libeufin - OLD_AMOUNT=`echo "SELECT amount FROM TalerRequestedPayments WHERE id='1';" | sqlite3 ${MYDIR}/${DB}-nexus.sqlite3` + OLD_AMOUNT=$(echo "SELECT amount FROM TalerRequestedPayments WHERE id='1';" | psql "${DB}") NEW_AMOUNT="TESTKUDOS:50" - echo "UPDATE TalerRequestedPayments SET amount='${NEW_AMOUNT}' WHERE id='1';" | sqlite3 ${MYDIR}/${DB}-nexus.sqlite3 + echo "UPDATE TalerRequestedPayments SET amount='${NEW_AMOUNT}' WHERE id='1';" | psql "${DB}" launch_libeufin audit_only echo -n "Testing inconsistency detection... " - AMOUNT=`jq -r .wire_out_amount_inconsistencies[0].amount_justified < test-audit-wire.json` + AMOUNT=$(jq -r .wire_out_amount_inconsistencies[0].amount_justified < test-audit-wire.json) if test "x$AMOUNT" != "x$OLD_AMOUNT" then exit_fail "Reported justified amount wrong: $AMOUNT" fi - AMOUNT=`jq -r .wire_out_amount_inconsistencies[0].amount_wired < test-audit-wire.json` + AMOUNT=$(jq -r .wire_out_amount_inconsistencies[0].amount_wired < test-audit-wire.json) if test "x$AMOUNT" != "x$NEW_AMOUNT" then exit_fail "Reported wired amount wrong: $AMOUNT" fi - TOTAL_AMOUNT=`jq -r .total_wire_out_delta_minus < test-audit-wire.json` + TOTAL_AMOUNT=$(jq -r .total_wire_out_delta_minus < test-audit-wire.json) if test "x$TOTAL_AMOUNT" != "xTESTKUDOS:0" then exit_fail "Reported total wired amount minus wrong: $TOTAL_AMOUNT" fi - TOTAL_AMOUNT=`jq -r .total_wire_out_delta_plus < test-audit-wire.json` + TOTAL_AMOUNT=$(jq -r .total_wire_out_delta_plus < test-audit-wire.json) if test "x$TOTAL_AMOUNT" = "xTESTKUDOS:0" then exit_fail "Reported total wired amount plus wrong: $TOTAL_AMOUNT" @@ -1208,28 +1406,28 @@ function test_16() { stop_libeufin echo "Second modification: wire nothing" NEW_AMOUNT="TESTKUDOS:0" - echo "UPDATE TalerRequestedPayments SET amount='${NEW_AMOUNT}' WHERE id='1';" | sqlite3 ${MYDIR}/${DB}-nexus.sqlite3 + echo "UPDATE TalerRequestedPayments SET amount='${NEW_AMOUNT}' WHERE id='1';" | psql "${DB}" launch_libeufin audit_only stop_libeufin echo -n "Testing inconsistency detection... " - AMOUNT=`jq -r .wire_out_amount_inconsistencies[0].amount_justified < test-audit-wire.json` + AMOUNT=$(jq -r .wire_out_amount_inconsistencies[0].amount_justified < test-audit-wire.json) if test "x$AMOUNT" != "x$OLD_AMOUNT" then exit_fail "Reported justified amount wrong: $AMOUNT" fi - AMOUNT=`jq -r .wire_out_amount_inconsistencies[0].amount_wired < test-audit-wire.json` + AMOUNT=$(jq -r .wire_out_amount_inconsistencies[0].amount_wired < test-audit-wire.json) if test "x$AMOUNT" != "x$NEW_AMOUNT" then exit_fail "Reported wired amount wrong: $AMOUNT" fi - TOTAL_AMOUNT=`jq -r .total_wire_out_delta_minus < test-audit-wire.json` + TOTAL_AMOUNT=$(jq -r .total_wire_out_delta_minus < test-audit-wire.json) if test "x$TOTAL_AMOUNT" != "x$OLD_AMOUNT" then exit_fail "Reported total wired amount minus wrong: $TOTAL_AMOUNT (wanted $OLD_AMOUNT)" fi - TOTAL_AMOUNT=`jq -r .total_wire_out_delta_plus < test-audit-wire.json` + TOTAL_AMOUNT=$(jq -r .total_wire_out_delta_plus < test-audit-wire.json) if test "x$TOTAL_AMOUNT" != "xTESTKUDOS:0" then exit_fail "Reported total wired amount plus wrong: $TOTAL_AMOUNT" @@ -1253,25 +1451,26 @@ function test_17() { pre_audit aggregator stop_libeufin OLD_ID=1 - OLD_PREP=`echo "SELECT payment FROM TalerRequestedPayments WHERE id='${OLD_ID}';" | sqlite3 ${MYDIR}/${DB}-nexus.sqlite3` - OLD_DATE=`echo "SELECT preparationDate FROM PaymentInitiations WHERE id='${OLD_ID}';" | sqlite3 ${MYDIR}/${DB}-nexus.sqlite3` + OLD_PREP=$(echo "SELECT payment FROM TalerRequestedPayments WHERE id='${OLD_ID}';" | psql "${DB}") + OLD_DATE=$(echo "SELECT preparationDate FROM PaymentInitiations WHERE id='${OLD_ID}';" | psql "${DB}") # Note: need - interval '1h' as "NOW()" may otherwise be exactly what is already in the DB # (due to rounding, if this machine is fast...) NOW_1HR=$(expr $(date +%s) - 3600) - echo "UPDATE PaymentInitiations SET preparationDate='$NOW_1HR' WHERE id='${OLD_PREP}';" | sqlite3 ${MYDIR}/${DB}-nexus.sqlite3 + echo "UPDATE PaymentInitiations SET preparationDate='$NOW_1HR' WHERE id='${OLD_PREP}';" \ + | psql "${DB}" launch_libeufin echo DONE audit_only post_audit echo -n "Testing inconsistency detection... " - TABLE=`jq -r .row_minor_inconsistencies[0].table < test-audit-wire.json` + TABLE=$(jq -r .row_minor_inconsistencies[0].table < test-audit-wire.json) if test "x$TABLE" != "xwire_out" then exit_fail "Reported table wrong: $TABLE" fi - DIAG=`jq -r .row_minor_inconsistencies[0].diagnostic < test-audit-wire.json` - DIAG=`echo "$DIAG" | awk '{print $1 " " $2 " " $3}'` + DIAG=$(jq -r .row_minor_inconsistencies[0].diagnostic < test-audit-wire.json) + DIAG=$(echo "$DIAG" | awk '{print $1 " " $2 " " $3}') if test "x$DIAG" != "xexecution date mismatch" then exit_fail "Reported diagnostic wrong: $DIAG" @@ -1289,7 +1488,7 @@ function test_17() { function test_18() { echo "===========18: emergency=================" - echo "DELETE FROM exchange.reserves_out;" | psql -Aqt $DB + echo "DELETE FROM exchange.reserves_out;" | psql -Aqt "$DB" run_audit @@ -1305,12 +1504,12 @@ function test_18() { echo -n "Testing loss calculation... " - AMOUNT=`jq -r .emergencies_loss < test-audit-coins.json` + AMOUNT=$(jq -r .emergencies_loss < test-audit-coins.json) if test "x$AMOUNT" == "xTESTKUDOS:0" then exit_fail "Reported amount wrong: $AMOUNT" fi - AMOUNT=`jq -r .emergencies_loss_by_count < test-audit-coins.json` + AMOUNT=$(jq -r .emergencies_loss_by_count < test-audit-coins.json) if test "x$AMOUNT" == "xTESTKUDOS:0" then exit_fail "Reported amount wrong: $AMOUNT" @@ -1328,16 +1527,16 @@ function test_18() { function test_19() { echo "===========19: reserve closure done properly =================" - OLD_TIME=`echo "SELECT execution_date FROM exchange.reserves_in WHERE reserve_in_serial_id=1;" | psql $DB -Aqt` - OLD_VAL=`echo "SELECT credit_val FROM exchange.reserves_in WHERE reserve_in_serial_id=1;" | psql $DB -Aqt` - RES_PUB=`echo "SELECT reserve_pub FROM exchange.reserves_in WHERE reserve_in_serial_id=1;" | psql $DB -Aqt` - OLD_EXP=`echo "SELECT expiration_date FROM exchange.reserves WHERE reserve_pub='${RES_PUB}';" | psql $DB -Aqt` + OLD_TIME=$(echo "SELECT execution_date FROM exchange.reserves_in WHERE reserve_in_serial_id=1;" | psql "$DB" -Aqt) + OLD_VAL=$(echo "SELECT credit_val FROM exchange.reserves_in WHERE reserve_in_serial_id=1;" | psql "$DB" -Aqt) + RES_PUB=$(echo "SELECT reserve_pub FROM exchange.reserves_in WHERE reserve_in_serial_id=1;" | psql "$DB" -Aqt) + OLD_EXP=$(echo "SELECT expiration_date FROM exchange.reserves WHERE reserve_pub='${RES_PUB}';" | psql "$DB" -Aqt) VAL_DELTA=1 - NEW_TIME=`expr $OLD_TIME - 3024000000000 || true` # 5 weeks - NEW_EXP=`expr $OLD_EXP - 3024000000000 || true` # 5 weeks - NEW_CREDIT=`expr $OLD_VAL + $VAL_DELTA || true` - echo "UPDATE exchange.reserves_in SET execution_date='${NEW_TIME}',credit_val=${NEW_CREDIT} WHERE reserve_in_serial_id=1;" | psql -Aqt $DB - echo "UPDATE exchange.reserves SET current_balance_val=${VAL_DELTA}+current_balance_val,expiration_date='${NEW_EXP}' WHERE reserve_pub='${RES_PUB}';" | psql -Aqt $DB + NEW_TIME=$(expr $OLD_TIME - 3024000000000 || true) # 5 weeks + NEW_EXP=$(expr $OLD_EXP - 3024000000000 || true) # 5 weeks + NEW_CREDIT=$(expr $OLD_VAL + $VAL_DELTA || true) + echo "UPDATE exchange.reserves_in SET execution_date='${NEW_TIME}',credit_val=${NEW_CREDIT} WHERE reserve_in_serial_id=1;" | psql -Aqt "$DB" + echo "UPDATE exchange.reserves SET current_balance_val=${VAL_DELTA}+current_balance_val,expiration_date='${NEW_EXP}' WHERE reserve_pub='${RES_PUB}';" | psql -Aqt "$DB" # Need to run with the aggregator so the reserve closure happens run_audit aggregator @@ -1362,13 +1561,13 @@ function test_19() { function test_20() { echo "===========20: reserve closure missing =================" - OLD_TIME=`echo "SELECT execution_date FROM exchange.reserves_in WHERE reserve_in_serial_id=1;" | psql $DB -Aqt` - OLD_VAL=`echo "SELECT credit_val FROM exchange.reserves_in WHERE reserve_in_serial_id=1;" | psql $DB -Aqt` - RES_PUB=`echo "SELECT reserve_pub FROM exchange.reserves_in WHERE reserve_in_serial_id=1;" | psql $DB -Aqt` - NEW_TIME=`expr $OLD_TIME - 3024000000000 || true` # 5 weeks - NEW_CREDIT=`expr $OLD_VAL + 100 || true` - echo "UPDATE exchange.reserves_in SET execution_date='${NEW_TIME}',credit_val=${NEW_CREDIT} WHERE reserve_in_serial_id=1;" | psql -Aqt $DB - echo "UPDATE exchange.reserves SET current_balance_val=100+current_balance_val WHERE reserve_pub='${RES_PUB}';" | psql -Aqt $DB + OLD_TIME=$(echo "SELECT execution_date FROM exchange.reserves_in WHERE reserve_in_serial_id=1;" | psql "$DB" -Aqt) + OLD_VAL=$(echo "SELECT credit_val FROM exchange.reserves_in WHERE reserve_in_serial_id=1;" | psql "$DB" -Aqt) + RES_PUB=$(echo "SELECT reserve_pub FROM exchange.reserves_in WHERE reserve_in_serial_id=1;" | psql "$DB" -Aqt) + NEW_TIME=$(expr $OLD_TIME - 3024000000000 || true) # 5 weeks + NEW_CREDIT=$(expr $OLD_VAL + 100 || true) + echo "UPDATE exchange.reserves_in SET execution_date='${NEW_TIME}',credit_val=${NEW_CREDIT} WHERE reserve_in_serial_id=1;" | psql -Aqt "$DB" + echo "UPDATE exchange.reserves SET current_balance_val=100+current_balance_val WHERE reserve_pub='${RES_PUB}';" | psql -Aqt "$DB" # This time, run without the aggregator so the reserve closure is skipped! run_audit @@ -1377,15 +1576,15 @@ function test_20() { jq -e .reserve_not_closed_inconsistencies[0] < test-audit-reserves.json > /dev/null || exit_fail "Reserve not closed inconsistency not detected" echo "PASS" - AMOUNT=`jq -r .total_balance_reserve_not_closed < test-audit-reserves.json` + AMOUNT=$(jq -r .total_balance_reserve_not_closed < test-audit-reserves.json) if test "x$AMOUNT" == "xTESTKUDOS:0" then exit_fail "Reported total amount wrong: $AMOUNT" fi # Undo - echo "UPDATE exchange.reserves_in SET execution_date='${OLD_TIME}',credit_val=${OLD_VAL} WHERE reserve_in_serial_id=1;" | psql -Aqt $DB - echo "UPDATE exchange.reserves SET current_balance_val=current_balance_val-100 WHERE reserve_pub='${RES_PUB}';" | psql -Aqt $DB + echo "UPDATE exchange.reserves_in SET execution_date='${OLD_TIME}',credit_val=${OLD_VAL} WHERE reserve_in_serial_id=1;" | psql -Aqt "$DB" + echo "UPDATE exchange.reserves SET current_balance_val=current_balance_val-100 WHERE reserve_pub='${RES_PUB}';" | psql -Aqt "$DB" } @@ -1394,23 +1593,23 @@ function test_20() { function test_21() { echo "===========21: reserve closure missreported =================" - OLD_TIME=`echo "SELECT execution_date FROM exchange.reserves_in WHERE reserve_in_serial_id=1;" | psql $DB -Aqt` - OLD_VAL=`echo "SELECT credit_val FROM exchange.reserves_in WHERE reserve_in_serial_id=1;" | psql $DB -Aqt` - RES_PUB=`echo "SELECT reserve_pub FROM exchange.reserves_in WHERE reserve_in_serial_id=1;" | psql $DB -Aqt` - OLD_EXP=`echo "SELECT expiration_date FROM exchange.reserves WHERE reserve_pub='${RES_PUB}';" | psql $DB -Aqt` + OLD_TIME=$(echo "SELECT execution_date FROM exchange.reserves_in WHERE reserve_in_serial_id=1;" | psql "$DB" -Aqt) + OLD_VAL=$(echo "SELECT credit_val FROM exchange.reserves_in WHERE reserve_in_serial_id=1;" | psql "$DB" -Aqt) + RES_PUB=$(echo "SELECT reserve_pub FROM exchange.reserves_in WHERE reserve_in_serial_id=1;" | psql "$DB" -Aqt) + OLD_EXP=$(echo "SELECT expiration_date FROM exchange.reserves WHERE reserve_pub='${RES_PUB}';" | psql "$DB" -Aqt) VAL_DELTA=1 - NEW_TIME=`expr $OLD_TIME - 3024000000000 || true` # 5 weeks - NEW_EXP=`expr $OLD_EXP - 3024000000000 || true` # 5 weeks - NEW_CREDIT=`expr $OLD_VAL + $VAL_DELTA || true` - echo "UPDATE exchange.reserves_in SET execution_date='${NEW_TIME}',credit_val=${NEW_CREDIT} WHERE reserve_in_serial_id=1;" | psql -Aqt $DB - echo "UPDATE exchange.reserves SET current_balance_val=${VAL_DELTA}+current_balance_val,expiration_date='${NEW_EXP}' WHERE reserve_pub='${RES_PUB}';" | psql -Aqt $DB + NEW_TIME=$(expr $OLD_TIME - 3024000000000 || true) # 5 weeks + NEW_EXP=$(expr $OLD_EXP - 3024000000000 || true) # 5 weeks + NEW_CREDIT=$(expr $OLD_VAL + $VAL_DELTA || true) + echo "UPDATE exchange.reserves_in SET execution_date='${NEW_TIME}',credit_val=${NEW_CREDIT} WHERE reserve_in_serial_id=1;" | psql -Aqt "$DB" + echo "UPDATE exchange.reserves SET current_balance_val=${VAL_DELTA}+current_balance_val,expiration_date='${NEW_EXP}' WHERE reserve_pub='${RES_PUB}';" | psql -Aqt "$DB" # Need to first run the aggregator so the transfer is marked as done exists pre_audit aggregator stop_libeufin # remove transaction from bank DB # Currently emulating this (to be deleted): - echo "DELETE FROM TalerRequestedPayments WHERE amount='TESTKUDOS:${VAL_DELTA}'" | sqlite3 ${MYDIR}/${DB}-nexus.sqlite3 + echo "DELETE FROM TalerRequestedPayments WHERE amount='TESTKUDOS:${VAL_DELTA}'" | psql "${DB}" launch_libeufin audit_only post_audit @@ -1419,12 +1618,12 @@ function test_21() { jq -e .reserve_lag_details[0] < test-audit-wire.json > /dev/null || exit_fail "Reserve closure lag not detected" - AMOUNT=`jq -r .reserve_lag_details[0].amount < test-audit-wire.json` + AMOUNT=$(jq -r .reserve_lag_details[0].amount < test-audit-wire.json) if test "x$AMOUNT" != "xTESTKUDOS:${VAL_DELTA}" then exit_fail "Reported total amount wrong: $AMOUNT" fi - AMOUNT=`jq -r .total_closure_amount_lag < test-audit-wire.json` + AMOUNT=$(jq -r .total_closure_amount_lag < test-audit-wire.json) if test "x$AMOUNT" != "xTESTKUDOS:${VAL_DELTA}" then exit_fail "Reported total amount wrong: $AMOUNT" @@ -1441,14 +1640,14 @@ function test_21() { function test_22() { echo "===========22: denomination key expired =================" - S_DENOM=`echo 'SELECT denominations_serial FROM exchange.reserves_out LIMIT 1;' | psql $DB -Aqt` + S_DENOM=$(echo 'SELECT denominations_serial FROM exchange.reserves_out LIMIT 1;' | psql "$DB" -Aqt) - OLD_START=`echo "SELECT valid_from FROM exchange.denominations WHERE denominations_serial='${S_DENOM}';" | psql $DB -Aqt` - OLD_WEXP=`echo "SELECT expire_withdraw FROM exchange.denominations WHERE denominations_serial='${S_DENOM}';" | psql $DB -Aqt` + OLD_START=$(echo "SELECT valid_from FROM exchange.denominations WHERE denominations_serial='${S_DENOM}';" | psql "$DB" -Aqt) + OLD_WEXP=$(echo "SELECT expire_withdraw FROM exchange.denominations WHERE denominations_serial='${S_DENOM}';" | psql "$DB" -Aqt) # Basically expires 'immediately', so that the withdraw must have been 'invalid' NEW_WEXP=$OLD_START - echo "UPDATE exchange.denominations SET expire_withdraw=${NEW_WEXP} WHERE denominations_serial='${S_DENOM}';" | psql -Aqt $DB + echo "UPDATE exchange.denominations SET expire_withdraw=${NEW_WEXP} WHERE denominations_serial='${S_DENOM}';" | psql -Aqt "$DB" run_audit @@ -1459,7 +1658,7 @@ function test_22() { echo PASS # Undo modification - echo "UPDATE exchange.denominations SET expire_withdraw=${OLD_WEXP} WHERE denominations_serial='${S_DENOM}';" | psql -Aqt $DB + echo "UPDATE exchange.denominations SET expire_withdraw=${OLD_WEXP} WHERE denominations_serial='${S_DENOM}';" | psql -Aqt "$DB" } @@ -1472,9 +1671,9 @@ function test_23() { # Need to first run the aggregator so the transfer is marked as done exists pre_audit aggregator - OLD_AMOUNT=`echo "SELECT amount_frac FROM exchange.wire_out WHERE wireout_uuid=1;" | psql $DB -Aqt` - NEW_AMOUNT=`expr $OLD_AMOUNT - 1000000 || true` - echo "UPDATE exchange.wire_out SET amount_frac=${NEW_AMOUNT} WHERE wireout_uuid=1;" | psql -Aqt $DB + OLD_AMOUNT=$(echo "SELECT amount_frac FROM exchange.wire_out WHERE wireout_uuid=1;" | psql "$DB" -Aqt) + NEW_AMOUNT=$(expr $OLD_AMOUNT - 1000000 || true) + echo "UPDATE exchange.wire_out SET amount_frac=${NEW_AMOUNT} WHERE wireout_uuid=1;" | psql -Aqt "$DB" audit_only post_audit @@ -1483,17 +1682,17 @@ function test_23() { jq -e .wire_out_inconsistencies[0] < test-audit-aggregation.json > /dev/null || exit_fail "Wire out inconsistency not detected" - ROW=`jq .wire_out_inconsistencies[0].rowid < test-audit-aggregation.json` - if test $ROW != 1 + ROW=$(jq .wire_out_inconsistencies[0].rowid < test-audit-aggregation.json) + if [ "$ROW" != 1 ] then exit_fail "Row wrong" fi - AMOUNT=`jq -r .total_wire_out_delta_plus < test-audit-aggregation.json` + AMOUNT=$(jq -r .total_wire_out_delta_plus < test-audit-aggregation.json) if test "x$AMOUNT" != "xTESTKUDOS:0" then exit_fail "Reported amount wrong: $AMOUNT" fi - AMOUNT=`jq -r .total_wire_out_delta_minus < test-audit-aggregation.json` + AMOUNT=$(jq -r .total_wire_out_delta_minus < test-audit-aggregation.json) if test "x$AMOUNT" != "xTESTKUDOS:0.01" then exit_fail "Reported total amount wrong: $AMOUNT" @@ -1501,8 +1700,8 @@ function test_23() { echo PASS echo "Second pass: changing how amount is wrong to other direction" - NEW_AMOUNT=`expr $OLD_AMOUNT + 1000000 || true` - echo "UPDATE exchange.wire_out SET amount_frac=${NEW_AMOUNT} WHERE wireout_uuid=1;" | psql -Aqt $DB + NEW_AMOUNT=$(expr $OLD_AMOUNT + 1000000 || true) + echo "UPDATE exchange.wire_out SET amount_frac=${NEW_AMOUNT} WHERE wireout_uuid=1;" | psql -Aqt "$DB" pre_audit audit_only @@ -1512,17 +1711,17 @@ function test_23() { jq -e .wire_out_inconsistencies[0] < test-audit-aggregation.json > /dev/null || exit_fail "Wire out inconsistency not detected" - ROW=`jq .wire_out_inconsistencies[0].rowid < test-audit-aggregation.json` - if test $ROW != 1 + ROW=$(jq .wire_out_inconsistencies[0].rowid < test-audit-aggregation.json) + if [ "$ROW" != 1 ] then exit_fail "Row wrong" fi - AMOUNT=`jq -r .total_wire_out_delta_minus < test-audit-aggregation.json` + AMOUNT=$(jq -r .total_wire_out_delta_minus < test-audit-aggregation.json) if test "x$AMOUNT" != "xTESTKUDOS:0" then exit_fail "Reported amount wrong: $AMOUNT" fi - AMOUNT=`jq -r .total_wire_out_delta_plus < test-audit-aggregation.json` + AMOUNT=$(jq -r .total_wire_out_delta_plus < test-audit-aggregation.json) if test "x$AMOUNT" != "xTESTKUDOS:0.01" then exit_fail "Reported total amount wrong: $AMOUNT" @@ -1540,13 +1739,13 @@ function test_24() { echo "===========24: deposits missing ===========" # Modify denom_sig, so it is wrong - CNT=`echo "SELECT COUNT(*) FROM auditor.deposit_confirmations;" | psql -Aqt $DB` + CNT=$(echo "SELECT COUNT(*) FROM auditor.deposit_confirmations;" | psql -Aqt "$DB") if test x$CNT = x0 then echo "Skipping deposits missing test: no deposit confirmations in database!" else - echo "DELETE FROM exchange.deposits;" | psql -Aqt $DB - echo "DELETE FROM exchange.deposits WHERE deposit_serial_id=1;" | psql -Aqt $DB + echo "DELETE FROM exchange.deposits;" | psql -Aqt "$DB" + echo "DELETE FROM exchange.deposits WHERE deposit_serial_id=1;" | psql -Aqt "$DB" run_audit @@ -1554,12 +1753,12 @@ function test_24() { jq -e .deposit_confirmation_inconsistencies[0] < test-audit-deposits.json > /dev/null || exit_fail "Deposit confirmation inconsistency NOT detected" - AMOUNT=`jq -er .missing_deposit_confirmation_total < test-audit-deposits.json` + AMOUNT=$(jq -er .missing_deposit_confirmation_total < test-audit-deposits.json) if test x$AMOUNT = xTESTKUDOS:0 then exit_fail "Expected non-zero total missing deposit confirmation amount" fi - COUNT=`jq -er .missing_deposit_confirmation_count < test-audit-deposits.json` + COUNT=$(jq -er .missing_deposit_confirmation_count < test-audit-deposits.json) if test x$AMOUNT = x0 then exit_fail "Expected non-zero total missing deposit confirmation count" @@ -1579,7 +1778,7 @@ function test_25() { echo "=========25: inconsistent coin history=========" # Drop refund, so coin history is bogus. - echo "DELETE FROM exchange.refunds WHERE refund_serial_id=1;" | psql -Aqt $DB + echo "DELETE FROM exchange.refunds WHERE refund_serial_id=1;" | psql -Aqt "$DB" run_audit aggregator @@ -1591,14 +1790,14 @@ function test_25() { # go legitimately unnoticed. jq -e .emergencies[0] < test-audit-coins.json > /dev/null || exit_fail "Denomination value emergency NOT reported" - AMOUNT=`jq -er .total_coin_delta_minus < test-audit-aggregation.json` + AMOUNT=$(jq -er .total_coin_delta_minus < test-audit-aggregation.json) if test x$AMOUNT = xTESTKUDOS:0 then exit_fail "Expected non-zero total inconsistency amount from coins" fi # Note: if the wallet withdrew much more than it spent, this might indeed # go legitimately unnoticed. - COUNT=`jq -er .emergencies_risk_by_amount < test-audit-coins.json` + COUNT=$(jq -er .emergencies_risk_by_amount < test-audit-coins.json) if test x$AMOUNT = xTESTKUDOS:0 then exit_fail "Expected non-zero emergency-by-amount" @@ -1614,10 +1813,14 @@ function test_25() { function test_26() { echo "===========26: deposit wire target malformed =================" # Expects 'payto_uri', not 'url' (also breaks signature, but we cannot even check that). - SERIAL=`echo "SELECT deposit_serial_id FROM exchange.deposits WHERE amount_with_fee_val=3 AND amount_with_fee_frac=0 ORDER BY deposit_serial_id LIMIT 1" | psql $DB -Aqt` - OLD_WIRE_ID=`echo "SELECT wire_target_h_payto FROM exchange.deposits WHERE deposit_serial_id=${SERIAL};" | psql $DB -Aqt` - echo "INSERT INTO exchange.wire_targets (payto_uri, wire_target_h_payto) VALUES ('payto://x-taler-bank/localhost/testuser-xxlargtp', '\x1e8f31936b3cee8f8afd3aac9e38b5db42d45b721ffc4eb1e5b9ddaf1565660b');" | psql $DB -Aqt - echo "UPDATE exchange.deposits SET wire_target_h_payto='\x1e8f31936b3cee8f8afd3aac9e38b5db42d45b721ffc4eb1e5b9ddaf1565660b' WHERE deposit_serial_id=${SERIAL}" | psql -Aqt $DB + SERIAL=$(echo "SELECT deposit_serial_id FROM exchange.deposits WHERE amount_with_fee_val=3 AND amount_with_fee_frac=0 ORDER BY deposit_serial_id LIMIT 1" | psql "$DB" -Aqt) + OLD_WIRE_ID=$(echo "SELECT wire_target_h_payto FROM exchange.deposits WHERE deposit_serial_id=${SERIAL};" | psql "$DB" -Aqt) +# shellcheck disable=SC2028 + echo "INSERT INTO exchange.wire_targets (payto_uri, wire_target_h_payto) VALUES ('payto://x-taler-bank/localhost/testuser-xxlargtp', '\x1e8f31936b3cee8f8afd3aac9e38b5db42d45b721ffc4eb1e5b9ddaf1565660b');" \ + | psql "$DB" -Aqt +# shellcheck disable=SC2028 + echo "UPDATE exchange.deposits SET wire_target_h_payto='\x1e8f31936b3cee8f8afd3aac9e38b5db42d45b721ffc4eb1e5b9ddaf1565660b' WHERE deposit_serial_id=${SERIAL}" \ + | psql -Aqt "$DB" run_audit @@ -1625,34 +1828,34 @@ function test_26() { jq -e .bad_sig_losses[0] < test-audit-coins.json > /dev/null || exit_fail "Bad signature not detected" - ROW=`jq -e .bad_sig_losses[0].row < test-audit-coins.json` - if test $ROW != ${SERIAL} + ROW=$(jq -e .bad_sig_losses[0].row < test-audit-coins.json) + if [ "$ROW" != ${SERIAL} ] then exit_fail "Row wrong, got $ROW" fi - LOSS=`jq -r .bad_sig_losses[0].loss < test-audit-coins.json` - if test $LOSS != "TESTKUDOS:3" + LOSS=$(jq -r .bad_sig_losses[0].loss < test-audit-coins.json) + if [ "$LOSS" != "TESTKUDOS:3" ] then exit_fail "Wrong deposit bad signature loss, got $LOSS" fi - OP=`jq -r .bad_sig_losses[0].operation < test-audit-coins.json` - if test $OP != "deposit" + OP=$(jq -r .bad_sig_losses[0].operation < test-audit-coins.json) + if [ "$OP" != "deposit" ] then exit_fail "Wrong operation, got $OP" fi - LOSS=`jq -r .irregular_loss < test-audit-coins.json` - if test $LOSS != "TESTKUDOS:3" + LOSS=$(jq -r .irregular_loss < test-audit-coins.json) + if [ "$LOSS" != "TESTKUDOS:3" ] then exit_fail "Wrong total bad sig loss, got $LOSS" fi - echo PASS + echo "PASS" # Undo: - echo "UPDATE exchange.deposits SET wire_target_h_payto='$OLD_WIRE_ID' WHERE deposit_serial_id=${SERIAL}" | psql -Aqt $DB - + echo "UPDATE exchange.deposits SET wire_target_h_payto='$OLD_WIRE_ID' WHERE deposit_serial_id=${SERIAL}" \ + | psql -Aqt "$DB" } # Test for duplicate wire transfer subject @@ -1662,26 +1865,28 @@ function test_27() { pre_audit aggregator stop_libeufin # Obtain data to duplicate. - WTID=`echo SELECT wtid FROM TalerRequestedPayments WHERE id=1 | sqlite3 ${MYDIR}/${DB}-nexus.sqlite3` + WTID=$(echo SELECT wtid FROM TalerRequestedPayments WHERE id=1 | psql "${DB}") echo WTID=$WTID - OTHER_IBAN=`echo -e "SELECT iban FROM BankAccounts WHERE label='fortytwo'" | sqlite3 ${MYDIR}/${DB}-sandbox.sqlite3` + OTHER_IBAN=$(echo -e "SELECT iban FROM BankAccounts WHERE label='fortytwo'" | psql "${DB}") # 'rawConfirmation' is set to 2 here, that doesn't # point to any record. That's only needed to set a non null value. - echo -e "INSERT INTO PaymentInitiations (bankAccount,preparationDate,submissionDate,sum,currency,endToEndId,paymentInformationId,instructionId,subject,creditorIban,creditorBic,creditorName,submitted,messageId,rawConfirmation) VALUES (1,$(date +%s),$(expr $(date +%s) + 2),10,'TESTKUDOS','NOTGIVEN','unused','unused','$WTID http://exchange.example.com/','$OTHER_IBAN','SANDBOXX','Forty Two','unused',1,2)" | sqlite3 ${MYDIR}/${DB}-nexus.sqlite3 - echo -e "INSERT INTO TalerRequestedPayments (facade,payment,requestUid,amount,exchangeBaseUrl,wtid,creditAccount) VALUES (1,2,'unused','TESTKUDOS:1','http://exchange.example.com/','$WTID','payto://iban/SANDBOXX/$OTHER_IBAN?receiver-name=Forty+Two')" | sqlite3 ${MYDIR}/${DB}-nexus.sqlite3 + echo -e "INSERT INTO PaymentInitiations (bankAccount,preparationDate,submissionDate,sum,currency,endToEndId,paymentInformationId,instructionId,subject,creditorIban,creditorBic,creditorName,submitted,messageId,rawConfirmation) VALUES (1,$(date +%s),$(expr $(date +%s) + 2),10,'TESTKUDOS','NOTGIVEN','unused','unused','$WTID http://exchange.example.com/','$OTHER_IBAN','SANDBOXX','Forty Two','unused',1,2)" \ + | psql "${DB}" + echo -e "INSERT INTO TalerRequestedPayments (facade,payment,requestUid,amount,exchangeBaseUrl,wtid,creditAccount) VALUES (1,2,'unused','TESTKUDOS:1','http://exchange.example.com/','$WTID','payto://iban/SANDBOXX/$OTHER_IBAN?receiver-name=Forty+Two')" \ + | psql "${DB}" launch_libeufin audit_only post_audit echo -n "Testing inconsistency detection... " - AMOUNT=`jq -r .wire_format_inconsistencies[0].amount < test-audit-wire.json` + AMOUNT=$(jq -r .wire_format_inconsistencies[0].amount < test-audit-wire.json) if test "${AMOUNT}" != "TESTKUDOS:1" then exit_fail "Amount wrong, got ${AMOUNT}" fi - AMOUNT=`jq -r .total_wire_format_amount < test-audit-wire.json` + AMOUNT=$(jq -r .total_wire_format_amount < test-audit-wire.json) if test "${AMOUNT}" != "TESTKUDOS:1" then exit_fail "Wrong total wire format amount, got $AMOUNT" @@ -1700,32 +1905,34 @@ function test_28() { echo "===========28: known_coins signature wrong=================" # Modify denom_sig, so it is wrong - OLD_SIG=`echo 'SELECT denom_sig FROM exchange.known_coins LIMIT 1;' | psql $DB -Aqt` - COIN_PUB=`echo "SELECT coin_pub FROM exchange.known_coins WHERE denom_sig='$OLD_SIG';" | psql $DB -Aqt` - echo "UPDATE exchange.known_coins SET denom_sig='\x0000000100000000287369672d76616c200a2028727361200a2020287320233542383731423743393036444643303442424430453039353246413642464132463537303139374131313437353746324632323332394644443146324643333445393939413336363430334233413133324444464239413833353833464536354442374335434445304441453035374438363336434541423834463843323843344446304144363030343430413038353435363039373833434431333239393736423642433437313041324632414132414435413833303432434346314139464635394244434346374436323238344143354544364131373739463430353032323241373838423837363535453434423145443831364244353638303232413123290a2020290a20290b' WHERE coin_pub='$COIN_PUB'" | psql -Aqt $DB + OLD_SIG=$(echo 'SELECT denom_sig FROM exchange.known_coins LIMIT 1;' | psql "$DB" -Aqt) + COIN_PUB=$(echo "SELECT coin_pub FROM exchange.known_coins WHERE denom_sig='$OLD_SIG';" | psql "$DB" -Aqt) +# shellcheck disable=SC2028 + echo "UPDATE exchange.known_coins SET denom_sig='\x0000000100000000287369672d76616c200a2028727361200a2020287320233542383731423743393036444643303442424430453039353246413642464132463537303139374131313437353746324632323332394644443146324643333445393939413336363430334233413133324444464239413833353833464536354442374335434445304441453035374438363336434541423834463843323843344446304144363030343430413038353435363039373833434431333239393736423642433437313041324632414132414435413833303432434346314139464635394244434346374436323238344143354544364131373739463430353032323241373838423837363535453434423145443831364244353638303232413123290a2020290a20290b' WHERE coin_pub='$COIN_PUB'" \ + | psql -Aqt "$DB" run_audit aggregator echo -n "Testing inconsistency detection... " - LOSS=`jq -r .bad_sig_losses[0].loss < test-audit-aggregation.json` - if test $LOSS == "TESTKUDOS:0" + LOSS=$(jq -r .bad_sig_losses[0].loss < test-audit-aggregation.json) + if [ "$LOSS" == "TESTKUDOS:0" ] then exit_fail "Wrong deposit bad signature loss, got $LOSS" fi - OP=`jq -r .bad_sig_losses[0].operation < test-audit-aggregation.json` - if test $OP != "wire" + OP=$(jq -r .bad_sig_losses[0].operation < test-audit-aggregation.json) + if [ "$OP" != "wire" ] then exit_fail "Wrong operation, got $OP" fi - TAB=`jq -r .row_inconsistencies[0].table < test-audit-aggregation.json` + TAB=$(jq -r .row_inconsistencies[0].table < test-audit-aggregation.json) if test $TAB != "deposit" then exit_fail "Wrong table for row inconsistency, got $TAB" fi - LOSS=`jq -r .total_bad_sig_loss < test-audit-aggregation.json` - if test $LOSS == "TESTKUDOS:0" + LOSS=$(jq -r .total_bad_sig_loss < test-audit-aggregation.json) + if [ "$LOSS" == "TESTKUDOS:0" ] then exit_fail "Wrong total bad sig loss, got $LOSS" fi @@ -1742,25 +1949,25 @@ function test_28() { function test_29() { echo "===========29: withdraw fee inconsistency =================" - echo "UPDATE exchange.denominations SET fee_withdraw_frac=5000000 WHERE coin_val=1;" | psql -Aqt $DB + echo "UPDATE exchange.denominations SET fee_withdraw_frac=5000000 WHERE coin_val=1;" | psql -Aqt "$DB" run_audit echo -n "Testing inconsistency detection... " - AMOUNT=`jq -r .total_balance_summary_delta_minus < test-audit-reserves.json` + AMOUNT=$(jq -r .total_balance_summary_delta_minus < test-audit-reserves.json) if test "x$AMOUNT" == "xTESTKUDOS:0" then exit_fail "Reported total amount wrong: $AMOUNT" fi - PROFIT=`jq -r .amount_arithmetic_inconsistencies[0].profitable < test-audit-coins.json` + PROFIT=$(jq -r .amount_arithmetic_inconsistencies[0].profitable < test-audit-coins.json) if test "x$PROFIT" != "x-1" then exit_fail "Reported wrong profitability: $PROFIT" fi echo "OK" # Undo - echo "UPDATE exchange.denominations SET fee_withdraw_frac=2000000 WHERE coin_val=1;" | psql -Aqt $DB + echo "UPDATE exchange.denominations SET fee_withdraw_frac=2000000 WHERE coin_val=1;" | psql -Aqt "$DB" } @@ -1770,17 +1977,17 @@ function test_29() { function test_30() { echo "===========30: melt fee inconsistency =================" - echo "UPDATE exchange.denominations SET fee_refresh_frac=5000000 WHERE coin_val=10;" | psql -Aqt $DB + echo "UPDATE exchange.denominations SET fee_refresh_frac=5000000 WHERE coin_val=10;" | psql -Aqt "$DB" run_audit echo -n "Testing inconsistency detection... " - AMOUNT=`jq -r .bad_sig_losses[0].loss < test-audit-coins.json` + AMOUNT=$(jq -r .bad_sig_losses[0].loss < test-audit-coins.json) if test "x$AMOUNT" == "xTESTKUDOS:0" then exit_fail "Reported total amount wrong: $AMOUNT" fi - PROFIT=`jq -r .amount_arithmetic_inconsistencies[0].profitable < test-audit-coins.json` + PROFIT=$(jq -r .amount_arithmetic_inconsistencies[0].profitable < test-audit-coins.json) if test "x$PROFIT" != "x-1" then exit_fail "Reported profitability wrong: $PROFIT" @@ -1789,7 +1996,7 @@ function test_30() { jq -e .emergencies[0] < test-audit-coins.json > /dev/null && exit_fail "Unexpected emergency detected in ordinary run" echo "OK" # Undo - echo "UPDATE exchange.denominations SET fee_refresh_frac=3000000 WHERE coin_val=10;" | psql -Aqt $DB + echo "UPDATE exchange.denominations SET fee_refresh_frac=3000000 WHERE coin_val=10;" | psql -Aqt "$DB" } @@ -1800,17 +2007,17 @@ function test_31() { echo "===========31: deposit fee inconsistency =================" - echo "UPDATE exchange.denominations SET fee_deposit_frac=5000000 WHERE coin_val=8;" | psql -Aqt $DB + echo "UPDATE exchange.denominations SET fee_deposit_frac=5000000 WHERE coin_val=8;" | psql -Aqt "$DB" run_audit aggregator echo -n "Testing inconsistency detection... " - AMOUNT=`jq -r .irregular_loss < test-audit-coins.json` + AMOUNT=$(jq -r .irregular_loss < test-audit-coins.json) if test "x$AMOUNT" == "xTESTKUDOS:0" then exit_fail "Reported total amount wrong: $AMOUNT" fi - OP=`jq -r --arg dep "deposit" '.bad_sig_losses[] | select(.operation == $dep) | .operation'< test-audit-coins.json | head -n1` + OP=$(jq -r --arg dep "deposit" '.bad_sig_losses[] | select(.operation == $dep) | .operation'< test-audit-coins.json | head -n1) if test "x$OP" != "xdeposit" then exit_fail "Reported wrong operation: $OP" @@ -1818,7 +2025,7 @@ function test_31() { echo "OK" # Undo - echo "UPDATE exchange.denominations SET fee_deposit_frac=2000000 WHERE coin_val=8;" | psql -Aqt $DB + echo "UPDATE exchange.denominations SET fee_deposit_frac=2000000 WHERE coin_val=8;" | psql -Aqt "$DB" } @@ -1830,20 +2037,22 @@ function test_32() { echo "===========32: known_coins signature wrong w. aggregation=================" # Modify denom_sig, so it is wrong - OLD_SIG=`echo 'SELECT denom_sig FROM exchange.known_coins LIMIT 1;' | psql $DB -At` - COIN_PUB=`echo "SELECT coin_pub FROM exchange.known_coins WHERE denom_sig='$OLD_SIG';" | psql $DB -At` - echo "UPDATE exchange.known_coins SET denom_sig='\x0000000100000000287369672d76616c200a2028727361200a2020287320233542383731423743393036444643303442424430453039353246413642464132463537303139374131313437353746324632323332394644443146324643333445393939413336363430334233413133324444464239413833353833464536354442374335434445304441453035374438363336434541423834463843323843344446304144363030343430413038353435363039373833434431333239393736423642433437313041324632414132414435413833303432434346314139464635394244434346374436323238344143354544364131373739463430353032323241373838423837363535453434423145443831364244353638303232413123290a2020290a20290b' WHERE coin_pub='$COIN_PUB'" | psql -Aqt $DB + OLD_SIG=$(echo 'SELECT denom_sig FROM exchange.known_coins LIMIT 1;' | psql "$DB" -At) + COIN_PUB=$(echo "SELECT coin_pub FROM exchange.known_coins WHERE denom_sig='$OLD_SIG';" | psql "$DB" -At) +# shellcheck disable=SC2028 + echo "UPDATE exchange.known_coins SET denom_sig='\x0000000100000000287369672d76616c200a2028727361200a2020287320233542383731423743393036444643303442424430453039353246413642464132463537303139374131313437353746324632323332394644443146324643333445393939413336363430334233413133324444464239413833353833464536354442374335434445304441453035374438363336434541423834463843323843344446304144363030343430413038353435363039373833434431333239393736423642433437313041324632414132414435413833303432434346314139464635394244434346374436323238344143354544364131373739463430353032323241373838423837363535453434423145443831364244353638303232413123290a2020290a20290b' WHERE coin_pub='$COIN_PUB'" \ + | psql -Aqt "$DB" run_audit aggregator echo -n "Testing inconsistency detection... " - AMOUNT=`jq -r .total_bad_sig_loss < test-audit-aggregation.json` + AMOUNT=$(jq -r .total_bad_sig_loss < test-audit-aggregation.json) if test "x$AMOUNT" == "xTESTKUDOS:0" then exit_fail "Reported total amount wrong: $AMOUNT" fi - OP=`jq -r .bad_sig_losses[0].operation < test-audit-aggregation.json` + OP=$(jq -r .bad_sig_losses[0].operation < test-audit-aggregation.json) if test "x$OP" != "xwire" then exit_fail "Reported wrong operation: $OP" @@ -1886,83 +2095,83 @@ function test_33() { echo PASS - LOSS=`jq -r .total_bad_sig_loss < test-audit-aggregation.json` - if test $LOSS != "TESTKUDOS:0" + LOSS=$(jq -r .total_bad_sig_loss < test-audit-aggregation.json) + if [ "$LOSS" != "TESTKUDOS:0" ] then exit_fail "Wrong total bad sig loss from aggregation, got unexpected loss of $LOSS" fi - LOSS=`jq -r .irregular_loss < test-audit-coins.json` - if test $LOSS != "TESTKUDOS:0" + LOSS=$(jq -r .irregular_loss < test-audit-coins.json) + if [ "$LOSS" != "TESTKUDOS:0" ] then exit_fail "Wrong total bad sig loss from coins, got unexpected loss of $LOSS" fi - LOSS=`jq -r .total_bad_sig_loss < test-audit-reserves.json` - if test $LOSS != "TESTKUDOS:0" + LOSS=$(jq -r .total_bad_sig_loss < test-audit-reserves.json) + if [ "$LOSS" != "TESTKUDOS:0" ] then exit_fail "Wrong total bad sig loss from reserves, got unexpected loss of $LOSS" fi echo -n "Test for wire amounts... " - WIRED=`jq -r .total_wire_in_delta_plus < test-audit-wire.json` - if test $WIRED != "TESTKUDOS:0" + WIRED=$(jq -r .total_wire_in_delta_plus < test-audit-wire.json) + if [ "$WIRED" != "TESTKUDOS:0" ] then exit_fail "Expected total wire delta plus wrong, got $WIRED" fi - WIRED=`jq -r .total_wire_in_delta_minus < test-audit-wire.json` - if test $WIRED != "TESTKUDOS:0" + WIRED=$(jq -r .total_wire_in_delta_minus < test-audit-wire.json) + if [ "$WIRED" != "TESTKUDOS:0" ] then exit_fail "Expected total wire delta minus wrong, got $WIRED" fi - WIRED=`jq -r .total_wire_out_delta_plus < test-audit-wire.json` - if test $WIRED != "TESTKUDOS:0" + WIRED=$(jq -r .total_wire_out_delta_plus < test-audit-wire.json) + if [ "$WIRED" != "TESTKUDOS:0" ] then exit_fail "Expected total wire delta plus wrong, got $WIRED" fi - WIRED=`jq -r .total_wire_out_delta_minus < test-audit-wire.json` - if test $WIRED != "TESTKUDOS:0" + WIRED=$(jq -r .total_wire_out_delta_minus < test-audit-wire.json) + if [ "$WIRED" != "TESTKUDOS:0" ] then exit_fail "Expected total wire delta minus wrong, got $WIRED" fi - WIRED=`jq -r .total_misattribution_in < test-audit-wire.json` - if test $WIRED != "TESTKUDOS:0" + WIRED=$(jq -r .total_misattribution_in < test-audit-wire.json) + if [ "$WIRED" != "TESTKUDOS:0" ] then exit_fail "Expected total misattribution in wrong, got $WIRED" fi echo PASS echo -n "Checking for unexpected arithmetic differences " - LOSS=`jq -r .total_arithmetic_delta_plus < test-audit-aggregation.json` - if test $LOSS != "TESTKUDOS:0" + LOSS=$(jq -r .total_arithmetic_delta_plus < test-audit-aggregation.json) + if [ "$LOSS" != "TESTKUDOS:0" ] then exit_fail "Wrong arithmetic delta from aggregations, got unexpected plus of $LOSS" fi - LOSS=`jq -r .total_arithmetic_delta_minus < test-audit-aggregation.json` - if test $LOSS != "TESTKUDOS:0" + LOSS=$(jq -r .total_arithmetic_delta_minus < test-audit-aggregation.json) + if [ "$LOSS" != "TESTKUDOS:0" ] then exit_fail "Wrong arithmetic delta from aggregation, got unexpected minus of $LOSS" fi - LOSS=`jq -r .total_arithmetic_delta_plus < test-audit-coins.json` - if test $LOSS != "TESTKUDOS:0" + LOSS=$(jq -r .total_arithmetic_delta_plus < test-audit-coins.json) + if [ "$LOSS" != "TESTKUDOS:0" ] then exit_fail "Wrong arithmetic delta from coins, got unexpected plus of $LOSS" fi - LOSS=`jq -r .total_arithmetic_delta_minus < test-audit-coins.json` - if test $LOSS != "TESTKUDOS:0" + LOSS=$(jq -r .total_arithmetic_delta_minus < test-audit-coins.json) + if [ "$LOSS" != "TESTKUDOS:0" ] then exit_fail "Wrong arithmetic delta from coins, got unexpected minus of $LOSS" fi - LOSS=`jq -r .total_arithmetic_delta_plus < test-audit-reserves.json` - if test $LOSS != "TESTKUDOS:0" + LOSS=$(jq -r .total_arithmetic_delta_plus < test-audit-reserves.json) + if [ "$LOSS" != "TESTKUDOS:0" ] then exit_fail "Wrong arithmetic delta from reserves, got unexpected plus of $LOSS" fi - LOSS=`jq -r .total_arithmetic_delta_minus < test-audit-reserves.json` - if test $LOSS != "TESTKUDOS:0" + LOSS=$(jq -r .total_arithmetic_delta_minus < test-audit-reserves.json) + if [ "$LOSS" != "TESTKUDOS:0" ] then exit_fail "Wrong arithmetic delta from reserves, got unexpected minus of $LOSS" fi - DRAINED=`jq -r .total_drained < test-audit-wire.json` + DRAINED=$(jq -r .total_drained < test-audit-wire.json) if test $DRAINED != "TESTKUDOS:0.1" then exit_fail "Wrong amount drained, got unexpected drain of $DRAINED" @@ -1990,13 +2199,13 @@ function test_33() { # Sets $fail to 0 on success, non-zero on failure. function check_with_database() { - BASEDB=$1 - CONF=$1.conf + BASEDB="$1" + CONF="$1.conf" ORIGIN=$(pwd) MY_TMP_DIR=$(dirname $1) echo "Running test suite with database $BASEDB using configuration $CONF" - MASTER_PRIV_FILE=${BASEDB}.mpriv - taler-config -f -c ${CONF} -s exchange-offline -o MASTER_PRIV_FILE -V ${MASTER_PRIV_FILE} + MASTER_PRIV_FILE="${BASEDB}.mpriv" + taler-config -f -c "${CONF}" -s exchange-offline -o MASTER_PRIV_FILE -V ${MASTER_PRIV_FILE} MASTER_PUB=$(gnunet-ecc -p $MASTER_PRIV_FILE) echo "MASTER PUB is ${MASTER_PUB} using file ${MASTER_PRIV_FILE}" @@ -2021,8 +2230,6 @@ function check_with_database() - - # *************** Main logic starts here ************** # ####### Setup globals ###### @@ -2078,6 +2285,18 @@ echo " DONE" PGHOST="$TMPDIR/sockets" export PGHOST +# FIXME... +MYDIR=bar/ +DB=foo +# foo.sql +# foo.conf +# foo.mpriv +check_with_database "$MYDIR/$DB" + + +exit 0 + + echo "Generating fresh database at $MYDIR" if faketime -f '-1 d' ./generate-auditor-basedb.sh "$MYDIR/$DB" then From e6023a54b9b9718e172e5498b7c02f1a344fc67e Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Mon, 17 Jul 2023 16:39:34 +0200 Subject: [PATCH 2/3] -work on auditor test --- src/auditor/test-auditor.sh | 330 ++++++++++++++++++++++-------------- 1 file changed, 201 insertions(+), 129 deletions(-) diff --git a/src/auditor/test-auditor.sh b/src/auditor/test-auditor.sh index b9b7792d6..99f744a6d 100755 --- a/src/auditor/test-auditor.sh +++ b/src/auditor/test-auditor.sh @@ -1275,30 +1275,31 @@ function test_13() { COIN_PUB=$(echo "SELECT old_coin_pub FROM exchange.refresh_commitments LIMIT 1;" | psql "$DB" -Aqt) OLD_SIG=$(echo "SELECT old_coin_sig FROM exchange.refresh_commitments WHERE old_coin_pub='$COIN_PUB';" | psql "$DB" -Aqt) NEW_SIG="\xba588af7c13c477dca1ac458f65cc484db8fba53b969b873f4353ecbd815e6b4c03f42c0cb63a2b609c2d726e612fd8e0c084906a41f409b6a23a08a83c89a02" - echo "UPDATE exchange.refresh_commitments SET old_coin_sig='$NEW_SIG' WHERE old_coin_pub='$COIN_PUB'" | psql -Aqt "$DB" + echo "UPDATE exchange.refresh_commitments SET old_coin_sig='$NEW_SIG' WHERE old_coin_pub='$COIN_PUB'" \ + | psql -Aqt "$DB" run_audit echo -n "Testing inconsistency detection... " OP=$(jq -er .bad_sig_losses[0].operation < test-audit-coins.json) - if test x$OP != xmelt + if [ "$OP" != "melt" ] then exit_fail "Operation wrong, got $OP" fi LOSS=$(jq -er .bad_sig_losses[0].loss < test-audit-coins.json) TOTAL_LOSS=$(jq -er .irregular_loss < test-audit-coins.json) - if test x$LOSS != x$TOTAL_LOSS + if [ "$LOSS" != "$TOTAL_LOSS" ] then exit_fail "Loss inconsistent, got $LOSS and $TOTAL_LOSS" fi - if test x$TOTAL_LOSS = TESTKUDOS:0 + if [ "$TOTAL_LOSS" = "TESTKUDOS:0" ] then exit_fail "Loss zero" fi - echo PASS + echo "PASS" # cannot easily undo DELETE, hence full reload full_reload @@ -1314,22 +1315,23 @@ function test_14() { # actual outgoing wire transfers, so we need to run the # aggregator here. pre_audit aggregator - echo "UPDATE exchange.wire_fee SET wire_fee_frac=100;" | psql -Aqt "$DB" + echo "UPDATE exchange.wire_fee SET wire_fee_frac=100;" \ + | psql -Aqt "$DB" audit_only post_audit echo -n "Testing inconsistency detection... " TABLE=$(jq -r .row_inconsistencies[0].table < test-audit-aggregation.json) - if test "x$TABLE" != "xwire-fee" + if [ "$TABLE" != "wire-fee" ] then exit_fail "Reported table wrong: $TABLE" fi DIAG=$(jq -r .row_inconsistencies[0].diagnostic < test-audit-aggregation.json) - if test "x$DIAG" != "xwire fee signature invalid at given time" + if [ "$DIAG" != "wire fee signature invalid at given time" ] then exit_fail "Reported diagnostic wrong: $DIAG" fi - echo PASS + echo "PASS" # cannot easily undo aggregator, hence full reload full_reload @@ -1350,14 +1352,15 @@ function test_15() { echo -n "Testing inconsistency detection... " OP=$(jq -r .bad_sig_losses[0].operation < test-audit-coins.json) - if test "x$OP" != "xdeposit" + if [ "$OP" != "deposit" ] then exit_fail "Reported operation wrong: $OP" fi - echo PASS + echo "PASS" # Restore DB - echo "UPDATE exchange.deposits SET wire_salt='$SALT' WHERE deposit_serial_id=1;" | psql -Aqt "$DB" + echo "UPDATE exchange.deposits SET wire_salt='$SALT' WHERE deposit_serial_id=1;" \ + | psql -Aqt "$DB" } @@ -1375,64 +1378,66 @@ function test_16() { stop_libeufin OLD_AMOUNT=$(echo "SELECT amount FROM TalerRequestedPayments WHERE id='1';" | psql "${DB}") NEW_AMOUNT="TESTKUDOS:50" - echo "UPDATE TalerRequestedPayments SET amount='${NEW_AMOUNT}' WHERE id='1';" | psql "${DB}" + echo "UPDATE TalerRequestedPayments SET amount='${NEW_AMOUNT}' WHERE id='1';" \ + | psql "${DB}" launch_libeufin audit_only echo -n "Testing inconsistency detection... " AMOUNT=$(jq -r .wire_out_amount_inconsistencies[0].amount_justified < test-audit-wire.json) - if test "x$AMOUNT" != "x$OLD_AMOUNT" + if [ "$AMOUNT" != "$OLD_AMOUNT" ] then exit_fail "Reported justified amount wrong: $AMOUNT" fi AMOUNT=$(jq -r .wire_out_amount_inconsistencies[0].amount_wired < test-audit-wire.json) - if test "x$AMOUNT" != "x$NEW_AMOUNT" + if [ "$AMOUNT" != "$NEW_AMOUNT" ] then exit_fail "Reported wired amount wrong: $AMOUNT" fi TOTAL_AMOUNT=$(jq -r .total_wire_out_delta_minus < test-audit-wire.json) - if test "x$TOTAL_AMOUNT" != "xTESTKUDOS:0" + if [ "$TOTAL_AMOUNT" != "TESTKUDOS:0" ] then exit_fail "Reported total wired amount minus wrong: $TOTAL_AMOUNT" fi TOTAL_AMOUNT=$(jq -r .total_wire_out_delta_plus < test-audit-wire.json) - if test "x$TOTAL_AMOUNT" = "xTESTKUDOS:0" + if [ "$TOTAL_AMOUNT" = "TESTKUDOS:0" ] then exit_fail "Reported total wired amount plus wrong: $TOTAL_AMOUNT" fi - echo PASS + echo "PASS" stop_libeufin echo "Second modification: wire nothing" NEW_AMOUNT="TESTKUDOS:0" - echo "UPDATE TalerRequestedPayments SET amount='${NEW_AMOUNT}' WHERE id='1';" | psql "${DB}" + echo "UPDATE TalerRequestedPayments SET amount='${NEW_AMOUNT}' WHERE id='1';" \ + | psql "${DB}" launch_libeufin audit_only stop_libeufin echo -n "Testing inconsistency detection... " AMOUNT=$(jq -r .wire_out_amount_inconsistencies[0].amount_justified < test-audit-wire.json) - if test "x$AMOUNT" != "x$OLD_AMOUNT" + if [ "$AMOUNT" != "$OLD_AMOUNT" ] then exit_fail "Reported justified amount wrong: $AMOUNT" fi AMOUNT=$(jq -r .wire_out_amount_inconsistencies[0].amount_wired < test-audit-wire.json) - if test "x$AMOUNT" != "x$NEW_AMOUNT" + if [ "$AMOUNT" != "$NEW_AMOUNT" ] then exit_fail "Reported wired amount wrong: $AMOUNT" fi TOTAL_AMOUNT=$(jq -r .total_wire_out_delta_minus < test-audit-wire.json) - if test "x$TOTAL_AMOUNT" != "x$OLD_AMOUNT" + if [ "$TOTAL_AMOUNT" != "$OLD_AMOUNT" ] then exit_fail "Reported total wired amount minus wrong: $TOTAL_AMOUNT (wanted $OLD_AMOUNT)" fi TOTAL_AMOUNT=$(jq -r .total_wire_out_delta_plus < test-audit-wire.json) - if test "x$TOTAL_AMOUNT" != "xTESTKUDOS:0" + if [ "$TOTAL_AMOUNT" != "TESTKUDOS:0" ] then exit_fail "Reported total wired amount plus wrong: $TOTAL_AMOUNT" fi - echo PASS + echo "PASS" post_audit @@ -1455,27 +1460,27 @@ function test_17() { OLD_DATE=$(echo "SELECT preparationDate FROM PaymentInitiations WHERE id='${OLD_ID}';" | psql "${DB}") # Note: need - interval '1h' as "NOW()" may otherwise be exactly what is already in the DB # (due to rounding, if this machine is fast...) - NOW_1HR=$(expr $(date +%s) - 3600) + NOW_1HR=$(( $(date +%s) - 3600)) echo "UPDATE PaymentInitiations SET preparationDate='$NOW_1HR' WHERE id='${OLD_PREP}';" \ | psql "${DB}" launch_libeufin - echo DONE + echo "DONE" audit_only post_audit echo -n "Testing inconsistency detection... " TABLE=$(jq -r .row_minor_inconsistencies[0].table < test-audit-wire.json) - if test "x$TABLE" != "xwire_out" + if [ "$TABLE" != "wire_out" ] then exit_fail "Reported table wrong: $TABLE" fi DIAG=$(jq -r .row_minor_inconsistencies[0].diagnostic < test-audit-wire.json) DIAG=$(echo "$DIAG" | awk '{print $1 " " $2 " " $3}') - if test "x$DIAG" != "xexecution date mismatch" + if [ "$DIAG" != "execution date mismatch" ] then exit_fail "Reported diagnostic wrong: $DIAG" fi - echo PASS + echo "PASS" # cannot easily undo aggregator, hence full reload full_reload @@ -1488,34 +1493,42 @@ function test_17() { function test_18() { echo "===========18: emergency=================" - echo "DELETE FROM exchange.reserves_out;" | psql -Aqt "$DB" + echo "DELETE FROM exchange.reserves_out;" \ + | psql -Aqt "$DB" run_audit echo -n "Testing emergency detection... " - - jq -e .reserve_balance_summary_wrong_inconsistencies[0] < test-audit-reserves.json > /dev/null || exit_fail "Reserve balance inconsistency not detected" - - jq -e .emergencies[0] < test-audit-coins.json > /dev/null || exit_fail "Emergency not detected" - jq -e .emergencies_by_count[0] < test-audit-coins.json > /dev/null || exit_fail "Emergency by count not detected" - jq -e .amount_arithmetic_inconsistencies[0] < test-audit-coins.json > /dev/null || exit_fail "Escrow balance calculation impossibility not detected" - - echo PASS + jq -e .reserve_balance_summary_wrong_inconsistencies[0] \ + < test-audit-reserves.json \ + > /dev/null \ + || exit_fail "Reserve balance inconsistency not detected" + jq -e .emergencies[0] \ + < test-audit-coins.json \ + > /dev/null \ + || exit_fail "Emergency not detected" + jq -e .emergencies_by_count[0] \ + < test-audit-coins.json \ + > /dev/null \ + || exit_fail "Emergency by count not detected" + jq -e .amount_arithmetic_inconsistencies[0] \ + < test-audit-coins.json \ + > /dev/null \ + || exit_fail "Escrow balance calculation impossibility not detected" + echo "PASS" echo -n "Testing loss calculation... " - AMOUNT=$(jq -r .emergencies_loss < test-audit-coins.json) - if test "x$AMOUNT" == "xTESTKUDOS:0" + if [ "$AMOUNT" == "TESTKUDOS:0" ] then exit_fail "Reported amount wrong: $AMOUNT" fi AMOUNT=$(jq -r .emergencies_loss_by_count < test-audit-coins.json) - if test "x$AMOUNT" == "xTESTKUDOS:0" + if [ "$AMOUNT" == "TESTKUDOS:0" ] then exit_fail "Reported amount wrong: $AMOUNT" fi - - echo PASS + echo "PASS" # cannot easily undo broad DELETE operation, hence full reload full_reload @@ -1532,11 +1545,13 @@ function test_19() { RES_PUB=$(echo "SELECT reserve_pub FROM exchange.reserves_in WHERE reserve_in_serial_id=1;" | psql "$DB" -Aqt) OLD_EXP=$(echo "SELECT expiration_date FROM exchange.reserves WHERE reserve_pub='${RES_PUB}';" | psql "$DB" -Aqt) VAL_DELTA=1 - NEW_TIME=$(expr $OLD_TIME - 3024000000000 || true) # 5 weeks - NEW_EXP=$(expr $OLD_EXP - 3024000000000 || true) # 5 weeks - NEW_CREDIT=$(expr $OLD_VAL + $VAL_DELTA || true) - echo "UPDATE exchange.reserves_in SET execution_date='${NEW_TIME}',credit_val=${NEW_CREDIT} WHERE reserve_in_serial_id=1;" | psql -Aqt "$DB" - echo "UPDATE exchange.reserves SET current_balance_val=${VAL_DELTA}+current_balance_val,expiration_date='${NEW_EXP}' WHERE reserve_pub='${RES_PUB}';" | psql -Aqt "$DB" + NEW_TIME=$(( OLD_TIME - 3024000000000)) # 5 weeks + NEW_EXP=$(( OLD_EXP - 3024000000000)) # 5 weeks + NEW_CREDIT=$(( OLD_VAL + VAL_DELTA)) + echo "UPDATE exchange.reserves_in SET execution_date='${NEW_TIME}',credit_val=${NEW_CREDIT} WHERE reserve_in_serial_id=1;" \ + | psql -Aqt "$DB" + echo "UPDATE exchange.reserves SET current_balance_val=${VAL_DELTA}+current_balance_val,expiration_date='${NEW_EXP}' WHERE reserve_pub='${RES_PUB}';" \ + | psql -Aqt "$DB" # Need to run with the aggregator so the reserve closure happens run_audit aggregator @@ -1564,27 +1579,34 @@ function test_20() { OLD_TIME=$(echo "SELECT execution_date FROM exchange.reserves_in WHERE reserve_in_serial_id=1;" | psql "$DB" -Aqt) OLD_VAL=$(echo "SELECT credit_val FROM exchange.reserves_in WHERE reserve_in_serial_id=1;" | psql "$DB" -Aqt) RES_PUB=$(echo "SELECT reserve_pub FROM exchange.reserves_in WHERE reserve_in_serial_id=1;" | psql "$DB" -Aqt) - NEW_TIME=$(expr $OLD_TIME - 3024000000000 || true) # 5 weeks - NEW_CREDIT=$(expr $OLD_VAL + 100 || true) - echo "UPDATE exchange.reserves_in SET execution_date='${NEW_TIME}',credit_val=${NEW_CREDIT} WHERE reserve_in_serial_id=1;" | psql -Aqt "$DB" - echo "UPDATE exchange.reserves SET current_balance_val=100+current_balance_val WHERE reserve_pub='${RES_PUB}';" | psql -Aqt "$DB" + NEW_TIME=$(( OLD_TIME - 3024000000000 )) # 5 weeks + NEW_CREDIT=$(( OLD_VAL + 100 )) + echo "UPDATE exchange.reserves_in SET execution_date='${NEW_TIME}',credit_val=${NEW_CREDIT} WHERE reserve_in_serial_id=1;" \ + | psql -Aqt "$DB" + echo "UPDATE exchange.reserves SET current_balance_val=100+current_balance_val WHERE reserve_pub='${RES_PUB}';" \ + | psql -Aqt "$DB" # This time, run without the aggregator so the reserve closure is skipped! run_audit echo -n "Testing reserve closure missing detected... " - jq -e .reserve_not_closed_inconsistencies[0] < test-audit-reserves.json > /dev/null || exit_fail "Reserve not closed inconsistency not detected" + jq -e .reserve_not_closed_inconsistencies[0] \ + < test-audit-reserves.json \ + > /dev/null \ + || exit_fail "Reserve not closed inconsistency not detected" echo "PASS" AMOUNT=$(jq -r .total_balance_reserve_not_closed < test-audit-reserves.json) - if test "x$AMOUNT" == "xTESTKUDOS:0" + if [ "$AMOUNT" == "TESTKUDOS:0" ] then exit_fail "Reported total amount wrong: $AMOUNT" fi # Undo - echo "UPDATE exchange.reserves_in SET execution_date='${OLD_TIME}',credit_val=${OLD_VAL} WHERE reserve_in_serial_id=1;" | psql -Aqt "$DB" - echo "UPDATE exchange.reserves SET current_balance_val=current_balance_val-100 WHERE reserve_pub='${RES_PUB}';" | psql -Aqt "$DB" + echo "UPDATE exchange.reserves_in SET execution_date='${OLD_TIME}',credit_val=${OLD_VAL} WHERE reserve_in_serial_id=1;" \ + | psql -Aqt "$DB" + echo "UPDATE exchange.reserves SET current_balance_val=current_balance_val-100 WHERE reserve_pub='${RES_PUB}';" \ + | psql -Aqt "$DB" } @@ -1598,33 +1620,39 @@ function test_21() { RES_PUB=$(echo "SELECT reserve_pub FROM exchange.reserves_in WHERE reserve_in_serial_id=1;" | psql "$DB" -Aqt) OLD_EXP=$(echo "SELECT expiration_date FROM exchange.reserves WHERE reserve_pub='${RES_PUB}';" | psql "$DB" -Aqt) VAL_DELTA=1 - NEW_TIME=$(expr $OLD_TIME - 3024000000000 || true) # 5 weeks - NEW_EXP=$(expr $OLD_EXP - 3024000000000 || true) # 5 weeks - NEW_CREDIT=$(expr $OLD_VAL + $VAL_DELTA || true) - echo "UPDATE exchange.reserves_in SET execution_date='${NEW_TIME}',credit_val=${NEW_CREDIT} WHERE reserve_in_serial_id=1;" | psql -Aqt "$DB" - echo "UPDATE exchange.reserves SET current_balance_val=${VAL_DELTA}+current_balance_val,expiration_date='${NEW_EXP}' WHERE reserve_pub='${RES_PUB}';" | psql -Aqt "$DB" + NEW_TIME=$(( OLD_TIME - 3024000000000 )) # 5 weeks + NEW_EXP=$(( OLD_EXP - 3024000000000 )) # 5 weeks + NEW_CREDIT=$(( OLD_VAL + VAL_DELTA )) + echo "UPDATE exchange.reserves_in SET execution_date='${NEW_TIME}',credit_val=${NEW_CREDIT} WHERE reserve_in_serial_id=1;" \ + | psql -Aqt "$DB" + echo "UPDATE exchange.reserves SET current_balance_val=${VAL_DELTA}+current_balance_val,expiration_date='${NEW_EXP}' WHERE reserve_pub='${RES_PUB}';" \ + | psql -Aqt "$DB" # Need to first run the aggregator so the transfer is marked as done exists pre_audit aggregator stop_libeufin # remove transaction from bank DB # Currently emulating this (to be deleted): - echo "DELETE FROM TalerRequestedPayments WHERE amount='TESTKUDOS:${VAL_DELTA}'" | psql "${DB}" + echo "DELETE FROM TalerRequestedPayments WHERE amount='TESTKUDOS:${VAL_DELTA}'" \ + | psql "${DB}" launch_libeufin audit_only post_audit echo -n "Testing lack of reserve closure transaction detected... " - jq -e .reserve_lag_details[0] < test-audit-wire.json > /dev/null || exit_fail "Reserve closure lag not detected" + jq -e .reserve_lag_details[0] \ + < test-audit-wire.json \ + > /dev/null \ + || exit_fail "Reserve closure lag not detected" AMOUNT=$(jq -r .reserve_lag_details[0].amount < test-audit-wire.json) - if test "x$AMOUNT" != "xTESTKUDOS:${VAL_DELTA}" + if [ "$AMOUNT" != "TESTKUDOS:${VAL_DELTA}" ] then exit_fail "Reported total amount wrong: $AMOUNT" fi AMOUNT=$(jq -r .total_closure_amount_lag < test-audit-wire.json) - if test "x$AMOUNT" != "xTESTKUDOS:${VAL_DELTA}" + if [ "$AMOUNT" != "TESTKUDOS:${VAL_DELTA}" ] then exit_fail "Reported total amount wrong: $AMOUNT" fi @@ -1655,7 +1683,7 @@ function test_22() { echo -n "Testing inconsistency detection... " jq -e .denomination_key_validity_withdraw_inconsistencies[0] < test-audit-reserves.json > /dev/null || exit_fail "Denomination key withdraw inconsistency for $S_DENOM not detected" - echo PASS + echo "PASS" # Undo modification echo "UPDATE exchange.denominations SET expire_withdraw=${OLD_WEXP} WHERE denominations_serial='${S_DENOM}';" | psql -Aqt "$DB" @@ -1672,15 +1700,19 @@ function test_23() { pre_audit aggregator OLD_AMOUNT=$(echo "SELECT amount_frac FROM exchange.wire_out WHERE wireout_uuid=1;" | psql "$DB" -Aqt) - NEW_AMOUNT=$(expr $OLD_AMOUNT - 1000000 || true) - echo "UPDATE exchange.wire_out SET amount_frac=${NEW_AMOUNT} WHERE wireout_uuid=1;" | psql -Aqt "$DB" + NEW_AMOUNT=$(( OLD_AMOUNT - 1000000 )) + echo "UPDATE exchange.wire_out SET amount_frac=${NEW_AMOUNT} WHERE wireout_uuid=1;" \ + | psql -Aqt "$DB" audit_only post_audit echo -n "Testing inconsistency detection... " - jq -e .wire_out_inconsistencies[0] < test-audit-aggregation.json > /dev/null || exit_fail "Wire out inconsistency not detected" + jq -e .wire_out_inconsistencies[0] \ + < test-audit-aggregation.json \ + > /dev/null \ + || exit_fail "Wire out inconsistency not detected" ROW=$(jq .wire_out_inconsistencies[0].rowid < test-audit-aggregation.json) if [ "$ROW" != 1 ] @@ -1688,19 +1720,19 @@ function test_23() { exit_fail "Row wrong" fi AMOUNT=$(jq -r .total_wire_out_delta_plus < test-audit-aggregation.json) - if test "x$AMOUNT" != "xTESTKUDOS:0" + if [ "$AMOUNT" != "TESTKUDOS:0" ] then exit_fail "Reported amount wrong: $AMOUNT" fi AMOUNT=$(jq -r .total_wire_out_delta_minus < test-audit-aggregation.json) - if test "x$AMOUNT" != "xTESTKUDOS:0.01" + if [ "$AMOUNT" != "TESTKUDOS:0.01" ] then exit_fail "Reported total amount wrong: $AMOUNT" fi - echo PASS + echo "PASS" echo "Second pass: changing how amount is wrong to other direction" - NEW_AMOUNT=$(expr $OLD_AMOUNT + 1000000 || true) + NEW_AMOUNT=$(( OLD_AMOUNT + 1000000 )) echo "UPDATE exchange.wire_out SET amount_frac=${NEW_AMOUNT} WHERE wireout_uuid=1;" | psql -Aqt "$DB" pre_audit @@ -1717,16 +1749,16 @@ function test_23() { exit_fail "Row wrong" fi AMOUNT=$(jq -r .total_wire_out_delta_minus < test-audit-aggregation.json) - if test "x$AMOUNT" != "xTESTKUDOS:0" + if [ "$AMOUNT" != "TESTKUDOS:0" ] then exit_fail "Reported amount wrong: $AMOUNT" fi AMOUNT=$(jq -r .total_wire_out_delta_plus < test-audit-aggregation.json) - if test "x$AMOUNT" != "xTESTKUDOS:0.01" + if [ "$AMOUNT" != "TESTKUDOS:0.01" ] then exit_fail "Reported total amount wrong: $AMOUNT" fi - echo PASS + echo "PASS" # cannot easily undo aggregator, hence full reload full_reload @@ -1740,31 +1772,35 @@ function test_24() { echo "===========24: deposits missing ===========" # Modify denom_sig, so it is wrong CNT=$(echo "SELECT COUNT(*) FROM auditor.deposit_confirmations;" | psql -Aqt "$DB") - if test x$CNT = x0 + if [ "$CNT" = "0" ] then echo "Skipping deposits missing test: no deposit confirmations in database!" else echo "DELETE FROM exchange.deposits;" | psql -Aqt "$DB" - echo "DELETE FROM exchange.deposits WHERE deposit_serial_id=1;" | psql -Aqt "$DB" + echo "DELETE FROM exchange.deposits WHERE deposit_serial_id=1;" \ + | psql -Aqt "$DB" run_audit echo -n "Testing inconsistency detection... " - jq -e .deposit_confirmation_inconsistencies[0] < test-audit-deposits.json > /dev/null || exit_fail "Deposit confirmation inconsistency NOT detected" + jq -e .deposit_confirmation_inconsistencies[0] \ + < test-audit-deposits.json \ + > /dev/null \ + || exit_fail "Deposit confirmation inconsistency NOT detected" AMOUNT=$(jq -er .missing_deposit_confirmation_total < test-audit-deposits.json) - if test x$AMOUNT = xTESTKUDOS:0 + if [ "$AMOUNT" = "TESTKUDOS:0" ] then exit_fail "Expected non-zero total missing deposit confirmation amount" fi COUNT=$(jq -er .missing_deposit_confirmation_count < test-audit-deposits.json) - if test x$AMOUNT = x0 + if [ "$AMOUNT" = "0" ] then exit_fail "Expected non-zero total missing deposit confirmation count" fi - echo PASS + echo "PASS" # cannot easily undo DELETE, hence full reload full_reload @@ -1778,31 +1814,38 @@ function test_25() { echo "=========25: inconsistent coin history=========" # Drop refund, so coin history is bogus. - echo "DELETE FROM exchange.refunds WHERE refund_serial_id=1;" | psql -Aqt "$DB" + echo "DELETE FROM exchange.refunds WHERE refund_serial_id=1;" \ + | psql -Aqt "$DB" run_audit aggregator echo -n "Testing inconsistency detection... " - jq -e .coin_inconsistencies[0] < test-audit-aggregation.json > /dev/null || exit_fail "Coin inconsistency NOT detected" + jq -e .coin_inconsistencies[0] \ + < test-audit-aggregation.json \ + > /dev/null \ + || exit_fail "Coin inconsistency NOT detected" # Note: if the wallet withdrew much more than it spent, this might indeed # go legitimately unnoticed. - jq -e .emergencies[0] < test-audit-coins.json > /dev/null || exit_fail "Denomination value emergency NOT reported" + jq -e .emergencies[0] \ + < test-audit-coins.json \ + > /dev/null \ + || exit_fail "Denomination value emergency NOT reported" AMOUNT=$(jq -er .total_coin_delta_minus < test-audit-aggregation.json) - if test x$AMOUNT = xTESTKUDOS:0 + if [ "$AMOUNT" = "TESTKUDOS:0" ] then exit_fail "Expected non-zero total inconsistency amount from coins" fi # Note: if the wallet withdrew much more than it spent, this might indeed # go legitimately unnoticed. COUNT=$(jq -er .emergencies_risk_by_amount < test-audit-coins.json) - if test x$AMOUNT = xTESTKUDOS:0 + if [ "$COUNT" = "TESTKUDOS:0" ] then exit_fail "Expected non-zero emergency-by-amount" fi - echo PASS + echo "PASS" # cannot easily undo DELETE, hence full reload full_reload @@ -1829,7 +1872,7 @@ function test_26() { jq -e .bad_sig_losses[0] < test-audit-coins.json > /dev/null || exit_fail "Bad signature not detected" ROW=$(jq -e .bad_sig_losses[0].row < test-audit-coins.json) - if [ "$ROW" != ${SERIAL} ] + if [ "$ROW" != "${SERIAL}" ] then exit_fail "Row wrong, got $ROW" fi @@ -1866,11 +1909,10 @@ function test_27() { stop_libeufin # Obtain data to duplicate. WTID=$(echo SELECT wtid FROM TalerRequestedPayments WHERE id=1 | psql "${DB}") - echo WTID=$WTID OTHER_IBAN=$(echo -e "SELECT iban FROM BankAccounts WHERE label='fortytwo'" | psql "${DB}") # 'rawConfirmation' is set to 2 here, that doesn't # point to any record. That's only needed to set a non null value. - echo -e "INSERT INTO PaymentInitiations (bankAccount,preparationDate,submissionDate,sum,currency,endToEndId,paymentInformationId,instructionId,subject,creditorIban,creditorBic,creditorName,submitted,messageId,rawConfirmation) VALUES (1,$(date +%s),$(expr $(date +%s) + 2),10,'TESTKUDOS','NOTGIVEN','unused','unused','$WTID http://exchange.example.com/','$OTHER_IBAN','SANDBOXX','Forty Two','unused',1,2)" \ + echo -e "INSERT INTO PaymentInitiations (bankAccount,preparationDate,submissionDate,sum,currency,endToEndId,paymentInformationId,instructionId,subject,creditorIban,creditorBic,creditorName,submitted,messageId,rawConfirmation) VALUES (1,$(date +%s),$(( $(date +%s) + 2)),10,'TESTKUDOS','NOTGIVEN','unused','unused','$WTID http://exchange.example.com/','$OTHER_IBAN','SANDBOXX','Forty Two','unused',1,2)" \ | psql "${DB}" echo -e "INSERT INTO TalerRequestedPayments (facade,payment,requestUid,amount,exchangeBaseUrl,wtid,creditAccount) VALUES (1,2,'unused','TESTKUDOS:1','http://exchange.example.com/','$WTID','payto://iban/SANDBOXX/$OTHER_IBAN?receiver-name=Forty+Two')" \ | psql "${DB}" @@ -1881,13 +1923,13 @@ function test_27() { echo -n "Testing inconsistency detection... " AMOUNT=$(jq -r .wire_format_inconsistencies[0].amount < test-audit-wire.json) - if test "${AMOUNT}" != "TESTKUDOS:1" + if [ "${AMOUNT}" != "TESTKUDOS:1" ] then exit_fail "Amount wrong, got ${AMOUNT}" fi AMOUNT=$(jq -r .total_wire_format_amount < test-audit-wire.json) - if test "${AMOUNT}" != "TESTKUDOS:1" + if [ "${AMOUNT}" != "TESTKUDOS:1" ] then exit_fail "Wrong total wire format amount, got $AMOUNT" fi @@ -1926,7 +1968,7 @@ function test_28() { exit_fail "Wrong operation, got $OP" fi TAB=$(jq -r .row_inconsistencies[0].table < test-audit-aggregation.json) - if test $TAB != "deposit" + if [ "$TAB" != "deposit" ] then exit_fail "Wrong table for row inconsistency, got $TAB" fi @@ -1955,13 +1997,13 @@ function test_29() { echo -n "Testing inconsistency detection... " AMOUNT=$(jq -r .total_balance_summary_delta_minus < test-audit-reserves.json) - if test "x$AMOUNT" == "xTESTKUDOS:0" + if [ "$AMOUNT" == "TESTKUDOS:0" ] then exit_fail "Reported total amount wrong: $AMOUNT" fi PROFIT=$(jq -r .amount_arithmetic_inconsistencies[0].profitable < test-audit-coins.json) - if test "x$PROFIT" != "x-1" + if [ "$PROFIT" != "-1" ] then exit_fail "Reported wrong profitability: $PROFIT" fi @@ -1982,13 +2024,13 @@ function test_30() { run_audit echo -n "Testing inconsistency detection... " AMOUNT=$(jq -r .bad_sig_losses[0].loss < test-audit-coins.json) - if test "x$AMOUNT" == "xTESTKUDOS:0" + if [ "$AMOUNT" == "TESTKUDOS:0" ] then exit_fail "Reported total amount wrong: $AMOUNT" fi PROFIT=$(jq -r .amount_arithmetic_inconsistencies[0].profitable < test-audit-coins.json) - if test "x$PROFIT" != "x-1" + if [ "$PROFIT" != "-1" ] then exit_fail "Reported profitability wrong: $PROFIT" fi @@ -2012,13 +2054,13 @@ function test_31() { run_audit aggregator echo -n "Testing inconsistency detection... " AMOUNT=$(jq -r .irregular_loss < test-audit-coins.json) - if test "x$AMOUNT" == "xTESTKUDOS:0" + if [ "$AMOUNT" == "TESTKUDOS:0" ] then exit_fail "Reported total amount wrong: $AMOUNT" fi OP=$(jq -r --arg dep "deposit" '.bad_sig_losses[] | select(.operation == $dep) | .operation'< test-audit-coins.json | head -n1) - if test "x$OP" != "xdeposit" + if [ "$OP" != "deposit" ] then exit_fail "Reported wrong operation: $OP" fi @@ -2047,13 +2089,13 @@ function test_32() { echo -n "Testing inconsistency detection... " AMOUNT=$(jq -r .total_bad_sig_loss < test-audit-aggregation.json) - if test "x$AMOUNT" == "xTESTKUDOS:0" + if [ "$AMOUNT" == "TESTKUDOS:0" ] then exit_fail "Reported total amount wrong: $AMOUNT" fi OP=$(jq -r .bad_sig_losses[0].operation < test-audit-aggregation.json) - if test "x$OP" != "xwire" + if [ "$OP" != "wire" ] then exit_fail "Reported wrong operation: $OP" fi @@ -2172,19 +2214,31 @@ function test_33() { fi DRAINED=$(jq -r .total_drained < test-audit-wire.json) - if test $DRAINED != "TESTKUDOS:0.1" + if [ "$DRAINED" != "TESTKUDOS:0.1" ] then exit_fail "Wrong amount drained, got unexpected drain of $DRAINED" fi - jq -e .amount_arithmetic_inconsistencies[0] < test-audit-aggregation.json > /dev/null && exit_fail "Unexpected arithmetic inconsistencies from aggregations detected in ordinary run" - jq -e .amount_arithmetic_inconsistencies[0] < test-audit-coins.json > /dev/null && exit_fail "Unexpected arithmetic inconsistencies from coins detected in ordinary run" - jq -e .amount_arithmetic_inconsistencies[0] < test-audit-reserves.json > /dev/null && exit_fail "Unexpected arithmetic inconsistencies from reserves detected in ordinary run" - echo PASS + jq -e .amount_arithmetic_inconsistencies[0] \ + < test-audit-aggregation.json \ + > /dev/null \ + && exit_fail "Unexpected arithmetic inconsistencies from aggregations detected in ordinary run" + jq -e .amount_arithmetic_inconsistencies[0] \ + < test-audit-coins.json \ + > /dev/null \ + && exit_fail "Unexpected arithmetic inconsistencies from coins detected in ordinary run" + jq -e .amount_arithmetic_inconsistencies[0] \ + < test-audit-reserves.json \ + > /dev/null \ + && exit_fail "Unexpected arithmetic inconsistencies from reserves detected in ordinary run" + echo "PASS" echo -n "Checking for unexpected wire out differences " - jq -e .wire_out_inconsistencies[0] < test-audit-aggregation.json > /dev/null && exit_fail "Unexpected wire out inconsistencies detected in ordinary run" - echo PASS + jq -e .wire_out_inconsistencies[0] \ + < test-audit-aggregation.json \ + > /dev/null \ + && exit_fail "Unexpected wire out inconsistencies detected in ordinary run" + echo "PASS" # cannot easily undo aggregator, hence full reload full_reload @@ -2202,11 +2256,16 @@ function check_with_database() BASEDB="$1" CONF="$1.conf" ORIGIN=$(pwd) - MY_TMP_DIR=$(dirname $1) + MY_TMP_DIR=$(dirname "$1") echo "Running test suite with database $BASEDB using configuration $CONF" MASTER_PRIV_FILE="${BASEDB}.mpriv" - taler-config -f -c "${CONF}" -s exchange-offline -o MASTER_PRIV_FILE -V ${MASTER_PRIV_FILE} - MASTER_PUB=$(gnunet-ecc -p $MASTER_PRIV_FILE) + taler-config \ + -f \ + -c "${CONF}" \ + -s exchange-offline \ + -o MASTER_PRIV_FILE \ + -V "${MASTER_PRIV_FILE}" + MASTER_PUB=$(gnunet-ecc -p "$MASTER_PRIV_FILE") echo "MASTER PUB is ${MASTER_PUB} using file ${MASTER_PRIV_FILE}" @@ -2217,7 +2276,7 @@ function check_with_database() fail=0 for i in $TESTS do - test_$i + "test_$i" if test 0 != $fail then break @@ -2253,24 +2312,30 @@ taler-wallet-cli -h >/dev/null /dev/null || exit_skip "taler-wallet echo -n "Testing for Postgres" # Available directly in path? INITDB_BIN=$(command -v initdb) || true -if [[ ! -z "$INITDB_BIN" ]]; then - echo " FOUND (in path) at" $INITDB_BIN +if [[ -n "$INITDB_BIN" ]]; then + echo " FOUND (in path) at $INITDB_BIN" else - HAVE_INITDB=$(find /usr -name "initdb" | head -1 2> /dev/null | grep postgres) || exit_skip " MISSING" - echo " FOUND at" $(dirname $HAVE_INITDB) - INITDB_BIN=$(echo $HAVE_INITDB | grep bin/initdb | grep postgres | sort -n | tail -n1) + HAVE_INITDB=$(find /usr -name "initdb" | head -1 2> /dev/null | grep postgres) \ + || exit_skip " MISSING" + echo " FOUND at $(dirname "$HAVE_INITDB")" + INITDB_BIN=$(echo "$HAVE_INITDB" | grep bin/initdb | grep postgres | sort -n | tail -n1) fi -POSTGRES_PATH=$(dirname $INITDB_BIN) +POSTGRES_PATH=$(dirname "$INITDB_BIN") MYDIR=$(mktemp -d /tmp/taler-auditor-basedbXXXXXX) echo "Using $MYDIR for logging and temporary data" TMPDIR="$MYDIR/postgres/" -mkdir -p $TMPDIR +mkdir -p "$TMPDIR" echo -n "Setting up Postgres DB at $TMPDIR ..." -$INITDB_BIN --no-sync --auth=trust -D ${TMPDIR} > ${MYDIR}/postgres-dbinit.log 2> ${MYDIR}/postgres-dbinit.err +$INITDB_BIN \ + --no-sync \ + --auth=trust \ + -D "${TMPDIR}" \ + > "${MYDIR}/postgres-dbinit.log" \ + 2> "${MYDIR}/postgres-dbinit.err" echo "DONE" -mkdir ${TMPDIR}/sockets +mkdir "${TMPDIR}/sockets" echo -n "Launching Postgres service" -cat - >> $TMPDIR/postgresql.conf <> "$TMPDIR/postgresql.conf" < $TMPDIR/pg_hba.conf.new -mv $TMPDIR/pg_hba.conf.new $TMPDIR/pg_hba.conf -${POSTGRES_PATH}/pg_ctl -D $TMPDIR -l /dev/null start > ${MYDIR}/postgres-start.log 2> ${MYDIR}/postgres-start.err +grep -v host \ + < "$TMPDIR/pg_hba.conf" \ + > "$TMPDIR/pg_hba.conf.new" +mv "$TMPDIR/pg_hba.conf.new" "$TMPDIR/pg_hba.conf" +"${POSTGRES_PATH}/pg_ctl" \ + -D "$TMPDIR" \ + -l /dev/null \ + start \ + > "${MYDIR}/postgres-start.log" \ + 2> "${MYDIR}/postgres-start.err" echo " DONE" PGHOST="$TMPDIR/sockets" export PGHOST @@ -2301,12 +2373,12 @@ echo "Generating fresh database at $MYDIR" if faketime -f '-1 d' ./generate-auditor-basedb.sh "$MYDIR/$DB" then check_with_database "$MYDIR/$DB" - if test x$fail != x0 + if [ "$fail" != "0" ] then - exit $fail + exit "$fail" else echo "Cleaning up $MYDIR..." - rm -rf $MYDIR || echo "Removing $MYDIR failed" + rm -rf "$MYDIR" || echo "Removing $MYDIR failed" fi else echo "Generation failed" From f7fad83a82fcc02fbf3e61965cbef5fd8de36a3e Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Tue, 18 Jul 2023 00:36:48 +0200 Subject: [PATCH 3/3] -work on auditor test --- src/auditor/generate-auditor-basedb.sh | 2 +- src/auditor/test-auditor.sh | 100 +++++++++++-------------- 2 files changed, 44 insertions(+), 58 deletions(-) diff --git a/src/auditor/generate-auditor-basedb.sh b/src/auditor/generate-auditor-basedb.sh index 3dae047c5..6399d94b6 100755 --- a/src/auditor/generate-auditor-basedb.sh +++ b/src/auditor/generate-auditor-basedb.sh @@ -76,7 +76,7 @@ mkdir -p "$(dirname "$BASEDB")" echo "Dumping database ${BASEDB}.sql" pg_dump -O "auditor-basedb" | sed -e '/AS integer/d' > "${BASEDB}.sql" cp "${CONF}.edited" "${BASEDB}.conf" -cp "$(taler-config -s exchange-offline -o MASTER_PRIV_FILE -f)" "${BASEDB}.mpriv" +cp "$(taler-config -c "${CONF}.edited" -s exchange-offline -o MASTER_PRIV_FILE -f)" "${BASEDB}.mpriv" # clean up echo -n "Final clean up ..." diff --git a/src/auditor/test-auditor.sh b/src/auditor/test-auditor.sh index 99f744a6d..6d46d8fd7 100755 --- a/src/auditor/test-auditor.sh +++ b/src/auditor/test-auditor.sh @@ -58,24 +58,24 @@ LIBEUFIN_SETTLE_TIME=1 # Stop libeufin sandbox and nexus (if running) function stop_libeufin() { - echo "Stopping libeufin..." - if test -f ${MYDIR:-/}/libeufin-sandbox.pid + echo -n "Stopping libeufin... " + if test -f ${MY_TMP_DIR:-/}/libeufin-sandbox.pid then - PID=$(cat ${MYDIR}/libeufin-sandbox.pid 2> /dev/null) + PID=$(cat ${MY_TMP_DIR}/libeufin-sandbox.pid 2> /dev/null) echo "Killing libeufin sandbox $PID" - rm "${MYDIR}/libeufin-sandbox.pid" + rm "${MY_TMP_DIR}/libeufin-sandbox.pid" kill "$PID" 2> /dev/null || true wait "$PID" || true fi - if test -f ${MYDIR:-/}/libeufin-nexus.pid + if test -f ${MY_TMP_DIR:-/}/libeufin-nexus.pid then - PID=$(cat ${MYDIR}/libeufin-nexus.pid 2> /dev/null) + PID=$(cat ${MY_TMP_DIR}/libeufin-nexus.pid 2> /dev/null) echo "Killing libeufin nexus $PID" - rm "${MYDIR}/libeufin-nexus.pid" + rm "${MY_TMP_DIR}/libeufin-nexus.pid" kill "$PID" 2> /dev/null || true wait "$PID" || true fi - echo "Stopping libeufin DONE" + echo "DONE" } # Cleanup exchange and libeufin between runs. @@ -119,20 +119,19 @@ function exit_cleanup() trap exit_cleanup EXIT function launch_libeufin () { - cd "$MYDIR" # shellcheck disable=SC2016 - export LIBEUFIN_SANDBOX_DB_CONNECTION='jdbc:postgresql://localhost/auditor-basedb?socketFactory=org.newsclub.net.unix.AFUNIXSocketFactory$FactoryArg&socketFactoryArg=/var/run/postgresql/.s.PGSQL.5432' + export LIBEUFIN_SANDBOX_DB_CONNECTION='jdbc:postgresql://localhost/'"${DB}"'?socketFactory=org.newsclub.net.unix.AFUNIXSocketFactory$FactoryArg&socketFactoryArg='"$SOCKETDIR"'/.s.PGSQL.5432' + export MY_TMP_DIR libeufin-sandbox serve --no-auth --port 18082 \ - > "${MYDIR}/libeufin-sandbox-stdout.log" \ - 2> "${MYDIR}/libeufin-sandbox-stderr.log" & - echo $! > "${MYDIR}/libeufin-sandbox.pid" + > "${MY_TMP_DIR}/libeufin-sandbox-stdout.log" \ + 2> "${MY_TMP_DIR}/libeufin-sandbox-stderr.log" & + echo $! > "${MY_TMP_DIR}/libeufin-sandbox.pid" # shellcheck disable=SC2016 - export LIBEUFIN_NEXUS_DB_CONNECTION='jdbc:postgresql://localhost/auditor-basedb?socketFactory=org.newsclub.net.unix.AFUNIXSocketFactory$FactoryArg&socketFactoryArg=/var/run/postgresql/.s.PGSQL.5432' + export LIBEUFIN_NEXUS_DB_CONNECTION='jdbc:postgresql://localhost/'"${DB}"'?socketFactory=org.newsclub.net.unix.AFUNIXSocketFactory$FactoryArg&socketFactoryArg='"$SOCKETDIR"'/.s.PGSQL.5432' libeufin-nexus serve --port 8082 \ - 2> "${MYDIR}/libeufin-nexus-stderr.log" \ - > "${MYDIR}/libeufin-nexus-stdout.log" & - echo $! > "${MYDIR}/libeufin-nexus.pid" - cd "$ORIGIN" + 2> "${MY_TMP_DIR}/libeufin-nexus-stderr.log" \ + > "${MY_TMP_DIR}/libeufin-nexus-stdout.log" & + echo $! > "${MY_TMP_DIR}/libeufin-nexus.pid" } # Downloads new transactions from the bank. @@ -140,12 +139,11 @@ function nexus_fetch_transactions () { export LIBEUFIN_NEXUS_USERNAME="exchange" export LIBEUFIN_NEXUS_PASSWORD="x" export LIBEUFIN_NEXUS_URL="http://localhost:8082/" - cd "$MY_TMP_DIR" - libeufin-cli accounts fetch-transactions \ + libeufin-cli accounts \ + fetch-transactions \ --range-type since-last \ --level report \ exchange-nexus > /dev/null - cd "$ORIGIN" unset LIBEUFIN_NEXUS_USERNAME unset LIBEUFIN_NEXUS_PASSWORD unset LIBEUFIN_NEXUS_URL @@ -158,10 +156,9 @@ function nexus_submit_to_sandbox () { export LIBEUFIN_NEXUS_USERNAME="exchange" export LIBEUFIN_NEXUS_PASSWORD="x" export LIBEUFIN_NEXUS_URL="http://localhost:8082/" - cd "$MY_TMP_DIR" - libeufin-cli accounts submit-payments\ + libeufin-cli accounts \ + submit-payments\ exchange-nexus - cd "$ORIGIN" unset LIBEUFIN_NEXUS_USERNAME unset LIBEUFIN_NEXUS_PASSWORD unset LIBEUFIN_NEXUS_URL @@ -212,7 +209,7 @@ function pre_audit () { echo -n "Running exchange aggregator ..." taler-exchange-aggregator \ -y \ - -L INFO \ + -L "INFO" \ -t \ -c "$CONF" \ 2> "${MY_TMP_DIR}/aggregator.log" \ @@ -220,7 +217,7 @@ function pre_audit () { echo " DONE" echo -n "Running exchange closer ..." taler-exchange-closer \ - -L INFO\ + -L "INFO" \ -t \ -c "$CONF" \ 2> "${MY_TMP_DIR}/closer.log" \ @@ -228,7 +225,7 @@ function pre_audit () { echo " DONE" echo -n "Running exchange transfer ..." taler-exchange-transfer \ - -L INFO \ + -L "INFO" \ -t \ -c "$CONF" \ 2> "${MY_TMP_DIR}/transfer.log" \ @@ -390,7 +387,7 @@ function run_audit () { taler-exchange-httpd \ -c "${CONF}" \ -L INFO \ - 2> "${MYDIR}/exchange-httpd-drain.err" & + 2> "${MY_TMP_DIR}/exchange-httpd-drain.err" & EPID=$! # Wait for all services to be available @@ -444,7 +441,6 @@ function run_audit () { export LIBEUFIN_NEXUS_USERNAME="exchange" export LIBEUFIN_NEXUS_PASSWORD="x" export LIBEUFIN_NEXUS_URL="http://localhost:8082/" - cd "$MY_TMP_DIR" PAIN_UUID=$(libeufin-cli accounts list-payments exchange-nexus | jq .initiatedPayments[] | jq 'select(.submitted==false)' | jq -r .paymentInitiationId) if test -z "${PAIN_UUID}" then @@ -467,7 +463,6 @@ function run_audit () { --level report \ exchange-nexus echo " DONE" - cd "$ORIGIN" fi audit_only post_audit @@ -477,7 +472,7 @@ function run_audit () { # Do a full reload of the (original) database function full_reload() { - echo "Doing full reload of the database ($BASEDB - $DB)... " + echo -n "Doing full reload of the database ($BASEDB - $DB)... " dropdb "$DB" 2> /dev/null || true createdb -T template0 "$DB" \ || exit_skip "could not create database $DB (at $PGHOST)" @@ -491,7 +486,6 @@ function full_reload() echo "DONE" # Technically, this call shouldn't be needed as libeufin should already be stopped here... stop_libeufin - echo "DONE" } @@ -1031,7 +1025,6 @@ function test_8() { echo "===========8: wire-transfer-subject disagreement===========" # Technically, this call shouldn't be needed, as libeufin should already be stopped here. stop_libeufin - cd "$MYDIR" OLD_ID=$(echo "SELECT id FROM NexusBankTransactions WHERE amount='10' AND currency='TESTKUDOS' ORDER BY id LIMIT 1;" | psql "${DB}") \ || exit_fail "Failed to SELECT FROM NexusBankTransactions nexus DB!" OLD_WTID=$(echo "SELECT reservePublicKey FROM TalerIncomingPayments WHERE payment='$OLD_ID';" \ @@ -1040,7 +1033,6 @@ function test_8() { echo "UPDATE TalerIncomingPayments SET reservePublicKey='$NEW_WTID' WHERE payment='$OLD_ID';" \ | psql "${DB}" \ || exit_fail "Failed to update TalerIncomingPayments" - cd "$ORIGIN" run_audit @@ -1097,10 +1089,8 @@ function test_8() { echo "PASS" # Undo database modification - cd "$MYDIR" echo "UPDATE TalerIncomingPayments SET reservePublicKey='$OLD_WTID' WHERE payment='$OLD_ID';" \ | psql "${DB}" - cd "$ORIGIN" } @@ -2256,7 +2246,6 @@ function check_with_database() BASEDB="$1" CONF="$1.conf" ORIGIN=$(pwd) - MY_TMP_DIR=$(dirname "$1") echo "Running test suite with database $BASEDB using configuration $CONF" MASTER_PRIV_FILE="${BASEDB}.mpriv" taler-config \ @@ -2292,7 +2281,7 @@ function check_with_database() # *************** Main logic starts here ************** # ####### Setup globals ###### -# Postgres database to use +# Postgres database to use (must match configuration file) export DB="auditor-basedb" # test required commands exist @@ -2321,19 +2310,21 @@ else INITDB_BIN=$(echo "$HAVE_INITDB" | grep bin/initdb | grep postgres | sort -n | tail -n1) fi POSTGRES_PATH=$(dirname "$INITDB_BIN") -MYDIR=$(mktemp -d /tmp/taler-auditor-basedbXXXXXX) -echo "Using $MYDIR for logging and temporary data" -TMPDIR="$MYDIR/postgres/" + +MY_TMP_DIR=$(mktemp -d /tmp/taler-auditor-basedbXXXXXX) +echo "Using $MY_TMP_DIR for logging and temporary data" +TMPDIR="$MY_TMP_DIR/postgres" mkdir -p "$TMPDIR" echo -n "Setting up Postgres DB at $TMPDIR ..." $INITDB_BIN \ --no-sync \ --auth=trust \ -D "${TMPDIR}" \ - > "${MYDIR}/postgres-dbinit.log" \ - 2> "${MYDIR}/postgres-dbinit.err" + > "${MY_TMP_DIR}/postgres-dbinit.log" \ + 2> "${MY_TMP_DIR}/postgres-dbinit.err" echo "DONE" -mkdir "${TMPDIR}/sockets" +SOCKETDIR="${TMPDIR}/sockets" +mkdir "${SOCKETDIR}" echo -n "Launching Postgres service" cat - >> "$TMPDIR/postgresql.conf" < "${MYDIR}/postgres-start.log" \ - 2> "${MYDIR}/postgres-start.err" + > "${MY_TMP_DIR}/postgres-start.log" \ + 2> "${MY_TMP_DIR}/postgres-start.err" echo " DONE" PGHOST="$TMPDIR/sockets" export PGHOST -# FIXME... -MYDIR=bar/ -DB=foo -# foo.sql -# foo.conf -# foo.mpriv -check_with_database "$MYDIR/$DB" +# FIXME: here for testing, avoids generation skip. +# Should probably introduce getopt to make this +# possible via CLI. +check_with_database "bar/${DB}" exit 0 - +MYDIR="${MY_TMP_DIR}/basedb" +mkdir -p "${MYDIR}" echo "Generating fresh database at $MYDIR" if faketime -f '-1 d' ./generate-auditor-basedb.sh "$MYDIR/$DB" then @@ -2376,9 +2365,6 @@ then if [ "$fail" != "0" ] then exit "$fail" - else - echo "Cleaning up $MYDIR..." - rm -rf "$MYDIR" || echo "Removing $MYDIR failed" fi else echo "Generation failed"