SbcNetworkGuard implements NetworkGuard { @Override public boolean isSecure(NetworkChannel channel, InetSocketAddress source) { if (channel.isConnected()) { return channel.getRemoteAddress().equals(source); } else { try { channel.connect(source); return true; } catch (IOException e) { log.warn("Could not connect channel", e); return false; } } }  @Override boolean isSecure(NetworkChannel channel, InetSocketAddress source);  }
@Test public void testSecureSourceWhileChannelDisconnected() { final InetSocketAddress remoteAddress = new InetSocketAddress("192.168.1.22", 2727); final NetworkChannel channel = Mockito.mock(NetworkChannel.class); final NetworkGuard guard = new SbcNetworkGuard(); Mockito.when(channel.isConnected()).thenReturn(false); Mockito.when(channel.getRemoteAddress()).thenReturn(null); 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 NetworkChannel channel = Mockito.mock(NetworkChannel.class); final NetworkGuard guard = new SbcNetworkGuard(); 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 testInsecureSourceWhileChannelConnected() { 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 SbcNetworkGuard(); Mockito.when(channel.isConnected()).thenReturn(true); Mockito.when(channel.getRemoteAddress()).thenReturn(remoteAddress); final boolean secure = guard.isSecure(channel, unknownRemoteAddress); Assert.assertFalse(secure); }