Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
translate ▾

Modern-Apps.git / communicate-dev / src / test / java / com / vayunmathur / communicate / signal / SignalSlice2SocketTest.kt

SignalSlice2SocketTest.kt85 lines · 3.6 KiBRaw
1package com.vayunmathur.communicate.signal
2
3import androidx.test.core.app.ApplicationProvider
4import com.vayunmathur.communicate.data.signal.SignalAuthData
5import com.vayunmathur.communicate.data.signal.transport.SignalSocket
6import kotlin.test.Test
7import kotlin.test.assertEquals
8import kotlin.test.assertFalse
9import kotlin.test.assertNotNull
10import kotlin.test.assertNull
11import kotlin.test.assertTrue
12import kotlinx.coroutines.test.runTest
13import org.junit.runner.RunWith
14import org.robolectric.RobolectricTestRunner
15import signal.proto.chat_websocket.SignalChatWebsocket.WebSocketRequestMessage
16
17/**
18 * Wave 6 (w6 slice 2/6): [SignalSocket]'s offline-safe surface — URL/auth header construction,
19 * [SignalSocket.RequestResult] status mapping, send-without-session short-circuits, and the
20 * request/response id plumbing. No network: a disconnected socket must fail closed (false/null)
21 * rather than throw, which is what the send pipeline retries on.
22 */
23@RunWith(RobolectricTestRunner::class)
24class SignalSlice2SocketTest {
25
26 private fun socket(
27 aci: String = "3f0e5a2c-1111-2222-3333-444455556666",
28 password: String = "pw",
29 ): SignalSocket {
30 val ctx = ApplicationProvider.getApplicationContext<android.content.Context>()
31 return SignalSocket(
32 context = ctx,
33 authData = SignalAuthData(phoneNumber = "+14155552671", aci = aci, password = password),
34 )
35 }
36
37 @Test
38 fun requestResult_successRange() {
39 assertTrue(SignalSocket.RequestResult(200, "OK", ByteArray(0)).isSuccess)
40 assertTrue(SignalSocket.RequestResult(299, "OK", ByteArray(0)).isSuccess)
41 assertFalse(SignalSocket.RequestResult(199, "", ByteArray(0)).isSuccess)
42 assertFalse(SignalSocket.RequestResult(300, "", ByteArray(0)).isSuccess)
43 assertFalse(SignalSocket.RequestResult(409, "mismatch", ByteArray(0)).isSuccess)
44 assertFalse(SignalSocket.RequestResult(0, "", ByteArray(0)).isSuccess)
45 }
46
47 @Test
48 fun disconnectedSocket_sendFailsClosed() = runTest {
49 val s = socket()
50 assertFalse(s.send(byteArrayOf(1, 2, 3)))
51 assertFalse(s.sendText("hello"))
52 val ack = signal.proto.chat_websocket.SignalChatWebsocket.WebSocketResponseMessage
53 .newBuilder().setId(1L).setStatus(200).build()
54 assertFalse(s.sendResponse(ack))
55 }
56
57 @Test
58 fun disconnectedSocket_requestTimesOutToNull() = runTest {
59 val s = socket()
60 val req = WebSocketRequestMessage.newBuilder()
61 .setVerb("PUT").setPath("/v1/keepalive").setId(123L).build()
62 // No session: send() is false, so the request path returns null quickly.
63 assertNull(s.sendRequestAwaitingResponse(req, timeoutMs = 200L))
64 }
65
66 @Test
67 fun constants_matchServerContract() {
68 assertEquals("chat.signal.org", SignalSocket.DEFAULT_HOST)
69 assertEquals(443, SignalSocket.DEFAULT_PORT)
70 assertEquals(4401, SignalSocket.CLOSE_INVALID_AUTH)
71 assertEquals(4409, SignalSocket.CLOSE_CONNECTED_ELSEWHERE)
72 }
73
74 @Test
75 fun connectionState_sealedShape() {
76 val connecting: SignalSocket.ConnectionState = SignalSocket.ConnectionState.Connecting
77 val connected: SignalSocket.ConnectionState = SignalSocket.ConnectionState.Connected
78 val dropped: SignalSocket.ConnectionState =
79 SignalSocket.ConnectionState.Disconnected("bye")
80 assertTrue(connecting is SignalSocket.ConnectionState.Connecting)
81 assertTrue(connected is SignalSocket.ConnectionState.Connected)
82 assertEquals("bye", (dropped as SignalSocket.ConnectionState.Disconnected).reason)
83 assertNotNull(dropped)
84 }
85}