Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / madeveloper / src / test / java / com / vayunmathur / madeveloper / DevProtocolTest.kt
DevProtocolTest.kt63 lines · 3.1 KiBRaw
| 1 | package com.vayunmathur.madeveloper |
| 2 | |
| 3 | import com.vayunmathur.madeveloper.domain.DevMessage |
| 4 | import com.vayunmathur.madeveloper.domain.DevProtocol |
| 5 | import com.vayunmathur.madeveloper.domain.InstallPhase |
| 6 | import com.vayunmathur.madeveloper.domain.InstalledAppInfo |
| 7 | import com.vayunmathur.madeveloper.domain.PendingInstall |
| 8 | import kotlin.test.Test |
| 9 | import kotlin.test.assertEquals |
| 10 | import kotlin.test.assertFalse |
| 11 | import kotlin.test.assertTrue |
| 12 | |
| 13 | /** Tests for the push protocol, backoff, hashes and install state machine. */ |
| 14 | class DevProtocolTest { |
| 15 | @Test fun parsesPushMessages() { |
| 16 | assertTrue(DevProtocol.parse("""{"type":"ping"}""") is DevMessage.Ping) |
| 17 | assertTrue(DevProtocol.parse("""{"type":"listApps"}""") is DevMessage.ListApps) |
| 18 | val install = DevProtocol.parse("""{"type":"install","package":"com.vayunmathur.notes","versionCode":3,"apkUrl":"https://x/a.apk","sha256":"ab","sizeBytes":10}""") |
| 19 | assertTrue(install is DevMessage.Install) |
| 20 | assertEquals(3L, (install as DevMessage.Install).versionCode) |
| 21 | assertTrue(DevProtocol.parse("not json") is DevMessage.Unknown) |
| 22 | } |
| 23 | |
| 24 | @Test fun backoff() { |
| 25 | assertEquals(1000L, DevProtocol.backoffMs(0)) |
| 26 | assertEquals(2000L, DevProtocol.backoffMs(1)) |
| 27 | assertEquals(60000L, DevProtocol.backoffMs(20)) |
| 28 | } |
| 29 | |
| 30 | @Test fun registerContractRoundTrip() { |
| 31 | assertEquals("""{"deviceName":"Pixel 9"}""", DevProtocol.registerRequestBody("Pixel 9")) |
| 32 | assertEquals("""{"deviceName":"a\"b"}""", DevProtocol.registerRequestBody("a\"b")) |
| 33 | val registration = DevProtocol.parseRegistration( |
| 34 | """{"deviceId":"dev-1","pushToken":"tok-1","pairingCode":"123456"}""", |
| 35 | ) |
| 36 | assertEquals("dev-1", registration?.deviceId) |
| 37 | assertEquals("tok-1", registration?.pushToken) |
| 38 | assertEquals("123456", registration?.pairingCode) |
| 39 | assertEquals(null, DevProtocol.parseRegistration("""{"deviceId":"dev-1"}""")) |
| 40 | assertEquals(null, DevProtocol.parseRegistration("not json")) |
| 41 | // A stale server "paired" frame is tolerated as unknown, never acted on. |
| 42 | assertTrue(DevProtocol.parse("""{"type":"paired","token":"t","deviceId":"d"}""") is DevMessage.Unknown) |
| 43 | } |
| 44 | |
| 45 | @Test fun shaVerification() { |
| 46 | val bytes = "hello".toByteArray() |
| 47 | val hash = DevProtocol.sha256Hex(bytes) |
| 48 | assertTrue(DevProtocol.verifySha256(bytes, hash.uppercase())) |
| 49 | assertFalse(DevProtocol.verifySha256(bytes, "00")) |
| 50 | } |
| 51 | |
| 52 | @Test fun installStateMachineRejectsIllegalJumps() { |
| 53 | val pushed = PendingInstall("com.vayunmathur.notes", 1, "https://x/a.apk", "ab", 10) |
| 54 | assertEquals(InstallPhase.PUSHED, DevProtocol.transition(pushed, InstallPhase.READY).phase) |
| 55 | val downloading = DevProtocol.transition(pushed, InstallPhase.DOWNLOADING) |
| 56 | assertEquals(InstallPhase.VERIFYING, DevProtocol.transition(downloading, InstallPhase.VERIFYING).phase) |
| 57 | } |
| 58 | |
| 59 | @Test fun listAppsReplyShape() { |
| 60 | val reply = DevProtocol.listAppsReply(listOf(InstalledAppInfo("com.vayunmathur.notes", 5))) |
| 61 | assertEquals("""{"type":"installed","packages":[{"package":"com.vayunmathur.notes","versionCode":5}]}""", reply) |
| 62 | } |
| 63 | } |