Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / communicate-dev / src / test / java / com / vayunmathur / communicate / signal / SignalSlice2DatabaseTest.kt
SignalSlice2DatabaseTest.kt378 lines ยท 15.2 KiBRaw
| 1 | package com.vayunmathur.communicate.signal |
| 2 | |
| 3 | import androidx.room3.Room |
| 4 | import androidx.sqlite.driver.bundled.BundledSQLiteDriver |
| 5 | import androidx.test.core.app.ApplicationProvider |
| 6 | import com.vayunmathur.communicate.data.signal.SignalCachedMessage |
| 7 | import com.vayunmathur.communicate.data.signal.SignalCallLog |
| 8 | import com.vayunmathur.communicate.data.signal.SignalContact |
| 9 | import com.vayunmathur.communicate.data.signal.SignalConversation |
| 10 | import com.vayunmathur.communicate.data.signal.SignalDatabase |
| 11 | import com.vayunmathur.communicate.data.signal.SignalTypeConverters |
| 12 | import kotlin.test.AfterTest |
| 13 | import kotlin.test.BeforeTest |
| 14 | import kotlin.test.Test |
| 15 | import kotlin.test.assertEquals |
| 16 | import kotlin.test.assertNull |
| 17 | import kotlin.test.assertTrue |
| 18 | import kotlinx.coroutines.test.runTest |
| 19 | import org.junit.runner.RunWith |
| 20 | import org.robolectric.RobolectricTestRunner |
| 21 | |
| 22 | /** |
| 23 | * Wave 2 (w6 slice 2/6): [SignalDatabase] DAO round trips โ conversations, cached messages |
| 24 | * (incl. the delivery-status ladder), reactions, contacts (ACI/PNI lookups), sender |
| 25 | * certificates, profile keys, E2E stores and the call log โ plus type converters and the |
| 26 | * migration chain. In-memory Room under Robolectric, mirroring RcsSlice3DatabaseTest. |
| 27 | */ |
| 28 | @RunWith(RobolectricTestRunner::class) |
| 29 | class SignalSlice2DatabaseTest { |
| 30 | |
| 31 | private lateinit var db: SignalDatabase |
| 32 | |
| 33 | @BeforeTest |
| 34 | fun setUp() { |
| 35 | val ctx = ApplicationProvider.getApplicationContext<android.content.Context>() |
| 36 | db = Room.inMemoryDatabaseBuilder(ctx, SignalDatabase::class.java) |
| 37 | .setDriver(BundledSQLiteDriver()) |
| 38 | .allowMainThreadQueries() |
| 39 | .build() |
| 40 | } |
| 41 | |
| 42 | @AfterTest |
| 43 | fun tearDown() { |
| 44 | db.close() |
| 45 | } |
| 46 | |
| 47 | @Test |
| 48 | fun conversationCrudAndFlags() = runTest { |
| 49 | val dao = db.conversationDao() |
| 50 | assertNull(dao.getConversation("sig-alice")) |
| 51 | dao.upsert(SignalConversation(chatId = "sig-alice", name = "Alice", unreadCount = 2)) |
| 52 | val got = dao.getConversation("sig-alice") ?: error("missing") |
| 53 | assertEquals("Alice", got.name) |
| 54 | assertEquals(2, got.unreadCount) |
| 55 | // Replace + flags. |
| 56 | dao.upsert(got.copy(pinned = true, archived = true, isGroup = true, participants = "a,b")) |
| 57 | val flagged = dao.getConversation("sig-alice")!! |
| 58 | assertTrue(flagged.pinned && flagged.archived && flagged.isGroup) |
| 59 | assertEquals("a,b", flagged.participants) |
| 60 | dao.updatePinned("sig-alice", false) |
| 61 | assertEquals(false, dao.getConversation("sig-alice")?.pinned) |
| 62 | dao.updateArchived("sig-alice", false) |
| 63 | assertEquals(false, dao.getConversation("sig-alice")?.archived) |
| 64 | // Recent ordering + delete. |
| 65 | dao.upsert(SignalConversation(chatId = "sig-bob", lastMessageTimestamp = 999L)) |
| 66 | assertEquals("sig-bob", dao.getRecent(10).first().chatId) |
| 67 | dao.delete("sig-alice") |
| 68 | assertNull(dao.getConversation("sig-alice")) |
| 69 | dao.deleteAll() |
| 70 | assertTrue(dao.getRecent(10).isEmpty()) |
| 71 | } |
| 72 | |
| 73 | @Test |
| 74 | fun groupConversationKeepsMasterKeyAndRevision() = runTest { |
| 75 | val dao = db.conversationDao() |
| 76 | val masterKey = ByteArray(32) { 7 } |
| 77 | dao.upsert( |
| 78 | SignalConversation( |
| 79 | chatId = "group:abc", isGroup = true, name = "Trip", |
| 80 | participants = "alice,bob", groupMasterKey = masterKey, groupRevision = 3, |
| 81 | ), |
| 82 | ) |
| 83 | val got = dao.getConversation("group:abc")!! |
| 84 | assertTrue(got.groupMasterKey!!.contentEquals(masterKey)) |
| 85 | assertEquals(3, got.groupRevision) |
| 86 | assertEquals("alice,bob", got.participants) |
| 87 | } |
| 88 | |
| 89 | @Test |
| 90 | fun cachedMessageCrudAndStatusLadder() = runTest { |
| 91 | val dao = db.cachedMessageDao() |
| 92 | dao.upsert( |
| 93 | SignalCachedMessage( |
| 94 | messageId = "s2-m1", conversationId = "s2-conv", body = "hello", |
| 95 | timestamp = 100L, outgoing = true, senderId = "me", |
| 96 | ), |
| 97 | ) |
| 98 | assertEquals("hello", dao.get("s2-m1")?.body) |
| 99 | // Status only advances upward. |
| 100 | dao.upsert(dao.get("s2-m1")!!.copy(status = 1)) |
| 101 | dao.markDelivered("s2-m1") |
| 102 | assertEquals(2, dao.get("s2-m1")?.status) |
| 103 | dao.markDelivered("s2-m1") |
| 104 | assertEquals(2, dao.get("s2-m1")?.status) |
| 105 | dao.markReadStatus("s2-m1") |
| 106 | assertEquals(3, dao.get("s2-m1")?.status) |
| 107 | dao.markReadStatus("s2-m1") |
| 108 | assertEquals(3, dao.get("s2-m1")?.status) |
| 109 | // Second message for ordering + lookups. |
| 110 | dao.upsert( |
| 111 | SignalCachedMessage( |
| 112 | messageId = "s2-m2", conversationId = "s2-conv", body = "second", |
| 113 | timestamp = 200L, outgoing = true, |
| 114 | ), |
| 115 | ) |
| 116 | assertEquals(listOf("s2-m1", "s2-m2"), dao.getForConversation("s2-conv").map { it.messageId }) |
| 117 | assertEquals("s2-m2", dao.getLatestPerConversation().first().messageId) |
| 118 | // Timestamp lookups: outgoing-only vs either direction. |
| 119 | assertEquals("s2-m2", dao.getOutgoingByTimestamp(200L)?.messageId) |
| 120 | dao.upsert( |
| 121 | SignalCachedMessage( |
| 122 | messageId = "s2-m3", conversationId = "s2-conv", body = "in", |
| 123 | timestamp = 300L, outgoing = false, |
| 124 | ), |
| 125 | ) |
| 126 | assertNull(dao.getOutgoingByTimestamp(300L)) |
| 127 | assertEquals("s2-m3", dao.getByTimestamp(300L)?.messageId) |
| 128 | // Edit / revoke / service-data. |
| 129 | dao.markEdited("s2-m1", "hello v2") |
| 130 | assertEquals("hello v2", dao.get("s2-m1")?.body) |
| 131 | assertEquals(true, dao.get("s2-m1")?.isEdited) |
| 132 | dao.markRevoked("s2-m2") |
| 133 | assertEquals("", dao.get("s2-m2")?.body) |
| 134 | assertEquals(true, dao.get("s2-m2")?.isRevoked) |
| 135 | dao.updateServiceData("s2-m1", """{"isEdited":true}""") |
| 136 | assertEquals("""{"isEdited":true}""", dao.get("s2-m1")?.serviceData) |
| 137 | // Delete by conversation + all. |
| 138 | dao.deleteConversation("s2-conv") |
| 139 | assertTrue(dao.getForConversation("s2-conv").isEmpty()) |
| 140 | dao.upsert( |
| 141 | SignalCachedMessage( |
| 142 | messageId = "s2-m4", conversationId = "s2-other", body = "x", |
| 143 | timestamp = 1L, outgoing = true, |
| 144 | ), |
| 145 | ) |
| 146 | dao.deleteAll() |
| 147 | assertNull(dao.get("s2-m4")) |
| 148 | } |
| 149 | |
| 150 | @Test |
| 151 | fun reactionsUpsertAndRemove() = runTest { |
| 152 | val dao = db.cachedReactionDao() |
| 153 | assertTrue(dao.getForMessage("s2-m1").isEmpty()) |
| 154 | dao.upsert( |
| 155 | com.vayunmathur.communicate.data.signal.SignalCachedReaction("s2-m1", "๐", "alice", 10L), |
| 156 | ) |
| 157 | dao.upsert( |
| 158 | com.vayunmathur.communicate.data.signal.SignalCachedReaction("s2-m1", "โค", "bob", 11L), |
| 159 | ) |
| 160 | assertEquals(2, dao.getForMessage("s2-m1").size) |
| 161 | // Same sender re-reacts: replace. |
| 162 | dao.upsert( |
| 163 | com.vayunmathur.communicate.data.signal.SignalCachedReaction("s2-m1", "๐", "alice", 12L), |
| 164 | ) |
| 165 | assertEquals(2, dao.getForMessage("s2-m1").size) |
| 166 | assertTrue(dao.getForMessage("s2-m1").map { it.emoji }.contains("๐")) |
| 167 | dao.remove("s2-m1", "alice") |
| 168 | assertEquals(listOf("bob"), dao.getForMessage("s2-m1").map { it.senderId }) |
| 169 | dao.clearForMessage("s2-m1") |
| 170 | assertTrue(dao.getForMessage("s2-m1").isEmpty()) |
| 171 | } |
| 172 | |
| 173 | @Test |
| 174 | fun contactAciPniAndPhoneLookups() = runTest { |
| 175 | val dao = db.contactDao() |
| 176 | dao.upsert( |
| 177 | SignalContact( |
| 178 | aci = "aci-alice", phoneE164 = "+14155552671", displayName = "Alice", |
| 179 | onSignal = true, pni = "PNI:pni-alice", |
| 180 | ), |
| 181 | ) |
| 182 | assertEquals("Alice", dao.get("aci-alice")?.displayName) |
| 183 | assertEquals("aci-alice", dao.getByPhone("+14155552671")?.aci) |
| 184 | assertEquals("aci-alice", dao.getByPni("PNI:pni-alice")?.aci) |
| 185 | assertNull(dao.getByPni("PNI:unknown")) |
| 186 | // Empty PNI never matches. |
| 187 | dao.upsert(SignalContact(aci = "aci-bob", phoneE164 = "+14155550000")) |
| 188 | assertNull(dao.getByPni("")) |
| 189 | assertEquals(1, dao.getOnSignal().size) |
| 190 | assertEquals(2, dao.getAll().size) |
| 191 | dao.deleteByPhone("+14155552671") |
| 192 | assertNull(dao.get("aci-alice")) |
| 193 | dao.deleteAll() |
| 194 | assertTrue(dao.getAll().isEmpty()) |
| 195 | } |
| 196 | |
| 197 | @Test |
| 198 | fun sealedSenderRows() = runTest { |
| 199 | val certDao = db.senderCertificateDao() |
| 200 | assertNull(certDao.get()) |
| 201 | certDao.upsert( |
| 202 | com.vayunmathur.communicate.data.signal.SignalSenderCertificate( |
| 203 | record = byteArrayOf(1, 2, 3), expiration = 999L, includesE164 = false, |
| 204 | ), |
| 205 | ) |
| 206 | val cert = certDao.get()!! |
| 207 | assertTrue(cert.record.contentEquals(byteArrayOf(1, 2, 3))) |
| 208 | assertEquals(999L, cert.expiration) |
| 209 | certDao.clear() |
| 210 | assertNull(certDao.get()) |
| 211 | |
| 212 | val pkDao = db.profileKeyDao() |
| 213 | assertNull(pkDao.get("aci-x")) |
| 214 | pkDao.upsert( |
| 215 | com.vayunmathur.communicate.data.signal.SignalProfileKey("aci-x", ByteArray(32) { 4 }), |
| 216 | ) |
| 217 | assertTrue(pkDao.get("aci-x")!!.profileKey.contentEquals(ByteArray(32) { 4 })) |
| 218 | } |
| 219 | |
| 220 | @Test |
| 221 | fun e2eStoresRoundTrip() = runTest { |
| 222 | sessionStoreRoundTrip() |
| 223 | identityStoreRoundTrip() |
| 224 | preKeyStoreRoundTrip() |
| 225 | signedPreKeyStoreRoundTrip() |
| 226 | kyberStoreRoundTrip() |
| 227 | senderKeyStoreRoundTrip() |
| 228 | } |
| 229 | |
| 230 | private suspend fun sessionStoreRoundTrip() { |
| 231 | // Sessions. |
| 232 | val sessDao = db.e2eSessionDao() |
| 233 | assertEquals(false, sessDao.exists("alice", 1)) |
| 234 | sessDao.insert( |
| 235 | com.vayunmathur.communicate.data.signal.SignalE2ESession("alice", 1, byteArrayOf(9)), |
| 236 | ) |
| 237 | assertEquals(true, sessDao.exists("alice", 1)) |
| 238 | assertEquals(listOf(1), sessDao.getSubDeviceIds("alice").filter { it == 1 }) |
| 239 | sessDao.delete("alice", 1) |
| 240 | assertEquals(false, sessDao.exists("alice", 1)) |
| 241 | } |
| 242 | |
| 243 | private suspend fun identityStoreRoundTrip() { |
| 244 | // Identities. |
| 245 | val idDao = db.e2eIdentityDao() |
| 246 | assertNull(idDao.get("alice")) |
| 247 | idDao.insert( |
| 248 | com.vayunmathur.communicate.data.signal.SignalE2EIdentity("alice", byteArrayOf(5)), |
| 249 | ) |
| 250 | assertTrue(idDao.get("alice")!!.identityKey.contentEquals(byteArrayOf(5))) |
| 251 | idDao.delete("alice") |
| 252 | assertNull(idDao.get("alice")) |
| 253 | } |
| 254 | |
| 255 | private suspend fun preKeyStoreRoundTrip() { |
| 256 | // One-time pre-keys. |
| 257 | val preDao = db.e2ePreKeyDao() |
| 258 | assertEquals(0, preDao.getCount()) |
| 259 | preDao.insert( |
| 260 | com.vayunmathur.communicate.data.signal.SignalE2EPreKey(21, byteArrayOf(1), uploaded = false), |
| 261 | ) |
| 262 | assertEquals(1, preDao.getUnuploaded().size) |
| 263 | assertEquals(21, preDao.getMaxId()) |
| 264 | assertEquals(true, preDao.exists(21)) |
| 265 | preDao.markUploadedUpTo(21) |
| 266 | assertTrue(preDao.getUnuploaded().isEmpty()) |
| 267 | preDao.insertAll( |
| 268 | listOf(com.vayunmathur.communicate.data.signal.SignalE2EPreKey(22, byteArrayOf(2))), |
| 269 | ) |
| 270 | assertEquals(2, preDao.getCount()) |
| 271 | preDao.delete(21) |
| 272 | assertEquals(false, preDao.exists(21)) |
| 273 | } |
| 274 | |
| 275 | private suspend fun signedPreKeyStoreRoundTrip() { |
| 276 | // Signed pre-keys. |
| 277 | val spkDao = db.e2eSignedPreKeyDao() |
| 278 | assertEquals(false, spkDao.exists(11)) |
| 279 | spkDao.insert( |
| 280 | com.vayunmathur.communicate.data.signal.SignalE2ESignedPreKey(11, byteArrayOf(3)), |
| 281 | ) |
| 282 | assertEquals(true, spkDao.exists(11)) |
| 283 | assertEquals(1, spkDao.getAll().size) |
| 284 | spkDao.delete(11) |
| 285 | assertEquals(false, spkDao.exists(11)) |
| 286 | } |
| 287 | |
| 288 | private suspend fun kyberStoreRoundTrip() { |
| 289 | // Kyber pre-keys + used base keys. |
| 290 | val kyDao = db.e2eKyberPreKeyDao() |
| 291 | assertNull(kyDao.get(31)) |
| 292 | kyDao.insert( |
| 293 | com.vayunmathur.communicate.data.signal.SignalE2EKyberPreKey(31, byteArrayOf(4), lastResort = true), |
| 294 | ) |
| 295 | assertEquals(true, kyDao.exists(31)) |
| 296 | assertEquals(true, kyDao.getAll().first().lastResort) |
| 297 | val usedDao = db.e2eKyberUsedBaseKeyDao() |
| 298 | assertEquals(false, usedDao.exists(31, 11, "YmFzZQ==")) |
| 299 | usedDao.insert( |
| 300 | com.vayunmathur.communicate.data.signal.SignalE2EKyberUsedBaseKey(31, 11, "YmFzZQ=="), |
| 301 | ) |
| 302 | assertEquals(true, usedDao.exists(31, 11, "YmFzZQ==")) |
| 303 | usedDao.deleteForKyberPreKey(31) |
| 304 | assertEquals(false, usedDao.exists(31, 11, "YmFzZQ==")) |
| 305 | kyDao.delete(31) |
| 306 | assertEquals(false, kyDao.exists(31)) |
| 307 | } |
| 308 | |
| 309 | private suspend fun senderKeyStoreRoundTrip() { |
| 310 | // Sender keys. |
| 311 | val skDao = db.e2eSenderKeyDao() |
| 312 | assertNull(skDao.get("alice", 1, "dist-1")) |
| 313 | skDao.insert( |
| 314 | com.vayunmathur.communicate.data.signal.SignalE2ESenderKey("alice", 1, "dist-1", byteArrayOf(6)), |
| 315 | ) |
| 316 | assertTrue(skDao.get("alice", 1, "dist-1")!!.record.contentEquals(byteArrayOf(6))) |
| 317 | skDao.delete("alice", 1, "dist-1") |
| 318 | assertNull(skDao.get("alice", 1, "dist-1")) |
| 319 | } |
| 320 | |
| 321 | @Test |
| 322 | fun deviceAndSessionTransportRows() = runTest { |
| 323 | val devDao = db.deviceDao() |
| 324 | assertNull(devDao.getDevice("d1")) |
| 325 | devDao.insertDevice( |
| 326 | com.vayunmathur.communicate.data.signal.SignalDevice("d1", "+1", "Alice", "android", 5L), |
| 327 | ) |
| 328 | assertEquals("+1", devDao.getDevice("d1")?.phoneNumber) |
| 329 | devDao.deleteDevice("d1") |
| 330 | assertNull(devDao.getDevice("d1")) |
| 331 | |
| 332 | val sessDao = db.sessionDao() |
| 333 | assertNull(sessDao.getSession("alice.1")) |
| 334 | assertEquals(false, sessDao.containsSession("alice.1")) |
| 335 | sessDao.insertSession( |
| 336 | com.vayunmathur.communicate.data.signal.SignalSession("alice.1", byteArrayOf(7), 8L), |
| 337 | ) |
| 338 | assertEquals(true, sessDao.containsSession("alice.1")) |
| 339 | assertEquals(1, sessDao.getAllSessions().size) |
| 340 | sessDao.deleteSession("alice.1") |
| 341 | assertEquals(false, sessDao.containsSession("alice.1")) |
| 342 | sessDao.insertSession( |
| 343 | com.vayunmathur.communicate.data.signal.SignalSession("alice.1", byteArrayOf(7), 8L), |
| 344 | ) |
| 345 | sessDao.clearAllSessions() |
| 346 | assertTrue(sessDao.getAllSessions().isEmpty()) |
| 347 | } |
| 348 | |
| 349 | @Test |
| 350 | fun callLogOrdersByStartTimeDesc() = runTest { |
| 351 | val dao = db.callLogDao() |
| 352 | assertNull(dao.get("c1")) |
| 353 | dao.upsert(SignalCallLog(callId = "c1", peerId = "alice", outgoing = true, startTime = 100L)) |
| 354 | dao.upsert(SignalCallLog(callId = "c2", peerId = "bob", outgoing = false, startTime = 200L)) |
| 355 | assertEquals("alice", dao.get("c1")?.peerId) |
| 356 | assertEquals(listOf("c2", "c1"), dao.getRecent(10).map { it.callId }) |
| 357 | assertEquals(1, dao.getRecent(1).size) |
| 358 | // Replace semantics. |
| 359 | dao.upsert(SignalCallLog(callId = "c1", peerId = "alice", durationSeconds = 60L, outcome = "ended")) |
| 360 | assertEquals(60L, dao.get("c1")?.durationSeconds) |
| 361 | dao.deleteAll() |
| 362 | assertTrue(dao.getRecent(10).isEmpty()) |
| 363 | } |
| 364 | |
| 365 | @Test |
| 366 | fun typeConvertersAndMigrations() { |
| 367 | val conv = SignalTypeConverters() |
| 368 | assertEquals("", conv.fromStringList(emptyList())) |
| 369 | assertEquals("a,b,c", conv.fromStringList(listOf("a", "b", "c"))) |
| 370 | assertEquals(emptyList(), conv.toStringList("")) |
| 371 | assertEquals(listOf("a", "b"), conv.toStringList("a,b")) |
| 372 | val versions = SignalDatabase.migrations.map { it.startVersion to it.endVersion } |
| 373 | assertTrue(versions.contains(1 to 2)) |
| 374 | assertTrue(versions.contains(2 to 3)) |
| 375 | assertTrue(versions.contains(3 to 4)) |
| 376 | assertTrue(versions.contains(4 to 5)) |
| 377 | } |
| 378 | } |