Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/integration-tests-nav3-r8.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: Nav3 Built-in Back Stack Mappers R8 Check

# Verifies behavior of BackStackEntryMapper.SerialName from sentry-android-navigation3 in
# R8-minified builds on an emulator. Runs tests in:
# Verifies behavior of BackStackEntryMapper.SerialName and SerialNameAndProperties from
# sentry-android-navigation3 in R8-minified builds on an emulator. Runs tests in:
#
# - R8 full mode against the compile floor of kotlinx-serialization-core and its latest stable
# release; and
Expand Down Expand Up @@ -34,7 +34,7 @@ on:
- 'gradle/dependency-matrix.versions.toml'
- 'buildSrc/**'
- 'build-logic/**'
# Utilities the mapper uses to handle exceptions.
# Utilities the mappers use to handle exceptions and filter sensitive arguments.
- 'sentry/src/main/java/io/sentry/util/**'
pull_request:
paths: *nav3-r8-paths
Expand Down
29 changes: 21 additions & 8 deletions scripts/test-nav3-built-in-back-stack-mappers-r8.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env bash
# Verifies BackStackEntryMapper.SerialName in R8-minified builds on a booted emulator or device.
# Verifies BackStackEntryMapper.SerialName and SerialNameAndProperties in R8-minified builds on a
# booted emulator or device. Each APK is launched once per mapper.
# See sentry-android-integration-tests/nav3-built-in-back-stack-mappers-r8-check/README.md.
#
# Usage:
Expand All @@ -12,6 +13,7 @@ module="nav3-built-in-back-stack-mappers-r8-check"
moduleDir="sentry-android-integration-tests/nav3-built-in-back-stack-mappers-r8-check"
appId="io.sentry.tests.nav3.r8"
tag="NAV3_R8_CHECK"
mappers=(SerialName SerialNameAndProperties)

