-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase64_test.c
More file actions
79 lines (63 loc) · 2.1 KB
/
base64_test.c
File metadata and controls
79 lines (63 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include "base64.h"
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
void run_test(const char *input, bool url_safe) {
size_t input_length = strlen(input);
int encoded_length = base64_encoded_length(input_length);
printf("Input: %s\n", input);
printf("Input length: %zu\n", input_length);
printf("Calculated encoded length: %d\n", encoded_length);
// Allocate memory for encoded and decoded strings
char encoded[encoded_length + 1]; // +1 for null terminator
char decoded[input_length + 1]; // +1 for null terminator
// Encode
if (base64_encode(encoded, input, input_length, url_safe) != 0) {
printf("Encoding failed for input: %s\n", input);
return;
}
printf("Input: %s\n", input);
printf("Encoded (%s): %s\n", url_safe ? "URL-safe" : "Standard", encoded);
// Decode
int decoded_length =
base64_decoded_length(encoded, strlen(encoded), url_safe);
if (decoded_length == -1) {
printf("Invalid Base64 string: %s\n", encoded);
return;
}
if (base64_decode(decoded, encoded, strlen(encoded), url_safe) == -1) {
printf("Decoding failed for encoded string: %s\n", encoded);
return;
}
decoded[decoded_length] = '\0'; // Null-terminate the decoded string
printf("Decoded: %s\n", decoded);
// Verify correctness
if (strcmp(input, decoded) == 0) {
printf("Test passed!\n");
} else {
printf("Test failed! Decoded output does not match the original input.\n");
}
printf("\n");
}
int main() {
// Test cases
const char *test_cases[] = {
"Hello, World!",
"Base64 encoding test.",
"This is a longer test string to check encoding and decoding.",
"Special characters: !@#$%^&*()_+-=[]{}|;':,.<>/?",
"",
"A",
"AB",
"ABC"};
size_t num_tests = sizeof(test_cases) / sizeof(test_cases[0]);
printf("=== Testing Standard Base64 ===\n");
for (size_t i = 0; i < num_tests; i++) {
run_test(test_cases[i], false); // Standard Base64
}
printf("=== Testing URL-Safe Base64 ===\n");
for (size_t i = 0; i < num_tests; i++) {
run_test(test_cases[i], true); // URL-Safe Base64
}
return 0;
}