DirectNetworkGuard implements NetworkGuard { @Override public boolean isSecure(Channel channel, InetSocketAddress source) { if (channel.isActive()) { return channel.remoteAddress().equals(source); } return false; }  @Override boolean isSecure(Channel channel, InetSocketAddress source);  }
@Test public void testSecureSource() { final InetSocketAddress remoteAddress = new InetSocketAddress("192.168.1.22", 2727); final Channel channel = Mockito.mock(Channel.class); final NetworkGuard guard = new DirectNetworkGuard(); 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 testInsecureSource() { 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 DirectNetworkGuard(); Mockito.when(channel.isActive()).thenReturn(true); Mockito.when(channel.remoteAddress()).thenReturn(remoteAddress); final boolean secure = guard.isSecure(channel, unknownRemoteAddress); Assert.assertFalse(secure); }
@Test public void testInsecureSourceWithDisActiveChannel() { final InetSocketAddress remoteAddress = new InetSocketAddress("192.168.1.22", 2727); final Channel channel = Mockito.mock(Channel.class); final NetworkGuard guard = new DirectNetworkGuard(); Mockito.when(channel.isActive()).thenReturn(false); final boolean secure = guard.isSecure(channel, remoteAddress); Assert.assertFalse(secure); }