libutil

C Utility Library
git clone http://git.omkov.net/libutil
Log | Tree | Refs | README | LICENCE | Download

libutil/src/test/test_base64.c (101 lines, 2.1 KiB) -rw-r--r-- blame download

0123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
// test_base64.c
// Base64 unit test for libutil
// Copyright (C) 2021, Jakob Wakeling
// MIT Licence

#include "../base64.h"
#include "unit.h"

#include <stdint.h>
#include <stdio.h>
#include <string.h>

int main(void) {
	{
		uint8_t i[9] = "123456789";
		uint8_t o[12] = { 0 };
		
		b64encode(o, i, 0);
		ASSERT("T000 ENCODE STRING LENGTH 0",
				!memcmp(o, "", 1));
		
		b64encode(o, i, 1);
		ASSERT("T001 ENCODE STRING LENGTH 1",
				!memcmp(o, "MQ==", 4));
		
		b64encode(o, i, 2);
		ASSERT("T002 ENCODE STRING LENGTH 2",
				!memcmp(o, "MTI=", 4));
		
		b64encode(o, i, 3);
		ASSERT("T003 ENCODE STRING LENGTH 3",
				!memcmp(o, "MTIz", 4));
		
		b64encode(o, i, 4);
		ASSERT("T004 ENCODE STRING LENGTH 4",
				!memcmp(o, "MTIzNA==", 8));
		
		b64encode(o, i, 5);
		ASSERT("T005 ENCODE STRING LENGTH 5",
				!memcmp(o, "MTIzNDU=", 8));
		
		b64encode(o, i, 6);
		ASSERT("T006 ENCODE STRING LENGTH 6",
				!memcmp(o, "MTIzNDU2", 8));
		
		b64encode(o, i, 7);
		ASSERT("T007 ENCODE STRING LENGTH 7",
				!memcmp(o, "MTIzNDU2Nw==", 12));
		
		b64encode(o, i, 8);
		ASSERT("T008 ENCODE STRING LENGTH 8",
				!memcmp(o, "MTIzNDU2Nzg=", 12));
		
		b64encode(o, i, 9);
		ASSERT("T009 ENCODE STRING LENGTH 9",
				!memcmp(o, "MTIzNDU2Nzg5", 12));
	}
	
	{
		uint8_t i[12] = "MTIzNDU2Nw==";
		uint8_t o[10] = { 0 };
		
		b64decode(o, i, 10);
		ASSERT("T010 DECODE STRING LENGTH 10",
				!memcmp(o, "1234567", 7));
		
		b64decode(o, i, 11);
		ASSERT("T011 DECODE STRING LENGTH 10 HALF PADDED",
				!memcmp(o, "1234567", 7));
		
		b64decode(o, i, 12);
		ASSERT("T012 DECODE STRING LENGTH 10 PADDED",
				!memcmp(o, "1234567", 7));
	}
	
	{
		uint8_t i[12] = "MTIzNDU2Nzg=";
		uint8_t o[10] = { 0 };
		
		b64decode(o, i, 11);
		ASSERT("T013 DECODE STRING LENGTH 11",
				!memcmp(o, "12345678", 8));
		
		b64decode(o, i, 12);
		ASSERT("T014 DECODE STRING LENGTH 11 PADDED",
				!memcmp(o, "12345678", 8));
	}
	
	{
		uint8_t i[12] = "MTIzNDU2Nzg5";
		uint8_t o[10] = { 0 };
		
		b64decode(o, i, 12);
		ASSERT("T015 DECODE STRING LENGTH 12",
				!memcmp(o, "123456789", 9));
	}
	
	printf("%d of %d tests passed\n", testspassed, testsrun);
	return testsfailed;
}