-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: tobiasKaminsky <[email protected]>
- Loading branch information
1 parent
60a3055
commit 39fcd31
Showing
24 changed files
with
1,338 additions
and
90 deletions.
There are no files selected for viewing
This file contains 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,3 @@ | ||
[submodule "dav4jvm"] | ||
path = dav4jvm | ||
url = /home/tobi/projekt/github/bitfireAT/dav4jvm |
Submodule dav4jvm
added at
c61e4b
This file contains 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 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
260 changes: 260 additions & 0 deletions
260
library/src/androidTest/java/com/owncloud/android/Dav4JVMtest.kt
This file contains 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,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) | ||
} | ||
} |
This file contains 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
64 changes: 64 additions & 0 deletions
64
library/src/main/java/com/nextcloud/common/NextcloudAuthenticator.java
This file contains 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,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(); | ||
} | ||
} |
Oops, something went wrong.