3fda51f |
Jamozed |
2021-02-05 02:10:40 |
0
|
// test_base64.c |
98e5939 |
Jamozed |
2021-11-27 17:53:46 |
1
|
// Base64 unit test for libutil |
3fda51f |
Jamozed |
2021-02-05 02:10:40 |
2
|
// Copyright (C) 2021, Jakob Wakeling |
7f427d9 |
Jamozed |
2022-03-06 12:55:13 |
3
|
// MIT Licence |
3fda51f |
Jamozed |
2021-02-05 02:10:40 |
4
|
|
3fda51f |
Jamozed |
2021-02-05 02:10:40 |
5
|
#include "../base64.h" |
3fda51f |
Jamozed |
2021-02-05 02:10:40 |
6
|
#include "unit.h" |
3fda51f |
Jamozed |
2021-02-05 02:10:40 |
7
|
|
3fda51f |
Jamozed |
2021-02-05 02:10:40 |
8
|
#include <stdint.h> |
3fda51f |
Jamozed |
2021-02-05 02:10:40 |
9
|
#include <stdio.h> |
3fda51f |
Jamozed |
2021-02-05 02:10:40 |
10
|
#include <string.h> |
3fda51f |
Jamozed |
2021-02-05 02:10:40 |
11
|
|
3fda51f |
Jamozed |
2021-02-05 02:10:40 |
12
|
int main(void) { |
3fda51f |
Jamozed |
2021-02-05 02:10:40 |
13
|
{ |
3fda51f |
Jamozed |
2021-02-05 02:10:40 |
14
|
uint8_t i[9] = "123456789"; |
3fda51f |
Jamozed |
2021-02-05 02:10:40 |
15
|
uint8_t o[12] = { 0 }; |
3fda51f |
Jamozed |
2021-02-05 02:10:40 |
16
|
|
3fda51f |
Jamozed |
2021-02-05 02:10:40 |
17
|
b64encode(o, i, 0); |
3fda51f |
Jamozed |
2021-02-05 02:10:40 |
18
|
ASSERT("T000 ENCODE STRING LENGTH 0", |
3fda51f |
Jamozed |
2021-02-05 02:10:40 |
19
|
!memcmp(o, "", 1)); |
3fda51f |
Jamozed |
2021-02-05 02:10:40 |
20
|
|
3fda51f |
Jamozed |
2021-02-05 02:10:40 |
21
|
b64encode(o, i, 1); |
3fda51f |
Jamozed |
2021-02-05 02:10:40 |
22
|
ASSERT("T001 ENCODE STRING LENGTH 1", |
3fda51f |
Jamozed |
2021-02-05 02:10:40 |
23
|
!memcmp(o, "MQ==", 4)); |
3fda51f |
Jamozed |
2021-02-05 02:10:40 |
24
|
|
3fda51f |
Jamozed |
2021-02-05 02:10:40 |
25
|
b64encode(o, i, 2); |
3fda51f |
Jamozed |
2021-02-05 02:10:40 |
26
|
ASSERT("T002 ENCODE STRING LENGTH 2", |
3fda51f |
Jamozed |
2021-02-05 02:10:40 |
27
|
!memcmp(o, "MTI=", 4)); |
3fda51f |
Jamozed |
2021-02-05 02:10:40 |
28
|
|
3fda51f |
Jamozed |
2021-02-05 02:10:40 |
29
|
b64encode(o, i, 3); |
3fda51f |
Jamozed |
2021-02-05 02:10:40 |
30
|
ASSERT("T003 ENCODE STRING LENGTH 3", |
3fda51f |
Jamozed |
2021-02-05 02:10:40 |
31
|
!memcmp(o, "MTIz", 4)); |
3fda51f |
Jamozed |
2021-02-05 02:10:40 |
32
|
|
3fda51f |
Jamozed |
2021-02-05 02:10:40 |
33
|
b64encode(o, i, 4); |
3fda51f |
Jamozed |
2021-02-05 02:10:40 |
34
|
ASSERT("T004 ENCODE STRING LENGTH 4", |
3fda51f |
Jamozed |
2021-02-05 02:10:40 |
35
|
!memcmp(o, "MTIzNA==", 8)); |
3fda51f |
Jamozed |
2021-02-05 02:10:40 |
36
|
|
3fda51f |
Jamozed |
2021-02-05 02:10:40 |
37
|
b64encode(o, i, 5); |
3fda51f |
Jamozed |
2021-02-05 02:10:40 |
38
|
ASSERT("T005 ENCODE STRING LENGTH 5", |
3fda51f |
Jamozed |
2021-02-05 02:10:40 |
39
|
!memcmp(o, "MTIzNDU=", 8)); |
3fda51f |
Jamozed |
2021-02-05 02:10:40 |
40
|
|
3fda51f |
Jamozed |
2021-02-05 02:10:40 |
41
|
b64encode(o, i, 6); |
3fda51f |
Jamozed |
2021-02-05 02:10:40 |
42
|
ASSERT("T006 ENCODE STRING LENGTH 6", |
3fda51f |
Jamozed |
2021-02-05 02:10:40 |
43
|
!memcmp(o, "MTIzNDU2", 8)); |
3fda51f |
Jamozed |
2021-02-05 02:10:40 |
44
|
|
3fda51f |
Jamozed |
2021-02-05 02:10:40 |
45
|
b64encode(o, i, 7); |
3fda51f |
Jamozed |
2021-02-05 02:10:40 |
46
|
ASSERT("T007 ENCODE STRING LENGTH 7", |
3fda51f |
Jamozed |
2021-02-05 02:10:40 |
47
|
!memcmp(o, "MTIzNDU2Nw==", 12)); |
3fda51f |
Jamozed |
2021-02-05 02:10:40 |
48
|
|
3fda51f |
Jamozed |
2021-02-05 02:10:40 |
49
|
b64encode(o, i, 8); |
3fda51f |
Jamozed |
2021-02-05 02:10:40 |
50
|
ASSERT("T008 ENCODE STRING LENGTH 8", |
3fda51f |
Jamozed |
2021-02-05 02:10:40 |
51
|
!memcmp(o, "MTIzNDU2Nzg=", 12)); |
3fda51f |
Jamozed |
2021-02-05 02:10:40 |
52
|
|
3fda51f |
Jamozed |
2021-02-05 02:10:40 |
53
|
b64encode(o, i, 9); |
3fda51f |
Jamozed |
2021-02-05 02:10:40 |
54
|
ASSERT("T009 ENCODE STRING LENGTH 9", |
3fda51f |
Jamozed |
2021-02-05 02:10:40 |
55
|
!memcmp(o, "MTIzNDU2Nzg5", 12)); |
3fda51f |
Jamozed |
2021-02-05 02:10:40 |
56
|
} |
3fda51f |
Jamozed |
2021-02-05 02:10:40 |
57
|
|
3fda51f |
Jamozed |
2021-02-05 02:10:40 |
58
|
{ |
346e60f |
Jamozed |
2021-02-20 20:35:37 |
59
|
uint8_t i[12] = "MTIzNDU2Nw=="; |
346e60f |
Jamozed |
2021-02-20 20:35:37 |
60
|
uint8_t o[10] = { 0 }; |
3fda51f |
Jamozed |
2021-02-05 02:10:40 |
61
|
|
3fda51f |
Jamozed |
2021-02-05 02:10:40 |
62
|
b64decode(o, i, 10); |
346e60f |
Jamozed |
2021-02-20 20:35:37 |
63
|
ASSERT("T010 DECODE STRING LENGTH 10", |
3fda51f |
Jamozed |
2021-02-05 02:10:40 |
64
|
!memcmp(o, "1234567", 7)); |
3fda51f |
Jamozed |
2021-02-05 02:10:40 |
65
|
|
3fda51f |
Jamozed |
2021-02-05 02:10:40 |
66
|
b64decode(o, i, 11); |
346e60f |
Jamozed |
2021-02-20 20:35:37 |
67
|
ASSERT("T011 DECODE STRING LENGTH 10 HALF PADDED", |
3fda51f |
Jamozed |
2021-02-05 02:10:40 |
68
|
!memcmp(o, "1234567", 7)); |
3fda51f |
Jamozed |
2021-02-05 02:10:40 |
69
|
|
3fda51f |
Jamozed |
2021-02-05 02:10:40 |
70
|
b64decode(o, i, 12); |
346e60f |
Jamozed |
2021-02-20 20:35:37 |
71
|
ASSERT("T012 DECODE STRING LENGTH 10 PADDED", |
3fda51f |
Jamozed |
2021-02-05 02:10:40 |
72
|
!memcmp(o, "1234567", 7)); |
3fda51f |
Jamozed |
2021-02-05 02:10:40 |
73
|
} |
3fda51f |
Jamozed |
2021-02-05 02:10:40 |
74
|
|
3fda51f |
Jamozed |
2021-02-05 02:10:40 |
75
|
{ |
346e60f |
Jamozed |
2021-02-20 20:35:37 |
76
|
uint8_t i[12] = "MTIzNDU2Nzg="; |
346e60f |
Jamozed |
2021-02-20 20:35:37 |
77
|
uint8_t o[10] = { 0 }; |
3fda51f |
Jamozed |
2021-02-05 02:10:40 |
78
|
|
3fda51f |
Jamozed |
2021-02-05 02:10:40 |
79
|
b64decode(o, i, 11); |
346e60f |
Jamozed |
2021-02-20 20:35:37 |
80
|
ASSERT("T013 DECODE STRING LENGTH 11", |
3fda51f |
Jamozed |
2021-02-05 02:10:40 |
81
|
!memcmp(o, "12345678", 8)); |
3fda51f |
Jamozed |
2021-02-05 02:10:40 |
82
|
|
3fda51f |
Jamozed |
2021-02-05 02:10:40 |
83
|
b64decode(o, i, 12); |
346e60f |
Jamozed |
2021-02-20 20:35:37 |
84
|
ASSERT("T014 DECODE STRING LENGTH 11 PADDED", |
3fda51f |
Jamozed |
2021-02-05 02:10:40 |
85
|
!memcmp(o, "12345678", 8)); |
3fda51f |
Jamozed |
2021-02-05 02:10:40 |
86
|
} |
3fda51f |
Jamozed |
2021-02-05 02:10:40 |
87
|
|
3fda51f |
Jamozed |
2021-02-05 02:10:40 |
88
|
{ |
346e60f |
Jamozed |
2021-02-20 20:35:37 |
89
|
uint8_t i[12] = "MTIzNDU2Nzg5"; |
346e60f |
Jamozed |
2021-02-20 20:35:37 |
90
|
uint8_t o[10] = { 0 }; |
3fda51f |
Jamozed |
2021-02-05 02:10:40 |
91
|
|
3fda51f |
Jamozed |
2021-02-05 02:10:40 |
92
|
b64decode(o, i, 12); |
346e60f |
Jamozed |
2021-02-20 20:35:37 |
93
|
ASSERT("T015 DECODE STRING LENGTH 12", |
3fda51f |
Jamozed |
2021-02-05 02:10:40 |
94
|
!memcmp(o, "123456789", 9)); |
3fda51f |
Jamozed |
2021-02-05 02:10:40 |
95
|
} |
3fda51f |
Jamozed |
2021-02-05 02:10:40 |
96
|
|
3fda51f |
Jamozed |
2021-02-05 02:10:40 |
97
|
printf("%d of %d tests passed\n", testspassed, testsrun); |
3fda51f |
Jamozed |
2021-02-05 02:10:40 |
98
|
return testsfailed; |
3fda51f |
Jamozed |
2021-02-05 02:10:40 |
99
|
} |
|
|
|
100
|
|