commit c17c586184dd6c3a5f7b98759e77194ad3b5dcca
parent 53120e371609ab266df16ad6aad1049751d5d974
Author: Santtu Lakkala <santtu.lakkala@digital14.com>
Date: Wed, 27 Sep 2023 14:00:23 +0300
Add stress tester script
Diffstat:
M | main.c | | | 9 | +++++++-- |
A | tests/stress.sh | | | 89 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
2 files changed, 96 insertions(+), 2 deletions(-)
diff --git a/main.c b/main.c
@@ -162,9 +162,14 @@ void write_filter_key(struct token *token,
struct write_filter_data *d = data;
if (d->filter) {
- char descbuf[UINT8_MAX + 1];
+ char descbuf[UINT8_MAX * 2 + 2];
+ char *w = descbuf;
- *(char *)mempushb(descbuf, token->desc) = '\0';
+ if (bytes_len(token->issuer)) {
+ w = mempushb(w, token->issuer);
+ *w++ = ':';
+ }
+ *(char *)mempushb(w, token->desc) = '\0';
if (!fnmatch(d->filter, descbuf, FNM_NOESCAPE))
return;
diff --git a/tests/stress.sh b/tests/stress.sh
@@ -0,0 +1,89 @@
+#!/bin/sh
+
+onexit() {
+ [ -n "$KEYFILE" ] && rm "$KEYFILE"
+ [ -n "$DBFILE" ] && rm "$DBFILE"
+ [ -n "$URIFILE" ] && rm "$URIFILE"
+}
+
+trap onexit EXIT
+KEYFILE="$(mktemp)"
+DBFILE="$(mktemp)"
+URIFILE="$(mktemp)"
+
+b32encode() {
+ od -vt u1 | awk '
+ BEGIN {
+ b32 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
+ acc = 0;
+ n = 0;
+ }
+ {
+ for (i = 2; i <= NF; i++) {
+ acc = acc * 256 + $i;
+ n += 8;
+ while (n >= 5) {
+ printf("%s", substr(b32, int(acc / (2 ^ (n - 5))) + 1, 1));
+ acc %= (2 ^ (n - 5));
+ n -= 5;
+ }
+ }
+ } END {
+ if (n > 0)
+ printf("%s", substr(b32, acc * 2 ^ (5 - n) + 1, 1));
+ print "";
+ }'
+}
+
+readbytes() {
+ dd bs=1 count=$1 2>/dev/null
+}
+
+randuri() {
+ keysize="$(readbytes 1 | od -t u1 | awk 'NR == 1 { print 8 * (($2 % 31) + 1) }')"
+ key="$(readbytes "$keysize" | b32encode)"
+ digits="$(readbytes 1 | od -t u1 | awk 'NR == 1 { print (($2 % 3) + 6) }')"
+ algo="$(readbytes 1 | od -t u1 | awk 'BEGIN { arr[0] = "SHA1"; arr[1] = "SHA256"; arr[2] = "SHA512" } NR == 1 { print arr[$2 % 3] }')"
+ period="$(readbytes 1 | od -t u1 | awk 'NR == 1 { print 10 + ($2 % 240) }')"
+
+ echo "otpauth://totp/issuer$1:ident$1?secret=$key&issuer=issuer$1&algorithm=$algo&digits=$digits&period=$period"
+}
+
+rand() {
+ awk 'BEGIN {
+ srand();
+ while (1) {
+ printf("%c", rand() * 256);
+ }
+ }'
+}
+
+FirstKey=0
+LastKey=0
+
+rand | (
+ readbytes 16 | b32encode > "$KEYFILE"
+ rm "$DBFILE"
+ while true; do
+ case "$(readbytes 1 | od -t u1 | awk 'NR == 1 { print $2 % 3 }')" in
+ 0)
+ Uri="$(randuri $((LastKey)))"
+ "$1" -K "$KEYFILE" -f "$DBFILE" -a "$Uri"
+ echo "$Uri" >>"$URIFILE"
+ LastKey="$((LastKey + 1))"
+ ;;
+ 1)
+ if ! test "$LastKey" = "$FirstKey"; then
+ "$1" -K "$KEYFILE" -f "$DBFILE" -d "issuer$FirstKey:ident$FirstKey"
+ sed -i -e '1d' "$URIFILE"
+ FirstKey="$((FirstKey + 1))"
+ fi
+ ;;
+ 2)
+ if ! "$1" -K "$KEYFILE" -f "$DBFILE" -e | diff - "$URIFILE"; then
+ echo "Mismatch" >&2
+ fi
+ ;;
+ esac
+ done
+)