use of java.util.Base64 in project j2objc by google.
the class Base64Test method testDecoder_extraChars_mime.
public void testDecoder_extraChars_mime() throws Exception {
Decoder mimeDecoder = Base64.getMimeDecoder();
// Characters outside alphabet before padding.
assertEquals("hello, world", decodeToAscii(mimeDecoder, " aGVsbG8sIHdvcmxk"));
assertEquals("hello, world", decodeToAscii(mimeDecoder, "aGV sbG8sIHdvcmxk"));
assertEquals("hello, world", decodeToAscii(mimeDecoder, "aGVsbG8sIHdvcmxk "));
assertEquals("hello, world", decodeToAscii(mimeDecoder, "_aGVsbG8sIHdvcmxk"));
assertEquals("hello, world", decodeToAscii(mimeDecoder, "aGV_sbG8sIHdvcmxk"));
assertEquals("hello, world", decodeToAscii(mimeDecoder, "aGVsbG8sIHdvcmxk_"));
assertEquals("hello, world", decodeToAscii(mimeDecoder, "*aGVsbG8sIHdvcmxk"));
assertEquals("hello, world", decodeToAscii(mimeDecoder, "aGV*sbG8sIHdvcmxk"));
assertEquals("hello, world", decodeToAscii(mimeDecoder, "aGVsbG8sIHdvcmxk*"));
assertEquals("hello, world", decodeToAscii(mimeDecoder, "\r\naGVsbG8sIHdvcmxk"));
assertEquals("hello, world", decodeToAscii(mimeDecoder, "aGV\r\nsbG8sIHdvcmxk"));
assertEquals("hello, world", decodeToAscii(mimeDecoder, "aGVsbG8sIHdvcmxk\r\n"));
assertEquals("hello, world", decodeToAscii(mimeDecoder, "\naGVsbG8sIHdvcmxk"));
assertEquals("hello, world", decodeToAscii(mimeDecoder, "aGV\nsbG8sIHdvcmxk"));
assertEquals("hello, world", decodeToAscii(mimeDecoder, "aGVsbG8sIHdvcmxk\n"));
// padding 0
assertEquals("hello, world", decodeToAscii(mimeDecoder, "aGVsbG8sIHdvcmxk"));
// Extra padding
assertDecodeThrowsIAe(mimeDecoder, "aGVsbG8sIHdvcmxk=");
assertDecodeThrowsIAe(mimeDecoder, "aGVsbG8sIHdvcmxk==");
// Characters outside alphabet intermixed with (too much) padding.
assertDecodeThrowsIAe(mimeDecoder, "aGVsbG8sIHdvcmxk =");
assertDecodeThrowsIAe(mimeDecoder, "aGVsbG8sIHdvcmxk = = ");
// padding 1
assertEquals("hello, world?!", decodeToAscii(mimeDecoder, "aGVsbG8sIHdvcmxkPyE="));
// Missing padding
assertEquals("hello, world?!", decodeToAscii(mimeDecoder, "aGVsbG8sIHdvcmxkPyE"));
// Characters outside alphabet before padding.
assertEquals("hello, world?!", decodeToAscii(mimeDecoder, "aGVsbG8sIHdvcmxkPyE ="));
assertEquals("hello, world?!", decodeToAscii(mimeDecoder, "aGVsbG8sIHdvcmxkPyE*="));
// Trailing characters, otherwise valid.
assertEquals("hello, world?!", decodeToAscii(mimeDecoder, "aGVsbG8sIHdvcmxkPyE= "));
assertEquals("hello, world?!", decodeToAscii(mimeDecoder, "aGVsbG8sIHdvcmxkPyE=*"));
assertDecodeThrowsIAe(mimeDecoder, "aGVsbG8sIHdvcmxkPyE=X");
assertDecodeThrowsIAe(mimeDecoder, "aGVsbG8sIHdvcmxkPyE=XY");
assertDecodeThrowsIAe(mimeDecoder, "aGVsbG8sIHdvcmxkPyE=XYZ");
assertDecodeThrowsIAe(mimeDecoder, "aGVsbG8sIHdvcmxkPyE=XYZA");
assertEquals("hello, world?!", decodeToAscii(mimeDecoder, "aGVsbG8sIHdvcmxkPyE=\n"));
assertEquals("hello, world?!", decodeToAscii(mimeDecoder, "aGVsbG8sIHdvcmxkPyE=\r\n"));
assertEquals("hello, world?!", decodeToAscii(mimeDecoder, "aGVsbG8sIHdvcmxkPyE= "));
assertEquals("hello, world?!", decodeToAscii(mimeDecoder, "aGVsbG8sIHdvcmxkPyE=="));
// Characters outside alphabet intermixed with (too much) padding.
assertEquals("hello, world?!", decodeToAscii(mimeDecoder, "aGVsbG8sIHdvcmxkPyE =="));
assertEquals("hello, world?!", decodeToAscii(mimeDecoder, "aGVsbG8sIHdvcmxkPyE = = "));
// padding 2
assertEquals("hello, world.", decodeToAscii(mimeDecoder, "aGVsbG8sIHdvcmxkLg=="));
// Missing padding
assertEquals("hello, world.", decodeToAscii(mimeDecoder, "aGVsbG8sIHdvcmxkLg"));
// Partially missing padding
assertDecodeThrowsIAe(mimeDecoder, "aGVsbG8sIHdvcmxkLg=");
// Characters outside alphabet before padding.
assertEquals("hello, world.", decodeToAscii(mimeDecoder, "aGVsbG8sIHdvcmxkLg =="));
assertEquals("hello, world.", decodeToAscii(mimeDecoder, "aGVsbG8sIHdvcmxkLg*=="));
// Trailing characters, otherwise valid.
assertEquals("hello, world.", decodeToAscii(mimeDecoder, "aGVsbG8sIHdvcmxkLg== "));
assertEquals("hello, world.", decodeToAscii(mimeDecoder, "aGVsbG8sIHdvcmxkLg==*"));
assertDecodeThrowsIAe(mimeDecoder, "aGVsbG8sIHdvcmxkLg==X");
assertDecodeThrowsIAe(mimeDecoder, "aGVsbG8sIHdvcmxkLg==XY");
assertDecodeThrowsIAe(mimeDecoder, "aGVsbG8sIHdvcmxkLg==XYZ");
assertDecodeThrowsIAe(mimeDecoder, "aGVsbG8sIHdvcmxkLg==XYZA");
assertEquals("hello, world.", decodeToAscii(mimeDecoder, "aGVsbG8sIHdvcmxkLg==\n"));
assertEquals("hello, world.", decodeToAscii(mimeDecoder, "aGVsbG8sIHdvcmxkLg==\r\n"));
assertEquals("hello, world.", decodeToAscii(mimeDecoder, "aGVsbG8sIHdvcmxkLg== "));
assertEquals("hello, world.", decodeToAscii(mimeDecoder, "aGVsbG8sIHdvcmxkLg==="));
// Characters outside alphabet inside padding are not allowed by the MIME decoder.
assertDecodeThrowsIAe(mimeDecoder, "aGVsbG8sIHdvcmxkLg= =");
assertDecodeThrowsIAe(mimeDecoder, "aGVsbG8sIHdvcmxkLg=*=");
assertDecodeThrowsIAe(mimeDecoder, "aGVsbG8sIHdvcmxkLg=\r\n=");
// Characters inside alphabet inside padding.
assertDecodeThrowsIAe(mimeDecoder, "aGVsbG8sIHdvcmxkLg=X=");
}
use of java.util.Base64 in project j2objc by google.
the class Base64Test method testDecoder_nonPrintableBytes_url.
/**
* Check decoding sample non-ASCII byte[] values from a {@link #TABLE_2}
* (url safe) encoded String.
*/
public void testDecoder_nonPrintableBytes_url() throws Exception {
Decoder decoder = Base64.getUrlDecoder();
assertArrayPrefixEquals(SAMPLE_NON_ASCII_BYTES, 0, decoder.decode(""));
assertArrayPrefixEquals(SAMPLE_NON_ASCII_BYTES, 1, decoder.decode("_w=="));
assertArrayPrefixEquals(SAMPLE_NON_ASCII_BYTES, 2, decoder.decode("_-4="));
assertArrayPrefixEquals(SAMPLE_NON_ASCII_BYTES, 3, decoder.decode("_-7d"));
assertArrayPrefixEquals(SAMPLE_NON_ASCII_BYTES, 4, decoder.decode("_-7dzA=="));
assertArrayPrefixEquals(SAMPLE_NON_ASCII_BYTES, 5, decoder.decode("_-7dzLs="));
assertArrayPrefixEquals(SAMPLE_NON_ASCII_BYTES, 6, decoder.decode("_-7dzLuq"));
assertArrayPrefixEquals(SAMPLE_NON_ASCII_BYTES, 7, decoder.decode("_-7dzLuqmQ=="));
assertArrayPrefixEquals(SAMPLE_NON_ASCII_BYTES, 8, decoder.decode("_-7dzLuqmYg="));
}
use of java.util.Base64 in project j2objc by google.
the class Base64Test method testRoundTrip_allBytes_mime_lineLength_isRoundedDown.
/**
* checks that the specified line length is rounded down to the nearest multiple of 4.
*/
public void testRoundTrip_allBytes_mime_lineLength_isRoundedDown() {
Decoder decoder = Base64.getMimeDecoder();
byte[] separator = new byte[] { '\r', '\n' };
checkRoundTrip_allBytes(Base64.getMimeEncoder(60, separator), decoder, wrapLines("\r\n", ALL_BYTE_VALUES_ENCODED, 60));
checkRoundTrip_allBytes(Base64.getMimeEncoder(63, separator), decoder, wrapLines("\r\n", ALL_BYTE_VALUES_ENCODED, 60));
checkRoundTrip_allBytes(Base64.getMimeEncoder(10, separator), decoder, wrapLines("\r\n", ALL_BYTE_VALUES_ENCODED, 8));
}
use of java.util.Base64 in project jdk8u_jdk by JetBrains.
the class TestBase64 method testMalformedPadding.
private static void testMalformedPadding() throws Throwable {
Object[] data = new Object[] { // illegal ending unit
"$=#", // illegal ending unit
"", // illegal ending unit
0, // dangling single byte
"A", // dangling single byte
"", // dangling single byte
0, "A=", "", 0, "A==", "", 0, "QUJDA", "ABC", 4, "QUJDA=", "ABC", 4, "QUJDA==", "ABC", 4, // unnecessary padding
"=", // unnecessary padding
"", // unnecessary padding
0, //"ABC".encode() -> "QUJD"
"QUJD=", //"ABC".encode() -> "QUJD"
"ABC", //"ABC".encode() -> "QUJD"
4, // incomplete padding
"AA=", // incomplete padding
"", // incomplete padding
0, "QQ=", "", 0, // incorrect padding
"QQ=N", // incorrect padding
"", // incorrect padding
0, "QQ=?", "", 0, "QUJDQQ=", "ABC", 4, "QUJDQQ=N", "ABC", 4, "QUJDQQ=?", "ABC", 4 };
Base64.Decoder[] decs = new Base64.Decoder[] { Base64.getDecoder(), Base64.getUrlDecoder(), Base64.getMimeDecoder() };
for (Base64.Decoder dec : decs) {
for (int i = 0; i < data.length; i += 3) {
final String srcStr = (String) data[i];
final byte[] srcBytes = srcStr.getBytes("ASCII");
final ByteBuffer srcBB = ByteBuffer.wrap(srcBytes);
byte[] expected = ((String) data[i + 1]).getBytes("ASCII");
int pos = (Integer) data[i + 2];
// decode(byte[])
checkIAE(() -> dec.decode(srcBytes));
// decode(String)
checkIAE(() -> dec.decode(srcStr));
// decode(ByteBuffer)
checkIAE(() -> dec.decode(srcBB));
// wrap stream
checkIOE(new Testable() {
public void test() throws IOException {
try (InputStream is = dec.wrap(new ByteArrayInputStream(srcBytes))) {
while (is.read() != -1) ;
}
}
});
}
}
}
use of java.util.Base64 in project riposte by Nike-Inc.
the class BasicAuthSecurityValidator method validateSecureRequestForEndpoint.
@Override
public void validateSecureRequestForEndpoint(RequestInfo<?> requestInfo, Endpoint<?> endpoint) {
String authorizationHeader = requestInfo.getHeaders().get("Authorization");
if (authorizationHeader == null) {
throw new Unauthorized401Exception("Missing authorization header.", requestInfo.getPath(), null);
}
final String[] authSplit = authorizationHeader.split(" ");
if (authSplit.length != 2 || !"Basic".equals(authSplit[0])) {
throw new Unauthorized401Exception("Authorization header does not contain Basic", requestInfo.getPath(), authorizationHeader);
}
Base64.Decoder decoder = Base64.getDecoder();
byte[] decodedBytes;
try {
decodedBytes = decoder.decode(authSplit[1]);
} catch (IllegalArgumentException ex) {
throw new Unauthorized401Exception("Malformed Authorization header (not Base64 encoded), caused by: " + ex.toString(), requestInfo.getPath(), authorizationHeader);
}
String pair = new String(decodedBytes);
String[] userDetails = pair.split(":", 2);
if (userDetails.length != 2) {
throw new Unauthorized401Exception("Malformed Authorization header.", requestInfo.getPath(), authorizationHeader);
}
String username = userDetails[0];
String password = userDetails[1];
if (!username.equals(expectedUsername) || !password.equals(expectedPassword)) {
throw new Unauthorized401Exception("Invalid username or password", requestInfo.getPath(), authorizationHeader);
}
}
Aggregations