Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
Signed-off-by: tobiasKaminsky <[email protected]>
  • Loading branch information
tobiasKaminsky committed Oct 6, 2022
1 parent 60a3055 commit 39fcd31
Show file tree
Hide file tree
Showing 24 changed files with 1,338 additions and 90 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "dav4jvm"]
path = dav4jvm
url = /home/tobi/projekt/github/bitfireAT/dav4jvm
1 change: 1 addition & 0 deletions dav4jvm
Submodule dav4jvm added at c61e4b
3 changes: 2 additions & 1 deletion library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ configurations {
dependencies {
implementation 'org.apache.jackrabbit:jackrabbit-webdav:2.13.5'
api 'com.squareup.okhttp3:okhttp:5.0.0-alpha.10'
implementation 'com.gitlab.bitfireAT:dav4jvm:2.1.3' // in transition phase, we use old and new libs
implementation 'com.github.bitfireAT:dav4jvm:2.1.4'
// in transition phase, we use old and new libs
implementation group: 'com.google.code.gson', name: 'gson', version: '2.9.1'
implementation 'androidx.annotation:annotation:1.5.0'
compileOnly 'com.google.code.findbugs:annotations:3.0.1u2'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ public static void beforeAll() throws InterruptedException,
String userId = loginName; // for test same as userId
String credentials = Credentials.basic(loginName, password);
nextcloudClient = new NextcloudClient(url, userId, credentials, context);
nextcloudClient.setUserId(userId);

waitForServer(client, url);
testConnection();
Expand Down
260 changes: 260 additions & 0 deletions library/src/androidTest/java/com/owncloud/android/Dav4JVMtest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,260 @@
/*
* Nextcloud Android client application
*
* @author Tobias Kaminsky
* Copyright (C) 2022 Tobias Kaminsky
* Copyright (C) 2022 Nextcloud GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/

package com.owncloud.android

import at.bitfire.dav4jvm.DavResource
import at.bitfire.dav4jvm.PropertyRegistry
import at.bitfire.dav4jvm.Response
import at.bitfire.dav4jvm.property.CreationDate
import at.bitfire.dav4jvm.property.GetContentType
import at.bitfire.dav4jvm.property.GetLastModified
import at.bitfire.dav4jvm.property.ResourceType
import com.nextcloud.common.NextcloudAuthenticator
import com.nextcloud.talk.components.filebrowser.models.properties.OCId
import com.nextcloud.talk.components.filebrowser.models.properties.OCOwnerDisplayName
import com.nextcloud.talk.components.filebrowser.models.properties.OCOwnerId
import com.nextcloud.talk.components.filebrowser.models.properties.OCSize
import com.owncloud.android.lib.common.network.WebdavUtils
import com.owncloud.android.lib.common.utils.WebDavFileUtils
import com.owncloud.android.lib.resources.files.CreateFolderRemoteOperation
import com.owncloud.android.lib.resources.files.ReadFolderRemoteOperation
import com.owncloud.android.lib.resources.files.SearchRemoteOperation
import com.owncloud.android.lib.resources.files.ToggleFavoriteRemoteOperation
import com.owncloud.android.lib.resources.files.UploadFileRemoteOperation
import com.owncloud.android.lib.resources.files.model.RemoteFile
import com.owncloud.android.lib.resources.files.webdav.NCEtag
import com.owncloud.android.lib.resources.files.webdav.NCFavorite
import com.owncloud.android.lib.resources.files.webdav.NCMountType
import com.owncloud.android.lib.resources.files.webdav.Permissions
import com.owncloud.android.lib.resources.status.OCCapability
import okhttp3.HttpUrl.Companion.toHttpUrl
import org.apache.jackrabbit.webdav.DavConstants
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Test
import java.io.IOException

class Dav4JVMtest : AbstractIT() {
@Test
@Throws(IOException::class)
fun singlePropfind() {
val path = "/testFolder/"

// create folder
CreateFolderRemoteOperation(
path,
true
).execute(client).isSuccess

// verify folder
assertTrue(ReadFolderRemoteOperation(path).execute(client).isSuccess)

// add favorite
assertTrue(ToggleFavoriteRemoteOperation(true, path).execute(client).isSuccess)

// do old read folder operation to compare data against it
val result = ReadFolderRemoteOperation(path).execute(client).data as List<RemoteFile>
val oldRemoteFile = result[0]

// new
val httpUrl = (nextcloudClient.filesDavUri.toString() + path).toHttpUrl()

var davResponse: Response? = null

val memberElements: MutableList<Response> = ArrayList()
var rootElement: Response? = null

// disable redirect
val client = nextcloudClient.client
.newBuilder()
.followRedirects(false)
.authenticator(NextcloudAuthenticator(nextcloudClient.credentials, "Authorization"))
.build()

// register custom property
registerProperties()

DavResource(
client,
httpUrl
)

DavResource(
client,
httpUrl
).propfind(
DavConstants.DEPTH_1,
CreationDate.NAME,
NCFavorite.NAME,
NCEtag.NAME,
GetLastModified.NAME,
GetContentType.NAME,
ResourceType.NAME,
Permissions.NAME,
OCId.NAME,
OCSize.NAME,
NCMountType.NAME,
OCOwnerId.NAME,
OCOwnerDisplayName.NAME
) { response: Response, hrefRelation: Response.HrefRelation? ->
davResponse = response
when (hrefRelation) {
Response.HrefRelation.MEMBER -> memberElements.add(response)
Response.HrefRelation.SELF -> rootElement = response
Response.HrefRelation.OTHER -> {}
else -> {}
}
}

assertTrue(davResponse?.isSuccess() == true)
assertTrue(rootElement != null)
assertEquals(0, memberElements.size)

val remoteFile = WebDavFileUtils().parseResponse(rootElement, nextcloudClient.filesDavUri)

val date = davResponse?.get(CreationDate::class.java)
assertEquals(
oldRemoteFile.creationTimestamp,
(WebdavUtils.parseResponseDate(date?.creationDate)?.time ?: 0) / 1000
)

assertTrue(oldRemoteFile.isFavorite)
val favorite = davResponse?.get(NCFavorite::class.java)
assertTrue(favorite?.isOcFavorite == true)

assertEquals(oldRemoteFile.remotePath, remoteFile.remotePath)
assertEquals(oldRemoteFile.mimeType, remoteFile.mimeType)
assertEquals(oldRemoteFile.length, remoteFile.length)
assertEquals(oldRemoteFile.creationTimestamp, remoteFile.creationTimestamp)
// assertEquals(oldRemoteFile.modifiedTimestamp, remoteFile.modifiedTimestamp)
assertEquals(oldRemoteFile.uploadTimestamp, remoteFile.uploadTimestamp)
assertEquals(oldRemoteFile.etag, remoteFile.etag)
assertEquals(oldRemoteFile.permissions, remoteFile.permissions)
assertEquals(oldRemoteFile.remoteId, remoteFile.remoteId)
assertEquals(oldRemoteFile.size, remoteFile.size)
assertEquals(oldRemoteFile.isFavorite, remoteFile.isFavorite)
assertEquals(oldRemoteFile.isEncrypted, remoteFile.isEncrypted)
assertEquals(oldRemoteFile.mountType, remoteFile.mountType)
assertEquals(oldRemoteFile.ownerId, remoteFile.ownerId)
assertEquals(oldRemoteFile.ownerDisplayName, remoteFile.ownerDisplayName)
assertEquals(oldRemoteFile.unreadCommentsCount, remoteFile.unreadCommentsCount)
assertEquals(oldRemoteFile.isHasPreview, remoteFile.isHasPreview)
assertEquals(oldRemoteFile.note, remoteFile.note)
assertEquals(oldRemoteFile.sharees, remoteFile.sharees)
assertEquals(oldRemoteFile.richWorkspace, remoteFile.richWorkspace)
assertEquals(oldRemoteFile.isLocked, remoteFile.isLocked)
assertEquals(oldRemoteFile.lockType, remoteFile.lockType)
assertEquals(oldRemoteFile.lockOwner, remoteFile.lockOwner)
assertEquals(oldRemoteFile.lockOwnerDisplayName, remoteFile.lockOwnerDisplayName)
assertEquals(oldRemoteFile.lockTimestamp, remoteFile.lockTimestamp)
assertEquals(oldRemoteFile.lockOwnerEditor, remoteFile.lockOwnerEditor)
assertEquals(oldRemoteFile.lockTimeout, remoteFile.lockTimeout)
assertEquals(oldRemoteFile.lockToken, remoteFile.lockToken)
assertEquals(oldRemoteFile.localId, remoteFile.localId)

// assertEquals(oldRemoteFile, remoteFile)
}

@Test
fun search() {
val path = "/testFolder/"

// create folder
// assertTrue(
CreateFolderRemoteOperation(
path,
true
).execute(client).isSuccess
// )

// create file
val filePath = createFile("text")
val remotePath = "/test.md"

assertTrue(
UploadFileRemoteOperation(
filePath,
remotePath,
"text/markdown",
"",
RANDOM_MTIME,
System.currentTimeMillis(),
true
).execute(client).isSuccess
)

registerProperties()

var ror = SearchRemoteOperation(
"test",
SearchRemoteOperation.SearchType.FILE_SEARCH,
false,
OCCapability(23, 0, 0)
).execute(
client
)

assertTrue(ror.isSuccess)
assertEquals(2, ror.resultData.size)

val oldRemoteFile = ror.resultData[0]
assertEquals(path, oldRemoteFile.remotePath)

ror = SearchRemoteOperation(
"test",
SearchRemoteOperation.SearchType.FILE_SEARCH,
false,
OCCapability(23, 0, 0)
).execute(
nextcloudClient
)

assertTrue(ror.isSuccess)
assertEquals(2, ror.resultData.size)

val remoteFile = ror.resultData[0]
assertEquals(path, remoteFile.remotePath)

assertEquals(oldRemoteFile.remoteId, remoteFile.remoteId)
}

@Test
fun proppatch() {
assertTrue(false)
}

private fun registerProperties() {
val list = listOf(
NCFavorite.Factory(),
NCEtag.Factory(),
Permissions.Factory(),
OCId.Factory(),
OCSize.Factory(),
NCMountType.Factory(),
OCOwnerId.Factory(),
OCOwnerDisplayName.Factory()
)

PropertyRegistry.register(list)
}
}
9 changes: 5 additions & 4 deletions library/src/androidTest/java/com/owncloud/android/FileIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
*/
package com.owncloud.android;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import android.net.Uri;

import com.owncloud.android.lib.common.operations.RemoteOperationResult;
Expand All @@ -43,10 +47,6 @@
import java.util.ArrayList;
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

/**
* Tests related to file operations
*/
Expand All @@ -60,6 +60,7 @@ public void testCreateFolderSuccess() {

// verify folder
assertTrue(new ReadFolderRemoteOperation(path).execute(client).isSuccess());
assertTrue(new ReadFolderRemoteOperation(path).execute(nextcloudClient).isSuccess());

// remove folder
assertTrue(new RemoveFileRemoteOperation(path).execute(client).isSuccess());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Nextcloud Android client application
*
* @author Tobias Kaminsky
* Copyright (C) 2022 Tobias Kaminsky
* Copyright (C) 2022 Nextcloud GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/

package com.nextcloud.common;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import okhttp3.Authenticator;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.Route;

public class NextcloudAuthenticator implements Authenticator {
private String credentials;
private String authenticatorType;

public NextcloudAuthenticator(@NonNull String credentials, @NonNull String authenticatorType) {
this.credentials = credentials;
this.authenticatorType = authenticatorType;
}

@Nullable
@Override
public Request authenticate(@Nullable Route route, @NonNull Response response) {
if (response.request().header(authenticatorType) != null) {
return null;
}

Response countedResponse = response;

int attemptsCount = 0;

while ((countedResponse = countedResponse.priorResponse()) != null) {
attemptsCount++;
if (attemptsCount == 3) {
return null;
}
}

return response.request().newBuilder()
.header(authenticatorType, credentials)
.build();
}
}
Loading

0 comments on commit 39fcd31

Please sign in to comment.