From ac811bcbd1b7a879b30e824c1e844395d05efdad Mon Sep 17 00:00:00 2001
From: downtownallday <downtownallday@gmail.com>
Date: Wed, 7 Apr 2021 18:11:21 -0400
Subject: [PATCH] Add some test scripts

---
 management/reporting/capture/.gitignore       |  1 -
 management/reporting/capture/tests/.gitignore |  3 ++
 .../reporting/capture/tests/config.json       | 12 +++++++
 management/reporting/capture/tests/load.sh    | 35 +++++++++++++++++++
 management/reporting/capture/tests/run.sh     | 34 ++++++++++++++++++
 5 files changed, 84 insertions(+), 1 deletion(-)
 create mode 100644 management/reporting/capture/tests/.gitignore
 create mode 100644 management/reporting/capture/tests/config.json
 create mode 100755 management/reporting/capture/tests/load.sh
 create mode 100755 management/reporting/capture/tests/run.sh

diff --git a/management/reporting/capture/.gitignore b/management/reporting/capture/.gitignore
index 2bfa6a4d..e69de29b 100644
--- a/management/reporting/capture/.gitignore
+++ b/management/reporting/capture/.gitignore
@@ -1 +0,0 @@
-tests/
diff --git a/management/reporting/capture/tests/.gitignore b/management/reporting/capture/tests/.gitignore
new file mode 100644
index 00000000..c0b23705
--- /dev/null
+++ b/management/reporting/capture/tests/.gitignore
@@ -0,0 +1,3 @@
+*.log
+pos.json
+*.sqlite
diff --git a/management/reporting/capture/tests/config.json b/management/reporting/capture/tests/config.json
new file mode 100644
index 00000000..db8c7d55
--- /dev/null
+++ b/management/reporting/capture/tests/config.json
@@ -0,0 +1,12 @@
+{
+    "capture": true,
+    "prune_policy": {
+        "frequency_min": 2400,
+        "older_than_days": 30
+    },
+    "drop_disposition": {
+        "failed_login_attempt": false,
+        "suspected_scanner": false,
+        "reject": false
+    }
+}
diff --git a/management/reporting/capture/tests/load.sh b/management/reporting/capture/tests/load.sh
new file mode 100755
index 00000000..36938d79
--- /dev/null
+++ b/management/reporting/capture/tests/load.sh
@@ -0,0 +1,35 @@
+#!/bin/bash
+
+# load a mail.log file into the current test vm's capture.sqlite
+#
+if [ -z "$1" ]; then
+    echo "usage: $0 /path/to/mail.log"
+    exit 1
+fi
+
+log="$1"
+if [ ! -e "$log" ]; then
+    echo "Does not exist: $log"
+    exit 1
+fi
+
+. /etc/mailinabox.conf
+if [ $? -ne 0 ]; then
+    echo "Could not load /etc/mailinabox.conf !!"
+    exit 1
+fi
+
+
+echo "Stopping maibldap-capture daemon"
+systemctl stop miabldap-capture || exit 1
+
+echo "Ensuring access to capture.sqlite"
+capture_db=$STORAGE_ROOT/reporting/capture.sqlite
+sqlite3 "$capture_db" "select value from db_info where key='schema_version'" >/dev/null
+[ $? -ne 0 ] && exit 1
+
+echo "Loading $log"
+python3 ../capture.py -d -loglevel info -logfile "$log" -stopateof
+
+echo "Starting miabldap-capture daemon"
+systemctl start miabldap-capture
diff --git a/management/reporting/capture/tests/run.sh b/management/reporting/capture/tests/run.sh
new file mode 100755
index 00000000..17a87168
--- /dev/null
+++ b/management/reporting/capture/tests/run.sh
@@ -0,0 +1,34 @@
+#!/bin/bash
+
+#
+# interactively load a mail.log file and create a capture.sqlite
+# database in the current directory
+
+log="./mail.log"
+pos="./pos.json"
+sqlite="./capture.sqlite"
+config="./config.json"
+
+if [ -e "./debug.log" ]; then
+    log="./debug.log"
+fi
+
+case "$1" in
+    *.log )
+        log="$1"
+        shift
+        ;;
+esac
+
+if [ "$1" != "-c" ]; then
+    # Start over. Don't continue where we left off
+    echo "STARTING OVER"
+    rm -f "$pos"
+    rm -f "$sqlite"
+else
+    shift
+fi
+
+echo "USING LOG: $log"
+echo "DB: $sqlite"
+python3 ../capture.py -d -loglevel info $@ -logfile "$log" -posfile "$pos" -sqlitefile "$sqlite" -config "$config"