-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathVoipPayloadExpiryTest.kt
More file actions
108 lines (94 loc) · 4.12 KB
/
VoipPayloadExpiryTest.kt
File metadata and controls
108 lines (94 loc) · 4.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package chat.rocket.reactnative.voip
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config
import java.text.SimpleDateFormat
import java.util.Locale
import java.util.TimeZone
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [28])
class VoipPayloadExpiryTest {
private val incomingCallLifetimeMs = 60_000L
private fun isoString(epochMs: Long): String {
val formatter = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX", Locale.US)
formatter.timeZone = TimeZone.getTimeZone("UTC")
return formatter.format(java.util.Date(epochMs))
}
private fun makePayload(createdAt: String?): VoipPayload {
return VoipPayload(
callId = "call-1",
caller = "Caller",
username = "user1",
host = "https://example.com",
type = VoipPushType.INCOMING_CALL.value,
hostName = "Example",
avatarUrl = null,
createdAt = createdAt,
voipAcceptFailed = false,
)
}
@Test
fun `getRemainingLifetimeMs returns full lifetime when device clock is far ahead of createdAt`() {
val createdAtMs = 1_700_000_000_000L
val payload = makePayload(isoString(createdAtMs))
// Device clock is 20 minutes ahead of createdAt -> skew exceeds threshold (10 min)
val nowMs = createdAtMs + 20 * 60_000L
assertEquals(incomingCallLifetimeMs, payload.getRemainingLifetimeMs(nowMs))
}
@Test
fun `getRemainingLifetimeMs returns full lifetime when device clock is far behind createdAt`() {
val createdAtMs = 1_700_000_000_000L
val payload = makePayload(isoString(createdAtMs))
// Device clock is 20 minutes behind createdAt -> skew exceeds threshold (10 min)
val nowMs = createdAtMs - 20 * 60_000L
assertEquals(incomingCallLifetimeMs, payload.getRemainingLifetimeMs(nowMs))
}
@Test
fun `getRemainingLifetimeMs returns expected remaining when skew is small and call is fresh`() {
val createdAtMs = 1_700_000_000_000L
val payload = makePayload(isoString(createdAtMs))
// 30s after createdAt -> ~30_000 ms remaining
val nowMs = createdAtMs + 30_000L
val remaining = payload.getRemainingLifetimeMs(nowMs)
assertEquals(30_000L, remaining)
}
@Test
fun `getRemainingLifetimeMs returns 0 when skew is small and call is stale`() {
val createdAtMs = 1_700_000_000_000L
val payload = makePayload(isoString(createdAtMs))
// 70s after createdAt -> expired
val nowMs = createdAtMs + 70_000L
assertEquals(0L, payload.getRemainingLifetimeMs(nowMs))
}
@Test
fun `getRemainingLifetimeMs returns null when createdAt is missing`() {
val payload = makePayload(null)
assertNull(payload.getRemainingLifetimeMs(System.currentTimeMillis()))
}
@Test
fun `getRemainingLifetimeMs returns null when createdAt is unparseable`() {
val payload = makePayload("not-a-date")
assertNull(payload.getRemainingLifetimeMs(System.currentTimeMillis()))
}
@Test
fun `isExpired follows getRemainingLifetimeMs across all skew scenarios`() {
val createdAtMs = 1_700_000_000_000L
val payload = makePayload(isoString(createdAtMs))
// Far-ahead skew: device clock untrusted -> not expired
assertFalse(payload.isExpired(createdAtMs + 20 * 60_000L))
// Far-behind skew: device clock untrusted -> not expired
assertFalse(payload.isExpired(createdAtMs - 20 * 60_000L))
// Fresh call within trusted skew -> not expired
assertFalse(payload.isExpired(createdAtMs + 30_000L))
// Stale call within trusted skew -> expired
assertTrue(payload.isExpired(createdAtMs + 70_000L))
// Missing createdAt -> expired (remaining lifetime is null)
val nullPayload = makePayload(null)
assertTrue(nullPayload.isExpired(System.currentTimeMillis()))
}
}