use of java.util.Base64.Decoder 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.Decoder 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);
}
}
use of java.util.Base64.Decoder in project j2objc by google.
the class Base64Test method testRoundTrip_allBytes_mime_singleLine.
/**
* Checks that if the lineSeparator is empty or the line length is {@code <= 3}
* or larger than the data to be encoded, a single line is returned.
*/
public void testRoundTrip_allBytes_mime_singleLine() {
Decoder decoder = Base64.getMimeDecoder();
checkRoundTrip_allBytes_singleLine(Base64.getMimeEncoder(76, new byte[0]), decoder);
// Line lengths <= 3 mean no wrapping; the separator is ignored in that case.
byte[] separator = new byte[] { '*' };
checkRoundTrip_allBytes_singleLine(Base64.getMimeEncoder(Integer.MIN_VALUE, separator), decoder);
checkRoundTrip_allBytes_singleLine(Base64.getMimeEncoder(-1, separator), decoder);
checkRoundTrip_allBytes_singleLine(Base64.getMimeEncoder(0, separator), decoder);
checkRoundTrip_allBytes_singleLine(Base64.getMimeEncoder(1, separator), decoder);
checkRoundTrip_allBytes_singleLine(Base64.getMimeEncoder(2, separator), decoder);
checkRoundTrip_allBytes_singleLine(Base64.getMimeEncoder(3, separator), decoder);
// output fits into the permitted line length
checkRoundTrip_allBytes_singleLine(Base64.getMimeEncoder(ALL_BYTE_VALUES_ENCODED.length(), separator), decoder);
checkRoundTrip_allBytes_singleLine(Base64.getMimeEncoder(Integer.MAX_VALUE, separator), decoder);
}
use of java.util.Base64.Decoder in project j2objc by google.
the class Base64Test method testRoundTrip_wrap_url.
public void testRoundTrip_wrap_url() throws Exception {
Encoder encoder = Base64.getUrlEncoder();
Decoder decoder = Base64.getUrlDecoder();
checkRoundTrip_wrapInputStream(encoder, decoder);
}
use of java.util.Base64.Decoder in project j2objc by google.
the class Base64Test method testRoundtrip_wrap_mime.
public void testRoundtrip_wrap_mime() throws Exception {
Encoder encoder = Base64.getMimeEncoder();
Decoder decoder = Base64.getMimeDecoder();
checkRoundTrip_wrapInputStream(encoder, decoder);
}
Aggregations