Search in sources :

Example 11 with StatusRuntimeException

use of io.grpc.StatusRuntimeException in project grpc-java by grpc.

the class TlsTest method noClientAuthFailure.

/**
   * Tests that a server configured to require client authentication actually does require client
   * authentication.
   */
@Test
public void noClientAuthFailure() throws Exception {
    // Create & start a server.
    File serverCertFile = TestUtils.loadCert("server1.pem");
    File serverPrivateKeyFile = TestUtils.loadCert("server1.key");
    X509Certificate[] serverTrustedCaCerts = { TestUtils.loadX509Cert("ca.pem") };
    server = serverBuilder(0, serverCertFile, serverPrivateKeyFile, serverTrustedCaCerts).addService(new TestServiceImpl(executor)).build().start();
    // Create a client. It has no credentials.
    X509Certificate[] clientTrustedCaCerts = { TestUtils.loadX509Cert("ca.pem") };
    channel = clientChannel(server.getPort(), clientContextBuilder.trustManager(clientTrustedCaCerts).build());
    TestServiceGrpc.TestServiceBlockingStub client = TestServiceGrpc.newBlockingStub(channel);
    // Check that the TLS handshake fails.
    Empty request = Empty.getDefaultInstance();
    try {
        client.emptyCall(request);
        fail("TLS handshake should have failed, but didn't; received RPC response");
    } catch (StatusRuntimeException e) {
        // GRPC reports this situation by throwing a StatusRuntimeException that wraps either a
        // javax.net.ssl.SSLHandshakeException or a java.nio.channels.ClosedChannelException.
        // Thus, reliably detecting the underlying cause is not feasible.
        assertEquals(Throwables.getStackTraceAsString(e), Status.Code.UNAVAILABLE, e.getStatus().getCode());
    }
}
Also used : Empty(com.google.protobuf.EmptyProtos.Empty) StatusRuntimeException(io.grpc.StatusRuntimeException) File(java.io.File) X509Certificate(java.security.cert.X509Certificate) Test(org.junit.Test)

Example 12 with StatusRuntimeException

use of io.grpc.StatusRuntimeException in project grpc-java by grpc.

the class TlsTest method serverRejectsUntrustedClientCert.

/**
   * Tests that a server configured to require client authentication refuses to accept connections
   * from a client that has an untrusted certificate.
   */
@Test
public void serverRejectsUntrustedClientCert() throws Exception {
    // Create & start a server. It requires client authentication and trusts only the test CA.
    File serverCertFile = TestUtils.loadCert("server1.pem");
    File serverPrivateKeyFile = TestUtils.loadCert("server1.key");
    X509Certificate[] serverTrustedCaCerts = { TestUtils.loadX509Cert("ca.pem") };
    server = serverBuilder(0, serverCertFile, serverPrivateKeyFile, serverTrustedCaCerts).addService(new TestServiceImpl(executor)).build().start();
    // Create a client. Its credentials come from a CA that the server does not trust. The client
    // trusts both test CAs, so we can be sure that the handshake failure is due to the server
    // rejecting the client's cert, not the client rejecting the server's cert.
    File clientCertChainFile = TestUtils.loadCert("badclient.pem");
    File clientPrivateKeyFile = TestUtils.loadCert("badclient.key");
    X509Certificate[] clientTrustedCaCerts = { TestUtils.loadX509Cert("ca.pem") };
    channel = clientChannel(server.getPort(), clientContextBuilder.keyManager(clientCertChainFile, clientPrivateKeyFile).trustManager(clientTrustedCaCerts).build());
    TestServiceGrpc.TestServiceBlockingStub client = TestServiceGrpc.newBlockingStub(channel);
    // Check that the TLS handshake fails.
    Empty request = Empty.getDefaultInstance();
    try {
        client.emptyCall(request);
        fail("TLS handshake should have failed, but didn't; received RPC response");
    } catch (StatusRuntimeException e) {
        // GRPC reports this situation by throwing a StatusRuntimeException that wraps either a
        // javax.net.ssl.SSLHandshakeException or a java.nio.channels.ClosedChannelException.
        // Thus, reliably detecting the underlying cause is not feasible.
        assertEquals(Throwables.getStackTraceAsString(e), Status.Code.UNAVAILABLE, e.getStatus().getCode());
    }
}
Also used : Empty(com.google.protobuf.EmptyProtos.Empty) StatusRuntimeException(io.grpc.StatusRuntimeException) File(java.io.File) X509Certificate(java.security.cert.X509Certificate) Test(org.junit.Test)

