Created
June 11, 2020 02:20
-
-
Save rhamedy/2ee1217718caf3b8265866a381c2b1b7 to your computer and use it in GitHub Desktop.
Test encoding and decoding outputs of com.sun.jersey.core.util.Base64 with java.util.Base64
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import static org.junit.Assert.*; | |
import com.sun.jersey.core.util.Base64; | |
import org.junit.Test; | |
import java.util.Arrays; | |
import java.util.List; | |
import java.util.stream.Collectors; | |
public class TestEncodingDecoding { | |
List<String> stringsToEncode = Arrays.asList("", " ", "f", "fo", "foo", "foob", "fooba", "foobar", "foobara", "foobaraa"); | |
// encoded strings using https://www.base64encode.org/ | |
// Encoded " ", "f", "fo", ... "foobaraa" | |
List<String> encodedStringsToDecode = Arrays.asList("IA==", "Zg==", "Zm8=", "Zm9v", "Zm9vYg==", "Zm9vYmE=", "Zm9vYmFy", "Zm9vYmFyYQ==", "Zm9vYmFyYWE="); | |
//some urls with special characthers | |
String url1 = "https://www.something.com/"; | |
String url2 = "https://localhost:8080"; | |
String url3 = "https://www.something.com/processes/{id}"; | |
String url4 = "https://www.something.com/processes?a=b"; | |
String url5 = "https://www.something.com/process?a=b&&b=c"; | |
String url6 = "https://www.something.com/something#tab1"; | |
String url7 = "https://www.something.com/process/{id}"; | |
String url8 = "https://www.something.com/process/something-abc"; | |
String url9 = "https://www.something.com/process?_abc=123"; | |
String url10 = "https://www.something.com/process?query=abc.123"; | |
String url11 = "https://www.something.com/process/123/abc.xyz"; | |
String url12 = "https://[email protected]/process/123/abc.xyz"; | |
String url13 = "https://localhost:8080?query=<abc>&&filter=aa@bb"; | |
String url14 = "https://localhost:8080/test?query=abc,xyz"; | |
String url15 = "https://localhost:8080/abc/abc$456"; | |
String url16 = "https://localhost:8080/processes/1abc*"; | |
String url17 = "https://localhost:8080/something?redirect_uri=dXNlcm5hbWU6cGFzc3dvcmQ="; | |
String url18 = "https://localhost:8080/something/abc;xyz"; | |
String url19 = "https://localhost:8080/something?query=abc;xyz**"; | |
String url20 = "192.168.0.1:8080"; | |
String url21 = "http://localhost:8080/abc?query=abc123\\lkjasdf"; | |
String url22 = "http://localhost:8080/abc?query=abc123&&xyz=(lksadf)"; | |
String url23 = "http://localhost:8080/abc?query=abc123&&xyz=[lksadf]"; | |
String url24 = "http://localhost:8080/abc?query=abc123&&xyz={lksadf}"; | |
String url25 = "http://localhost:8080/abc?query=lkajsf\""; | |
String url26 = "http://localhost:8080/abc?query=testSingleQuote'asdf"; | |
String url27 = "http://localhost:8080/abc?query=abc123\\/asdf"; | |
String url28 = "localhost:8080/abc/< > / \\\\ \\\\\"':;&?@%#"; | |
List<String> urls = Arrays.asList( | |
url1, | |
url2, | |
url3, | |
url4, | |
url5, | |
url6, | |
url7, | |
url8, | |
url9, | |
url10, | |
url11, | |
url12, | |
url13, | |
url14, | |
url15, | |
url16, | |
url17, | |
url18, | |
url19, | |
url20, | |
url21, | |
url22, | |
url23, | |
url24, | |
url25, | |
url26, | |
url27, | |
url28); | |
@Test | |
public void test_encoding_0char_to_8chars_including_empty_and_space() { | |
stringsToEncode.forEach(str -> { | |
assertTrue(java.util.Base64.getEncoder().encodeToString(str.getBytes()).equals(new String(Base64.encode(str)))); | |
}); | |
} | |
@Test | |
public void test_decoding_0char_to_8chars_including_empty_and_space() { | |
encodedStringsToDecode.forEach(encodedStr -> { | |
assertTrue(new String(java.util.Base64.getDecoder().decode(encodedStr.getBytes())).equals(new String(Base64.decode(encodedStr)))); | |
}); | |
} | |
@Test | |
public void test_encoding_url_with_special_characters_in_them() { | |
urls.forEach(url -> { | |
assertTrue(java.util.Base64.getEncoder().encodeToString(url.getBytes()).equals(new String(Base64.encode(url)))); | |
}); | |
} | |
@Test | |
public void test_decoding_url_with_special_characters_in_them() { | |
List<String> jerseyBasedEncoding = urls.stream().map(url -> new String(Base64.encode(url))).collect(Collectors.toList()); | |
List<String> javaUtilBasedEncoding = urls.stream().map(url -> new String(java.util.Base64.getEncoder().encode(url.getBytes()))) | |
.collect(Collectors.toList()); | |
jerseyBasedEncoding.stream().forEach(entry -> { | |
assertTrue(urls.contains(new String(Base64.decode(entry)))); | |
assertTrue(urls.contains(new String(java.util.Base64.getDecoder().decode(entry)))); | |
}); | |
javaUtilBasedEncoding.stream().forEach(entry -> { | |
assertTrue(urls.contains(new String(Base64.decode(entry)))); | |
assertTrue(urls.contains(new String(java.util.Base64.getDecoder().decode(entry)))); | |
}); | |
} | |
@Test | |
public void test_encode_all_special_chars_in_url() { | |
String urlWithSpecialChars = "http://localhost:8080/\\/(){}<>%#$?abc=235^asdf*[]@&-_;,\"'"; | |
// Used https://www.base64encode.org/ | |
String encodedUrlWithSpecialChars = "aHR0cDovL2xvY2FsaG9zdDo4MDgwL1wvKCl7fTw+JSMkP2FiYz0yMzVeYXNkZipbXUAmLV87LCIn"; | |
assertTrue(new String(java.util.Base64.getEncoder().encode(urlWithSpecialChars.getBytes())).equals(new String(Base64.encode(urlWithSpecialChars.getBytes())))); | |
assertTrue(new String(java.util.Base64.getDecoder().decode(encodedUrlWithSpecialChars.getBytes())).equals(new String(Base64.decode(encodedUrlWithSpecialChars.getBytes())))); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment