diff --git a/sentry-android-navigation3/src/main/kotlin/io/sentry/compose/navigation3/internal/backstack/ArgumentConversion.kt b/sentry-android-navigation3/src/main/kotlin/io/sentry/compose/navigation3/internal/backstack/ArgumentConversion.kt index f7cb4f05182..6fa5934613b 100644 --- a/sentry-android-navigation3/src/main/kotlin/io/sentry/compose/navigation3/internal/backstack/ArgumentConversion.kt +++ b/sentry-android-navigation3/src/main/kotlin/io/sentry/compose/navigation3/internal/backstack/ArgumentConversion.kt @@ -18,21 +18,30 @@ import java.util.IdentityHashMap * * With Nav3, host apps manage their own nav back stack, which they implement as a list of * arbitrarily typed entries. In order to be displayed in Sentry, each of those entries has to be - * mapped to a Sentry name (e.g., ProductDetailScreen) and zero or more arguments (e.g., productId) - * via a `BackStackEntryMapper`. + * mapped to a Sentry name (e.g., ProductDetailScreen) and zero or more arguments (e.g., productId). + * There are two ways for host apps to provide us with those mappings: + * + * 1. via a custom `BackStackEntryMapper`; or + * + * 2. via one of our own built-in mappers (viz., `BackStackEntryMapper.SerialName` or + * `BackStackEntryMapper.SerialNameAndProperties`). * * `ArgumentSink` and `ArgumentSource` create the seam that lets us support variable processing on - * the source side (e.g., a plain map supplied by the host app vs. arguments that write themselves) - * but uniform processing on the sink side (argument pruning, protecting against constraint - * violation, and tree materialization). + * the source side (the host app's logic vs built-in logic) but uniform processing on the sink side + * (argument pruning, protecting against constraint violation, and tree materialization). * * Here are the moving parts: * ```text * ArgumentSource - * ┌────────────────────┐ - * │ HostArgumentSource │ - * │ (plain host map) │ - * └─────────┬──────────┘ + * Custom mapper Built-in mappers + * ┌────────────────────┐ ┌───────────────────────┐ + * │ HostArgumentSource │ │ KotlinxArgumentSource │ + * │ (plain host map) │ │ (drives encoder) │ + * └────────┬───────────┘ └────────┬──────────────┘ + * │ │ via + * │ KotlinxPropertiesEncoder + SensitiveKeyFilter + * │ │ + * └──────────────┬──────────────┘ * ▼ * ┌───────────────────────────────┐ * │ ArgumentSink │ diff --git a/sentry-android-navigation3/src/main/kotlin/io/sentry/compose/navigation3/internal/backstack/BackStackConverter.kt b/sentry-android-navigation3/src/main/kotlin/io/sentry/compose/navigation3/internal/backstack/BackStackConverter.kt index 32b068c095b..d531a039d5e 100644 --- a/sentry-android-navigation3/src/main/kotlin/io/sentry/compose/navigation3/internal/backstack/BackStackConverter.kt +++ b/sentry-android-navigation3/src/main/kotlin/io/sentry/compose/navigation3/internal/backstack/BackStackConverter.kt @@ -325,6 +325,7 @@ internal enum class ArgumentDropReason( CHARACTER_LIMIT("max_character_limit_exceeded", exhaustsUpdateBudget = true), DUPLICATE_KEY("duplicate_key"), + ENCODING_FAILED("encoding_failed"), INVALID_STRUCTURE("invalid_structure"), MAPPING_FAILED("mapping_failed"), MAX_COUNT("max_argument_count_exceeded", exhaustsUpdateBudget = true), diff --git a/sentry-android-navigation3/src/main/kotlin/io/sentry/compose/navigation3/internal/backstack/kotlinx/KotlinxArgumentSource.kt b/sentry-android-navigation3/src/main/kotlin/io/sentry/compose/navigation3/internal/backstack/kotlinx/KotlinxArgumentSource.kt new file mode 100644 index 00000000000..8262907f091 --- /dev/null +++ b/sentry-android-navigation3/src/main/kotlin/io/sentry/compose/navigation3/internal/backstack/kotlinx/KotlinxArgumentSource.kt @@ -0,0 +1,157 @@ +package io.sentry.compose.navigation3.internal.backstack.kotlinx + +import io.sentry.compose.navigation3.internal.backstack.ArgumentBudget +import io.sentry.compose.navigation3.internal.backstack.ArgumentDropReason +import io.sentry.compose.navigation3.internal.backstack.ArgumentDropReason.Companion.ARGUMENT_DROP_REASON_KEY +import io.sentry.compose.navigation3.internal.backstack.ArgumentSink +import io.sentry.compose.navigation3.internal.backstack.ArgumentSource +import io.sentry.compose.navigation3.internal.backstack.DropArguments +import io.sentry.compose.navigation3.internal.backstack.HostArgumentSource +import io.sentry.compose.navigation3.internal.backstack.TreeArgumentWriter +import io.sentry.util.ExceptionUtils +import kotlinx.serialization.KSerializer + +/** + * The [ArgumentSource] for our built-in `BackStackEntryMapper`s. + * + * It keeps a reference to the [backStackEntry] whose properties it reads, as well as the + * [serializer] needed to encode them, for as long as the map itself is reachable. (The SDK doesn't + * retain these maps past a back stack update, but a host app that keeps a mapper's result also + * keeps its entry.) + * + * Encoding is performed on demand and cached in order to: + * + * - guarantee a single, consistent snapshot for all consumers; and + * - avoid doing work for arguments that will be dropped or never read. + * + * If the arguments are dropped, reading them as a map returns only the drop reason. + * + * In general, all failures are reported to [onFailure]. The sole exception is an argument + * constraint violated while writing to the `ArgumentSanitizer`'s sink. The sanitizer reports that + * instead, as those limits can depend on what fraction of the [ArgumentBudget] the update's other + * entries spent. + */ +internal class KotlinxArgumentSource( + private val backStackEntry: Any, + private val serializer: KSerializer, + private val onFailure: (Throwable) -> Unit, +) : AbstractMap(), ArgumentSource { + + private val copy: Lazy = + // PUBLICATION so that no lock is held while encoding runs the host app's serializers, which + // could otherwise deadlock on a lock held by the app. Readers that race on the first read may + // each encode, but all of them see the copy published first. + lazy(LazyThreadSafetyMode.PUBLICATION) { encodeCopy() } + + override val entries: Set> + get() = copy.value.arguments.entries + + /** + * Writes the entry's arguments to [sink], encoding its properties if needed. Uses the previously + * cached copy instead if it was encoded successfully. + * + * Throws [DropArguments] if the arguments can't be written, either with the reason for the + * constraint they violated, or with [ArgumentDropReason.ENCODING_FAILED] if encoding failed. + */ + override fun writeTo(sink: ArgumentSink) { + val cached = if (copy.isInitialized()) copy.value else null + if (cached != null && !cached.isDropped) { + cached.writeTo(sink) + } else { + // A failed copy used a separate budget. Encode again so values written before the failure + // consume the update's shared budget; rethrowing the cached failure would leave extra room + // for older entries' arguments. + encode(sink) + } + } + + @Suppress("ThrowsCount") + private fun encode(sink: ArgumentSink) { + try { + KotlinxPropertiesEncoder.encode(serializer, backStackEntry, sink) + } catch (e: DropArguments) { + // Prefer the sink's first failure because it determines whether older entries keep their + // arguments. + throw sink.failure ?: e + } catch (e: LinkageError) { + // Preserve the entry's name and drop its properties. The creation-time probe checks encoder + // compatibility but can't exercise every path. This LinkageError may be specific to the + // entry's serializers, so we keep the failure local to this type. + sink.failure?.let { throw it } + onFailure(e) + throw DropArguments(ArgumentDropReason.ENCODING_FAILED, warning = null) + } catch (@Suppress("TooGenericExceptionCaught") t: Throwable) { + // App serializers and map-key toString() calls may throw anything. Catch broadly to log once + // per type. + ExceptionUtils.rethrowIfFatal(t) + + // A serializer may replace a constraint violation with its own exception. Prefer the sink's + // recorded failure because it determines whether older entries keep their arguments. + sink.failure?.let { throw it } + onFailure(t) + throw DropArguments(ArgumentDropReason.ENCODING_FAILED, warning = null) + } + } + + private fun encodeCopy(): EncodedCopy { + val sink = TreeArgumentWriter(ArgumentBudget()) + + return try { + encode(sink) + EncodedCopy(sink.argumentsOrThrow(), isDropped = false) + } catch (e: DropArguments) { + // encode already reported an encoding failure. + if (e.reason != ArgumentDropReason.ENCODING_FAILED) { + onFailure(e) + } + EncodedCopy(mapOf(ARGUMENT_DROP_REASON_KEY to e.reason.serializedValue), isDropped = true) + } + } + + /** Cached arguments encoded from a single back stack entry's properties. */ + private class EncodedCopy( + /** + * The encoded arguments, or, if they were dropped, only the drop reason under + * [ARGUMENT_DROP_REASON_KEY]. + * + * A host app mapper that copies or filters a dropped entry's arguments passes the reason on as + * an ordinary argument. Sentry then records it as it would for this mapper's own output, rather + * than recording no arguments and no reason. + */ + val arguments: Map, + /** + * Whether the back stack entry's arguments were dropped, because of an encoding failure or a + * violated argument constraint. + */ + val isDropped: Boolean, + ) : ArgumentSource { + + /** Writes the encoded arguments to [sink]. Only for a copy that wasn't [dropped][isDropped]. */ + override fun writeTo(sink: ArgumentSink) { + check(!isDropped) { "A dropped copy is encoded again instead of written." } + // The copy holds only plain maps, lists, and values the sink supports. + HostArgumentSource(arguments, onUnsupportedValue = {}).writeTo(sink) + } + } +} + +/** + * The arguments of a back stack entry whose properties can't be encoded because the app's + * kotlinx.serialization version doesn't support the properties encoder. + * + * Handles encoding failures like [KotlinxArgumentSource]: map reads return `{"dropped_by_sentry": + * "encoding_failed"}`, and writes drop the entry's arguments with + * [ArgumentDropReason.ENCODING_FAILED]. + */ +internal object KotlinxUnencodableArguments : AbstractMap(), ArgumentSource { + + private val dropReason = + mapOf(ARGUMENT_DROP_REASON_KEY to ArgumentDropReason.ENCODING_FAILED.serializedValue) + + override val entries: Set> + get() = dropReason.entries + + override fun writeTo(sink: ArgumentSink) { + throw DropArguments(ArgumentDropReason.ENCODING_FAILED, warning = null) + } +} diff --git a/sentry-android-navigation3/src/test/kotlin/io/sentry/compose/navigation3/internal/backstack/kotlinx/KotlinxArgumentSourceTest.kt b/sentry-android-navigation3/src/test/kotlin/io/sentry/compose/navigation3/internal/backstack/kotlinx/KotlinxArgumentSourceTest.kt new file mode 100644 index 00000000000..a04445ab345 --- /dev/null +++ b/sentry-android-navigation3/src/test/kotlin/io/sentry/compose/navigation3/internal/backstack/kotlinx/KotlinxArgumentSourceTest.kt @@ -0,0 +1,721 @@ +package io.sentry.compose.navigation3.internal.backstack.kotlinx + +import com.google.common.truth.Truth.assertThat +import io.sentry.compose.navigation3.internal.backstack.ArgumentBudget +import io.sentry.compose.navigation3.internal.backstack.ArgumentConstraints.MAX_ARGUMENT_CHARACTERS +import io.sentry.compose.navigation3.internal.backstack.ArgumentDropReason +import io.sentry.compose.navigation3.internal.backstack.DropArguments +import io.sentry.compose.navigation3.internal.backstack.TreeArgumentWriter +import io.sentry.compose.navigation3.internal.backstack.budgetWithCharactersRemaining +import io.sentry.compose.navigation3.internal.backstack.budgetWithValuesRemaining +import java.util.concurrent.CountDownLatch +import java.util.concurrent.TimeUnit +import java.util.concurrent.atomic.AtomicInteger +import java.util.concurrent.locks.ReentrantLock +import kotlin.concurrent.thread +import kotlin.test.assertFailsWith +import kotlinx.serialization.KSerializer +import kotlinx.serialization.Serializable +import kotlinx.serialization.SerializationException +import kotlinx.serialization.descriptors.PrimitiveKind +import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor +import kotlinx.serialization.descriptors.SerialDescriptor +import kotlinx.serialization.encoding.Decoder +import kotlinx.serialization.encoding.Encoder +import org.junit.Test + +class KotlinxArgumentSourceTest { + + @Serializable data class Values(val values: List) + + @Serializable data class MutableDetail(var id: String) + + /** + * Delegates to [delegate], but wraps a [DropArguments] it throws in an exception of its own, as a + * serializer that catches and rethrows everything might. + */ + private class WrappingSerializer( + private val delegate: KSerializer, + private val wrap: (DropArguments) -> Throwable, + ) : KSerializer by delegate { + override fun serialize(encoder: Encoder, value: T) { + try { + delegate.serialize(encoder, value) + } catch (e: DropArguments) { + throw wrap(e) + } + } + } + + /** + * Writes [text], recording a [DropArguments] that throws in [caught], then encodes the link with + * [LinkSerializer], so a chain long enough breaks the encoder's nesting limit after the sink's. + */ + private class NestsAfterCaughtLimitSerializer(private val text: String) : KSerializer { + var caught: DropArguments? = null + + override val descriptor: SerialDescriptor = + PrimitiveSerialDescriptor("test.NestsAfterCaughtLimit", PrimitiveKind.STRING) + + override fun serialize(encoder: Encoder, value: Link) { + try { + encoder.encodeString(text) + } catch (e: DropArguments) { + caught = e + } + encoder.encodeSerializableValue(LinkSerializer, value) + } + + override fun deserialize(decoder: Decoder): Link = throw UnsupportedOperationException() + } + + /** + * Encodes a [Detail] whose id is the number of this serializer's call, so that each encoding of + * an entry differs. Runs [beforeEncoding] with that number first. + */ + private class NumberingDetailSerializer(private val beforeEncoding: (call: Int) -> Unit) : + KSerializer { + private val calls = AtomicInteger() + + override val descriptor: SerialDescriptor = Detail.serializer().descriptor + + override fun serialize(encoder: Encoder, value: Detail) { + val call = calls.incrementAndGet() + beforeEncoding(call) + Detail.serializer().serialize(encoder, Detail("encoding-$call")) + } + + override fun deserialize(decoder: Decoder): Detail = throw UnsupportedOperationException() + } + + /** Delegates to [delegate], then throws [error], as a serializer that fails late might. */ + private class ThrowsAfterEncodingSerializer(private val delegate: KSerializer) : + KSerializer by delegate { + val error = IllegalStateException("thrown after encoding") + + override fun serialize(encoder: Encoder, value: T) { + delegate.serialize(encoder, value) + throw error + } + } + + private val failures = mutableListOf() + + private val detailSerializer = RecordingSerializer(Detail.serializer()) + private val mutableDetailSerializer = RecordingSerializer(MutableDetail.serializer()) + + /** A value over the argument character limit. */ + private val overLimitId = "x".repeat(MAX_ARGUMENT_CHARACTERS + 1) + + private val encodingFailed = droppedBy(ArgumentDropReason.ENCODING_FAILED) + + /** A link chain whose serializers nest one call more than the encoder allows. */ + private val tooDeeplyLinked = linked(length = KotlinxPropertiesEncoder.MAX_SERIALIZER_NESTING + 1) + + // Reading as a map + + @Test + fun `reads as the entry's encoded properties`() { + val source = argumentSourceOf(detailSerializer, Detail("1")) + + assertThat(source.entries.associate { it.key to it.value }).isEqualTo(mapOf("id" to "1")) + } + + @Test + fun `doesn't encode the entry until its arguments are read`() { + argumentSourceOf(detailSerializer, Detail("1")) + + assertThat(detailSerializer.serialized).isEmpty() + } + + @Test + fun `encodes the entry when its arguments are first read through hashCode()`() { + val source = argumentSourceOf(detailSerializer, Detail("1")) + + val hashCode = source.hashCode() + + assertThat(hashCode).isEqualTo(mapOf("id" to "1").hashCode()) + assertThat(detailSerializer.serialized).containsExactly(Detail("1")) + } + + @Test + fun `encodes the entry once across repeated reads`() { + val source = argumentSourceOf(detailSerializer, Detail("1")) + + source.entries + source.size + source["id"] + source.containsKey("id") + source.equals(mapOf("id" to "1")) + + assertThat(detailSerializer.serialized).containsExactly(Detail("1")) + } + + @Test + fun `equals a plain map with the same entries`() { + val source = argumentSourceOf(detailSerializer, Detail("1")) + + assertThat(source).isEqualTo(mapOf("id" to "1")) + assertThat(mapOf("id" to "1")).isEqualTo(source) + assertThat(source.hashCode()).isEqualTo(mapOf("id" to "1").hashCode()) + } + + @Test + fun `doesn't equal a plain map with other entries`() { + val source = argumentSourceOf(detailSerializer, Detail("1")) + + assertThat(source).isNotEqualTo(mapOf("id" to "2")) + assertThat(mapOf("id" to "2")).isNotEqualTo(source) + } + + @Test + fun `reads each instance's arguments with a budget of its own`() { + // Each entry has 151 values, so two entries wouldn't fit in one budget. + val first = argumentSourceOf(Values.serializer(), Values(List(150) { it })) + val second = argumentSourceOf(Values.serializer(), Values(List(150) { it })) + + assertThat(first).isEqualTo(mapOf("values" to List(150) { it })) + assertThat(second).isEqualTo(mapOf("values" to List(150) { it })) + } + + // Reading from several threads + + @Test + fun `doesn't block a read on another thread's encoding that waits for a lock the reader holds`() { + // Models a deadlock: the reader holds a lock of the app's, and the serializer encoding on + // another thread needs it. + val appLock = ReentrantLock() + val encodingStarted = CountDownLatch(1) + var otherThreadGotLock = false + val serializer = NumberingDetailSerializer { call -> + if (call == 1) { + encodingStarted.countDown() + otherThreadGotLock = appLock.tryLock(TIMEOUT_SECONDS, TimeUnit.SECONDS) + if (otherThreadGotLock) appLock.unlock() + } + } + val source = argumentSourceOf(serializer, Detail("1")) + appLock.lock() + val otherRead = readOnAnotherThread(source) + assertThat(encodingStarted.await(TIMEOUT_SECONDS, TimeUnit.SECONDS)).isTrue() + + val read = + try { + source.toMap() + } finally { + appLock.unlock() + } + + assertThat(otherRead.await()).isEqualTo(read) + assertThat(otherThreadGotLock).isTrue() + } + + @Test + fun `reads the copy published first on every thread when reads race on the first read`() { + val encodingStarted = CountDownLatch(1) + val firstReadDone = CountDownLatch(1) + var otherThreadSawFirstRead = false + val serializer = NumberingDetailSerializer { call -> + // The other thread's encoding, the first call, waits until this thread's read returns. + if (call == 1) { + encodingStarted.countDown() + otherThreadSawFirstRead = firstReadDone.await(TIMEOUT_SECONDS, TimeUnit.SECONDS) + } + } + val source = argumentSourceOf(serializer, Detail("1")) + val otherRead = readOnAnotherThread(source) + assertThat(encodingStarted.await(TIMEOUT_SECONDS, TimeUnit.SECONDS)).isTrue() + + val read = source.toMap() + firstReadDone.countDown() + + assertThat(read).isEqualTo(mapOf("id" to "encoding-2")) + assertThat(otherRead.await()).isEqualTo(read) + assertThat(source.toMap()).isEqualTo(read) + assertThat(otherThreadSawFirstRead).isTrue() + } + + // Reading failures + + @Test + fun `reads as only the broken limit when the entry breaks an argument limit`() { + val source = argumentSourceOf(detailSerializer, Detail(overLimitId)) + + assertThat(source).isEqualTo(droppedBy(ArgumentDropReason.CHARACTER_LIMIT)) + } + + @Test + fun `reports the broken limit to onFailure when read`() { + val source = argumentSourceOf(detailSerializer, Detail(overLimitId)) + + source.size + + assertThat(failures).hasSize(1) + assertThat((failures.single() as DropArguments).reason) + .isEqualTo(ArgumentDropReason.CHARACTER_LIMIT) + } + + @Test + fun `reads as only the encoding failure and reports a serializer's exception once across reads`() { + val error = IllegalStateException("boom") + detailSerializer.error = error + val source = argumentSourceOf(detailSerializer, Detail("1")) + + val reads = List(3) { source.toMap() } + + assertThat(reads).containsExactly(encodingFailed, encodingFailed, encodingFailed) + assertThat(failures).containsExactly(error) + } + + @Test + fun `reads as only the encoding failure and reports a serializer's LinkageError once across reads`() { + val error = NoClassDefFoundError("missing") + detailSerializer.error = error + val source = argumentSourceOf(detailSerializer, Detail("1")) + + val reads = List(3) { source.toMap() } + + assertThat(reads).containsExactly(encodingFailed, encodingFailed, encodingFailed) + assertThat(failures).containsExactly(error) + } + + @Test + fun `reads as only the broken limit and reports it when a serializer replaced it with its own exception`() { + val serializer = WrappingSerializer(Detail.serializer()) { SerializationException("replaced") } + val source = argumentSourceOf(serializer, Detail(overLimitId)) + + val read = source.toMap() + + assertThat(read).isEqualTo(droppedBy(ArgumentDropReason.CHARACTER_LIMIT)) + assertThat(failures).hasSize(1) + assertThat((failures.single() as DropArguments).reason) + .isEqualTo(ArgumentDropReason.CHARACTER_LIMIT) + } + + @Test + fun `reads as only the nesting limit and reports it when a serializer replaced it with its own exception`() { + val serializer = WrappingSerializer(LinkSerializer) { SerializationException("replaced") } + val source = argumentSourceOf(serializer, tooDeeplyLinked) + + val read = source.toMap() + + assertThat(read).isEqualTo(droppedBy(ArgumentDropReason.INVALID_STRUCTURE)) + assertThat(failures).hasSize(1) + assertThat((failures.single() as DropArguments).reason) + .isEqualTo(ArgumentDropReason.INVALID_STRUCTURE) + } + + @Test + fun `reads as only the sink's limit and reports it when the encoder's nesting limit follows it`() { + val serializer = NestsAfterCaughtLimitSerializer(overLimitId) + val source = + argumentSourceOf(serializer, linked(length = KotlinxPropertiesEncoder.MAX_SERIALIZER_NESTING)) + + val read = source.toMap() + + assertThat(read).isEqualTo(droppedBy(ArgumentDropReason.CHARACTER_LIMIT)) + assertThat(failures).hasSize(1) + assertThat((failures.single() as DropArguments).reason) + .isEqualTo(ArgumentDropReason.CHARACTER_LIMIT) + } + + @Test + fun `propagates fatal errors when read, without reporting them`() { + val error = OutOfMemoryError() + detailSerializer.error = error + val source = argumentSourceOf(detailSerializer, Detail("1")) + + val thrown = assertFailsWith { source.size } + + assertThat(thrown).isSameInstanceAs(error) + assertThat(failures).isEmpty() + } + + // writeTo() + + @Test + fun `writeTo() - writes the entry's properties to the sink`() { + val sink = TreeArgumentWriter(ArgumentBudget()) + + argumentSourceOf(detailSerializer, Detail("1")).writeTo(sink) + + assertThat(sink.argumentsOrThrow()).isEqualTo(mapOf("id" to "1")) + } + + @Test + fun `writeTo() - encodes the entry each time it's written`() { + val source = argumentSourceOf(detailSerializer, Detail("1")) + + source.writeTo(TreeArgumentWriter(ArgumentBudget())) + source.writeTo(TreeArgumentWriter(ArgumentBudget())) + + assertThat(detailSerializer.serialized).containsExactly(Detail("1"), Detail("1")) + } + + @Test + fun `writeTo() - spends the sink's budget`() { + // Each entry has 151 values, so two entries don't fit in one budget. + val budget = ArgumentBudget() + argumentSourceOf(Values.serializer(), Values(List(150) { it })) + .writeTo(TreeArgumentWriter(budget)) + val second = argumentSourceOf(Values.serializer(), Values(List(150) { it })) + + val thrown = assertFailsWith { second.writeTo(TreeArgumentWriter(budget)) } + + assertThat(thrown.reason).isEqualTo(ArgumentDropReason.MAX_COUNT) + } + + @Test + fun `writeTo() - propagates a broken limit unchanged, without reporting it`() { + val sink = TreeArgumentWriter(budgetWithValuesRemaining(0)) + + val thrown = + assertFailsWith { + argumentSourceOf(detailSerializer, Detail("1")).writeTo(sink) + } + + assertThat(thrown).isSameInstanceAs(assertFailsWith { sink.argumentsOrThrow() }) + assertThat(thrown.reason).isEqualTo(ArgumentDropReason.MAX_COUNT) + assertThat(failures).isEmpty() + } + + @Test + fun `writeTo() - reports a serializer's exception and drops the arguments as an unlogged encoding failure`() { + val error = IllegalStateException("boom") + detailSerializer.error = error + + val thrown = + assertFailsWith { + argumentSourceOf(detailSerializer, Detail("1")) + .writeTo(TreeArgumentWriter(ArgumentBudget())) + } + + assertThat(thrown.reason).isEqualTo(ArgumentDropReason.ENCODING_FAILED) + assertThat(thrown.warning).isNull() + assertThat(failures).containsExactly(error) + } + + @Test + fun `writeTo() - reports a serializer's LinkageError and drops the arguments as an unlogged encoding failure`() { + val error = AbstractMethodError() + detailSerializer.error = error + + val thrown = + assertFailsWith { + argumentSourceOf(detailSerializer, Detail("1")) + .writeTo(TreeArgumentWriter(ArgumentBudget())) + } + + assertThat(thrown.reason).isEqualTo(ArgumentDropReason.ENCODING_FAILED) + assertThat(thrown.warning).isNull() + assertThat(failures).containsExactly(error) + } + + @Test + fun `writeTo() - propagates fatal errors without reporting them`() { + val error = OutOfMemoryError() + detailSerializer.error = error + + val thrown = + assertFailsWith { + argumentSourceOf(detailSerializer, Detail("1")) + .writeTo(TreeArgumentWriter(ArgumentBudget())) + } + + assertThat(thrown).isSameInstanceAs(error) + assertThat(failures).isEmpty() + } + + @Test + fun `writeTo() - rethrows a broken limit that a serializer wrapped, without reporting it`() { + val serializer = + WrappingSerializer(Detail.serializer()) { SerializationException("wrapped", it) } + val sink = TreeArgumentWriter(budgetWithValuesRemaining(0)) + + val thrown = + assertFailsWith { + argumentSourceOf(serializer, Detail("1")).writeTo(sink) + } + + assertThat(thrown).isSameInstanceAs(sink.failure) + assertThat(thrown.reason).isEqualTo(ArgumentDropReason.MAX_COUNT) + assertThat(failures).isEmpty() + } + + @Test + fun `writeTo() - rethrows the sink's limit that a serializer replaced with its own exception, unreported`() { + val serializer = WrappingSerializer(Detail.serializer()) { SerializationException("replaced") } + val sink = TreeArgumentWriter(ArgumentBudget()) + + val thrown = + assertFailsWith { + argumentSourceOf(serializer, Detail(overLimitId)).writeTo(sink) + } + + assertThat(thrown).isSameInstanceAs(sink.failure) + assertThat(thrown.reason).isEqualTo(ArgumentDropReason.CHARACTER_LIMIT) + assertThat(failures).isEmpty() + } + + @Test + fun `writeTo() - rethrows the sink's limit that a serializer replaced with a LinkageError, unreported`() { + val serializer = WrappingSerializer(Detail.serializer()) { NoClassDefFoundError("replaced") } + val sink = TreeArgumentWriter(ArgumentBudget()) + + val thrown = + assertFailsWith { + argumentSourceOf(serializer, Detail(overLimitId)).writeTo(sink) + } + + assertThat(thrown).isSameInstanceAs(sink.failure) + assertThat(thrown.reason).isEqualTo(ArgumentDropReason.CHARACTER_LIMIT) + assertThat(failures).isEmpty() + } + + @Test + fun `writeTo() - rethrows the nesting limit that a serializer replaced with its own exception, unreported`() { + val serializer = WrappingSerializer(LinkSerializer) { SerializationException("replaced") } + val sink = TreeArgumentWriter(ArgumentBudget()) + + val thrown = + assertFailsWith { + argumentSourceOf(serializer, tooDeeplyLinked).writeTo(sink) + } + + assertThat(thrown).isSameInstanceAs(sink.failure) + assertThat(thrown.reason).isEqualTo(ArgumentDropReason.INVALID_STRUCTURE) + assertThat(failures).isEmpty() + } + + @Test + fun `writeTo() - rethrows the nesting limit that a serializer replaced with a LinkageError, unreported`() { + val serializer = WrappingSerializer(LinkSerializer) { NoClassDefFoundError("replaced") } + val sink = TreeArgumentWriter(ArgumentBudget()) + + val thrown = + assertFailsWith { + argumentSourceOf(serializer, tooDeeplyLinked).writeTo(sink) + } + + assertThat(thrown).isSameInstanceAs(sink.failure) + assertThat(thrown.reason).isEqualTo(ArgumentDropReason.INVALID_STRUCTURE) + assertThat(failures).isEmpty() + } + + @Test + fun `writeTo() - rethrows the sink's limit when the encoder's nesting limit follows it, unreported`() { + val serializer = NestsAfterCaughtLimitSerializer(overLimitId) + val sink = TreeArgumentWriter(ArgumentBudget()) + + val thrown = + assertFailsWith { + argumentSourceOf( + serializer, + linked(length = KotlinxPropertiesEncoder.MAX_SERIALIZER_NESTING), + ) + .writeTo(sink) + } + + assertThat(thrown).isSameInstanceAs(sink.failure) + assertThat(thrown.reason).isEqualTo(ArgumentDropReason.CHARACTER_LIMIT) + assertThat(failures).isEmpty() + } + + // writeTo() after the arguments are read + + @Test + fun `writeTo() - writes the copy read as a map rather than the entry's current properties`() { + val entry = MutableDetail("1") + val source = argumentSourceOf(mutableDetailSerializer, entry) + source.size + entry.id = "2" + val sink = TreeArgumentWriter(ArgumentBudget()) + + source.writeTo(sink) + + assertThat(sink.argumentsOrThrow()).isEqualTo(mapOf("id" to "1")) + } + + @Test + fun `writeTo() - doesn't encode the entry again after its arguments are read`() { + val source = argumentSourceOf(mutableDetailSerializer, MutableDetail("1")) + source.size + + source.writeTo(TreeArgumentWriter(ArgumentBudget())) + source.writeTo(TreeArgumentWriter(ArgumentBudget())) + + assertThat(mutableDetailSerializer.serialized).hasSize(1) + } + + @Test + fun `writeTo() - encodes the entry's current properties when its arguments weren't read`() { + val entry = MutableDetail("1") + val source = argumentSourceOf(mutableDetailSerializer, entry) + entry.id = "2" + val sink = TreeArgumentWriter(ArgumentBudget()) + + source.writeTo(sink) + + assertThat(sink.argumentsOrThrow()).isEqualTo(mapOf("id" to "2")) + } + + @Test + fun `reads a copy encoded separately from what writeTo() wrote`() { + val entry = MutableDetail("1") + val source = argumentSourceOf(mutableDetailSerializer, entry) + source.writeTo(TreeArgumentWriter(ArgumentBudget())) + entry.id = "2" + + val read = source.toMap() + + assertThat(read).isEqualTo(mapOf("id" to "2")) + assertThat(mutableDetailSerializer.serialized).hasSize(2) + } + + @Test + fun `writeTo() - spends the sink's value budget when writing the copy read`() { + // The entry has 151 values, which fit in its own budget but not in the 100 the sink has left. + val source = argumentSourceOf(Values.serializer(), Values(List(150) { it })) + source.size + val sink = TreeArgumentWriter(budgetWithValuesRemaining(100)) + + val thrown = assertFailsWith { source.writeTo(sink) } + + assertThat(thrown.reason).isEqualTo(ArgumentDropReason.MAX_COUNT) + assertThat(thrown.warning).isEqualTo(DropArguments.MAX_COUNT_WARNING) + assertThat(failures).isEmpty() + } + + @Test + fun `writeTo() - spends the sink's character budget when writing the copy read`() { + val source = argumentSourceOf(detailSerializer, Detail("123")) + source.size + val sink = TreeArgumentWriter(budgetWithCharactersRemaining(4)) + + val thrown = assertFailsWith { source.writeTo(sink) } + + assertThat(thrown.reason).isEqualTo(ArgumentDropReason.CHARACTER_LIMIT) + assertThat(thrown.warning).isEqualTo(DropArguments.MAX_CHARACTER_WARNING) + assertThat(failures).isEmpty() + } + + @Test + fun `writeTo() - encodes again and drops the arguments for the sink's limit when the copy read broke a limit`() { + val source = argumentSourceOf(detailSerializer, Detail(overLimitId)) + source.size + val sink = TreeArgumentWriter(ArgumentBudget()) + + val thrown = assertFailsWith { source.writeTo(sink) } + + assertThat(thrown).isSameInstanceAs(sink.failure) + assertThat(thrown.reason).isEqualTo(ArgumentDropReason.CHARACTER_LIMIT) + assertThat(thrown.warning).isEqualTo(DropArguments.MAX_CHARACTER_WARNING) + assertThat(detailSerializer.serialized).hasSize(2) + // Only the read reports the limit; the sink's owner reports the one writeTo() broke. + assertThat(failures).hasSize(1) + } + + @Test + fun `writeTo() - encodes the entry again and reports a serializer's exception again when the copy read failed`() { + val error = IllegalStateException("boom") + detailSerializer.error = error + val source = argumentSourceOf(detailSerializer, Detail("1")) + source.size + + val thrown = + assertFailsWith { + source.writeTo(TreeArgumentWriter(ArgumentBudget())) + } + + assertThat(thrown.reason).isEqualTo(ArgumentDropReason.ENCODING_FAILED) + assertThat(thrown.warning).isNull() + assertThat(detailSerializer.serialized).hasSize(2) + // The mapper's onFailure logs once per type, so reporting each encoding adds no warnings. + assertThat(failures).containsExactly(error, error) + } + + @Test + fun `writeTo() - drops the arguments for the sink's limit that encoding again breaks when the copy read failed`() { + // The entry's 151 values fit in the copy's own budget, so the copy failed only once its + // serializer threw. Encoding again overflows the 100 values the sink has left first. + val serializer = ThrowsAfterEncodingSerializer(Values.serializer()) + val source = argumentSourceOf(serializer, Values(List(150) { it })) + source.size + val sink = TreeArgumentWriter(budgetWithValuesRemaining(100)) + + val thrown = assertFailsWith { source.writeTo(sink) } + + assertThat(thrown).isSameInstanceAs(sink.failure) + assertThat(thrown.reason).isEqualTo(ArgumentDropReason.MAX_COUNT) + assertThat(failures).containsExactly(serializer.error) + } + + @Test + fun `writeTo() - writes the entry's properties when encoding again succeeds after the copy read failed`() { + detailSerializer.error = IllegalStateException("boom") + detailSerializer.failsFor = { detailSerializer.serialized.size == 1 } + val source = argumentSourceOf(detailSerializer, Detail("1")) + source.size + val sink = TreeArgumentWriter(ArgumentBudget()) + + source.writeTo(sink) + + assertThat(sink.argumentsOrThrow()).isEqualTo(mapOf("id" to "1")) + } + + @Test + fun `reads as only the drop reason of the copy read after writeTo() encodes the entry again`() { + detailSerializer.error = IllegalStateException("boom") + detailSerializer.failsFor = { detailSerializer.serialized.size == 1 } + val source = argumentSourceOf(detailSerializer, Detail("1")) + source.size + source.writeTo(TreeArgumentWriter(ArgumentBudget())) + + val read = source.toMap() + + assertThat(read).isEqualTo(encodingFailed) + } + + /** + * Reads [source] as a map on a new thread. The returned read's [OtherThreadRead.await] fails + * rather than hangs if the thread doesn't finish in time. + */ + private fun readOnAnotherThread(source: Map): OtherThreadRead { + val read = OtherThreadRead() + read.thread = + thread(name = "other-reader") { + try { + read.result = source.toMap() + } catch (@Suppress("TooGenericExceptionCaught") t: Throwable) { + read.error = t + } + } + return read + } + + /** A read of the arguments on another thread, whose result [await] returns. */ + private class OtherThreadRead { + lateinit var thread: Thread + @Volatile var result: Map? = null + @Volatile var error: Throwable? = null + + fun await(): Map? { + thread.join(TimeUnit.SECONDS.toMillis(TIMEOUT_SECONDS * 2)) + assertThat(thread.isAlive).isFalse() + error?.let { throw it } + return result + } + } + + @Suppress("UNCHECKED_CAST") + private fun argumentSourceOf( + serializer: KSerializer, + entry: T, + ): KotlinxArgumentSource = + KotlinxArgumentSource(entry, serializer as KSerializer) { failures += it } + + private companion object { + /** Bounds every wait, so that a deadlock fails the test rather than hanging it. */ + const val TIMEOUT_SECONDS = 5L + } +} diff --git a/sentry-android-navigation3/src/test/kotlin/io/sentry/compose/navigation3/internal/backstack/kotlinx/KotlinxUnencodableArgumentsTest.kt b/sentry-android-navigation3/src/test/kotlin/io/sentry/compose/navigation3/internal/backstack/kotlinx/KotlinxUnencodableArgumentsTest.kt new file mode 100644 index 00000000000..4d0c81cd776 --- /dev/null +++ b/sentry-android-navigation3/src/test/kotlin/io/sentry/compose/navigation3/internal/backstack/kotlinx/KotlinxUnencodableArgumentsTest.kt @@ -0,0 +1,36 @@ +package io.sentry.compose.navigation3.internal.backstack.kotlinx + +import com.google.common.truth.Truth.assertThat +import io.sentry.compose.navigation3.internal.backstack.ArgumentDropReason +import io.sentry.compose.navigation3.internal.backstack.ArgumentSink +import io.sentry.compose.navigation3.internal.backstack.DropArguments +import org.junit.Assert.assertThrows +import org.junit.Test +import org.mockito.kotlin.mock +import org.mockito.kotlin.verifyNoInteractions + +class KotlinxUnencodableArgumentsTest { + + @Test + fun `reads as only the encoding failure`() { + val arguments: Map = KotlinxUnencodableArguments + + assertThat(arguments) + .containsExactly( + ArgumentDropReason.ARGUMENT_DROP_REASON_KEY, + ArgumentDropReason.ENCODING_FAILED.serializedValue, + ) + } + + @Test + fun `writeTo() - drops the arguments as an unlogged encoding failure without writing to the sink`() { + val sink = mock() + + val thrown = + assertThrows(DropArguments::class.java) { KotlinxUnencodableArguments.writeTo(sink) } + + assertThat(thrown.reason).isEqualTo(ArgumentDropReason.ENCODING_FAILED) + assertThat(thrown.warning).isNull() + verifyNoInteractions(sink) + } +}