summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/Makefile3
-rw-r--r--test/base64.c48
-rwxr-xr-xtest/runtest.sh34
3 files changed, 85 insertions, 0 deletions
diff --git a/test/Makefile b/test/Makefile
new file mode 100644
index 0000000..1a86ed0
--- /dev/null
+++ b/test/Makefile
@@ -0,0 +1,3 @@
+DIR_TEST_ITEMS := base64:base64.c
+
+include $(NSBUILD)/Makefile.subdir
diff --git a/test/base64.c b/test/base64.c
new file mode 100644
index 0000000..f86e65e
--- /dev/null
+++ b/test/base64.c
@@ -0,0 +1,48 @@
+/*
+ * Copyright 2014 Vincent Sanders <vince@netsurf-browser.org>
+ *
+ * This file is part of libnsutils.
+ *
+ * Licensed under the MIT License,
+ * http://www.opensource.org/licenses/mit-license.php
+ */
+
+/**
+ * \file
+ *
+ * Base64 test program. Reads data from stdin and en/de codes it to/from base64
+ */
+
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+
+#include <nsutils/base64.h>
+
+int main(int argc, char**argv)
+{
+ uint8_t *buffer;
+ size_t buffer_len;
+ uint8_t *output;
+ size_t output_len;
+
+ if (scanf("%1024mc%n", &buffer, (int *)&buffer_len) < 1) {
+ return 1;
+ }
+
+ if (argc == 1) {
+ /* encode */
+ base64_encode_alloc(buffer, buffer_len, &output, &output_len);
+ } else {
+ /* decode */
+ base64_decode_alloc(buffer, buffer_len, &output, &output_len);
+ }
+
+ if (output != NULL) {
+ printf("%.*s", (int)output_len, output);
+ free(output);
+ }
+
+ free(buffer);
+
+}
diff --git a/test/runtest.sh b/test/runtest.sh
new file mode 100755
index 0000000..efcb79f
--- /dev/null
+++ b/test/runtest.sh
@@ -0,0 +1,34 @@
+#!/bin/sh
+TEST_PATH=$1
+
+b64enctst()
+{
+ ENC=$(echo -n "${1}" | ${TEST_PATH}/test_base64 )
+ if [ "${ENC}" != "${2}" ];then
+ echo "Base64 encode error ${ENC} != ${2}"
+ exit 2
+ fi
+}
+
+b64dectst()
+{
+ DEC=$(echo -n "$1" | ${TEST_PATH}/test_base64 -d )
+ if [ "${DEC}" != "$2" ];then
+ echo "Base64 decode error ${DEC} != $2"
+ exit 3
+ fi
+}
+
+b64enctst 'f' 'Zg=='
+b64enctst 'fo' 'Zm8='
+b64enctst 'foo' 'Zm9v'
+b64enctst 'foob' 'Zm9vYg=='
+b64enctst 'fooba' 'Zm9vYmE='
+b64enctst 'foobar' 'Zm9vYmFy'
+
+b64dectst 'Zg==' 'f'
+b64dectst 'Zm8=' 'fo'
+b64dectst 'Zm9v' 'foo'
+b64dectst 'Zm9vYg==' 'foob'
+b64dectst 'Zm9vYmE=' 'fooba'
+b64dectst 'Zm9vYmFy' 'foobar'