Path: blob/main/crypto/krb5/src/util/support/t_base64.c
34889 views
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */1/* util/support/t_base64.c - base64 encoding and decoding tests */2/*3* Copyright (c) 1999 - 2001 Kungliga Tekniska Högskolan4* (Royal Institute of Technology, Stockholm, Sweden).5* All rights reserved.6*7* Redistribution and use in source and binary forms, with or without8* modification, are permitted provided that the following conditions9* are met:10*11* 1. Redistributions of source code must retain the above copyright12* notice, this list of conditions and the following disclaimer.13*14* 2. Redistributions in binary form must reproduce the above copyright15* notice, this list of conditions and the following disclaimer in the16* documentation and/or other materials provided with the distribution.17*18* 3. Neither the name of the Institute nor the names of its contributors19* may be used to endorse or promote products derived from this software20* without specific prior written permission.21*22* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND23* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE24* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE25* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE26* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL27* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS28* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)29* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT30* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY31* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF32* SUCH DAMAGE.33*/3435#include <k5-platform.h>36#include <k5-base64.h>3738static struct test {39void *data;40size_t len;41const char *result;42} tests[] = {43{ "", 0 , "" },44{ "1", 1, "MQ==" },45{ "22", 2, "MjI=" },46{ "333", 3, "MzMz" },47{ "4444", 4, "NDQ0NA==" },48{ "55555", 5, "NTU1NTU=" },49{ "abc:def", 7, "YWJjOmRlZg==" },50{ "f", 1, "Zg==" },51{ "fo", 2, "Zm8=" },52{ "foo", 3, "Zm9v" },53{ "foob", 4, "Zm9vYg==" },54{ "fooba", 5, "Zm9vYmE=" },55{ "foobar", 6, "Zm9vYmFy" },56{ NULL, 0, NULL }57};5859static char *negative_tests[] = {60"M=M=",61"MM=M",62"MQ===",63"====",64"M===",65NULL66};6768int69main(int argc, char **argv)70{71char *str, **ntest;72void *data;73int numerr = 0, numtest = 1;74const struct test *t;75size_t len;7677for (t = tests; t->data != NULL; t++) {78str = k5_base64_encode(t->data, t->len);79if (strcmp(str, t->result) != 0) {80fprintf(stderr, "failed test %d: %s != %s\n", numtest,81str, t->result);82numerr++;83}84free(str);85data = k5_base64_decode(t->result, &len);86if (len != t->len) {87fprintf(stderr, "failed test %d: len %lu != %lu\n", numtest,88(unsigned long)len, (unsigned long)t->len);89numerr++;90} else if (memcmp(data, t->data, t->len) != 0) {91fprintf(stderr, "failed test %d: data\n", numtest);92numerr++;93}94free(data);95numtest++;96}9798for (ntest = negative_tests; *ntest != NULL; ntest++) {99data = k5_base64_decode(*ntest, &len);100if (data != NULL || len != SIZE_MAX) {101fprintf(stderr, "failed test %d: successful decode: %s\n",102numtest, *ntest);103numerr++;104}105numtest++;106}107108return numerr ? 1 : 0;109}110111112