|
| 1 | +/* |
| 2 | + * Copyright (c) 2002-2017 "Neo Technology," |
| 3 | + * Network Engine for Objects in Lund AB [http://neotechnology.com] |
| 4 | + * |
| 5 | + * This file is part of Neo4j. |
| 6 | + * |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + */ |
| 19 | +package org.neo4j.driver.internal.async; |
| 20 | + |
| 21 | +import io.netty.channel.embedded.EmbeddedChannel; |
| 22 | +import io.netty.channel.pool.ChannelPoolHandler; |
| 23 | +import io.netty.handler.ssl.SslHandler; |
| 24 | +import org.junit.Test; |
| 25 | + |
| 26 | +import org.neo4j.driver.internal.security.SecurityPlan; |
| 27 | +import org.neo4j.driver.internal.util.Clock; |
| 28 | +import org.neo4j.driver.internal.util.FakeClock; |
| 29 | + |
| 30 | +import static org.junit.Assert.assertEquals; |
| 31 | +import static org.junit.Assert.assertNotNull; |
| 32 | +import static org.junit.Assert.assertNull; |
| 33 | +import static org.mockito.Mockito.mock; |
| 34 | +import static org.mockito.Mockito.verify; |
| 35 | +import static org.mockito.Mockito.when; |
| 36 | +import static org.neo4j.driver.internal.async.ChannelAttributes.address; |
| 37 | +import static org.neo4j.driver.internal.async.ChannelAttributes.creationTimestamp; |
| 38 | +import static org.neo4j.driver.internal.async.ChannelAttributes.messageDispatcher; |
| 39 | +import static org.neo4j.driver.internal.net.BoltServerAddress.LOCAL_DEFAULT; |
| 40 | + |
| 41 | +public class NettyChannelInitializerTest |
| 42 | +{ |
| 43 | + @Test |
| 44 | + public void shouldAddSslHandlerWhenRequiresEncryption() throws Exception |
| 45 | + { |
| 46 | + SecurityPlan securityPlan = SecurityPlan.forAllCertificates(); |
| 47 | + NettyChannelInitializer initializer = new NettyChannelInitializer( LOCAL_DEFAULT, securityPlan, |
| 48 | + mock( ChannelPoolHandler.class ), new FakeClock() ); |
| 49 | + |
| 50 | + EmbeddedChannel channel = new EmbeddedChannel(); |
| 51 | + initializer.initChannel( channel ); |
| 52 | + |
| 53 | + assertNotNull( channel.pipeline().get( SslHandler.class ) ); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void shouldNotAddSslHandlerWhenDoesNotRequireEncryption() throws Exception |
| 58 | + { |
| 59 | + SecurityPlan securityPlan = SecurityPlan.insecure(); |
| 60 | + NettyChannelInitializer initializer = new NettyChannelInitializer( LOCAL_DEFAULT, securityPlan, |
| 61 | + mock( ChannelPoolHandler.class ), new FakeClock() ); |
| 62 | + |
| 63 | + EmbeddedChannel channel = new EmbeddedChannel(); |
| 64 | + initializer.initChannel( channel ); |
| 65 | + |
| 66 | + assertNull( channel.pipeline().get( SslHandler.class ) ); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void shouldUpdateChannelAttributes() throws Exception |
| 71 | + { |
| 72 | + Clock clock = mock( Clock.class ); |
| 73 | + when( clock.millis() ).thenReturn( 42L ); |
| 74 | + NettyChannelInitializer initializer = new NettyChannelInitializer( LOCAL_DEFAULT, SecurityPlan.insecure(), |
| 75 | + mock( ChannelPoolHandler.class ), clock ); |
| 76 | + |
| 77 | + EmbeddedChannel channel = new EmbeddedChannel(); |
| 78 | + initializer.initChannel( channel ); |
| 79 | + |
| 80 | + assertEquals( LOCAL_DEFAULT, address( channel ) ); |
| 81 | + assertEquals( 42L, creationTimestamp( channel ) ); |
| 82 | + assertNotNull( messageDispatcher( channel ) ); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + public void shouldNotifyPoolHandlerAboutCreatedConnection() throws Exception |
| 87 | + { |
| 88 | + ChannelPoolHandler poolHandler = mock( ChannelPoolHandler.class ); |
| 89 | + NettyChannelInitializer initializer = new NettyChannelInitializer( LOCAL_DEFAULT, SecurityPlan.insecure(), |
| 90 | + poolHandler, new FakeClock() ); |
| 91 | + |
| 92 | + EmbeddedChannel channel = new EmbeddedChannel(); |
| 93 | + initializer.initChannel( channel ); |
| 94 | + |
| 95 | + verify( poolHandler ).channelCreated( channel ); |
| 96 | + } |
| 97 | +} |
0 commit comments