euicc: opening a second activation code can no longer install the first one's profile
- Commit
17bab0aa8adb4c12123d57026f70ff783eb3d11e- Author
- Vayun Mathur <[email protected]>
- When
- 2026-12-01 01:43 UTC (5 days ago)
- Parents
09663c9
The download state machine did not know which activation code its state belonged to, and the old route entry started a download only from Idle. Complete profile A, leave, and open code B: from stale Complete(A), B was instantly shown 'download complete' for a profile that never installed; from stale Confirm(A), pressing Download called confirmDownload on A's pendingTransaction — installing A's session while the route named B. The ViewModel now tracks activeActivationCode (set synchronously when a download begins), and route entry goes through enterDownload, which applies the pure downloadStartDecision: same code resumes its terminal/preview state (rotation-safe), a different code restarts after cancelling a stale preview session, busy states never race. Explicit Retry still re-enters terminal states for the same code. New tests cover the decision matrix, both stale-state routes, and an end-to-end fake-SM-DP+ flow where B after A re-authenticates, cancels A's session and installs B's. Profile operations and activation-code validation were audited and cleared. Verified: :euicc:testDevUnitTest 112 tests pass; lint errors are pre-existing on untouched files.
Diff
euicc/src/main/java/com/vayunmathur/euicc/Navigation.kt
diff --git a/euicc/src/main/java/com/vayunmathur/euicc/Navigation.kt b/euicc/src/main/java/com/vayunmathur/euicc/Navigation.ktindex 029c6c0d0d..a38ce07d7a 100644--- a/euicc/src/main/java/com/vayunmathur/euicc/Navigation.kt+++ b/euicc/src/main/java/com/vayunmathur/euicc/Navigation.kt@@ -80,6 +80,7 @@ fun Navigation(viewModel: EuiccViewModel, start: Route? = null) { confirmationCode = route.confirmationCode, state = viewModel.download, onStart = viewModel::startDownload,+ onEnter = viewModel::enterDownload, onConfirm = viewModel::confirmDownload, onSubmitCode = viewModel::submitConfirmationCode, onCancelSession = {
euicc/src/main/java/com/vayunmathur/euicc/platform/DownloadState.kt
diff --git a/euicc/src/main/java/com/vayunmathur/euicc/platform/DownloadState.kt b/euicc/src/main/java/com/vayunmathur/euicc/platform/DownloadState.ktindex 92c201e8dd..6241cc5a1e 100644--- a/euicc/src/main/java/com/vayunmathur/euicc/platform/DownloadState.kt+++ b/euicc/src/main/java/com/vayunmathur/euicc/platform/DownloadState.kt@@ -47,3 +47,51 @@ sealed interface DownloadState { /** Gave up. [message] is the native core's or the server's own wording where there is one. */ data class Failed(val message: String?) : DownloadState }++/** What entering a download destination should do about the existing [DownloadState]. */+internal enum class DownloadStartDecision {+ /** No live session for the requested code: authenticate it. */+ START,++ /** The existing state is already for the requested code: keep showing it. */+ RESUME,++ /** A live preview session exists for a different code: free it, then authenticate. */+ RESTART_AFTER_CANCEL,++ /** Authentication/install is in flight: a new start would race the session. */+ BLOCKED,+}++/**+ * Decides entry into a download for [requestedActivationCode] given the+ * ViewModel's surviving [state] and the code that state belongs to.+ *+ * The download state lives in the activity-scoped ViewModel, not in the+ * route, so re-entering with a new code can otherwise inherit the previous+ * code's [DownloadState.Complete] (shown as if the new profile installed) or+ * its [DownloadState.Confirm] (whose confirm installs the previous code's+ * authenticated session). A state already for the requested code resumes+ * (so recreating the screen shows its result/preview, not a re-download);+ * a state for a different code starts or, for a live preview, cancels first.+ */+internal fun downloadStartDecision(+ state: DownloadState,+ activeActivationCode: String?,+ requestedActivationCode: String,+): DownloadStartDecision {+ val sameCode = activeActivationCode?.trim() == requestedActivationCode.trim()+ return when (state) {+ is DownloadState.Idle -> DownloadStartDecision.START+ is DownloadState.Failed, is DownloadState.Complete ->+ if (sameCode) DownloadStartDecision.RESUME else DownloadStartDecision.START+ is DownloadState.Preparing, is DownloadState.Installing ->+ DownloadStartDecision.BLOCKED+ is DownloadState.Confirm, is DownloadState.AwaitingConfirmationCode ->+ if (sameCode) {+ DownloadStartDecision.RESUME+ } else {+ DownloadStartDecision.RESTART_AFTER_CANCEL+ }+ }+}
euicc/src/main/java/com/vayunmathur/euicc/platform/EuiccViewModel.kt
diff --git a/euicc/src/main/java/com/vayunmathur/euicc/platform/EuiccViewModel.kt b/euicc/src/main/java/com/vayunmathur/euicc/platform/EuiccViewModel.ktindex be4ba4901a..6b58074d29 100644--- a/euicc/src/main/java/com/vayunmathur/euicc/platform/EuiccViewModel.kt+++ b/euicc/src/main/java/com/vayunmathur/euicc/platform/EuiccViewModel.kt@@ -101,6 +101,8 @@ class EuiccViewModel(app: Application) : AndroidViewModel(app) { * the same server session. */ private var pendingTransaction: String? = null+ /** Activation code the current [download] state belongs to (set at start). */+ private var activeActivationCode: String? = null private var pendingActivation: String? = null private var pendingImei: String? = null private var pendingConfirmationCode: String? = null@@ -130,14 +132,40 @@ class EuiccViewModel(app: Application) : AndroidViewModel(app) { * @param confirmationCode already-known confirmation code, or null. */ fun startDownload(activationCode: String, imei: String? = null, confirmationCode: String? = null) {- // A session is already driving the flow in every non-terminal state;- // Failed/Complete/Idle are the only re-entry points (retry / fresh start).- if (download !is DownloadState.Idle &&- download !is DownloadState.Failed &&- download !is DownloadState.Complete- ) {- return+ // Explicit start/retry: terminal states re-enter even for the same+ // code (Failed's Try again), a live preview for the same code is+ // kept, one for another code is cancelled first, and a busy state+ // is never raced.+ when (download) {+ is DownloadState.Preparing, is DownloadState.Installing -> return+ is DownloadState.Confirm, is DownloadState.AwaitingConfirmationCode -> {+ if (activeActivationCode?.trim() == activationCode.trim()) return+ cancelDownload()+ }+ else -> Unit+ }+ beginDownload(activationCode, imei, confirmationCode)+ }++ /**+ * Route-entry start: like [startDownload], except a terminal state for+ * this same code resumes instead of re-downloading — recreating the+ * download screen (rotation, process restore) must show the result it+ * already reached, while a state left by a different code must never be+ * presented as this code's. See [downloadStartDecision].+ */+ fun enterDownload(activationCode: String, imei: String? = null, confirmationCode: String? = null) {+ when (downloadStartDecision(download, activeActivationCode, activationCode)) {+ DownloadStartDecision.RESUME, DownloadStartDecision.BLOCKED -> return+ DownloadStartDecision.RESTART_AFTER_CANCEL -> cancelDownload()+ DownloadStartDecision.START -> Unit }+ beginDownload(activationCode, imei, confirmationCode)+ }++ /** Authenticates [activationCode] and lands on its preview/code state. */+ private fun beginDownload(activationCode: String, imei: String?, confirmationCode: String?) {+ activeActivationCode = activationCode.trim() download = DownloadState.Preparing viewModelScope.launch { val session = withContext(Dispatchers.IO) {@@ -268,6 +296,7 @@ class EuiccViewModel(app: Application) : AndroidViewModel(app) { fun cancelDownload(reason: Int = 0) { val transactionId = pendingTransaction pendingTransaction = null+ activeActivationCode = null pendingConfirmationCode = null if (transactionId != null) { viewModelScope.launch(Dispatchers.IO) {@@ -285,6 +314,7 @@ class EuiccViewModel(app: Application) : AndroidViewModel(app) { fun clearDownload() { // Leaving mid-authentication orphans the server session unless freed. if (pendingTransaction != null) cancelDownload()+ activeActivationCode = null pendingActivation = null pendingImei = null pendingConfirmationCode = null
euicc/src/main/java/com/vayunmathur/euicc/ui/DownloadScreen.kt
diff --git a/euicc/src/main/java/com/vayunmathur/euicc/ui/DownloadScreen.kt b/euicc/src/main/java/com/vayunmathur/euicc/ui/DownloadScreen.ktindex 8263686cc6..9f2cb7d739 100644--- a/euicc/src/main/java/com/vayunmathur/euicc/ui/DownloadScreen.kt+++ b/euicc/src/main/java/com/vayunmathur/euicc/ui/DownloadScreen.kt@@ -28,13 +28,24 @@ fun DownloadScreen( confirmationCode: String?, state: DownloadState, onStart: (String, String?, String?) -> Unit,+ // Route entry (resume-aware); defaults to the explicit start for callers+ // that do not distinguish entry from retry.+ onEnter: (String, String?, String?) -> Unit = onStart, onConfirm: (String?) -> Unit, onSubmitCode: (String) -> Unit, onCancelSession: () -> Unit, onDone: () -> Unit, ) {+ // On entry, ask the ViewModel to drive this route's code ([onEnter]).+ // The surviving state can be a previous code's terminal/preview state+ // (the ViewModel outlives the route), so only the busy states skip the+ // request — the ViewModel decides resume vs cancel-and-restart vs fresh+ // start. This effect does not re-run when this composition's own state+ // changes, so reaching Complete/Failed here does not restart it. LaunchedEffect(activationCode) {- if (state is DownloadState.Idle) onStart(activationCode, imei, confirmationCode)+ if (state !is DownloadState.Preparing && state !is DownloadState.Installing) {+ onEnter(activationCode, imei, confirmationCode)+ } } val busy = state is DownloadState.Preparing || state is DownloadState.Installing
euicc/src/test/java/com/vayunmathur/euicc/FakeSmdpIntegrationTest.kt
diff --git a/euicc/src/test/java/com/vayunmathur/euicc/FakeSmdpIntegrationTest.kt b/euicc/src/test/java/com/vayunmathur/euicc/FakeSmdpIntegrationTest.ktindex ebf93ad722..9febc5dfa2 100644--- a/euicc/src/test/java/com/vayunmathur/euicc/FakeSmdpIntegrationTest.kt+++ b/euicc/src/test/java/com/vayunmathur/euicc/FakeSmdpIntegrationTest.kt@@ -184,6 +184,63 @@ class FakeSmdpIntegrationTest { assertNull(vm.state.error) } + @Test fun `viewmodel entering with new code does not reuse previous completion`() {+ // Regression: the previous code's Complete survived in the ViewModel+ // and was shown for a new route's code without authenticating it.+ val vm = vm()+ awaitVm(vm)+ val first = smdp.activationCode()+ val second = "LPA:1\$http://127.0.0.1:${smdp.port}\$OTHER-2"+ vm.startDownload(first)+ awaitDownload(vm, { it is DownloadState.Confirm || it is DownloadState.Failed })+ assertTrue(vm.download is DownloadState.Confirm, "download=${vm.download}")+ vm.confirmDownload()+ awaitDownload(vm, { it is DownloadState.Complete || it is DownloadState.Failed })+ assertTrue(vm.download is DownloadState.Complete, "download=${vm.download}")+ val authsBefore = smdp.count("initiateAuthentication")+ vm.enterDownload(second)+ awaitDownload(vm, { it is DownloadState.Confirm || it is DownloadState.Failed })+ assertTrue(vm.download is DownloadState.Confirm, "download=${vm.download}")+ assertTrue(+ smdp.count("initiateAuthentication") > authsBefore,+ "second code was not authenticated: counts=${smdp.counts}",+ )+ // Re-entering with that same code resumes its preview, no re-auth.+ val authsAtConfirm = smdp.count("initiateAuthentication")+ vm.enterDownload(second)+ assertTrue(vm.download is DownloadState.Confirm, "download=${vm.download}")+ assertEquals(authsAtConfirm, smdp.count("initiateAuthentication"))+ vm.cancelDownload()+ }++ @Test fun `viewmodel entering with new code cancels previous preview`() {+ // Regression: confirming the previous code's preview would install+ // its session even though the route named a different code.+ val vm = vm()+ awaitVm(vm)+ val second = "LPA:1\$http://127.0.0.1:${smdp.port}\$OTHER-2"+ vm.startDownload(smdp.activationCode())+ awaitDownload(vm, { it is DownloadState.Confirm || it is DownloadState.Failed })+ assertTrue(vm.download is DownloadState.Confirm, "download=${vm.download}")+ val cancelsBefore = smdp.count("cancelSession")+ val authsBefore = smdp.count("initiateAuthentication")+ vm.enterDownload(second)+ awaitDownload(vm, { it is DownloadState.Confirm || it is DownloadState.Failed })+ assertTrue(vm.download is DownloadState.Confirm, "download=${vm.download}")+ assertTrue(+ smdp.count("initiateAuthentication") > authsBefore,+ "second code was not authenticated: counts=${smdp.counts}",+ )+ assertTrue(+ smdp.count("cancelSession") > cancelsBefore,+ "previous session was not freed: counts=${smdp.counts}",+ )+ // The preview now installs the second code's session to completion.+ vm.confirmDownload()+ awaitDownload(vm, { it is DownloadState.Complete || it is DownloadState.Failed })+ assertTrue(vm.download is DownloadState.Complete, "download=${vm.download}")+ }+ @Test fun `viewmodel download cancel from confirm returns to idle`() { val vm = vm() awaitVm(vm)
euicc/src/test/java/com/vayunmathur/euicc/platform/DownloadStateAndRouteTest.kt
diff --git a/euicc/src/test/java/com/vayunmathur/euicc/platform/DownloadStateAndRouteTest.kt b/euicc/src/test/java/com/vayunmathur/euicc/platform/DownloadStateAndRouteTest.ktindex 7f1974b186..1b39968c19 100644--- a/euicc/src/test/java/com/vayunmathur/euicc/platform/DownloadStateAndRouteTest.kt+++ b/euicc/src/test/java/com/vayunmathur/euicc/platform/DownloadStateAndRouteTest.kt@@ -27,6 +27,55 @@ class DownloadStateAndRouteTest { assertNull(DownloadState.Failed(null).message) } + @Test fun `start decision restarts terminal states only for a different code`() {+ // Regression: a previous code's Complete was shown for a new route's+ // code without ever authenticating it (falsely complete). The same+ // code's terminal state resumes (screen recreation shows its result;+ // explicit retry is startDownload's separate path).+ val old = "LPA:1\$old.example.com\$OLD-1"+ val new = "LPA:1\$new.example.com\$NEW-1"+ assertEquals(+ DownloadStartDecision.START,+ downloadStartDecision(DownloadState.Idle, old, old),+ )+ for (state in listOf(DownloadState.Failed("boom"), DownloadState.Complete("Old"))) {+ assertEquals(DownloadStartDecision.START, downloadStartDecision(state, old, new))+ assertEquals(DownloadStartDecision.RESUME, downloadStartDecision(state, old, old))+ assertEquals(DownloadStartDecision.START, downloadStartDecision(state, null, new))+ }+ }++ @Test fun `start decision resumes preview only for its own code`() {+ // Regression: confirming a previous code's preview installs its+ // authenticated session even when the route names a different code.+ val old = "LPA:1\$old.example.com\$OLD-1"+ val new = "LPA:1\$new.example.com\$NEW-1"+ for (state in listOf(+ DownloadState.Confirm("Old", "Plan"),+ DownloadState.AwaitingConfirmationCode("Old"),+ )) {+ assertEquals(DownloadStartDecision.RESUME, downloadStartDecision(state, old, old))+ assertEquals(DownloadStartDecision.RESUME, downloadStartDecision(state, " $old ", old))+ assertEquals(+ DownloadStartDecision.RESTART_AFTER_CANCEL,+ downloadStartDecision(state, old, new),+ )+ assertEquals(+ DownloadStartDecision.RESTART_AFTER_CANCEL,+ downloadStartDecision(state, null, new),+ )+ }+ }++ @Test fun `start decision never races a busy download`() {+ val old = "LPA:1\$old.example.com\$OLD-1"+ val new = "LPA:1\$new.example.com\$NEW-1"+ for (state in listOf(DownloadState.Preparing, DownloadState.Installing(0.5f))) {+ assertEquals(DownloadStartDecision.BLOCKED, downloadStartDecision(state, old, old))+ assertEquals(DownloadStartDecision.BLOCKED, downloadStartDecision(state, old, new))+ }+ }+ @Test fun `route download defaults and equality`() { val r = Route.Download("LPA:1\$a.b\$X") assertEquals("LPA:1\$a.b\$X", r.activationCode)
euicc/src/test/java/com/vayunmathur/euicc/ui/DownloadScreensTest.kt
diff --git a/euicc/src/test/java/com/vayunmathur/euicc/ui/DownloadScreensTest.kt b/euicc/src/test/java/com/vayunmathur/euicc/ui/DownloadScreensTest.ktindex 9b88a9b539..a821429893 100644--- a/euicc/src/test/java/com/vayunmathur/euicc/ui/DownloadScreensTest.kt+++ b/euicc/src/test/java/com/vayunmathur/euicc/ui/DownloadScreensTest.kt@@ -140,13 +140,48 @@ class DownloadScreensTest { onConfirm = {}, onSubmitCode = {}, onCancelSession = {}, onDone = { dones++ }, ) }+ // Entering the destination asks the ViewModel to drive this route's+ // code (it restarts terminal states); Try again asks again.+ assertEquals(listOf("code9"), starts) rule.onNodeWithText("SM-DP+ refused").assertIsDisplayed() rule.onNodeWithText(str(R.string.try_again)).performClick()- assertEquals(listOf("code9"), starts)+ assertEquals(listOf("code9", "code9"), starts) rule.onNodeWithText(uiStr(com.vayunmathur.library.ui.R.string.cancel)).performClick() assertEquals(1, dones) } + @Test fun `download screen entered with stale complete starts the route code`() {+ // Regression: the ViewModel outlives the route, so a previous+ // download's Complete must not be presented as this code's result;+ // entry requests a start for the route's own code instead.+ var starts = mutableListOf<String>()+ rule.setContent {+ DownloadScreen(+ activationCode = "LPA:1\$new.example.com\$NEW-1", imei = null, confirmationCode = null,+ state = DownloadState.Complete("OldCarrier"),+ onStart = { a, _, _ -> starts += a },+ onConfirm = {}, onSubmitCode = {}, onCancelSession = {}, onDone = {},+ )+ }+ assertEquals(listOf("LPA:1\$new.example.com\$NEW-1"), starts)+ }++ @Test fun `download screen entered with stale confirm asks viewmodel to reconcile`() {+ // The ViewModel decides resume (same code) vs cancel-and-restart+ // (different code) — see downloadStartDecision — so entry always+ // forwards the route's code rather than silently keeping the preview.+ var starts = mutableListOf<String>()+ rule.setContent {+ DownloadScreen(+ activationCode = "LPA:1\$new.example.com\$NEW-1", imei = null, confirmationCode = null,+ state = DownloadState.Confirm("OldCarrier", "Old Plan"),+ onStart = { a, _, _ -> starts += a },+ onConfirm = {}, onSubmitCode = {}, onCancelSession = {}, onDone = {},+ )+ }+ assertEquals(listOf("LPA:1\$new.example.com\$NEW-1"), starts)+ }+ @Test fun `failed content without message shows generic only`() { rule.setContent { FailedContent(message = null, onRetry = {}, onCancel = {}) } rule.onNodeWithText(str(R.string.download_fail_dialog_title)).assertExists()