Example 13 with StatusRuntimeException

use of io.grpc.StatusRuntimeException in project grpc-java by grpc.

the class ProtoLiteUtilsTest method parseInvalid.

@Test
public void parseInvalid() throws Exception {
    InputStream is = new ByteArrayInputStream(new byte[] { -127 });
    try {
        marshaller.parse(is);
        fail("Expected exception");
    } catch (StatusRuntimeException ex) {
        assertEquals(Status.Code.INTERNAL, ex.getStatus().getCode());
        assertNotNull(((InvalidProtocolBufferException) ex.getCause()).getUnfinishedMessage());
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) StatusRuntimeException(io.grpc.StatusRuntimeException) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) Test(org.junit.Test)

Example 14 with StatusRuntimeException

use of io.grpc.StatusRuntimeException in project grpc-java by grpc.

the class NanoUtilsTest method parseInvalid.

@Test
public void parseInvalid() throws Exception {
    InputStream is = new ByteArrayInputStream(new byte[] { -127 });
    try {
        marshaller.parse(is);
        fail("Expected exception");
    } catch (StatusRuntimeException ex) {
        assertEquals(Status.Code.INTERNAL, ex.getStatus().getCode());
        assertTrue(ex.getCause() instanceof InvalidProtocolBufferNanoException);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) StatusRuntimeException(io.grpc.StatusRuntimeException) InvalidProtocolBufferNanoException(com.google.protobuf.nano.InvalidProtocolBufferNanoException) Test(org.junit.Test)

Example 15 with StatusRuntimeException

use of io.grpc.StatusRuntimeException in project grpc-java by grpc.

the class ProtoUtilsTest method testJsonIoException.

@Test
public void testJsonIoException() throws Exception {
    Marshaller<Type> marshaller = ProtoUtils.jsonMarshaller(Type.getDefaultInstance());
    final IOException ioe = new IOException();
    try {
        marshaller.parse(new ByteArrayInputStream("{}".getBytes("UTF-8")) {

            @Override
            public void close() throws IOException {
                throw ioe;
            }
        });
        fail("Exception expected");
    } catch (StatusRuntimeException ex) {
        assertEquals(Status.Code.INTERNAL, ex.getStatus().getCode());
        assertEquals(ioe, ex.getCause());
    }
}
Also used : Type(com.google.protobuf.Type) ByteArrayInputStream(java.io.ByteArrayInputStream) StatusRuntimeException(io.grpc.StatusRuntimeException) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

StatusRuntimeException (io.grpc.StatusRuntimeException)125 Test (org.junit.Test)108 ApiException (com.google.api.gax.grpc.ApiException)74 ByteString (com.google.protobuf.ByteString)12 Status (io.grpc.Status)12 ArrayList (java.util.ArrayList)11 SubscriptionName (com.google.pubsub.v1.SubscriptionName)9 Metadata (io.grpc.Metadata)7 ProjectName (com.google.monitoring.v3.ProjectName)6 TopicName (com.google.pubsub.v1.TopicName)6 StreamObserver (io.grpc.stub.StreamObserver)6 ByteArrayInputStream (java.io.ByteArrayInputStream)6 Document (com.google.cloud.language.v1beta2.Document)5 ParentNameOneof (com.google.logging.v2.ParentNameOneof)5 ExecutionException (java.util.concurrent.ExecutionException)5 Document (com.google.cloud.language.v1.Document)4 EncodingType (com.google.cloud.language.v1beta2.EncodingType)4 HelloReply (io.grpc.examples.helloworld.HelloReply)4 HelloRequest (io.grpc.examples.helloworld.HelloRequest)4 IOException (java.io.IOException)4