/** * Gets the insecure port number that this service has bound to. * <p> * The port number is determined as follows: * <ol> * <li>if this service is already listening on an insecure port, the corresponding socket's actual port number is returned, else</li> * <li>if this service has been configured to listen on an insecure port, the configured port number is returned, else</li> * <li>{@link Constants#PORT_UNCONFIGURED} is returned.</li> * </ol> * * @return The port number. */ public final int getInsecurePort() { if (getActualInsecurePort() != Constants.PORT_UNCONFIGURED) { return getActualInsecurePort(); } else if (isInsecurePortEnabled()) { return getConfig().getInsecurePort(getInsecurePortDefaultValue()); } else { return Constants.PORT_UNCONFIGURED; } }
/** * Gets the insecure port number that this service has bound to. * <p> * The port number is determined as follows: * <ol> * <li>if this service is already listening on an insecure port, the corresponding socket's actual port number is returned, else</li> * <li>if this service has been configured to listen on an insecure port, the configured port number is returned, else</li> * <li>{@link Constants#PORT_UNCONFIGURED} is returned.</li> * </ol> * * @return The port number. */ public final int getInsecurePort() { if (getActualInsecurePort() != Constants.PORT_UNCONFIGURED) { return getActualInsecurePort(); } else if (isInsecurePortEnabled()) { return getConfig().getInsecurePort(getInsecurePortDefaultValue()); } else { return Constants.PORT_UNCONFIGURED; } }
/** * Verifies that a Hono server will bind to the default insecure port only * when using a default configuration with the insecure port being enabled. */ @Test public void checkInsecureOnlyPort() { // GIVEN a default configuration with insecure port being enabled but no key store being set final ServiceConfigProperties configProperties = new ServiceConfigProperties(); configProperties.setInsecurePortEnabled(true); // WHEN using this configuration to determine the server's port configuration final AbstractServiceBase<ServiceConfigProperties> server = createService(configProperties); final Future<Void> portConfigurationTracker = server.checkPortConfiguration(); // THEN the server will bind to the default insecure port only assertTrue(portConfigurationTracker.succeeded()); assertFalse(server.isSecurePortEnabled()); assertTrue(server.isInsecurePortEnabled()); assertThat(server.getInsecurePort(), is(INSECURE_PORT_NR)); }
/** * Verifies that a Hono server will bind to both the default insecure and secure ports * when using a default configuration with the insecure port being enabled and the * key store property being set. */ @Test public void checkBothPortsOpen() { // GIVEN a default configuration with insecure port being enabled and a key store being set. final ServiceConfigProperties configProperties = new ServiceConfigProperties(); configProperties.setInsecurePortEnabled(true); configProperties.setKeyStorePath(PREFIX_KEY_PATH + "/honoKeyStore.p12"); // WHEN using this configuration to determine the server's port configuration final AbstractServiceBase<ServiceConfigProperties> server = createService(configProperties); final Future<Void> portConfigurationTracker = server.checkPortConfiguration(); // THEN the server will bind to both the default insecure and secure ports assertTrue(portConfigurationTracker.succeeded()); assertTrue(server.isSecurePortEnabled()); assertThat(server.getPort(), is(PORT_NR)); assertTrue(server.isInsecurePortEnabled()); assertThat(server.getInsecurePort(), is(INSECURE_PORT_NR)); }
/** * Verifies that a Hono server will bind to the default port only * when using a default configuration with only the key store property being set. */ @Test public void checkSecurePortAutoSelect() { // GIVEN a configuration with a key store set final ServiceConfigProperties configProperties = new ServiceConfigProperties(); configProperties.setKeyStorePath(PREFIX_KEY_PATH + "/honoKeyStore.p12"); // WHEN using this configuration to determine the server's port configuration // secure port config: no port set -> secure IANA port selected final AbstractServiceBase<ServiceConfigProperties> server = createService(configProperties); final Future<Void> portConfigurationTracker = server.checkPortConfiguration(); // THEN the default secure port is selected and no insecure port will be opened assertTrue(portConfigurationTracker.succeeded()); assertTrue(server.isSecurePortEnabled()); assertThat(server.getPort(), is(PORT_NR)); assertFalse(server.isInsecurePortEnabled()); }
/** * Verifies that a Hono server will bind to a configured insecure port only * when using a default configuration with the insecure port property being set. */ @Test public void checkInsecureOnlyPortExplicitlySet() { // GIVEN a default configuration with insecure port being set to a specific port. final ServiceConfigProperties configProperties = new ServiceConfigProperties(); configProperties.setInsecurePortEnabled(true); configProperties.setInsecurePort(8888); // WHEN using this configuration to determine the server's port configuration final AbstractServiceBase<ServiceConfigProperties> server = createService(configProperties); final Future<Void> portConfigurationTracker = server.checkPortConfiguration(); // THEN the server will bind to the configured insecure port only assertTrue(portConfigurationTracker.succeeded()); assertFalse(server.isSecurePortEnabled()); assertTrue(server.isInsecurePortEnabled()); assertThat(server.getInsecurePort(), is(8888)); }
/** * Verifies that a Hono server will bind to the configured secure port only * when using a default configuration with only the key store and the port property being set. */ @Test public void checkSecurePortExplicitlySet() { // GIVEN a configuration with a key store and a secure port being set final ServiceConfigProperties configProperties = new ServiceConfigProperties(); configProperties.setKeyStorePath(PREFIX_KEY_PATH + "/honoKeyStore.p12"); configProperties.setPort(8989); // WHEN using this configuration to determine the server's port configuration // secure port config: explicit port set -> port used final AbstractServiceBase<ServiceConfigProperties> server = createService(configProperties); final Future<Void> portConfigurationTracker = server.checkPortConfiguration(); // THEN the configured port is used and no insecure port will be opened assertTrue(portConfigurationTracker.succeeded()); assertTrue(server.isSecurePortEnabled()); assertThat(server.getPort(), is(8989)); assertFalse(server.isInsecurePortEnabled()); }