@ParameterizedTest
    @MethodSource("arguments")
    public void incomingTest(final ServerSettings localServerSettings, final ServerSettings remoteServerSettings)
        throws Exception
    {
        final ExpectedOutcome expected = ExpectedOutcome.generateExpectedOutcome(remoteServerSettings, localServerSettings);
        AbstractRemoteServerDummy.log("Executing test:\n - Local Server (Recipient, System Under Test) Settings: " + localServerSettings + "\n - Remote Server (Initiator, dummy/mock server) Settings: " + remoteServerSettings + "\nExpected outcome: " + expected.getConnectionState());

        ConnectionListener connectionListener = null;
        try {
            AbstractRemoteServerDummy.log("Setup fixture: (start setting up fixture)");

            // Setup test fixture.
            AbstractRemoteServerDummy.log("Setup fixture: remote server TLS policy.");
            remoteInitiatingServerDummy.setEncryptionPolicy(remoteServerSettings.encryptionPolicy);

            AbstractRemoteServerDummy.log("Setup fixture: remote server dialback.");
            remoteInitiatingServerDummy.setDisableDialback(!remoteServerSettings.dialbackSupported);

            AbstractRemoteServerDummy.log("Setup fixture: remote server certificate state.");
            switch (remoteServerSettings.certificateState) {
                case INVALID:
                    remoteInitiatingServerDummy.setUseExpiredEndEntityCertificate(true);
                    // Intended fall-through
                case VALID:
                    remoteInitiatingServerDummy.preparePKIX();

                    // Install in local server's truststore.
                    final X509Certificate[] chain = remoteInitiatingServerDummy.getGeneratedPKIX().getCertificateChain();
                    final X509Certificate caCert = chain[chain.length-1];
                    trustStore.installCertificate("unit-test", KeystoreTestUtils.toPemFormat(caCert));
                    break;
                case MISSING:
                    break;
                default:
                    throw new IllegalStateException("Unsupported remote certificate state");
            }

            AbstractRemoteServerDummy.log("Setup fixture: local server TLS policy.");
            JiveGlobals.setProperty(ConnectionSettings.Server.TLS_POLICY, localServerSettings.encryptionPolicy.toString());

            AbstractRemoteServerDummy.log("Setup fixture: local server dialback.");
            JiveGlobals.setProperty(ConnectionSettings.Server.DIALBACK_ENABLED, localServerSettings.dialbackSupported ? "true" : "false");

            AbstractRemoteServerDummy.log("Setup fixture: local server certificate state.");
            switch (localServerSettings.certificateState) {
                case MISSING:
                    // Do not install domain certificate.
                    break;
                case INVALID:
                    // Insert an expired certificate into the identity store
                    identityStore.installCertificate(Fixtures.expiredX509Certificate, Fixtures.privateKeyForExpiredCert, "");
                    break;
                case VALID:
                    // Adds a valid certificate into identity store
                    identityStore.getStore().setKeyEntry( "selfsignedkey", FIXTURE_VALID_KEY, "secret".toCharArray(), FIXTURE_VALID_CERTIFICATE_CHAIN);
                    break;
            }

            AbstractRemoteServerDummy.log("Setup fixture: remote server init");
            remoteInitiatingServerDummy.init();
            if (remoteInitiatingServerDummy.getDialbackAuthoritativeServerPort() > 0) {
                DNSUtil.setDnsOverride(Map.of(RemoteInitiatingServerDummy.XMPP_DOMAIN, new SrvRecord("localhost", remoteInitiatingServerDummy.getDialbackAuthoritativeServerPort(), false)));
            }
            AbstractRemoteServerDummy.log("Setup fixture: (done with setting up fixture)");

            // execute system under test.
            AbstractRemoteServerDummy.log("Execute system under test: (start with execution)");
            JiveGlobals.setProperty(ConnectionSettings.Server.OLD_SSLPORT, String.valueOf(findFreeLocalPort()));
            connectionListener = new ConnectionListener(ConnectionType.SOCKET_S2S,
                ConnectionSettings.Server.OLD_SSLPORT,
                ConnectionManagerImpl.DEFAULT_SERVER_SSL_PORT,
                ConnectionSettings.Server.ENABLE_OLD_SSLPORT,
                "xmpp.server.processing.threads",
                null,
                JiveGlobals.getProperty(ConnectionSettings.Server.TLS_POLICY),
                ConnectionSettings.Server.AUTH_PER_CLIENTCERT_POLICY,
                null,
                identityStore.getConfiguration(),
                trustStore.getConfiguration(),
                ConnectionSettings.Server.COMPRESSION_SETTINGS);
            AbstractRemoteServerDummy.log("Execute system under test: starting connection listener");
            connectionListener.start();

            AbstractRemoteServerDummy.log("Execute system under test: mocking connection manager");
            final ConnectionManager connectionManager = Fixtures.mockConnectionManager();
            doReturn(Set.of(connectionListener)).when(connectionManager).getListeners(any(ConnectionType.class));
            doReturn(connectionListener).when(connectionManager).getListener(any(ConnectionType.class), anyBoolean());
            doReturn(connectionManager).when(XMPPServer.getInstance()).getConnectionManager();

            AbstractRemoteServerDummy.log("Execute system under test: make the remote server connect.");
            remoteInitiatingServerDummy.connect(connectionListener.getPort());
            AbstractRemoteServerDummy.log("Execute system under test: start connecting, block until done.");
            remoteInitiatingServerDummy.blockUntilDone(1, TimeUnit.MINUTES);
            AbstractRemoteServerDummy.log("Execute system under test: done connecting.");

            AbstractRemoteServerDummy.log("Execute system under test: get the incoming server session object.");
            final LocalIncomingServerSession result;
            if (remoteInitiatingServerDummy.getReceivedStreamIDs().isEmpty()) {
                result = null;
            } else {
                // Get the _last_ stream ID.
                final StreamID lastReceivedID = remoteInitiatingServerDummy.getReceivedStreamIDs().get(remoteInitiatingServerDummy.getReceivedStreamIDs().size()-1);
                result = XMPPServer.getInstance().getSessionManager().getIncomingServerSession( lastReceivedID );
            }
            AbstractRemoteServerDummy.log("Execute system under test: (done with execution)");

            // Verify results
            AbstractRemoteServerDummy.log("Verify results (start)");
            AbstractRemoteServerDummy.log("Expect: " + expected.getConnectionState() + ", Result: " + result);
            switch (expected.getConnectionState())
            {
                case NO_CONNECTION:
                    if (result == null) {
                        assertNull(result); // Yes, this is silly.
                    } else {
                        assertFalse(result.isAuthenticated());
                    }
                    break;
                case NON_ENCRYPTED_WITH_DIALBACK_AUTH:
                    assertNotNull(result);
                    assertFalse(result.isClosed());
                    assertFalse(result.isEncrypted());
                    assertTrue(result.isAuthenticated());
                    assertEquals(ServerSession.AuthenticationMethod.DIALBACK, result.getAuthenticationMethod());
                    break;
                case ENCRYPTED_WITH_DIALBACK_AUTH:
                    assertNotNull(result);
                    assertFalse(result.isClosed());
                    assertTrue(result.isEncrypted());
                    assertTrue(result.isAuthenticated());
                    assertEquals(ServerSession.AuthenticationMethod.DIALBACK, result.getAuthenticationMethod());
                    assertEquals( "TLSv1.3", result.getConnection().getTLSProtocolName().get());

                    // Assertions that are specific to OF-1913:
                    assertEquals(2, remoteInitiatingServerDummy.getReceivedStreamIDs().size());
                    assertNotEquals(remoteInitiatingServerDummy.getReceivedStreamIDs().get(0), remoteInitiatingServerDummy.getReceivedStreamIDs().get(1));
                    assertEquals(2, remoteInitiatingServerDummy.getReceivedStreamToValues().size());
                    assertEquals(2, remoteInitiatingServerDummy.getReceivedStreamFromValues().size());
                    break;
                case ENCRYPTED_WITH_SASLEXTERNAL_AUTH:
                    assertNotNull(result);
                    assertFalse(result.isClosed());
                    assertTrue(result.isEncrypted());
                    assertTrue(result.isAuthenticated());
                    assertEquals(ServerSession.AuthenticationMethod.SASL_EXTERNAL, result.getAuthenticationMethod());
                    assertEquals("TLSv1.3", result.getConnection().getTLSProtocolName().get());

                    // Assertions that are specific to OF-1913:
                    assertEquals(2, remoteInitiatingServerDummy.getReceivedStreamIDs().size());
                    assertNotEquals(remoteInitiatingServerDummy.getReceivedStreamIDs().get(0), remoteInitiatingServerDummy.getReceivedStreamIDs().get(1));
                    assertEquals(2, remoteInitiatingServerDummy.getReceivedStreamToValues().size());
                    assertEquals(2, remoteInitiatingServerDummy.getReceivedStreamFromValues().size());
                    break;
            }
            AbstractRemoteServerDummy.log("Expectation met.");
            AbstractRemoteServerDummy.log("Verify results (done)");
        } finally {
            // Teardown test fixture.
            AbstractRemoteServerDummy.log("Teardown test fixture (start)");
            trustStore.delete("unit-test");
            if (connectionListener != null) {
                connectionListener.stop();
            }
            AbstractRemoteServerDummy.log("Teardown test fixture (done)");
        }
    }
Unit test in which Openfire reacts to an inbound server-to-server connection attempt.

This test is parameterized, meaning that the configuration of both the local server and the remote mock server is
passed as an argument to this method. These configurations are used to both initialize and execute the test, but
also to calculate the expected outcome of the test, given the provided configuration. This expected outcome is
asserted by the test implementation

@param localServerSettings Server settings for the system under test (the 'local' server).
@param remoteServerSettings Server settings for the mock server that is used as a peer in this test.
xmppserver/src/test/java/org/jivesoftware/openfire/session/LocalIncomingServerSessionTest.java