|
| 1 | +package net.twasi.obsremotejava.test; |
| 2 | + |
| 3 | +import net.twasi.obsremotejava.OBSCommunicator; |
| 4 | +import org.eclipse.jetty.websocket.client.ClientUpgradeRequest; |
| 5 | +import org.eclipse.jetty.websocket.client.WebSocketClient; |
| 6 | +import org.junit.jupiter.api.Test; |
| 7 | + |
| 8 | +import java.net.URI; |
| 9 | +import java.util.concurrent.TimeUnit; |
| 10 | +import java.util.concurrent.atomic.AtomicReference; |
| 11 | + |
| 12 | +import static org.junit.jupiter.api.Assertions.fail; |
| 13 | + |
| 14 | +public class OBSCommunicatorUnsecuredIT { |
| 15 | + /** |
| 16 | + * - Set these two values before running these tests |
| 17 | + * - Make sure your OBS is running and available for connection |
| 18 | + */ |
| 19 | + private final String obsAddress = "ws://localhost:4444"; |
| 20 | + |
| 21 | + /** |
| 22 | + * Before running this test: |
| 23 | + * - Start OBS locally |
| 24 | + * - Disable websocket authentication |
| 25 | + * - Run test |
| 26 | + */ |
| 27 | + @Test |
| 28 | + void testConnectToUnsecureServerWithoutPassword() throws Exception { |
| 29 | + WebSocketClient client = new WebSocketClient(); |
| 30 | + OBSCommunicator connector = new OBSCommunicator(true); |
| 31 | + |
| 32 | + AtomicReference<String> testFailedReason = new AtomicReference<>(); |
| 33 | + |
| 34 | + try { |
| 35 | + client.start(); |
| 36 | + |
| 37 | + URI echoUri = new URI(obsAddress); |
| 38 | + ClientUpgradeRequest request = new ClientUpgradeRequest(); |
| 39 | + client.connect(connector, echoUri, request); |
| 40 | + System.out.printf("Connecting to : %s%n", echoUri); |
| 41 | + |
| 42 | + connector.registerOnDisconnect(response -> System.out.println("Disconnected")); |
| 43 | + |
| 44 | + connector.registerOnConnect(response -> { |
| 45 | + System.out.println("Connected successfully without password!"); |
| 46 | + closeConnectionAndStopClient(client, connector); |
| 47 | + }); |
| 48 | + |
| 49 | + connector.registerOnConnectionFailed(message -> { |
| 50 | + testFailedReason.set("Connection failed:" + message); |
| 51 | + closeConnectionAndStopClient(client, connector); |
| 52 | + }); |
| 53 | + |
| 54 | + connector.await(); |
| 55 | + |
| 56 | + } finally { |
| 57 | + closeConnectionAndStopClient(client, connector); |
| 58 | + } |
| 59 | + |
| 60 | + if (testFailedReason.get() != null) { |
| 61 | + fail(testFailedReason.get()); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + private void closeConnectionAndStopClient(WebSocketClient client, OBSCommunicator connector) { |
| 66 | + // wait for closed socket connection |
| 67 | + try { |
| 68 | + System.out.println("Closing connection"); |
| 69 | + connector.awaitClose(1, TimeUnit.SECONDS); |
| 70 | + } catch (InterruptedException e) { |
| 71 | + e.printStackTrace(); |
| 72 | + } |
| 73 | + |
| 74 | + if (!client.isStopped() && !client.isStopping()) { |
| 75 | + try { |
| 76 | + System.out.println("Stopping client"); |
| 77 | + client.stop(); |
| 78 | + } catch (Exception e) { |
| 79 | + e.printStackTrace(); |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | +} |
0 commit comments