2019-08-22 15:30:02 +02:00
#!/bin/bash
2019-08-22 15:43:30 +02:00
# Setup database which was generated from a perfectly normal
# exchange-wallet interaction and run the auditor against it.
#
# Check that the auditor report is as expected.
#
# Requires 'jq' tool and Postgres superuser rights!
2019-08-22 15:30:02 +02:00
set -eu
2019-08-23 15:10:10 +02:00
2019-09-03 03:14:23 +02:00
# Set of numbers for all the testcases.
# When adding new tests, increase the last number:
2019-09-03 04:45:26 +02:00
ALL_TESTS = ` seq 0 5`
2019-09-03 03:14:23 +02:00
# $TESTS determines which tests we should run.
# This construction is used to make it easy to
2019-09-02 03:32:28 +02:00
# only run a subset of the tests. To only run a subset,
# pass the numbers of the tests to run as the FIRST
2019-09-03 03:14:23 +02:00
# argument to test-auditor.sh, i.e.:
#
# $ test-auditor.sh "1 3"
#
# to run tests 1 and 3 only. By default, all tests are run.
#
2019-09-02 03:32:28 +02:00
TESTS = ${ 1 :- $ALL_TESTS }
2019-09-03 03:14:23 +02:00
# Exit, with status code "skip" (no 'real' failure)
2019-08-26 03:16:36 +02:00
function exit_skip( ) {
echo $1
exit 77
}
2019-09-03 03:14:23 +02:00
# Exit, with error message (hard failure)
2019-08-26 03:16:36 +02:00
function exit_fail( ) {
echo $1
kill ` jobs -p`
exit 1
}
2019-09-03 03:14:23 +02:00
# Run audit process on current database, including report
2019-09-03 04:29:43 +02:00
# generation. Pass "aggregator" as $1 to run
# $ taler-exchange-aggregator
# before auditor (to trigger pending wire transfers).
2019-08-26 03:16:36 +02:00
function run_audit ( ) {
# Launch bank
echo "Launching bank"
taler-bank-manage -c test-auditor.conf serve-http 2>bank.err >bank.log &
while true
do
echo -n "."
wget http://localhost:8082/ -o /dev/null -O /dev/null >/dev/null && break
sleep 1
done
echo "OK"
2019-09-03 04:29:43 +02:00
if test ${ 1 :- no } = "aggregator"
then
echo "Running exchange aggregator"
taler-exchange-aggregator -t -c test-auditor.conf
fi
2019-08-26 03:16:36 +02:00
# Run the auditor!
echo "Running audit(s)"
2019-09-03 04:29:43 +02:00
taler-auditor -r -c test-auditor.conf -m $MASTER_PUB > test-audit.json 2> test-audit.log || exit_fail "auditor failed"
2019-09-02 03:32:28 +02:00
2019-09-03 04:29:43 +02:00
taler-wire-auditor -r -c test-auditor.conf -m $MASTER_PUB > test-wire-audit.json 2> test-wire-audit.log || exit_fail "wire auditor failed"
2019-08-26 03:16:36 +02:00
echo "Shutting down services"
2019-09-03 04:29:43 +02:00
kill ` jobs -p` || true
2019-08-26 03:16:36 +02:00
echo "TeXing"
2019-09-02 08:01:55 +02:00
../../contrib/render.py test-audit.json test-wire-audit.json < ../../contrib/auditor-report.tex.j2 > test-report.tex || exit_fail "Renderer failed"
2019-08-26 03:16:36 +02:00
2019-09-02 08:01:55 +02:00
timeout 10 pdflatex test-report.tex >/dev/null || exit_fail "pdflatex failed"
2019-09-02 03:32:28 +02:00
timeout 10 pdflatex test-report.tex >/dev/null
2019-08-26 03:16:36 +02:00
}
2019-09-03 04:29:43 +02:00
# Do a full reload of the (original) database
full_reload( )
{
dropdb $DB 2> /dev/null || true
createdb -T template0 $DB || exit_skip "could not create database"
# Import pre-generated database, -q(ietly) using single (-1) transaction
psql -Aqt $DB -q -1 -f ../benchmark/auditor-basedb.sql > /dev/null
}
2019-08-23 15:17:54 +02:00
2019-09-03 04:29:43 +02:00
test_0( ) {
echo "===========0: normal run with aggregator==========="
run_audit aggregator
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.json > /dev/null && exit_fail "Unexpected emergency detected in ordinary run" || echo OK
jq -e .emergencies_by_count[ 0] < test-audit.json > /dev/null && exit_fail "Unexpected emergency by count detected in ordinary run" || echo OK
echo -n "Test for wire inconsistencies... "
jq -e .wire_out_amount_inconsistencies[ 0] < test-wire-audit.json > /dev/null && exit_fail "Unexpected wire out inconsistency detected in ordinary run"
jq -e .reserve_in_amount_inconsistencies[ 0] < test-wire-audit.json > /dev/null && exit_fail "Unexpected reserve in inconsistency detected in ordinary run"
jq -e .missattribution_inconsistencies[ 0] < test-wire-audit.json > /dev/null && exit_fail "Unexpected missattribution inconsistency detected in ordinary run"
jq -e .row_inconsistencies[ 0] < test-wire-audit.json > /dev/null && exit_fail "Unexpected row inconsistency detected in ordinary run"
jq -e .row_minor_inconsistencies[ 0] < test-wire-audit.json > /dev/null && exit_fail "Unexpected minor row inconsistency detected in ordinary run"
jq -e .lag_details[ 0] < test-wire-audit.json > /dev/null && exit_fail "Unexpected lag detected in ordinary run"
jq -e .wire_format_inconsistencies[ 0] < test-wire-audit.json > /dev/null && exit_fail "Unexpected wire format inconsistencies detected in ordinary run"
# FIXME: check operation balances are correct (once we have more transaction types)
# FIXME: check revenue summaries are correct (once we have more transaction types)
echo OK
echo -n "Test for wire amounts... "
WIRED = ` jq -r .total_wire_in_delta_plus < test-wire-audit.json`
if test $WIRED != "TESTKUDOS:0"
then
exit_fail " Expected total wire delta plus wrong, got $WIRED "
fi
WIRED = ` jq -r .total_wire_in_delta_minus < test-wire-audit.json`
if test $WIRED != "TESTKUDOS:0"
then
exit_fail " Expected total wire delta minus wrong, got $WIRED "
fi
WIRED = ` jq -r .total_wire_out_delta_plus < test-wire-audit.json`
if test $WIRED != "TESTKUDOS:0"
then
exit_fail " Expected total wire delta plus wrong, got $WIRED "
fi
WIRED = ` jq -r .total_wire_out_delta_minus < test-wire-audit.json`
if test $WIRED != "TESTKUDOS:0"
then
exit_fail " Expected total wire delta minus wrong, got $WIRED "
fi
WIRED = ` jq -r .total_missattribution_in < test-wire-audit.json`
if test $WIRED != "TESTKUDOS:0"
then
exit_fail " Expected total missattribution in wrong, got $WIRED "
fi
2019-08-22 15:30:02 +02:00
2019-09-03 04:29:43 +02:00
# FIXME: check NO lag reported
2019-09-02 03:32:28 +02:00
2019-09-03 04:29:43 +02:00
# cannot easily undo aggregator, hence full reload
full_reload
echo "OK"
}
# Run without aggregator, hence auditor should detect wire
# transfer lag!
2019-09-02 03:32:28 +02:00
test_1( ) {
2019-08-26 03:16:36 +02:00
echo "===========1: normal run==========="
run_audit
2019-08-23 15:10:10 +02:00
echo "Checking output"
2019-08-22 15:43:30 +02:00
# if an emergency was detected, that is a bug and we should fail
echo -n "Test for emergencies... "
2019-08-26 03:16:36 +02:00
jq -e .emergencies[ 0] < test-audit.json > /dev/null && exit_fail "Unexpected emergency detected in ordinary run" || echo OK
2019-09-02 08:01:55 +02:00
jq -e .emergencies_by_count[ 0] < test-audit.json > /dev/null && exit_fail "Unexpected emergency by count detected in ordinary run" || echo OK
2019-08-26 03:16:36 +02:00
echo -n "Test for wire inconsistencies... "
jq -e .wire_out_amount_inconsistencies[ 0] < test-wire-audit.json > /dev/null && exit_fail "Unexpected wire out inconsistency detected in ordinary run"
jq -e .reserve_in_amount_inconsistencies[ 0] < test-wire-audit.json > /dev/null && exit_fail "Unexpected reserve in inconsistency detected in ordinary run"
jq -e .missattribution_inconsistencies[ 0] < test-wire-audit.json > /dev/null && exit_fail "Unexpected missattribution inconsistency detected in ordinary run"
jq -e .row_inconsistencies[ 0] < test-wire-audit.json > /dev/null && exit_fail "Unexpected row inconsistency detected in ordinary run"
jq -e .row_minor_inconsistencies[ 0] < test-wire-audit.json > /dev/null && exit_fail "Unexpected minor row inconsistency detected in ordinary run"
jq -e .lag_details[ 0] < test-wire-audit.json > /dev/null && exit_fail "Unexpected lag detected in ordinary run"
jq -e .wire_format_inconsistencies[ 0] < test-wire-audit.json > /dev/null && exit_fail "Unexpected wire format inconsistencies detected in ordinary run"
2019-09-02 08:01:55 +02:00
# FIXME: check operation balances are correct (once we have more transaction types)
# FIXME: check revenue summaries are correct (once we have more transaction types)
2019-08-26 03:16:36 +02:00
echo OK
2019-09-03 04:29:43 +02:00
# FIXME: check wire transfer lag reported (no aggregator!)
2019-08-26 03:16:36 +02:00
echo -n "Test for wire amounts... "
WIRED = ` jq -r .total_wire_in_delta_plus < test-wire-audit.json`
if test $WIRED != "TESTKUDOS:0"
then
exit_fail " Expected total wire delta plus wrong, got $WIRED "
fi
WIRED = ` jq -r .total_wire_in_delta_minus < test-wire-audit.json`
if test $WIRED != "TESTKUDOS:0"
then
exit_fail " Expected total wire delta minus wrong, got $WIRED "
fi
WIRED = ` jq -r .total_wire_out_delta_plus < test-wire-audit.json`
if test $WIRED != "TESTKUDOS:0"
then
exit_fail " Expected total wire delta plus wrong, got $WIRED "
fi
WIRED = ` jq -r .total_wire_out_delta_minus < test-wire-audit.json`
if test $WIRED != "TESTKUDOS:0"
then
exit_fail " Expected total wire delta minus wrong, got $WIRED "
fi
WIRED = ` jq -r .total_missattribution_in < test-wire-audit.json`
if test $WIRED != "TESTKUDOS:0"
then
exit_fail " Expected total missattribution in wrong, got $WIRED "
fi
2019-09-03 04:29:43 +02:00
# Database was unmodified, no need to undo
2019-08-26 03:16:36 +02:00
echo "OK"
2019-09-02 03:32:28 +02:00
}
2019-08-26 03:16:36 +02:00
2019-09-03 04:29:43 +02:00
# Change amount of wire transfer reported by exchange
2019-09-02 03:32:28 +02:00
test_2( ) {
2019-08-26 03:16:36 +02:00
echo "===========2: reserves_in inconsitency==========="
2019-09-03 04:29:43 +02:00
echo "UPDATE reserves_in SET credit_val=5 WHERE reserve_in_serial_id=1" | psql -Aqt $DB
2019-08-22 15:30:02 +02:00
2019-08-26 03:16:36 +02:00
run_audit
echo -n "Test for inconsistency detection... "
ROW = ` jq .reserve_in_amount_inconsistencies[ 0] .row < test-wire-audit.json`
if test $ROW != 1
then
exit_fail "Row wrong"
fi
WIRED = ` jq -r .reserve_in_amount_inconsistencies[ 0] .amount_wired < test-wire-audit.json`
if test $WIRED != "TESTKUDOS:10"
then
exit_fail "Amount wrong"
fi
EXPECTED = ` jq -r .reserve_in_amount_inconsistencies[ 0] .amount_exchange_expected < test-wire-audit.json`
if test $EXPECTED != "TESTKUDOS:5"
then
exit_fail "Expected amount wrong"
fi
2019-09-02 03:32:28 +02:00
WIRED = ` jq -r .total_wire_in_delta_minus < test-wire-audit.json`
if test $WIRED != "TESTKUDOS:0"
then
exit_fail " Wrong total wire_in_delta_minus, got $WIRED "
fi
2019-08-26 03:16:36 +02:00
DELTA = ` jq -r .total_wire_in_delta_plus < test-wire-audit.json`
if test $DELTA != "TESTKUDOS:5"
then
exit_fail "Expected total wire delta plus wrong"
fi
echo OK
# Undo database modification
2019-09-03 04:29:43 +02:00
echo "UPDATE reserves_in SET credit_val=10 WHERE reserve_in_serial_id=1" | psql -Aqt $DB
2019-08-26 03:16:36 +02:00
2019-09-02 03:32:28 +02:00
}
# Check for incoming wire transfer amount given being
# lower than what exchange claims to have received.
test_3( ) {
echo "===========3: reserves_in inconsitency==========="
2019-09-03 04:29:43 +02:00
echo "UPDATE reserves_in SET credit_val=15 WHERE reserve_in_serial_id=1" | psql -Aqt $DB
2019-09-02 03:32:28 +02:00
run_audit
2019-09-02 08:01:55 +02:00
EXPECTED = ` jq -r .reserve_balance_summary_wrong_inconsistencies[ 0] .auditor < test-audit.json`
if test $EXPECTED != "TESTKUDOS:5.01"
2019-09-02 03:32:28 +02:00
then
2019-09-02 08:01:55 +02:00
exit_fail " Expected reserve balance summary amount wrong, got $EXPECTED (auditor) "
fi
EXPECTED = ` jq -r .reserve_balance_summary_wrong_inconsistencies[ 0] .exchange < test-audit.json`
if test $EXPECTED != "TESTKUDOS:0.01"
then
exit_fail " Expected reserve balance summary amount wrong, got $EXPECTED (exchange) "
2019-09-02 03:32:28 +02:00
fi
WIRED = ` jq -r .total_loss_balance_insufficient < test-audit.json`
2019-09-02 08:01:55 +02:00
if test $WIRED != "TESTKUDOS:0"
2019-09-02 03:32:28 +02:00
then
exit_fail " Wrong total loss from insufficient balance, got $WIRED "
fi
ROW = ` jq -e .reserve_in_amount_inconsistencies[ 0] .row < test-wire-audit.json`
if test $ROW != 1
then
exit_fail " Row wrong, got $ROW "
fi
WIRED = ` jq -r .reserve_in_amount_inconsistencies[ 0] .amount_exchange_expected < test-wire-audit.json`
if test $WIRED != "TESTKUDOS:15"
then
exit_fail " Wrong amount_exchange_expected, got $WIRED "
fi
WIRED = ` jq -r .reserve_in_amount_inconsistencies[ 0] .amount_wired < test-wire-audit.json`
if test $WIRED != "TESTKUDOS:10"
then
exit_fail " Wrong amount_wired, got $WIRED "
fi
WIRED = ` jq -r .total_wire_in_delta_minus < test-wire-audit.json`
if test $WIRED != "TESTKUDOS:5"
then
exit_fail " Wrong total wire_in_delta_minus, got $WIRED "
fi
WIRED = ` jq -r .total_wire_in_delta_plus < test-wire-audit.json`
if test $WIRED != "TESTKUDOS:0"
then
exit_fail " Wrong total wire_in_delta_plus, got $WIRED "
fi
# Undo database modification
2019-09-03 04:29:43 +02:00
echo "UPDATE reserves_in SET credit_val=10 WHERE reserve_in_serial_id=1" | psql -Aqt $DB
2019-09-02 03:32:28 +02:00
}
2019-09-03 03:14:23 +02:00
# Check for incoming wire transfer amount given being
# lower than what exchange claims to have received.
test_4( ) {
echo "===========4: deposit wire target wrong================="
# Original target bank account was 43, changing to 44
2019-09-03 04:29:43 +02:00
echo "UPDATE deposits SET wire='{\"url\":\"payto://x-taler-bank/localhost:8082/44\",\"salt\":\"test-salt (must be constant for aggregation tests)\"}' WHERE deposit_serial_id=1" | psql -Aqt $DB
2019-09-03 03:14:23 +02:00
run_audit
ROW = ` jq -e .bad_sig_losses[ 0] .row < test-audit.json`
if test $ROW != 1
then
exit_fail " Row wrong, got $ROW "
fi
LOSS = ` jq -r .bad_sig_losses[ 0] .loss < test-audit.json`
if test $LOSS != "TESTKUDOS:0.1"
then
exit_fail " Wrong deposit bad signature loss, got $LOSS "
fi
OP = ` jq -r .bad_sig_losses[ 0] .operation < test-audit.json`
if test $OP != "deposit"
then
exit_fail " Wrong operation, got $OP "
fi
LOSS = ` jq -r .total_bad_sig_loss < test-audit.json`
if test $LOSS != "TESTKUDOS:0.1"
then
exit_fail " Wrong total bad sig loss, got $LOSS "
fi
# Undo:
2019-09-03 04:29:43 +02:00
echo "UPDATE deposits SET wire='{\"url\":\"payto://x-taler-bank/localhost:8082/43\",\"salt\":\"test-salt (must be constant for aggregation tests)\"}' WHERE deposit_serial_id=1" | psql -Aqt $DB
2019-09-03 03:14:23 +02:00
}
2019-09-03 04:29:43 +02:00
# Test where h_contract_terms in the deposit table is wrong
# (=> bad signature)
test_5( ) {
echo "===========5: deposit contract hash wrong================="
# Modify h_wire hash, so it is inconsistent with 'wire'
OLD_H = ` echo 'SELECT h_contract_terms FROM deposits WHERE deposit_serial_id=1;' | psql taler-auditor-test -Aqt`
echo "UPDATE deposits SET h_contract_terms='\x12bb676444955c98789f219148aa31899d8c354a63330624d3d143222cf3bb8b8e16f69accd5a8773127059b804c1955696bf551dd7be62719870613332aa8d5' WHERE deposit_serial_id=1" | psql -Aqt $DB
run_audit
ROW = ` jq -e .bad_sig_losses[ 0] .row < test-audit.json`
if test $ROW != 1
then
exit_fail " Row wrong, got $ROW "
fi
LOSS = ` jq -r .bad_sig_losses[ 0] .loss < test-audit.json`
if test $LOSS != "TESTKUDOS:0.1"
then
exit_fail " Wrong deposit bad signature loss, got $LOSS "
fi
OP = ` jq -r .bad_sig_losses[ 0] .operation < test-audit.json`
if test $OP != "deposit"
then
exit_fail " Wrong operation, got $OP "
fi
LOSS = ` jq -r .total_bad_sig_loss < test-audit.json`
if test $LOSS != "TESTKUDOS:0.1"
then
exit_fail " Wrong total bad sig loss, got $LOSS "
fi
2019-09-03 03:14:23 +02:00
2019-09-03 04:29:43 +02:00
# Undo:
echo " UPDATE deposits SET h_contract_terms=' ${ OLD_H } ' WHERE deposit_serial_id=1 " | psql -Aqt $DB
2019-09-03 03:14:23 +02:00
2019-09-03 04:29:43 +02:00
}
2019-09-03 04:44:48 +02:00
# Test where denom_sig in known_coins table is wrong
# (=> bad signature)
test_6( ) {
echo "===========6: known_coins signature wrong================="
# Modify denom_sig, so it is wrong
OLD_SIG = ` echo 'SELECT denom_sig FROM known_coins LIMIT 1;' | psql taler-auditor-test -Aqt`
COIN_PUB = ` echo " SELECT coin_pub FROM known_coins WHERE denom_sig=' $OLD_SIG '; " | psql taler-auditor-test -Aqt`
echo " UPDATE known_coins SET denom_sig='\x287369672d76616c200a2028727361200a2020287320233542383731423743393036444643303442424430453039353246413642464132463537303139374131313437353746324632323332394644443146324643333445393939413336363430334233413133324444464239413833353833464536354442374335434445304441443035374438363336434541423834463843323843344446304144363030343430413038353435363039373833434431333239393736423642433437313041324632414132414435413833303432434346314139464635394244434346374436323238344143354544364131373739463430353032323241373838423837363535453434423145443831364244353638303232413123290a2020290a20290b' WHERE coin_pub=' $COIN_PUB ' " | psql -Aqt $DB
run_audit
# FIXME: add logic to check bad signature was detected
# (NOTE: FIXME42-bug: auditor does not yet check denom_sigs!)
# Undo
echo " UPDATE known_coins SET denom_sig=' $OLD_SIG ' WHERE coin_pub=' $COIN_PUB ' " | psql -Aqt $DB
}
2019-09-03 04:29:43 +02:00
# Test where h_wire in the deposit table is wrong
test_99( ) {
echo "===========99: deposit wire hash wrong================="
# Modify h_wire hash, so it is inconsistent with 'wire'
echo "UPDATE deposits SET h_wire='\x973e52d193a357940be9ef2939c19b0575ee1101f52188c3c01d9005b7d755c397e92624f09cfa709104b3b65605fe5130c90d7e1b7ee30f8fc570f39c16b853' WHERE deposit_serial_id=1" | psql -Aqt $DB
# The auditor checks h_wire consistency only for
# coins where the wire transfer has happened, hence
# run aggregator first to get this test to work.
#
# FIXME: current test database has transfers still
# in the *distant* future, test cannot yet work.
# patch up once DB was re-generated!
run_audit aggregator
# FIXME: check for the respective inconsistency in the report!
# Undo:
# echo "UPDATE deposits SET h_wire='\x973e52d193a357940be9ef2939c19b0575ee1101f52188c3c01d9005b7d755c397e92624f09cfa709104b3b65605fe5130c90d7e1b7ee30f8fc570f39c16b852' WHERE deposit_serial_id=1" | psql -Aqt $DB
}
# **************************************************
2019-09-03 03:14:23 +02:00
# Add more tests here! :-)
2019-09-03 04:29:43 +02:00
# **************************************************
# *************** Main logic starts here **************
# Setup globals
DB = taler-auditor-test
MASTER_PUB = ` cat ../benchmark/auditor-basedb.mpub`
# test required commands exist
echo "Testing for jq"
jq -h > /dev/null || exit_skip "jq required"
echo "Testing for taler-bank-manage"
taler-bank-manage -h >/dev/null </dev/null || exit_skip "taler-bank-manage required"
echo "Testing for pdflatex"
which pdflatex > /dev/null </dev/null || exit_skip "pdflatex required"
echo "Database setup"
full_reload
2019-09-03 03:14:23 +02:00
2019-09-03 04:29:43 +02:00
# Run test suite
2019-09-02 03:32:28 +02:00
fail = 0
for i in $TESTS
do
test_$i
if test 0 != $fail
then
break
fi
done
2019-08-23 15:10:10 +02:00
echo "Cleanup"
2019-09-03 04:29:43 +02:00
# dropdb $DB
# rm -f test-audit.log test-wire-audit.log
2019-08-22 15:30:02 +02:00
2019-08-22 15:43:30 +02:00
exit $fail