Author | Jamozed <[email protected]> |
Date | 2021-02-04 13:10:40 |
Commit | 3fda51f0607d3ad97c1eb68740b4f06ff5fc8b22 |
Parent | 0becaf1f146b7414edc56190f1352093c8a264c0 |
base64: Add base64 unit test
Diffstat
A | src/test/test_base64.c | | | 152 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1 files changed, 152 insertions, 0 deletions
diff --git a/src/test/test_base64.c b/src/test/test_base64.c new file mode 100644 index 0000000..48172f0 --- /dev/null +++ b/src/test/test_base64.c @@ -0,0 +1,152 @@ +// test_base64.c +// Base64 unit test for OMKOV lib +// Copyright (C) 2021, Jakob Wakeling +// All rights reserved. + +/* +OMKOV Public Domain Licence, version 1.0 + +Permission is hereby granted to deal with this software and its associated +documentation files without restriction. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT +HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE SOFTWARE. +*/ + +#include "../base64.h" +#include "unit.h" + +#include <stdint.h> +#include <stdio.h> +#include <string.h> + +int main(void) { + { + uint8_t i[9] = "123456789"; + uint8_t o[12] = { 0 }; + + b64encode(o, i, 0); + ASSERT("T000 ENCODE STRING LENGTH 0", + !memcmp(o, "", 1)); + + b64encode(o, i, 1); + ASSERT("T001 ENCODE STRING LENGTH 1", + !memcmp(o, "MQ==", 4)); + + b64encode(o, i, 2); + ASSERT("T002 ENCODE STRING LENGTH 2", + !memcmp(o, "MTI=", 4)); + + b64encode(o, i, 3); + ASSERT("T003 ENCODE STRING LENGTH 3", + !memcmp(o, "MTIz", 4)); + + b64encode(o, i, 4); + ASSERT("T004 ENCODE STRING LENGTH 4", + !memcmp(o, "MTIzNA==", 8)); + + b64encode(o, i, 5); + ASSERT("T005 ENCODE STRING LENGTH 5", + !memcmp(o, "MTIzNDU=", 8)); + + b64encode(o, i, 6); + ASSERT("T006 ENCODE STRING LENGTH 6", + !memcmp(o, "MTIzNDU2", 8)); + + b64encode(o, i, 7); + ASSERT("T007 ENCODE STRING LENGTH 7", + !memcmp(o, "MTIzNDU2Nw==", 12)); + + b64encode(o, i, 8); + ASSERT("T008 ENCODE STRING LENGTH 8", + !memcmp(o, "MTIzNDU2Nzg=", 12)); + + b64encode(o, i, 9); + ASSERT("T009 ENCODE STRING LENGTH 9", + !memcmp(o, "MTIzNDU2Nzg5", 12)); + } + + { + uint8_t i[9] = { 0x9B, 0x65, 0x37, 0xF9, 0x9C, 0x88, 0xF0, 0x4D, 0x66 }; + uint8_t o[12] = { 0 }; + + b64encode(o, i, 1); + ASSERT("T010 ENCODE RANDOM DATA LENGTH 1", + !memcmp(o, "mw==", 4)); + + b64encode(o, i, 2); + ASSERT("T011 ENCODE RANDOM DATA LENGTH 2", + !memcmp(o, "m2U=", 4)); + + b64encode(o, i, 3); + ASSERT("T012 ENCODE RANDOM DATA LENGTH 3", + !memcmp(o, "m2U3", 4)); + + b64encode(o, i, 4); + ASSERT("T013 ENCODE RANDOM DATA LENGTH 4", + !memcmp(o, "m2U3+Q==", 8)); + + b64encode(o, i, 5); + ASSERT("T014 ENCODE RANDOM DATA LENGTH 5", + !memcmp(o, "m2U3+Zw=", 8)); + + b64encode(o, i, 6); + ASSERT("T015 ENCODE RANDOM DATA LENGTH 6", + !memcmp(o, "m2U3+ZyI", 8)); + + b64encode(o, i, 7); + ASSERT("T016 ENCODE RANDOM DATA LENGTH 7", + !memcmp(o, "m2U3+ZyI8A==", 12)); + + b64encode(o, i, 8); + ASSERT("T017 ENCODE RANDOM DATA LENGTH 8", + !memcmp(o, "m2U3+ZyI8E0=", 12)); + + b64encode(o, i, 9); + ASSERT("T018 ENCODE RANDOM DATA LENGTH 9", + !memcmp(o, "m2U3+ZyI8E1m", 12)); + } + + { + uint8_t i[12] = "MTIzNDU2Nw=="; uint8_t o[10] = { 0 }; + + b64decode(o, i, 10); + ASSERT("T019 DECODE STRING LENGTH 10", + !memcmp(o, "1234567", 7)); + + b64decode(o, i, 11); + ASSERT("T020 DECODE STRING LENGTH 10 HALF PADDED", + !memcmp(o, "1234567", 7)); + + b64decode(o, i, 12); + ASSERT("T021 DECODE STRING LENGTH 10 PADDED", + !memcmp(o, "1234567", 7)); + } + + { + uint8_t i[12] = "MTIzNDU2Nzg="; uint8_t o[10] = { 0 }; + + b64decode(o, i, 11); + ASSERT("T023 DECODE STRING LENGTH 11", + !memcmp(o, "12345678", 8)); + + b64decode(o, i, 12); + ASSERT("T024 DECODE STRING LENGTH 10 PADDED", + !memcmp(o, "12345678", 8)); + } + + { + uint8_t i[12] = "MTIzNDU2Nzg5"; uint8_t o[10] = { 0 }; + + b64decode(o, i, 12); + ASSERT("T027 DECODE STRING LENGTH 12", + !memcmp(o, "123456789", 9)); + } + + printf("%d of %d tests passed\n", testspassed, testsrun); + return testsfailed; +}