SbcNetworkGuard implements NetworkGuard { @Override public boolean isSecure(Channel channel, InetSocketAddress source) { if (channel.isActive()) { return channel.remoteAddress().equals(source); } else { try { ChannelFuture future = channel.connect(source).sync(); return future.isSuccess(); } catch (Exception e) { log.warn("Could not connect channel", e); return false; } } }  @Override boolean isSecure(Channel channel, InetSocketAddress source);  }
@Test public void testSecureSourceWhileChannelInactive() throws Exception { final InetSocketAddress remoteAddress = new InetSocketAddress("192.168.1.22", 2727); final Channel channel = Mockito.mock(Channel.class); final ChannelFuture connectFuture = Mockito.mock(ChannelFuture.class); final NetworkGuard guard = new SbcNetworkGuard(); Mockito.when(channel.isActive()).thenReturn(false); Mockito.when(channel.remoteAddress()).thenReturn(null); Mockito.when(channel.connect(remoteAddress)).thenReturn(connectFuture); Mockito.when(connectFuture.isSuccess()).thenReturn(true); Mockito.when(connectFuture.sync()).thenReturn(connectFuture); final boolean secure = guard.isSecure(channel, remoteAddress); Assert.assertTrue(secure); }
@Test public void testSecureSourceWhileChannelConnected() { final InetSocketAddress remoteAddress = new InetSocketAddress("192.168.1.22", 2727); final Channel channel = Mockito.mock(Channel.class); final NetworkGuard guard = new SbcNetworkGuard(); Mockito.when(channel.isActive()).thenReturn(true); Mockito.when(channel.remoteAddress()).thenReturn(remoteAddress); final boolean secure = guard.isSecure(channel, remoteAddress); Assert.assertTrue(secure); }
@Test public void testInsecureSourceWhileChannelConnected() { final InetSocketAddress remoteAddress = new InetSocketAddress("192.168.1.22", 2727); final InetSocketAddress unknownRemoteAddress = new InetSocketAddress("232.122.55.20", 2727); final Channel channel = Mockito.mock(Channel.class); final NetworkGuard guard = new SbcNetworkGuard(); Mockito.when(channel.isActive()).thenReturn(true); Mockito.when(channel.remoteAddress()).thenReturn(remoteAddress); final boolean secure = guard.isSecure(channel, unknownRemoteAddress); Assert.assertFalse(secure); }