use of io.grpc.netty.InternalProtocolNegotiator.ProtocolNegotiator in project grpc-java by grpc.
the class SdsProtocolNegotiatorsTest method clientSdsProtocolNegotiatorNewHandler_noTlsContextAttribute.
@Test
public void clientSdsProtocolNegotiatorNewHandler_noTlsContextAttribute() {
ChannelHandler mockChannelHandler = mock(ChannelHandler.class);
ProtocolNegotiator mockProtocolNegotiator = mock(ProtocolNegotiator.class);
when(mockProtocolNegotiator.newHandler(grpcHandler)).thenReturn(mockChannelHandler);
ClientSdsProtocolNegotiator pn = new ClientSdsProtocolNegotiator(mockProtocolNegotiator);
ChannelHandler newHandler = pn.newHandler(grpcHandler);
assertThat(newHandler).isNotNull();
assertThat(newHandler).isSameInstanceAs(mockChannelHandler);
}
use of io.grpc.netty.InternalProtocolNegotiator.ProtocolNegotiator in project grpc-java by grpc.
the class SdsProtocolNegotiatorsTest method serverSdsHandler_nullTlsContext_expectFallbackProtocolNegotiator.
@Test
public void serverSdsHandler_nullTlsContext_expectFallbackProtocolNegotiator() {
ChannelHandler mockChannelHandler = mock(ChannelHandler.class);
ProtocolNegotiator mockProtocolNegotiator = mock(ProtocolNegotiator.class);
when(mockProtocolNegotiator.newHandler(grpcHandler)).thenReturn(mockChannelHandler);
SdsProtocolNegotiators.HandlerPickerHandler handlerPickerHandler = new SdsProtocolNegotiators.HandlerPickerHandler(grpcHandler, mockProtocolNegotiator);
pipeline.addLast(handlerPickerHandler);
channelHandlerCtx = pipeline.context(handlerPickerHandler);
// should find HandlerPickerHandler
assertThat(channelHandlerCtx).isNotNull();
// kick off protocol negotiation
pipeline.fireUserEventTriggered(InternalProtocolNegotiationEvent.getDefault());
channelHandlerCtx = pipeline.context(handlerPickerHandler);
assertThat(channelHandlerCtx).isNull();
// need this for tasks to execute on eventLoop
channel.runPendingTasks();
Iterator<Map.Entry<String, ChannelHandler>> iterator = pipeline.iterator();
assertThat(iterator.next().getValue()).isSameInstanceAs(mockChannelHandler);
// no more handlers in the pipeline
assertThat(iterator.hasNext()).isFalse();
}
use of io.grpc.netty.InternalProtocolNegotiator.ProtocolNegotiator in project grpc-java by grpc.
the class SdsProtocolNegotiatorsTest method serverSdsHandler_defaultDownstreamTlsContext_expectFallbackProtocolNegotiator.
@Test
public void serverSdsHandler_defaultDownstreamTlsContext_expectFallbackProtocolNegotiator() throws IOException {
ChannelHandler mockChannelHandler = mock(ChannelHandler.class);
ProtocolNegotiator mockProtocolNegotiator = mock(ProtocolNegotiator.class);
when(mockProtocolNegotiator.newHandler(grpcHandler)).thenReturn(mockChannelHandler);
// we need InetSocketAddress instead of EmbeddedSocketAddress as localAddress for this test
channel = new EmbeddedChannel() {
@Override
public SocketAddress localAddress() {
return new InetSocketAddress("172.168.1.1", 80);
}
};
pipeline = channel.pipeline();
SdsProtocolNegotiators.HandlerPickerHandler handlerPickerHandler = new SdsProtocolNegotiators.HandlerPickerHandler(grpcHandler, mockProtocolNegotiator);
pipeline.addLast(handlerPickerHandler);
channelHandlerCtx = pipeline.context(handlerPickerHandler);
// should find HandlerPickerHandler
assertThat(channelHandlerCtx).isNotNull();
// kick off protocol negotiation: should replace HandlerPickerHandler with ServerSdsHandler
ProtocolNegotiationEvent event = InternalProtocolNegotiationEvent.getDefault();
Attributes attr = InternalProtocolNegotiationEvent.getAttributes(event).toBuilder().set(ATTR_SERVER_SSL_CONTEXT_PROVIDER_SUPPLIER, null).build();
pipeline.fireUserEventTriggered(InternalProtocolNegotiationEvent.withAttributes(event, attr));
channelHandlerCtx = pipeline.context(handlerPickerHandler);
assertThat(channelHandlerCtx).isNull();
// need this for tasks to execute on eventLoop
channel.runPendingTasks();
Iterator<Map.Entry<String, ChannelHandler>> iterator = pipeline.iterator();
assertThat(iterator.next().getValue()).isSameInstanceAs(mockChannelHandler);
// no more handlers in the pipeline
assertThat(iterator.hasNext()).isFalse();
}
use of io.grpc.netty.InternalProtocolNegotiator.ProtocolNegotiator in project grpc-java by grpc.
the class XdsClientWrapperForServerSdsTestMisc method getSslContextProviderSupplier.
private SslContextProviderSupplier getSslContextProviderSupplier(FilterChainSelector selector) throws Exception {
final SettableFuture<SslContextProviderSupplier> sslSet = SettableFuture.create();
ChannelHandler next = new ChannelInboundHandlerAdapter() {
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) {
ProtocolNegotiationEvent e = (ProtocolNegotiationEvent) evt;
sslSet.set(InternalProtocolNegotiationEvent.getAttributes(e).get(ATTR_SERVER_SSL_CONTEXT_PROVIDER_SUPPLIER));
ctx.pipeline().remove(this);
}
};
ProtocolNegotiator mockDelegate = mock(ProtocolNegotiator.class);
GrpcHttp2ConnectionHandler grpcHandler = FakeGrpcHttp2ConnectionHandler.newHandler();
when(mockDelegate.newHandler(grpcHandler)).thenReturn(next);
FilterChainSelectorManager manager = new FilterChainSelectorManager();
manager.updateSelector(selector);
FilterChainMatchingHandler filterChainMatchingHandler = new FilterChainMatchingHandler(grpcHandler, manager, mockDelegate);
pipeline.addLast(filterChainMatchingHandler);
ProtocolNegotiationEvent event = InternalProtocolNegotiationEvent.getDefault();
pipeline.fireUserEventTriggered(event);
channel.runPendingTasks();
sslSet.set(InternalProtocolNegotiationEvent.getAttributes(event).get(ATTR_SERVER_SSL_CONTEXT_PROVIDER_SUPPLIER));
return sslSet.get();
}
use of io.grpc.netty.InternalProtocolNegotiator.ProtocolNegotiator in project grpc-java by grpc.
the class AltsChannelBuilderTest method buildsNettyChannel.
@Test
public void buildsNettyChannel() {
AltsChannelBuilder builder = AltsChannelBuilder.forTarget("localhost:8080").enableUntrustedAltsForTesting();
ProtocolNegotiator protocolNegotiator = builder.getProtocolNegotiatorForTest();
assertThat(protocolNegotiator).isNotNull();
// Avoids exposing this class
assertThat(protocolNegotiator.getClass().getSimpleName()).isEqualTo("ClientAltsProtocolNegotiator");
}
Aggregations