From e6c9bdd681e106bf0d2b403dc8379d74fc9638b3 Mon Sep 17 00:00:00 2001 From: buenaflor Date: Tue, 16 May 2023 14:20:45 +0200 Subject: [PATCH 1/6] fix nsnumber conversion to long --- .../io/sentry/kotlin/multiplatform/SentryExceptionTest.kt | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 sentry-kotlin-multiplatform/src/commonAppleTest/kotlin/io/sentry/kotlin/multiplatform/SentryExceptionTest.kt diff --git a/sentry-kotlin-multiplatform/src/commonAppleTest/kotlin/io/sentry/kotlin/multiplatform/SentryExceptionTest.kt b/sentry-kotlin-multiplatform/src/commonAppleTest/kotlin/io/sentry/kotlin/multiplatform/SentryExceptionTest.kt new file mode 100644 index 00000000..a446f8db --- /dev/null +++ b/sentry-kotlin-multiplatform/src/commonAppleTest/kotlin/io/sentry/kotlin/multiplatform/SentryExceptionTest.kt @@ -0,0 +1,4 @@ +package io.sentry.kotlin.multiplatform + +class SentryExceptionTest { +} \ No newline at end of file From a00062221011af528c4ef3e13c7be3a2e1c1d3f2 Mon Sep 17 00:00:00 2001 From: buenaflor Date: Tue, 16 May 2023 14:24:31 +0200 Subject: [PATCH 2/6] fix nsnumber conversion to long --- .../extensions/SentryEventExtensions.kt | 4 +- .../extensions/SentryExceptionExtensions.kt | 2 +- .../multiplatform/SentryExceptionTest.kt | 77 +++++++++++++++++++ .../sentry.kmp.demo/sentry/SentrySetup.kt | 6 +- 4 files changed, 81 insertions(+), 8 deletions(-) diff --git a/sentry-kotlin-multiplatform/src/commonAppleMain/kotlin/io/sentry/kotlin/multiplatform/extensions/SentryEventExtensions.kt b/sentry-kotlin-multiplatform/src/commonAppleMain/kotlin/io/sentry/kotlin/multiplatform/extensions/SentryEventExtensions.kt index 94674881..73e6d8f3 100644 --- a/sentry-kotlin-multiplatform/src/commonAppleMain/kotlin/io/sentry/kotlin/multiplatform/extensions/SentryEventExtensions.kt +++ b/sentry-kotlin-multiplatform/src/commonAppleMain/kotlin/io/sentry/kotlin/multiplatform/extensions/SentryEventExtensions.kt @@ -15,8 +15,8 @@ internal fun CocoaSentryEvent.applyKmpEvent(kmpEvent: SentryEvent): CocoaSentryE user = kmpEvent.user?.toCocoaUser() serverName = kmpEvent.serverName dist = kmpEvent.dist - breadcrumbs = kmpEvent.breadcrumbs?.map { it.toCocoaBreadcrumb() }?.toMutableList() - tags = kmpEvent.tags?.toMutableMap() + breadcrumbs = kmpEvent.breadcrumbs.map { it.toCocoaBreadcrumb() }.toMutableList() + tags = kmpEvent.tags.toMutableMap() eventId = SentryId(kmpEvent.eventId.toString()) return this } diff --git a/sentry-kotlin-multiplatform/src/commonAppleMain/kotlin/io/sentry/kotlin/multiplatform/extensions/SentryExceptionExtensions.kt b/sentry-kotlin-multiplatform/src/commonAppleMain/kotlin/io/sentry/kotlin/multiplatform/extensions/SentryExceptionExtensions.kt index 8308d12d..d811f745 100644 --- a/sentry-kotlin-multiplatform/src/commonAppleMain/kotlin/io/sentry/kotlin/multiplatform/extensions/SentryExceptionExtensions.kt +++ b/sentry-kotlin-multiplatform/src/commonAppleMain/kotlin/io/sentry/kotlin/multiplatform/extensions/SentryExceptionExtensions.kt @@ -7,5 +7,5 @@ internal fun CocoaSentryException.toKmpSentryException() = SentryException( type = type, value = value, module = module, - threadId = threadId as Long? + threadId = threadId?.longLongValue // longLong represents a 64-bit integer like Kotlin Long ) diff --git a/sentry-kotlin-multiplatform/src/commonAppleTest/kotlin/io/sentry/kotlin/multiplatform/SentryExceptionTest.kt b/sentry-kotlin-multiplatform/src/commonAppleTest/kotlin/io/sentry/kotlin/multiplatform/SentryExceptionTest.kt index a446f8db..cb9bd53a 100644 --- a/sentry-kotlin-multiplatform/src/commonAppleTest/kotlin/io/sentry/kotlin/multiplatform/SentryExceptionTest.kt +++ b/sentry-kotlin-multiplatform/src/commonAppleTest/kotlin/io/sentry/kotlin/multiplatform/SentryExceptionTest.kt @@ -1,4 +1,81 @@ package io.sentry.kotlin.multiplatform +import io.sentry.kotlin.multiplatform.extensions.toKmpSentryException +import io.sentry.kotlin.multiplatform.protocol.SentryException +import platform.Foundation.NSNumber +import kotlin.test.Test + class SentryExceptionTest { + + @Test + fun `SentryException ThreadId NSNumber long conversion`() { + val value = "testValue" + val type = "type" + val cocoaSentryException = CocoaSentryException(value = value, type = type).apply { + threadId = NSNumber(long = 1) + } + val sentryException = SentryException( + type = type, + value = value, + threadId = 1 + ) + assert(cocoaSentryException.toKmpSentryException() == sentryException) + } + + @Test + fun `SentryException ThreadId NSNumber longLong conversion`() { + val value = "testValue" + val type = "type" + val cocoaSentryException = CocoaSentryException(value = value, type = type).apply { + threadId = NSNumber(longLong = 1L) + } + val sentryException = SentryException( + type = type, + value = value, + threadId = 1 + ) + assert(cocoaSentryException.toKmpSentryException() == sentryException) + } + + @Test + fun `SentryException ThreadId NSNumber int conversion`() { + val value = "testValue" + val type = "type" + val cocoaSentryException = CocoaSentryException(value = value, type = type).apply { + threadId = NSNumber(int = 1) + } + val sentryException = SentryException( + type = type, + value = value, + threadId = 1 + ) + assert(cocoaSentryException.toKmpSentryException() == sentryException) + } + + @Test + fun `SentryException ThreadId NSNumber short conversion`() { + val value = "testValue" + val type = "type" + val cocoaSentryException = CocoaSentryException(value = value, type = type).apply { + threadId = NSNumber(short = 1) + } + val sentryException = SentryException( + type = type, + value = value, + threadId = 1 + ) + assert(cocoaSentryException.toKmpSentryException() == sentryException) + } + + @Test + fun `SentryException ThreadId NSNumber null conversion`() { + val value = "testValue" + val type = "type" + val cocoaSentryException = CocoaSentryException(value = value, type = type) + val sentryException = SentryException( + type = type, + value = value + ) + assert(cocoaSentryException.toKmpSentryException() == sentryException) + } } \ No newline at end of file diff --git a/sentry-samples/kmp-app-mvvm-di/shared/src/commonMain/kotlin/sentry.kmp.demo/sentry/SentrySetup.kt b/sentry-samples/kmp-app-mvvm-di/shared/src/commonMain/kotlin/sentry.kmp.demo/sentry/SentrySetup.kt index 920aa3a2..ba23fcbc 100644 --- a/sentry-samples/kmp-app-mvvm-di/shared/src/commonMain/kotlin/sentry.kmp.demo/sentry/SentrySetup.kt +++ b/sentry-samples/kmp-app-mvvm-di/shared/src/commonMain/kotlin/sentry.kmp.demo/sentry/SentrySetup.kt @@ -15,11 +15,7 @@ private val optionsConfiguration: OptionsConfiguration = { it.attachScreenshot = true it.release = "kmp-release@0.0.1" it.beforeSend = { event -> - if (event.environment == "test") { - null - } else { - event - } + event } it.beforeBreadcrumb = { breadcrumb -> breadcrumb.message = "Add message before every breadcrumb" From d0523e216eb1a2ebb68078b539e6def9863fe582 Mon Sep 17 00:00:00 2001 From: buenaflor Date: Tue, 16 May 2023 14:25:01 +0200 Subject: [PATCH 3/6] revert sample setup --- .../commonMain/kotlin/sentry.kmp.demo/sentry/SentrySetup.kt | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/sentry-samples/kmp-app-mvvm-di/shared/src/commonMain/kotlin/sentry.kmp.demo/sentry/SentrySetup.kt b/sentry-samples/kmp-app-mvvm-di/shared/src/commonMain/kotlin/sentry.kmp.demo/sentry/SentrySetup.kt index ba23fcbc..920aa3a2 100644 --- a/sentry-samples/kmp-app-mvvm-di/shared/src/commonMain/kotlin/sentry.kmp.demo/sentry/SentrySetup.kt +++ b/sentry-samples/kmp-app-mvvm-di/shared/src/commonMain/kotlin/sentry.kmp.demo/sentry/SentrySetup.kt @@ -15,7 +15,11 @@ private val optionsConfiguration: OptionsConfiguration = { it.attachScreenshot = true it.release = "kmp-release@0.0.1" it.beforeSend = { event -> - event + if (event.environment == "test") { + null + } else { + event + } } it.beforeBreadcrumb = { breadcrumb -> breadcrumb.message = "Add message before every breadcrumb" From 4dd2b725eb5c900b0b2b63207922e911a1e47451 Mon Sep 17 00:00:00 2001 From: Sentry Github Bot Date: Tue, 16 May 2023 12:29:52 +0000 Subject: [PATCH 4/6] Format code --- .../io/sentry/kotlin/multiplatform/SentryExceptionTest.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sentry-kotlin-multiplatform/src/commonAppleTest/kotlin/io/sentry/kotlin/multiplatform/SentryExceptionTest.kt b/sentry-kotlin-multiplatform/src/commonAppleTest/kotlin/io/sentry/kotlin/multiplatform/SentryExceptionTest.kt index cb9bd53a..d9a17d01 100644 --- a/sentry-kotlin-multiplatform/src/commonAppleTest/kotlin/io/sentry/kotlin/multiplatform/SentryExceptionTest.kt +++ b/sentry-kotlin-multiplatform/src/commonAppleTest/kotlin/io/sentry/kotlin/multiplatform/SentryExceptionTest.kt @@ -78,4 +78,4 @@ class SentryExceptionTest { ) assert(cocoaSentryException.toKmpSentryException() == sentryException) } -} \ No newline at end of file +} From 38317ddf14acedd40a97189f7a3bf336f5e79aac Mon Sep 17 00:00:00 2001 From: buenaflor Date: Tue, 16 May 2023 14:39:40 +0200 Subject: [PATCH 5/6] add changelog --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8819e947..baf25e30 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## Unreleased +### Fixes + +- fix: NSNumber to Long crash during SentryException conversion ([#92](https://github.com/getsentry/sentry-kotlin-multiplatform/pull/92)) + ### Improvements - ref: improve samples & add SPM docs ([#82](https://github.com/getsentry/sentry-kotlin-multiplatform/pull/82)) From 2c40388a5af0ee003b4a6276a468d05cd669cefd Mon Sep 17 00:00:00 2001 From: buenaflor Date: Tue, 16 May 2023 16:06:22 +0200 Subject: [PATCH 6/6] Add more foundation tests and refactor SentryExceptionTest --- .../kotlin/multiplatform/FoundationTest.kt | 22 ++++++ .../multiplatform/SentryExceptionTest.kt | 69 +++++++------------ 2 files changed, 48 insertions(+), 43 deletions(-) diff --git a/sentry-kotlin-multiplatform/src/commonAppleTest/kotlin/io/sentry/kotlin/multiplatform/FoundationTest.kt b/sentry-kotlin-multiplatform/src/commonAppleTest/kotlin/io/sentry/kotlin/multiplatform/FoundationTest.kt index f71ddf37..459ed529 100644 --- a/sentry-kotlin-multiplatform/src/commonAppleTest/kotlin/io/sentry/kotlin/multiplatform/FoundationTest.kt +++ b/sentry-kotlin-multiplatform/src/commonAppleTest/kotlin/io/sentry/kotlin/multiplatform/FoundationTest.kt @@ -2,6 +2,7 @@ package io.sentry.kotlin.multiplatform import io.sentry.kotlin.multiplatform.extensions.toByteArray import io.sentry.kotlin.multiplatform.extensions.toNSData +import platform.Foundation.NSNumber import platform.Foundation.NSString import platform.Foundation.NSUTF8StringEncoding import platform.Foundation.dataUsingEncoding @@ -24,4 +25,25 @@ class FoundationTest { assertEquals(nsData, byteArray.toNSData()) assertEquals(nsData, nsData.toByteArray().toNSData()) } + + @Test + fun `NSNumber longLong converts to Long correctly`() { + val longValue = 4937446359977427944L + val nsNumber = NSNumber(longLong = longValue) + assertEquals(longValue, nsNumber.longLongValue) + } + + @Test + fun `NSNumber int converts to Long correctly`() { + val intValue = 493744635 + val nsNumber = NSNumber(int = intValue) + assertEquals(intValue.toLong(), nsNumber.longLongValue) + } + + @Test + fun `NSNumber short converts to Long correctly`() { + val shortValue: Short = 4937 + val nsNumber = NSNumber(short = shortValue) + assertEquals(shortValue.toLong(), nsNumber.longLongValue) + } } diff --git a/sentry-kotlin-multiplatform/src/commonAppleTest/kotlin/io/sentry/kotlin/multiplatform/SentryExceptionTest.kt b/sentry-kotlin-multiplatform/src/commonAppleTest/kotlin/io/sentry/kotlin/multiplatform/SentryExceptionTest.kt index d9a17d01..fbcf2037 100644 --- a/sentry-kotlin-multiplatform/src/commonAppleTest/kotlin/io/sentry/kotlin/multiplatform/SentryExceptionTest.kt +++ b/sentry-kotlin-multiplatform/src/commonAppleTest/kotlin/io/sentry/kotlin/multiplatform/SentryExceptionTest.kt @@ -2,80 +2,63 @@ package io.sentry.kotlin.multiplatform import io.sentry.kotlin.multiplatform.extensions.toKmpSentryException import io.sentry.kotlin.multiplatform.protocol.SentryException +import kotlinx.cinterop.convert import platform.Foundation.NSNumber import kotlin.test.Test class SentryExceptionTest { + private val value = "testValue" + private val type = "type" + private val threadId = 1 + + private fun getCocoaSentryException(): CocoaSentryException { + return CocoaSentryException(value = value, type = type) + } + + private fun getKmpSentryException(threadId: Long? = this.threadId.toLong()): SentryException { + return SentryException(value = value, type = type, threadId = threadId) + } @Test fun `SentryException ThreadId NSNumber long conversion`() { - val value = "testValue" - val type = "type" - val cocoaSentryException = CocoaSentryException(value = value, type = type).apply { - threadId = NSNumber(long = 1) + val cocoaSentryException = getCocoaSentryException().apply { + threadId = NSNumber(long = this@SentryExceptionTest.threadId.convert()) } - val sentryException = SentryException( - type = type, - value = value, - threadId = 1 - ) + val sentryException = getKmpSentryException() assert(cocoaSentryException.toKmpSentryException() == sentryException) } @Test fun `SentryException ThreadId NSNumber longLong conversion`() { - val value = "testValue" - val type = "type" - val cocoaSentryException = CocoaSentryException(value = value, type = type).apply { - threadId = NSNumber(longLong = 1L) + val cocoaSentryException = getCocoaSentryException().apply { + threadId = NSNumber(longLong = this@SentryExceptionTest.threadId.convert()) } - val sentryException = SentryException( - type = type, - value = value, - threadId = 1 - ) + val sentryException = getKmpSentryException() assert(cocoaSentryException.toKmpSentryException() == sentryException) } @Test fun `SentryException ThreadId NSNumber int conversion`() { - val value = "testValue" - val type = "type" - val cocoaSentryException = CocoaSentryException(value = value, type = type).apply { - threadId = NSNumber(int = 1) + val cocoaSentryException = getCocoaSentryException().apply { + threadId = NSNumber(int = this@SentryExceptionTest.threadId.convert()) } - val sentryException = SentryException( - type = type, - value = value, - threadId = 1 - ) + val sentryException = getKmpSentryException() assert(cocoaSentryException.toKmpSentryException() == sentryException) } @Test fun `SentryException ThreadId NSNumber short conversion`() { - val value = "testValue" - val type = "type" - val cocoaSentryException = CocoaSentryException(value = value, type = type).apply { - threadId = NSNumber(short = 1) + val cocoaSentryException = getCocoaSentryException().apply { + threadId = NSNumber(short = this@SentryExceptionTest.threadId.convert()) } - val sentryException = SentryException( - type = type, - value = value, - threadId = 1 - ) + val sentryException = getKmpSentryException() assert(cocoaSentryException.toKmpSentryException() == sentryException) } @Test fun `SentryException ThreadId NSNumber null conversion`() { - val value = "testValue" - val type = "type" - val cocoaSentryException = CocoaSentryException(value = value, type = type) - val sentryException = SentryException( - type = type, - value = value - ) + val cocoaSentryException = getCocoaSentryException() + val sentryException = getKmpSentryException(threadId = null) assert(cocoaSentryException.toKmpSentryException() == sentryException) } }