anastasis

Credential backup and recovery protocol and service
Log | Files | Refs | Submodules | README | LICENSE

test_anastasis_config_currencies.sh (5120B)


      1 #!/bin/bash
      2 # This file is part of Anastasis
      3 # Copyright (C) 2026 Anastasis SARL
      4 #
      5 # Anastasis is free software; you can redistribute it and/or modify it under the
      6 # terms of the GNU Affero General Public License as published by the Free Software
      7 # Foundation; either version 3, or (at your option) any later version.
      8 #
      9 # Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY
     10 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
     11 # A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more details.
     12 #
     13 # You should have received a copy of the GNU Affero General Public License along with
     14 # Anastasis; see the file COPYING.GPL.  If not, see <http://www.gnu.org/licenses/>
     15 #
     16 # Check the multi-currency configuration rules that anastasis-httpd enforces
     17 # at startup: a price must be uniformly free or uniformly priced, and a
     18 # priced option must cover exactly [anastasis] CURRENCIES.
     19 #
     20 # Needs neither a database nor any Taler service.  Every case below is
     21 # decided while the configuration is parsed, which happens before
     22 # anastasis-httpd connects to anything.
     23 set -eu
     24 
     25 # Exit code for skipped tests, as expected by meson.
     26 SKIP=77
     27 
     28 if ! anastasis-httpd --help > /dev/null 2> /dev/null
     29 then
     30     echo "anastasis-httpd not found, skipping"
     31     exit $SKIP
     32 fi
     33 
     34 WORK_DIR=$(mktemp -p "${TMPDIR:-/tmp}" -d test-anastasis-currencies-XXXXXX)
     35 # shellcheck disable=SC2317
     36 cleanup () {
     37     rm -rf "$WORK_DIR"
     38 }
     39 trap cleanup EXIT
     40 
     41 BASE="$(pwd)/test_reducer.conf"
     42 LOG="$WORK_DIR/httpd.log"
     43 
     44 # Start anastasis-httpd on test_reducer.conf plus the given overrides and
     45 # report whether the price lists were accepted.  Prints "accepted" or
     46 # "refused"; the log is left in $LOG.
     47 #
     48 # The verdict is read off the log rather than off the exit status on
     49 # purpose: this configuration is deliberately incomplete in other ways
     50 # (no BUSINESS_NAME, no initialised database), so the process stops
     51 # either way and only *why* it stopped distinguishes the cases.
     52 try_config ()
     53 {
     54     cat > "$WORK_DIR/test.conf" <<EOF
     55 @INLINE@ $BASE
     56 $1
     57 EOF
     58     anastasis-httpd -c "$WORK_DIR/test.conf" > "$LOG" 2>&1 &
     59     local pid=$!
     60     sleep 1
     61     kill "$pid" 2> /dev/null || true
     62     wait "$pid" 2> /dev/null || true
     63     if grep -q "Price list error" "$LOG"
     64     then
     65         echo "refused"
     66     else
     67         echo "accepted"
     68     fi
     69 }
     70 
     71 fail ()
     72 {
     73     cat "$LOG"
     74     echo "FAIL: $1"
     75     exit 1
     76 }
     77 
     78 # A configuration that prices everything in both currencies; each case
     79 # below deviates from it in exactly one option.
     80 GOOD="[anastasis]
     81 CURRENCIES = TESTKUDOS;EUR
     82 ANNUAL_FEE = TESTKUDOS:4.99;EUR:4
     83 TRUTH_UPLOAD_FEE = TESTKUDOS:0.01;EUR:0.01
     84 INSURANCE = TESTKUDOS:1.0;EUR:1.0
     85 
     86 [authorization-question]
     87 COST = TESTKUDOS:0.0;EUR:0.0"
     88 
     89 echo -n "Test that a well-formed multi-currency configuration is accepted ..."
     90 RES=$(try_config "$GOOD")
     91 if [ "$RES" != "accepted" ]
     92 then
     93     fail "anastasis-httpd rejected a well-formed multi-currency configuration"
     94 fi
     95 echo " OK"
     96 
     97 echo -n "Test that a mixed free/priced option is refused ..."
     98 RES=$(try_config "${GOOD}
     99 
    100 [anastasis]
    101 ANNUAL_FEE = TESTKUDOS:4.99;EUR:0")
    102 if [ "$RES" != "refused" ]
    103 then
    104     fail "anastasis-httpd accepted ANNUAL_FEE = TESTKUDOS:4.99;EUR:0"
    105 fi
    106 # The diagnostic has to name the option, or an operator cannot act on it.
    107 if ! grep -q "ANNUAL_FEE" "$LOG"
    108 then
    109     fail "refusal did not name the offending option"
    110 fi
    111 echo " OK"
    112 
    113 echo -n "Test that an option missing a currency is refused ..."
    114 RES=$(try_config "${GOOD}
    115 
    116 [anastasis]
    117 TRUTH_UPLOAD_FEE = TESTKUDOS:0.01")
    118 if [ "$RES" != "refused" ]
    119 then
    120     fail "anastasis-httpd accepted a TRUTH_UPLOAD_FEE missing EUR"
    121 fi
    122 if ! grep -q "TRUTH_UPLOAD_FEE" "$LOG" || ! grep -q "EUR" "$LOG"
    123 then
    124     fail "refusal did not name the option and the missing currency"
    125 fi
    126 echo " OK"
    127 
    128 echo -n "Test that an option priced in an undeclared currency is refused ..."
    129 RES=$(try_config "${GOOD}
    130 
    131 [anastasis]
    132 TRUTH_UPLOAD_FEE = TESTKUDOS:0.01;EUR:0.01;CHF:0.01")
    133 if [ "$RES" != "refused" ]
    134 then
    135     fail "anastasis-httpd accepted a fee in a currency not in CURRENCIES"
    136 fi
    137 if ! grep -q "CHF" "$LOG"
    138 then
    139     fail "refusal did not name the undeclared currency"
    140 fi
    141 echo " OK"
    142 
    143 echo -n "Test that an option free in every currency is accepted ..."
    144 # Zero is a first-class answer: it is how an operator says a method costs
    145 # nothing, e.g. because the IBAN transfer is already the cost of using it.
    146 RES=$(try_config "${GOOD}
    147 
    148 [authorization-question]
    149 COST = TESTKUDOS:0;EUR:0")
    150 if [ "$RES" != "accepted" ]
    151 then
    152     fail "anastasis-httpd rejected an all-zero COST"
    153 fi
    154 echo " OK"
    155 
    156 echo -n "Test that an empty option means free ..."
    157 RES=$(try_config "${GOOD}
    158 
    159 [authorization-question]
    160 COST =")
    161 if [ "$RES" != "accepted" ]
    162 then
    163     fail "anastasis-httpd rejected an empty COST"
    164 fi
    165 echo " OK"
    166 
    167 echo -n "Test that a single-currency configuration still works ..."
    168 # No CURRENCIES option at all: this is every configuration written before
    169 # multi-currency support, and none of them may break.
    170 RES=$(try_config "")
    171 if [ "$RES" != "accepted" ]
    172 then
    173     fail "anastasis-httpd rejected a single-currency configuration"
    174 fi
    175 echo " OK"
    176 
    177 exit 0