Search in sources :

Example 26 with ByteString

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");
    }
}
Also used : Buffer(okio.Buffer) ProtocolException(java.net.ProtocolException) ByteString(okio.ByteString) Test(org.junit.Test)

Example 27 with ByteString

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);
}
Also used : Buffer(okio.Buffer) ByteString(okio.ByteString) ByteString(okio.ByteString) Test(org.junit.Test)

Example 28 with ByteString

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");
    }
}
Also used : Buffer(okio.Buffer) ProtocolException(java.net.ProtocolException) ByteString(okio.ByteString) Test(org.junit.Test)

Example 29 with ByteString

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());
    }
}
Also used : ByteString(okio.ByteString) Test(org.junit.Test)

Example 30 with ByteString

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());
}
Also used : ByteString(okio.ByteString) SSLPeerUnverifiedException(javax.net.ssl.SSLPeerUnverifiedException) X509Certificate(java.security.cert.X509Certificate)

Aggregations

ByteString (okio.ByteString)59 Test (org.junit.Test)37 Buffer (okio.Buffer)26 MockResponse (okhttp3.mockwebserver.MockResponse)11 ProtocolException (java.net.ProtocolException)5 IOException (java.io.IOException)4 OneField (com.squareup.wire.protos.edgecases.OneField)3 EOFException (java.io.EOFException)3 BufferedSink (okio.BufferedSink)3 BufferedSource (okio.BufferedSource)3 AllTypes (com.squareup.wire.protos.alltypes.AllTypes)2 Person (com.squareup.wire.protos.person.Person)2 ArrayList (java.util.ArrayList)2 Headers (okhttp3.Headers)2 Request (okhttp3.Request)2 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)2 GzipSink (okio.GzipSink)2 Ignore (org.junit.Ignore)2 Phone (retrofit2.converter.protobuf.PhoneProtos.Phone)2 GsonBuilder (com.google.gson.GsonBuilder)1