From ec8190755e36898bd45152c7589bd8b3a21f81d9 Mon Sep 17 00:00:00 2001 From: Torsten Grote Date: Tue, 9 Apr 2024 17:14:10 -0300 Subject: [PATCH] Add a method for testing if storage works This is especially useful for WebDAV storage where the user can supply whatever URL and before proceeding, we need to know whether that URL and the provided credentials are actually working. --- .../seedvault/plugins/StoragePlugin.kt | 6 ++++++ .../saf/DocumentsProviderStoragePlugin.kt | 5 +++++ .../seedvault/plugins/webdav/WebDavStorage.kt | 1 + .../plugins/webdav/WebDavStoragePlugin.kt | 16 ++++++++++++++++ .../plugins/webdav/WebDavStoragePluginTest.kt | 11 +++++++++++ 5 files changed, 39 insertions(+) diff --git a/app/src/main/java/com/stevesoltys/seedvault/plugins/StoragePlugin.kt b/app/src/main/java/com/stevesoltys/seedvault/plugins/StoragePlugin.kt index e052bf7a..6f023d22 100644 --- a/app/src/main/java/com/stevesoltys/seedvault/plugins/StoragePlugin.kt +++ b/app/src/main/java/com/stevesoltys/seedvault/plugins/StoragePlugin.kt @@ -9,6 +9,12 @@ import java.io.OutputStream interface StoragePlugin { + /** + * Returns true if the plugin is working, or false if it isn't. + * @throws Exception any kind of exception to provide more info on the error + */ + suspend fun test(): Boolean + /** * Start a new [RestoreSet] with the given token. * diff --git a/app/src/main/java/com/stevesoltys/seedvault/plugins/saf/DocumentsProviderStoragePlugin.kt b/app/src/main/java/com/stevesoltys/seedvault/plugins/saf/DocumentsProviderStoragePlugin.kt index 5f2f3510..56e49c2a 100644 --- a/app/src/main/java/com/stevesoltys/seedvault/plugins/saf/DocumentsProviderStoragePlugin.kt +++ b/app/src/main/java/com/stevesoltys/seedvault/plugins/saf/DocumentsProviderStoragePlugin.kt @@ -33,6 +33,11 @@ internal class DocumentsProviderStoragePlugin( private val packageManager: PackageManager = appContext.packageManager + override suspend fun test(): Boolean { + val dir = storage.rootBackupDir + return dir != null && dir.exists() + } + @Throws(IOException::class) override suspend fun startNewRestoreSet(token: Long) { // reset current storage diff --git a/app/src/main/java/com/stevesoltys/seedvault/plugins/webdav/WebDavStorage.kt b/app/src/main/java/com/stevesoltys/seedvault/plugins/webdav/WebDavStorage.kt index 3027da3b..e1174278 100644 --- a/app/src/main/java/com/stevesoltys/seedvault/plugins/webdav/WebDavStorage.kt +++ b/app/src/main/java/com/stevesoltys/seedvault/plugins/webdav/WebDavStorage.kt @@ -62,6 +62,7 @@ internal abstract class WebDavStorage( .retryOnConnectionFailure(true) .build() + protected val baseUrl = webDavConfig.url protected val url = "${webDavConfig.url}/$root" @Throws(IOException::class) diff --git a/app/src/main/java/com/stevesoltys/seedvault/plugins/webdav/WebDavStoragePlugin.kt b/app/src/main/java/com/stevesoltys/seedvault/plugins/webdav/WebDavStoragePlugin.kt index b78ad2d2..a6e25022 100644 --- a/app/src/main/java/com/stevesoltys/seedvault/plugins/webdav/WebDavStoragePlugin.kt +++ b/app/src/main/java/com/stevesoltys/seedvault/plugins/webdav/WebDavStoragePlugin.kt @@ -27,6 +27,22 @@ internal class WebDavStoragePlugin( root: String = DIRECTORY_ROOT, ) : WebDavStorage(webDavConfig, root), StoragePlugin { + override suspend fun test(): Boolean { + val location = baseUrl.toHttpUrl() + val davCollection = DavCollection(okHttpClient, location) + + val webDavSupported = suspendCoroutine { cont -> + davCollection.options { davCapabilities, response -> + debugLog { "test() = $davCapabilities $response" } + if (davCapabilities.contains("1")) cont.resume(true) + else if (davCapabilities.contains("2")) cont.resume(true) + else if (davCapabilities.contains("3")) cont.resume(true) + else cont.resume(false) + } + } + return webDavSupported + } + @Throws(IOException::class) override suspend fun startNewRestoreSet(token: Long) { val location = "$url/$token".toHttpUrl() diff --git a/app/src/test/java/com/stevesoltys/seedvault/plugins/webdav/WebDavStoragePluginTest.kt b/app/src/test/java/com/stevesoltys/seedvault/plugins/webdav/WebDavStoragePluginTest.kt index 89b4bcf4..d3691015 100644 --- a/app/src/test/java/com/stevesoltys/seedvault/plugins/webdav/WebDavStoragePluginTest.kt +++ b/app/src/test/java/com/stevesoltys/seedvault/plugins/webdav/WebDavStoragePluginTest.kt @@ -30,6 +30,17 @@ internal class WebDavStoragePluginTest : TransportTest() { private val plugin = WebDavStoragePlugin(context, WebDavTestConfig.getConfig()) + @Test + fun `test self-test`() = runBlocking { + assertTrue(plugin.test()) + + val plugin2 = WebDavStoragePlugin(context, WebDavConfig("https://github.com/", "", "")) + val e = assertThrows { + assertFalse(plugin2.test()) + } + println(e) + } + @Test fun `test restore sets and reading+writing`() = runBlocking { val token = System.currentTimeMillis()