package eu.friedcutlet.yokan.services.notifications import eu.friedcutlet.yokan.domain.ProfileId import eu.friedcutlet.yokan.app.profiles.ProfileSession import kotlinx.coroutines.test.runTest import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertFalse import kotlin.test.assertNotEquals import kotlin.test.assertNull import kotlin.test.assertTrue class ProfileNotificationsTest { @Test fun defaultAdoptsLegacyAndroidAndIosNotificationIdentities() { val namespace = ProfileNotificationNamespace(ProfileId.Default) for (tag in listOf(null, "", "chapter-42", "periodic.sync")) { assertNull(namespace.androidTag(NotificationChannel.Downloads, tag)) assertEquals(("yokan_downloads" + tag.orEmpty()).hashCode(), namespace.androidId("yokan_downloads", tag)) assertEquals("yokan.downloads.${tag.orEmpty()}", namespace.iosIdentifier(NotificationChannel.Downloads, tag)) } } @Test fun identicalAndHashCollidingTagsCannotCrossExtraOwnersOrChannels() { assertEquals("Aa".hashCode(), "BB".hashCode()) val android = mutableSetOf>() val ios = mutableSetOf() for (id in listOf(A, B)) for (channel in NotificationChannel.entries) for (tag in listOf("Aa", "BB", "chapter-42", "")) { val namespace = ProfileNotificationNamespace(id) assertTrue(android.add(namespace.androidTag(channel, tag) to namespace.androidId("channel", tag))) assertTrue(ios.add(namespace.iosIdentifier(channel, tag))) } assertNotEquals(ProfileNotificationNamespace(A).androidTag(NotificationChannel.Downloads, ""), ProfileNotificationNamespace(A).androidTag(NotificationChannel.Downloads, "0:")) } @Test fun aDefaultTagCannotImpersonateAnExtraOwnersNotificationKey() { val extra = ProfileNotificationNamespace(A) val forgedTag = extra.androidTag(NotificationChannel.Downloads, "chapter-42") val default = ProfileNotificationNamespace(ProfileId.Default) assertNotEquals(default.androidTag(NotificationChannel.Downloads, forgedTag) to default.androidId("channel", forgedTag), extra.androidTag(NotificationChannel.Downloads, "chapter-42") to extra.androidId("channel", "chapter-42")) } @Test fun sessionForcesItsOriginAndSuppressesStaleNotificationReferences() = runTest { val recording = RecordingNotifications() val owner = ProfileSession(A, databaseFactory = { error("Notifications must not open storage") }, notificationsFactory = { assertEquals(A, it); recording }) val captured = owner.notifications captured.notify(NotificationPayload(NotificationChannel.Downloads, "Title", "Progress", "chapter-42", profileId = B)) captured.requestPermission(); captured.cancel(NotificationChannel.Downloads, "chapter-42") assertEquals(A, recording.posted.single().profileId) assertEquals(1, recording.permissionRequests); assertEquals(1, recording.cancelled.size) owner.close() captured.notify(NotificationPayload(NotificationChannel.Downloads, "Stale", "Stale")) captured.cancel(NotificationChannel.Downloads, "chapter-42"); captured.requestPermission() assertFalse(captured.supported()); assertEquals(1, recording.posted.size) assertEquals(1, recording.permissionRequests); assertEquals(1, recording.cancelled.size) } @Test fun retiringOneOwnerLeavesOtherOwnersNotificationEndpointUsable() = runTest { val a = RecordingNotifications(); val b = RecordingNotifications() val left = ProfileSession(A, notificationsFactory = { a }); val right = ProfileSession(B, notificationsFactory = { b }) val old = left.notifications; val current = right.notifications left.close() old.notify(NotificationPayload(NotificationChannel.Sync, "Old", "Old")) current.notify(NotificationPayload(NotificationChannel.Sync, "B", "B")) assertEquals(0, a.posted.size); assertEquals(B, b.posted.single().profileId); assertTrue(current.supported()) right.close() } @Test fun inAppMessagesKeepDeviceEventsVisibleAndSuppressForeignProfileEvents() { val owned = InAppMessage("A", "Private", profileId = A) assertTrue(owned.visibleTo(A)); assertFalse(owned.visibleTo(B)); assertFalse(owned.visibleTo(ProfileId.Default)) val device = InAppMessage("Device", "Permission") assertTrue(device.visibleTo(A)); assertTrue(device.visibleTo(B)); assertTrue(device.visibleTo(ProfileId.Default)) } private companion object { val A = ProfileId("p_" + "a".repeat(32)) val B = ProfileId("p_" + "b".repeat(32)) } }