aboutsummaryrefslogtreecommitdiff
path: root/src/supplemental/websocket/base64_test.c
diff options
context:
space:
mode:
authorGarrett D'Amore <garrett@damore.org>2025-01-01 13:10:08 -0800
committerGarrett D'Amore <garrett@damore.org>2025-01-01 13:15:31 -0800
commit8294f8993d10385e8470ce8270a8bf64047b8b07 (patch)
tree68dc3cd8cd6fb420b2ef97921c1ce94c8ce5d284 /src/supplemental/websocket/base64_test.c
parent61d581e2343af9677815fa7dc13e8a36a6f5ec3e (diff)
downloadnng-8294f8993d10385e8470ce8270a8bf64047b8b07.tar.gz
nng-8294f8993d10385e8470ce8270a8bf64047b8b07.tar.bz2
nng-8294f8993d10385e8470ce8270a8bf64047b8b07.zip
base64: move it to private for websockets
There are no other consumers for this, and reasonably unlikely to be others for now. (Other use cases are JWTs, but that would be another whole set of functionality that we're not ready to take on.)
Diffstat (limited to 'src/supplemental/websocket/base64_test.c')
-rw-r--r--src/supplemental/websocket/base64_test.c108
1 files changed, 108 insertions, 0 deletions
diff --git a/src/supplemental/websocket/base64_test.c b/src/supplemental/websocket/base64_test.c
new file mode 100644
index 00000000..ae8df8e6
--- /dev/null
+++ b/src/supplemental/websocket/base64_test.c
@@ -0,0 +1,108 @@
+//
+// Copyright 2024 Staysail Systems, Inc. <info@staysail.tech>
+// Copyright 2018 Capitar IT Group BV <info@capitar.com>
+//
+// This software is supplied under the terms of the MIT License, a
+// copy of which should be located in the distribution where this
+// file was obtained (LICENSE.txt). A copy of the license may also be
+// found online at https://opensource.org/licenses/MIT.
+//
+
+#include <string.h>
+
+#include <nng/nng.h>
+
+#include "base64.h"
+
+#include <acutest.h>
+
+typedef struct {
+ char *decoded;
+ char *encoded;
+} test_case;
+
+static test_case cases[] = {
+ { "", "" },
+ { "f", "Zg==" },
+ { "fo", "Zm8=" },
+ { "foo", "Zm9v" },
+ { "foob", "Zm9vYg==" },
+ { "fooba", "Zm9vYmE=" },
+ { "foobar", "Zm9vYmFy" },
+ { NULL, NULL },
+};
+
+void
+test_encode(void)
+{
+ int i;
+ void *dec;
+
+ for (i = 0; (dec = cases[i].decoded) != NULL; i++) {
+ char buf[1024];
+ char name[8];
+ int rv;
+
+ (void) snprintf(name, sizeof(name), "%d", i);
+ TEST_CASE(name);
+ rv = (int) nni_base64_encode(dec, strlen(dec), buf, 1024);
+ TEST_CHECK(rv >= 0);
+ TEST_CHECK(rv == (int) strlen(cases[i].encoded));
+ buf[rv] = 0;
+ TEST_CHECK(strcmp(buf, cases[i].encoded) == 0);
+ }
+}
+
+void
+test_decode(void)
+{
+ int i;
+ void *enc;
+
+ for (i = 0; (enc = cases[i].encoded) != NULL; i++) {
+ char buf[1024];
+ char name[8];
+ size_t sz;
+
+ (void) snprintf(name, sizeof(name), "%d", i);
+ TEST_CASE(name);
+
+ sz = nni_base64_decode(enc, strlen(enc), (void *) buf, 1024);
+ TEST_CHECK(sz != (size_t) -1);
+ TEST_CHECK(sz == strlen(cases[i].decoded));
+ buf[sz] = 0;
+ TEST_CHECK(strcmp(buf, cases[i].decoded) == 0);
+ }
+}
+
+void
+test_overflow(void)
+{
+ char tmp[1024];
+ for (int i = 1; cases[i].decoded != NULL; i++) {
+ void *enc = cases[i].encoded;
+ void *dec = cases[i].decoded;
+ void *buf = tmp;
+
+ char name[8];
+ (void) snprintf(name, sizeof(name), "%d", i);
+ TEST_CASE(name);
+
+ TEST_CHECK(nni_base64_encode(dec, strlen(dec), buf,
+ strlen(enc) - 1) == (size_t) -1);
+ TEST_CHECK(nni_base64_encode(dec, strlen(dec), buf, 0) ==
+ (size_t) -1);
+
+ TEST_CHECK(nni_base64_decode(enc, strlen(enc), buf,
+ strlen(dec) - 1) == (size_t) -1);
+ TEST_CHECK(nni_base64_encode(enc, strlen(enc), buf, 0) ==
+ (size_t) -1);
+ }
+}
+
+TEST_LIST = {
+ { "encode", test_encode },
+ { "decode", test_decode },
+ { "overflow", test_overflow },
+ { NULL, NULL },
+};