xref: /freebsd/crypto/heimdal/lib/roken/base64-test.c (revision 6a068746777241722b2b32c5d0bc443a2a64d80b)
14137ff4cSJacques Vidrine /*
2*ae771770SStanislav Sedov  * Copyright (c) 1999 - 2001 Kungliga Tekniska Högskolan
34137ff4cSJacques Vidrine  * (Royal Institute of Technology, Stockholm, Sweden).
44137ff4cSJacques Vidrine  * All rights reserved.
54137ff4cSJacques Vidrine  *
64137ff4cSJacques Vidrine  * Redistribution and use in source and binary forms, with or without
74137ff4cSJacques Vidrine  * modification, are permitted provided that the following conditions
84137ff4cSJacques Vidrine  * are met:
94137ff4cSJacques Vidrine  *
104137ff4cSJacques Vidrine  * 1. Redistributions of source code must retain the above copyright
114137ff4cSJacques Vidrine  *    notice, this list of conditions and the following disclaimer.
124137ff4cSJacques Vidrine  *
134137ff4cSJacques Vidrine  * 2. Redistributions in binary form must reproduce the above copyright
144137ff4cSJacques Vidrine  *    notice, this list of conditions and the following disclaimer in the
154137ff4cSJacques Vidrine  *    documentation and/or other materials provided with the distribution.
164137ff4cSJacques Vidrine  *
174137ff4cSJacques Vidrine  * 3. Neither the name of the Institute nor the names of its contributors
184137ff4cSJacques Vidrine  *    may be used to endorse or promote products derived from this software
194137ff4cSJacques Vidrine  *    without specific prior written permission.
204137ff4cSJacques Vidrine  *
214137ff4cSJacques Vidrine  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
224137ff4cSJacques Vidrine  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
234137ff4cSJacques Vidrine  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
244137ff4cSJacques Vidrine  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
254137ff4cSJacques Vidrine  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
264137ff4cSJacques Vidrine  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
274137ff4cSJacques Vidrine  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
284137ff4cSJacques Vidrine  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
294137ff4cSJacques Vidrine  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
304137ff4cSJacques Vidrine  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
314137ff4cSJacques Vidrine  * SUCH DAMAGE.
324137ff4cSJacques Vidrine  */
334137ff4cSJacques Vidrine 
344137ff4cSJacques Vidrine #include <config.h>
354137ff4cSJacques Vidrine 
36c19800e8SDoug Rabson #include "roken.h"
374137ff4cSJacques Vidrine #include <base64.h>
384137ff4cSJacques Vidrine 
394137ff4cSJacques Vidrine int
main(int argc,char ** argv)404137ff4cSJacques Vidrine main(int argc, char **argv)
414137ff4cSJacques Vidrine {
424137ff4cSJacques Vidrine     int numerr = 0;
434137ff4cSJacques Vidrine     int numtest = 1;
444137ff4cSJacques Vidrine     struct test {
454137ff4cSJacques Vidrine 	void *data;
464137ff4cSJacques Vidrine 	size_t len;
474137ff4cSJacques Vidrine 	const char *result;
484137ff4cSJacques Vidrine     } *t, tests[] = {
494137ff4cSJacques Vidrine 	{ "", 0 , "" },
504137ff4cSJacques Vidrine 	{ "1", 1, "MQ==" },
514137ff4cSJacques Vidrine 	{ "22", 2, "MjI=" },
524137ff4cSJacques Vidrine 	{ "333", 3, "MzMz" },
534137ff4cSJacques Vidrine 	{ "4444", 4, "NDQ0NA==" },
544137ff4cSJacques Vidrine 	{ "55555", 5, "NTU1NTU=" },
554137ff4cSJacques Vidrine 	{ "abc:def", 7, "YWJjOmRlZg==" },
564137ff4cSJacques Vidrine 	{ NULL }
574137ff4cSJacques Vidrine     };
584137ff4cSJacques Vidrine     for(t = tests; t->data; t++) {
594137ff4cSJacques Vidrine 	char *str;
604137ff4cSJacques Vidrine 	int len;
614137ff4cSJacques Vidrine 	len = base64_encode(t->data, t->len, &str);
624137ff4cSJacques Vidrine 	if(strcmp(str, t->result) != 0) {
634137ff4cSJacques Vidrine 	    fprintf(stderr, "failed test %d: %s != %s\n", numtest,
644137ff4cSJacques Vidrine 		    str, t->result);
654137ff4cSJacques Vidrine 	    numerr++;
664137ff4cSJacques Vidrine 	}
674137ff4cSJacques Vidrine 	free(str);
684137ff4cSJacques Vidrine 	str = strdup(t->result);
694137ff4cSJacques Vidrine 	len = base64_decode(t->result, str);
704137ff4cSJacques Vidrine 	if(len != t->len) {
71c19800e8SDoug Rabson 	    fprintf(stderr, "failed test %d: len %lu != %lu\n", numtest,
72c19800e8SDoug Rabson 		    (unsigned long)len, (unsigned long)t->len);
734137ff4cSJacques Vidrine 	    numerr++;
744137ff4cSJacques Vidrine 	} else if(memcmp(str, t->data, t->len) != 0) {
754137ff4cSJacques Vidrine 	    fprintf(stderr, "failed test %d: data\n", numtest);
764137ff4cSJacques Vidrine 	    numerr++;
774137ff4cSJacques Vidrine 	}
784137ff4cSJacques Vidrine 	free(str);
794137ff4cSJacques Vidrine 	numtest++;
804137ff4cSJacques Vidrine     }
814137ff4cSJacques Vidrine 
824137ff4cSJacques Vidrine     {
834137ff4cSJacques Vidrine 	char str[32];
844137ff4cSJacques Vidrine 	if(base64_decode("M=M=", str) != -1) {
854137ff4cSJacques Vidrine 	    fprintf(stderr, "failed test %d: successful decode of `M=M='\n",
864137ff4cSJacques Vidrine 		    numtest++);
874137ff4cSJacques Vidrine 	    numerr++;
884137ff4cSJacques Vidrine 	}
894137ff4cSJacques Vidrine 	if(base64_decode("MQ===", str) != -1) {
904137ff4cSJacques Vidrine 	    fprintf(stderr, "failed test %d: successful decode of `MQ==='\n",
914137ff4cSJacques Vidrine 		    numtest++);
924137ff4cSJacques Vidrine 	    numerr++;
934137ff4cSJacques Vidrine 	}
944137ff4cSJacques Vidrine     }
954137ff4cSJacques Vidrine     return numerr;
964137ff4cSJacques Vidrine }
97