Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Handle exception if Context.registerReceiver throws #1747

Merged
merged 3 commits into from
Oct 4, 2021
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased

* Fix: Handle exception if Context.registerReceiver throws (#1747)
* Feat: Attach Java vendor and version to events and transactions (#1703)

## 5.2.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,18 @@ public void register(final @NotNull IHub hub, final @NotNull SentryOptions optio
for (String item : actions) {
filter.addAction(item);
}
context.registerReceiver(receiver, filter);
options.getLogger().log(SentryLevel.DEBUG, "SystemEventsBreadcrumbsIntegration installed.");
try {
// registerReceiver can throw SecurityException but it's not documented in the official docs
context.registerReceiver(receiver, filter);
this.options
.getLogger()
.log(SentryLevel.DEBUG, "SystemEventsBreadcrumbsIntegration installed.");
} catch (Exception e) {
this.options.setEnableSystemEventBreadcrumbs(false);
this.options
.getLogger()
.log(SentryLevel.ERROR, "Failed to initialize SystemEventsBreadcrumbsIntegration.", e);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,27 @@ import com.nhaarman.mockitokotlin2.check
import com.nhaarman.mockitokotlin2.mock
import com.nhaarman.mockitokotlin2.never
import com.nhaarman.mockitokotlin2.verify
import com.nhaarman.mockitokotlin2.whenever
import io.sentry.Breadcrumb
import io.sentry.IHub
import io.sentry.SentryLevel
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertNotNull
import kotlin.test.assertNull

class SystemEventsBreadcrumbsIntegrationTest {

private class Fixture {
val context = mock<Context>()
var options = SentryAndroidOptions()
val hub = mock<IHub>()

fun getSut(): SystemEventsBreadcrumbsIntegration {
fun getSut(enableSystemEventBreadcrumbs: Boolean = true): SystemEventsBreadcrumbsIntegration {
options = SentryAndroidOptions().apply {
isEnableSystemEventBreadcrumbs = enableSystemEventBreadcrumbs
}
return SystemEventsBreadcrumbsIntegration(context)
}
}
Expand All @@ -30,52 +37,59 @@ class SystemEventsBreadcrumbsIntegrationTest {
@Test
fun `When system events breadcrumb is enabled, it registers callback`() {
val sut = fixture.getSut()
val options = SentryAndroidOptions()
val hub = mock<IHub>()
sut.register(hub, options)

sut.register(fixture.hub, fixture.options)

verify(fixture.context).registerReceiver(any(), any())
assertNotNull(sut.receiver)
}

@Test
fun `When system events breadcrumb is disabled, it doesn't register callback`() {
val sut = fixture.getSut()
val options = SentryAndroidOptions().apply {
isEnableSystemEventBreadcrumbs = false
}
val hub = mock<IHub>()
sut.register(hub, options)
val sut = fixture.getSut(enableSystemEventBreadcrumbs = false)

sut.register(fixture.hub, fixture.options)

verify(fixture.context, never()).registerReceiver(any(), any())
assertNull(sut.receiver)
}

@Test
fun `When ActivityBreadcrumbsIntegration is closed, it should unregister the callback`() {
val sut = fixture.getSut()
val options = SentryAndroidOptions()
val hub = mock<IHub>()
sut.register(hub, options)

sut.register(fixture.hub, fixture.options)
sut.close()

verify(fixture.context).unregisterReceiver(any())
assertNull(sut.receiver)
}

@Test
fun `When broadcast received, added breadcrumb with type and category`() {
val sut = fixture.getSut()
val options = SentryAndroidOptions()
val hub = mock<IHub>()
sut.register(hub, options)

sut.register(fixture.hub, fixture.options)
val intent = Intent().apply {
action = Intent.ACTION_TIME_CHANGED
}
sut.receiver!!.onReceive(any(), intent)

verify(hub).addBreadcrumb(check<Breadcrumb> {
verify(fixture.hub).addBreadcrumb(check<Breadcrumb> {
assertEquals("device.event", it.category)
assertEquals("system", it.type)
assertEquals(SentryLevel.INFO, it.level)
// cant assert data, its not a public API
})
}

@Test
fun `Do not crash if registerReceiver throws exception`() {
val sut = fixture.getSut()
whenever(fixture.context.registerReceiver(any(), any())).thenThrow(SecurityException())

sut.register(fixture.hub, fixture.options)

assertFalse(fixture.options.isEnableSystemEventBreadcrumbs)
}
}