Search in sources :

Example 41 with SocketAddress

use of org.neo4j.configuration.helpers.SocketAddress in project neo4j by neo4j.

the class SingleInstanceGetRoutingTableProcedureTest method shouldThrowIfClientProvidedPortIsNegative.

@ParameterizedTest
@EnumSource(value = RoutingMode.class)
void shouldThrowIfClientProvidedPortIsNegative(RoutingMode routingMode) {
    // given
    var advertisedBoldPort = 8776;
    var clientProvidedPort = -5;
    var advertisedBoltAddress = new SocketAddress("neo4j.com", advertisedBoldPort);
    var clientProvidedHostPortStr = String.format("%s:%d", "my.neo4j-service.com", clientProvidedPort);
    var ctxContents = new MapValueBuilder();
    ctxContents.add(ADDRESS_CONTEXT_KEY, Values.stringValue(clientProvidedHostPortStr));
    var ctx = ctxContents.build();
    var portRegister = mock(ConnectorPortRegister.class);
    when(portRegister.getLocalAddress(BoltConnector.NAME)).thenReturn(new HostnamePort("neo4j.com", advertisedBoldPort));
    var config = newConfig(Config.defaults(SERVER_DEFAULTS), Duration.ofSeconds(100), advertisedBoltAddress);
    config.set(routing_default_router, routingMode);
    var databaseManager = databaseManagerMock(config, true);
    var logProvider = new AssertableLogProvider();
    var procedure = newProcedure(databaseManager, portRegister, config, logProvider);
    var expectedMessage = "An address key is included in the query string provided to the GetRoutingTableProcedure, but its value could not be parsed.";
    // when
    assertThrows(ProcedureException.class, () -> invoke(procedure, ID, ctx), expectedMessage);
}
Also used : MapValueBuilder(org.neo4j.values.virtual.MapValueBuilder) HostnamePort(org.neo4j.internal.helpers.HostnamePort) SocketAddress(org.neo4j.configuration.helpers.SocketAddress) AssertableLogProvider(org.neo4j.logging.AssertableLogProvider) EnumSource(org.junit.jupiter.params.provider.EnumSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 42 with SocketAddress

use of org.neo4j.configuration.helpers.SocketAddress in project neo4j by neo4j.

the class SingleInstanceGetRoutingTableProcedureTest method shouldUseConfiguredAdvertisedAddressIfClientProvidedPortIsZero.

/**
 * This tests a very specific non-documented behaviour - if the client provided address has the port explicitly set to zero then the returned routing table
 * uses the advertised address. We make use of this behaviour in some internal tests. In "real life" we should never encounter a client provided address
 * specifically set to zero so there's no particular reason to change this.
 */
@RoutingConfigsTest
void shouldUseConfiguredAdvertisedAddressIfClientProvidedPortIsZero(Config config, String clientAddress) throws Exception {
    // given
    var advertisedBoldPort = 8776;
    var clientProvidedPort = 0;
    var advertisedBoltAddress = new SocketAddress("neo4j.com", advertisedBoldPort);
    config.set(BoltConnector.advertised_address, advertisedBoltAddress);
    var clientProvidedHost = clientAddress == null ? "mydomain.com" : clientAddress.split(":")[0];
    var clientProvidedHostPortStr = String.format("%s:%d", clientProvidedHost, clientProvidedPort);
    var portRegister = mock(ConnectorPortRegister.class);
    when(portRegister.getLocalAddress(BoltConnector.NAME)).thenReturn(new HostnamePort("neo4j.com", advertisedBoldPort));
    var databaseManager = databaseManagerMock(config, true);
    var logProvider = new AssertableLogProvider();
    var procedure = newProcedure(databaseManager, portRegister, config, logProvider);
    var expectedAddress = advertisedBoltAddress;
    // when
    RoutingResult result = invoke(procedure, clientProvidedHostPortStr);
    // then
    assertEquals(singletonList(expectedAddress), result.readEndpoints());
    assertEquals(expectedWriters(expectedAddress), result.writeEndpoints());
    assertEquals(singletonList(expectedAddress), result.routeEndpoints());
}
Also used : HostnamePort(org.neo4j.internal.helpers.HostnamePort) SocketAddress(org.neo4j.configuration.helpers.SocketAddress) AssertableLogProvider(org.neo4j.logging.AssertableLogProvider)

Example 43 with SocketAddress

use of org.neo4j.configuration.helpers.SocketAddress in project neo4j by neo4j.

the class SingleInstanceGetRoutingTableProcedureTest method shouldThrowWhenAddressCtxIsPresentButEmpty.

@ParameterizedTest
@EnumSource(value = RoutingMode.class)
void shouldThrowWhenAddressCtxIsPresentButEmpty(RoutingMode routingMode) {
    // given
    var ctxContents = new MapValueBuilder();
    ctxContents.add(ADDRESS_CONTEXT_KEY, Values.EMPTY_STRING);
    var ctx = ctxContents.build();
    var portRegister = mock(ConnectorPortRegister.class);
    var config = newConfig(Config.defaults(SERVER_DEFAULTS), Duration.ofSeconds(100), new SocketAddress("neo4j.com", 7687));
    config.set(routing_default_router, routingMode);
    var databaseManager = databaseManagerMock(config, true);
    var logProvider = new AssertableLogProvider();
    var procedure = newProcedure(databaseManager, portRegister, config, logProvider);
    var expectedMessage = "An address key is included in the query string provided to the GetRoutingTableProcedure, but its value could not be parsed.";
    // when/then
    assertThrows(ProcedureException.class, () -> invoke(procedure, ID, ctx), expectedMessage);
}
Also used : MapValueBuilder(org.neo4j.values.virtual.MapValueBuilder) SocketAddress(org.neo4j.configuration.helpers.SocketAddress) AssertableLogProvider(org.neo4j.logging.AssertableLogProvider) EnumSource(org.junit.jupiter.params.provider.EnumSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 44 with SocketAddress

use of org.neo4j.configuration.helpers.SocketAddress in project neo4j by neo4j.

the class NeoWebServerPortConflictIT method shouldComplainIfServerPortIsAlreadyTaken.

@Test
public void shouldComplainIfServerPortIsAlreadyTaken() throws IOException {
    try (ServerSocket socket = new ServerSocket(0, 0, InetAddress.getLocalHost())) {
        SocketAddress contestedAddress = new SocketAddress(socket.getInetAddress().getHostName(), socket.getLocalPort());
        AssertableLogProvider logProvider = new AssertableLogProvider();
        TestWebContainer testWebContainer = null;
        try {
            testWebContainer = builder(logProvider).onAddress(contestedAddress).usingDataDir(folder.directory(name.getMethodName()).toAbsolutePath().toString()).build();
            fail("Should have reported failure to start");
        } catch (Exception e) {
            assertThat(e.getMessage()).contains("Error starting Neo4j database server");
        } finally {
            if (testWebContainer != null) {
                testWebContainer.shutdown();
            }
        }
        assertThat(logProvider).forClass(CommunityNeoWebServer.class).forLevel(ERROR).containsMessageWithArguments("Failed to start Neo4j on %s: %s", contestedAddress, format("Address %s is already in use, cannot bind to it.", contestedAddress));
    }
}
Also used : TestWebContainer(org.neo4j.server.helpers.TestWebContainer) ServerSocket(java.net.ServerSocket) SocketAddress(org.neo4j.configuration.helpers.SocketAddress) IOException(java.io.IOException) AssertableLogProvider(org.neo4j.logging.AssertableLogProvider) Test(org.junit.Test)

Example 45 with SocketAddress

use of org.neo4j.configuration.helpers.SocketAddress in project neo4j by neo4j.

the class NeoWebServerPortConflictIT method shouldComplainIfServerHTTPSPortIsAlreadyTaken.

@Test
public void shouldComplainIfServerHTTPSPortIsAlreadyTaken() throws IOException {
    try (ServerSocket httpsSocket = new ServerSocket(0, 0, InetAddress.getLocalHost())) {
        SocketAddress unContestedAddress = new SocketAddress(httpsSocket.getInetAddress().getHostName(), 0);
        SocketAddress httpsAddress = new SocketAddress(httpsSocket.getInetAddress().getHostName(), httpsSocket.getLocalPort());
        AssertableLogProvider logProvider = new AssertableLogProvider();
        TestWebContainer testWebContainer = null;
        try {
            testWebContainer = builder(logProvider).onAddress(unContestedAddress).onHttpsAddress(httpsAddress).withHttpsEnabled().usingDataDir(folder.directory(name.getMethodName()).toAbsolutePath().toString()).build();
            fail("Should have reported failure to start");
        } catch (Exception e) {
            assertThat(e.getMessage()).contains("Error starting Neo4j database server");
        } finally {
            if (testWebContainer != null) {
                testWebContainer.shutdown();
            }
        }
        assertThat(logProvider).forClass(CommunityNeoWebServer.class).forLevel(ERROR).containsMessageWithArguments("Failed to start Neo4j on %s: %s", unContestedAddress, format("At least one of the addresses %s or %s is already in use, cannot bind to it.", unContestedAddress, httpsAddress));
    }
}
Also used : TestWebContainer(org.neo4j.server.helpers.TestWebContainer) ServerSocket(java.net.ServerSocket) SocketAddress(org.neo4j.configuration.helpers.SocketAddress) IOException(java.io.IOException) AssertableLogProvider(org.neo4j.logging.AssertableLogProvider) Test(org.junit.Test)

Aggregations

SocketAddress (org.neo4j.configuration.helpers.SocketAddress)54 Test (org.junit.jupiter.api.Test)27 InetSocketAddress (java.net.InetSocketAddress)12 AssertableLogProvider (org.neo4j.logging.AssertableLogProvider)11 Test (org.junit.Test)5 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)5 HostnamePort (org.neo4j.internal.helpers.HostnamePort)5 MapValueBuilder (org.neo4j.values.virtual.MapValueBuilder)5 EnumSource (org.junit.jupiter.params.provider.EnumSource)4 SslPolicyConfig (org.neo4j.configuration.ssl.SslPolicyConfig)4 BeforeEach (org.junit.jupiter.api.BeforeEach)3 EnterpriseDatabaseManagementServiceBuilder (com.neo4j.dbms.api.EnterpriseDatabaseManagementServiceBuilder)2 IOException (java.io.IOException)2 ServerSocket (java.net.ServerSocket)2 HashMap (java.util.HashMap)2 DynamicTest (org.junit.jupiter.api.DynamicTest)2 DynamicTest.dynamicTest (org.junit.jupiter.api.DynamicTest.dynamicTest)2 Config (org.neo4j.configuration.Config)2 ConnectorPortRegister (org.neo4j.configuration.connectors.ConnectorPortRegister)2 DatabaseManagementService (org.neo4j.dbms.api.DatabaseManagementService)2