copyApk() {
cp "${moduleDir}/build/outputs/apk/release/nav3-built-in-back-stack-mappers-r8-check-release.apk" "$1"
Expand Down Expand Up @@ -47,17 +49,20 @@ build() {
done
}

# Installs and launches APK $1, prints its check's output, and fails unless the check passed.
# Installs and launches APK $1 to check mapper $2, prints its check's output, and fails unless the
# check passed.
runApk() {
local apk="$1"
local mapper="$2"
adb uninstall "$appId" > /dev/null 2>&1 || true
adb install "$apk" > /dev/null || return 1
# Reads only this launch's lines, so the previous APK's RESULT=PASS can't hide a failure. The
# timestamp has second precision, hence the sleep.
local since
sleep 1
since="$(adb shell "date +'%m-%d %H:%M:%S.000'" | tr -d '\r')" || return 1
adb shell am start -W -n "${appId}/.BuiltInBackStackMappersActivity" > /dev/null || return 1
adb shell am start -W -n "${appId}/.BuiltInBackStackMappersActivity" --es mapper "$mapper" \
> /dev/null || return 1

local output=""
for _ in $(seq 1 60); do
Expand All @@ -78,6 +83,12 @@ runApk() {
adb logcat -d -T "$since" -b crash | grep -A 20 "$appId" || true
return 1
fi
# The Activity checks SerialName if the extra is missing, so a launch that passed for the wrong
# mapper would otherwise go unnoticed.
if ! sed -E "s/^.*${tag}: //" <<< "$output" | tr -d '\r' | grep -qx "MAPPER=${mapper}"; then
echo "Expected MAPPER=${mapper}, but the Activity checked another mapper."
return 1
fi
}

run() {
Expand All @@ -94,11 +105,13 @@ run() {
for apk in "${apks[@]}"; do
local name
name="$(basename "$apk" .apk)"
echo "=== $name"
if ! runApk "$apk"; then
echo "FAILED: $name"
failed=1
fi
for mapper in "${mappers[@]}"; do
echo "=== $name ($mapper)"
if ! runApk "$apk" "$mapper"; then
echo "FAILED: $name ($mapper)"
failed=1
fi
done
done

if [[ $failed -ne 0 ]]; then
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# nav3-built-in-back-stack-mappers-r8-check

Checks that `BackStackEntryMapper.SerialName` works after R8 obfuscation:
Checks that `BackStackEntryMapper.SerialName` and `BackStackEntryMapper.SerialNameAndProperties` work after R8 obfuscation:

- The names and `unknown_entry_reason`s that `SentryNavEffect` records in the `Navigation` context match expectations.
- The mapper's kotlinx.serialization compatibility check passes.
- R8 actually obfuscates a representative class.
- The names, `unknown_entry_reason`s, and arguments that `SentryNavEffect` records in the `Navigation` context match expectations.
- The mappers' kotlinx.serialization compatibility checks pass.
- R8 actually obfuscates a representative class, and the fields of a class whose properties are recorded.

The app uses kotlinx.serialization's consumer rules and its own `proguard-rules.pro`; the SDK supplies no keep rules for app serialization
classes.
Expand All @@ -18,7 +18,8 @@ Boot an emulator or device, then run:
```

Versions come from `gradle/dependency-matrix.versions.toml`. Both floor and latestStable run in R8 full mode; floor also runs in compat mode
to cover older named-companion rules. Each APK must report `RESULT=PASS`.
to cover older named-companion rules. Each APK is launched once per mapper (`--es mapper SerialName` or `SerialNameAndProperties`), and
each launch must report `RESULT=PASS`.

CI also runs the module's unit tests against latestStable.

Expand All @@ -31,6 +32,6 @@ upgrade and passing checks. Closing the PR tells Dependabot to ignore that versi
compilers may still work.

**Test failure:** investigate changes to serializer lookup, serial descriptors, or R8 rules. Fix incorrect names or unexpected `/unknown`
results, then merge the fix with the update.
results or arguments, then merge the fix with the update.

Preserve support for the floor in either case; see "Version floor" in the `kotlinx` package README.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Checks BackStackEntryMapper.SerialName in R8-minified release builds. See README.md.
// Checks the built-in BackStackEntryMappers in R8-minified release builds. See README.md.
plugins {
id("com.android.application")
alias(libs.plugins.kotlin.android)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ import io.sentry.compose.navigation3.SentryNavOptions
import kotlinx.coroutines.delay

/**
* Shows every [Expectation]'s key as one back stack to `SentryNavEffect` with
* `BackStackEntryMapper.SerialName`, and reports the names and reasons it records.
* Shows every [Expectation]'s key as one back stack to `SentryNavEffect` with the built-in mapper
* named by the [MAPPER_EXTRA] intent extra (`BackStackEntryMapper.SerialName` by default), and
* reports the names, reasons, and arguments it records.
*
* Only one `SentryNavEffect` records an Activity's back stack, so each mapper needs its own launch.
*
* An exception crashes the Activity, which the runner script reports as a failure.
*/
Expand All @@ -23,6 +26,20 @@ class BuiltInBackStackMappersActivity : ComponentActivity() {
super.onCreate(savedInstanceState)

// Before the mapper's first map() call, which runs its compatibility check.
val mapperName = intent.getStringExtra(MAPPER_EXTRA) ?: SERIAL_NAME
val includeProperties =
when (mapperName) {
SERIAL_NAME -> false
SERIAL_NAME_AND_PROPERTIES -> true
else -> error("Unknown $MAPPER_EXTRA: $mapperName")
}
val mapper =
if (includeProperties) {
BackStackEntryMapper.SerialNameAndProperties
} else {
BackStackEntryMapper.SerialName
}

val logger = RecordingLogger()
val recorder = BackStackRecorder()
initSentry(this, logger, recorder)
Expand All @@ -45,7 +62,7 @@ class BuiltInBackStackMappersActivity : ComponentActivity() {
}

setContent {
SentryNavEffect(backStack, BackStackEntryMapper.SerialName, options)
SentryNavEffect(backStack, mapper, options)

LaunchedEffect(Unit) {
// The back stack reaches the scope once the effect holds the Activity's nav lease.
Expand All @@ -55,18 +72,27 @@ class BuiltInBackStackMappersActivity : ComponentActivity() {
if (recorded != null) break
delay(RECORD_RETRY_MILLIS)
}
report(expectations, recorded?.reversed(), logger, recorder.lastEventSummary)
report(
mapperName,
includeProperties,
expectations,
recorded?.reversed(),
logger,
recorder.lastEventSummary,
)
}
}
}

private fun report(
mapperName: String,
includeProperties: Boolean,
expectations: List<Expectation>,
recorded: List<RecordedEntry>?,
logger: RecordingLogger,
lastEventSummary: String,
) {
val lines = mutableListOf<String>()
val lines = mutableListOf("MAPPER=$mapperName")
var failures = 0

if (recorded?.size != expectations.size) {
Expand All @@ -83,8 +109,11 @@ class BuiltInBackStackMappersActivity : ComponentActivity() {
entry.unknownEntryReason == null
val unnamedAsExpected =
unnamed && entry.unknownEntryReason in expectation.unknownEntryReasons
// SerialName never returns arguments, so no entry it maps may have any.
val passed = !entry.hasArguments && (namedAsExpected || unnamedAsExpected)
// SerialName returns no arguments, and an unnamed entry has none with either mapper.
val expectedArguments =
if (includeProperties && !unnamed) expectation.expectedArguments else null
val argumentsAsExpected = entry.arguments == expectedArguments
val passed = argumentsAsExpected && (namedAsExpected || unnamedAsExpected)
val fellBack = expectation.expected != null && unnamedAsExpected

if (!passed) failures++
Expand All @@ -93,7 +122,11 @@ class BuiltInBackStackMappersActivity : ComponentActivity() {
"${if (passed) "PASS" else "FAIL"} ${expectation.label} " +
"(runtime class ${expectation.key.javaClass.name}): " +
"expected=${expectation.expected?.let { "/$it" } ?: UNKNOWN} actual=${entry.name}" +
(if (entry.hasArguments) " (has arguments)" else "") +
(if (!argumentsAsExpected) {
" arguments=${entry.arguments} (expected $expectedArguments)"
} else {
""
}) +
(if (unnamed) {
" reason=${entry.unknownEntryReason} (expected ${expectation.unknownEntryReasons})"
} else if (entry.unknownEntryReason != null) {
Expand All @@ -114,11 +147,19 @@ class BuiltInBackStackMappersActivity : ComponentActivity() {
lines.forEach { Log.i(TAG, it) }
}

private companion object {
const val TAG = "NAV3_R8_CHECK"
const val UNKNOWN = "/unknown"
const val RECORD_ATTEMPTS = 50
const val RECORD_RETRY_MILLIS = 100L
companion object {
/**
* The intent extra that names the mapper to check: [SERIAL_NAME] (the default) or
* [SERIAL_NAME_AND_PROPERTIES].
*/
const val MAPPER_EXTRA = "mapper"
const val SERIAL_NAME = "SerialName"
const val SERIAL_NAME_AND_PROPERTIES = "SerialNameAndProperties"

private const val TAG = "NAV3_R8_CHECK"
private const val UNKNOWN = "/unknown"
private const val RECORD_ATTEMPTS = 50
private const val RECORD_RETRY_MILLIS = 100L
}
}

Expand All @@ -128,7 +169,8 @@ class Check(val description: String, val passed: Boolean)
/**
* A back stack key and the name `SentryNavEffect` should record for it with
* [BackStackEntryMapper.SerialName], or `null` if it should be unnamed, with one of
* [unknownEntryReasons].
* [unknownEntryReasons]. [expectedArguments] are the arguments it should record for a named key
* with [BackStackEntryMapper.SerialNameAndProperties], or `null` if none.
*
* A key with an [expected] name may also have [unknownEntryReasons], if neither
* kotlinx.serialization nor R8 guarantees whether it can be named. It may then instead be recorded
Expand All @@ -139,6 +181,7 @@ class Expectation(
val key: Any,
val expected: String?,
val unknownEntryReasons: Set<String> = emptySet(),
val expectedArguments: Map<String, Any?>? = null,
) {
init {
require(expected != null || unknownEntryReasons.isNotEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,14 @@ fun compatibilityLogChecks(logger: RecordingLogger): List<Check> {
}

/**
* The prefixes of the messages the mapper logs when its compatibility check fails, or a built-in
* mapper can't be created.
* The prefixes of the messages the mapper logs when one of its compatibility checks fails, or a
* built-in mapper can't be created.
*
* See `KotlinxMapperDiagnostics` and `BuiltInBackStackEntryMappers` in sentry-android-navigation3.
*/
private val COMPATIBILITY_MESSAGE_PREFIXES =
listOf(
"Nav3 couldn't verify kotlinx.serialization compatibility",
"Nav3 SerialNameAndProperties is incompatible",
"Nav3 built-in mapper creation failed",
)
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ const val SERIALIZER_NOT_FOUND = "serializer_not_found"
const val SERIALIZER_ACCESS_THREW = "serializer_access_threw"

/** One back stack entry as SentryNavEffect records it in the `Navigation` context. */
class RecordedEntry(val name: String?, val unknownEntryReason: String?, val hasArguments: Boolean)
class RecordedEntry(val name: String?, val unknownEntryReason: String?, val arguments: Map<*, *>?) {
val hasArguments: Boolean
get() = arguments != null
}

/**
* Captures the back stack SentryNavEffect records, by reading the `Navigation` context of an event
Expand Down Expand Up @@ -55,7 +58,7 @@ class BackStackRecorder : SentryOptions.BeforeSendCallback {
RecordedEntry(
name = fields["entry"] as String?,
unknownEntryReason = fields["unknown_entry_reason"] as String?,
hasArguments = "entry_arguments" in fields,
arguments = fields["entry_arguments"] as Map<*, *>?,
)
}

Expand Down
Loading
Loading