|
| 1 | +package net.twasi.obsremotejava.test; |
| 2 | + |
| 3 | +import net.twasi.obsremotejava.OBSCommunicator; |
| 4 | +import org.eclipse.jetty.websocket.api.Session; |
| 5 | +import org.eclipse.jetty.websocket.client.ClientUpgradeRequest; |
| 6 | +import org.eclipse.jetty.websocket.client.WebSocketClient; |
| 7 | +import org.junit.jupiter.api.Test; |
| 8 | + |
| 9 | +import java.net.URI; |
| 10 | +import java.util.concurrent.Future; |
| 11 | +import java.util.concurrent.TimeUnit; |
| 12 | +import java.util.concurrent.atomic.AtomicReference; |
| 13 | + |
| 14 | +import static org.junit.jupiter.api.Assertions.*; |
| 15 | + |
| 16 | +/** |
| 17 | + * Read comment instructions before each test |
| 18 | + */ |
| 19 | +class OBSCommunicatorIT { |
| 20 | + |
| 21 | + /** |
| 22 | + * - Set these two values before running these tests |
| 23 | + * - Make sure your OBS is running and available for connection |
| 24 | + */ |
| 25 | + private final String obsAddress = "ws://localhost:4444"; |
| 26 | + private final String obsPassword = "password"; |
| 27 | + |
| 28 | + /** |
| 29 | + * Before running this test: |
| 30 | + * - Start OBS locally |
| 31 | + * - Disable websocket authentication |
| 32 | + * - Run test |
| 33 | + */ |
| 34 | + @Test |
| 35 | + void testConnectToUnsecureServerWithoutPassword() throws Exception { |
| 36 | + WebSocketClient client = new WebSocketClient(); |
| 37 | + OBSCommunicator connector = new OBSCommunicator(true); |
| 38 | + |
| 39 | + AtomicReference<String> testFailedReason = new AtomicReference<>(); |
| 40 | + |
| 41 | + try { |
| 42 | + client.start(); |
| 43 | + |
| 44 | + URI echoUri = new URI(obsAddress); |
| 45 | + ClientUpgradeRequest request = new ClientUpgradeRequest(); |
| 46 | + client.connect(connector, echoUri, request); |
| 47 | + System.out.printf("Connecting to : %s%n", echoUri); |
| 48 | + |
| 49 | + connector.registerOnDisconnect(response -> System.out.println("Disconnected")); |
| 50 | + |
| 51 | + connector.registerOnConnect(response -> { |
| 52 | + System.out.println("Connected successfully without password!"); |
| 53 | + closeConnectionAndStopClient(client, connector); |
| 54 | + }); |
| 55 | + |
| 56 | + connector.registerOnConnectionFailed(message -> { |
| 57 | + testFailedReason.set("Connection failed:" + message); |
| 58 | + closeConnectionAndStopClient(client, connector); |
| 59 | + }); |
| 60 | + |
| 61 | + connector.await(); |
| 62 | + |
| 63 | + } finally { |
| 64 | + closeConnectionAndStopClient(client, connector); |
| 65 | + } |
| 66 | + |
| 67 | + if (testFailedReason.get() != null) { |
| 68 | + fail(testFailedReason.get()); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Before running this test: |
| 74 | + * - Start OBS locally |
| 75 | + * - Enable websocket authentication |
| 76 | + * - Run test |
| 77 | + */ |
| 78 | + @Test |
| 79 | + void testConnectToSecuredServerWithoutPassword() throws Exception { |
| 80 | + String websocketPassword = null; |
| 81 | + |
| 82 | + WebSocketClient client = new WebSocketClient(); |
| 83 | + OBSCommunicator connector = new OBSCommunicator(true, websocketPassword); |
| 84 | + |
| 85 | + AtomicReference<String> testFailedReason = new AtomicReference<>(); |
| 86 | + AtomicReference<String> connectionFailedResult = new AtomicReference<>(); |
| 87 | + |
| 88 | + try { |
| 89 | + client.start(); |
| 90 | + |
| 91 | + URI echoUri = new URI(obsAddress); |
| 92 | + ClientUpgradeRequest request = new ClientUpgradeRequest(); |
| 93 | + Future<Session> connection = client.connect(connector, echoUri, request); |
| 94 | + System.out.printf("Connecting to : %s%n", echoUri); |
| 95 | + |
| 96 | + connection.get(); |
| 97 | + |
| 98 | + connector.registerOnDisconnect(response -> System.out.println("Disconnected")); |
| 99 | + |
| 100 | + connector.registerOnConnect(response -> { |
| 101 | + testFailedReason.set("Connected without a password to secured server"); |
| 102 | + closeConnectionAndStopClient(client, connector); |
| 103 | + }); |
| 104 | + |
| 105 | + connector.registerOnConnectionFailed(message -> { |
| 106 | + connectionFailedResult.set(message); |
| 107 | + closeConnectionAndStopClient(client, connector); |
| 108 | + }); |
| 109 | + |
| 110 | + connector.await(); |
| 111 | + |
| 112 | + } finally { |
| 113 | + closeConnectionAndStopClient(client, connector); |
| 114 | + } |
| 115 | + |
| 116 | + if (testFailedReason.get() != null) { |
| 117 | + fail(testFailedReason.get()); |
| 118 | + } |
| 119 | + |
| 120 | + assertEquals("Authentication required by server but no password set by client", |
| 121 | + connectionFailedResult.get()); |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Before running this test: |
| 126 | + * - Start OBS locally |
| 127 | + * - Enable websocket authentication |
| 128 | + * - Run test |
| 129 | + */ |
| 130 | + @Test |
| 131 | + void testConnectToSecuredServerWithInCorrectPassword() throws Exception { |
| 132 | + String websocketPassword = obsPassword + "giberish"; |
| 133 | + |
| 134 | + WebSocketClient client = new WebSocketClient(); |
| 135 | + OBSCommunicator connector = new OBSCommunicator(true, websocketPassword); |
| 136 | + |
| 137 | + AtomicReference<String> testFailedReason = new AtomicReference<>(); |
| 138 | + AtomicReference<String> connectionFailedResult = new AtomicReference<>(); |
| 139 | + |
| 140 | + try { |
| 141 | + client.start(); |
| 142 | + |
| 143 | + URI echoUri = new URI(obsAddress); |
| 144 | + ClientUpgradeRequest request = new ClientUpgradeRequest(); |
| 145 | + Future<Session> connection = client.connect(connector, echoUri, request); |
| 146 | + System.out.printf("Connecting to : %s%n", echoUri); |
| 147 | + |
| 148 | + connection.get(); |
| 149 | + |
| 150 | + connector.registerOnDisconnect(response -> System.out.println("Disconnected")); |
| 151 | + |
| 152 | + connector.registerOnConnect(response -> { |
| 153 | + testFailedReason.set("Connected with an incorrect password to secured server"); |
| 154 | + closeConnectionAndStopClient(client, connector); |
| 155 | + }); |
| 156 | + |
| 157 | + connector.registerOnConnectionFailed(message -> { |
| 158 | + connectionFailedResult.set(message); |
| 159 | + closeConnectionAndStopClient(client, connector); |
| 160 | + }); |
| 161 | + |
| 162 | + connector.await(); |
| 163 | + |
| 164 | + } finally { |
| 165 | + closeConnectionAndStopClient(client, connector); |
| 166 | + } |
| 167 | + |
| 168 | + if (testFailedReason.get() != null) { |
| 169 | + fail(testFailedReason.get()); |
| 170 | + } |
| 171 | + |
| 172 | + assertEquals("Failed to authenticate with password. Error: Authentication Failed.", |
| 173 | + connectionFailedResult.get()); |
| 174 | + } |
| 175 | + |
| 176 | + /** |
| 177 | + * Before running this test: |
| 178 | + * - Start OBS locally |
| 179 | + * - Enable websocket authentication |
| 180 | + * - Set obsPassword to your OBS websocket's password |
| 181 | + * - Run test |
| 182 | + */ |
| 183 | + @Test |
| 184 | + void testConnectToSecuredServerWithCorrectPassword() throws Exception { |
| 185 | + String websocketPassword = obsPassword; |
| 186 | + |
| 187 | + WebSocketClient client = new WebSocketClient(); |
| 188 | + OBSCommunicator connector = new OBSCommunicator(true, websocketPassword); |
| 189 | + |
| 190 | + AtomicReference<String> testFailedReason = new AtomicReference<>(); |
| 191 | + |
| 192 | + try { |
| 193 | + client.start(); |
| 194 | + |
| 195 | + URI echoUri = new URI(obsAddress); |
| 196 | + ClientUpgradeRequest request = new ClientUpgradeRequest(); |
| 197 | + Future<Session> connection = client.connect(connector, echoUri, request); |
| 198 | + System.out.printf("Connecting to : %s%n", echoUri); |
| 199 | + |
| 200 | + connection.get(); |
| 201 | + |
| 202 | + connector.registerOnDisconnect(response -> System.out.println("Disconnected")); |
| 203 | + |
| 204 | + connector.registerOnConnect(response -> { |
| 205 | + System.out.println("Connected successfully with password!"); |
| 206 | + closeConnectionAndStopClient(client, connector); |
| 207 | + }); |
| 208 | + |
| 209 | + connector.registerOnConnectionFailed(message -> { |
| 210 | + testFailedReason.set("Connection failed:" + message); |
| 211 | + closeConnectionAndStopClient(client, connector); |
| 212 | + }); |
| 213 | + |
| 214 | + connector.await(); |
| 215 | + |
| 216 | + } finally { |
| 217 | + closeConnectionAndStopClient(client, connector); |
| 218 | + } |
| 219 | + |
| 220 | + if (testFailedReason.get() != null) { |
| 221 | + fail(testFailedReason.get()); |
| 222 | + } |
| 223 | + } |
| 224 | + |
| 225 | + private void closeConnectionAndStopClient(WebSocketClient client, OBSCommunicator connector) { |
| 226 | + // wait for closed socket connection |
| 227 | + try { |
| 228 | + System.out.println("Closing connection"); |
| 229 | + connector.awaitClose(1, TimeUnit.SECONDS); |
| 230 | + } catch (InterruptedException e) { |
| 231 | + e.printStackTrace(); |
| 232 | + } |
| 233 | + |
| 234 | + if (!client.isStopped() && !client.isStopping()) { |
| 235 | + try { |
| 236 | + System.out.println("Stopping client"); |
| 237 | + client.stop(); |
| 238 | + } catch (Exception e) { |
| 239 | + e.printStackTrace(); |
| 240 | + } |
| 241 | + } |
| 242 | + } |
| 243 | + |
| 244 | +} |
0 commit comments