From 0bcb57e8581b35c8d1d49284abdb0720607c818b Mon Sep 17 00:00:00 2001 From: Garrett D'Amore Date: Tue, 28 Nov 2017 15:08:33 -0800 Subject: Added base64 implementation, needed by websocket. --- tests/CMakeLists.txt | 1 + tests/base64.c | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+) create mode 100644 tests/base64.c (limited to 'tests') diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 30f76a1d..aa6905f2 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -122,6 +122,7 @@ else () endif () add_nng_test(aio 5) +add_nng_test(base64 5) add_nng_test(bus 5) add_nng_test(files 5) add_nng_test(idhash 5) diff --git a/tests/base64.c b/tests/base64.c new file mode 100644 index 00000000..9682475c --- /dev/null +++ b/tests/base64.c @@ -0,0 +1,88 @@ +// +// Copyright 2017 Staysail Systems, Inc. +// Copyright 2017 Capitar IT Group BV +// +// 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 + +#include "convey.h" + +#include "nng.h" + +#include "supplemental/base64/base64.h" + +typedef struct testcase { + char *decoded; + char *encoded; +} testcase; + +static struct testcase cases[] = { + // clang-format off + { "", "" }, + { "f", "Zg==" }, + { "fo", "Zm8=" }, + { "foo", "Zm9v" }, + { "foob", "Zm9vYg==" }, + { "fooba", "Zm9vYmE=" }, + { "foobar", "Zm9vYmFy" }, + { NULL, NULL } + // clang-format on +}; + +TestMain("Base64 Verification", { + + Convey("Encode Works", { + int rv; + char buf[1024]; + int i; + void *dec; + + for (i = 0; (dec = cases[i].decoded) != NULL; i++) { + rv = nni_base64_encode(dec, strlen(dec), buf, 1024); + So(rv >= 0); + So(rv == strlen(cases[i].encoded)); + buf[rv] = 0; + So(strcmp(buf, cases[i].encoded) == 0); + } + }); + + Convey("Decode Works", { + int rv; + char buf[1024]; + int i; + void *enc; + + for (i = 0; (enc = cases[i].encoded) != NULL; i++) { + rv = nni_base64_decode( + enc, strlen(enc), (void *) buf, 1024); + So(rv >= 0); + So(rv == strlen(cases[i].decoded)); + buf[rv] = 0; + So(strcmp(buf, cases[i].decoded) == 0); + } + }); + + Convey("Overflow Works", { + 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; + + So(nni_base64_encode( + dec, strlen(dec), buf, strlen(enc) - 1) == -1); + So(nni_base64_encode(dec, strlen(dec), buf, 0) == -1); + + So(nni_base64_decode( + enc, strlen(enc), buf, strlen(dec) - 1) == -1); + So(nni_base64_encode(enc, strlen(enc), buf, 0) == -1); + } + + }) + +}); -- cgit v1.2.3-70-g09d2