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-- file download

3e0d5cb Jamozed 2021-02-19 18:06:05
0
// test_base32.c
98e5939 Jamozed 2021-11-27 17:53:46
1
// Base32 unit test for libutil
3e0d5cb Jamozed 2021-02-19 18:06:05
2
// Copyright (C) 2021, Jakob Wakeling
7f427d9 Jamozed 2022-03-06 12:55:13
3
// MIT Licence
3e0d5cb Jamozed 2021-02-19 18:06:05
4
3e0d5cb Jamozed 2021-02-19 18:06:05
5
#include "../base32.h"
3e0d5cb Jamozed 2021-02-19 18:06:05
6
#include "unit.h"
3e0d5cb Jamozed 2021-02-19 18:06:05
7
3e0d5cb Jamozed 2021-02-19 18:06:05
8
#include <stdint.h>
3e0d5cb Jamozed 2021-02-19 18:06:05
9
#include <stdio.h>
3e0d5cb Jamozed 2021-02-19 18:06:05
10
#include <string.h>
3e0d5cb Jamozed 2021-02-19 18:06:05
11
3e0d5cb Jamozed 2021-02-19 18:06:05
12
int main(void) {
3e0d5cb Jamozed 2021-02-19 18:06:05
13
	{
3e0d5cb Jamozed 2021-02-19 18:06:05
14
		uint8_t i[10] = "1234567890";
3e0d5cb Jamozed 2021-02-19 18:06:05
15
		uint8_t o[16] = { 0 };
3e0d5cb Jamozed 2021-02-19 18:06:05
16
		
3e0d5cb Jamozed 2021-02-19 18:06:05
17
		b32encode(o, i, 0);
3e0d5cb Jamozed 2021-02-19 18:06:05
18
		ASSERT("T000 ENCODE STRING LENGTH 0",
3e0d5cb Jamozed 2021-02-19 18:06:05
19
				!memcmp(o, "", 1));
3e0d5cb Jamozed 2021-02-19 18:06:05
20
		
3e0d5cb Jamozed 2021-02-19 18:06:05
21
		b32encode(o, i, 1);
3e0d5cb Jamozed 2021-02-19 18:06:05
22
		ASSERT("T001 ENCODE STRING LENGTH 1",
3e0d5cb Jamozed 2021-02-19 18:06:05
23
				!memcmp(o, "GE======", 8));
3e0d5cb Jamozed 2021-02-19 18:06:05
24
		
3e0d5cb Jamozed 2021-02-19 18:06:05
25
		b32encode(o, i, 2);
3e0d5cb Jamozed 2021-02-19 18:06:05
26
		ASSERT("T002 ENCODE STRING LENGTH 2",
3e0d5cb Jamozed 2021-02-19 18:06:05
27
				!memcmp(o, "GEZA====", 8));
3e0d5cb Jamozed 2021-02-19 18:06:05
28
		
3e0d5cb Jamozed 2021-02-19 18:06:05
29
		b32encode(o, i, 3);
3e0d5cb Jamozed 2021-02-19 18:06:05
30
		ASSERT("T003 ENCODE STRING LENGTH 3",
3e0d5cb Jamozed 2021-02-19 18:06:05
31
				!memcmp(o, "GEZDG===", 8));
3e0d5cb Jamozed 2021-02-19 18:06:05
32
		
3e0d5cb Jamozed 2021-02-19 18:06:05
33
		b32encode(o, i, 4);
3e0d5cb Jamozed 2021-02-19 18:06:05
34
		ASSERT("T004 ENCODE STRING LENGTH 4",
3e0d5cb Jamozed 2021-02-19 18:06:05
35
				!memcmp(o, "GEZDGNA=", 8));
3e0d5cb Jamozed 2021-02-19 18:06:05
36
		
3e0d5cb Jamozed 2021-02-19 18:06:05
37
		b32encode(o, i, 5);
3e0d5cb Jamozed 2021-02-19 18:06:05
38
		ASSERT("T005 ENCODE STRING LENGTH 5",
3e0d5cb Jamozed 2021-02-19 18:06:05
39
				!memcmp(o, "GEZDGNBV", 8));
3e0d5cb Jamozed 2021-02-19 18:06:05
40
		
3e0d5cb Jamozed 2021-02-19 18:06:05
41
		b32encode(o, i, 6);
3e0d5cb Jamozed 2021-02-19 18:06:05
42
		ASSERT("T006 ENCODE STRING LENGTH 6",
3e0d5cb Jamozed 2021-02-19 18:06:05
43
				!memcmp(o, "GEZDGNBVGY======", 16));
3e0d5cb Jamozed 2021-02-19 18:06:05
44
		
3e0d5cb Jamozed 2021-02-19 18:06:05
45
		b32encode(o, i, 7);
3e0d5cb Jamozed 2021-02-19 18:06:05
46
		ASSERT("T007 ENCODE STRING LENGTH 7",
3e0d5cb Jamozed 2021-02-19 18:06:05
47
				!memcmp(o, "GEZDGNBVGY3Q====", 16));
3e0d5cb Jamozed 2021-02-19 18:06:05
48
		
3e0d5cb Jamozed 2021-02-19 18:06:05
49
		b32encode(o, i, 8);
3e0d5cb Jamozed 2021-02-19 18:06:05
50
		ASSERT("T008 ENCODE STRING LENGTH 8",
3e0d5cb Jamozed 2021-02-19 18:06:05
51
				!memcmp(o, "GEZDGNBVGY3TQ===", 16));
3e0d5cb Jamozed 2021-02-19 18:06:05
52
		
3e0d5cb Jamozed 2021-02-19 18:06:05
53
		b32encode(o, i, 9);
3e0d5cb Jamozed 2021-02-19 18:06:05
54
		ASSERT("T009 ENCODE STRING LENGTH 9",
3e0d5cb Jamozed 2021-02-19 18:06:05
55
				!memcmp(o, "GEZDGNBVGY3TQOI=", 16));
3e0d5cb Jamozed 2021-02-19 18:06:05
56
		
3e0d5cb Jamozed 2021-02-19 18:06:05
57
		b32encode(o, i, 10);
3e0d5cb Jamozed 2021-02-19 18:06:05
58
		ASSERT("T010 ENCODE STRING LENGTH 10",
3e0d5cb Jamozed 2021-02-19 18:06:05
59
				!memcmp(o, "GEZDGNBVGY3TQOJQ", 16));
3e0d5cb Jamozed 2021-02-19 18:06:05
60
	}
3e0d5cb Jamozed 2021-02-19 18:06:05
61
	
3e0d5cb Jamozed 2021-02-19 18:06:05
62
	{
7bc6501 Jamozed 2021-02-20 20:37:56
63
		uint8_t i[16] = "GEZDGNBVGY3TQOJQ";
7bc6501 Jamozed 2021-02-20 20:37:56
64
		uint8_t o[10] = { 0 };
3f2ab85 Jamozed 2021-02-19 22:46:54
65
		
3f2ab85 Jamozed 2021-02-19 22:46:54
66
		b32decode(o, i, 8);
3f2ab85 Jamozed 2021-02-19 22:46:54
67
		ASSERT("T021 DECODE STRING LENGTH 8",
3f2ab85 Jamozed 2021-02-19 22:46:54
68
				!memcmp(o, "12345", 5));
3f2ab85 Jamozed 2021-02-19 22:46:54
69
		
3f2ab85 Jamozed 2021-02-19 22:46:54
70
		b32decode(o, i, 16);
3f2ab85 Jamozed 2021-02-19 22:46:54
71
		ASSERT("T022 DECODE STRING LENGTH 16",
3f2ab85 Jamozed 2021-02-19 22:46:54
72
				!memcmp(o, "1234567890", 10));
3f2ab85 Jamozed 2021-02-19 22:46:54
73
	}
3f2ab85 Jamozed 2021-02-19 22:46:54
74
	
3f2ab85 Jamozed 2021-02-19 22:46:54
75
	{
7bc6501 Jamozed 2021-02-20 20:37:56
76
		uint8_t i[16] = "GEZDGNBVGY======";
7bc6501 Jamozed 2021-02-20 20:37:56
77
		uint8_t o[10] = { 0 };
3e0d5cb Jamozed 2021-02-19 18:06:05
78
		
3e0d5cb Jamozed 2021-02-19 18:06:05
79
		b32decode(o, i, 10);
3f2ab85 Jamozed 2021-02-19 22:46:54
80
		ASSERT("T023 DECODE STRING LENGTH 10",
3e0d5cb Jamozed 2021-02-19 18:06:05
81
				!memcmp(o, "123456", 6));
3e0d5cb Jamozed 2021-02-19 18:06:05
82
		
3f2ab85 Jamozed 2021-02-19 22:46:54
83
		b32decode(o, i, 16);
3f2ab85 Jamozed 2021-02-19 22:46:54
84
		ASSERT("T024 DECODE STRING LENGTH 10 PADDED",
3e0d5cb Jamozed 2021-02-19 18:06:05
85
				!memcmp(o, "123456", 6));
3f2ab85 Jamozed 2021-02-19 22:46:54
86
	}
3f2ab85 Jamozed 2021-02-19 22:46:54
87
	
3f2ab85 Jamozed 2021-02-19 22:46:54
88
	{
7bc6501 Jamozed 2021-02-20 20:37:56
89
		uint8_t i[16] = "GEZDGNBVGY3Q====";
7bc6501 Jamozed 2021-02-20 20:37:56
90
		uint8_t o[10] = { 0 };
3e0d5cb Jamozed 2021-02-19 18:06:05
91
		
3e0d5cb Jamozed 2021-02-19 18:06:05
92
		b32decode(o, i, 12);
3f2ab85 Jamozed 2021-02-19 22:46:54
93
		ASSERT("T025 DECODE STRING LENGTH 12",
3f2ab85 Jamozed 2021-02-19 22:46:54
94
				!memcmp(o, "1234567", 7));
3f2ab85 Jamozed 2021-02-19 22:46:54
95
		
3f2ab85 Jamozed 2021-02-19 22:46:54
96
		b32decode(o, i, 16);
3f2ab85 Jamozed 2021-02-19 22:46:54
97
		ASSERT("T026 DECODE STRING LENGTH 12 PADDED",
3f2ab85 Jamozed 2021-02-19 22:46:54
98
				!memcmp(o, "1234567", 7));
3f2ab85 Jamozed 2021-02-19 22:46:54
99
	}
3f2ab85 Jamozed 2021-02-19 22:46:54
100
	
3f2ab85 Jamozed 2021-02-19 22:46:54
101
	{
7bc6501 Jamozed 2021-02-20 20:37:56
102
		uint8_t i[16] = "GEZDGNBVGY3TQ===";
7bc6501 Jamozed 2021-02-20 20:37:56
103
		uint8_t o[10] = { 0 };
3e0d5cb Jamozed 2021-02-19 18:06:05
104
		
3e0d5cb Jamozed 2021-02-19 18:06:05
105
		b32decode(o, i, 13);
3f2ab85 Jamozed 2021-02-19 22:46:54
106
		ASSERT("T027 DECODE STRING LENGTH 13",
3f2ab85 Jamozed 2021-02-19 22:46:54
107
				!memcmp(o, "12345678", 8));
3e0d5cb Jamozed 2021-02-19 18:06:05
108
		
3f2ab85 Jamozed 2021-02-19 22:46:54
109
		b32decode(o, i, 16);
3f2ab85 Jamozed 2021-02-19 22:46:54
110
		ASSERT("T028 DECODE STRING LENGTH 13 PADDED",
3f2ab85 Jamozed 2021-02-19 22:46:54
111
				!memcmp(o, "12345678", 8));
3f2ab85 Jamozed 2021-02-19 22:46:54
112
	}
3f2ab85 Jamozed 2021-02-19 22:46:54
113
	
3f2ab85 Jamozed 2021-02-19 22:46:54
114
	{
7bc6501 Jamozed 2021-02-20 20:37:56
115
		uint8_t i[16] = "GEZDGNBVGY3TQOI=";
7bc6501 Jamozed 2021-02-20 20:37:56
116
		uint8_t o[10] = { 0 };
3e0d5cb Jamozed 2021-02-19 18:06:05
117
		
3e0d5cb Jamozed 2021-02-19 18:06:05
118
		b32decode(o, i, 15);
3f2ab85 Jamozed 2021-02-19 22:46:54
119
		ASSERT("T029 DECODE STRING LENGTH 13",
3f2ab85 Jamozed 2021-02-19 22:46:54
120
				!memcmp(o, "123456789", 9));
3e0d5cb Jamozed 2021-02-19 18:06:05
121
		
3e0d5cb Jamozed 2021-02-19 18:06:05
122
		b32decode(o, i, 16);
3f2ab85 Jamozed 2021-02-19 22:46:54
123
		ASSERT("T030 DECODE STRING LENGTH 13 PADDED",
3f2ab85 Jamozed 2021-02-19 22:46:54
124
				!memcmp(o, "123456789", 9));
3e0d5cb Jamozed 2021-02-19 18:06:05
125
	}
3e0d5cb Jamozed 2021-02-19 18:06:05
126
	
3e0d5cb Jamozed 2021-02-19 18:06:05
127
	printf("%d of %d tests passed\n", testspassed, testsrun);
3e0d5cb Jamozed 2021-02-19 18:06:05
128
	return testsfailed;
3e0d5cb Jamozed 2021-02-19 18:06:05
129
}
130