--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/base64.c Mon Nov 07 19:32:17 2016 +0100 @@ -0,0 +1,157 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2016 Olaf Wintermann. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "base64.h" + +#include <ucx/string.h> +#include <libidav/utils.h> + +UCX_TEST(test_util_base64decode) { + char *s1 = util_base64decode("YWJj"); + char *s2 = util_base64decode("aGVsbG8gd29ybGQ="); + char *s3 = util_base64decode("MA=="); + char *s4 = util_base64decode("MHh4MQ=="); + + UCX_TEST_BEGIN; + + UCX_TEST_ASSERT(!strcmp(s1, "abc"), "s1 wrong"); + UCX_TEST_ASSERT(!strcmp(s2, "hello world"), "s2 wrong"); + UCX_TEST_ASSERT(!strcmp(s3, "0"), "s3 wrong"); + UCX_TEST_ASSERT(!strcmp(s4, "0xx1"), "s4 wrong"); + + UCX_TEST_END; + + free(s1); + free(s2); + free(s3); + free(s4); +} + +UCX_TEST(test_util_base64decode_len) { + int len1, len2, len3, len4, len5, len6; + char *s1 = util_base64decode_len("aGVsbG8=", &len1); // 5 + char *s2 = util_base64decode_len("MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5", &len2); // 30 + char *s3 = util_base64decode_len("Lg==", &len3); // 1 + char *s4 = util_base64decode_len("bG9s", &len4); // 3 + char *s5 = util_base64decode_len("YWJjLS0tLS0tM3g=", &len5); // 11 + char *s6 = util_base64decode_len("YWJjZGVmZy4uLjs7MGhlbGxv", &len6); // 18 + + UCX_TEST_BEGIN; + + UCX_TEST_ASSERT(!strcmp(s1, "hello"), "s1 wrong"); + UCX_TEST_ASSERT(len1 == 5, "len1 wrong"); + + UCX_TEST_ASSERT(!strcmp(s2, "012345678901234567890123456789"), "s2 wrong"); + UCX_TEST_ASSERT(len2 == 30, "len2 wrong"); + + UCX_TEST_ASSERT(!strcmp(s3, "."), "s3 wrong"); + UCX_TEST_ASSERT(len3 == 1, "len3 wrong"); + + UCX_TEST_ASSERT(!strcmp(s4, "lol"), "s4 wrong"); + UCX_TEST_ASSERT(len4 == 3, "len4 wrong"); + + UCX_TEST_ASSERT(!strcmp(s5, "abc------3x"), "s5 wrong"); + UCX_TEST_ASSERT(len5 == 11, "len5 wrong"); + + UCX_TEST_ASSERT(!strcmp(s6, "abcdefg...;;0hello"), "s6 failed"); + UCX_TEST_ASSERT(len6 == 18, "len6 wrong"); + + UCX_TEST_END; + + free(s1); + free(s2); + free(s3); + free(s4); + free(s5); + free(s6); +} + +UCX_TEST(test_util_base64encode) { + char *str1 = "hello world"; + char *str2 = "test string"; + char *str3 = "01234567890123456789012345678901234567890123456789"; + char str4[8]; + str4[0] = 0; + str4[1] = 12; + str4[2] = 3; + str4[3] = 50; + str4[4] = 2; + str4[5] = 110; + str4[6] = 63; + str4[7] = 1; + + char *str5 = "a"; + char *str6 = "ab"; + char *str7 = "abc"; + char *str8 = "abcd"; + char *str9 = "abcde"; + char *str10 = "abcdef"; + + char *b1 = util_base64encode(str1, strlen(str1)); + char *b2 = util_base64encode(str2, strlen(str2)); + char *b3 = util_base64encode(str3, strlen(str3)); + char *b4 = util_base64encode(str4, 8); + char *b5 = util_base64encode(str5, strlen(str5)); + char *b6 = util_base64encode(str6, strlen(str6)); + char *b7 = util_base64encode(str7, strlen(str7)); + char *b8 = util_base64encode(str8, strlen(str8)); + char *b9 = util_base64encode(str9, strlen(str9)); + char *b10 = util_base64encode(str10, strlen(str10)); + + UCX_TEST_BEGIN; + + UCX_TEST_ASSERT(!strcmp(b1, "aGVsbG8gd29ybGQ="), "b1 failed"); + UCX_TEST_ASSERT(!strcmp(b2, "dGVzdCBzdHJpbmc="), "b2 failed"); + UCX_TEST_ASSERT(!strcmp(b3, "MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODk="), "b3 failed"); + UCX_TEST_ASSERT(!strcmp(b4, "AAwDMgJuPwE="), "b4 failed"); + + UCX_TEST_ASSERT(!strcmp(b5, "YQ=="), "b5 failed"); + UCX_TEST_ASSERT(!strcmp(b6, "YWI="), "b6 failed"); + UCX_TEST_ASSERT(!strcmp(b7, "YWJj"), "b7 failed"); + UCX_TEST_ASSERT(!strcmp(b8, "YWJjZA=="), "b8 failed"); + UCX_TEST_ASSERT(!strcmp(b9, "YWJjZGU="), "b9 failed"); + UCX_TEST_ASSERT(!strcmp(b10, "YWJjZGVm"), "b10 failed"); + + UCX_TEST_END; + + free(b1); + free(b2); + free(b3); + free(b4); + free(b5); + free(b6); + free(b7); + free(b8); + free(b9); + free(b10); +} +