Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit f1a441c2 authored by Ricki Hirner's avatar Ricki Hirner
Browse files

WebDAV: accept compliance class 2 or 3 (closes bitfireAT/davx5#21); fix...

WebDAV: accept compliance class 2 or 3 (closes bitfireAT/davx5#21); fix division by zero in progress

- WebDAV service detection: accept compliance class 2 or 3, because they both imply class 1 (there are servers which don't show class 1 although they MUST)
- WebDAV random access: fix division by zero when file size reported by HEAD is zero
parent 56c7d2d7
Loading
Loading
Loading
Loading
+10 −0
Original line number Diff line number Diff line
package at.bitfire.davdroid

import android.app.Application
import androidx.test.platform.app.InstrumentationRegistry

object TestUtils {

    val targetApplication by lazy { InstrumentationRegistry.getInstrumentation().targetContext.applicationContext as Application }

}
 No newline at end of file
+46 −0
Original line number Diff line number Diff line
package at.bitfire.davdroid.ui.webdav

import at.bitfire.davdroid.TestUtils
import at.bitfire.davdroid.model.WebDavMount
import okhttp3.mockwebserver.MockResponse
import okhttp3.mockwebserver.MockWebServer
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Test

class AddWebdavMountActivityTest {

    val model = AddWebdavMountActivity.Model(TestUtils.targetApplication)
    val web = MockWebServer()

    @Test
    fun testHasWebDav_NoDavHeader() {
        web.enqueue(MockResponse().setResponseCode(200))
        assertFalse(model.hasWebDav(WebDavMount(name = "Test", url = web.url("/")), null))
    }

    @Test
    fun testHasWebDav_DavClass_1() {
        web.enqueue(MockResponse()
            .setResponseCode(200)
            .addHeader("DAV", "1"))
        assertTrue(model.hasWebDav(WebDavMount(name = "Test", url = web.url("/")), null))
    }

    @Test
    fun testHasWebDav_DavClass_1and2() {
        web.enqueue(MockResponse()
            .setResponseCode(200)
            .addHeader("DAV", "1,2"))
        assertTrue(model.hasWebDav(WebDavMount(name = "Test", url = web.url("/")), null))
    }

    @Test
    fun testHasWebDav_DavClass_2() {
        web.enqueue(MockResponse()
            .setResponseCode(200)
            .addHeader("DAV", "2"))
        assertTrue(model.hasWebDav(WebDavMount(name = "Test", url = web.url("/")), null))
    }

}
 No newline at end of file
+3 −2
Original line number Diff line number Diff line
@@ -28,6 +28,7 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import okhttp3.HttpUrl
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
import org.apache.commons.collections4.CollectionUtils
import java.net.URI
import java.net.URISyntaxException
import java.util.logging.Level
@@ -165,12 +166,12 @@ class AddWebdavMountActivity: AppCompatActivity() {
            return true
        }

        private fun hasWebDav(mount: WebDavMount, credentials: Credentials?): Boolean {
        fun hasWebDav(mount: WebDavMount, credentials: Credentials?): Boolean {
            var supported = false
            HttpClient.Builder(getApplication(), null, credentials).build().use { client ->
                val dav = DavResource(client.okHttpClient, mount.url)
                dav.options { davCapabilities, _ ->
                    if (davCapabilities.contains("1"))
                    if (CollectionUtils.containsAny(davCapabilities, "1", "2", "3"))
                        supported = true
                }
            }
+6 −1
Original line number Diff line number Diff line
@@ -74,10 +74,15 @@ class RandomAccessCallback(
    override fun onRead(offset: Long, size: Int, data: ByteArray): Int {
        Logger.log.fine("onRead $url $offset $size")

        val progress =
            if (fileSize == 0L)     // avoid division by zero
                100
            else
                (offset*100/fileSize).toInt()
        notificationManager.notify(
            notificationTag,
            NotificationUtils.NOTIFY_WEBDAV_ACCESS,
            notification.setProgress(100, (offset*100/fileSize).toInt(), false).build()
            notification.setProgress(100, progress, false).build()
        )

        if (cancellationSignal?.isCanceled == true)