Browse Source

Fixed flaky TwoFactorAuthTest pre-verification token WebSocket test

The server accepts the WebSocket handshake and closes the session right
away when it rejects a pre-verification token in afterConnectionEstablished.
Java-WebSocket's connectBlocking() releases its latch on both open and close
and then returns engine.isOpen(), so it returns false when the client reads
the close frame before the caller checks the state. The asserted connect in
buildAndConnectWebSocketClient then failed.

The test now connects without asserting the connect result, waits for the
close, and asserts the server close code (1011), so it still proves the
token was rejected.
pull/16352/head
Sergey Matvienko 1 month ago
parent
commit
81dfbfd474
  1. 6
      application/src/test/java/org/thingsboard/server/controller/AbstractControllerTest.java
  2. 3
      application/src/test/java/org/thingsboard/server/controller/TbTestWebSocketClient.java
  3. 8
      application/src/test/java/org/thingsboard/server/controller/TwoFactorAuthTest.java

6
application/src/test/java/org/thingsboard/server/controller/AbstractControllerTest.java

@ -92,7 +92,7 @@ public abstract class AbstractControllerTest extends AbstractNotifyEntityTest {
}
protected TbTestWebSocketClient buildAndConnectWebSocketClient(String path) throws URISyntaxException, InterruptedException {
TbTestWebSocketClient wsClient = new TbTestWebSocketClient(new URI(WS_URL + wsPort + path));
TbTestWebSocketClient wsClient = buildWebSocketClient(path);
assertThat(wsClient.connectBlocking(TIMEOUT, TimeUnit.SECONDS)).isTrue();
if (!path.contains("token=")) {
wsClient.authenticate(token);
@ -100,4 +100,8 @@ public abstract class AbstractControllerTest extends AbstractNotifyEntityTest {
return wsClient;
}
protected TbTestWebSocketClient buildWebSocketClient(String path) throws URISyntaxException {
return new TbTestWebSocketClient(new URI(WS_URL + wsPort + path));
}
}

3
application/src/test/java/org/thingsboard/server/controller/TbTestWebSocketClient.java

@ -42,6 +42,8 @@ public class TbTestWebSocketClient extends WebSocketClient {
@Getter
private volatile String lastMsg;
@Getter
private volatile Integer closeCode;
private volatile CountDownLatch reply;
private volatile CountDownLatch update;
@ -74,6 +76,7 @@ public class TbTestWebSocketClient extends WebSocketClient {
@Override
public void onClose(int i, String s, boolean b) {
closeCode = i;
log.info("CLOSED.");
}

8
application/src/test/java/org/thingsboard/server/controller/TwoFactorAuthTest.java

@ -4,6 +4,7 @@ package org.thingsboard.server.controller;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import org.java_websocket.framing.CloseFrame;
import org.jboss.aerogear.security.otp.Totp;
import org.junit.After;
import org.junit.Before;
@ -161,9 +162,14 @@ public class TwoFactorAuthTest extends AbstractControllerTest {
configureTotpTwoFa();
logInWithPreVerificationToken(username, password);
wsClient = buildAndConnectWebSocketClient("/api/ws?token=" + token);
// The server accepts the handshake and closes the session right away from afterConnectionEstablished().
// connectBlocking() returns false when that close frame is processed before it checks the open state,
// so its result is not asserted here; the server-side close code proves the token was rejected.
wsClient = buildWebSocketClient("/api/ws?token=" + token);
wsClient.connectBlocking(TIMEOUT, TimeUnit.SECONDS);
await().atMost(TIMEOUT, TimeUnit.SECONDS).until(wsClient::isClosed);
assertThat(wsClient.getCloseCode()).isEqualTo(CloseFrame.UNEXPECTED_CONDITION);
}
@Test

Loading…
Cancel
Save