Path: blob/main/crypto/heimdal/lib/roken/base64-test.c
39507 views
/*1* Copyright (c) 1999 - 2001 Kungliga Tekniska Högskolan2* (Royal Institute of Technology, Stockholm, Sweden).3* All rights reserved.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8*9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11*12* 2. Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in the14* documentation and/or other materials provided with the distribution.15*16* 3. Neither the name of the Institute nor the names of its contributors17* may be used to endorse or promote products derived from this software18* without specific prior written permission.19*20* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND21* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE22* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE23* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE24* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL25* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS26* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)27* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT28* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY29* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF30* SUCH DAMAGE.31*/3233#include <config.h>3435#include "roken.h"36#include <base64.h>3738int39main(int argc, char **argv)40{41int numerr = 0;42int numtest = 1;43struct test {44void *data;45size_t len;46const char *result;47} *t, tests[] = {48{ "", 0 , "" },49{ "1", 1, "MQ==" },50{ "22", 2, "MjI=" },51{ "333", 3, "MzMz" },52{ "4444", 4, "NDQ0NA==" },53{ "55555", 5, "NTU1NTU=" },54{ "abc:def", 7, "YWJjOmRlZg==" },55{ NULL }56};57for(t = tests; t->data; t++) {58char *str;59int len;60len = base64_encode(t->data, t->len, &str);61if(strcmp(str, t->result) != 0) {62fprintf(stderr, "failed test %d: %s != %s\n", numtest,63str, t->result);64numerr++;65}66free(str);67str = strdup(t->result);68len = base64_decode(t->result, str);69if(len != t->len) {70fprintf(stderr, "failed test %d: len %lu != %lu\n", numtest,71(unsigned long)len, (unsigned long)t->len);72numerr++;73} else if(memcmp(str, t->data, t->len) != 0) {74fprintf(stderr, "failed test %d: data\n", numtest);75numerr++;76}77free(str);78numtest++;79}8081{82char str[32];83if(base64_decode("M=M=", str) != -1) {84fprintf(stderr, "failed test %d: successful decode of `M=M='\n",85numtest++);86numerr++;87}88if(base64_decode("MQ===", str) != -1) {89fprintf(stderr, "failed test %d: successful decode of `MQ==='\n",90numtest++);91numerr++;92}93}94return numerr;95}969798