Refine search
@Test void testMatchesNullHostWithIP() throws Exception { HostnamePort hostnamePortSinglePort = new HostnamePort( ":1234" ); String host1IP = InetAddress.getLocalHost().getHostAddress(); assertFalse( hostnamePortSinglePort.matches( URI.create( "ha://" + host1IP + ":1234" ) ) ); }
public String toString( String defaultHost ) { StringBuilder builder = new StringBuilder(); String host = getHost( defaultHost ); if ( host != null ) { builder.append( host ); } if ( getPort() != 0 ) { builder.append( ':' ); builder.append( getPort() ); if ( isRange() ) { builder.append( '-' ).append( getPorts()[1] ); } } return builder.toString(); }
public boolean matches( URI toMatch ) { boolean result = false; for ( int port = ports[0]; port <= ports[1]; port++ ) { if ( port == toMatch.getPort() ) { result = true; break; } } if ( host == null && toMatch.getHost() == null ) { return result; } else if ( host == null ) { return false; } // URI may contain IP, so make sure we check it too by converting ours, if necessary String toMatchHost = toMatch.getHost(); // this tries to match hostnames as they are at first, then tries to extract and match ip addresses of both return result && (host.equalsIgnoreCase( toMatchHost ) || getHost( null ).equalsIgnoreCase( getHostAddress( toMatchHost, toMatchHost ) )); }
/** * Takes a raw address that can have a single port or 2 ports (lower and upper bounds of port range) and * processes it to a clean separation of host and ports. When only one port is specified, it is in the lower bound. * The presence of an upper bound implies a range. * * @param rawAddress the raw address that a user can provide via config or command line * @return the host, lower bound port, and upper bound port */ public static OptionalHostnamePort toOptionalHostnamePortFromRawAddress( String rawAddress ) { HostnamePort hostnamePort = new HostnamePort( rawAddress ); Optional<String> processedHost = Optional.ofNullable( hostnamePort.getHost() ) .map( str -> str.replaceAll( "\\[", "" ) ) .map( str -> str.replaceAll( "]", "" ) ); return new OptionalHostnamePort( processedHost, optionalFromZeroable( hostnamePort.getPorts()[0] ), optionalFromZeroable( hostnamePort.getPorts()[1] ) ); }
@Test public void shouldExposeBaseUriWhenHttpDisabledAndHttpsEnabled() throws Exception { startServer( false, true ); URI uri = server.baseUri(); assertEquals( "https", uri.getScheme() ); HostnamePort expectedHostPort = addressForConnector( "https" ); assertEquals( expectedHostPort.getHost(), uri.getHost() ); assertEquals( expectedHostPort.getPort(), uri.getPort() ); }
@Test public void shouldOnlyReadOnceIfAllBytesAreRead() throws Exception { // GIVEN Socket socket = mock( Socket.class ); InputStream stream = mock( InputStream.class ); when(socket.getInputStream()).thenReturn( stream ); when(stream.read( any(byte[].class), anyInt(), anyInt() )).thenReturn( 4 ); SocketConnection connection = new SocketConnection( socket ); connection.connect( new HostnamePort( "my.domain", 1234 ) ); // WHEN connection.recv( 4 ); // THEN verify(stream, times(1)).read( any(byte[].class), anyInt(), anyInt() ); }
@Before public void setUp() throws URISyntaxException { if ( portRegistryOverrider != null ) { portRegistryOverrider.accept( portRegistry ); } else { when( portRegistry.getLocalAddress( "bolt" ) ).thenReturn( new HostnamePort( "localhost", 7687 ) ); } DependencyResolver dependencyResolver = mock( DependencyResolver.class ); when( dependencyResolver.resolveDependency( ConnectorPortRegister.class ) ).thenReturn( portRegistry ); when( neoServer.getDatabase().getGraph().getDependencyResolver() ).thenReturn( dependencyResolver ); }
@Test void testMatchesNullHostWithUnknownHost() { // Given HostnamePort hostnamePortSinglePort = new HostnamePort( ":1234" ); String unknownHost = "unknownHost"; assertThrows( UnknownHostException.class, () -> InetAddress.getByName( unknownHost ) ); // When & Then assertFalse( hostnamePortSinglePort.matches( URI.create( "ha://" + unknownHost + ":1234" ) ) ); }
@Test void testGetHostAddress() throws Exception { // Given String hostName = InetAddress.getLocalHost().getHostName(); // When & Then // should return default, when host is null assertThat( HostnamePort.getHostAddress( null, "default" ), equalTo( "default" ) ); // should return host ip address when host is known assertThat( HostnamePort.getHostAddress( hostName, "default" ), equalTo( hostName ) ); }
private static Consumer<ConnectorPortRegister> register( String connector, String host, int port ) { return register -> when( register.getLocalAddress( connector ) ).thenReturn( new HostnamePort( host, port ) ); }
@Test public void shouldOpenBoltPort() throws Throwable { // given try ( ServerControls controls = getTestServerBuilder( testDir.directory() ).newServer() ) { URI uri = controls.boltURI(); // when new SocketConnection().connect( new HostnamePort( uri.getHost(), uri.getPort() ) ); // then no exception } }
@Test void testMatchesIP() { // Given HostnamePort hostnamePortSinglePort = new HostnamePort( "1.2.3.4:1234" ); HostnamePort hostnamePortWithRange = new HostnamePort( "1.2.3.4:1234-1236" ); // When & Then // Should match, same host and port assertTrue( hostnamePortSinglePort.matches( URI.create( "ha://1.2.3.4:1234" ) ) ); // Should fail, different host or port assertFalse( hostnamePortSinglePort.matches( URI.create( "ha://1.2.3.4:1235" ) ) ); assertFalse( hostnamePortSinglePort.matches( URI.create( "ha://5.6.7.8:1234" ) ) ); assertFalse( hostnamePortSinglePort.matches( URI.create( "ha://5.6.7.8:1235" ) ) ); // Should fail, no port assertFalse( hostnamePortSinglePort.matches( URI.create( "ha://1.2.3.4" ) ) ); assertFalse( hostnamePortSinglePort.matches( URI.create( "ha://5.6.7.8" ) ) ); // Should match, port in range and host the same assertTrue( hostnamePortWithRange.matches( URI.create( "ha://1.2.3.4:1234" ) ) ); assertTrue( hostnamePortWithRange.matches( URI.create( "ha://1.2.3.4:1235" ) ) ); assertTrue( hostnamePortWithRange.matches( URI.create( "ha://1.2.3.4:1236" ) ) ); // Should not match, different host assertFalse( hostnamePortWithRange.matches( URI.create( "ha://5.6.7.8:1234" ) ) ); assertFalse( hostnamePortWithRange.matches( URI.create( "ha://5.6.7.8:1235" ) ) ); // Should not match, port outside of range assertFalse( hostnamePortWithRange.matches( URI.create( "ha://1.2.3.4:1233" ) ) ); assertFalse( hostnamePortWithRange.matches( URI.create( "ha://1.2.3.4:1237" ) ) ); // Should not match, no port assertFalse( hostnamePortWithRange.matches( URI.create( "ha://1.2.3.4" ) ) ); assertFalse( hostnamePortWithRange.matches( URI.create( "ha://5.6.7.8" ) ) ); }
@Test void testGetHostAddressUnknown() { // Given String unknownHost = "unknownHost"; assertThrows( UnknownHostException.class, () -> InetAddress.getByName( unknownHost ) ); // should return hostname when it is unknown assertThat( HostnamePort.getHostAddress( unknownHost, "default" ), equalTo( unknownHost ) ); }
@Test public void shouldIncludePingCountInURI() throws IOException { final int EXPECTED_PING_COUNT = 16; final HostnamePort hostURL = new HostnamePort( hostname, server.getLocalPort() ); final Map<String,String> udcFields = new HashMap<>(); Pinger p = new Pinger( hostURL, new TestUdcCollector( udcFields ) ); for ( int i = 0; i < EXPECTED_PING_COUNT; i++ ) { p.ping(); } assertThat( p.getPingCount(), is( equalTo( EXPECTED_PING_COUNT ) ) ); Map<String,String> actualQueryMap = handler.getQueryMap(); assertThat( actualQueryMap.get( UdcConstants.PING ), is( Integer.toString( EXPECTED_PING_COUNT ) ) ); }
@Test public void shouldPingServer() throws IOException { final HostnamePort hostURL = new HostnamePort( hostname, server.getLocalPort() ); final Map<String,String> udcFields = new HashMap<>(); udcFields.put( ID, EXPECTED_STORE_ID ); udcFields.put( UdcConstants.VERSION, EXPECTED_KERNEL_VERSION ); Pinger p = new Pinger( hostURL, new TestUdcCollector( udcFields ) ); p.ping(); Map<String,String> actualQueryMap = handler.getQueryMap(); assertThat( actualQueryMap, notNullValue() ); assertThat( actualQueryMap.get( ID ), is( EXPECTED_STORE_ID ) ); }
@Test void testDefaultHost() { HostnamePort hostnamePort = new HostnamePort( ":1234" ); assertThat( hostnamePort.getHost( "1.2.3.4" ), equalTo( "1.2.3.4" ) ); }