forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBase64Test.java
More file actions
183 lines (155 loc) · 7.02 KB
/
Base64Test.java
File metadata and controls
183 lines (155 loc) · 7.02 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package com.thealgorithms.conversions;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
/**
* Test cases for Base64 encoding and decoding.
*
* Author: Nithin U.
* Github: https://github.com/NithinU2802
*/
class Base64Test {
@Test
void testBase64Alphabet() {
// Test that all Base64 characters are handled correctly
String allChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
String encoded = Base64.encode(allChars);
String decoded = Base64.decodeToString(encoded);
assertEquals(allChars, decoded);
}
@ParameterizedTest
@CsvSource({"'', ''", "A, QQ==", "AB, QUI=", "ABC, QUJD", "ABCD, QUJDRA==", "Hello, SGVsbG8=", "'Hello World', SGVsbG8gV29ybGQ=", "'Hello, World!', 'SGVsbG8sIFdvcmxkIQ=='", "'The quick brown fox jumps over the lazy dog', 'VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5IGRvZw=='",
"123456789, MTIzNDU2Nzg5", "'Base64 encoding test', 'QmFzZTY0IGVuY29kaW5nIHRlc3Q='"})
void
testStringEncoding(String input, String expected) {
assertEquals(expected, Base64.encode(input));
}
@ParameterizedTest
@CsvSource({"'', ''", "QQ==, A", "QUI=, AB", "QUJD, ABC", "QUJDRA==, ABCD", "SGVsbG8=, Hello", "'SGVsbG8gV29ybGQ=', 'Hello World'", "'SGVsbG8sIFdvcmxkIQ==', 'Hello, World!'", "'VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5IGRvZw==', 'The quick brown fox jumps over the lazy dog'",
"MTIzNDU2Nzg5, 123456789", "'QmFzZTY0IGVuY29kaW5nIHRlc3Q=', 'Base64 encoding test'"})
void
testStringDecoding(String input, String expected) {
assertEquals(expected, Base64.decodeToString(input));
}
@Test
void testByteArrayEncoding() {
byte[] input = {72, 101, 108, 108, 111};
String expected = "SGVsbG8=";
assertEquals(expected, Base64.encode(input));
}
@Test
void testByteArrayDecoding() {
String input = "SGVsbG8=";
byte[] expected = {72, 101, 108, 108, 111};
assertArrayEquals(expected, Base64.decode(input));
}
@Test
void testRoundTripEncoding() {
String[] testStrings = {"", "A", "AB", "ABC", "Hello, World!", "The quick brown fox jumps over the lazy dog", "1234567890", "Special chars: !@#$%^&*()_+-=[]{}|;:,.<>?",
"Unicode: வணக்கம்", // Tamil for "Hello"
"Multi-line\nstring\rwith\tdifferent\nwhitespace"};
for (String original : testStrings) {
String encoded = Base64.encode(original);
String decoded = Base64.decodeToString(encoded);
assertEquals(original, decoded, "Round trip failed for: " + original);
}
}
@Test
void testRoundTripByteArrayEncoding() {
byte[][] testArrays = {{}, {0}, {-1}, {0, 1, 2, 3, 4, 5}, {-128, -1, 0, 1, 127}, {72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33}};
for (byte[] original : testArrays) {
String encoded = Base64.encode(original);
byte[] decoded = Base64.decode(encoded);
assertArrayEquals(original, decoded, "Round trip failed for byte array");
}
}
@Test
void testBinaryData() {
// Test with binary data that might contain null bytes
byte[] binaryData = new byte[256];
for (int i = 0; i < 256; i++) {
binaryData[i] = (byte) i;
}
String encoded = Base64.encode(binaryData);
byte[] decoded = Base64.decode(encoded);
assertArrayEquals(binaryData, decoded);
}
@Test
void testNullInputEncoding() {
assertThrows(IllegalArgumentException.class, () -> Base64.encode((String) null));
assertThrows(IllegalArgumentException.class, () -> Base64.encode((byte[]) null));
}
@Test
void testNullInputDecoding() {
assertThrows(IllegalArgumentException.class, () -> Base64.decode(null));
assertThrows(IllegalArgumentException.class, () -> Base64.decodeToString(null));
}
@Test
void testInvalidBase64Characters() {
assertThrows(IllegalArgumentException.class, () -> Base64.decode("SGVsbG8@"));
assertThrows(IllegalArgumentException.class, () -> Base64.decode("SGVsbG8#"));
assertThrows(IllegalArgumentException.class, () -> Base64.decode("SGVsbG8$"));
assertThrows(IllegalArgumentException.class, () -> Base64.decode("SGVsbG8%"));
}
@Test
void testInvalidLength() {
// Length must be multiple of 4
assertThrows(IllegalArgumentException.class, () -> Base64.decode("Q"));
assertThrows(IllegalArgumentException.class, () -> Base64.decode("QQ"));
assertThrows(IllegalArgumentException.class, () -> Base64.decode("QQQ"));
}
@Test
void testInvalidPaddingPosition() {
// '=' can only appear at the end
assertThrows(IllegalArgumentException.class, () -> Base64.decode("Q=QQ"));
assertThrows(IllegalArgumentException.class, () -> Base64.decode("Q=Q="));
assertThrows(IllegalArgumentException.class, () -> Base64.decode("=QQQ"));
}
@Test
void testPaddingVariations() {
// Test different padding scenarios '='
assertEquals("A", Base64.decodeToString("QQ=="));
assertEquals("AB", Base64.decodeToString("QUI="));
assertEquals("ABC", Base64.decodeToString("QUJD"));
}
@Test
void testPaddingConsistency() {
// Ensure that strings requiring different amounts of padding encode/decode correctly
String[] testCases = {"A", "AB", "ABC", "ABCD", "ABCDE", "ABCDEF"};
for (String test : testCases) {
String encoded = Base64.encode(test);
String decoded = Base64.decodeToString(encoded);
assertEquals(test, decoded);
// Verify padding is correct
int expectedPadding = (3 - (test.length() % 3)) % 3;
int actualPadding = 0;
for (int i = encoded.length() - 1; i >= 0 && encoded.charAt(i) == '='; i--) {
actualPadding++;
}
assertEquals(expectedPadding, actualPadding, "Incorrect padding for: " + test);
}
}
@Test
void testLargeData() {
// Test with larger data to ensure scalability
StringBuilder largeString = new StringBuilder();
for (int i = 0; i < 1000; i++) {
largeString.append("This is a test string for Base64 encoding. ");
}
String original = largeString.toString();
String encoded = Base64.encode(original);
String decoded = Base64.decodeToString(encoded);
assertEquals(original, decoded);
}
@Test
void testEmptyAndSingleCharacter() {
// Test edge cases
assertEquals("", Base64.encode(""));
assertEquals("", Base64.decodeToString(""));
assertEquals("QQ==", Base64.encode("A"));
assertEquals("A", Base64.decodeToString("QQ=="));
}
}