190926Snectar/*
2233294Sstas * Copyright (c) 1999 - 2001 Kungliga Tekniska H��gskolan
390926Snectar * (Royal Institute of Technology, Stockholm, Sweden).
490926Snectar * All rights reserved.
5233294Sstas *
690926Snectar * Redistribution and use in source and binary forms, with or without
790926Snectar * modification, are permitted provided that the following conditions
890926Snectar * are met:
9233294Sstas *
1090926Snectar * 1. Redistributions of source code must retain the above copyright
1190926Snectar *    notice, this list of conditions and the following disclaimer.
12233294Sstas *
1390926Snectar * 2. Redistributions in binary form must reproduce the above copyright
1490926Snectar *    notice, this list of conditions and the following disclaimer in the
1590926Snectar *    documentation and/or other materials provided with the distribution.
16233294Sstas *
1790926Snectar * 3. Neither the name of the Institute nor the names of its contributors
1890926Snectar *    may be used to endorse or promote products derived from this software
1990926Snectar *    without specific prior written permission.
20233294Sstas *
2190926Snectar * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
2290926Snectar * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2390926Snectar * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2490926Snectar * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
2590926Snectar * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2690926Snectar * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2790926Snectar * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2890926Snectar * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2990926Snectar * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3090926Snectar * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3190926Snectar * SUCH DAMAGE.
3290926Snectar */
3390926Snectar
3490926Snectar#include <config.h>
3590926Snectar
36178825Sdfr#include "roken.h"
3790926Snectar#include <base64.h>
3890926Snectar
3990926Snectarint
4090926Snectarmain(int argc, char **argv)
4190926Snectar{
4290926Snectar    int numerr = 0;
4390926Snectar    int numtest = 1;
4490926Snectar    struct test {
4590926Snectar	void *data;
4690926Snectar	size_t len;
4790926Snectar	const char *result;
4890926Snectar    } *t, tests[] = {
4990926Snectar	{ "", 0 , "" },
5090926Snectar	{ "1", 1, "MQ==" },
5190926Snectar	{ "22", 2, "MjI=" },
5290926Snectar	{ "333", 3, "MzMz" },
5390926Snectar	{ "4444", 4, "NDQ0NA==" },
5490926Snectar	{ "55555", 5, "NTU1NTU=" },
5590926Snectar	{ "abc:def", 7, "YWJjOmRlZg==" },
5690926Snectar	{ NULL }
5790926Snectar    };
5890926Snectar    for(t = tests; t->data; t++) {
5990926Snectar	char *str;
6090926Snectar	int len;
6190926Snectar	len = base64_encode(t->data, t->len, &str);
6290926Snectar	if(strcmp(str, t->result) != 0) {
63233294Sstas	    fprintf(stderr, "failed test %d: %s != %s\n", numtest,
6490926Snectar		    str, t->result);
6590926Snectar	    numerr++;
6690926Snectar	}
6790926Snectar	free(str);
6890926Snectar	str = strdup(t->result);
6990926Snectar	len = base64_decode(t->result, str);
7090926Snectar	if(len != t->len) {
71178825Sdfr	    fprintf(stderr, "failed test %d: len %lu != %lu\n", numtest,
72178825Sdfr		    (unsigned long)len, (unsigned long)t->len);
7390926Snectar	    numerr++;
7490926Snectar	} else if(memcmp(str, t->data, t->len) != 0) {
7590926Snectar	    fprintf(stderr, "failed test %d: data\n", numtest);
7690926Snectar	    numerr++;
7790926Snectar	}
7890926Snectar	free(str);
7990926Snectar	numtest++;
8090926Snectar    }
8190926Snectar
8290926Snectar    {
8390926Snectar	char str[32];
8490926Snectar	if(base64_decode("M=M=", str) != -1) {
85233294Sstas	    fprintf(stderr, "failed test %d: successful decode of `M=M='\n",
8690926Snectar		    numtest++);
8790926Snectar	    numerr++;
8890926Snectar	}
8990926Snectar	if(base64_decode("MQ===", str) != -1) {
90233294Sstas	    fprintf(stderr, "failed test %d: successful decode of `MQ==='\n",
9190926Snectar		    numtest++);
9290926Snectar	    numerr++;
9390926Snectar	}
9490926Snectar    }
9590926Snectar    return numerr;
9690926Snectar}
97