Skip to content

Commit

Permalink
Fix: Handle exception if Context.registerReceiver throws (#1747)
Browse files Browse the repository at this point in the history
  • Loading branch information
marandaneto authored Oct 4, 2021
1 parent ed295d6 commit 7b4b8c2
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 19 deletions.
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)
}
}

0 comments on commit 7b4b8c2

Please sign in to comment.