--- /dev/null
+# This file is a template, and might need editing before it works on your project.
+# use the official gcc image, based on debian
+# can use verions as well, like gcc:5.2
+# see https://hub.docker.com/_/gcc/
+image: gcc
+
+build:
+ stage: build
+ # instead of calling g++ directly you can also use some build toolkit like make
+ # install the necessary build tools when needed
+ before_script:
+ - apt update
+ - apt -y install libncurses-dev libmodbus-dev libsqlite3-dev libident-dev
+ script:
+ - make -C src/
+ artifacts:
+ paths:
+ - dispense
+ - cokebank_sqlite.so
+ - dispsrv
+ # depending on your build setup it's most likely a good idea to cache outputs to reduce the build time
+ # cache:
+ # paths:
+ # - "*.o"
+
+# run tests using the binary built before
+test:
+ stage: test
+ before_script:
+ # Install the -dev versions to avoid needing to find the actual library names
+ - apt update && apt -y install libncurses-dev libmodbus-dev libsqlite3-dev libident netcat sqlite3
+ - useradd testuser && chown -R testuser .
+ # SETUID on the client, so it can use AUTOAUTH (ident doesn't work on the container)
+ - chmod u+s dispense
+ script:
+ - cd tests && su -c './TEST_basic.sh' testuser
+ artifacts:
+ paths:
+ - tests/rundir/**
int Server_int_ParseArgs(int bUseLongArg, char *ArgStr, ...);
int Server_int_ParseFlags(tClient *Client, const char *Str, int *Mask, int *Value);
+#define CLIENT_DEBUG_LOW(Client, ...) do { if(giDebugLevel>1) Debug(Client, __VA_ARGS__); } while(0)
+#define CLIENT_DEBUG(Client, ...) do { if(giDebugLevel) Debug(Client, __VA_ARGS__); } while(0)
+
// === CONSTANTS ===
// - Commands
const struct sClientCommand {
return ;
}
- // Check if trusted
- if( Client->UID != 0 )
+ // Check if trusted (has to be root)
+ if( Client->UID != 1 )
{
if(giDebugLevel)
Debug(Client, "Attempting to use AUTHCARD as non-root");
return ;
}
+ CLIENT_DEBUG(Client, "MIFARE auth with '%s'", card_id);
int uid = Bank_GetAcctByCard(card_id);
if( uid < 0 )
{
if(!require_auth(Client)) return;
- int uid = Client->EffectiveUID;
- if(uid == -1)
- uid = Client->UID;
+ int uid = Client->EffectiveUID > 0 ? Client->EffectiveUID : Client->UID;
+ CLIENT_DEBUG(Client, "Setting PIN for UID %i", uid);
// Can only pinset yourself (well, the effective user)
Bank_SetPin(uid, pin);
sendf(Client->Socket, "200 Pin updated\n");
if(!require_auth(Client)) return;
- if( Bank_AddAcctCard(Client->UID, card_id) )
+ int uid = Client->EffectiveUID > 0 ? Client->EffectiveUID : Client->UID;
+ CLIENT_DEBUG(Client, "Add card '%s' to UID %i", card_id, uid);
+ if( Bank_AddAcctCard(uid, card_id) )
{
sendf(Client->Socket, "408 Card already exists\n");
return ;
#!/bin/bash
-set -eux
+set -eu
TESTNAME=basic
. _common.sh
-if $DISPENSE acct tpg; then
- FAIL "Database contains '$USER'"
-fi
-
-sqlite3 "${BASEDIR}cokebank.db" "INSERT INTO accounts (acct_name,acct_is_admin,acct_uid) VALUES ('${USER}',1,1);"
+TRY_COMMAND "$DISPENSE acct ${USER}"
+TRY_COMMAND "$DISPENSE pseudo:0"
+# Try to add a new user
TRY_COMMAND "$DISPENSE user add unittest_user0"
+# Ensure that the add worked
LOG "Checking for test user"
TRY_COMMAND $DISPENSE acct unittest_user0 | grep ': $ 0.00'
+# Manipulate user's balance
TRY_COMMAND $DISPENSE acct unittest_user0 +100 Unit_test
TRY_COMMAND $DISPENSE acct unittest_user0 | grep ': $ 1.00'
TRY_COMMAND $DISPENSE acct unittest_user0 -100 Unit_test
-
+#
+# NOTE: Not a script, to be included by scripts
+#
+USER=$(id -un)
BASEDIR=rundir/${TESTNAME}/
PORT=22222
mkdir -p ${BASEDIR}
rm -f ${BASEDIR}cokebank.db
+# Template configuration (disables door and the coke machine)
cat << EOF > ${BASEDIR}cfg_server.conf
# AUTOGENERATED Test ${TESTNAME}
server_port ${PORT}
EOF
echo "# AUTOGENERATED Test ${TESTNAME}" > ${BASEDIR}cfg_items.conf
+echo "pseudo 0 0 Test item" >> ${BASEDIR}cfg_items.conf
LOG() {
echo "TEST ${TESTNAME}: "$*
}
TRY_COMMAND() {
cmd="$*"
+ echo ">> $cmd"
if ! $cmd ; then
FAIL "Command \`$cmd\` failed"
fi
}
-DISPENSE="../dispense -H localhost -P ${PORT}"
+DISPENSE="../dispense -f /dev/null -H localhost -P ${PORT}"
-LD_LIBRARY_PATH=.. ../dispsrv -f ${BASEDIR}cfg_server.conf --dont-daemonise > ${BASEDIR}server.log 2>&1 &
+LD_LIBRARY_PATH=.. ../dispsrv -f ${BASEDIR}cfg_server.conf --dont-daemonise -d 2 > ${BASEDIR}server.log 2>&1 &
server_pid=$!
cleanup() {
- LOG "Killing ${server_pid}"
- kill ${server_pid}
+ if pidof dispsrv | grep ${server_pid} > /dev/null; then
+ LOG "Killing ${server_pid}"
+ kill ${server_pid}; true
+ else
+ LOG "Server already terminated"
+ fi
}
trap cleanup EXIT
LOG "Server running on PID ${server_pid}"
sleep 1
-echo "" | nc localhost ${PORT}
+# - Make sure that the server started
+if ! (echo "" | nc localhost ${PORT}); then
+ LOG "Server not responding on ${PORT}"
+ LOG "Server log contents:"
+ cat ${BASEDIR}server.log
+ exit 1
+fi
+
+if [[ "x$USER" == "xroot" ]]; then
+ FAIL "Running as root"
+fi
+# Ensure that the database doesn't already contain the current user
+# - 1. Protects against running the test against the production database
+# - 2. Allows adding the current user as an admin
+if $DISPENSE acct "${USER}" 2> /dev/null; then
+ FAIL "Database contains '$USER', are you running on production?"
+fi
+# Add the current user as an admin
+sqlite3 "${BASEDIR}cokebank.db" "INSERT INTO accounts (acct_name,acct_is_admin,acct_uid) VALUES ('${USER}',1,1);"