aboutsummaryrefslogtreecommitdiff
path: root/tools/server/test_base64.c
diff options
context:
space:
mode:
authorOnur Gündüz <ogunduz@gmail.com>2026-06-20 20:10:56 +0200
committerOnur Gündüz <ogunduz@gmail.com>2026-06-21 05:57:19 +0200
commit6b969d25ce136eaf695222bd1a10bff1ecac0220 (patch)
treec23afb50c28b9deaddae5c6526a74ee11cf2d865 /tools/server/test_base64.c
parentd4bdb8dae753462f621507d5fc3582e0e53da35c (diff)
downloadffwd-6b969d25ce136eaf695222bd1a10bff1ecac0220.tar.gz
ffwd-6b969d25ce136eaf695222bd1a10bff1ecac0220.zip
all: split Makefile and reorg library/tools
Diffstat (limited to 'tools/server/test_base64.c')
-rw-r--r--tools/server/test_base64.c51
1 files changed, 51 insertions, 0 deletions
diff --git a/tools/server/test_base64.c b/tools/server/test_base64.c
new file mode 100644
index 0000000..3519c0c
--- /dev/null
+++ b/tools/server/test_base64.c
@@ -0,0 +1,51 @@
+/* test_base64.c - base64_encode against RFC 4648 vectors and binary inputs. */
+
+#include "base64.h"
+#include "test_util.h"
+
+#include <stdlib.h>
+#include <string.h>
+
+static void test_base64_rfc4648(void) {
+ /* RFC 4648 test vectors. */
+ static const struct {
+ const char *in, *out;
+ } cases[] = {
+ {"", ""},
+ {"f", "Zg=="},
+ {"fo", "Zm8="},
+ {"foo", "Zm9v"},
+ {"foob", "Zm9vYg=="},
+ {"fooba", "Zm9vYmE="},
+ {"foobar", "Zm9vYmFy"},
+ };
+ for (size_t i = 0; i < sizeof(cases) / sizeof(cases[0]); i++) {
+ char *got = base64_encode((const unsigned char *)cases[i].in, strlen(cases[i].in));
+ TEST_ASSERT(strcmp(got, cases[i].out) == 0);
+ free(got);
+ }
+}
+
+static void test_base64_binary_vectors(void) {
+ static const struct {
+ unsigned char bytes[5];
+ size_t n;
+ const char *out;
+ } cases[] = {
+ {{0xff}, 1, "/w=="},
+ {{0xff, 0xee}, 2, "/+4="},
+ {{0xff, 0xee, 0xdd}, 3, "/+7d"},
+ {{0x00, 0x01, 0x02, 0x03, 0x04}, 5, "AAECAwQ="},
+ };
+ for (size_t i = 0; i < sizeof(cases) / sizeof(cases[0]); i++) {
+ char *got = base64_encode(cases[i].bytes, cases[i].n);
+ TEST_ASSERT(strcmp(got, cases[i].out) == 0);
+ free(got);
+ }
+}
+
+int main(void) {
+ test_base64_rfc4648();
+ test_base64_binary_vectors();
+ return TEST_REPORT("base64");
+}