|
| 1 | +/* |
| 2 | + * Copyright (c) 2026 Meshtastic LLC |
| 3 | + * |
| 4 | + * This program is free software: you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU General Public License as published by |
| 6 | + * the Free Software Foundation, either version 3 of the License, or |
| 7 | + * (at your option) any later version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License |
| 15 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 | + */ |
| 17 | +package org.meshtastic.app.ui |
| 18 | + |
| 19 | +import org.meshtastic.core.common.util.CommonUri |
| 20 | +import org.meshtastic.core.navigation.DeepLinkRouter |
| 21 | +import org.w3c.dom.Element |
| 22 | +import java.io.File |
| 23 | +import javax.xml.parsers.DocumentBuilderFactory |
| 24 | +import kotlin.test.Test |
| 25 | +import kotlin.test.assertNotNull |
| 26 | +import kotlin.test.assertTrue |
| 27 | +import kotlin.test.fail |
| 28 | + |
| 29 | +/** |
| 30 | + * Guards against drift between [DeepLinkRouter] and the https App Links intent-filter (`android:autoVerify="true"`, |
| 31 | + * host `meshtastic.org`) in `androidApp/src/main/AndroidManifest.xml`. |
| 32 | + * |
| 33 | + * Every top-level path segment routed by [DeepLinkRouter.route] must be declared as an `android:pathPrefix` in that |
| 34 | + * filter — otherwise `https://meshtastic.org/{path}` links open in the browser instead of the app, even though the |
| 35 | + * `meshtastic://` scheme works. The segments come straight from [DeepLinkRouter.topLevelPathSegments], the set |
| 36 | + * [DeepLinkRouter.route] gates its dispatch on, so a new router segment fails here until the manifest declares it. |
| 37 | + */ |
| 38 | +class DeepLinkManifestConsistencyTest { |
| 39 | + |
| 40 | + @Test |
| 41 | + fun `every canonical segment is actually routed by DeepLinkRouter`() { |
| 42 | + DeepLinkRouter.topLevelPathSegments.forEach { segment -> |
| 43 | + assertNotNull( |
| 44 | + DeepLinkRouter.route(CommonUri.parse("https://meshtastic.org/$segment")), |
| 45 | + "DeepLinkRouter.topLevelPathSegments lists /$segment but route() has no branch for it — " + |
| 46 | + "add the branch or remove the segment from the set and the manifest", |
| 47 | + ) |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + fun `app links intent filter declares a pathPrefix for every routed segment`() { |
| 53 | + val prefixes = appLinkPathPrefixes() |
| 54 | + DeepLinkRouter.topLevelPathSegments.forEach { segment -> |
| 55 | + assertTrue( |
| 56 | + "/$segment" in prefixes, |
| 57 | + "AndroidManifest.xml autoVerify filter is missing <data android:pathPrefix=\"/$segment\" /> — " + |
| 58 | + "https://meshtastic.org/$segment will open in the browser instead of the app", |
| 59 | + ) |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + /** Collects the pathPrefix values of the autoVerify (App Links) intent-filter for meshtastic.org. */ |
| 64 | + private fun appLinkPathPrefixes(): Set<String> { |
| 65 | + val manifest = manifestFile() |
| 66 | + val factory = |
| 67 | + DocumentBuilderFactory.newInstance().apply { |
| 68 | + // Harden against XXE even though we only parse our own manifest. |
| 69 | + setFeature("http://apache.org/xml/features/disallow-doctype-decl", true) |
| 70 | + setFeature("http://xml.org/sax/features/external-general-entities", false) |
| 71 | + setFeature("http://xml.org/sax/features/external-parameter-entities", false) |
| 72 | + isXIncludeAware = false |
| 73 | + isExpandEntityReferences = false |
| 74 | + } |
| 75 | + val document = factory.newDocumentBuilder().parse(manifest) |
| 76 | + val filters = document.getElementsByTagName("intent-filter") |
| 77 | + val prefixes = mutableSetOf<String>() |
| 78 | + for (i in 0 until filters.length) { |
| 79 | + val filter = filters.item(i) as Element |
| 80 | + if (filter.getAttribute("android:autoVerify") != "true") continue |
| 81 | + val dataElements = filter.getElementsByTagName("data") |
| 82 | + val attrs = (0 until dataElements.length).map { dataElements.item(it) as Element } |
| 83 | + if (attrs.none { it.getAttribute("android:host") == "meshtastic.org" }) continue |
| 84 | + if (attrs.none { it.getAttribute("android:scheme") == "https" }) continue |
| 85 | + attrs.mapNotNullTo(prefixes) { it.getAttribute("android:pathPrefix").ifEmpty { null } } |
| 86 | + } |
| 87 | + if (prefixes.isEmpty()) fail("No https App Links intent-filter for meshtastic.org found in ${manifest.path}") |
| 88 | + return prefixes |
| 89 | + } |
| 90 | + |
| 91 | + private fun manifestFile(): File = listOf("src/main/AndroidManifest.xml", "androidApp/src/main/AndroidManifest.xml") |
| 92 | + .map(::File) |
| 93 | + .firstOrNull(File::exists) |
| 94 | + ?: fail("Could not locate AndroidManifest.xml from working directory ${File(".").absolutePath}") |
| 95 | +} |
0 commit comments