use of okio.ByteString in project wire by square.
the class SchemaProtoAdapterTest method unexpectedEndGroup.
@Test
public void unexpectedEndGroup() throws IOException {
ProtoAdapter<Object> adapter = new RepoBuilder().add("message.proto", "" + "message Message {\n" + " optional string a = 1;\n" + "}\n").protoAdapter("Message");
ByteString encoded = ByteString.decodeHex("0a01611c");
try {
adapter.decode(new Buffer().write(encoded));
fail();
} catch (ProtocolException expected) {
assertThat(expected).hasMessage("Unexpected end group");
}
}
use of okio.ByteString in project wire by square.
the class SchemaProtoAdapterTest method groupsIgnored.
@Test
public void groupsIgnored() throws IOException {
ProtoAdapter<Object> adapter = new RepoBuilder().add("message.proto", "" + "message Message {\n" + " optional string a = 1;\n" + " // repeated group Group1 = 2 {\n" + " // optional SomeMessage a = 11;\n" + " // }\n" + " // repeated group Group2 = 3 {\n" + " // optional SomeMessage b = 21;\n" + " // }\n" + " optional string b = 4;\n" + "}\n").protoAdapter("Message");
ByteString encoded = ByteString.decodeHex("0a0161135a02080114135a02100214135a090803720568656c6c" + "6f141baa010208011c1baa010210021c1baa01090803720568656c6c6f1c220162");
ImmutableMap<String, Object> expected = ImmutableMap.<String, Object>of("a", "a", "b", "b");
assertThat(adapter.decode(new Buffer().write(encoded))).isEqualTo(expected);
}
use of okio.ByteString in project wire by square.
the class SchemaProtoAdapterTest method endGroupDoesntMatchStartGroup.
@Test
public void endGroupDoesntMatchStartGroup() throws IOException {
ProtoAdapter<Object> adapter = new RepoBuilder().add("message.proto", "" + "message Message {\n" + " optional string a = 1;\n" + "}\n").protoAdapter("Message");
ByteString encoded = ByteString.decodeHex("130a01611c");
try {
adapter.decode(new Buffer().write(encoded));
fail();
} catch (ProtocolException expected) {
assertThat(expected).hasMessage("Unexpected end group");
}
}
use of okio.ByteString in project okhttp by square.
the class WebSocketWriterTest method closeTooLongThrows.
@Test
public void closeTooLongThrows() throws IOException {
try {
ByteString longReason = ByteString.encodeUtf8(repeat('X', 124));
serverWriter.writeClose(1000, longReason);
fail();
} catch (IllegalArgumentException e) {
assertEquals("Payload size must be less than or equal to 125", e.getMessage());
}
}
use of okio.ByteString in project okhttp by square.
the class CertificatePinner method check.
/**
* Confirms that at least one of the certificates pinned for {@code hostname} is in {@code
* peerCertificates}. Does nothing if there are no certificates pinned for {@code hostname}.
* OkHttp calls this after a successful TLS handshake, but before the connection is used.
*
* @throws SSLPeerUnverifiedException if {@code peerCertificates} don't match the certificates
* pinned for {@code hostname}.
*/
public void check(String hostname, List<Certificate> peerCertificates) throws SSLPeerUnverifiedException {
List<Pin> pins = findMatchingPins(hostname);
if (pins.isEmpty())
return;
if (certificateChainCleaner != null) {
peerCertificates = certificateChainCleaner.clean(peerCertificates, hostname);
}
for (int c = 0, certsSize = peerCertificates.size(); c < certsSize; c++) {
X509Certificate x509Certificate = (X509Certificate) peerCertificates.get(c);
// Lazily compute the hashes for each certificate.
ByteString sha1 = null;
ByteString sha256 = null;
for (int p = 0, pinsSize = pins.size(); p < pinsSize; p++) {
Pin pin = pins.get(p);
if (pin.hashAlgorithm.equals("sha256/")) {
if (sha256 == null)
sha256 = sha256(x509Certificate);
// Success!
if (pin.hash.equals(sha256))
return;
} else if (pin.hashAlgorithm.equals("sha1/")) {
if (sha1 == null)
sha1 = sha1(x509Certificate);
// Success!
if (pin.hash.equals(sha1))
return;
} else {
throw new AssertionError();
}
}
}
// If we couldn't find a matching pin, format a nice exception.
StringBuilder message = new StringBuilder().append("Certificate pinning failure!").append("\n Peer certificate chain:");
for (int c = 0, certsSize = peerCertificates.size(); c < certsSize; c++) {
X509Certificate x509Certificate = (X509Certificate) peerCertificates.get(c);
message.append("\n ").append(pin(x509Certificate)).append(": ").append(x509Certificate.getSubjectDN().getName());
}
message.append("\n Pinned certificates for ").append(hostname).append(":");
for (int p = 0, pinsSize = pins.size(); p < pinsSize; p++) {
Pin pin = pins.get(p);
message.append("\n ").append(pin);
}
throw new SSLPeerUnverifiedException(message.toString());
}
Aggregations