From 733fb54a8c98092ad6323df2f5b0f8e2cf17af08 Mon Sep 17 00:00:00 2001 From: Guus der Kinderen Date: Wed, 11 Nov 2026 14:53:32 +0100 Subject: [PATCH 01/28] OF-3459: Measure the detach timeout from the moment a session detached A client session that uses Stream Management is kept for a while after its connection breaks, so that the client can resume its stream: for xmpp.session.detach.timeout, 10 minutes by default, which Openfire also announces to the client in the 'max' attribute of . The task that ends detached sessions measured that time from the last stanza that the session sent or received, not from the moment at which it was detached. A session that had been quiet for longer than its timeout before its connection broke was therefore ended at the first run of that task after it was detached, within about a minute with the default settings, before the client could resume it. A session now records the moment at which it is detached, and the timeout is measured from that moment, or from the last activity of the session when that is later. A session that is resumed and later detached again gets the full timeout from its second detach. A session that is registered as detached without being detached has no such moment, or only that of an earlier detach, and is measured from its last activity. A client whose connection broke after a quiet period therefore stays available to its contacts for up to the detach timeout, as a busy one already did. The last activity of a session, as shown in the admin console, keeps its meaning. Termination delegates, which decide for themselves when a detached session ends, are not affected. --- .../jivesoftware/openfire/SessionManager.java | 22 +- .../openfire/session/LocalSession.java | 21 ++ .../SessionManagerDetachTimeoutTest.java | 290 ++++++++++++++++++ 3 files changed, 328 insertions(+), 5 deletions(-) create mode 100644 xmppserver/src/test/java/org/jivesoftware/openfire/SessionManagerDetachTimeoutTest.java diff --git a/xmppserver/src/main/java/org/jivesoftware/openfire/SessionManager.java b/xmppserver/src/main/java/org/jivesoftware/openfire/SessionManager.java index c49ac297bd..21e10e0ca2 100644 --- a/xmppserver/src/main/java/org/jivesoftware/openfire/SessionManager.java +++ b/xmppserver/src/main/java/org/jivesoftware/openfire/SessionManager.java @@ -2386,7 +2386,8 @@ private void restoreCacheContent() */ private class DetachedCleanupTask extends TimerTask { /** - * Close detached client sessions that haven't seen activity. + * Close detached client sessions that have been detached for longer than the detach timeout, measured from the + * moment at which they were most recently detached, or from their last activity when that is later. */ @Override public void run() { @@ -2395,7 +2396,7 @@ public void run() { return; } final Duration allowableInactivity = Duration.ofMillis(idleTime); - final Instant deadline = Instant.now().minus(allowableInactivity); + final Instant now = Instant.now(); for (LocalSession session : detachedSessions.values()) { try { final Set delegates = session.getStreamManager().getTerminationDelegates(); @@ -2407,11 +2408,22 @@ public void run() { Log.trace("Detached session '{}' ({}) uses termination delegation, that does not want to have this session cleaned up yet.", session.getAddress(), session.getStreamID()); } } else { - if (session.getLastActiveDate().toInstant().isBefore(deadline)) { - Log.debug("Detached session '{}' ({}) has been detached for longer than {} and will be cleaned up.", session.getAddress(), session.getStreamID(), Duration.ofMillis(idleTime)); + final Instant lastDetached = session.getLastDetachedDate(); + final Instant lastActive = session.getLastActiveDate().toInstant(); + final Instant detachedSince; + if (lastDetached != null && !lastActive.isAfter(lastDetached)) { + detachedSince = lastDetached; + } else { + // OF-3459: No detach moment, or one before the last activity: registered without LocalSession#setDetached(), or a stanza was still in flight when it detached. + detachedSince = lastActive; + Log.debug("Detached session '{}' ({}) has no recorded moment of detaching ({}) that is later than its last activity ({}). It is measured from its last activity.", session.getAddress(), session.getStreamID(), lastDetached, lastActive); + } + final Duration detachedFor = Duration.between(detachedSince, now); + if (detachedFor.compareTo(allowableInactivity) > 0) { + Log.debug("Detached session '{}' ({}) has been detached since {} ({}), which is longer than the configured maximum of {}. It will be cleaned up.", session.getAddress(), session.getStreamID(), detachedSince, detachedFor, allowableInactivity); terminateDetached(session); } else { - Log.trace("Detached session '{}' ({}) has been detached for {}, which is not longer than the configured maximum of {}. It will not (yet) be cleaned up.", session.getAddress(), session.getStreamID(), Duration.ofMillis(System.currentTimeMillis()-session.getLastActiveDate().getTime()), Duration.ofMillis(idleTime)); + Log.trace("Detached session '{}' ({}) has been detached since {} ({}), which is not longer than the configured maximum of {}. It will not (yet) be cleaned up.", session.getAddress(), session.getStreamID(), detachedSince, detachedFor, allowableInactivity); } } } diff --git a/xmppserver/src/main/java/org/jivesoftware/openfire/session/LocalSession.java b/xmppserver/src/main/java/org/jivesoftware/openfire/session/LocalSession.java index e1a35d2fe6..f03afebad3 100644 --- a/xmppserver/src/main/java/org/jivesoftware/openfire/session/LocalSession.java +++ b/xmppserver/src/main/java/org/jivesoftware/openfire/session/LocalSession.java @@ -34,6 +34,7 @@ import javax.annotation.Nullable; import java.net.UnknownHostException; import java.security.cert.Certificate; +import java.time.Instant; import java.util.*; import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.locks.Lock; @@ -91,6 +92,12 @@ public abstract class LocalSession implements Session { private long lastActiveDate; + /** + * The moment at which the session was most recently detached, or null when it has never been detached. + */ + @Nullable + private volatile Instant lastDetachedDate; + private AtomicLong clientPacketCount = new AtomicLong( 0 ); private AtomicLong serverPacketCount = new AtomicLong( 0 ); @@ -160,6 +167,8 @@ public void setDetached() { lock.lock(); try { Log.debug("Setting session with address {} and streamID {} in detached mode.", this.address, this.streamID ); + // OF-3459: Recorded before the session is registered as detached, so that the cleanup task measures from this detach, not an earlier one. + this.lastDetachedDate = Instant.now(); this.sessionManager.addDetached(this); this.conn = null; }finally { @@ -403,6 +412,18 @@ public Date getLastActiveDate() { return new Date(lastActiveDate); } + /** + * Obtain the moment at which this session was most recently detached (see {@link #setDetached()}). While a + * session that was detached through {@link #setDetached()} is detached, this is the moment at which its current + * detached period started. The value is kept when the session is resumed, and replaced when it is detached again. + * + * @return The moment at which this session was most recently detached, or null if it has never been detached. + */ + @Nullable + public Instant getLastDetachedDate() { + return lastDetachedDate; + } + /** * Increments the number of packets sent from the client to the server. */ diff --git a/xmppserver/src/test/java/org/jivesoftware/openfire/SessionManagerDetachTimeoutTest.java b/xmppserver/src/test/java/org/jivesoftware/openfire/SessionManagerDetachTimeoutTest.java new file mode 100644 index 0000000000..52dba4456c --- /dev/null +++ b/xmppserver/src/test/java/org/jivesoftware/openfire/SessionManagerDetachTimeoutTest.java @@ -0,0 +1,290 @@ +/* + * Copyright (C) 2026 Ignite Realtime Foundation. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jivesoftware.openfire; + +import org.jivesoftware.Fixtures; +import org.jivesoftware.openfire.session.LocalClientSession; +import org.jivesoftware.openfire.session.LocalSession; +import org.jivesoftware.openfire.spi.BasicStreamIDFactory; +import org.jivesoftware.util.JiveGlobals; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.quality.Strictness; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Field; +import java.time.Duration; +import java.time.Instant; +import java.util.Date; +import java.util.Locale; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.*; + +/** + * Verifies that a detached session (XEP-0198 § 5) is kept for xmpp.session.detach.timeout from the moment at which it + * was detached, rather than from its last activity. + * + * These tests run the task that ends detached sessions directly, with a session manager that is spied on so that the + * tests can tell which sessions it ends, without ending them. + * + * @author Guus der Kinderen, guus@goodbytes.nl + */ +public class SessionManagerDetachTimeoutTest +{ + private static final Duration DETACH_TIMEOUT = Duration.ofMinutes(5); + + private SessionManager sessionManager; + + private SessionManager spiedSessionManager; + + @BeforeAll + public static void setUpClass() throws Exception + { + Fixtures.reconfigureOpenfireHome(); + Fixtures.disableDatabasePersistence(); + } + + @BeforeEach + public void setUp() + { + Fixtures.clearExistingProperties(); + + final XMPPServer server = Fixtures.mockXMPPServer(); + doReturn(mock(RoutingTable.class, withSettings().strictness(Strictness.LENIENT))).when(server).getRoutingTable(); + doReturn(mock(PacketRouter.class, withSettings().strictness(Strictness.LENIENT))).when(server).getPacketRouter(); + //noinspection deprecation + XMPPServer.setInstance(server); + + sessionManager = new SessionManager(); + sessionManager.initialize(server); + doReturn(sessionManager).when(server).getSessionManager(); + JiveGlobals.setProperty("xmpp.session.detach.timeout", String.valueOf(DETACH_TIMEOUT.toMillis())); + + // The spy shares the detached sessions of the session manager; it records which sessions are ended, without ending them. + spiedSessionManager = spy(sessionManager); + doNothing().when(spiedSessionManager).terminateDetached(any()); + } + + @AfterEach + public void tearDown() + { + Fixtures.clearExistingProperties(); + //noinspection deprecation + XMPPServer.setInstance(null); + } + + /** + * Verifies that a session that had no activity for longer than the detach timeout before its connection broke is + * kept after it is detached, so that the client can still resume it. + */ + @Test + void sessionInactiveBeforeDetachingIsKeptWithinDetachTimeout() throws Exception + { + // Setup test fixture. + final LocalClientSession session = clientSessionInactiveFor(DETACH_TIMEOUT.plusMinutes(1)); + session.setDetached(); + + // Execute system under test. + runDetachedCleanupTask(); + + // Verify result. + verify(spiedSessionManager, never().description("A session that was just detached must not be ended, however long it had no activity before.")).terminateDetached(any()); + assertTrue(sessionManager.isDetached(session), "A session that was just detached must still be detached, however long it had no activity before."); + } + + /** + * Verifies that a session that has been detached for longer than the detach timeout is ended. + */ + @Test + void sessionDetachedForLongerThanDetachTimeoutIsEnded() throws Exception + { + // Setup test fixture. + final LocalClientSession session = clientSessionInactiveFor(DETACH_TIMEOUT.plusMinutes(2)); + session.setDetached(); + setLastDetachedDate(session, Instant.now().minus(DETACH_TIMEOUT.plusMinutes(1))); + + // Execute system under test. + runDetachedCleanupTask(); + + // Verify result. + verify(spiedSessionManager, times(1).description("A session that has been detached for longer than the detach timeout must be ended.")).terminateDetached(session); + } + + /** + * Verifies that a session that is detached records the moment at which that happened. + */ + @Test + void detachingRecordsMomentOfDetaching() + { + // Setup test fixture. + final LocalClientSession session = clientSessionInactiveFor(DETACH_TIMEOUT.plusMinutes(1)); + final Instant before = Instant.now(); + + // Execute system under test. + session.setDetached(); + + // Verify result. + final Instant after = Instant.now(); + final Instant result = session.getLastDetachedDate(); + assertNotNull(result, "A session that is detached must record the moment at which it was detached."); + assertFalse(result.isBefore(before) || result.isAfter(after), "The recorded moment must be the moment at which the session was detached, but was " + result + " for a detach between " + before + " and " + after + "."); + } + + /** + * Verifies that a session that is detached, resumed and detached again is measured from the second detach: a + * session of which the first detach is longer ago than the detach timeout is kept. The resumption is represented by + * taking the session from the detached sessions, as a resumption does before it moves the connection. + */ + @Test + void detachingAgainAfterResumptionStartsNewDetachedPeriod() throws Exception + { + // Setup test fixture. + final LocalClientSession session = clientSessionInactiveFor(DETACH_TIMEOUT.plusMinutes(2)); + session.setDetached(); + final Instant firstDetach = Instant.now().minus(DETACH_TIMEOUT.plusMinutes(1)); + setLastDetachedDate(session, firstDetach); + assertTrue(sessionManager.removeDetached(session), "Test fixture expects the session to be taken from the detached sessions when it is resumed."); + session.setDetached(); + + // Execute system under test. + runDetachedCleanupTask(); + + // Verify result. + assertTrue(session.getLastDetachedDate().isAfter(firstDetach), "A session that is detached again must record the moment of its second detach."); + verify(spiedSessionManager, never().description("A session that was detached again must not be ended because its first detach is longer ago than the detach timeout.")).terminateDetached(any()); + assertTrue(sessionManager.isDetached(session), "A session that was detached again must still be detached while its second detach is not longer ago than the detach timeout."); + } + + /** + * Verifies that a session that is registered as detached without being detached, and therefore has no recorded + * detach moment, is ended once its last activity is longer ago than the detach timeout. + */ + @Test + void detachedSessionWithoutDetachMomentIsMeasuredFromLastActivity() throws Exception + { + // Setup test fixture. + final LocalClientSession session = clientSessionInactiveFor(DETACH_TIMEOUT.plusMinutes(1)); + sessionManager.addDetached(session); + assertNull(session.getLastDetachedDate(), "Test fixture expects a session that was registered as detached without being detached to have no detach moment."); + + // Execute system under test. + runDetachedCleanupTask(); + + // Verify result. + verify(spiedSessionManager, times(1).description("A detached session without a recorded detach moment must be ended once its last activity is longer ago than the detach timeout.")).terminateDetached(session); + } + + /** + * Verifies that a session that is registered as detached without being detached, and therefore has no recorded + * detach moment, is kept while its last activity is not longer ago than the detach timeout. + */ + @Test + void detachedSessionWithoutDetachMomentIsKeptWhileRecentlyActive() throws Exception + { + // Setup test fixture. + final LocalClientSession session = clientSessionInactiveFor(DETACH_TIMEOUT.minusMinutes(1)); + sessionManager.addDetached(session); + assertNull(session.getLastDetachedDate(), "Test fixture expects a session that was registered as detached without being detached to have no detach moment."); + + // Execute system under test. + runDetachedCleanupTask(); + + // Verify result. + verify(spiedSessionManager, never().description("A detached session without a recorded detach moment must not be ended while its last activity is not longer ago than the detach timeout.")).terminateDetached(any()); + assertTrue(sessionManager.isDetached(session), "A detached session without a recorded detach moment must still be detached while its last activity is not longer ago than the detach timeout."); + } + + /** + * Verifies that a session that was detached and resumed, and that is later registered as detached without being + * detached again, is measured from its last activity after the resumption, not from its earlier detach. + */ + @Test + void detachedSessionWithEarlierDetachMomentIsMeasuredFromLaterActivity() throws Exception + { + // Setup test fixture. + final LocalClientSession session = clientSessionInactiveFor(DETACH_TIMEOUT.minusMinutes(1)); + session.setDetached(); + setLastDetachedDate(session, Instant.now().minus(DETACH_TIMEOUT.plusMinutes(1))); + assertTrue(sessionManager.removeDetached(session), "Test fixture expects the session to be taken from the detached sessions when it is resumed."); + sessionManager.addDetached(session); + + // Execute system under test. + runDetachedCleanupTask(); + + // Verify result. + verify(spiedSessionManager, never().description("A session registered as detached without being detached again must not be ended because an earlier detach is longer ago than the detach timeout.")).terminateDetached(any()); + assertTrue(sessionManager.isDetached(session), "A session registered as detached without being detached again must still be detached while its last activity is not longer ago than the detach timeout."); + } + + /** + * Verifies that a session of which the last activity is later than its recorded detach moment is ended once that + * last activity is longer ago than the detach timeout. + */ + @Test + void detachedSessionWithEarlierDetachMomentIsEndedOnceLaterActivityExpires() throws Exception + { + // Setup test fixture. + final LocalClientSession session = clientSessionInactiveFor(DETACH_TIMEOUT.plusMinutes(1)); + session.setDetached(); + setLastDetachedDate(session, Instant.now().minus(DETACH_TIMEOUT.plusMinutes(3))); + assertTrue(sessionManager.removeDetached(session), "Test fixture expects the session to be taken from the detached sessions when it is resumed."); + sessionManager.addDetached(session); + + // Execute system under test. + runDetachedCleanupTask(); + + // Verify result. + verify(spiedSessionManager, times(1).description("A session of which the last activity is later than its detach moment must be ended once that last activity is longer ago than the detach timeout.")).terminateDetached(session); + } + + /** + * A client session, of which the last activity is reported to be the provided duration ago, that uses the session + * manager under test. Detaching it records the moment of detaching, as for any session. + */ + private static LocalClientSession clientSessionInactiveFor(final Duration inactiveFor) + { + final Connection connection = mock(Connection.class, withSettings().strictness(Strictness.LENIENT)); + final LocalClientSession session = spy(new LocalClientSession(Fixtures.XMPP_DOMAIN, connection, new BasicStreamIDFactory().createStreamID(), Locale.ENGLISH)); + doReturn(Date.from(Instant.now().minus(inactiveFor))).when(session).getLastActiveDate(); + return session; + } + + /** + * Sets the moment at which the provided session was most recently detached, so that a test can have a session that + * was detached in the past. + */ + private static void setLastDetachedDate(final LocalSession session, final Instant moment) throws Exception + { + final Field field = LocalSession.class.getDeclaredField("lastDetachedDate"); + field.setAccessible(true); + field.set(session, moment); + } + + /** + * Runs the private task that ends detached sessions once, for the spied-on session manager. + */ + private void runDetachedCleanupTask() throws Exception + { + final Class taskClass = Class.forName(SessionManager.class.getName() + "$DetachedCleanupTask"); + final Constructor constructor = taskClass.getDeclaredConstructor(SessionManager.class); + constructor.setAccessible(true); + ((Runnable) constructor.newInstance(spiedSessionManager)).run(); + } +} From 686bc32f7f623abcf2ce31713577eda10eb8d11d Mon Sep 17 00:00:00 2001 From: Guus der Kinderen Date: Fri, 6 Nov 2026 11:26:45 +0100 Subject: [PATCH 02/28] OF-2425: Test how Stream Management enables, acknowledges, resumes and ends client sessions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit StreamManager had little test coverage for enabling Stream Management, answering , requesting acknowledgements, resuming a stream, and what happens to a session that is not resumed. The fixes for OF-3433 and OF-3434 added tests for the processing of , the roll-over at 2^32 and the maximum number of unacknowledged stanzas, but not for these. Reusing this code for server connections requires refactoring it, which needs a record of what it does now. StreamManagerTest now also covers enabling with and without resumption in both namespaces, the attributes of , the count of handled stanzas in the answer to , how often is sent, and the acknowledgements that are ignored. StreamManagerResumptionTest covers a successful resumption, refused resumptions, and what happens to the unacknowledged stanzas of a stream that is not resumed, including the rules that OF-3442 introduced. SessionManagerDetachedSessionTest covers the detaching of a resumable session when its connection closes, and the ending of a detached session after its timeout. Where XEP-0198 does not define the error condition of a refused resumption, the tests expect the current one. For a 'previd' that cannot be decoded, the current condition is unexpected-request, while XEP-0198 § 5 recommends item-not-found; the test records this deviation. The mock client session now has a non-anonymous authentication token, so that it can be made resumable. No functional change. --- .../SessionManagerDetachedSessionTest.java | 295 ++++++ .../StreamManagerResumptionTest.java | 956 ++++++++++++++++++ .../streammanagement/StreamManagerTest.java | 515 +++++++++- 3 files changed, 1765 insertions(+), 1 deletion(-) create mode 100644 xmppserver/src/test/java/org/jivesoftware/openfire/SessionManagerDetachedSessionTest.java create mode 100644 xmppserver/src/test/java/org/jivesoftware/openfire/streammanagement/StreamManagerResumptionTest.java diff --git a/xmppserver/src/test/java/org/jivesoftware/openfire/SessionManagerDetachedSessionTest.java b/xmppserver/src/test/java/org/jivesoftware/openfire/SessionManagerDetachedSessionTest.java new file mode 100644 index 0000000000..8bd543d9f4 --- /dev/null +++ b/xmppserver/src/test/java/org/jivesoftware/openfire/SessionManagerDetachedSessionTest.java @@ -0,0 +1,295 @@ +/* + * Copyright (C) 2026 Ignite Realtime Foundation. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jivesoftware.openfire; + +import org.jivesoftware.Fixtures; +import org.jivesoftware.openfire.session.LocalClientSession; +import org.jivesoftware.openfire.spi.BasicStreamIDFactory; +import org.jivesoftware.openfire.streammanagement.StreamManager; +import org.jivesoftware.util.JiveGlobals; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.quality.Strictness; +import org.xmpp.packet.JID; +import org.xmpp.packet.Presence; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.time.Duration; +import java.time.Instant; +import java.util.Date; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.atomic.AtomicBoolean; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; + +/** + * Unit tests that verify how {@link SessionManager} keeps a client session that uses Stream Management when its + * connection breaks, so that the session can be resumed, and how it ends that session when it is not resumed in time + * (XEP-0198 § 4 and § 7). + * + * @author Guus der Kinderen, guus@goodbytes.nl + */ +public class SessionManagerDetachedSessionTest +{ + private static final JID FULL_JID = new JID("johndoe", Fixtures.XMPP_DOMAIN, "test-resource", true); + private static final Duration DETACH_TIMEOUT = Duration.ofMinutes(5); + + private SessionManager sessionManager; + private RoutingTable routingTable; + + @BeforeAll + public static void setUpClass() throws Exception + { + Fixtures.reconfigureOpenfireHome(); + Fixtures.disableDatabasePersistence(); + } + + @BeforeEach + public void setUp() + { + routingTable = mock(RoutingTable.class, withSettings().strictness(Strictness.LENIENT)); + final PacketRouter packetRouter = mock(PacketRouter.class, withSettings().strictness(Strictness.LENIENT)); + + final XMPPServer server = Fixtures.mockXMPPServer(); + doReturn(routingTable).when(server).getRoutingTable(); + doReturn(packetRouter).when(server).getPacketRouter(); + + //noinspection deprecation + XMPPServer.setInstance(server); + + sessionManager = new SessionManager(); + sessionManager.initialize(server); + JiveGlobals.setProperty("xmpp.session.detach.timeout", String.valueOf(DETACH_TIMEOUT.toMillis())); + } + + @AfterEach + public void tearDown() + { + Fixtures.clearExistingProperties(); + //noinspection deprecation + XMPPServer.setInstance(null); + } + + /** + * Verifies that a session of which the stream can be resumed is detached when its connection closes, rather than + * removed, so that the client can resume it (XEP-0198 § 7). + */ + @Test + void resumableSessionIsDetachedWhenConnectionCloses() throws Exception + { + // Setup test fixture. + final LocalClientSession session = clientSession(true); + + // Execute system under test. + fireConnectionClose(session); + + // Verify result. + verify(session, times(1).description("A session of which the stream can be resumed must be detached when its connection closes.")).setDetached(); + verify(routingTable, never().description("The route of a detached session must be kept, so that the session can be resumed.")).removeClientRoute(any()); + verify(session.getStreamManager(), never().description("The unacknowledged stanzas of a detached session must be kept, so that they can be sent again on resumption.")).onClose(any(), any()); + } + + /** + * Verifies that a session of which the stream cannot be resumed is removed when its connection closes, rather than + * detached. + */ + @Test + void nonResumableSessionIsRemovedWhenConnectionCloses() throws Exception + { + // Setup test fixture. + final LocalClientSession session = clientSession(false); + + // Execute system under test. + fireConnectionClose(session); + + // Verify result. + verify(session, never().description("A session of which the stream cannot be resumed must not be detached.")).setDetached(); + verify(routingTable, times(1).description("The route of a session that cannot be resumed must be removed.")).removeClientRoute(FULL_JID); + verify(session.getStreamManager(), times(1).description("The unacknowledged stanzas of a session that cannot be resumed must be handled.")).onClose(any(), any()); + } + + /** + * Verifies that a detached session is ended once it has been detached for longer than xmpp.session.detach.timeout: + * it is no longer detached, its unacknowledged stanzas are handled, and its route is removed. + */ + @Test + void detachedSessionIsEndedAfterDetachTimeout() throws Exception + { + // Setup test fixture. + final LocalClientSession session = detachedSession(DETACH_TIMEOUT.plusMinutes(1)); + + // Execute system under test. + runDetachedCleanupTask(); + + // Verify result. + assertFalse(sessionManager.isDetached(session), "A session that was detached for longer than the detach timeout must no longer be detached."); + verify(session.getStreamManager(), times(1).description("The unacknowledged stanzas of a session that is ended must be handled.")).onClose(any(), any()); + verify(routingTable, times(1).description("The route of a session that is ended must be removed.")).removeClientRoute(FULL_JID); + } + + /** + * Verifies that a detached session is kept while it has been detached for less than xmpp.session.detach.timeout, + * so that the client can still resume it. + */ + @Test + void detachedSessionIsKeptWithinDetachTimeout() throws Exception + { + // Setup test fixture. + final LocalClientSession session = detachedSession(DETACH_TIMEOUT.minusMinutes(1)); + + // Execute system under test. + runDetachedCleanupTask(); + + // Verify result. + assertTrue(sessionManager.isDetached(session), "A session that was detached for less than the detach timeout must still be detached."); + verify(session.getStreamManager(), never().description("The unacknowledged stanzas of a session that can still be resumed must be kept.")).onClose(any(), any()); + verify(routingTable, never().description("The route of a session that can still be resumed must be kept.")).removeClientRoute(any()); + } + + /** + * Verifies that a detached session is kept, however long it has been detached, when xmpp.session.detach.timeout is + * set to -1. + */ + @Test + void detachedSessionIsKeptWhenDetachTimeoutIsDisabled() throws Exception + { + // Setup test fixture. + JiveGlobals.setProperty("xmpp.session.detach.timeout", "-1"); + final LocalClientSession session = detachedSession(Duration.ofDays(1)); + + // Execute system under test. + runDetachedCleanupTask(); + + // Verify result. + assertTrue(sessionManager.isDetached(session), "A detached session must be kept when the detach timeout is disabled."); + verify(session.getStreamManager(), never().description("The unacknowledged stanzas of a session that is kept must be kept.")).onClose(any(), any()); + } + + /** + * Verifies that the unacknowledged stanzas of a session that is removed when its connection closes are handled after + * the session is removed. XEP-0198 § 4 has them treated as stanzas for an unavailable resource. While the session is + * still registered, a message would be routed back to it, rather than to another resource of the user. + */ + @Test + void unacknowledgedStanzasAreHandledAfterClosingSessionIsRemoved() throws Exception + { + // Setup test fixture. + final LocalClientSession session = clientSession(false); + final AtomicBoolean removedBeforeOnClose = recordWhetherRemovedBeforeOnClose(session); + + // Execute system under test. + fireConnectionClose(session); + + // Verify result. + assertTrue(removedBeforeOnClose.get(), "The session must be removed before its unacknowledged stanzas are handled."); + } + + /** + * Verifies that the unacknowledged stanzas of a detached session that is ended are handled after the session is + * removed, for the same reason as for a session that is removed when its connection closes. + */ + @Test + void unacknowledgedStanzasAreHandledAfterDetachedSessionIsRemoved() throws Exception + { + // Setup test fixture. + final LocalClientSession session = detachedSession(DETACH_TIMEOUT.plusMinutes(1)); + final AtomicBoolean removedBeforeOnClose = recordWhetherRemovedBeforeOnClose(session); + + // Execute system under test. + runDetachedCleanupTask(); + + // Verify result. + assertTrue(removedBeforeOnClose.get(), "The session must be removed before its unacknowledged stanzas are handled."); + } + + /** + * A mock of a client session that is the route for its full JID, and of which the stream can or cannot be resumed. + */ + private LocalClientSession clientSession(final boolean resumable) + { + final LocalClientSession session = mock(LocalClientSession.class, withSettings().strictness(Strictness.LENIENT)); + doReturn(BasicStreamIDFactory.createStreamID("test-stream-id")).when(session).getStreamID(); + doReturn(FULL_JID).when(session).getAddress(); + doReturn(new Presence()).when(session).getPresence(); + doReturn(false).when(session).isDetached(); + doReturn(session).when(routingTable).getClientRoute(FULL_JID); + + final StreamManager streamManager = mock(StreamManager.class, withSettings().strictness(Strictness.LENIENT)); + doReturn(resumable).when(streamManager).getResume(); + doReturn(streamManager).when(session).getStreamManager(); + return session; + } + + /** + * A mock of a client session of which the stream can be resumed, registered as detached for the provided duration. + */ + private LocalClientSession detachedSession(final Duration detachedFor) + { + final LocalClientSession session = clientSession(true); + doReturn(true).when(session).isDetached(); + doReturn(Date.from(Instant.now().minus(detachedFor))).when(session).getLastActiveDate(); + sessionManager.addDetached(session); + assertTrue(sessionManager.isDetached(session), "Test fixture expects the session to be registered as detached."); + return session; + } + + /** + * Records whether the route of a session was removed by the time its unacknowledged stanzas are handled. + */ + private AtomicBoolean recordWhetherRemovedBeforeOnClose(final LocalClientSession session) + { + final AtomicBoolean removed = new AtomicBoolean(false); + final AtomicBoolean removedBeforeOnClose = new AtomicBoolean(false); + doAnswer(invocation -> { removed.set(true); return true; }).when(routingTable).removeClientRoute(FULL_JID); + final StreamManager streamManager = session.getStreamManager(); + doAnswer(invocation -> { removedBeforeOnClose.set(removed.get()); return null; }).when(streamManager).onClose(any(), any()); + return removedBeforeOnClose; + } + + /** + * Invokes the private close listener for client sessions for the provided session, and waits for it to complete. + */ + private void fireConnectionClose(final LocalClientSession session) throws Exception + { + final Field listenerField = SessionManager.class.getDeclaredField("clientSessionListener"); + listenerField.setAccessible(true); + final Object listener = listenerField.get(sessionManager); + + final Method method = listener.getClass().getDeclaredMethod("onConnectionClosing", Object.class); + method.setAccessible(true); + + final Object result = method.invoke(listener, session); + if (result instanceof CompletableFuture) { + ((CompletableFuture) result).join(); + } + } + + /** + * Runs the private task that ends detached sessions once, rather than waiting for it to be scheduled. + */ + private void runDetachedCleanupTask() throws Exception + { + final Class taskClass = Class.forName(SessionManager.class.getName() + "$DetachedCleanupTask"); + final Constructor constructor = taskClass.getDeclaredConstructor(SessionManager.class); + constructor.setAccessible(true); + ((Runnable) constructor.newInstance(sessionManager)).run(); + } +} diff --git a/xmppserver/src/test/java/org/jivesoftware/openfire/streammanagement/StreamManagerResumptionTest.java b/xmppserver/src/test/java/org/jivesoftware/openfire/streammanagement/StreamManagerResumptionTest.java new file mode 100644 index 0000000000..8d4e3fc811 --- /dev/null +++ b/xmppserver/src/test/java/org/jivesoftware/openfire/streammanagement/StreamManagerResumptionTest.java @@ -0,0 +1,956 @@ +/* + * Copyright (C) 2026 Ignite Realtime Foundation. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jivesoftware.openfire.streammanagement; + +import org.dom4j.DocumentException; +import org.dom4j.DocumentHelper; +import org.dom4j.Element; +import org.dom4j.QName; +import org.jivesoftware.Fixtures; +import org.jivesoftware.openfire.Connection; +import org.jivesoftware.openfire.PacketRouter; +import org.jivesoftware.openfire.RoutingTable; +import org.jivesoftware.openfire.XMPPServer; +import org.jivesoftware.openfire.auth.AuthToken; +import org.jivesoftware.openfire.session.LocalClientSession; +import org.jivesoftware.openfire.session.LocalSession; +import org.jivesoftware.openfire.session.RemoteClientSession; +import org.jivesoftware.openfire.spi.BasicStreamIDFactory; +import org.jivesoftware.util.XMPPDateTimeFormat; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; +import org.mockito.ArgumentCaptor; +import org.mockito.invocation.Invocation; +import org.xmpp.packet.IQ; +import org.xmpp.packet.JID; +import org.xmpp.packet.Message; +import org.xmpp.packet.Packet; +import org.xmpp.packet.PacketError; +import org.xmpp.packet.Presence; +import org.xmpp.packet.StreamError; + +import java.nio.charset.StandardCharsets; +import java.time.Instant; +import java.time.temporal.ChronoUnit; +import java.util.Base64; +import java.util.List; +import java.util.concurrent.atomic.AtomicBoolean; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; + +/** + * Unit tests that verify how {@link StreamManager} resumes a stream, refuses to resume one, and handles the stanzas + * that are unacknowledged when a stream is not resumed (XEP-0198 § 4 and § 5). + * + * @author Guus der Kinderen, guus@goodbytes.nl + */ +public class StreamManagerResumptionTest +{ + private static final JID FULL_JID = new JID("johndoe", Fixtures.XMPP_DOMAIN, "test-resource", true); + private static final JID OTHER_ENTITY = new JID("janedoe", Fixtures.XMPP_DOMAIN, "other-resource", true); + private static final JID SERVER_ADDRESS = new JID(Fixtures.XMPP_DOMAIN); + private static final String STREAM_ID = "test-stream-id"; + + private RoutingTable routingTable; + + @BeforeAll + public static void setUpClass() throws Exception + { + Fixtures.reconfigureOpenfireHome(); + Fixtures.disableDatabasePersistence(); + } + + @BeforeEach + public void setUp() + { + final XMPPServer server = Fixtures.mockXMPPServer(); + routingTable = mock(RoutingTable.class); + doReturn(routingTable).when(server).getRoutingTable(); + + //noinspection deprecation + XMPPServer.setInstance(server); + } + + @AfterEach + public void tearDown() + { + Fixtures.clearExistingProperties(); + //noinspection deprecation + XMPPServer.setInstance(null); + } + + /** + * Verifies that a resume request for a detached session that can be resumed reattaches that session to the + * connection of the session that requested the resumption, using the value of 'h' from the request. + */ + @Test + void resumeRequestReattachesSession() throws Exception + { + // Setup test fixture. + final LocalClientSession original = resumableClientSession(); + final String previd = enableResumption(original, StreamManager.NAMESPACE_V3); + final LocalClientSession resuming = resumingClientSession(); + + // Execute system under test. + new StreamManager(resuming).process(resume(StreamManager.NAMESPACE_V3, previd, 0)); + + // Verify result. + verify(original, times(1).description("The session must be reattached to the connection of the session that requested the resumption.")).reattach(resuming, 0L); + verify(resuming, never().description("No failure is to be reported for a resumption that succeeds.")).deliverRawText(anyString()); + } + + /** + * Verifies that a resume request for a session of which the former stream is still open closes that stream with a + * 'conflict' stream error, as XEP-0198 § 5 recommends, and reattaches the session. + */ + @Test + void resumeRequestForSessionWithOpenStreamClosesThatStream() throws Exception + { + // Setup test fixture. + final LocalClientSession original = resumableClientSession(); + final String previd = enableResumption(original, StreamManager.NAMESPACE_V3); + final Connection formerConnection = mock(Connection.class); + doReturn(false).when(original).isDetached(); + doReturn(formerConnection).when(original).getConnection(); + final LocalClientSession resuming = resumingClientSession(); + + // Execute system under test. + new StreamManager(resuming).process(resume(StreamManager.NAMESPACE_V3, previd, 0)); + + // Verify result. + final ArgumentCaptor captor = ArgumentCaptor.forClass(StreamError.class); + verify(formerConnection, times(1).description("The former stream that is still open must be closed.")).close(captor.capture()); + assertEquals(StreamError.Condition.conflict, captor.getValue().getCondition(), "The former stream must be closed with a 'conflict' stream error (XEP-0198 § 5)."); + verify(original, times(1).description("The session must be reattached to the connection of the session that requested the resumption.")).reattach(resuming, 0L); + } + + /** + * Verifies that the {@code } element identifies the former stream with its SM-ID, in the namespace that + * the stream used, as XEP-0198 § 5 requires. + */ + @ParameterizedTest + @ValueSource(strings = {StreamManager.NAMESPACE_V2, StreamManager.NAMESPACE_V3}) + void resumedElementIdentifiesFormerStream(final String namespace) throws Exception + { + // Setup test fixture. + final LocalClientSession original = resumableClientSession(); + final String previd = enableResumption(original, namespace); + final Connection connection = reattached(original); + + // Execute system under test. + original.getStreamManager().onResume(SERVER_ADDRESS, 0); + + // Verify result. + final Element resumed = firstRawTextDelivered(connection); + assertEquals(QName.get("resumed", namespace), resumed.getQName(), "The response must be a 'resumed' element in the namespace of the stream."); + assertEquals(previd, resumed.attributeValue("previd"), "The 'previd' attribute must be the SM-ID of the former stream."); + } + + /** + * Verifies that the 'h' attribute of the {@code } element is the number of stanzas that were handled on + * the former stream, which is zero when none were handled (XEP-0198 § 5). + */ + @ParameterizedTest + @ValueSource(ints = {0, 1, 3}) + void resumedElementReportsNumberOfHandledStanzas(final int handled) throws Exception + { + // Setup test fixture. + final LocalClientSession original = resumableClientSession(); + enableResumption(original, StreamManager.NAMESPACE_V3); + final StreamManager sm = original.getStreamManager(); + for (int i = 0; i < handled; i++) { + sm.incrementServerProcessedStanzas(); + } + final Connection connection = reattached(original); + + // Execute system under test. + sm.onResume(SERVER_ADDRESS, 0); + + // Verify result. + assertEquals(String.valueOf(handled), firstRawTextDelivered(connection).attributeValue("h"), "The 'h' attribute must be the number of stanzas that were handled on the former stream."); + } + + /** + * Verifies that the stanzas that the 'h' value of a resume request does not acknowledge are sent again, in order, + * as XEP-0198 § 5 requires, and that they remain unacknowledged until the client acknowledges them. + */ + @Test + void unacknowledgedStanzasAreSentAgainOnResumption() throws Exception + { + // Setup test fixture. + final LocalClientSession original = resumableClientSession(); + enableResumption(original, StreamManager.NAMESPACE_V3); + final StreamManager sm = original.getStreamManager(); + sm.sentStanza(message("1")); + sm.sentStanza(message("2")); + sm.sentStanza(message("3")); + final Connection connection = reattached(original); + + // Execute system under test. + sm.onResume(SERVER_ADDRESS, 1); + + // Verify result. + assertEquals(List.of("2", "3"), deliveredIds(connection), "The stanzas that were not acknowledged must be sent again, in order."); + assertEquals(List.of("2", "3"), unacknowledgedMessageIds(sm), "The stanzas that were sent again must remain unacknowledged."); + } + + /** + * Verifies that no stanzas are sent again on resumption when the 'h' value of the resume request acknowledges all of + * them. + */ + @Test + void acknowledgedStanzasAreNotSentAgainOnResumption() throws Exception + { + // Setup test fixture. + final LocalClientSession original = resumableClientSession(); + enableResumption(original, StreamManager.NAMESPACE_V3); + final StreamManager sm = original.getStreamManager(); + sm.sentStanza(message("1")); + sm.sentStanza(message("2")); + final Connection connection = reattached(original); + + // Execute system under test. + sm.onResume(SERVER_ADDRESS, 2); + + // Verify result. + verify(connection, never().description("Stanzas that were acknowledged in the resume request must not be sent again.")).deliver(any()); + assertEquals(List.of(), unacknowledgedMessageIds(sm), "No stanzas must remain unacknowledged after the resume request acknowledged all of them."); + } + + /** + * Verifies that all kinds of stanzas are sent again on resumption, as XEP-0198 § 5 requires for any stanza that is + * still unhandled. + */ + @Test + void stanzasOfAllKindsAreSentAgainOnResumption() throws Exception + { + // Setup test fixture. + final LocalClientSession original = resumableClientSession(); + enableResumption(original, StreamManager.NAMESPACE_V3); + final StreamManager sm = original.getStreamManager(); + sm.sentStanza(message("message")); + sm.sentStanza(presence("presence")); + sm.sentStanza(iq("iq", IQ.Type.get)); + final Connection connection = reattached(original); + + // Execute system under test. + sm.onResume(SERVER_ADDRESS, 0); + + // Verify result. + assertEquals(List.of("message", "presence", "iq"), deliveredIds(connection), "Every stanza that was not acknowledged must be sent again, whatever its kind."); + } + + /** + * Verifies that the {@code } element is sent before the stanzas that are sent again, as XEP-0198 § 5 has + * them sent after the 'h' value of the response was processed. + */ + @Test + void resumedElementIsSentBeforeStanzasAreSentAgain() throws Exception + { + // Setup test fixture. + final LocalClientSession original = resumableClientSession(); + enableResumption(original, StreamManager.NAMESPACE_V3); + final StreamManager sm = original.getStreamManager(); + sm.sentStanza(message("1")); + final Connection connection = reattached(original); + + // Execute system under test. + sm.onResume(SERVER_ADDRESS, 0); + + // Verify result. + final List resumed = invocations(connection, "deliverRawText").stream().filter(i -> isElement(i.getArgument(0), QName.get("resumed", StreamManager.NAMESPACE_V3))).toList(); + final List sentAgain = invocations(connection, "deliver"); + assertEquals(1, resumed.size(), "The element must be sent once."); + assertEquals(1, sentAgain.size(), "The unacknowledged stanza must be sent again."); + assertTrue(resumed.get(0).getSequenceNumber() < sentAgain.get(0).getSequenceNumber(), "The element must be sent before the stanzas that are sent again."); + } + + /** + * Verifies that unacknowledged messages and presences that are sent again on resumption have a delay element + * (XEP-0203) with the time at which they were originally sent. + */ + @Test + void stanzasSentAgainOnResumptionHaveDelayElement() throws Exception + { + // Setup test fixture. + final LocalClientSession original = resumableClientSession(); + enableResumption(original, StreamManager.NAMESPACE_V3); + final StreamManager sm = original.getStreamManager(); + final Instant before = Instant.now().truncatedTo(ChronoUnit.SECONDS); + sm.sentStanza(message("message")); + sm.sentStanza(presence("presence")); + final Instant after = Instant.now(); + // The stamp has millisecond precision. Waiting makes a stamp of the time of resending fall after 'after'. + Thread.sleep(2); + final Connection connection = reattached(original); + + // Execute system under test. + sm.onResume(SERVER_ADDRESS, 0); + + // Verify result. + final ArgumentCaptor captor = ArgumentCaptor.forClass(Packet.class); + verify(connection, times(2).description("Both unacknowledged stanzas must be sent again.")).deliver(captor.capture()); + for (final Packet sentAgain : captor.getAllValues()) { + final Element delay = sentAgain.getElement().element(QName.get("delay", "urn:xmpp:delay")); + assertNotNull(delay, "A stanza that is sent again must have a delay element (XEP-0203), but was: " + sentAgain.toXML()); + assertEquals(SERVER_ADDRESS.toBareJID(), delay.attributeValue("from"), "The delay element must name this server, but was: " + delay.asXML()); + final Instant stamp = new XMPPDateTimeFormat().parseString(delay.attributeValue("stamp")).toInstant(); + assertFalse(stamp.isBefore(before) || stamp.isAfter(after), "The delay element must have the time at which the stanza was originally sent, but was: " + stamp); + } + } + + /** + * Verifies that an acknowledgement is requested after the unacknowledged stanzas were sent again on resumption, so + * that the peer reports which of them it handled. + */ + @Test + void acknowledgementIsRequestedAfterStanzasAreSentAgain() throws Exception + { + // Setup test fixture. + final LocalClientSession original = resumableClientSession(); + enableResumption(original, StreamManager.NAMESPACE_V3); + final StreamManager sm = original.getStreamManager(); + sm.sentStanza(message("1")); + final Connection connection = reattached(original); + + // Execute system under test. + sm.onResume(SERVER_ADDRESS, 0); + + // Verify result. + final List sentAgain = invocations(connection, "deliver"); + final List requests = invocations(original, "deliverRawText").stream().filter(i -> isElement(i.getArgument(0), QName.get("r", StreamManager.NAMESPACE_V3))).toList(); + assertEquals(1, sentAgain.size(), "The unacknowledged stanza must be sent again."); + assertEquals(1, requests.size(), "An acknowledgement must be requested once."); + assertTrue(sentAgain.get(0).getSequenceNumber() < requests.get(0).getSequenceNumber(), "The acknowledgement must be requested after the stanzas were sent again."); + } + + /** + * Verifies that a resume request with a 'previd' that does not identify a session is refused with a + * {@code } element that has the 'item-not-found' condition, as XEP-0198 § 5 recommends. + */ + @Test + void resumeRequestForUnknownSessionIsRefusedWithItemNotFound() throws Exception + { + // Setup test fixture. + final LocalClientSession original = resumableClientSession(); + final String previd = enableResumption(original, StreamManager.NAMESPACE_V3); + doReturn(null).when(routingTable).getClientRoute(FULL_JID); + final LocalClientSession resuming = resumingClientSession(); + + // Execute system under test. + new StreamManager(resuming).process(resume(StreamManager.NAMESPACE_V3, previd, 0)); + + // Verify result. + assertFailed(lastDelivered(resuming), StreamManager.NAMESPACE_V3, PacketError.Condition.item_not_found); + verify(original, never().description("No session must be resumed.")).reattach(any(), anyLong()); + } + + /** + * Verifies that a resume request with a 'previd' for the full JID of a session, but of another stream, is refused + * with a {@code } element that has the 'item-not-found' condition. That happens when the former session + * timed out, and the user connected again with the same resource (XEP-0198 § 5). + */ + @Test + void resumeRequestForOtherStreamOfSameAddressIsRefusedWithItemNotFound() throws Exception + { + // Setup test fixture. + final LocalClientSession original = resumableClientSession(); + enableResumption(original, StreamManager.NAMESPACE_V3); + final String previdOfOtherStream = Base64.getEncoder().encodeToString((FULL_JID.getResource() + "\0" + "other-stream-id").getBytes(StandardCharsets.UTF_8)); + final LocalClientSession resuming = resumingClientSession(); + + // Execute system under test. + new StreamManager(resuming).process(resume(StreamManager.NAMESPACE_V3, previdOfOtherStream, 0)); + + // Verify result. + assertFailed(lastDelivered(resuming), StreamManager.NAMESPACE_V3, PacketError.Condition.item_not_found); + verify(original, never().description("A session of another stream must not be resumed.")).reattach(any(), anyLong()); + } + + /** + * Verifies that a resume request with a 'previd' that cannot be decoded is refused with a {@code } element. + * The condition currently is 'unexpected-request'. This deviates from XEP-0198 § 5, which says that the condition + * SHOULD be 'item-not-found' when the 'previd' is not recognized as an earlier session. + */ + @Test + void resumeRequestWithMalformedPrevidIsRefused() throws Exception + { + // Setup test fixture. + final LocalClientSession original = resumableClientSession(); + enableResumption(original, StreamManager.NAMESPACE_V3); + final LocalClientSession resuming = resumingClientSession(); + + // Execute system under test. + new StreamManager(resuming).process(resume(StreamManager.NAMESPACE_V3, "not base64!", 0)); + + // Verify result. + assertFailed(lastDelivered(resuming), StreamManager.NAMESPACE_V3, PacketError.Condition.unexpected_request); + verify(original, never().description("No session must be resumed for a 'previd' that cannot be decoded.")).reattach(any(), anyLong()); + } + + /** + * Verifies that a resume request for a session that is connected to another cluster node is refused, as a session + * can only be resumed on the cluster node that it was connected to. XEP-0198 does not define the condition for this; + * it currently is 'unexpected-request'. + */ + @Test + void resumeRequestForSessionOnOtherClusterNodeIsRefused() throws Exception + { + // Setup test fixture. + final LocalClientSession original = resumableClientSession(); + final String previd = enableResumption(original, StreamManager.NAMESPACE_V3); + final RemoteClientSession remote = mock(RemoteClientSession.class); + doReturn(remote).when(routingTable).getClientRoute(FULL_JID); + final LocalClientSession resuming = resumingClientSession(); + + // Execute system under test. + new StreamManager(resuming).process(resume(StreamManager.NAMESPACE_V3, previd, 0)); + + // Verify result. + assertFailed(lastDelivered(resuming), StreamManager.NAMESPACE_V3, PacketError.Condition.unexpected_request); + verify(original, never().description("No session must be resumed.")).reattach(any(), anyLong()); + } + + /** + * Verifies that a resume request from an anonymous session is refused. XEP-0198 does not define the condition for + * this; it currently is 'unexpected-request'. + */ + @Test + void resumeRequestFromAnonymousSessionIsRefused() throws Exception + { + // Setup test fixture. + final LocalClientSession original = resumableClientSession(); + final String previd = enableResumption(original, StreamManager.NAMESPACE_V3); + final LocalClientSession resuming = resumingClientSession(); + final AuthToken authToken = resuming.getAuthToken(); + doReturn(true).when(authToken).isAnonymous(); + + // Execute system under test. + new StreamManager(resuming).process(resume(StreamManager.NAMESPACE_V3, previd, 0)); + + // Verify result. + assertFailed(lastDelivered(resuming), StreamManager.NAMESPACE_V3, PacketError.Condition.unexpected_request); + verify(original, never().description("No session must be resumed from an anonymous session.")).reattach(any(), anyLong()); + } + + /** + * Verifies that a resume request from a session that did not authenticate is refused, as XEP-0198 § 10 does not + * allow a session to be resumed before the client authenticated. XEP-0198 does not define the condition for this; it + * currently is 'unexpected-request'. + */ + @Test + void resumeRequestBeforeAuthenticationIsRefused() throws Exception + { + // Setup test fixture. + final LocalClientSession original = resumableClientSession(); + final String previd = enableResumption(original, StreamManager.NAMESPACE_V3); + final LocalClientSession resuming = resumingClientSession(); + doReturn(null).when(resuming).getAuthToken(); + + // Execute system under test. + new StreamManager(resuming).process(resume(StreamManager.NAMESPACE_V3, previd, 0)); + + // Verify result. + assertFailed(lastDelivered(resuming), StreamManager.NAMESPACE_V3, PacketError.Condition.unexpected_request); + verify(original, never().description("No session must be resumed before the client authenticated.")).reattach(any(), anyLong()); + } + + /** + * Verifies that a resume request from a session that already bound a resource is refused, as resumption replaces + * resource binding (XEP-0198 § 3). XEP-0198 does not define the condition for this; it currently is + * 'unexpected-request'. + */ + @Test + void resumeRequestFromSessionWithBoundResourceIsRefused() throws Exception + { + // Setup test fixture. + final LocalClientSession original = resumableClientSession(); + final String previd = enableResumption(original, StreamManager.NAMESPACE_V3); + final LocalClientSession resuming = resumingClientSession(); + doReturn(true).when(resuming).isAuthenticated(); + + // Execute system under test. + new StreamManager(resuming).process(resume(StreamManager.NAMESPACE_V3, previd, 0)); + + // Verify result. + assertFailed(lastDelivered(resuming), StreamManager.NAMESPACE_V3, PacketError.Condition.unexpected_request); + verify(original, never().description("No session must be resumed from a session that already bound a resource.")).reattach(any(), anyLong()); + } + + /** + * Verifies that a resume request for a session that is being closed is refused (OF-2811). XEP-0198 does not define + * the condition for this; it currently is 'unexpected-request'. + */ + @Test + void resumeRequestForClosedSessionIsRefused() throws Exception + { + // Setup test fixture. + final LocalClientSession original = resumableClientSession(); + final String previd = enableResumption(original, StreamManager.NAMESPACE_V3); + doReturn(true).when(original).isClosed(); + final LocalClientSession resuming = resumingClientSession(); + + // Execute system under test. + new StreamManager(resuming).process(resume(StreamManager.NAMESPACE_V3, previd, 0)); + + // Verify result. + assertFailed(lastDelivered(resuming), StreamManager.NAMESPACE_V3, PacketError.Condition.unexpected_request); + verify(original, never().description("A session that is being closed must not be resumed.")).reattach(any(), anyLong()); + } + + /** + * Verifies that a resume request for a stream of which resumption was disabled, such as one that was closed + * formally, is refused. XEP-0198 does not define the condition for this; it currently is 'unexpected-request'. + */ + @Test + void resumeRequestForStreamWithResumptionDisabledIsRefused() throws Exception + { + // Setup test fixture. + final LocalClientSession original = resumableClientSession(); + final String previd = enableResumption(original, StreamManager.NAMESPACE_V3); + original.getStreamManager().formalClose(); + final LocalClientSession resuming = resumingClientSession(); + + // Execute system under test. + new StreamManager(resuming).process(resume(StreamManager.NAMESPACE_V3, previd, 0)); + + // Verify result. + assertFailed(lastDelivered(resuming), StreamManager.NAMESPACE_V3, PacketError.Condition.unexpected_request); + verify(original, never().description("A stream of which resumption was disabled must not be resumed.")).reattach(any(), anyLong()); + } + + /** + * Verifies that a resume request in another namespace than the one of the former stream is refused. XEP-0198 does + * not define the condition for this; it currently is 'unexpected-request'. + */ + @Test + void resumeRequestInOtherNamespaceIsRefused() throws Exception + { + // Setup test fixture. + final LocalClientSession original = resumableClientSession(); + final String previd = enableResumption(original, StreamManager.NAMESPACE_V3); + final LocalClientSession resuming = resumingClientSession(); + + // Execute system under test. + new StreamManager(resuming).process(resume(StreamManager.NAMESPACE_V2, previd, 0)); + + // Verify result. + assertFailed(lastDelivered(resuming), StreamManager.NAMESPACE_V2, PacketError.Condition.unexpected_request); + verify(original, never().description("A stream must not be resumed in another namespace.")).reattach(any(), anyLong()); + } + + /** + * Verifies that a resume request with a value of 'h' that acknowledges more stanzas than were sent is refused with a + * {@code } element, as XEP-0198 § 6 requires for an error with regard to a resume request. XEP-0198 does not + * define the condition for this; it currently is 'unexpected-request'. + */ + @Test + void resumeRequestWithValueAboveNumberOfStanzasSentIsRefused() throws Exception + { + // Setup test fixture. + final LocalClientSession original = resumableClientSession(); + final String previd = enableResumption(original, StreamManager.NAMESPACE_V3); + original.getStreamManager().sentStanza(message("1")); + original.getStreamManager().sentStanza(message("2")); + final LocalClientSession resuming = resumingClientSession(); + + // Execute system under test. + new StreamManager(resuming).process(resume(StreamManager.NAMESPACE_V3, previd, 3)); + + // Verify result. + assertFailed(lastDelivered(resuming), StreamManager.NAMESPACE_V3, PacketError.Condition.unexpected_request); + verify(original, never().description("A stream must not be resumed with a value of 'h' that is too high.")).reattach(any(), anyLong()); + } + + /** + * Verifies that a refused resume request leaves the session that requested it open, without Stream Management, so + * that the client can bind a resource instead, as XEP-0198 § 5 recommends. The session that was to be resumed is not + * affected. + */ + @Test + void refusedResumeRequestAllowsClientToBindResource() throws Exception + { + // Setup test fixture. + final LocalClientSession original = resumableClientSession(); + final String previd = enableResumption(original, StreamManager.NAMESPACE_V3); + doReturn(null).when(routingTable).getClientRoute(FULL_JID); + final LocalClientSession resuming = resumingClientSession(); + final StreamManager sm = new StreamManager(resuming); + + // Execute system under test. + sm.process(resume(StreamManager.NAMESPACE_V3, previd, 0)); + + // Verify result. + assertFalse(resuming.isClosed(), "The session that requested the resumption must not be closed."); + assertFalse(sm.isEnabled(), "Stream Management must not be enabled on the session that requested the resumption."); + assertFalse(original.isClosed(), "The session that was to be resumed must not be closed."); + } + + /** + * Verifies that an inline resume request (XEP-0198 § 9.2) that is refused returns a {@code } element for + * the caller to include in its response, without delivering anything itself, so that the client can proceed with + * resource binding. + */ + @Test + void refusedInlineResumeRequestReturnsFailedElement() throws Exception + { + // Setup test fixture. + final LocalClientSession original = resumableClientSession(); + final String previd = enableResumption(original, StreamManager.NAMESPACE_V3); + doReturn(null).when(routingTable).getClientRoute(FULL_JID); + final LocalClientSession resuming = resumingClientSession(); + + // Execute system under test. + final Sasl2ResumeResult result = new StreamManager(resuming).processSasl2Resume(ResumeRequest.from(resume(StreamManager.NAMESPACE_V3, previd, 0))); + + // Verify result. + assertFalse(result.isSuccess(), "The inline resume request must be refused."); + assertFailed(result.getResultElement(), StreamManager.NAMESPACE_V3, PacketError.Condition.item_not_found); + verify(resuming, never().description("Nothing must be delivered for an inline resume request: the caller includes the outcome in its response.")).deliverRawText(anyString()); + verify(original, never().description("No session must be resumed.")).reattachForSasl2(any()); + } + + /** + * Verifies that unacknowledged messages are routed again, in order, when a stream is not resumed, as XEP-0198 § 4 + * has them treated as stanzas for an unavailable resource. + */ + @Test + void unacknowledgedMessagesAreRoutedAgainOnClose() throws Exception + { + // Setup test fixture. + final LocalClientSession session = resumableClientSession(); + enableResumption(session, StreamManager.NAMESPACE_V3); + final StreamManager sm = session.getStreamManager(); + sm.sentStanza(message("1")); + sm.sentStanza(message("2")); + sm.process(ack(StreamManager.NAMESPACE_V3, "1")); + final PacketRouter router = mock(PacketRouter.class); + + // Execute system under test. + sm.onClose(router, SERVER_ADDRESS); + + // Verify result. + assertEquals(List.of("2"), routedIds(router), "The message that was not acknowledged must be routed again, and the acknowledged one must not."); + } + + /** + * Verifies that an unacknowledged message that is routed again when a stream is not resumed has a delay element with + * the time at which it was originally sent, as XEP-0198 § 4 recommends. + */ + @Test + void unacknowledgedMessageRoutedOnCloseHasDelayElement() throws Exception + { + // Setup test fixture. + final LocalClientSession session = resumableClientSession(); + enableResumption(session, StreamManager.NAMESPACE_V3); + final StreamManager sm = session.getStreamManager(); + final Instant before = Instant.now().truncatedTo(ChronoUnit.SECONDS); + sm.sentStanza(message("1")); + final Instant after = Instant.now(); + final PacketRouter router = mock(PacketRouter.class); + + // Execute system under test. + sm.onClose(router, SERVER_ADDRESS); + + // Verify result. + final List routed = routed(router); + assertEquals(1, routed.size(), "The unacknowledged message must be routed again, but routed were: " + routed); + final Element delay = routed.get(0).getElement().element(QName.get("delay", "urn:xmpp:delay")); + assertNotNull(delay, "The message must have a delay element (XEP-0203), but was: " + routed.get(0).toXML()); + final Instant stamp = new XMPPDateTimeFormat().parseString(delay.attributeValue("stamp")).toInstant(); + assertFalse(stamp.isBefore(before) || stamp.isAfter(after), "The delay element must have the time at which the message was originally sent, but was: " + stamp); + } + + /** + * Verifies that an unacknowledged IQ request is answered with a 'service-unavailable' error when a stream is not + * resumed. XEP-0198 § 4 has it treated as a stanza for an unavailable resource, for which RFC 6121 § 8.5.3.2.3 + * defines that error, and RFC 6120 § 8.2.3 requires a response to every IQ request. + */ + @Test + void unacknowledgedIqRequestIsAnsweredWithErrorOnClose() throws Exception + { + // Setup test fixture. + final LocalClientSession session = resumableClientSession(); + enableResumption(session, StreamManager.NAMESPACE_V3); + final StreamManager sm = session.getStreamManager(); + sm.sentStanza(iq("request", IQ.Type.get)); + final PacketRouter router = mock(PacketRouter.class); + + // Execute system under test. + sm.onClose(router, SERVER_ADDRESS); + + // Verify result. + final List routed = routed(router); + assertEquals(1, routed.size(), "An error must be returned for the unacknowledged IQ request, but routed were: " + routed); + final IQ error = assertInstanceOf(IQ.class, routed.get(0), "The response to an IQ request must be an IQ."); + assertEquals(IQ.Type.error, error.getType(), "The response must be an error."); + assertEquals("request", error.getID(), "The error must have the ID of the request."); + assertEquals(OTHER_ENTITY, error.getTo(), "The error must be addressed to the sender of the request."); + assertEquals(PacketError.Condition.service_unavailable, error.getError().getCondition(), "The error must have the 'service-unavailable' condition."); + } + + /** + * Verifies that unacknowledged IQ responses are discarded when a stream is not resumed, as RFC 6120 § 8.2.3 does + * not allow a response to an IQ response. + */ + @ParameterizedTest + @ValueSource(strings = {"result", "error"}) + void unacknowledgedIqResponseIsDiscardedOnClose(final String type) throws Exception + { + // Setup test fixture. + final LocalClientSession session = resumableClientSession(); + enableResumption(session, StreamManager.NAMESPACE_V3); + final StreamManager sm = session.getStreamManager(); + sm.sentStanza(iq("response", IQ.Type.valueOf(type))); + final PacketRouter router = mock(PacketRouter.class); + + // Execute system under test. + sm.onClose(router, SERVER_ADDRESS); + + // Verify result. + assertEquals(List.of(), routed(router), "Nothing must be routed for an unacknowledged IQ response."); + } + + /** + * Verifies that unacknowledged presence is discarded when a stream is not resumed, as RFC 6121 § 8.5.3.2.2 has a + * server silently ignore presence for an unavailable resource. + */ + @Test + void unacknowledgedPresenceIsDiscardedOnClose() throws Exception + { + // Setup test fixture. + final LocalClientSession session = resumableClientSession(); + enableResumption(session, StreamManager.NAMESPACE_V3); + final StreamManager sm = session.getStreamManager(); + sm.sentStanza(presence("presence")); + final PacketRouter router = mock(PacketRouter.class); + + // Execute system under test. + sm.onClose(router, SERVER_ADDRESS); + + // Verify result. + assertEquals(List.of(), routed(router), "Nothing must be routed for unacknowledged presence."); + } + + /** + * A mock of a detached client session that can be resumed: it has a full JID, a stream ID and a non-anonymous + * authentication token, and is the route for its full JID. + */ + private LocalClientSession resumableClientSession() + { + final LocalClientSession session = mock(LocalClientSession.class); + final AuthToken authToken = mock(AuthToken.class); + doReturn(FULL_JID.getNode()).when(authToken).getUsername(); + doReturn(authToken).when(session).getAuthToken(); + doReturn(true).when(session).isAuthenticated(); + doReturn(true).when(session).isDetached(); + doReturn(FULL_JID).when(session).getAddress(); + doReturn(BasicStreamIDFactory.createStreamID(STREAM_ID)).when(session).getStreamID(); + doReturn(session).when(routingTable).getClientRoute(FULL_JID); + mockCloseTracking(session); + return session; + } + + /** + * A mock of a new client session from which a resumption is requested: it authenticated as the same user, but did + * not bind a resource. + */ + private static LocalClientSession resumingClientSession() + { + final LocalClientSession session = mock(LocalClientSession.class); + final AuthToken authToken = mock(AuthToken.class); + doReturn(FULL_JID.getNode()).when(authToken).getUsername(); + doReturn(authToken).when(session).getAuthToken(); + doReturn(false).when(session).isAuthenticated(); + doReturn(Fixtures.XMPP_DOMAIN).when(session).getServerName(); + doReturn(BasicStreamIDFactory.createStreamID("resuming-stream-id")).when(session).getStreamID(); + mockCloseTracking(session); + return session; + } + + /** + * Makes the session report itself as closed once it is closed, through either close() or close(StreamError). A mock + * does not have one of these invoke the other, so verifying only one of them would miss a close through the other. + */ + private static void mockCloseTracking(final LocalClientSession session) + { + final AtomicBoolean closed = new AtomicBoolean(false); + doAnswer(invocation -> { closed.set(true); return null; }).when(session).close(); + doAnswer(invocation -> { closed.set(true); return null; }).when(session).close(nullable(StreamError.class)); + doAnswer(invocation -> closed.get()).when(session).isClosed(); + } + + /** + * Enables Stream Management with resumption on a session, and returns the SM-ID from the {@code } element. + * The stream manager is then available from the session. + */ + private static String enableResumption(final LocalClientSession session, final String namespace) throws DocumentException + { + final StreamManager sm = new StreamManager(session); + doReturn(sm).when(session).getStreamManager(); + final Element enable = DocumentHelper.createElement(QName.get("enable", namespace)); + enable.addAttribute("resume", "true"); + sm.process(enable); + assertTrue(sm.getResume(), "Test fixture expects a resumable stream."); + final String smId = lastDelivered(session).attributeValue("id"); + clearInvocations(session); + return smId; + } + + /** + * Gives a session a new connection, as reattaching it to the connection of the session that resumed it does. + */ + private static Connection reattached(final LocalClientSession session) + { + final Connection connection = mock(Connection.class); + doReturn(false).when(session).isDetached(); + doReturn(connection).when(session).getConnection(); + return connection; + } + + private static Element resume(final String namespace, final String previd, final long h) + { + return DocumentHelper.createElement(QName.get("resume", namespace)).addAttribute("previd", previd).addAttribute("h", String.valueOf(h)); + } + + private static Element ack(final String namespace, final String h) + { + return DocumentHelper.createElement(QName.get("a", namespace)).addAttribute("h", h); + } + + private static Message message(final String id) + { + final Message message = new Message(); + message.setID(id); + message.setFrom(OTHER_ENTITY); + message.setTo(FULL_JID); + return message; + } + + private static Presence presence(final String id) + { + final Presence presence = new Presence(); + presence.setID(id); + presence.setFrom(OTHER_ENTITY); + presence.setTo(FULL_JID); + return presence; + } + + private static IQ iq(final String id, final IQ.Type type) + { + final IQ iq = new IQ(type, id); + iq.setFrom(OTHER_ENTITY); + iq.setTo(FULL_JID); + if (type == IQ.Type.get) { + iq.setChildElement("query", "jabber:iq:version"); + } + return iq; + } + + /** + * Returns the last element that was delivered to the session as raw text. + */ + private static Element lastDelivered(final LocalSession session) throws DocumentException + { + final ArgumentCaptor captor = ArgumentCaptor.forClass(String.class); + verify(session, atLeastOnce().description("Expected something to be delivered to the session.")).deliverRawText(captor.capture()); + return DocumentHelper.parseText(captor.getValue()).getRootElement(); + } + + /** + * Returns the invocations of a method of a mock, in the order in which they were made. Their sequence numbers + * order them relative to invocations on other mocks. + */ + private static List invocations(final Object mock, final String methodName) + { + return mockingDetails(mock).getInvocations().stream() + .filter(invocation -> invocation.getMethod().getName().equals(methodName)) + .toList(); + } + + /** + * Checks whether raw text is an XML element with the given name. + */ + private static boolean isElement(final String xml, final QName name) + { + try { + return DocumentHelper.parseText(xml).getRootElement().getQName().equals(name); + } catch (DocumentException e) { + return false; + } + } + + /** + * Returns the first element that was delivered to the connection as raw text. + */ + private static Element firstRawTextDelivered(final Connection connection) throws DocumentException + { + final ArgumentCaptor captor = ArgumentCaptor.forClass(String.class); + verify(connection, atLeastOnce().description("Expected something to be delivered to the connection.")).deliverRawText(captor.capture()); + return DocumentHelper.parseText(captor.getAllValues().get(0)).getRootElement(); + } + + /** + * Returns the IDs of the stanzas that were delivered to the connection, in order. + */ + private static List deliveredIds(final Connection connection) throws Exception + { + final ArgumentCaptor captor = ArgumentCaptor.forClass(Packet.class); + verify(connection, atLeast(0)).deliver(captor.capture()); + return captor.getAllValues().stream().map(Packet::getID).toList(); + } + + /** + * Returns the stanzas that were routed, in order, through any of the methods of the router that route a stanza. + */ + private static List routed(final PacketRouter router) + { + return mockingDetails(router).getInvocations().stream() + .filter(invocation -> invocation.getMethod().getName().equals("route")) + .map(invocation -> (Packet) invocation.getArgument(0)) + .toList(); + } + + /** + * Returns the IDs of the stanzas that were routed, in order. + */ + private static List routedIds(final PacketRouter router) + { + return routed(router).stream().map(Packet::getID).toList(); + } + + /** + * Returns the IDs of the messages that are unacknowledged, in order. This closes the stream manager, as it uses the + * routing of unacknowledged messages on close to observe them. + */ + private static List unacknowledgedMessageIds(final StreamManager sm) + { + final PacketRouter router = mock(PacketRouter.class); + sm.onClose(router, SERVER_ADDRESS); + return routedIds(router); + } + + private static void assertFailed(final Element failed, final String namespace, final PacketError.Condition condition) + { + assertEquals(QName.get("failed", namespace), failed.getQName(), "The response must be a 'failed' element in the namespace of the request, but was: " + failed.asXML()); + assertNotNull(failed.element(QName.get(condition.toXMPP(), "urn:ietf:params:xml:ns:xmpp-stanzas")), "The response must report the '" + condition.toXMPP() + "' condition, but was: " + failed.asXML()); + } +} diff --git a/xmppserver/src/test/java/org/jivesoftware/openfire/streammanagement/StreamManagerTest.java b/xmppserver/src/test/java/org/jivesoftware/openfire/streammanagement/StreamManagerTest.java index 1b1ee21306..48574e0e3e 100644 --- a/xmppserver/src/test/java/org/jivesoftware/openfire/streammanagement/StreamManagerTest.java +++ b/xmppserver/src/test/java/org/jivesoftware/openfire/streammanagement/StreamManagerTest.java @@ -37,6 +37,7 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; import org.junit.jupiter.params.provider.ValueSource; import org.mockito.ArgumentCaptor; import org.mockito.InOrder; @@ -49,6 +50,8 @@ import java.lang.reflect.Field; import java.math.BigInteger; +import java.nio.charset.StandardCharsets; +import java.util.Base64; import java.util.List; import java.util.Locale; import java.util.concurrent.CompletableFuture; @@ -71,6 +74,7 @@ public class StreamManagerTest private static final JID FULL_JID = new JID("johndoe", Fixtures.XMPP_DOMAIN, "test-resource", true); private static final JID SERVER_ADDRESS = new JID(Fixtures.XMPP_DOMAIN); private static final String STREAM_ID = "test-stream-id"; + private static final int DETACH_TIME_MILLIS = 120_000; // OF-3433: Names of private fields, used to reach the roll-over at 2^32 without sending that many stanzas. private static final String HANDLED_COUNTER_FIELD = "serverProcessedStanzas"; @@ -90,6 +94,7 @@ public void setUp() { final XMPPServer server = Fixtures.mockXMPPServer(); sessionManager = server.getSessionManager(); + doReturn(DETACH_TIME_MILLIS).when(sessionManager).getSessionDetachTime(); //noinspection deprecation XMPPServer.setInstance(server); @@ -477,6 +482,352 @@ void detachesSessionBeforeClosingItsConnection() inOrder.verify(oldConnection).close(any(StreamError.class)); } + /** + * Verifies that enabling Stream Management without requesting resumption results in an {@code } element + * in the namespace of the request, without any of the attributes that relate to resumption. + */ + @ParameterizedTest + @ValueSource(strings = {StreamManager.NAMESPACE_V2, StreamManager.NAMESPACE_V3}) + void enableWithoutResume(final String namespace) throws Exception + { + // Setup test fixture. + final LocalClientSession session = authenticatedClientSession(); + final StreamManager sm = new StreamManager(session); + + // Execute system under test. + sm.process(smElement("enable", namespace)); + + // Verify result. + final Element enabled = lastDelivered(session); + assertEquals(QName.get("enabled", namespace), enabled.getQName(), "The response must be an 'enabled' element in the namespace of the request."); + assertTrue(enabled.attributes().isEmpty(), "No resumption attributes are expected when resumption was not requested, but got: " + enabled.asXML()); + assertTrue(sm.isEnabled(), "Stream Management must be enabled after a successful request."); + assertFalse(sm.getResume(), "The stream must not be resumable when resumption was not requested."); + } + + /** + * Verifies that enabling Stream Management with resumption results in an {@code } element that contains + * a resumption ID (based on the resource and stream ID), the cluster node to resume on, and the maximum resumption + * time in seconds. + */ + @Test + void enableWithResumeV3() throws Exception + { + // Setup test fixture. + final LocalClientSession session = authenticatedClientSession(); + final StreamManager sm = new StreamManager(session); + final Element enable = smElement("enable", StreamManager.NAMESPACE_V3); + enable.addAttribute("resume", "true"); + + // Execute system under test. + sm.process(enable); + + // Verify result. + final Element enabled = lastDelivered(session); + assertEquals(QName.get("enabled", StreamManager.NAMESPACE_V3), enabled.getQName(), "The response must be an 'enabled' element in the namespace of the request."); + assertEquals("true", enabled.attributeValue("resume"), "The response must confirm that the stream is resumable."); + assertEquals(Base64.getEncoder().encodeToString((FULL_JID.getResource() + "\0" + STREAM_ID).getBytes(StandardCharsets.UTF_8)), enabled.attributeValue("id"), "The resumption ID must be based on the resource and stream ID."); + assertEquals(Fixtures.HOSTNAME, enabled.attributeValue("location"), "The response must identify the cluster node to resume on."); + assertEquals(String.valueOf(DETACH_TIME_MILLIS / 1000), enabled.attributeValue("max"), "The response must report the session detach time, in seconds."); + assertTrue(sm.isEnabled(), "Stream Management must be enabled after a successful request."); + assertTrue(sm.getResume(), "The stream must be resumable."); + } + + /** + * Verifies that the location hint is not sent in the older version of the protocol, while the other attributes + * that relate to resumption are. + */ + @Test + void enableWithResumeV2HasNoLocation() throws Exception + { + // Setup test fixture. + final LocalClientSession session = authenticatedClientSession(); + final StreamManager sm = new StreamManager(session); + final Element enable = smElement("enable", StreamManager.NAMESPACE_V2); + enable.addAttribute("resume", "true"); + + // Execute system under test. + sm.process(enable); + + // Verify result. + final Element enabled = lastDelivered(session); + assertEquals(QName.get("enabled", StreamManager.NAMESPACE_V2), enabled.getQName(), "The response must be an 'enabled' element in the namespace of the request."); + assertEquals("true", enabled.attributeValue("resume"), "The response must confirm that the stream is resumable."); + assertNotNull(enabled.attributeValue("id"), "The response must contain a resumption ID."); + assertNull(enabled.attributeValue("location"), "The location hint must not be sent in version 2 of the protocol."); + assertEquals(String.valueOf(DETACH_TIME_MILLIS / 1000), enabled.attributeValue("max"), "The response must report the session detach time, in seconds."); + assertTrue(sm.getResume(), "The stream must be resumable."); + } + + /** + * Verifies that the location hint is left out when it is disabled by configuration. + */ + @Test + void enableWithResumeOmitsLocationWhenDisabled() throws Exception + { + // Setup test fixture. + StreamManager.LOCATION_ENABLED.setValue(false); + final LocalClientSession session = authenticatedClientSession(); + final StreamManager sm = new StreamManager(session); + final Element enable = smElement("enable", StreamManager.NAMESPACE_V3); + enable.addAttribute("resume", "true"); + + // Execute system under test. + sm.process(enable); + + // Verify result. + assertNull(lastDelivered(session).attributeValue("location"), "The location hint must not be sent when it is disabled by configuration."); + } + + /** + * Verifies that the maximum resumption time is left out when it is disabled by configuration. + */ + @Test + void enableWithResumeOmitsMaxWhenDisabled() throws Exception + { + // Setup test fixture. + StreamManager.MAX_SERVER_ENABLED.setValue(false); + final LocalClientSession session = authenticatedClientSession(); + final StreamManager sm = new StreamManager(session); + final Element enable = smElement("enable", StreamManager.NAMESPACE_V3); + enable.addAttribute("resume", "true"); + + // Execute system under test. + sm.process(enable); + + // Verify result. + assertNull(lastDelivered(session).attributeValue("max"), "The maximum resumption time must not be sent when it is disabled by configuration."); + } + + /** + * Verifies which values of the 'resume' attribute request resumption. + */ + @ParameterizedTest + @CsvSource({"true,true", "TRUE,true", "yes,true", "YES,true", "1,true", "false,false", "0,false", "no,false", "foobar,false"}) + void enableInterpretsResumeAttribute(final String value, final boolean expected) throws Exception + { + // Setup test fixture. + final LocalClientSession session = authenticatedClientSession(); + final StreamManager sm = new StreamManager(session); + final Element enable = smElement("enable", StreamManager.NAMESPACE_V3); + enable.addAttribute("resume", value); + + // Execute system under test. + sm.process(enable); + + // Verify result. + assertEquals(expected, sm.getResume(), "Unexpected interpretation of resume attribute value '" + value + "'."); + assertEquals(expected ? "true" : null, lastDelivered(session).attributeValue("resume"), "The response must reflect the interpretation of resume attribute value '" + value + "'."); + } + + /** + * Verifies that an anonymous session can enable Stream Management, but does not get a resumable stream. + */ + @Test + void enableWithResumeForAnonymousSessionIsNotResumable() throws Exception + { + // Setup test fixture. + final LocalClientSession session = authenticatedClientSession(); + final AuthToken authToken = session.getAuthToken(); + doReturn(true).when(authToken).isAnonymous(); + final StreamManager sm = new StreamManager(session); + final Element enable = smElement("enable", StreamManager.NAMESPACE_V3); + enable.addAttribute("resume", "true"); + + // Execute system under test. + sm.process(enable); + + // Verify result. + final Element enabled = lastDelivered(session); + assertEquals("enabled", enabled.getName(), "An anonymous session must be able to enable Stream Management."); + assertTrue(enabled.attributes().isEmpty(), "No resumption attributes are expected for an anonymous session, but got: " + enabled.asXML()); + assertTrue(sm.isEnabled(), "Stream Management must be enabled after a successful request."); + assertFalse(sm.getResume(), "The stream of an anonymous session must not be resumable."); + } + + /** + * Verifies that a session that is not a client session can enable Stream Management, but does not get a resumable + * stream. + */ + @Test + void enableWithResumeForNonClientSessionIsNotResumable() throws Exception + { + // Setup test fixture. + final LocalSession session = mock(LocalSession.class); + doReturn(true).when(session).isAuthenticated(); + final StreamManager sm = new StreamManager(session); + final Element enable = smElement("enable", StreamManager.NAMESPACE_V3); + enable.addAttribute("resume", "true"); + + // Execute system under test. + sm.process(enable); + + // Verify result. + final Element enabled = lastDelivered(session); + assertEquals("enabled", enabled.getName(), "A session that is not a client session must be able to enable Stream Management."); + assertTrue(enabled.attributes().isEmpty(), "No resumption attributes are expected for a session that is not a client session, but got: " + enabled.asXML()); + assertFalse(sm.getResume(), "The stream of a session that is not a client session must not be resumable."); + } + + /** + * Verifies that Stream Management cannot be enabled before the session is authenticated. + */ + @ParameterizedTest + @ValueSource(strings = {StreamManager.NAMESPACE_V2, StreamManager.NAMESPACE_V3}) + void enableBeforeAuthenticationFails(final String namespace) throws Exception + { + // Setup test fixture. + final LocalClientSession session = authenticatedClientSession(); + doReturn(false).when(session).isAuthenticated(); + final StreamManager sm = new StreamManager(session); + + // Execute system under test. + sm.process(smElement("enable", namespace)); + + // Verify result. + assertFailedWithUnexpectedRequest(lastDelivered(session), namespace); + assertFalse(sm.isEnabled(), "Stream Management must not be enabled on a session that is not authenticated."); + } + + /** + * Verifies that a second request to enable Stream Management fails, leaving the state from the first request intact. + */ + @Test + void enableTwiceFails() throws Exception + { + // Setup test fixture. + final LocalClientSession session = authenticatedClientSession(); + final StreamManager sm = enabledStreamManager(session, StreamManager.NAMESPACE_V3); + sm.incrementServerProcessedStanzas(); + + // Execute system under test. + sm.process(smElement("enable", StreamManager.NAMESPACE_V2)); + + // Verify result. + assertFailedWithUnexpectedRequest(lastDelivered(session), StreamManager.NAMESPACE_V2); + assertTrue(sm.isEnabled(), "Stream Management must remain enabled after a second request failed."); + + clearInvocations(session); + sm.process(smElement("r", StreamManager.NAMESPACE_V3)); + final Element ack = lastDelivered(session); + assertEquals(QName.get("a", StreamManager.NAMESPACE_V3), ack.getQName(), "The namespace of the first request must remain in use."); + assertEquals("1", ack.attributeValue("h"), "The count of handled stanzas must not be affected by a second request."); + } + + /** + * Verifies that an {@code } is answered with the number of stanzas that were handled since enabling. + */ + @ParameterizedTest + @ValueSource(strings = {StreamManager.NAMESPACE_V2, StreamManager.NAMESPACE_V3}) + void requestIsAnsweredWithCountOfHandledStanzas(final String namespace) throws Exception + { + // Setup test fixture. + final LocalClientSession session = authenticatedClientSession(); + final StreamManager sm = enabledStreamManager(session, namespace); + sm.incrementServerProcessedStanzas(); + sm.incrementServerProcessedStanzas(); + sm.incrementServerProcessedStanzas(); + + // Execute system under test. + sm.process(smElement("r", namespace)); + + // Verify result. + final Element ack = lastDelivered(session); + assertEquals(QName.get("a", namespace), ack.getQName(), "An 'a' element must be sent, in the namespace of the stream."); + assertEquals("3", ack.attributeValue("h"), "The count must equal the number of handled stanzas."); + } + + /** + * Verifies that an {@code } is answered with zero when no stanzas have been handled. + */ + @Test + void requestIsAnsweredWithZeroWhenNoStanzasWereHandled() throws Exception + { + // Setup test fixture. + final LocalClientSession session = authenticatedClientSession(); + final StreamManager sm = enabledStreamManager(session, StreamManager.NAMESPACE_V3); + + // Execute system under test. + sm.process(smElement("r", StreamManager.NAMESPACE_V3)); + + // Verify result. + assertEquals("0", lastDelivered(session).attributeValue("h"), "The count must be zero when no stanzas were handled."); + } + + /** + * Verifies that the count of handled stanzas is cumulative: it is not reset by reporting it. + */ + @Test + void countOfHandledStanzasIsCumulative() throws Exception + { + // Setup test fixture. + final LocalClientSession session = authenticatedClientSession(); + final StreamManager sm = enabledStreamManager(session, StreamManager.NAMESPACE_V3); + sm.incrementServerProcessedStanzas(); + sm.process(smElement("r", StreamManager.NAMESPACE_V3)); + sm.incrementServerProcessedStanzas(); + + // Execute system under test. + sm.process(smElement("r", StreamManager.NAMESPACE_V3)); + + // Verify result. + assertEquals("2", lastDelivered(session).attributeValue("h"), "The count must include stanzas handled before the previous acknowledgement."); + } + + /** + * Verifies that stanzas handled before Stream Management was enabled are not counted. + */ + @Test + void stanzasHandledBeforeEnablingAreNotCounted() throws Exception + { + // Setup test fixture. + final LocalClientSession session = authenticatedClientSession(); + final StreamManager sm = new StreamManager(session); + sm.incrementServerProcessedStanzas(); + sm.process(smElement("enable", StreamManager.NAMESPACE_V3)); + + // Execute system under test. + sm.process(smElement("r", StreamManager.NAMESPACE_V3)); + + // Verify result. + assertEquals("0", lastDelivered(session).attributeValue("h"), "Stanzas handled before enabling Stream Management must not be counted."); + } + + /** + * Verifies that an {@code } is silently ignored when Stream Management is not enabled. + */ + @Test + void requestIsIgnoredWhenNotEnabled() + { + // Setup test fixture. + final LocalClientSession session = authenticatedClientSession(); + final StreamManager sm = new StreamManager(session); + + // Execute system under test. + sm.process(smElement("r", StreamManager.NAMESPACE_V3)); + + // Verify result. + verify(session, never().description("Nothing is to be sent in response to a request when Stream Management is not enabled.")).deliverRawText(anyString()); + } + + /** + * Verifies that no acknowledgement is sent on a detached session. + */ + @Test + void requestIsNotAnsweredOnDetachedSession() + { + // Setup test fixture. + final LocalClientSession session = authenticatedClientSession(); + final StreamManager sm = enabledStreamManager(session, StreamManager.NAMESPACE_V3); + doReturn(true).when(session).isDetached(); + + // Execute system under test. + sm.process(smElement("r", StreamManager.NAMESPACE_V3)); + + // Verify result. + verify(session, never().description("Nothing can be sent on a detached session.")).deliverRawText(anyString()); + } + /** * Verifies that an {@code } removes the stanzas that it acknowledges, and no others. */ @@ -518,6 +869,25 @@ void acknowledgementOfAllStanzasRemovesAll() throws Exception assertFalse(session.isClosed(), "A valid acknowledgement must not close the session."); } + /** + * Verifies that an {@code } with a value of zero does not remove any stanzas. + */ + @Test + void acknowledgementOfZeroRemovesNothing() throws Exception + { + // Setup test fixture. + final LocalClientSession session = authenticatedClientSession(); + final StreamManager sm = enabledStreamManager(session, StreamManager.NAMESPACE_V3); + sm.sentStanza(message("1")); + + // Execute system under test. + sm.process(ack(StreamManager.NAMESPACE_V3, "0")); + + // Verify result. + assertEquals(List.of("1"), unacknowledgedMessageIds(sm), "A stanza must remain when no stanzas were acknowledged."); + assertFalse(session.isClosed(), "A valid acknowledgement must not close the session."); + } + /** * Verifies that stanzas are numbered on from the last acknowledged value, after all earlier stanzas were * acknowledged. @@ -560,6 +930,63 @@ void repeatedAcknowledgementIsAccepted() throws Exception assertFalse(session.isClosed(), "Repeating an acknowledgement must not close the session."); } + /** + * Verifies that stanzas that are sent before Stream Management is enabled are not tracked. + */ + @Test + void stanzasSentBeforeEnablingAreNotTracked() throws Exception + { + // Setup test fixture. + final LocalClientSession session = authenticatedClientSession(); + final StreamManager sm = new StreamManager(session); + sm.sentStanza(message("before")); + sm.process(smElement("enable", StreamManager.NAMESPACE_V3)); + + // Execute system under test. + sm.sentStanza(message("after")); + + // Verify result. + assertEquals(List.of("after"), unacknowledgedMessageIds(sm), "Only stanzas sent after enabling Stream Management must be tracked."); + } + + /** + * Verifies that an {@code } is silently ignored when Stream Management is not enabled, whatever its value. + */ + @Test + void acknowledgementIsIgnoredWhenNotEnabled() + { + // Setup test fixture. + final LocalClientSession session = authenticatedClientSession(); + final StreamManager sm = new StreamManager(session); + + // Execute system under test. + sm.process(ack(StreamManager.NAMESPACE_V3, "not-a-number")); + + // Verify result. + verify(session, never().description("Nothing is to be sent in response to an acknowledgement when Stream Management is not enabled.")).deliverRawText(anyString()); + assertFalse(session.isClosed(), "An acknowledgement must not close the session when Stream Management is not enabled."); + } + + /** + * Verifies that an {@code } without a 'h' attribute is silently ignored. + */ + @Test + void acknowledgementWithoutValueIsIgnored() throws Exception + { + // Setup test fixture. + final LocalClientSession session = authenticatedClientSession(); + final StreamManager sm = enabledStreamManager(session, StreamManager.NAMESPACE_V3); + sm.sentStanza(message("1")); + + // Execute system under test. + sm.process(smElement("a", StreamManager.NAMESPACE_V3)); + + // Verify result. + verify(session, never().description("An acknowledgement without a value does not cause anything to be sent.")).deliverRawText(anyString()); + assertFalse(session.isClosed(), "An acknowledgement without a value does not close the session."); + assertEquals(List.of("1"), unacknowledgedMessageIds(sm), "An acknowledgement without a value must not remove any stanzas."); + } + /** * Verifies that acknowledging a stanza while none were sent closes the session with a stream error. */ @@ -747,6 +1174,89 @@ void lowerAcknowledgementIsNotAnErrorWhenNoStanzasAreUnacknowledged() throws Exc assertEquals(List.of("4"), unacknowledgedMessageIds(sm), "A lower acknowledgement must not affect the numbers of the stanzas that follow."); } + /** + * Verifies that an {@code } is sent each time the number of unacknowledged stanzas reaches a multiple of the + * default request frequency. + */ + @Test + void acknowledgementIsRequestedAtDefaultFrequency() throws Exception + { + // Setup test fixture. + final LocalClientSession session = authenticatedClientSession(); + final StreamManager sm = enabledStreamManager(session, StreamManager.NAMESPACE_V3); + for (int i = 1; i <= 4; i++) { + sm.sentStanza(message(String.valueOf(i))); + } + verify(session, never().description("No acknowledgement is to be requested below the default frequency of 5.")).deliverRawText(anyString()); + + // Execute system under test. + sm.sentStanza(message("5")); + + // Verify result. + assertEquals(QName.get("r", StreamManager.NAMESPACE_V3), lastDelivered(session).getQName(), "An acknowledgement is to be requested at the default frequency of 5, in the namespace of the stream."); + } + + /** + * Verifies that the frequency at which an {@code } is sent can be configured. + */ + @Test + void acknowledgementIsRequestedAtConfiguredFrequency() + { + // Setup test fixture. + JiveGlobals.setProperty("stream.management.requestFrequency", "2"); + final LocalClientSession session = authenticatedClientSession(); + final StreamManager sm = enabledStreamManager(session, StreamManager.NAMESPACE_V3); + + // Execute system under test. + for (int i = 1; i <= 4; i++) { + sm.sentStanza(message(String.valueOf(i))); + } + + // Verify result. + verify(session, times(2).description("An acknowledgement is to be requested for every second unacknowledged stanza.")).deliverRawText(anyString()); + } + + /** + * Verifies that the request frequency is based on the number of stanzas that are unacknowledged, rather than the + * number of stanzas that were sent. + */ + @Test + void acknowledgementRequestDependsOnNumberOfUnacknowledgedStanzas() + { + // Setup test fixture. + JiveGlobals.setProperty("stream.management.requestFrequency", "2"); + final LocalClientSession session = authenticatedClientSession(); + final StreamManager sm = enabledStreamManager(session, StreamManager.NAMESPACE_V3); + sm.sentStanza(message("1")); + sm.process(ack(StreamManager.NAMESPACE_V3, "1")); + + // Execute system under test. + sm.sentStanza(message("2")); + + // Verify result. + verify(session, never().description("No acknowledgement is to be requested when only one stanza is unacknowledged.")).deliverRawText(anyString()); + } + + /** + * Verifies that no {@code } is sent on a detached session, while stanzas are still tracked. + */ + @Test + void acknowledgementIsNotRequestedOnDetachedSession() throws Exception + { + // Setup test fixture. + JiveGlobals.setProperty("stream.management.requestFrequency", "1"); + final LocalClientSession session = authenticatedClientSession(); + final StreamManager sm = enabledStreamManager(session, StreamManager.NAMESPACE_V3); + doReturn(true).when(session).isDetached(); + + // Execute system under test. + sm.sentStanza(message("1")); + + // Verify result. + verify(session, never().description("Nothing can be sent on a detached session.")).deliverRawText(anyString()); + assertEquals(List.of("1"), unacknowledgedMessageIds(sm), "A stanza must be tracked on a detached session."); + } + /** * Verifies that the reported count of handled stanzas rolls over to zero after 2^32-1, as defined by XEP-0198 § 4. */ @@ -1551,13 +2061,16 @@ private static Connection closableConnection() } /** - * A mock of an authenticated client session that has a connection, a full JID and a stream ID. + * A mock of an authenticated, non-anonymous client session that has a connection, a full JID and a stream ID. * * The session will set its closed flag when it is closed through invocation of either close() or close(StreamError) */ private static LocalClientSession authenticatedClientSession() { final LocalClientSession session = mock(LocalClientSession.class); + final AuthToken authToken = mock(AuthToken.class); + doReturn(false).when(authToken).isAnonymous(); + doReturn(authToken).when(session).getAuthToken(); doReturn(closableConnection()).when(session).getConnection(); doReturn(true).when(session).isAuthenticated(); doReturn(FULL_JID).when(session).getAddress(); From 22123e9df5d459729504ef25a7227eecc602b6a2 Mon Sep 17 00:00:00 2001 From: Guus der Kinderen Date: Wed, 4 Nov 2026 11:31:14 +0100 Subject: [PATCH 03/28] OF-2425: Name the Stream Management counters and methods after direction instead of client and server StreamManager named its counters, its queue of unacknowledged stanzas and its acknowledgement methods after the client and the server, such as serverProcessedStanzas and processClientAcknowledgement. That only holds while Openfire is the server end of the stream. Once Openfire opens server connections and takes the role that clients have today, the names would be the wrong way around. They are now named after the direction of the stanzas: inbound for those that Openfire receives from the peer, outbound for those that it sends to the peer. For example, serverProcessedStanzas is now handledInboundStanzas, unacknowledgedServerStanzas is unacknowledgedOutboundStanzas, sendServerAcknowledgement() is acknowledgeInboundStanzas() and processClientAcknowledgement() is processOutboundAcknowledgement(). Javadoc and log messages that used client and server in this sense now refer to the peer. Text that refers to the client role itself, such as for enabling and resuming Stream Management, and the text of stream errors that are sent to the peer, are unchanged. The two methods that were public, sendServerAcknowledgement() and incrementServerProcessedStanzas(), remain as deprecated delegates to their new names, for removal in or after Openfire 5.3.0, so that plugins that use them keep working, as ADR-010 describes. The tests only change where they refer to the renamed fields and methods. No functional change. --- .../openfire/session/LocalSession.java | 2 +- .../streammanagement/ResumeRequest.java | 2 +- .../streammanagement/StreamManager.java | 178 ++++++++++-------- .../StreamManagementPacketRouter.java | 2 +- .../StreamManagerResumptionTest.java | 2 +- .../streammanagement/StreamManagerTest.java | 62 +++--- 6 files changed, 137 insertions(+), 111 deletions(-) diff --git a/xmppserver/src/main/java/org/jivesoftware/openfire/session/LocalSession.java b/xmppserver/src/main/java/org/jivesoftware/openfire/session/LocalSession.java index f03afebad3..445804e662 100644 --- a/xmppserver/src/main/java/org/jivesoftware/openfire/session/LocalSession.java +++ b/xmppserver/src/main/java/org/jivesoftware/openfire/session/LocalSession.java @@ -430,7 +430,7 @@ public Instant getLastDetachedDate() { public void incrementClientPacketCount() { clientPacketCount.incrementAndGet(); lastActiveDate = System.currentTimeMillis(); - streamManager.incrementServerProcessedStanzas(); + streamManager.incrementHandledInboundStanzas(); } /** diff --git a/xmppserver/src/main/java/org/jivesoftware/openfire/streammanagement/ResumeRequest.java b/xmppserver/src/main/java/org/jivesoftware/openfire/streammanagement/ResumeRequest.java index c21815073f..36bb9d0883 100644 --- a/xmppserver/src/main/java/org/jivesoftware/openfire/streammanagement/ResumeRequest.java +++ b/xmppserver/src/main/java/org/jivesoftware/openfire/streammanagement/ResumeRequest.java @@ -119,7 +119,7 @@ private static ResumeRequest parse(@Nonnull final Element resumeElement, @Nonnul } if (h < 0 || h > StreamManager.MASK) { // XEP-0198 § 4: 'h' is an unsigned 32-bit integer. Out-of-range values are rejected here rather than - // later by StreamManager#validateClientAcknowledgement(long), which throws for them. + // later by StreamManager#validateOutboundAcknowledgement(long), which throws for them. throw new MalformedResumeRequestException("Stream resumption 'h' attribute must be an unsigned 32-bit integer, but was: " + h); } diff --git a/xmppserver/src/main/java/org/jivesoftware/openfire/streammanagement/StreamManager.java b/xmppserver/src/main/java/org/jivesoftware/openfire/streammanagement/StreamManager.java index 1e20c260c8..87af8aed96 100644 --- a/xmppserver/src/main/java/org/jivesoftware/openfire/streammanagement/StreamManager.java +++ b/xmppserver/src/main/java/org/jivesoftware/openfire/streammanagement/StreamManager.java @@ -45,7 +45,8 @@ /** * XEP-0198 Stream Manager. - * Handles client/server messages acknowledgement. + * Handles the acknowledgement of stanzas on a stream: inbound stanzas are those that Openfire receives from the peer, + * outbound stanzas are those that Openfire sends to the peer. * * @author jonnyheavey */ @@ -100,32 +101,30 @@ public static boolean isStreamManagementActive() { public static final String NAMESPACE_V3 = "urn:xmpp:sm:3"; /** - * Session (stream) to client. + * Session (stream) to the peer. */ private final LocalSession session; /** - * Namespace to be used in stanzas sent to client (depending on XEP-0198 version used by client) + * Namespace to be used in stanzas sent to the peer (depending on XEP-0198 version used by the peer) */ private String namespace; /** - * Count of how many stanzas/packets - * sent from the client that the server has processed + * Count of how many inbound stanzas/packets Openfire has handled */ - private AtomicLong serverProcessedStanzas = new AtomicLong( 0 ); + private AtomicLong handledInboundStanzas = new AtomicLong( 0 ); /** - * Count of how many stanzas/packets - * sent from the server that the client has processed + * Count of how many outbound stanzas/packets the peer has acknowledged as handled */ - private AtomicLong clientProcessedStanzas = new AtomicLong( 0 ); + private AtomicLong acknowledgedOutboundStanzas = new AtomicLong( 0 ); /** - * Whether the count of stanzas sent from the server that the client has processed has rolled over from (2^32)-1 to + * Whether the count of outbound stanzas that the peer has acknowledged as handled has rolled over from (2^32)-1 to * zero at least once. */ - private boolean clientProcessedStanzasRolledOver = false; + private boolean acknowledgedOutboundStanzasRolledOver = false; /** * Whether the number of unacknowledged stanzas exceeded the maximum, which causes the session to be closed. @@ -138,9 +137,9 @@ public static boolean isStreamManagementActive() { static final long MASK = new BigInteger( "2" ).pow( 32 ).longValue() - 1; /** - * Collection of stanzas/packets sent to client that haven't been acknowledged. + * Collection of outbound stanzas/packets that haven't been acknowledged. */ - private Deque unacknowledgedServerStanzas = new LinkedList<>(); + private Deque unacknowledgedOutboundStanzas = new LinkedList<>(); /** * Delegates that can determine if a detached session can be terminated. @@ -203,7 +202,7 @@ public void process( Element element ) resumeRequest = ResumeRequest.from(element); } catch (MalformedResumeRequestException e) { Log.info( "Closing client session that sent a malformed 'resume' request. Error message: {}. Affected session: {}", e.getMessage(), session ); - final StreamError error = new StreamError( StreamError.Condition.undefined_condition, e.getMessage() + " Our last unacknowledged stanza: " + (unacknowledgedServerStanzas.isEmpty() ? "(none)" : unacknowledgedServerStanzas.getLast().x) ); + final StreamError error = new StreamError( StreamError.Condition.undefined_condition, e.getMessage() + " Our last unacknowledged stanza: " + (unacknowledgedOutboundStanzas.isEmpty() ? "(none)" : unacknowledgedOutboundStanzas.getLast().x) ); // OF-3443: Unlike close(), this also makes the stream non-resumable (OF-2751). session.close( error ); return; @@ -212,10 +211,10 @@ public void process( Element element ) break; case "r": - sendServerAcknowledgement(); + acknowledgeInboundStanzas(); break; case "a": - processClientAcknowledgement(element); + processOutboundAcknowledgement(element); break; default: sendUnexpectedError(); @@ -511,7 +510,7 @@ private ResumeRequestValidationResult validateResumeRequest(@Nonnull final Resum Log.debug("Not allowing a client of '{}' to resume a session, the session to be resumed used a different version ({}) of the session management resumption feature as compared to the version that's requested now: {}.", fullJid, otherSession.getStreamManager().namespace, namespace); return ResumeRequestValidationResult.failure(PacketError.Condition.unexpected_request); } - if (!otherSession.getStreamManager().validateClientAcknowledgement(h)) { + if (!otherSession.getStreamManager().validateOutboundAcknowledgement(h)) { Log.debug("Not allowing a client of '{}' to resume a session, as it reports it received more stanzas from us than that we've send it.", fullJid); return ResumeRequestValidationResult.failure(PacketError.Condition.unexpected_request); } @@ -529,23 +528,33 @@ public void formalClose() { } /** - * Sends XEP-0198 acknowledgement <a /> to client from server + * Sends XEP-0198 acknowledgement <a /> of inbound stanzas to the peer */ - public void sendServerAcknowledgement() { + public void acknowledgeInboundStanzas() { if(isEnabled()) { if (session.isDetached()) { Log.debug("Session is detached, won't request an ack."); return; } - String ack = String.format("", namespace, serverProcessedStanzas.get() & MASK ); + String ack = String.format("", namespace, handledInboundStanzas.get() & MASK ); session.deliverRawText( ack ); } } /** - * Sends XEP-0198 request to client from server + * Sends XEP-0198 acknowledgement <a /> of inbound stanzas to the peer + * + * @deprecated Use {@link #acknowledgeInboundStanzas()} instead. */ - private void sendServerRequest() { + @Deprecated(forRemoval = true, since = "5.2.0") // Remove in or after Openfire 5.3.0 + public void sendServerAcknowledgement() { + acknowledgeInboundStanzas(); + } + + /** + * Sends XEP-0198 request to the peer, for an acknowledgement of outbound stanzas + */ + private void requestOutboundAcknowledgement() { if(isEnabled()) { if (session.isDetached()) { Log.debug("Session is detached, won't request an ack."); @@ -589,22 +598,22 @@ private static Element buildFailedElement(@Nonnull final String namespace, @Nonn } /** - * Checks if the amount of stanzas that the client acknowledges is not higher than the amount of stanzas that we've - * sent to the client. + * Checks if the amount of stanzas that the peer acknowledges is not higher than the amount of stanzas that we've + * sent to the peer. * - * @param h Then number of stanzas that the client acknowledges it has received from us. - * @return false if we sent fewer stanzas to the client than the number it is acknowledging. + * @param h Then number of stanzas that the peer acknowledges it has received from us. + * @return false if we sent fewer stanzas to the peer than the number it is acknowledging. */ - private synchronized boolean validateClientAcknowledgement(long h) { + private synchronized boolean validateOutboundAcknowledgement(long h) { if (h < 0) { throw new IllegalArgumentException("Argument 'h' cannot be negative, but was: " + h); } if (h > MASK) { throw new IllegalArgumentException("Argument 'h' cannot be larger than 2^32 -1, but was: " + h); } - final long oldH = clientProcessedStanzas.get(); - final Long lastUnackedX = unacknowledgedServerStanzas.isEmpty() ? null : unacknowledgedServerStanzas.getLast().x; - return validateClientAcknowledgement(h, oldH, lastUnackedX, clientProcessedStanzasRolledOver); + final long oldH = acknowledgedOutboundStanzas.get(); + final Long lastUnackedX = unacknowledgedOutboundStanzas.isEmpty() ? null : unacknowledgedOutboundStanzas.getLast().x; + return validateOutboundAcknowledgement(h, oldH, lastUnackedX, acknowledgedOutboundStanzasRolledOver); } /** @@ -615,11 +624,11 @@ private synchronized boolean validateClientAcknowledgement(long h) { * @param oldH The last acknowledged value. * @param lastUnackedX The number of the last stanza sent, or null if all stanzas that were sent are acknowledged. * @return false if the value is higher than the number of stanzas that were sent. - * @see #validateClientAcknowledgement(long, long, Long, boolean) + * @see #validateOutboundAcknowledgement(long, long, Long, boolean) */ @VisibleForTesting - static boolean validateClientAcknowledgement(final long h, final long oldH, final Long lastUnackedX) { - return validateClientAcknowledgement(h, oldH, lastUnackedX, false); + static boolean validateOutboundAcknowledgement(final long h, final long oldH, final Long lastUnackedX) { + return validateOutboundAcknowledgement(h, oldH, lastUnackedX, false); } /** @@ -641,7 +650,7 @@ static boolean validateClientAcknowledgement(final long h, final long oldH, fina * @return false if the value is higher than the number of stanzas that were sent. */ @VisibleForTesting - static boolean validateClientAcknowledgement(final long h, final long oldH, final Long lastUnackedX, final boolean rolledOver) { + static boolean validateOutboundAcknowledgement(final long h, final long oldH, final Long lastUnackedX, final boolean rolledOver) { final long lastSentX = lastUnackedX == null ? oldH : lastUnackedX; if (isInRange(h, oldH, lastSentX)) { return true; @@ -664,51 +673,51 @@ private static boolean isInRange(final long value, final long first, final long } /** - * Process client acknowledgements for a given value of h. + * Process acknowledgements of outbound stanzas for a given value of h. * * @param h Last handled stanza to be acknowledged. */ - private void processClientAcknowledgement(long h) { + private void processOutboundAcknowledgement(long h) { synchronized (this) { - if ( !validateClientAcknowledgement(h) ) { + if ( !validateOutboundAcknowledgement(h) ) { // All paths leading up to here should have checked for this. Race condition? - throw new IllegalStateException( "Client acknowledges stanzas that we didn't send! Client Ack h: "+h+", our last unacknowledged stanza: " + (unacknowledgedServerStanzas.isEmpty() ? "(none)" : unacknowledgedServerStanzas.getLast().x) ); + throw new IllegalStateException( "Peer acknowledges stanzas that we didn't send! Peer Ack h: "+h+", our last unacknowledged stanza: " + (unacknowledgedOutboundStanzas.isEmpty() ? "(none)" : unacknowledgedOutboundStanzas.getLast().x) ); } - final long oldH = clientProcessedStanzas.get(); - final long lastSentX = unacknowledgedServerStanzas.isEmpty() ? oldH : unacknowledgedServerStanzas.getLast().x; + final long oldH = acknowledgedOutboundStanzas.get(); + final long lastSentX = unacknowledgedOutboundStanzas.isEmpty() ? oldH : unacknowledgedOutboundStanzas.getLast().x; if ( !isInRange( h, oldH, lastSentX ) ) { // OF-3433: A value lower than an earlier one does not acknowledge anything new (XEP-0198 § 4). - Log.debug( "Ignoring client Ack (h={}), as it is lower than an earlier one: {}", h, oldH ); + Log.debug( "Ignoring peer Ack (h={}), as it is lower than an earlier one: {}", h, oldH ); return; } if ( h < oldH ) { // OF-3433: This acknowledges new stanzas, so a smaller value means that the count rolled over. - clientProcessedStanzasRolledOver = true; + acknowledgedOutboundStanzasRolledOver = true; } - clientProcessedStanzas.set( h ); + acknowledgedOutboundStanzas.set( h ); // Remove stanzas from temporary storage as now acknowledged - Log.trace( "Before processing client Ack (h={}): {} unacknowledged stanzas.", h, unacknowledgedServerStanzas.size() ); + Log.trace( "Before processing peer Ack (h={}): {} unacknowledged stanzas.", h, unacknowledgedOutboundStanzas.size() ); // Pop all acknowledged stanzas. - while( !unacknowledgedServerStanzas.isEmpty() && isInRange( unacknowledgedServerStanzas.getFirst().x, oldH, h ) ) + while( !unacknowledgedOutboundStanzas.isEmpty() && isInRange( unacknowledgedOutboundStanzas.getFirst().x, oldH, h ) ) { - unacknowledgedServerStanzas.removeFirst(); + unacknowledgedOutboundStanzas.removeFirst(); } - Log.trace( "After processing client Ack (h={}): {} unacknowledged stanzas.", h, unacknowledgedServerStanzas.size()); + Log.trace( "After processing peer Ack (h={}): {} unacknowledged stanzas.", h, unacknowledgedOutboundStanzas.size()); } } /** - * Receive and process acknowledgement packet from client + * Receive and process acknowledgement packet of outbound stanzas from the peer * @param ack XEP-0198 acknowledgement stanza to process */ - private void processClientAcknowledgement(Element ack) { + private void processOutboundAcknowledgement(Element ack) { if(isEnabled()) { if (ack.attribute("h") != null) { final String hValue = ack.attributeValue("h"); @@ -716,48 +725,48 @@ private void processClientAcknowledgement(Element ack) { try { h = Long.parseLong(hValue); } catch (NumberFormatException e) { - Log.warn( "Closing client session. Client sends non-numeric value for SM 'h': {}, affected session: {}", hValue, session ); - final StreamError error = new StreamError( StreamError.Condition.undefined_condition, "You acknowledged stanzas using a 'h' value that is not a number (which is illegal). Your Ack h: " + hValue + ", our last unacknowledged stanza: " + (unacknowledgedServerStanzas.isEmpty() ? "(none)" : unacknowledgedServerStanzas.getLast().x) ); + Log.warn( "Closing session. Peer sends non-numeric value for SM 'h': {}, affected session: {}", hValue, session ); + final StreamError error = new StreamError( StreamError.Condition.undefined_condition, "You acknowledged stanzas using a 'h' value that is not a number (which is illegal). Your Ack h: " + hValue + ", our last unacknowledged stanza: " + (unacknowledgedOutboundStanzas.isEmpty() ? "(none)" : unacknowledgedOutboundStanzas.getLast().x) ); // OF-3443: Unlike close(), this also makes the stream non-resumable (OF-2751). session.close( error ); return; } if (h < 0) { - Log.warn( "Closing client session. Client sends negative value for SM 'h': {}, affected session: {}", h, session ); - final StreamError error = new StreamError( StreamError.Condition.undefined_condition, "You acknowledged stanzas using a negative value (which is illegal). Your Ack h: " + h + ", our last unacknowledged stanza: " + (unacknowledgedServerStanzas.isEmpty() ? "(none)" : unacknowledgedServerStanzas.getLast().x) ); + Log.warn( "Closing session. Peer sends negative value for SM 'h': {}, affected session: {}", h, session ); + final StreamError error = new StreamError( StreamError.Condition.undefined_condition, "You acknowledged stanzas using a negative value (which is illegal). Your Ack h: " + h + ", our last unacknowledged stanza: " + (unacknowledgedOutboundStanzas.isEmpty() ? "(none)" : unacknowledgedOutboundStanzas.getLast().x) ); // OF-3443: Unlike close(), this also makes the stream non-resumable (OF-2751). session.close( error ); return; } if (h > MASK) { - Log.warn( "Closing client session. Client sends value for SM 'h' that is larger than 2^32-1: {}, affected session: {}", h, session ); - final StreamError error = new StreamError( StreamError.Condition.undefined_condition, "You acknowledged stanzas using a value larger than 2^32-1 (which is illegal). Your Ack h: " + h + ", our last unacknowledged stanza: " + (unacknowledgedServerStanzas.isEmpty() ? "(none)" : unacknowledgedServerStanzas.getLast().x) ); + Log.warn( "Closing session. Peer sends value for SM 'h' that is larger than 2^32-1: {}, affected session: {}", h, session ); + final StreamError error = new StreamError( StreamError.Condition.undefined_condition, "You acknowledged stanzas using a value larger than 2^32-1 (which is illegal). Your Ack h: " + h + ", our last unacknowledged stanza: " + (unacknowledgedOutboundStanzas.isEmpty() ? "(none)" : unacknowledgedOutboundStanzas.getLast().x) ); // OF-3443: Unlike close(), this also makes the stream non-resumable (OF-2751). session.close( error ); return; } - Log.debug( "Received acknowledgement from client: h={}", h ); + Log.debug( "Received acknowledgement from peer: h={}", h ); synchronized ( this ) { - if (!validateClientAcknowledgement(h)) { - Log.warn( "Closing client session. Client acknowledges stanzas that we didn't send! Client Ack h: {}, our last unacknowledged stanza: {}, affected session: {}", h, unacknowledgedServerStanzas.isEmpty() ? "(none)" : unacknowledgedServerStanzas.getLast().x, session ); - final StreamError error = new StreamError( StreamError.Condition.undefined_condition, "You acknowledged stanzas that we didn't send. Your Ack h: " + h + ", our last unacknowledged stanza: " + (unacknowledgedServerStanzas.isEmpty() ? "(none)" : unacknowledgedServerStanzas.getLast().x) ); - final long sendCount = unacknowledgedServerStanzas.isEmpty() ? clientProcessedStanzas.get() : unacknowledgedServerStanzas.getLast().x; + if (!validateOutboundAcknowledgement(h)) { + Log.warn( "Closing session. Peer acknowledges stanzas that we didn't send! Peer Ack h: {}, our last unacknowledged stanza: {}, affected session: {}", h, unacknowledgedOutboundStanzas.isEmpty() ? "(none)" : unacknowledgedOutboundStanzas.getLast().x, session ); + final StreamError error = new StreamError( StreamError.Condition.undefined_condition, "You acknowledged stanzas that we didn't send. Your Ack h: " + h + ", our last unacknowledged stanza: " + (unacknowledgedOutboundStanzas.isEmpty() ? "(none)" : unacknowledgedOutboundStanzas.getLast().x) ); + final long sendCount = unacknowledgedOutboundStanzas.isEmpty() ? acknowledgedOutboundStanzas.get() : unacknowledgedOutboundStanzas.getLast().x; error.getElement().addElement( QName.get( "handled-count-too-high", namespace ) ).addAttribute( "h", String.valueOf( h ) ).addAttribute( "send-count", String.valueOf( sendCount ) ); // OF-3443: Unlike close(), this also makes the stream non-resumable (OF-2751). session.close( error ); return; } - processClientAcknowledgement(h); + processOutboundAcknowledgement(h); } } } } /** - * Registers that Openfire sends a stanza to the client (which is expected to be acknowledged later). + * Registers that Openfire sends a stanza to the peer (which is expected to be acknowledged later). * @param packet The stanza that is sent. */ public void sentStanza(Packet packet) { @@ -777,10 +786,10 @@ public void sentStanza(Packet packet) { } // The next ID is one higher than the last stanza that was sent (which might be unacknowledged!), rolling over after 2^32-1 (XEP-0198 § 4). - final long x = ( 1 + ( unacknowledgedServerStanzas.isEmpty() ? clientProcessedStanzas.get() : unacknowledgedServerStanzas.getLast().x ) ) & MASK; - unacknowledgedServerStanzas.addLast( new StreamManager.UnackedPacket( x, packet.createCopy() ) ); + final long x = ( 1 + ( unacknowledgedOutboundStanzas.isEmpty() ? acknowledgedOutboundStanzas.get() : unacknowledgedOutboundStanzas.getLast().x ) ) & MASK; + unacknowledgedOutboundStanzas.addLast( new StreamManager.UnackedPacket( x, packet.createCopy() ) ); - size = unacknowledgedServerStanzas.size(); + size = unacknowledgedOutboundStanzas.size(); Log.trace( "Added stanza of type '{}' to collection of unacknowledged stanzas (x={}). Collection size is now {}.", packet.getElement().getName(), x, size ); @@ -799,7 +808,7 @@ public void sentStanza(Packet packet) { // When we have a sizable amount of unacknowledged stanzas, request acknowledgement. if ( size % requestFrequency == 0 ) { Log.debug( "Requesting acknowledgement from peer, as we have {} or more unacknowledged stanzas.", requestFrequency ); - sendServerRequest(); + requestOutboundAcknowledgement(); } } @@ -835,7 +844,7 @@ private void closeForExceedingMaximum( final int size ) } /** - * Handles the stanzas that the client did not acknowledge, for a session that closed without being resumed, and + * Handles the stanzas that the peer did not acknowledge, for a session that closed without being resumed, and * disables Stream Management for the session. * * XEP-0198 § 4 has a server treat unacknowledged stanzas in the same way as stanzas sent to an unavailable resource. @@ -855,7 +864,7 @@ public void onClose(PacketRouter router, JID serverAddress) { synchronized (this) { if(isEnabled()) { namespace = null; // disable stream management. - for (StreamManager.UnackedPacket unacked : unacknowledgedServerStanzas) { + for (StreamManager.UnackedPacket unacked : unacknowledgedOutboundStanzas) { // OF-3442: A failure to handle one stanza must not prevent the others from being handled. try { if (unacked.packet instanceof Message) { @@ -908,7 +917,7 @@ public void onResume(JID serverAddress, long h) { Element buildResumedElement() { final Element resumed = new DOMElement(QName.get("resumed", namespace)); resumed.addAttribute("previd", Base64.getEncoder().encodeToString((session.getAddress().getResource() + "\0" + session.getStreamID().getID()).getBytes(StandardCharsets.UTF_8))); - resumed.addAttribute("h", Long.toString(serverProcessedStanzas.get() & MASK)); + resumed.addAttribute("h", Long.toString(handledInboundStanzas.get() & MASK)); return resumed; } @@ -926,11 +935,11 @@ Element buildResumedElement() { */ public void redeliverUnackedStanzas(@Nonnull final JID serverAddress, final long h) { Log.debug("Resuming session: Ack for {}", h); - processClientAcknowledgement(h); + processOutboundAcknowledgement(h); synchronized (this) { if (exceededMaximumUnacknowledgedStanzas) { // OF-3434: The maximum was exceeded while detached. Closing under this lock keeps a stanza that LocalClientSession#pushPackets() delivers from going untracked. - closeForExceedingMaximum(unacknowledgedServerStanzas.size()); + closeForExceedingMaximum(unacknowledgedOutboundStanzas.size()); return; } // OF-3434: Re-delivering under the same lock keeps a stanza that exceeds the maximum from closing the stream first, which would route the re-delivered stanzas twice. @@ -939,7 +948,7 @@ public void redeliverUnackedStanzas(@Nonnull final JID serverAddress, final long assert connection != null; // While the client is resuming a session, the connection on which the session is resumed can't be null. // Re-deliver unacknowledged stanzas from broken stream (XEP-0198) if(isEnabled()) { - for (StreamManager.UnackedPacket unacked : unacknowledgedServerStanzas) { + for (StreamManager.UnackedPacket unacked : unacknowledgedOutboundStanzas) { try { if (unacked.packet instanceof Message) { Message m = (Message) unacked.packet; @@ -965,7 +974,7 @@ public void redeliverUnackedStanzas(@Nonnull final JID serverAddress, final long } } - sendServerRequest(); + requestOutboundAcknowledgement(); } } } @@ -980,15 +989,32 @@ public boolean isEnabled() { } /** - * Increments the count of stanzas processed by the server since + * Increments the count of inbound stanzas that Openfire has handled since * Stream Management was enabled. + * + * This is part of the processing of every inbound stanza. An invocation for a stanza that was not handled makes the + * count that is reported to the peer incorrect. */ - public void incrementServerProcessedStanzas() { + public void incrementHandledInboundStanzas() { if(isEnabled()) { - this.serverProcessedStanzas.incrementAndGet(); + this.handledInboundStanzas.incrementAndGet(); } } + /** + * Increments the count of inbound stanzas that Openfire has handled since + * Stream Management was enabled. + * + * This is part of the processing of every inbound stanza. An invocation for a stanza that was not handled makes the + * count that is reported to the peer incorrect. + * + * @deprecated Use {@link #incrementHandledInboundStanzas()} instead. + */ + @Deprecated(forRemoval = true, since = "5.2.0") // Remove in or after Openfire 5.3.0 + public void incrementServerProcessedStanzas() { + incrementHandledInboundStanzas(); + } + /** * The maximum amount of stanzas we keep, waiting for ack. * @return The maximum number of stanzas. diff --git a/xmppserver/src/main/java/org/jivesoftware/openfire/websocket/StreamManagementPacketRouter.java b/xmppserver/src/main/java/org/jivesoftware/openfire/websocket/StreamManagementPacketRouter.java index f1a3c91a0e..1aee125f19 100644 --- a/xmppserver/src/main/java/org/jivesoftware/openfire/websocket/StreamManagementPacketRouter.java +++ b/xmppserver/src/main/java/org/jivesoftware/openfire/websocket/StreamManagementPacketRouter.java @@ -63,7 +63,7 @@ public void route(Element wrappedElement) throws UnknownStanzaException { } else { super.route(wrappedElement); if (isUnsolicitedAckExpected()) { - session.getStreamManager().sendServerAcknowledgement(); + session.getStreamManager().acknowledgeInboundStanzas(); } } } diff --git a/xmppserver/src/test/java/org/jivesoftware/openfire/streammanagement/StreamManagerResumptionTest.java b/xmppserver/src/test/java/org/jivesoftware/openfire/streammanagement/StreamManagerResumptionTest.java index 8d4e3fc811..561ee68fe3 100644 --- a/xmppserver/src/test/java/org/jivesoftware/openfire/streammanagement/StreamManagerResumptionTest.java +++ b/xmppserver/src/test/java/org/jivesoftware/openfire/streammanagement/StreamManagerResumptionTest.java @@ -177,7 +177,7 @@ void resumedElementReportsNumberOfHandledStanzas(final int handled) throws Excep enableResumption(original, StreamManager.NAMESPACE_V3); final StreamManager sm = original.getStreamManager(); for (int i = 0; i < handled; i++) { - sm.incrementServerProcessedStanzas(); + sm.incrementHandledInboundStanzas(); } final Connection connection = reattached(original); diff --git a/xmppserver/src/test/java/org/jivesoftware/openfire/streammanagement/StreamManagerTest.java b/xmppserver/src/test/java/org/jivesoftware/openfire/streammanagement/StreamManagerTest.java index 48574e0e3e..44a54941b8 100644 --- a/xmppserver/src/test/java/org/jivesoftware/openfire/streammanagement/StreamManagerTest.java +++ b/xmppserver/src/test/java/org/jivesoftware/openfire/streammanagement/StreamManagerTest.java @@ -77,8 +77,8 @@ public class StreamManagerTest private static final int DETACH_TIME_MILLIS = 120_000; // OF-3433: Names of private fields, used to reach the roll-over at 2^32 without sending that many stanzas. - private static final String HANDLED_COUNTER_FIELD = "serverProcessedStanzas"; - private static final String ACKNOWLEDGED_COUNTER_FIELD = "clientProcessedStanzas"; + private static final String HANDLED_COUNTER_FIELD = "handledInboundStanzas"; + private static final String ACKNOWLEDGED_COUNTER_FIELD = "acknowledgedOutboundStanzas"; private SessionManager sessionManager; @@ -117,7 +117,7 @@ public void testValidateClientAcknowledgement() throws Exception final Long lastUnackedX = null; // Execute system under test. - final boolean result = StreamManager.validateClientAcknowledgement(h, oldH, lastUnackedX); + final boolean result = StreamManager.validateOutboundAcknowledgement(h, oldH, lastUnackedX); // Verify results. assertTrue(result); @@ -132,7 +132,7 @@ public void testValidateClientAcknowledgement_clientAcksSentStanza() throws Exce final Long lastUnackedX = 1L; // Execute system under test. - final boolean result = StreamManager.validateClientAcknowledgement(h, oldH, lastUnackedX); + final boolean result = StreamManager.validateOutboundAcknowledgement(h, oldH, lastUnackedX); // Verify results. assertTrue(result); @@ -147,7 +147,7 @@ public void testValidateClientAcknowledgement_clientAcksSentStanzaWithMoreInflig final Long lastUnackedX = 12L; // Execute system under test. - final boolean result = StreamManager.validateClientAcknowledgement(h, oldH, lastUnackedX); + final boolean result = StreamManager.validateOutboundAcknowledgement(h, oldH, lastUnackedX); // Verify results. assertTrue(result); @@ -162,7 +162,7 @@ public void testValidateClientAcknowledgement_clientAcksUnsentStanza() throws Ex final Long lastUnackedX = null; // Execute system under test. - final boolean result = StreamManager.validateClientAcknowledgement(h, oldH, lastUnackedX); + final boolean result = StreamManager.validateOutboundAcknowledgement(h, oldH, lastUnackedX); // Verify results. assertFalse(result); @@ -177,7 +177,7 @@ public void testValidateClientAcknowledgement_clientAcksUnsentStanzaA() throws E final Long lastUnackedX = 2L; // Execute system under test. - final boolean result = StreamManager.validateClientAcknowledgement(h, oldH, lastUnackedX); + final boolean result = StreamManager.validateOutboundAcknowledgement(h, oldH, lastUnackedX); // Verify results. assertFalse(result); @@ -193,7 +193,7 @@ public void testValidateClientAcknowledgement_rollover_edgecase() throws Excepti final Long lastUnackedX = null; // Execute system under test. - final boolean result = StreamManager.validateClientAcknowledgement(h, oldH, lastUnackedX); + final boolean result = StreamManager.validateOutboundAcknowledgement(h, oldH, lastUnackedX); // Verify results. assertTrue(result); @@ -209,7 +209,7 @@ public void testValidateClientAcknowledgement_rollover_edgecase_unsent() throws final Long lastUnackedX = null; // Execute system under test. - final boolean result = StreamManager.validateClientAcknowledgement(h, oldH, lastUnackedX); + final boolean result = StreamManager.validateOutboundAcknowledgement(h, oldH, lastUnackedX); // Verify results. assertFalse(result); @@ -226,7 +226,7 @@ public void testValidateClientAcknowledgement_rollover_edgecase_sent() throws Ex final Long lastUnackedX = MAX; // Execute system under test. - final boolean result = StreamManager.validateClientAcknowledgement(h, oldH, lastUnackedX); + final boolean result = StreamManager.validateOutboundAcknowledgement(h, oldH, lastUnackedX); // Verify results. assertTrue(result); @@ -242,7 +242,7 @@ public void testValidateClientAcknowledgement_rollover_edgecase1() throws Except final Long lastUnackedX = null; // Execute system under test. - final boolean result = StreamManager.validateClientAcknowledgement(h, oldH, lastUnackedX); + final boolean result = StreamManager.validateOutboundAcknowledgement(h, oldH, lastUnackedX); // Verify results. assertTrue(result); @@ -258,7 +258,7 @@ public void testValidateClientAcknowledgement_rollover_edgecase2() throws Except final Long lastUnackedX = MAX; // Execute system under test. - final boolean result = StreamManager.validateClientAcknowledgement(h, oldH, lastUnackedX); + final boolean result = StreamManager.validateOutboundAcknowledgement(h, oldH, lastUnackedX); // Verify results. assertTrue(result); @@ -273,7 +273,7 @@ public void testValidateClientAcknowledgement_rollover_edgecase3() throws Except final Long lastUnackedX = 0L; // Execute system under test. - final boolean result = StreamManager.validateClientAcknowledgement(h, oldH, lastUnackedX); + final boolean result = StreamManager.validateOutboundAcknowledgement(h, oldH, lastUnackedX); // Verify results. assertTrue(result); @@ -289,7 +289,7 @@ public void testValidateClientAcknowledgement_rollover_edgecase3a() throws Excep final Long lastUnackedX = 4L; // Execute system under test. - final boolean result = StreamManager.validateClientAcknowledgement(h, oldH, lastUnackedX); + final boolean result = StreamManager.validateOutboundAcknowledgement(h, oldH, lastUnackedX); // Verify results. assertTrue(result); @@ -305,7 +305,7 @@ public void testValidateClientAcknowledgement_rollover_edgecase3_unsent() throws final Long lastUnackedX = null; // Execute system under test. - final boolean result = StreamManager.validateClientAcknowledgement(h, oldH, lastUnackedX); + final boolean result = StreamManager.validateOutboundAcknowledgement(h, oldH, lastUnackedX); // Verify results. assertFalse(result); @@ -321,7 +321,7 @@ public void testValidateClientAcknowledgement_rollover_edgecase4() throws Except final Long lastUnackedX = 0L; // Execute system under test. - final boolean result = StreamManager.validateClientAcknowledgement(h, oldH, lastUnackedX); + final boolean result = StreamManager.validateOutboundAcknowledgement(h, oldH, lastUnackedX); // Verify results. assertTrue(result); @@ -337,7 +337,7 @@ public void testValidateClientAcknowledgement_rollover_edgecase4a() throws Excep final Long lastUnackedX = 3L; // Execute system under test. - final boolean result = StreamManager.validateClientAcknowledgement(h, oldH, lastUnackedX); + final boolean result = StreamManager.validateOutboundAcknowledgement(h, oldH, lastUnackedX); // Verify results. assertTrue(result); @@ -352,7 +352,7 @@ public void testValidateClientAcknowledgement_rollover_edgecase4_unsent() throws final Long lastUnackedX = null; // Execute system under test. - final boolean result = StreamManager.validateClientAcknowledgement(h, oldH, lastUnackedX); + final boolean result = StreamManager.validateOutboundAcknowledgement(h, oldH, lastUnackedX); // Verify results. assertFalse(result); @@ -368,7 +368,7 @@ public void testValidateClientAcknowledgement_rollover_edgecase5() throws Except final Long lastUnackedX = 4L; // Execute system under test. - final boolean result = StreamManager.validateClientAcknowledgement(h, oldH, lastUnackedX); + final boolean result = StreamManager.validateOutboundAcknowledgement(h, oldH, lastUnackedX); // Verify results. assertTrue(result); @@ -384,7 +384,7 @@ public void testValidateClientAcknowledgement_rollover_edgecase5a() throws Excep final Long lastUnackedX = 4L; // Execute system under test. - final boolean result = StreamManager.validateClientAcknowledgement(h, oldH, lastUnackedX); + final boolean result = StreamManager.validateOutboundAcknowledgement(h, oldH, lastUnackedX); // Verify results. assertTrue(result); @@ -400,7 +400,7 @@ public void testValidateClientAcknowledgement_rollover_edgecase5_unsent() throws final Long lastUnackedX = 4L; // Execute system under test. - final boolean result = StreamManager.validateClientAcknowledgement(h, oldH, lastUnackedX); + final boolean result = StreamManager.validateOutboundAcknowledgement(h, oldH, lastUnackedX); // Verify results. assertFalse(result); @@ -698,7 +698,7 @@ void enableTwiceFails() throws Exception // Setup test fixture. final LocalClientSession session = authenticatedClientSession(); final StreamManager sm = enabledStreamManager(session, StreamManager.NAMESPACE_V3); - sm.incrementServerProcessedStanzas(); + sm.incrementHandledInboundStanzas(); // Execute system under test. sm.process(smElement("enable", StreamManager.NAMESPACE_V2)); @@ -724,9 +724,9 @@ void requestIsAnsweredWithCountOfHandledStanzas(final String namespace) throws E // Setup test fixture. final LocalClientSession session = authenticatedClientSession(); final StreamManager sm = enabledStreamManager(session, namespace); - sm.incrementServerProcessedStanzas(); - sm.incrementServerProcessedStanzas(); - sm.incrementServerProcessedStanzas(); + sm.incrementHandledInboundStanzas(); + sm.incrementHandledInboundStanzas(); + sm.incrementHandledInboundStanzas(); // Execute system under test. sm.process(smElement("r", namespace)); @@ -763,9 +763,9 @@ void countOfHandledStanzasIsCumulative() throws Exception // Setup test fixture. final LocalClientSession session = authenticatedClientSession(); final StreamManager sm = enabledStreamManager(session, StreamManager.NAMESPACE_V3); - sm.incrementServerProcessedStanzas(); + sm.incrementHandledInboundStanzas(); sm.process(smElement("r", StreamManager.NAMESPACE_V3)); - sm.incrementServerProcessedStanzas(); + sm.incrementHandledInboundStanzas(); // Execute system under test. sm.process(smElement("r", StreamManager.NAMESPACE_V3)); @@ -783,7 +783,7 @@ void stanzasHandledBeforeEnablingAreNotCounted() throws Exception // Setup test fixture. final LocalClientSession session = authenticatedClientSession(); final StreamManager sm = new StreamManager(session); - sm.incrementServerProcessedStanzas(); + sm.incrementHandledInboundStanzas(); sm.process(smElement("enable", StreamManager.NAMESPACE_V3)); // Execute system under test. @@ -1271,11 +1271,11 @@ void countOfHandledStanzasRollsOver() throws Exception // Execute system under test. sm.process(smElement("r", StreamManager.NAMESPACE_V3)); final String before = lastDelivered(session).attributeValue("h"); - sm.incrementServerProcessedStanzas(); + sm.incrementHandledInboundStanzas(); clearInvocations(session); sm.process(smElement("r", StreamManager.NAMESPACE_V3)); final String after = lastDelivered(session).attributeValue("h"); - sm.incrementServerProcessedStanzas(); + sm.incrementHandledInboundStanzas(); clearInvocations(session); sm.process(smElement("r", StreamManager.NAMESPACE_V3)); final String afterNext = lastDelivered(session).attributeValue("h"); @@ -1297,7 +1297,7 @@ void countInResumedElementRollsOver() throws Exception final LocalClientSession session = authenticatedClientSession(); final StreamManager sm = enabledStreamManager(session, StreamManager.NAMESPACE_V3); setCounter(sm, HANDLED_COUNTER_FIELD, StreamManager.MASK); - sm.incrementServerProcessedStanzas(); + sm.incrementHandledInboundStanzas(); // Execute system under test. final Element result = sm.buildResumedElement(); From 90710477964cca52cf20784fbec56622b676233d Mon Sep 17 00:00:00 2001 From: Guus der Kinderen Date: Fri, 6 Nov 2026 11:32:19 +0100 Subject: [PATCH 04/28] OF-2425: Move the tracking of unacknowledged stanzas out of StreamManager into its own class StreamManager kept the stanzas that the peer had not acknowledged in a deque that it manipulated directly, together with the count of acknowledged stanzas and whether that count rolled over, the numbering of sent stanzas, the processing of acknowledgements and the check for the maximum. Outgoing server sessions need the same tracking, which was not available separately. This is now done by UnacknowledgedStanzaQueue, to which StreamManager delegates. The class is package-private, as only StreamManager uses it. Each of its entries has room to record that the stanza was sent again, which is not used yet. It logs through the logger of the StreamManager that uses it, so that its log messages still name the session. What happens when the maximum is exceeded remains in StreamManager, which closes the stream. StreamManager calls the queue under the same locks as before, which keep a sequence of calls consistent. In addition, each method of the queue holds the lock of the queue, and the entries are returned as a copy instead of a view, so that a single call is safe from any thread. This covers the places where StreamManager reads the last unacknowledged stanza for an error message without its own lock, which could fail when another thread emptied the queue at the same time. While the queue holds its lock, it calls nothing that takes the lock of StreamManager, so the two locks cannot deadlock. StreamManager.UnackedPacket is no longer used, and remains as a deprecated class without a replacement, for removal in or after Openfire 5.3.0, as ADR-010 describes. The tests in StreamManagerTest only change where they refer to the moved validation and counter. UnacknowledgedStanzaQueueTest tests the new class. No functional change. --- .../streammanagement/ResumeRequest.java | 2 +- .../streammanagement/StreamManager.java | 189 ++----- .../UnacknowledgedStanzaQueue.java | 376 ++++++++++++++ .../streammanagement/StreamManagerTest.java | 62 ++- .../UnacknowledgedStanzaQueueTest.java | 478 ++++++++++++++++++ 5 files changed, 936 insertions(+), 171 deletions(-) create mode 100644 xmppserver/src/main/java/org/jivesoftware/openfire/streammanagement/UnacknowledgedStanzaQueue.java create mode 100644 xmppserver/src/test/java/org/jivesoftware/openfire/streammanagement/UnacknowledgedStanzaQueueTest.java diff --git a/xmppserver/src/main/java/org/jivesoftware/openfire/streammanagement/ResumeRequest.java b/xmppserver/src/main/java/org/jivesoftware/openfire/streammanagement/ResumeRequest.java index 36bb9d0883..8abb2277fd 100644 --- a/xmppserver/src/main/java/org/jivesoftware/openfire/streammanagement/ResumeRequest.java +++ b/xmppserver/src/main/java/org/jivesoftware/openfire/streammanagement/ResumeRequest.java @@ -119,7 +119,7 @@ private static ResumeRequest parse(@Nonnull final Element resumeElement, @Nonnul } if (h < 0 || h > StreamManager.MASK) { // XEP-0198 § 4: 'h' is an unsigned 32-bit integer. Out-of-range values are rejected here rather than - // later by StreamManager#validateOutboundAcknowledgement(long), which throws for them. + // later by UnacknowledgedStanzaQueue#isValidAcknowledgement(long), which throws for them. throw new MalformedResumeRequestException("Stream resumption 'h' attribute must be an unsigned 32-bit integer, but was: " + h); } diff --git a/xmppserver/src/main/java/org/jivesoftware/openfire/streammanagement/StreamManager.java b/xmppserver/src/main/java/org/jivesoftware/openfire/streammanagement/StreamManager.java index 87af8aed96..cdff5fddf3 100644 --- a/xmppserver/src/main/java/org/jivesoftware/openfire/streammanagement/StreamManager.java +++ b/xmppserver/src/main/java/org/jivesoftware/openfire/streammanagement/StreamManager.java @@ -79,6 +79,12 @@ public class StreamManager { private final Logger Log; private boolean resume = false; + /** + * An outbound stanza that the peer has not acknowledged yet. + * + * @deprecated Stream Management does not use this class, which has no replacement. + */ + @Deprecated(forRemoval = true, since = "5.2.0") // Remove in or after Openfire 5.3.0 public static class UnackedPacket { public final long x; public final Date timestamp = new Date(); @@ -115,17 +121,6 @@ public static boolean isStreamManagementActive() { */ private AtomicLong handledInboundStanzas = new AtomicLong( 0 ); - /** - * Count of how many outbound stanzas/packets the peer has acknowledged as handled - */ - private AtomicLong acknowledgedOutboundStanzas = new AtomicLong( 0 ); - - /** - * Whether the count of outbound stanzas that the peer has acknowledged as handled has rolled over from (2^32)-1 to - * zero at least once. - */ - private boolean acknowledgedOutboundStanzasRolledOver = false; - /** * Whether the number of unacknowledged stanzas exceeded the maximum, which causes the session to be closed. */ @@ -137,9 +132,9 @@ public static boolean isStreamManagementActive() { static final long MASK = new BigInteger( "2" ).pow( 32 ).longValue() - 1; /** - * Collection of outbound stanzas/packets that haven't been acknowledged. + * Outbound stanzas/packets that haven't been acknowledged, and the count of those that have been. */ - private Deque unacknowledgedOutboundStanzas = new LinkedList<>(); + private final UnacknowledgedStanzaQueue unacknowledgedOutboundStanzas; /** * Delegates that can determine if a detached session can be terminated. @@ -164,6 +159,7 @@ public StreamManager(LocalSession session) { } this.Log = LoggerFactory.getLogger(StreamManager.class + "["+ (address == null ? "(unknown address)" : address) +"]" ); + this.unacknowledgedOutboundStanzas = new UnacknowledgedStanzaQueue( Log, this::getMaximumUnacknowledgedStanzas ); this.session = session; } @@ -202,7 +198,7 @@ public void process( Element element ) resumeRequest = ResumeRequest.from(element); } catch (MalformedResumeRequestException e) { Log.info( "Closing client session that sent a malformed 'resume' request. Error message: {}. Affected session: {}", e.getMessage(), session ); - final StreamError error = new StreamError( StreamError.Condition.undefined_condition, e.getMessage() + " Our last unacknowledged stanza: " + (unacknowledgedOutboundStanzas.isEmpty() ? "(none)" : unacknowledgedOutboundStanzas.getLast().x) ); + final StreamError error = new StreamError( StreamError.Condition.undefined_condition, e.getMessage() + " Our last unacknowledged stanza: " + unacknowledgedOutboundStanzas.describeLast() ); // OF-3443: Unlike close(), this also makes the stream non-resumable (OF-2751). session.close( error ); return; @@ -601,116 +597,15 @@ private static Element buildFailedElement(@Nonnull final String namespace, @Nonn * Checks if the amount of stanzas that the peer acknowledges is not higher than the amount of stanzas that we've * sent to the peer. * + * This holds the lock of this instance, so that the check does not see the queue of unacknowledged stanzas halfway + * through a sequence of changes that this instance makes under that lock. It is safe to invoke on the stream + * manager of another session. + * * @param h Then number of stanzas that the peer acknowledges it has received from us. * @return false if we sent fewer stanzas to the peer than the number it is acknowledging. */ private synchronized boolean validateOutboundAcknowledgement(long h) { - if (h < 0) { - throw new IllegalArgumentException("Argument 'h' cannot be negative, but was: " + h); - } - if (h > MASK) { - throw new IllegalArgumentException("Argument 'h' cannot be larger than 2^32 -1, but was: " + h); - } - final long oldH = acknowledgedOutboundStanzas.get(); - final Long lastUnackedX = unacknowledgedOutboundStanzas.isEmpty() ? null : unacknowledgedOutboundStanzas.getLast().x; - return validateOutboundAcknowledgement(h, oldH, lastUnackedX, acknowledgedOutboundStanzasRolledOver); - } - - /** - * Checks a value of 'h' against the stanzas that were sent, on a stream on which the acknowledged count has not - * rolled over. - * - * @param h The value to check. - * @param oldH The last acknowledged value. - * @param lastUnackedX The number of the last stanza sent, or null if all stanzas that were sent are acknowledged. - * @return false if the value is higher than the number of stanzas that were sent. - * @see #validateOutboundAcknowledgement(long, long, Long, boolean) - */ - @VisibleForTesting - static boolean validateOutboundAcknowledgement(final long h, final long oldH, final Long lastUnackedX) { - return validateOutboundAcknowledgement(h, oldH, lastUnackedX, false); - } - - /** - * Checks a value of 'h' against the stanzas that were sent, taking into account that stanza numbers and 'h' roll - * over from 2^32-1 to zero (XEP-0198 § 4). - * - * A value from the last acknowledged value up to and including the number of the last stanza sent is valid. A value - * lower than the last acknowledged value is valid too, as it is not higher than the number of stanzas that were sent - * (XEP-0198 § 6), but it does not acknowledge any new stanzas (XEP-0198 § 4). - * - * A value counts as lower when it is at most half of the range below the last acknowledged value, counting back - * across the roll-over. Until the acknowledged count has rolled over, it can't be below zero either, as the values - * that would be reached by counting back across the roll-over were never sent. - * - * @param h The value to check. - * @param oldH The last acknowledged value. - * @param lastUnackedX The number of the last stanza sent, or null if all stanzas that were sent are acknowledged. - * @param rolledOver Whether the acknowledged count has rolled over at least once. - * @return false if the value is higher than the number of stanzas that were sent. - */ - @VisibleForTesting - static boolean validateOutboundAcknowledgement(final long h, final long oldH, final Long lastUnackedX, final boolean rolledOver) { - final long lastSentX = lastUnackedX == null ? oldH : lastUnackedX; - if (isInRange(h, oldH, lastSentX)) { - return true; - } - final long earliestLowerH = rolledOver ? (oldH - (MASK >> 1)) & MASK : Math.max(0, oldH - (MASK >> 1)); - return isInRange(h, earliestLowerH, oldH); - } - - /** - * Checks if a value is in a range, taking into account that the range can include the roll-over from 2^32-1 to zero - * (XEP-0198 § 4). - * - * @param value The value to check. - * @param first The first value of the range. - * @param last The last value of the range. - * @return true if the value is in the range. - */ - private static boolean isInRange(final long value, final long first, final long last) { - return ((value - first) & MASK) <= ((last - first) & MASK); - } - - /** - * Process acknowledgements of outbound stanzas for a given value of h. - * - * @param h Last handled stanza to be acknowledged. - */ - private void processOutboundAcknowledgement(long h) { - synchronized (this) { - - if ( !validateOutboundAcknowledgement(h) ) { - // All paths leading up to here should have checked for this. Race condition? - throw new IllegalStateException( "Peer acknowledges stanzas that we didn't send! Peer Ack h: "+h+", our last unacknowledged stanza: " + (unacknowledgedOutboundStanzas.isEmpty() ? "(none)" : unacknowledgedOutboundStanzas.getLast().x) ); - } - - final long oldH = acknowledgedOutboundStanzas.get(); - final long lastSentX = unacknowledgedOutboundStanzas.isEmpty() ? oldH : unacknowledgedOutboundStanzas.getLast().x; - if ( !isInRange( h, oldH, lastSentX ) ) - { - // OF-3433: A value lower than an earlier one does not acknowledge anything new (XEP-0198 § 4). - Log.debug( "Ignoring peer Ack (h={}), as it is lower than an earlier one: {}", h, oldH ); - return; - } - - if ( h < oldH ) { - // OF-3433: This acknowledges new stanzas, so a smaller value means that the count rolled over. - acknowledgedOutboundStanzasRolledOver = true; - } - acknowledgedOutboundStanzas.set( h ); - - // Remove stanzas from temporary storage as now acknowledged - Log.trace( "Before processing peer Ack (h={}): {} unacknowledged stanzas.", h, unacknowledgedOutboundStanzas.size() ); - - // Pop all acknowledged stanzas. - while( !unacknowledgedOutboundStanzas.isEmpty() && isInRange( unacknowledgedOutboundStanzas.getFirst().x, oldH, h ) ) - { - unacknowledgedOutboundStanzas.removeFirst(); - } - - Log.trace( "After processing peer Ack (h={}): {} unacknowledged stanzas.", h, unacknowledgedOutboundStanzas.size()); - } + return unacknowledgedOutboundStanzas.isValidAcknowledgement(h); } /** @@ -726,21 +621,21 @@ private void processOutboundAcknowledgement(Element ack) { h = Long.parseLong(hValue); } catch (NumberFormatException e) { Log.warn( "Closing session. Peer sends non-numeric value for SM 'h': {}, affected session: {}", hValue, session ); - final StreamError error = new StreamError( StreamError.Condition.undefined_condition, "You acknowledged stanzas using a 'h' value that is not a number (which is illegal). Your Ack h: " + hValue + ", our last unacknowledged stanza: " + (unacknowledgedOutboundStanzas.isEmpty() ? "(none)" : unacknowledgedOutboundStanzas.getLast().x) ); + final StreamError error = new StreamError( StreamError.Condition.undefined_condition, "You acknowledged stanzas using a 'h' value that is not a number (which is illegal). Your Ack h: " + hValue + ", our last unacknowledged stanza: " + unacknowledgedOutboundStanzas.describeLast() ); // OF-3443: Unlike close(), this also makes the stream non-resumable (OF-2751). session.close( error ); return; } if (h < 0) { Log.warn( "Closing session. Peer sends negative value for SM 'h': {}, affected session: {}", h, session ); - final StreamError error = new StreamError( StreamError.Condition.undefined_condition, "You acknowledged stanzas using a negative value (which is illegal). Your Ack h: " + h + ", our last unacknowledged stanza: " + (unacknowledgedOutboundStanzas.isEmpty() ? "(none)" : unacknowledgedOutboundStanzas.getLast().x) ); + final StreamError error = new StreamError( StreamError.Condition.undefined_condition, "You acknowledged stanzas using a negative value (which is illegal). Your Ack h: " + h + ", our last unacknowledged stanza: " + unacknowledgedOutboundStanzas.describeLast() ); // OF-3443: Unlike close(), this also makes the stream non-resumable (OF-2751). session.close( error ); return; } if (h > MASK) { Log.warn( "Closing session. Peer sends value for SM 'h' that is larger than 2^32-1: {}, affected session: {}", h, session ); - final StreamError error = new StreamError( StreamError.Condition.undefined_condition, "You acknowledged stanzas using a value larger than 2^32-1 (which is illegal). Your Ack h: " + h + ", our last unacknowledged stanza: " + (unacknowledgedOutboundStanzas.isEmpty() ? "(none)" : unacknowledgedOutboundStanzas.getLast().x) ); + final StreamError error = new StreamError( StreamError.Condition.undefined_condition, "You acknowledged stanzas using a value larger than 2^32-1 (which is illegal). Your Ack h: " + h + ", our last unacknowledged stanza: " + unacknowledgedOutboundStanzas.describeLast() ); // OF-3443: Unlike close(), this also makes the stream non-resumable (OF-2751). session.close( error ); return; @@ -750,16 +645,16 @@ private void processOutboundAcknowledgement(Element ack) { synchronized ( this ) { if (!validateOutboundAcknowledgement(h)) { - Log.warn( "Closing session. Peer acknowledges stanzas that we didn't send! Peer Ack h: {}, our last unacknowledged stanza: {}, affected session: {}", h, unacknowledgedOutboundStanzas.isEmpty() ? "(none)" : unacknowledgedOutboundStanzas.getLast().x, session ); - final StreamError error = new StreamError( StreamError.Condition.undefined_condition, "You acknowledged stanzas that we didn't send. Your Ack h: " + h + ", our last unacknowledged stanza: " + (unacknowledgedOutboundStanzas.isEmpty() ? "(none)" : unacknowledgedOutboundStanzas.getLast().x) ); - final long sendCount = unacknowledgedOutboundStanzas.isEmpty() ? acknowledgedOutboundStanzas.get() : unacknowledgedOutboundStanzas.getLast().x; + Log.warn( "Closing session. Peer acknowledges stanzas that we didn't send! Peer Ack h: {}, our last unacknowledged stanza: {}, affected session: {}", h, unacknowledgedOutboundStanzas.describeLast(), session ); + final StreamError error = new StreamError( StreamError.Condition.undefined_condition, "You acknowledged stanzas that we didn't send. Your Ack h: " + h + ", our last unacknowledged stanza: " + unacknowledgedOutboundStanzas.describeLast() ); + final long sendCount = unacknowledgedOutboundStanzas.getLastSentNumber(); error.getElement().addElement( QName.get( "handled-count-too-high", namespace ) ).addAttribute( "h", String.valueOf( h ) ).addAttribute( "send-count", String.valueOf( sendCount ) ); // OF-3443: Unlike close(), this also makes the stream non-resumable (OF-2751). session.close( error ); return; } - processOutboundAcknowledgement(h); + unacknowledgedOutboundStanzas.acknowledge( h ); } } } @@ -785,16 +680,14 @@ public void sentStanza(Packet packet) { return; } - // The next ID is one higher than the last stanza that was sent (which might be unacknowledged!), rolling over after 2^32-1 (XEP-0198 § 4). - final long x = ( 1 + ( unacknowledgedOutboundStanzas.isEmpty() ? acknowledgedOutboundStanzas.get() : unacknowledgedOutboundStanzas.getLast().x ) ) & MASK; - unacknowledgedOutboundStanzas.addLast( new StreamManager.UnackedPacket( x, packet.createCopy() ) ); + final long x = unacknowledgedOutboundStanzas.add( packet.createCopy() ); size = unacknowledgedOutboundStanzas.size(); Log.trace( "Added stanza of type '{}' to collection of unacknowledged stanzas (x={}). Collection size is now {}.", packet.getElement().getName(), x, size ); // Prevent keeping to many stanzas in memory. OF-3434: the stanzas are kept, to be handled when the session closes. - closeForExceedingMaximum = size > getMaximumUnacknowledgedStanzas() && !exceededMaximumUnacknowledgedStanzas; + closeForExceedingMaximum = unacknowledgedOutboundStanzas.exceedsMaximum() && !exceededMaximumUnacknowledgedStanzas; if ( closeForExceedingMaximum ) { exceededMaximumUnacknowledgedStanzas = true; } @@ -864,18 +757,18 @@ public void onClose(PacketRouter router, JID serverAddress) { synchronized (this) { if(isEnabled()) { namespace = null; // disable stream management. - for (StreamManager.UnackedPacket unacked : unacknowledgedOutboundStanzas) { + for (final UnacknowledgedStanzaQueue.Entry unacked : unacknowledgedOutboundStanzas.getEntries()) { // OF-3442: A failure to handle one stanza must not prevent the others from being handled. try { - if (unacked.packet instanceof Message) { - Message m = (Message) unacked.packet; + if (unacked.getPacket() instanceof Message) { + Message m = (Message) unacked.getPacket(); if (m.getExtension("delay", "urn:xmpp:delay") == null) { Element delayInformation = m.addChildElement("delay", "urn:xmpp:delay"); - delayInformation.addAttribute("stamp", XMPPDateTimeFormat.format(unacked.timestamp)); + delayInformation.addAttribute("stamp", XMPPDateTimeFormat.format(unacked.getTimestamp())); delayInformation.addAttribute("from", serverAddress.toBareJID()); } - router.route(unacked.packet); - } else if (unacked.packet instanceof IQ iq && iq.isRequest()) { + router.route(unacked.getPacket()); + } else if (unacked.getPacket() instanceof IQ iq && iq.isRequest()) { // OF-3442: Every IQ request needs a response (RFC 6120 § 8.2.3), here the one for an unavailable resource (RFC 6121 § 8.5.3.2.3). final IQ error = IQ.createResultIQ(iq); if (error.getFrom() == null) { @@ -887,7 +780,7 @@ public void onClose(PacketRouter router, JID serverAddress) { } } catch (Exception e) { // The stanza itself is not logged, as it can have private content. - Log.warn("Unable to handle an unacknowledged '{}' stanza of type '{}' with ID '{}', of session {} that closed.", unacked.packet.getElement().getName(), unacked.packet.getElement().attributeValue("type"), unacked.packet.getID(), session, e); + Log.warn("Unable to handle an unacknowledged '{}' stanza of type '{}' with ID '{}', of session {} that closed.", unacked.getPacket().getElement().getName(), unacked.getPacket().getElement().attributeValue("type"), unacked.getPacket().getID(), session, e); } } } @@ -935,7 +828,9 @@ Element buildResumedElement() { */ public void redeliverUnackedStanzas(@Nonnull final JID serverAddress, final long h) { Log.debug("Resuming session: Ack for {}", h); - processOutboundAcknowledgement(h); + synchronized (this) { + unacknowledgedOutboundStanzas.acknowledge( h ); + } synchronized (this) { if (exceededMaximumUnacknowledgedStanzas) { // OF-3434: The maximum was exceeded while detached. Closing under this lock keeps a stanza that LocalClientSession#pushPackets() delivers from going untracked. @@ -948,26 +843,26 @@ public void redeliverUnackedStanzas(@Nonnull final JID serverAddress, final long assert connection != null; // While the client is resuming a session, the connection on which the session is resumed can't be null. // Re-deliver unacknowledged stanzas from broken stream (XEP-0198) if(isEnabled()) { - for (StreamManager.UnackedPacket unacked : unacknowledgedOutboundStanzas) { + for (final UnacknowledgedStanzaQueue.Entry unacked : unacknowledgedOutboundStanzas.getEntries()) { try { - if (unacked.packet instanceof Message) { - Message m = (Message) unacked.packet; + if (unacked.getPacket() instanceof Message) { + Message m = (Message) unacked.getPacket(); if (m.getExtension("delay", "urn:xmpp:delay") == null) { Element delayInformation = m.addChildElement("delay", "urn:xmpp:delay"); - delayInformation.addAttribute("stamp", XMPPDateTimeFormat.format(unacked.timestamp)); + delayInformation.addAttribute("stamp", XMPPDateTimeFormat.format(unacked.getTimestamp())); delayInformation.addAttribute("from", serverAddress.toBareJID()); } connection.deliver(m); - } else if (unacked.packet instanceof Presence) { - Presence p = (Presence) unacked.packet; + } else if (unacked.getPacket() instanceof Presence) { + Presence p = (Presence) unacked.getPacket(); if (p.getExtension("delay", "urn:xmpp:delay") == null) { Element delayInformation = p.addChildElement("delay", "urn:xmpp:delay"); - delayInformation.addAttribute("stamp", XMPPDateTimeFormat.format(unacked.timestamp)); + delayInformation.addAttribute("stamp", XMPPDateTimeFormat.format(unacked.getTimestamp())); delayInformation.addAttribute("from", serverAddress.toBareJID()); } connection.deliver(p); } else { - connection.deliver(unacked.packet); + connection.deliver(unacked.getPacket()); } } catch (UnauthorizedException e) { Log.warn("Caught unauthorized exception, which seems worrying: ", e); diff --git a/xmppserver/src/main/java/org/jivesoftware/openfire/streammanagement/UnacknowledgedStanzaQueue.java b/xmppserver/src/main/java/org/jivesoftware/openfire/streammanagement/UnacknowledgedStanzaQueue.java new file mode 100644 index 0000000000..dec45a70e0 --- /dev/null +++ b/xmppserver/src/main/java/org/jivesoftware/openfire/streammanagement/UnacknowledgedStanzaQueue.java @@ -0,0 +1,376 @@ +/* + * Copyright (C) 2026 Ignite Realtime Foundation. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jivesoftware.openfire.streammanagement; + +import com.google.common.annotations.VisibleForTesting; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.xmpp.packet.Packet; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import java.time.Instant; +import java.util.Collection; +import java.util.Deque; +import java.util.LinkedList; +import java.util.List; +import java.util.function.IntSupplier; + +/** + * The outbound stanzas of a stream that the peer has not acknowledged yet (XEP-0198 § 4), in the order in which they + * were sent, together with the number of outbound stanzas that the peer has acknowledged. + * + * Each stanza is numbered when it is added, so that an acknowledgement, which reports a number of stanzas, can be + * matched to the stanzas that it covers. + * + * Each method is thread-safe, and holds the lock of the instance while it runs. A sequence of calls that depends on + * the queue not changing in between, such as checking an acknowledgement and then processing it, still needs the + * caller to hold a lock that guards all changes of the queue. + * + * @author Guus der Kinderen, guus@goodbytes.nl + */ +class UnacknowledgedStanzaQueue +{ + /** + * The logger to use, which names the session of the stream. + */ + private final Logger Log; + + /** + * An outbound stanza that the peer has not acknowledged yet. + * + * The methods of an entry are thread-safe. The stanza that an entry holds is not copied: a caller that changes it + * must hold a lock that guards all users of the stanza. + */ + static class Entry + { + private final long number; + private final Instant timestamp = Instant.now(); + private final Packet packet; + private volatile boolean sentAgain = false; + + Entry(final long number, @Nonnull final Packet packet) + { + this.number = number; + this.packet = packet; + } + + /** + * The number of the stanza, which the peer reports in an acknowledgement once it handled this stanza. + * + * @return the number of the stanza. + */ + long getNumber() + { + return number; + } + + /** + * The time at which the stanza was added, which is when it was sent for the first time. + * + * @return the time at which the stanza was added. + */ + @Nonnull + Instant getTimestamp() + { + return timestamp; + } + + /** + * The stanza. + * + * @return the stanza. + */ + @Nonnull + Packet getPacket() + { + return packet; + } + + /** + * Whether the stanza was sent again after it was first sent, for example on a resumed stream. + * + * @return true if the stanza was sent again. + */ + boolean isSentAgain() + { + return sentAgain; + } + + /** + * Records that the stanza was sent again after it was first sent. + */ + void markSentAgain() + { + this.sentAgain = true; + } + } + + /** + * The stanzas that were sent but not acknowledged, in the order in which they were sent. + */ + private final Deque entries = new LinkedList<>(); + + /** + * The number of outbound stanzas that the peer has acknowledged as handled. + */ + private long acknowledged = 0; + + /** + * Whether the number of acknowledged stanzas has rolled over from (2^32)-1 to zero at least once. + */ + private boolean rolledOver = false; + + /** + * Provides the maximum number of stanzas to keep. + */ + private final IntSupplier maximum; + + /** + * Creates an empty queue that logs through a logger of its own class. + * + * @param maximum provides the maximum number of stanzas to keep. It is consulted whenever it is needed, so that a + * change of the value takes effect immediately. It is invoked while the lock of the queue is held. + */ + UnacknowledgedStanzaQueue(@Nonnull final IntSupplier maximum) + { + this(LoggerFactory.getLogger(UnacknowledgedStanzaQueue.class), maximum); + } + + /** + * Creates an empty queue. + * + * @param log the logger to use, so that what is logged can be related to the session of the stream. + * @param maximum provides the maximum number of stanzas to keep. It is consulted whenever it is needed, so that a + * change of the value takes effect immediately. It is invoked while the lock of the queue is held. + */ + UnacknowledgedStanzaQueue(@Nonnull final Logger log, @Nonnull final IntSupplier maximum) + { + this.Log = log; + this.maximum = maximum; + } + + /** + * Adds a stanza that is sent to the peer, numbering it one higher than the stanza that was sent before it. Numbers + * roll over from (2^32)-1 to zero, as 'h' does (XEP-0198 § 4). + * + * @param packet the stanza that is sent. + * @return the number of the stanza. + */ + synchronized long add(@Nonnull final Packet packet) + { + // The next ID is one higher than the last stanza that was sent (which might be unacknowledged!), rolling over after 2^32-1 (XEP-0198 § 4). + final long number = ( 1 + getLastSentNumber() ) & StreamManager.MASK; + entries.addLast(new Entry(number, packet)); + return number; + } + + /** + * Checks whether the queue holds more stanzas than its maximum. + * + * @return true if the queue holds more stanzas than its maximum. + */ + synchronized boolean exceedsMaximum() + { + return entries.size() > maximum.getAsInt(); + } + + /** + * Checks if the number of stanzas that the peer acknowledges is not higher than the number of stanzas that were sent + * to the peer. + * + * @param h The number of stanzas that the peer acknowledges it has received. + * @return false if fewer stanzas were sent to the peer than the number it is acknowledging. + * @throws IllegalArgumentException when 'h' is negative, or larger than 2^32-1. + */ + synchronized boolean isValidAcknowledgement(final long h) + { + if (h < 0) { + throw new IllegalArgumentException("Argument 'h' cannot be negative, but was: " + h); + } + if (h > StreamManager.MASK) { + throw new IllegalArgumentException("Argument 'h' cannot be larger than 2^32 -1, but was: " + h); + } + return isValidAcknowledgement(h, acknowledged, getLastNumber(), rolledOver); + } + + /** + * Checks a value of 'h' against the stanzas that were sent, on a stream on which the acknowledged count has not + * rolled over. + * + * @param h The value to check. + * @param oldH The last acknowledged value. + * @param lastUnackedX The number of the last stanza sent, or null if all stanzas that were sent are acknowledged. + * @return false if the value is higher than the number of stanzas that were sent. + * @see #isValidAcknowledgement(long, long, Long, boolean) + */ + @VisibleForTesting + static boolean isValidAcknowledgement(final long h, final long oldH, @Nullable final Long lastUnackedX) + { + return isValidAcknowledgement(h, oldH, lastUnackedX, false); + } + + /** + * Checks a value of 'h' against the stanzas that were sent, taking into account that stanza numbers and 'h' roll + * over from 2^32-1 to zero (XEP-0198 § 4). + * + * A value from the last acknowledged value up to and including the number of the last stanza sent is valid. A value + * lower than the last acknowledged value is valid too, as it is not higher than the number of stanzas that were sent + * (XEP-0198 § 6), but it does not acknowledge any new stanzas (XEP-0198 § 4). + * + * A value counts as lower when it is at most half of the range below the last acknowledged value, counting back + * across the roll-over. Until the acknowledged count has rolled over, it can't be below zero either, as the values + * that would be reached by counting back across the roll-over were never sent. + * + * @param h The value to check. + * @param oldH The last acknowledged value. + * @param lastUnackedX The number of the last stanza sent, or null if all stanzas that were sent are acknowledged. + * @param rolledOver Whether the acknowledged count has rolled over at least once. + * @return false if the value is higher than the number of stanzas that were sent. + */ + @VisibleForTesting + static boolean isValidAcknowledgement(final long h, final long oldH, @Nullable final Long lastUnackedX, final boolean rolledOver) + { + final long lastSentX = lastUnackedX == null ? oldH : lastUnackedX; + if (isInRange(h, oldH, lastSentX)) { + return true; + } + final long earliestLowerH = rolledOver ? (oldH - (StreamManager.MASK >> 1)) & StreamManager.MASK : Math.max(0, oldH - (StreamManager.MASK >> 1)); + return isInRange(h, earliestLowerH, oldH); + } + + /** + * Checks if a value is in a range, taking into account that the range can include the roll-over from 2^32-1 to zero + * (XEP-0198 § 4). + * + * @param value The value to check. + * @param first The first value of the range. + * @param last The last value of the range. + * @return true if the value is in the range. + */ + private static boolean isInRange(final long value, final long first, final long last) + { + return ((value - first) & StreamManager.MASK) <= ((last - first) & StreamManager.MASK); + } + + /** + * Processes an acknowledgement from the peer: records the number of stanzas that it acknowledges, and removes the + * stanzas that it covers. A value that is lower than an earlier one acknowledges nothing new (XEP-0198 § 4), and + * leaves the queue unchanged. + * + * @param h The number of stanzas that the peer acknowledges it has handled. + * @throws IllegalArgumentException when 'h' is negative, or larger than 2^32-1. + * @throws IllegalStateException when the peer acknowledges more stanzas than were sent to it. + */ + synchronized void acknowledge(final long h) + { + if ( !isValidAcknowledgement(h) ) { + // All paths leading up to here should have checked for this. Race condition? + throw new IllegalStateException( "Peer acknowledges stanzas that we didn't send! Peer Ack h: "+h+", our last unacknowledged stanza: " + describeLast() ); + } + + final long oldH = acknowledged; + if ( !isInRange( h, oldH, getLastSentNumber() ) ) + { + // OF-3433: A value lower than an earlier one does not acknowledge anything new (XEP-0198 § 4). + Log.debug( "Ignoring peer Ack (h={}), as it is lower than an earlier one: {}", h, oldH ); + return; + } + + if ( h < oldH ) { + // OF-3433: This acknowledges new stanzas, so a smaller value means that the count rolled over. + Log.debug( "Peer Ack (h={}) is lower than the earlier one ({}) and acknowledges new stanzas: the acknowledged count rolled over.", h, oldH ); + rolledOver = true; + } + acknowledged = h; + + // Remove stanzas from temporary storage as now acknowledged + Log.trace( "Before processing peer Ack (h={}): {} unacknowledged stanzas.", h, entries.size() ); + + // Pop all acknowledged stanzas. + while( !entries.isEmpty() && isInRange( entries.getFirst().getNumber(), oldH, h ) ) + { + entries.removeFirst(); + } + + Log.trace( "After processing peer Ack (h={}): {} unacknowledged stanzas.", h, entries.size()); + } + + /** + * The number of the stanza that was sent last, whether or not the peer acknowledged it. When no stanzas are + * unacknowledged, this is the number of stanzas that the peer acknowledged. + * + * @return the number of the stanza that was sent last. + */ + synchronized long getLastSentNumber() + { + return entries.isEmpty() ? acknowledged : entries.getLast().getNumber(); + } + + /** + * The number of the stanza that was added last. + * + * @return the number of the last stanza, or null if the queue is empty. + */ + @Nullable + synchronized Long getLastNumber() + { + return entries.isEmpty() ? null : entries.getLast().getNumber(); + } + + /** + * Describes the stanza that was added last, for use in messages. + * + * @return the number of the last stanza, or "(none)" if the queue is empty. + */ + @Nonnull + synchronized String describeLast() + { + return entries.isEmpty() ? "(none)" : String.valueOf(entries.getLast().getNumber()); + } + + /** + * The number of stanzas in the queue. + * + * @return the number of stanzas in the queue. + */ + synchronized int size() + { + return entries.size(); + } + + /** + * Checks whether the queue holds no stanzas. + * + * @return true if the queue holds no stanzas. + */ + synchronized boolean isEmpty() + { + return entries.isEmpty(); + } + + /** + * The stanzas in the queue, in the order in which they were sent. This is an unmodifiable copy, which does not + * reflect later changes of the queue. + * + * @return the stanzas in the queue. + */ + @Nonnull + synchronized Collection getEntries() + { + return List.copyOf(entries); + } +} diff --git a/xmppserver/src/test/java/org/jivesoftware/openfire/streammanagement/StreamManagerTest.java b/xmppserver/src/test/java/org/jivesoftware/openfire/streammanagement/StreamManagerTest.java index 44a54941b8..839d4779c7 100644 --- a/xmppserver/src/test/java/org/jivesoftware/openfire/streammanagement/StreamManagerTest.java +++ b/xmppserver/src/test/java/org/jivesoftware/openfire/streammanagement/StreamManagerTest.java @@ -78,7 +78,7 @@ public class StreamManagerTest // OF-3433: Names of private fields, used to reach the roll-over at 2^32 without sending that many stanzas. private static final String HANDLED_COUNTER_FIELD = "handledInboundStanzas"; - private static final String ACKNOWLEDGED_COUNTER_FIELD = "acknowledgedOutboundStanzas"; + private static final String ACKNOWLEDGED_COUNTER_FIELD = "unacknowledgedOutboundStanzas.acknowledged"; private SessionManager sessionManager; @@ -117,7 +117,7 @@ public void testValidateClientAcknowledgement() throws Exception final Long lastUnackedX = null; // Execute system under test. - final boolean result = StreamManager.validateOutboundAcknowledgement(h, oldH, lastUnackedX); + final boolean result = UnacknowledgedStanzaQueue.isValidAcknowledgement(h, oldH, lastUnackedX); // Verify results. assertTrue(result); @@ -132,7 +132,7 @@ public void testValidateClientAcknowledgement_clientAcksSentStanza() throws Exce final Long lastUnackedX = 1L; // Execute system under test. - final boolean result = StreamManager.validateOutboundAcknowledgement(h, oldH, lastUnackedX); + final boolean result = UnacknowledgedStanzaQueue.isValidAcknowledgement(h, oldH, lastUnackedX); // Verify results. assertTrue(result); @@ -147,7 +147,7 @@ public void testValidateClientAcknowledgement_clientAcksSentStanzaWithMoreInflig final Long lastUnackedX = 12L; // Execute system under test. - final boolean result = StreamManager.validateOutboundAcknowledgement(h, oldH, lastUnackedX); + final boolean result = UnacknowledgedStanzaQueue.isValidAcknowledgement(h, oldH, lastUnackedX); // Verify results. assertTrue(result); @@ -162,7 +162,7 @@ public void testValidateClientAcknowledgement_clientAcksUnsentStanza() throws Ex final Long lastUnackedX = null; // Execute system under test. - final boolean result = StreamManager.validateOutboundAcknowledgement(h, oldH, lastUnackedX); + final boolean result = UnacknowledgedStanzaQueue.isValidAcknowledgement(h, oldH, lastUnackedX); // Verify results. assertFalse(result); @@ -177,7 +177,7 @@ public void testValidateClientAcknowledgement_clientAcksUnsentStanzaA() throws E final Long lastUnackedX = 2L; // Execute system under test. - final boolean result = StreamManager.validateOutboundAcknowledgement(h, oldH, lastUnackedX); + final boolean result = UnacknowledgedStanzaQueue.isValidAcknowledgement(h, oldH, lastUnackedX); // Verify results. assertFalse(result); @@ -193,7 +193,7 @@ public void testValidateClientAcknowledgement_rollover_edgecase() throws Excepti final Long lastUnackedX = null; // Execute system under test. - final boolean result = StreamManager.validateOutboundAcknowledgement(h, oldH, lastUnackedX); + final boolean result = UnacknowledgedStanzaQueue.isValidAcknowledgement(h, oldH, lastUnackedX); // Verify results. assertTrue(result); @@ -209,7 +209,7 @@ public void testValidateClientAcknowledgement_rollover_edgecase_unsent() throws final Long lastUnackedX = null; // Execute system under test. - final boolean result = StreamManager.validateOutboundAcknowledgement(h, oldH, lastUnackedX); + final boolean result = UnacknowledgedStanzaQueue.isValidAcknowledgement(h, oldH, lastUnackedX); // Verify results. assertFalse(result); @@ -226,7 +226,7 @@ public void testValidateClientAcknowledgement_rollover_edgecase_sent() throws Ex final Long lastUnackedX = MAX; // Execute system under test. - final boolean result = StreamManager.validateOutboundAcknowledgement(h, oldH, lastUnackedX); + final boolean result = UnacknowledgedStanzaQueue.isValidAcknowledgement(h, oldH, lastUnackedX); // Verify results. assertTrue(result); @@ -242,7 +242,7 @@ public void testValidateClientAcknowledgement_rollover_edgecase1() throws Except final Long lastUnackedX = null; // Execute system under test. - final boolean result = StreamManager.validateOutboundAcknowledgement(h, oldH, lastUnackedX); + final boolean result = UnacknowledgedStanzaQueue.isValidAcknowledgement(h, oldH, lastUnackedX); // Verify results. assertTrue(result); @@ -258,7 +258,7 @@ public void testValidateClientAcknowledgement_rollover_edgecase2() throws Except final Long lastUnackedX = MAX; // Execute system under test. - final boolean result = StreamManager.validateOutboundAcknowledgement(h, oldH, lastUnackedX); + final boolean result = UnacknowledgedStanzaQueue.isValidAcknowledgement(h, oldH, lastUnackedX); // Verify results. assertTrue(result); @@ -273,7 +273,7 @@ public void testValidateClientAcknowledgement_rollover_edgecase3() throws Except final Long lastUnackedX = 0L; // Execute system under test. - final boolean result = StreamManager.validateOutboundAcknowledgement(h, oldH, lastUnackedX); + final boolean result = UnacknowledgedStanzaQueue.isValidAcknowledgement(h, oldH, lastUnackedX); // Verify results. assertTrue(result); @@ -289,7 +289,7 @@ public void testValidateClientAcknowledgement_rollover_edgecase3a() throws Excep final Long lastUnackedX = 4L; // Execute system under test. - final boolean result = StreamManager.validateOutboundAcknowledgement(h, oldH, lastUnackedX); + final boolean result = UnacknowledgedStanzaQueue.isValidAcknowledgement(h, oldH, lastUnackedX); // Verify results. assertTrue(result); @@ -305,7 +305,7 @@ public void testValidateClientAcknowledgement_rollover_edgecase3_unsent() throws final Long lastUnackedX = null; // Execute system under test. - final boolean result = StreamManager.validateOutboundAcknowledgement(h, oldH, lastUnackedX); + final boolean result = UnacknowledgedStanzaQueue.isValidAcknowledgement(h, oldH, lastUnackedX); // Verify results. assertFalse(result); @@ -321,7 +321,7 @@ public void testValidateClientAcknowledgement_rollover_edgecase4() throws Except final Long lastUnackedX = 0L; // Execute system under test. - final boolean result = StreamManager.validateOutboundAcknowledgement(h, oldH, lastUnackedX); + final boolean result = UnacknowledgedStanzaQueue.isValidAcknowledgement(h, oldH, lastUnackedX); // Verify results. assertTrue(result); @@ -337,7 +337,7 @@ public void testValidateClientAcknowledgement_rollover_edgecase4a() throws Excep final Long lastUnackedX = 3L; // Execute system under test. - final boolean result = StreamManager.validateOutboundAcknowledgement(h, oldH, lastUnackedX); + final boolean result = UnacknowledgedStanzaQueue.isValidAcknowledgement(h, oldH, lastUnackedX); // Verify results. assertTrue(result); @@ -352,7 +352,7 @@ public void testValidateClientAcknowledgement_rollover_edgecase4_unsent() throws final Long lastUnackedX = null; // Execute system under test. - final boolean result = StreamManager.validateOutboundAcknowledgement(h, oldH, lastUnackedX); + final boolean result = UnacknowledgedStanzaQueue.isValidAcknowledgement(h, oldH, lastUnackedX); // Verify results. assertFalse(result); @@ -368,7 +368,7 @@ public void testValidateClientAcknowledgement_rollover_edgecase5() throws Except final Long lastUnackedX = 4L; // Execute system under test. - final boolean result = StreamManager.validateOutboundAcknowledgement(h, oldH, lastUnackedX); + final boolean result = UnacknowledgedStanzaQueue.isValidAcknowledgement(h, oldH, lastUnackedX); // Verify results. assertTrue(result); @@ -384,7 +384,7 @@ public void testValidateClientAcknowledgement_rollover_edgecase5a() throws Excep final Long lastUnackedX = 4L; // Execute system under test. - final boolean result = StreamManager.validateOutboundAcknowledgement(h, oldH, lastUnackedX); + final boolean result = UnacknowledgedStanzaQueue.isValidAcknowledgement(h, oldH, lastUnackedX); // Verify results. assertTrue(result); @@ -400,7 +400,7 @@ public void testValidateClientAcknowledgement_rollover_edgecase5_unsent() throws final Long lastUnackedX = 4L; // Execute system under test. - final boolean result = StreamManager.validateOutboundAcknowledgement(h, oldH, lastUnackedX); + final boolean result = UnacknowledgedStanzaQueue.isValidAcknowledgement(h, oldH, lastUnackedX); // Verify results. assertFalse(result); @@ -2262,12 +2262,28 @@ private static void mockCloseTracking(final LocalClientSession session) } /** - * Sets a private counter of a stream manager, as sending 2^32 stanzas is not feasible in a unit test. + * Sets a private counter of a stream manager, as sending 2^32 stanzas is not feasible in a unit test. The name of the + * field can be a path of field names separated by dots, to reach a counter in an object that the stream manager holds. */ private static void setCounter(final StreamManager sm, final String fieldName, final long value) throws Exception { - final Field field = StreamManager.class.getDeclaredField(fieldName); + Object holder = sm; + final String[] names = fieldName.split("\\."); + for (int i = 0; i < names.length - 1; i++) { + holder = field(holder, names[i]).get(holder); + } + final Field counter = field(holder, names[names.length - 1]); + if (counter.getType() == long.class) { + counter.setLong(holder, value); + } else { + ((AtomicLong) counter.get(holder)).set(value); + } + } + + private static Field field(final Object holder, final String name) throws NoSuchFieldException + { + final Field field = holder.getClass().getDeclaredField(name); field.setAccessible(true); - ((AtomicLong) field.get(sm)).set(value); + return field; } } diff --git a/xmppserver/src/test/java/org/jivesoftware/openfire/streammanagement/UnacknowledgedStanzaQueueTest.java b/xmppserver/src/test/java/org/jivesoftware/openfire/streammanagement/UnacknowledgedStanzaQueueTest.java new file mode 100644 index 0000000000..f911e406ed --- /dev/null +++ b/xmppserver/src/test/java/org/jivesoftware/openfire/streammanagement/UnacknowledgedStanzaQueueTest.java @@ -0,0 +1,478 @@ +/* + * Copyright (C) 2026 Ignite Realtime Foundation. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jivesoftware.openfire.streammanagement; + +import org.junit.jupiter.api.Test; +import org.xmpp.packet.Message; +import org.xmpp.packet.Packet; + +import java.lang.reflect.Field; +import java.time.Instant; +import java.util.Collection; +import java.util.List; +import java.util.concurrent.atomic.AtomicInteger; + +import static org.junit.jupiter.api.Assertions.*; + +/** + * Unit tests that verify the implementation of {@link UnacknowledgedStanzaQueue}. + * + * @author Guus der Kinderen, guus@goodbytes.nl + */ +public class UnacknowledgedStanzaQueueTest +{ + private static final int MAXIMUM = 10; + + /** + * Verifies that the first stanza that is added is numbered 1, and that each next stanza is numbered one higher. + */ + @Test + void stanzasAreNumberedFromOne() + { + // Setup test fixture. + final UnacknowledgedStanzaQueue queue = new UnacknowledgedStanzaQueue(() -> MAXIMUM); + + // Execute system under test. + final long first = queue.add(message("1")); + final long second = queue.add(message("2")); + + // Verify result. + assertEquals(1, first, "The first stanza must be numbered 1."); + assertEquals(2, second, "The next stanza must be numbered one higher than the stanza that was added before it."); + } + + /** + * Verifies that a stanza that is added after all earlier stanzas were acknowledged is numbered one higher than the + * number of acknowledged stanzas. + */ + @Test + void stanzaAddedAfterAcknowledgementIsNumberedOn() + { + // Setup test fixture. + final UnacknowledgedStanzaQueue queue = new UnacknowledgedStanzaQueue(() -> MAXIMUM); + queue.add(message("1")); + queue.add(message("2")); + queue.acknowledge(2); + + // Execute system under test. + final long result = queue.add(message("3")); + + // Verify result. + assertEquals(3, result, "A stanza that is added after all earlier stanzas were acknowledged must be numbered on from the number of acknowledged stanzas."); + } + + /** + * Verifies that the queue holds its stanzas in the order in which they were added, with their numbers. + */ + @Test + void entriesAreInOrderOfAddition() + { + // Setup test fixture. + final UnacknowledgedStanzaQueue queue = new UnacknowledgedStanzaQueue(() -> MAXIMUM); + + // Execute system under test. + queue.add(message("a")); + queue.add(message("b")); + queue.add(message("c")); + + // Verify result. + assertEquals(List.of("a", "b", "c"), ids(queue), "The stanzas must be in the order in which they were added."); + assertEquals(List.of(1L, 2L, 3L), queue.getEntries().stream().map(UnacknowledgedStanzaQueue.Entry::getNumber).toList(), "Each stanza must have the number that it was given when it was added."); + assertEquals(3, queue.size(), "The size must be the number of stanzas that were added."); + assertEquals(3L, queue.getLastNumber(), "The last number must be the number of the stanza that was added last."); + } + + /** + * Verifies that an entry holds the stanza that was added, and the time at which it was added. + */ + @Test + void entryHoldsStanzaAndTimeOfAddition() + { + // Setup test fixture. + final UnacknowledgedStanzaQueue queue = new UnacknowledgedStanzaQueue(() -> MAXIMUM); + final Message message = message("1"); + final Instant before = Instant.now(); + + // Execute system under test. + queue.add(message); + + // Verify result. + final UnacknowledgedStanzaQueue.Entry entry = queue.getEntries().iterator().next(); + final Instant after = Instant.now(); + assertSame(message, entry.getPacket(), "The entry must hold the stanza that was added."); + assertFalse(entry.getTimestamp().isBefore(before) || entry.getTimestamp().isAfter(after), "The entry must hold the time at which the stanza was added, but was: " + entry.getTimestamp()); + } + + /** + * Verifies that an entry is not marked as sent again when it is added, and is once it is marked. + */ + @Test + void entryIsMarkedAsSentAgainOnlyWhenMarked() + { + // Setup test fixture. + final UnacknowledgedStanzaQueue queue = new UnacknowledgedStanzaQueue(() -> MAXIMUM); + queue.add(message("1")); + final UnacknowledgedStanzaQueue.Entry entry = queue.getEntries().iterator().next(); + assertFalse(entry.isSentAgain(), "A stanza that was just added must not be marked as sent again."); + + // Execute system under test. + entry.markSentAgain(); + + // Verify result. + assertTrue(entry.isSentAgain(), "A stanza that was marked as sent again must be reported as such."); + } + + /** + * Verifies that an acknowledgement removes the stanzas that it covers, and keeps the others. + */ + @Test + void acknowledgementRemovesCoveredStanzas() + { + // Setup test fixture. + final UnacknowledgedStanzaQueue queue = new UnacknowledgedStanzaQueue(() -> MAXIMUM); + queue.add(message("1")); + queue.add(message("2")); + queue.add(message("3")); + + // Execute system under test. + queue.acknowledge(2); + + // Verify result. + assertEquals(List.of("3"), ids(queue), "The stanzas that the acknowledgement covers must be removed, and the others kept."); + } + + /** + * Verifies that an acknowledgement of zero stanzas removes nothing. + */ + @Test + void acknowledgementOfZeroRemovesNothing() + { + // Setup test fixture. + final UnacknowledgedStanzaQueue queue = new UnacknowledgedStanzaQueue(() -> MAXIMUM); + queue.add(message("1")); + queue.add(message("2")); + + // Execute system under test. + queue.acknowledge(0); + + // Verify result. + assertEquals(List.of("1", "2"), ids(queue), "An acknowledgement of zero stanzas must not remove any stanza."); + } + + /** + * Verifies that an acknowledgement of all stanzas empties the queue. + */ + @Test + void acknowledgementOfAllStanzasEmptiesQueue() + { + // Setup test fixture. + final UnacknowledgedStanzaQueue queue = new UnacknowledgedStanzaQueue(() -> MAXIMUM); + queue.add(message("1")); + queue.add(message("2")); + + // Execute system under test. + queue.acknowledge(2); + + // Verify result. + assertTrue(queue.isEmpty(), "An acknowledgement of all stanzas must empty the queue."); + assertNull(queue.getLastNumber(), "An empty queue must not have a last number."); + assertEquals("(none)", queue.describeLast(), "An empty queue must be described as having no last stanza."); + } + + /** + * Verifies that an acknowledgement of more stanzas than were added is refused, without changing the queue. + */ + @Test + void acknowledgementOfMoreStanzasThanAddedIsRefused() + { + // Setup test fixture. + final UnacknowledgedStanzaQueue queue = new UnacknowledgedStanzaQueue(() -> MAXIMUM); + queue.add(message("1")); + queue.add(message("2")); + + // Execute system under test. + final IllegalStateException result = assertThrows(IllegalStateException.class, () -> queue.acknowledge(3), "An acknowledgement of more stanzas than were added must be refused."); + + // Verify result. + assertTrue(result.getMessage().contains("our last unacknowledged stanza: 2"), "The message must report the last stanza that was added, but was: " + result.getMessage()); + assertEquals(List.of("1", "2"), ids(queue), "A refused acknowledgement must not remove any stanza."); + } + + /** + * Verifies that a value of 'h' that is negative, or larger than 2^32-1, is not a valid acknowledgement. + */ + @Test + void illegalValueIsNotAValidAcknowledgement() + { + // Setup test fixture. + final UnacknowledgedStanzaQueue queue = new UnacknowledgedStanzaQueue(() -> MAXIMUM); + + // Execute system under test & verify result. + assertThrows(IllegalArgumentException.class, () -> queue.isValidAcknowledgement(-1), "A negative value of 'h' must be rejected."); + assertThrows(IllegalArgumentException.class, () -> queue.isValidAcknowledgement(StreamManager.MASK + 1), "A value of 'h' larger than 2^32-1 must be rejected."); + } + + /** + * Verifies that, when no stanzas are unacknowledged, only the number of stanzas that were acknowledged before is a + * valid acknowledgement. + */ + @Test + void acknowledgementWhenNoStanzasAreUnacknowledged() + { + // Setup test fixture. + final UnacknowledgedStanzaQueue queue = new UnacknowledgedStanzaQueue(() -> MAXIMUM); + queue.add(message("1")); + queue.acknowledge(1); + + // Execute system under test & verify result. + assertTrue(queue.isValidAcknowledgement(1), "Acknowledging the stanzas that were acknowledged before must be valid."); + assertFalse(queue.isValidAcknowledgement(2), "Acknowledging a stanza that was not added must not be valid."); + } + + /** + * Verifies that, when stanzas are unacknowledged, an acknowledgement up to the last stanza is valid, and one beyond + * it is not. + */ + @Test + void acknowledgementWhenStanzasAreUnacknowledged() + { + // Setup test fixture. + final UnacknowledgedStanzaQueue queue = new UnacknowledgedStanzaQueue(() -> MAXIMUM); + queue.add(message("1")); + queue.add(message("2")); + + // Execute system under test & verify result. + assertTrue(queue.isValidAcknowledgement(2), "Acknowledging up to the last stanza must be valid."); + assertFalse(queue.isValidAcknowledgement(3), "Acknowledging beyond the last stanza must not be valid."); + } + + /** + * Verifies that an acknowledgement that is lower than an earlier one is valid, but does not remove any stanzas, as + * XEP-0198 § 4 has it acknowledge nothing new. + */ + @Test + void lowerAcknowledgementIsValidAndRemovesNothing() + { + // Setup test fixture. + final UnacknowledgedStanzaQueue queue = new UnacknowledgedStanzaQueue(() -> MAXIMUM); + queue.add(message("1")); + queue.add(message("2")); + queue.add(message("3")); + queue.acknowledge(2); + assertTrue(queue.isValidAcknowledgement(1), "Test fixture expects a lower acknowledgement to be valid."); + + // Execute system under test. + queue.acknowledge(1); + + // Verify result. + assertEquals(List.of("3"), ids(queue), "A lower acknowledgement must not remove any stanzas."); + assertEquals(4, queue.add(message("4")), "A lower acknowledgement must not change the numbering of stanzas."); + } + + /** + * Verifies that the number of the stanza that was sent last is reported whether or not it was acknowledged. + */ + @Test + void lastSentNumberIncludesAcknowledgedStanzas() + { + // Setup test fixture. + final UnacknowledgedStanzaQueue queue = new UnacknowledgedStanzaQueue(() -> MAXIMUM); + assertEquals(0, queue.getLastSentNumber(), "Test fixture expects zero for a queue to which no stanzas were added."); + queue.add(message("1")); + queue.add(message("2")); + + // Execute system under test. + queue.acknowledge(2); + + // Verify result. + assertEquals(2, queue.getLastSentNumber(), "The number of the stanza that was sent last must be reported after it was acknowledged."); + } + + /** + * Verifies that the queue exceeds its maximum only when it holds more stanzas than the maximum. + */ + @Test + void maximumIsExceededOnlyAboveMaximum() + { + // Setup test fixture. + final UnacknowledgedStanzaQueue queue = new UnacknowledgedStanzaQueue(() -> 2); + queue.add(message("1")); + queue.add(message("2")); + assertFalse(queue.exceedsMaximum(), "A queue that holds as many stanzas as its maximum must not exceed it."); + + // Execute system under test. + queue.add(message("3")); + + // Verify result. + assertTrue(queue.exceedsMaximum(), "A queue that holds more stanzas than its maximum must exceed it."); + } + + /** + * Verifies that a change of the maximum takes effect immediately. + */ + @Test + void changedMaximumTakesEffectImmediately() + { + // Setup test fixture. + final AtomicInteger maximum = new AtomicInteger(5); + final UnacknowledgedStanzaQueue queue = new UnacknowledgedStanzaQueue(maximum::get); + queue.add(message("1")); + queue.add(message("2")); + assertFalse(queue.exceedsMaximum(), "Test fixture expects the queue to be within its maximum."); + + // Execute system under test. + maximum.set(1); + + // Verify result. + assertTrue(queue.exceedsMaximum(), "A lowered maximum must take effect without adding a stanza."); + } + + /** + * Verifies that the entries of the queue cannot be changed through the collection that is returned for them. + */ + @Test + void entriesCannotBeChangedThroughTheReturnedCollection() + { + // Setup test fixture. + final UnacknowledgedStanzaQueue queue = new UnacknowledgedStanzaQueue(() -> MAXIMUM); + queue.add(message("1")); + final Collection entries = queue.getEntries(); + + // Execute system under test & verify result. + assertThrows(UnsupportedOperationException.class, entries::clear, "The entries must not be changed through the collection that is returned for them."); + assertEquals(1, queue.size(), "The queue must be unchanged."); + } + + /** + * Verifies that the collection that is returned for the entries of the queue is a copy, which later changes of the + * queue do not affect, so that it can be iterated while the queue changes. + */ + @Test + void entriesAreACopy() + { + // Setup test fixture. + final UnacknowledgedStanzaQueue queue = new UnacknowledgedStanzaQueue(() -> MAXIMUM); + queue.add(message("1")); + final Collection entries = queue.getEntries(); + + // Execute system under test. + queue.add(message("2")); + queue.acknowledge(1); + + // Verify result. + assertEquals(List.of(1L), entries.stream().map(UnacknowledgedStanzaQueue.Entry::getNumber).toList(), "The returned collection must not reflect changes of the queue after it was returned."); + } + + /** + * Verifies that a stanza can be acknowledged with the largest legal value of 'h'. + */ + @Test + void acknowledgementWithLargestLegalValueRemovesCoveredStanza() throws Exception + { + // Setup test fixture. + final UnacknowledgedStanzaQueue queue = new UnacknowledgedStanzaQueue(() -> MAXIMUM); + setAcknowledged(queue, StreamManager.MASK - 1); + queue.add(message("peer-counts-2^32-1")); + + // Execute system under test. + queue.acknowledge(StreamManager.MASK); + + // Verify result. + assertTrue(queue.isEmpty(), "The stanza that the largest legal value of 'h' covers must be removed."); + } + + /** + * Verifies that stanza numbers roll over from 2^32-1 to zero, as XEP-0198 § 4 requires of 'h'. + */ + @Test + void stanzaNumbersRollOver() throws Exception + { + // Setup test fixture. + final UnacknowledgedStanzaQueue queue = new UnacknowledgedStanzaQueue(() -> MAXIMUM); + setAcknowledged(queue, StreamManager.MASK - 1); + queue.add(message("peer-counts-2^32-1")); + + // Execute system under test. + final long result = queue.add(message("peer-counts-0")); + + // Verify result. + assertEquals(0, result, "The stanza after the one numbered 2^32-1 must be numbered zero."); + } + + /** + * Verifies that an acknowledgement that rolled over removes only the stanzas that it covers. + */ + @Test + void acknowledgementAfterRollOverKeepsUncoveredStanzas() throws Exception + { + // Setup test fixture. + final UnacknowledgedStanzaQueue queue = new UnacknowledgedStanzaQueue(() -> MAXIMUM); + setAcknowledged(queue, StreamManager.MASK - 1); + queue.add(message("peer-counts-2^32-1")); + queue.add(message("peer-counts-0")); + queue.add(message("peer-counts-1")); + + // Execute system under test. + queue.acknowledge(0); + + // Verify result. + assertEquals(List.of("peer-counts-1"), ids(queue), "An acknowledgement that rolled over must remove only the stanzas that it covers."); + } + + /** + * Verifies that, once the number of acknowledged stanzas has rolled over, an acknowledgement from before the + * roll-over is valid as a lower value, while one that is higher than the number of stanzas that were sent is not. + */ + @Test + void acknowledgementFromBeforeRollOverIsValid() throws Exception + { + // Setup test fixture. + final UnacknowledgedStanzaQueue queue = new UnacknowledgedStanzaQueue(() -> MAXIMUM); + setAcknowledged(queue, StreamManager.MASK - 1); + queue.add(message("peer-counts-2^32-1")); + queue.add(message("peer-counts-0")); + queue.add(message("peer-counts-1")); + + // Execute system under test. + queue.acknowledge(0); + + // Verify result. + assertTrue(queue.isValidAcknowledgement(StreamManager.MASK - 1), "An acknowledgement from before the roll-over must be valid as a lower value."); + assertFalse(queue.isValidAcknowledgement(2), "An acknowledgement of more stanzas than were sent must not be valid after the roll-over."); + } + + private static Message message(final String id) + { + final Message message = new Message(); + message.setID(id); + return message; + } + + private static List ids(final UnacknowledgedStanzaQueue queue) + { + return queue.getEntries().stream().map(UnacknowledgedStanzaQueue.Entry::getPacket).map(Packet::getID).toList(); + } + + /** + * Sets the private count of acknowledged stanzas, as adding 2^32 stanzas is not feasible in a unit test. + */ + private static void setAcknowledged(final UnacknowledgedStanzaQueue queue, final long value) throws Exception + { + final Field field = UnacknowledgedStanzaQueue.class.getDeclaredField("acknowledged"); + field.setAccessible(true); + field.setLong(queue, value); + } +} From bef11f7f44abcf7a7b07d1e62e72c570ce726378 Mon Sep 17 00:00:00 2001 From: Guus der Kinderen Date: Wed, 4 Nov 2026 14:27:36 +0100 Subject: [PATCH 05/28] OF-2425: Separate the client-specific parts of Stream Management resumption from StreamManager StreamManager assumed that every resumable session is a client session: it checked the AuthToken of a LocalClientSession to decide whether a session may resume, built the SM-ID from the resource and the stream ID, and found the session to resume through the client routes. Server sessions need their own rules for each of these. These parts are now behind the Resumption interface. StreamManager creates an implementation for its session, chosen by session type. ClientResumption holds the existing client rules, including the removal of detached sessions elsewhere in the cluster (OF-2753). It logs through the logger of the StreamManager that uses it, so that its log messages still name the session. Other session types get an implementation that refuses resumption, as before. The checks that do not depend on the session type, such as the state of the session to resume and its Stream Management namespace and 'h', remain in StreamManager. StreamManager now keeps the SM-ID that it creates when resumption is enabled, and uses it for the 'previd' of , instead of creating it a second time. An implementation therefore does not need to produce the same SM-ID on every call. The SM-ID, and the fields that record whether the stream can be resumed and which namespace it uses, are volatile. The thread of the connection that resumes a stream reads them from the stream manager of another session, without holding its lock. The inline SASL2 resume flow requires ClientResumption, as SASL2 is only used by clients, and refuses other session types as before. Refusals that were not logged are now logged at debug level: a resume request for a session whose stream ID does not match, an inline resume request from a session that is not a client session, and a request to enable resumption from a session that may not resume. This is the one place where StreamManager still refers to LocalClientSession, as Sasl2ResumeResult returns the resumed client session. ResumptionTest tests ClientResumption and the implementation for other session types directly. StreamManagerResumptionTest also covers resume requests, traditional and inline, from a session that is not a client session. No functional change. --- .../openfire/net/SASLAuthentication.java | 2 +- .../streammanagement/ClientResumption.java | 152 +++++++++++++ .../streammanagement/ResumeRequest.java | 14 +- .../ResumeRequestValidationResult.java | 28 ++- .../openfire/streammanagement/Resumption.java | 119 ++++++++++ .../streammanagement/StreamManager.java | 169 ++++++-------- .../streammanagement/ResumptionTest.java | 215 ++++++++++++++++++ .../StreamManagerResumptionTest.java | 46 ++++ .../streammanagement/StreamManagerTest.java | 6 +- 9 files changed, 627 insertions(+), 124 deletions(-) create mode 100644 xmppserver/src/main/java/org/jivesoftware/openfire/streammanagement/ClientResumption.java create mode 100644 xmppserver/src/main/java/org/jivesoftware/openfire/streammanagement/Resumption.java create mode 100644 xmppserver/src/test/java/org/jivesoftware/openfire/streammanagement/ResumptionTest.java diff --git a/xmppserver/src/main/java/org/jivesoftware/openfire/net/SASLAuthentication.java b/xmppserver/src/main/java/org/jivesoftware/openfire/net/SASLAuthentication.java index 88a2d05c16..3e3a1ed5fb 100644 --- a/xmppserver/src/main/java/org/jivesoftware/openfire/net/SASLAuthentication.java +++ b/xmppserver/src/main/java/org/jivesoftware/openfire/net/SASLAuthentication.java @@ -1028,7 +1028,7 @@ else if (session instanceof LocalIncomingServerSession serverSession) { // SASL authentication has completed, but resource binding has not (yet). This one-argument form // records the identity without transitioning the session to AUTHENTICATED, which is exactly the state - // that inline XEP-0198 resumption (below) and StreamManager#allowResume() require. + // that inline XEP-0198 resumption (below) requires. clientSession.setAuthToken(clientAuthToken); // XEP-0198 § 9.2: an inline resume request, if present, must be processed before any Bind2 request. diff --git a/xmppserver/src/main/java/org/jivesoftware/openfire/streammanagement/ClientResumption.java b/xmppserver/src/main/java/org/jivesoftware/openfire/streammanagement/ClientResumption.java new file mode 100644 index 0000000000..60cdd01f66 --- /dev/null +++ b/xmppserver/src/main/java/org/jivesoftware/openfire/streammanagement/ClientResumption.java @@ -0,0 +1,152 @@ +/* + * Copyright (C) 2026 Ignite Realtime Foundation. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jivesoftware.openfire.streammanagement; + +import org.jivesoftware.openfire.XMPPServer; +import org.jivesoftware.openfire.auth.AuthToken; +import org.jivesoftware.openfire.session.ClientSession; +import org.jivesoftware.openfire.session.ClientSessionTask; +import org.jivesoftware.openfire.session.LocalClientSession; +import org.jivesoftware.openfire.session.RemoteSessionTask; +import org.jivesoftware.util.cache.CacheFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.xmpp.packet.JID; +import org.xmpp.packet.PacketError; + +import javax.annotation.Nonnull; +import java.nio.charset.StandardCharsets; +import java.util.Base64; +import java.util.StringTokenizer; + +/** + * Stream resumption for client sessions. + * + * A client session may resume when it is authenticated by a user that is not anonymous. A resume request refers to a + * session of that same user, on this cluster node. + * + * Instances are thread-safe: they hold no state other than the session that they are bound to. + * + * @author Guus der Kinderen, guus@goodbytes.nl + */ +final class ClientResumption implements Resumption +{ + /** + * The logger to use, which names the session of the stream. + */ + private final Logger Log; + + private final LocalClientSession session; + + /** + * Creates an instance bound to a client session, that logs through a logger of its own class. + * + * @param session the session (cannot be null). + */ + ClientResumption(@Nonnull final LocalClientSession session) + { + this(LoggerFactory.getLogger(ClientResumption.class), session); + } + + /** + * Creates an instance bound to a client session. + * + * @param log the logger to use, so that what is logged can be related to the session of the stream. + * @param session the session (cannot be null). + */ + ClientResumption(@Nonnull final Logger log, @Nonnull final LocalClientSession session) + { + this.Log = log; + this.session = session; + } + + @Override + public boolean isAllowed() + { + final AuthToken authToken = session.getAuthToken(); + return authToken != null && !authToken.isAnonymous(); + } + + /** + * Creates the SM-ID, which is the Base64 encoding of the resource of the session and its stream ID, separated by a + * NUL character. + * + * @return the SM-ID of the stream of the session (never null). + */ + @Nonnull + @Override + public String createId() + { + return Base64.getEncoder().encodeToString((session.getAddress().getResource() + "\0" + session.getStreamID().getID()).getBytes(StandardCharsets.UTF_8)); + } + + @Nonnull + @Override + public ResumeRequestValidationResult locate(@Nonnull final String previd) + { + final AuthToken authToken = session.getAuthToken(); + if (authToken == null) { + Log.debug("Unable to process session resumption attempt, as session {} does not provide any auth context.", session); + return ResumeRequestValidationResult.failure(PacketError.Condition.unexpected_request); + } + if (authToken.isAnonymous()) { + Log.debug("Unable to process session resumption attempt, as session {} is anonymous.", session); + return ResumeRequestValidationResult.failure(PacketError.Condition.unexpected_request); + } + + // Decode previd. + String resource; + String streamId; + try { + StringTokenizer toks = new StringTokenizer(new String(Base64.getDecoder().decode(previd), StandardCharsets.UTF_8), "\0"); + resource = toks.nextToken(); + streamId = toks.nextToken(); + } catch (Exception e) { + Log.debug("Exception from previd decode:", e); + return ResumeRequestValidationResult.failure(PacketError.Condition.unexpected_request); + } + final JID fullJid = new JID(authToken.getUsername(), session.getServerName(), resource, true); + Log.debug("Resuming session for '{}'. Current session: {}", fullJid, session.getStreamID()); + + // Locate existing session. + final ClientSession route = XMPPServer.getInstance().getRoutingTable().getClientRoute(fullJid); + if (route == null) { + Log.debug("Not able for client of '{}' to resume a session on this cluster node. No session was found for this client.", fullJid); + if (StreamManager.LOCATION_TERMINATE_OTHERS_ENABLED.getValue()) { + // When the client tries to resume a connection on this host, it is unlikely to try other hosts. Remove any detached sessions living elsewhere in the cluster. (OF-2753) + CacheFactory.doClusterTask(new ClientSessionTask(fullJid, RemoteSessionTask.Operation.removeDetached)); + } + return ResumeRequestValidationResult.failure(PacketError.Condition.item_not_found); + } + + if (!(route instanceof LocalClientSession otherSession)) { + Log.debug("Not allowing a client of '{}' to resume a session on this cluster node. The session can only be resumed on the Openfire cluster node where the original session was connected.", fullJid); + if (StreamManager.LOCATION_TERMINATE_OTHERS_ENABLED.getValue()) { + // When the client tries to resume a connection on this host, it is unlikely to try other hosts. Remove any detached sessions living elsewhere in the cluster. (OF-2753) + CacheFactory.doClusterTask(new ClientSessionTask(fullJid, RemoteSessionTask.Operation.removeDetached)); + } + return ResumeRequestValidationResult.failure(PacketError.Condition.unexpected_request); + } + + if (!otherSession.getStreamID().getID().equals(streamId)) { + Log.debug("Not allowing a client of '{}' to resume a session, as the stream ID in the previd ({}) does not match that of the existing session ({}).", fullJid, streamId, otherSession.getStreamID()); + return ResumeRequestValidationResult.failure(PacketError.Condition.item_not_found); + } + Log.debug("Found existing session for '{}', checking status", fullJid); + + return ResumeRequestValidationResult.success(otherSession); + } +} diff --git a/xmppserver/src/main/java/org/jivesoftware/openfire/streammanagement/ResumeRequest.java b/xmppserver/src/main/java/org/jivesoftware/openfire/streammanagement/ResumeRequest.java index 8abb2277fd..d4ee5bb96c 100644 --- a/xmppserver/src/main/java/org/jivesoftware/openfire/streammanagement/ResumeRequest.java +++ b/xmppserver/src/main/java/org/jivesoftware/openfire/streammanagement/ResumeRequest.java @@ -25,7 +25,7 @@ * * This is used by both variants of stream resumption defined by XEP-0198: *