|
| 1 | +package com.thealgorithms.conversions; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertArrayEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 6 | + |
| 7 | +import org.junit.jupiter.api.Test; |
| 8 | +import org.junit.jupiter.params.ParameterizedTest; |
| 9 | +import org.junit.jupiter.params.provider.CsvSource; |
| 10 | + |
| 11 | +/** |
| 12 | + * Test cases for Base64 encoding and decoding. |
| 13 | + * |
| 14 | + * Author: Nithin U. |
| 15 | + * Github: https://github.com/NithinU2802 |
| 16 | + */ |
| 17 | + |
| 18 | +class Base64Test { |
| 19 | + |
| 20 | + @Test |
| 21 | + void testBase64Alphabet() { |
| 22 | + // Test that all Base64 characters are handled correctly |
| 23 | + String allChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
| 24 | + String encoded = Base64.encode(allChars); |
| 25 | + String decoded = Base64.decodeToString(encoded); |
| 26 | + assertEquals(allChars, decoded); |
| 27 | + } |
| 28 | + |
| 29 | + @ParameterizedTest |
| 30 | + @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=='", |
| 31 | + "123456789, MTIzNDU2Nzg5", "'Base64 encoding test', 'QmFzZTY0IGVuY29kaW5nIHRlc3Q='"}) |
| 32 | + void |
| 33 | + testStringEncoding(String input, String expected) { |
| 34 | + assertEquals(expected, Base64.encode(input)); |
| 35 | + } |
| 36 | + |
| 37 | + @ParameterizedTest |
| 38 | + @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'", |
| 39 | + "MTIzNDU2Nzg5, 123456789", "'QmFzZTY0IGVuY29kaW5nIHRlc3Q=', 'Base64 encoding test'"}) |
| 40 | + void |
| 41 | + testStringDecoding(String input, String expected) { |
| 42 | + assertEquals(expected, Base64.decodeToString(input)); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + void testByteArrayEncoding() { |
| 47 | + byte[] input = {72, 101, 108, 108, 111}; |
| 48 | + String expected = "SGVsbG8="; |
| 49 | + assertEquals(expected, Base64.encode(input)); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + void testByteArrayDecoding() { |
| 54 | + String input = "SGVsbG8="; |
| 55 | + byte[] expected = {72, 101, 108, 108, 111}; |
| 56 | + assertArrayEquals(expected, Base64.decode(input)); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + void testRoundTripEncoding() { |
| 61 | + String[] testStrings = {"", "A", "AB", "ABC", "Hello, World!", "The quick brown fox jumps over the lazy dog", "1234567890", "Special chars: !@#$%^&*()_+-=[]{}|;:,.<>?", |
| 62 | + "Unicode: வணக்கம்", // Tamil for "Hello" |
| 63 | + "Multi-line\nstring\rwith\tdifferent\nwhitespace"}; |
| 64 | + |
| 65 | + for (String original : testStrings) { |
| 66 | + String encoded = Base64.encode(original); |
| 67 | + String decoded = Base64.decodeToString(encoded); |
| 68 | + assertEquals(original, decoded, "Round trip failed for: " + original); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + void testRoundTripByteArrayEncoding() { |
| 74 | + 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}}; |
| 75 | + |
| 76 | + for (byte[] original : testArrays) { |
| 77 | + String encoded = Base64.encode(original); |
| 78 | + byte[] decoded = Base64.decode(encoded); |
| 79 | + assertArrayEquals(original, decoded, "Round trip failed for byte array"); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + void testBinaryData() { |
| 85 | + // Test with binary data that might contain null bytes |
| 86 | + byte[] binaryData = new byte[256]; |
| 87 | + for (int i = 0; i < 256; i++) { |
| 88 | + binaryData[i] = (byte) i; |
| 89 | + } |
| 90 | + |
| 91 | + String encoded = Base64.encode(binaryData); |
| 92 | + byte[] decoded = Base64.decode(encoded); |
| 93 | + assertArrayEquals(binaryData, decoded); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + void testNullInputEncoding() { |
| 98 | + assertThrows(IllegalArgumentException.class, () -> Base64.encode((String) null)); |
| 99 | + assertThrows(IllegalArgumentException.class, () -> Base64.encode((byte[]) null)); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + void testNullInputDecoding() { |
| 104 | + assertThrows(IllegalArgumentException.class, () -> Base64.decode(null)); |
| 105 | + assertThrows(IllegalArgumentException.class, () -> Base64.decodeToString(null)); |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + void testInvalidBase64Characters() { |
| 110 | + assertThrows(IllegalArgumentException.class, () -> Base64.decode("SGVsbG8@")); |
| 111 | + assertThrows(IllegalArgumentException.class, () -> Base64.decode("SGVsbG8#")); |
| 112 | + assertThrows(IllegalArgumentException.class, () -> Base64.decode("SGVsbG8$")); |
| 113 | + assertThrows(IllegalArgumentException.class, () -> Base64.decode("SGVsbG8%")); |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + void testInvalidLength() { |
| 118 | + // Length must be multiple of 4 |
| 119 | + assertThrows(IllegalArgumentException.class, () -> Base64.decode("Q")); |
| 120 | + assertThrows(IllegalArgumentException.class, () -> Base64.decode("QQ")); |
| 121 | + assertThrows(IllegalArgumentException.class, () -> Base64.decode("QQQ")); |
| 122 | + } |
| 123 | + |
| 124 | + @Test |
| 125 | + void testInvalidPaddingPosition() { |
| 126 | + // '=' can only appear at the end |
| 127 | + assertThrows(IllegalArgumentException.class, () -> Base64.decode("Q=QQ")); |
| 128 | + assertThrows(IllegalArgumentException.class, () -> Base64.decode("Q=Q=")); |
| 129 | + assertThrows(IllegalArgumentException.class, () -> Base64.decode("=QQQ")); |
| 130 | + } |
| 131 | + |
| 132 | + @Test |
| 133 | + void testPaddingVariations() { |
| 134 | + // Test different padding scenarios '=' |
| 135 | + assertEquals("A", Base64.decodeToString("QQ==")); |
| 136 | + assertEquals("AB", Base64.decodeToString("QUI=")); |
| 137 | + assertEquals("ABC", Base64.decodeToString("QUJD")); |
| 138 | + } |
| 139 | + |
| 140 | + @Test |
| 141 | + void testPaddingConsistency() { |
| 142 | + // Ensure that strings requiring different amounts of padding encode/decode correctly |
| 143 | + String[] testCases = {"A", "AB", "ABC", "ABCD", "ABCDE", "ABCDEF"}; |
| 144 | + |
| 145 | + for (String test : testCases) { |
| 146 | + String encoded = Base64.encode(test); |
| 147 | + String decoded = Base64.decodeToString(encoded); |
| 148 | + assertEquals(test, decoded); |
| 149 | + |
| 150 | + // Verify padding is correct |
| 151 | + int expectedPadding = (3 - (test.length() % 3)) % 3; |
| 152 | + int actualPadding = 0; |
| 153 | + for (int i = encoded.length() - 1; i >= 0 && encoded.charAt(i) == '='; i--) { |
| 154 | + actualPadding++; |
| 155 | + } |
| 156 | + assertEquals(expectedPadding, actualPadding, "Incorrect padding for: " + test); |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + @Test |
| 161 | + void testLargeData() { |
| 162 | + // Test with larger data to ensure scalability |
| 163 | + StringBuilder largeString = new StringBuilder(); |
| 164 | + for (int i = 0; i < 1000; i++) { |
| 165 | + largeString.append("This is a test string for Base64 encoding. "); |
| 166 | + } |
| 167 | + |
| 168 | + String original = largeString.toString(); |
| 169 | + String encoded = Base64.encode(original); |
| 170 | + String decoded = Base64.decodeToString(encoded); |
| 171 | + assertEquals(original, decoded); |
| 172 | + } |
| 173 | + |
| 174 | + @Test |
| 175 | + void testEmptyAndSingleCharacter() { |
| 176 | + // Test edge cases |
| 177 | + assertEquals("", Base64.encode("")); |
| 178 | + assertEquals("", Base64.decodeToString("")); |
| 179 | + |
| 180 | + assertEquals("QQ==", Base64.encode("A")); |
| 181 | + assertEquals("A", Base64.decodeToString("QQ==")); |
| 182 | + } |
| 183 | +} |
0 commit comments