-
-
Notifications
You must be signed in to change notification settings - Fork 91
Avoid NPE when missing DTSTART
for recurring events
#1336
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
Merged
rfc2822
merged 20 commits into
main-ose
from
1265-jtx-board-task-sync-causes-nullpointerexception
Apr 16, 2025
Merged
Changes from 17 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
4c7c890
Added handling of null dtstart
ArnyminerZ 2709233
Added JtxSyncManagerTest
ArnyminerZ 4357545
Test recurrence id without dtstart does not cause NPE
sunkup 67e4bd7
Extract syncmanager creation from try-catch
sunkup 9781223
Add tests
sunkup fc4250d
Assert RRULE remains in main vtodo
sunkup 6915602
Skip tests when jtx board not installed
sunkup ec45659
Correct annotation
sunkup 40cf8fd
Simplify null checks
sunkup 7e13da7
Extract recurid definition
sunkup efee743
Update ical4android
sunkup e0f0043
Find recurrence instance without dtstart
sunkup 32bec93
Rename method for clarity and update kdoc
sunkup 72971bf
Use new method in test too
sunkup fee2e25
Fix lint warnings
sunkup 18a5142
Merge branch 'main-ose' into 1265-jtx-board-task-sync-causes-nullpoin…
sunkup ac75029
Use a custom rule to wrap and ignore security exception if jtxboard i…
sunkup b8ff89e
Use existing permission utils
sunkup 15f5300
Rename capture to catch exceptions rule
sunkup ed1701c
Format code
sunkup File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
app/src/androidTest/kotlin/at/bitfire/davdroid/CaptureExceptionsRule.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Copyright © All Contributors. See LICENSE and AUTHORS in the root directory for details. | ||
*/ | ||
|
||
package at.bitfire.davdroid | ||
|
||
import org.junit.rules.TestRule | ||
import org.junit.runner.Description | ||
import org.junit.runners.model.Statement | ||
import kotlin.reflect.KClass | ||
|
||
/** | ||
* Use this custom rule to ignore exceptions thrown by another rule. | ||
*/ | ||
class CaptureExceptionsRule( | ||
private val innerRule: TestRule, | ||
private vararg val exceptionsToIgnore: KClass<out Throwable> | ||
) : TestRule { | ||
override fun apply(base: Statement, description: Description): Statement { | ||
return object : Statement() { | ||
override fun evaluate() { | ||
try { | ||
innerRule.apply(base, description).evaluate() | ||
} catch (e: Throwable) { | ||
val shouldIgnore = exceptionsToIgnore.any { it.isInstance(e) } | ||
if (shouldIgnore) | ||
base.evaluate() | ||
else | ||
throw e | ||
} | ||
} | ||
} | ||
} | ||
} |
189 changes: 189 additions & 0 deletions
189
app/src/androidTest/kotlin/at/bitfire/davdroid/sync/JtxSyncManagerTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,189 @@ | ||
/* | ||
* Copyright © All Contributors. See LICENSE and AUTHORS in the root directory for details. | ||
*/ | ||
|
||
package at.bitfire.davdroid.sync | ||
|
||
import android.content.ContentProviderClient | ||
import android.content.Context | ||
import android.content.pm.PackageManager | ||
import androidx.core.content.ContextCompat | ||
import androidx.test.rule.GrantPermissionRule | ||
import at.bitfire.davdroid.CaptureExceptionsRule | ||
import at.bitfire.davdroid.db.Collection | ||
import at.bitfire.davdroid.db.Service | ||
import at.bitfire.davdroid.network.HttpClient | ||
import at.bitfire.davdroid.repository.DavServiceRepository | ||
import at.bitfire.davdroid.resource.LocalJtxCollection | ||
import at.bitfire.davdroid.resource.LocalJtxCollectionStore | ||
import at.bitfire.davdroid.sync.account.TestAccount | ||
import at.bitfire.ical4android.util.MiscUtils.closeCompat | ||
import at.techbee.jtx.JtxContract | ||
import dagger.hilt.android.qualifiers.ApplicationContext | ||
import dagger.hilt.android.testing.HiltAndroidRule | ||
import dagger.hilt.android.testing.HiltAndroidTest | ||
import okhttp3.HttpUrl.Companion.toHttpUrl | ||
import org.junit.After | ||
import org.junit.Assert.assertEquals | ||
import org.junit.Assume.assumeTrue | ||
import org.junit.Before | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import java.io.StringReader | ||
import javax.inject.Inject | ||
|
||
|
||
/** | ||
* Ensure you have jtxBoard installed on the emulator, before running these tests. Otherwise they | ||
* will be skipped. | ||
*/ | ||
@HiltAndroidTest | ||
class JtxSyncManagerTest { | ||
|
||
@Inject | ||
@ApplicationContext | ||
lateinit var context: Context | ||
|
||
@Inject | ||
lateinit var httpClientBuilder: HttpClient.Builder | ||
|
||
@Inject | ||
lateinit var serviceRepository: DavServiceRepository | ||
|
||
@Inject | ||
lateinit var localJtxCollectionStore: LocalJtxCollectionStore | ||
|
||
@Inject | ||
lateinit var jtxSyncManagerFactory: JtxSyncManager.Factory | ||
|
||
@get:Rule | ||
val hiltRule = HiltAndroidRule(this) | ||
|
||
@get:Rule | ||
val permissionRule = CaptureExceptionsRule( | ||
GrantPermissionRule.grant( | ||
"at.techbee.jtx.permission.READ", | ||
"at.techbee.jtx.permission.WRITE" | ||
), | ||
SecurityException::class | ||
) | ||
|
||
private val account = TestAccount.create() | ||
|
||
private lateinit var provider: ContentProviderClient | ||
private lateinit var syncManager: JtxSyncManager | ||
private lateinit var localJtxCollection: LocalJtxCollection | ||
|
||
@Before | ||
fun setUp() { | ||
hiltRule.inject() | ||
|
||
// Check jtxBoard permissions were granted (+jtxBoard is installed); skip test otherwise | ||
val canRead = permissionGranted("at.techbee.jtx.permission.READ") | ||
val canWrite = permissionGranted("at.techbee.jtx.permission.WRITE") | ||
assumeTrue(canRead && canWrite) | ||
sunkup marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// Acquire the jtx content provider | ||
provider = context.contentResolver.acquireContentProviderClient(JtxContract.AUTHORITY)!! | ||
|
||
// Create dummy dependencies | ||
val service = Service(0, account.name, Service.TYPE_CALDAV, null) | ||
val serviceId = serviceRepository.insertOrReplace(service) | ||
val dbCollection = Collection(0, serviceId, type = Collection.TYPE_CALENDAR, url = "https://example.com".toHttpUrl()) | ||
localJtxCollection = localJtxCollectionStore.create(provider, dbCollection)!! | ||
syncManager = jtxSyncManagerFactory.jtxSyncManager( | ||
account = account, | ||
extras = arrayOf(), | ||
httpClient = httpClientBuilder.build(), | ||
authority = JtxContract.AUTHORITY, | ||
syncResult = SyncResult(), | ||
localCollection = localJtxCollection, | ||
collection = dbCollection | ||
) | ||
} | ||
|
||
@After | ||
fun tearDown() { | ||
if (this::localJtxCollection.isInitialized) | ||
localJtxCollectionStore.delete(localJtxCollection) | ||
serviceRepository.deleteAll() | ||
if (this::provider.isInitialized) | ||
provider.closeCompat() | ||
TestAccount.remove(account) | ||
} | ||
|
||
|
||
@Test | ||
fun testProcessICalObject_addsVtodo() { | ||
val calendar = "BEGIN:VCALENDAR\n" + | ||
"PRODID:-Vivaldi Calendar V1.0//EN\n" + | ||
"VERSION:2.0\n" + | ||
"BEGIN:VTODO\n" + | ||
"SUMMARY:Test Task (Main VTODO)\n" + | ||
"DTSTAMP;VALUE=DATE-TIME:20250228T032800Z\n" + | ||
"UID:47a23c66-8c1a-4b44-bbe8-ebf33f8cf80f\n" + | ||
"END:VTODO\n" + | ||
"END:VCALENDAR" | ||
|
||
// Should create "demo-calendar" | ||
syncManager.processICalObject("demo-calendar", "abc123", StringReader(calendar)) | ||
|
||
// Verify main VTODO is created | ||
val localJtxIcalObject = localJtxCollection.findByName("demo-calendar")!! | ||
assertEquals("47a23c66-8c1a-4b44-bbe8-ebf33f8cf80f", localJtxIcalObject.uid) | ||
assertEquals("abc123", localJtxIcalObject.eTag) | ||
assertEquals("Test Task (Main VTODO)", localJtxIcalObject.summary) | ||
} | ||
|
||
@Test | ||
fun testProcessICalObject_addsRecurringVtodo_withoutDtStart() { | ||
// Valid calendar example (See bitfireAT/davx5-ose#1265) | ||
// Note: We don't support starting a recurrence from DUE (RFC 5545 leaves it open to interpretation) | ||
val calendar = "BEGIN:VCALENDAR\n" + | ||
"PRODID:-Vivaldi Calendar V1.0//EN\n" + | ||
"VERSION:2.0\n" + | ||
"BEGIN:VTODO\n" + | ||
|
||
"SUMMARY:Test Task (Exception)\n" + | ||
"DTSTAMP;VALUE=DATE-TIME:20250228T032800Z\n" + | ||
"DUE;TZID=America/New_York:20250228T130000\n" + | ||
"RECURRENCE-ID;TZID=America/New_York:20250228T130000\n" + | ||
"UID:47a23c66-8c1a-4b44-bbe8-ebf33f8cf80f\n" + | ||
|
||
"END:VTODO\n" + | ||
"BEGIN:VTODO\n" + | ||
|
||
"SUMMARY:Test Task (Main VTODO)\n" + | ||
"DTSTAMP;VALUE=DATE-TIME:20250228T032800Z\n" + | ||
"DUE;TZID=America/New_York:20250228T130000\n" + // Due date will NOT be assumed as start for recurrence | ||
"SEQUENCE:1\n" + | ||
"UID:47a23c66-8c1a-4b44-bbe8-ebf33f8cf80f\n" + | ||
"RRULE:FREQ=WEEKLY;INTERVAL=1;BYDAY=FR;UNTIL=20250505T235959Z\n" + | ||
|
||
"END:VTODO\n" + | ||
"END:VCALENDAR" | ||
|
||
// Create and store calendar | ||
syncManager.processICalObject("demo-calendar", "abc123", StringReader(calendar)) | ||
|
||
// Verify main VTODO was created with RRULE present | ||
val mainVtodo = localJtxCollection.findByName("demo-calendar")!! | ||
assertEquals("Test Task (Main VTODO)", mainVtodo.summary) | ||
assertEquals("FREQ=WEEKLY;UNTIL=20250505T235959Z;INTERVAL=1;BYDAY=FR", mainVtodo.rrule) | ||
|
||
// Verify the RRULE exception instance was created with correct recurrence-id timezone | ||
val vtodoException = localJtxCollection.findRecurInstance( | ||
uid = "47a23c66-8c1a-4b44-bbe8-ebf33f8cf80f", | ||
recurid = "20250228T130000" | ||
)!! | ||
assertEquals("Test Task (Exception)", vtodoException.summary) | ||
assertEquals("America/New_York", vtodoException.recuridTimezone) | ||
} | ||
|
||
|
||
// helpers | ||
|
||
private fun permissionGranted(permission: String) = | ||
ContextCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.