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