libutil

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

libutil/src/test/test_base32.c (131 lines, 2.8 KiB) -rw-r--r-- blame download

0123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
// test_base32.c
// Base32 unit test for libutil
// Copyright (C) 2021, Jakob Wakeling
// MIT Licence

#include "../base32.h"
#include "unit.h"

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

int main(void) {
	{
		uint8_t i[10] = "1234567890";
		uint8_t o[16] = { 0 };
		
		b32encode(o, i, 0);
		ASSERT("T000 ENCODE STRING LENGTH 0",
				!memcmp(o, "", 1));
		
		b32encode(o, i, 1);
		ASSERT("T001 ENCODE STRING LENGTH 1",
				!memcmp(o, "GE======", 8));
		
		b32encode(o, i, 2);
		ASSERT("T002 ENCODE STRING LENGTH 2",
				!memcmp(o, "GEZA====", 8));
		
		b32encode(o, i, 3);
		ASSERT("T003 ENCODE STRING LENGTH 3",
				!memcmp(o, "GEZDG===", 8));
		
		b32encode(o, i, 4);
		ASSERT("T004 ENCODE STRING LENGTH 4",
				!memcmp(o, "GEZDGNA=", 8));
		
		b32encode(o, i, 5);
		ASSERT("T005 ENCODE STRING LENGTH 5",
				!memcmp(o, "GEZDGNBV", 8));
		
		b32encode(o, i, 6);
		ASSERT("T006 ENCODE STRING LENGTH 6",
				!memcmp(o, "GEZDGNBVGY======", 16));
		
		b32encode(o, i, 7);
		ASSERT("T007 ENCODE STRING LENGTH 7",
				!memcmp(o, "GEZDGNBVGY3Q====", 16));
		
		b32encode(o, i, 8);
		ASSERT("T008 ENCODE STRING LENGTH 8",
				!memcmp(o, "GEZDGNBVGY3TQ===", 16));
		
		b32encode(o, i, 9);
		ASSERT("T009 ENCODE STRING LENGTH 9",
				!memcmp(o, "GEZDGNBVGY3TQOI=", 16));
		
		b32encode(o, i, 10);
		ASSERT("T010 ENCODE STRING LENGTH 10",
				!memcmp(o, "GEZDGNBVGY3TQOJQ", 16));
	}
	
	{
		uint8_t i[16] = "GEZDGNBVGY3TQOJQ";
		uint8_t o[10] = { 0 };
		
		b32decode(o, i, 8);
		ASSERT("T021 DECODE STRING LENGTH 8",
				!memcmp(o, "12345", 5));
		
		b32decode(o, i, 16);
		ASSERT("T022 DECODE STRING LENGTH 16",
				!memcmp(o, "1234567890", 10));
	}
	
	{
		uint8_t i[16] = "GEZDGNBVGY======";
		uint8_t o[10] = { 0 };
		
		b32decode(o, i, 10);
		ASSERT("T023 DECODE STRING LENGTH 10",
				!memcmp(o, "123456", 6));
		
		b32decode(o, i, 16);
		ASSERT("T024 DECODE STRING LENGTH 10 PADDED",
				!memcmp(o, "123456", 6));
	}
	
	{
		uint8_t i[16] = "GEZDGNBVGY3Q====";
		uint8_t o[10] = { 0 };
		
		b32decode(o, i, 12);
		ASSERT("T025 DECODE STRING LENGTH 12",
				!memcmp(o, "1234567", 7));
		
		b32decode(o, i, 16);
		ASSERT("T026 DECODE STRING LENGTH 12 PADDED",
				!memcmp(o, "1234567", 7));
	}
	
	{
		uint8_t i[16] = "GEZDGNBVGY3TQ===";
		uint8_t o[10] = { 0 };
		
		b32decode(o, i, 13);
		ASSERT("T027 DECODE STRING LENGTH 13",
				!memcmp(o, "12345678", 8));
		
		b32decode(o, i, 16);
		ASSERT("T028 DECODE STRING LENGTH 13 PADDED",
				!memcmp(o, "12345678", 8));
	}
	
	{
		uint8_t i[16] = "GEZDGNBVGY3TQOI=";
		uint8_t o[10] = { 0 };
		
		b32decode(o, i, 15);
		ASSERT("T029 DECODE STRING LENGTH 13",
				!memcmp(o, "123456789", 9));
		
		b32decode(o, i, 16);
		ASSERT("T030 DECODE STRING LENGTH 13 PADDED",
				!memcmp(o, "123456789", 9));
	}
	
	printf("%d of %d tests passed\n", testspassed, testsrun);
	return testsfailed;
}