Gitlab CI - Debugging master
authorJohn Hodge <[email protected]>
Mon, 18 Oct 2021 12:54:23 +0000 (20:54 +0800)
committerJohn Hodge <[email protected]>
Mon, 1 Nov 2021 11:36:59 +0000 (19:36 +0800)
.gitlab-ci.yml [new file with mode: 0644]
src/server/server.c
tests/TEST_basic.sh
tests/_common.sh

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
new file mode 100644 (file)
index 0000000..49af60d
--- /dev/null
@@ -0,0 +1,39 @@
+# 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/**
index 0f254b9..76b3b44 100644 (file)
@@ -94,6 +94,9 @@ void  Debug(tClient *Client, const char *Format, ...);
  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 {
@@ -663,8 +666,8 @@ void Server_Cmd_AUTHCARD(tClient* Client, char *Args)
                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");
@@ -672,6 +675,7 @@ void Server_Cmd_AUTHCARD(tClient* Client, char *Args)
                return ;
        }
 
+       CLIENT_DEBUG(Client, "MIFARE auth with '%s'", card_id);
        int uid = Bank_GetAcctByCard(card_id);
        if( uid < 0 )
        {
@@ -1651,9 +1655,8 @@ void Server_Cmd_PINSET(tClient *Client, char *Args)
 
        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");
@@ -1669,7 +1672,9 @@ void Server_Cmd_CARDADD(tClient* Client, char* Args)
 
        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 ;
index f2f1301..4636930 100755 (executable)
@@ -1,20 +1,20 @@
 #!/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
index 0019b99..0f62430 100644 (file)
@@ -1,10 +1,14 @@
-
+#
+# 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}
@@ -21,6 +25,7 @@ coke_dummy_mode yes
 EOF
 
 echo "# AUTOGENERATED Test ${TESTNAME}" > ${BASEDIR}cfg_items.conf
+echo "pseudo   0       0       Test item" >> ${BASEDIR}cfg_items.conf
 
 LOG() {
        echo "TEST ${TESTNAME}: "$*
@@ -31,22 +36,45 @@ FAIL() {
 }
 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);"

UCC git Repository :: git.ucc.asn.au