|
1 | 1 | package com.evsuite.hardware.probe |
2 | 2 |
|
3 | 3 | import android.content.Context |
| 4 | +import android.content.pm.PackageManager |
4 | 5 | import android.os.IBinder |
5 | 6 | import com.evsuite.hardware.EVHardware |
6 | 7 | import com.evsuite.hardware.FirmwareInfo |
@@ -61,6 +62,7 @@ object RuntimeInterfaceProbe { |
61 | 62 | * it from a capture that has been up for a while. |
62 | 63 | */ |
63 | 64 | fun connect(context: Context) { |
| 65 | + packages = context.applicationContext.packageManager |
64 | 66 | SaicHub.connect(context) |
65 | 67 | engMode.connect(context) |
66 | 68 | } |
@@ -276,6 +278,80 @@ object RuntimeInterfaceProbe { |
276 | 278 | add(vehicleProperty()) |
277 | 279 | add(vehicleCharging()) |
278 | 280 | add(carDiagnostics()) |
| 281 | + addAll(componentMap()) |
| 282 | + } |
| 283 | + |
| 284 | + /** Kept from [connect], so [componentMap] needs no extra argument from the callers. */ |
| 285 | + @Volatile private var packages: PackageManager? = null |
| 286 | + |
| 287 | + /** |
| 288 | + * Every service the vendor packages declare in their manifests, as PackageManager lists it. |
| 289 | + * |
| 290 | + * The hub maps above can only name what someone already guessed to ask for. This is the |
| 291 | + * other direction: what is installed, which services each package declares, and whether an |
| 292 | + * unprivileged caller may reach them — RI-001, RI-003, RI-004, RI-007 and RI-008 each start |
| 293 | + * by not knowing which component publishes their signal. |
| 294 | + * |
| 295 | + * Manifest metadata only. Nothing is bound, no transaction is sent and no vendor class is |
| 296 | + * loaded, so this cannot reach a write on any interface. Visibility of other packages holds |
| 297 | + * because the target is API 28; a later target needs `QUERY_ALL_PACKAGES`, and without it |
| 298 | + * this would read as a head unit with no vendor packages at all. |
| 299 | + */ |
| 300 | + fun componentMap(): List<InterfaceProbe> { |
| 301 | + val pm = packages ?: return listOf( |
| 302 | + InterfaceProbe(COMPONENT_MAP, null, BindVerdict.ABSENT, note = NOT_CONNECTED), |
| 303 | + ) |
| 304 | + val names = try { |
| 305 | + pm.getInstalledPackages(0).map { it.packageName } |
| 306 | + .filter { name -> COMPONENT_PREFIXES.any { name.startsWith(it) } }.sorted() |
| 307 | + } catch (e: RuntimeException) { |
| 308 | + return listOf( |
| 309 | + InterfaceProbe(COMPONENT_MAP, null, BindVerdict.DENIED, note = e.javaClass.name), |
| 310 | + ) |
| 311 | + } |
| 312 | + return listOf(componentSummary(names)) + names.take(MAX_COMPONENT_PACKAGES).map { name -> |
| 313 | + val services = try { |
| 314 | + pm.getPackageInfo(name, PackageManager.GET_SERVICES).services.orEmpty() |
| 315 | + .map { ServiceRow(it.name, it.exported, it.permission) } |
| 316 | + } catch (e: PackageManager.NameNotFoundException) { |
| 317 | + return@map InterfaceProbe("$COMPONENT_MAP:$name", null, BindVerdict.ABSENT) |
| 318 | + } catch (e: RuntimeException) { |
| 319 | + return@map InterfaceProbe( |
| 320 | + "$COMPONENT_MAP:$name", null, BindVerdict.DENIED, note = e.javaClass.name, |
| 321 | + ) |
| 322 | + } |
| 323 | + componentEntry(name, services) |
| 324 | + } |
| 325 | + } |
| 326 | + |
| 327 | + /** One declared service, as the manifest states it. */ |
| 328 | + internal data class ServiceRow(val name: String, val exported: Boolean, val permission: String?) |
| 329 | + |
| 330 | + /** How many vendor packages there are, and how many of them the entries below list. */ |
| 331 | + internal fun componentSummary(names: List<String>): InterfaceProbe = InterfaceProbe( |
| 332 | + COMPONENT_MAP, null, BindVerdict.ANSWERED, |
| 333 | + mapOf( |
| 334 | + "packages" to names.size.toString(), |
| 335 | + "listed" to minOf(names.size, MAX_COMPONENT_PACKAGES).toString(), |
| 336 | + ), |
| 337 | + COMPONENT_NOTE, |
| 338 | + ) |
| 339 | + |
| 340 | + /** |
| 341 | + * One package's services, keyed by class name, the value kept as the manifest's own words. |
| 342 | + * |
| 343 | + * A package that declares none is still ANSWERED: the manifest was read and said so. |
| 344 | + */ |
| 345 | + internal fun componentEntry(pkg: String, services: List<ServiceRow>): InterfaceProbe { |
| 346 | + val values = LinkedHashMap<String, String?>() |
| 347 | + values["services"] = services.size.toString() |
| 348 | + services.take(MAX_COMPONENT_SERVICES).forEach { |
| 349 | + values[it.name] = "exported=${it.exported} permission=${it.permission}" |
| 350 | + } |
| 351 | + if (services.size > MAX_COMPONENT_SERVICES) { |
| 352 | + values["truncated"] = (services.size - MAX_COMPONENT_SERVICES).toString() |
| 353 | + } |
| 354 | + return InterfaceProbe("$COMPONENT_MAP:$pkg", null, BindVerdict.ANSWERED, values) |
279 | 355 | } |
280 | 356 |
|
281 | 357 | /** |
@@ -365,6 +441,18 @@ object RuntimeInterfaceProbe { |
365 | 441 | */ |
366 | 442 | private val CODE_SWEEP = (0x0..0x12).toList() |
367 | 443 |
|
| 444 | + private const val COMPONENT_MAP = "componentMap" |
| 445 | + private val COMPONENT_PREFIXES = listOf("com.saicmotor.", "com.saicvehicleservice.") |
| 446 | + |
| 447 | + /** A bundle is pasted and read; a few hundred lines of manifest is the useful end of it. */ |
| 448 | + internal const val MAX_COMPONENT_PACKAGES = 64 |
| 449 | + internal const val MAX_COMPONENT_SERVICES = 40 |
| 450 | + |
| 451 | + private const val NOT_CONNECTED = "connect() was not called, so there is no PackageManager" |
| 452 | + private const val COMPONENT_NOTE = |
| 453 | + "PackageManager GET_SERVICES only: nothing bound, called or loaded. exported=false or " + |
| 454 | + "a permission we do not hold means unreachable from EVSuite, not absent." |
| 455 | + |
368 | 456 | /** |
369 | 457 | * What ABSENT means when there is no adapter client to ask. Carried on every swept entry |
370 | 458 | * rather than stated once at the top of the bundle: a later session reads one entry, not the |
|
0 commit comments