|
| 1 | +/* |
| 2 | + * Nextcloud Android Library |
| 3 | + * |
| 4 | + * SPDX-FileCopyrightText: 2023-2024 Nextcloud GmbH and Nextcloud contributors |
| 5 | + * SPDX-FileCopyrightText: 2023 Tobias Kaminsky <tobias@kaminsky.me> |
| 6 | + * SPDX-License-Identifier: MIT |
| 7 | + */ |
| 8 | +package com.owncloud.android.lib.resources.e2ee |
| 9 | + |
| 10 | +import android.text.TextUtils |
| 11 | +import com.nextcloud.common.defaultSessionTimeOut |
| 12 | +import com.nextcloud.test.RandomStringGenerator.make |
| 13 | +import com.owncloud.android.AbstractIT |
| 14 | +import com.owncloud.android.lib.common.OwnCloudClientManagerFactory |
| 15 | +import com.owncloud.android.lib.resources.files.CreateFolderRemoteOperation |
| 16 | +import com.owncloud.android.lib.resources.files.ReadFileRemoteOperation |
| 17 | +import com.owncloud.android.lib.resources.files.model.RemoteFile |
| 18 | +import com.owncloud.android.lib.resources.status.OwnCloudVersion |
| 19 | +import junit.framework.TestCase |
| 20 | +import org.junit.Assert |
| 21 | + |
| 22 | +@Suppress("LongMethod", "MagicNumber") |
| 23 | +class UpdateMetadataRemoteOperationIT : AbstractIT() { |
| 24 | + // @Test |
| 25 | + fun uploadAndModifyV1() { |
| 26 | + // tests only for NC19+ |
| 27 | + testOnlyOnServer(OwnCloudVersion.nextcloud_20) |
| 28 | + |
| 29 | + // E2E server app checks for official NC client with >=3.13.0, |
| 30 | + // and blocks all other clients, e.g. 3rd party apps using this lib |
| 31 | + OwnCloudClientManagerFactory.setUserAgent("Mozilla/5.0 (Android) Nextcloud-android/3.13.0") |
| 32 | + |
| 33 | + // create folder |
| 34 | + val folder = "/" + make(20) + "/" |
| 35 | + TestCase.assertTrue(CreateFolderRemoteOperation(folder, true).execute(client).isSuccess) |
| 36 | + val remoteFolder = |
| 37 | + ReadFileRemoteOperation(folder).execute(client).getSingleData() as RemoteFile? |
| 38 | + |
| 39 | + TestCase.assertNotNull(remoteFolder) |
| 40 | + |
| 41 | + // mark as encrypted |
| 42 | + TestCase.assertTrue( |
| 43 | + ToggleEncryptionRemoteOperation( |
| 44 | + remoteFolder!!.localId, |
| 45 | + remoteFolder.remotePath, |
| 46 | + true |
| 47 | + ).execute(client) |
| 48 | + .isSuccess |
| 49 | + ) |
| 50 | + |
| 51 | + // Lock |
| 52 | + var token = |
| 53 | + LockFileRemoteOperation(remoteFolder.localId) |
| 54 | + .execute(client) |
| 55 | + .getResultData() |
| 56 | + Assert.assertFalse(TextUtils.isEmpty(token)) |
| 57 | + |
| 58 | + // add metadata |
| 59 | + val expectedMetadata = "metadata" |
| 60 | + TestCase.assertTrue( |
| 61 | + StoreMetadataRemoteOperation(remoteFolder.localId, expectedMetadata) |
| 62 | + .execute(client) |
| 63 | + .isSuccess |
| 64 | + ) |
| 65 | + |
| 66 | + // unlock |
| 67 | + TestCase.assertTrue( |
| 68 | + UnlockFileRemoteOperation(remoteFolder.localId, token).execute(client).isSuccess |
| 69 | + ) |
| 70 | + |
| 71 | + // verify metadata |
| 72 | + val retrievedMetadata = |
| 73 | + GetMetadataRemoteOperation(remoteFolder.localId) |
| 74 | + .execute(client) |
| 75 | + .getResultData() |
| 76 | + |
| 77 | + TestCase.assertEquals(expectedMetadata, retrievedMetadata.metadata) |
| 78 | + |
| 79 | + // Lock |
| 80 | + token = |
| 81 | + LockFileRemoteOperation(remoteFolder.localId) |
| 82 | + .execute(client) |
| 83 | + .getResultData() |
| 84 | + Assert.assertFalse(TextUtils.isEmpty(token)) |
| 85 | + |
| 86 | + // update metadata |
| 87 | + val updatedMetadata = "metadata2" |
| 88 | + TestCase.assertTrue( |
| 89 | + UpdateMetadataRemoteOperation(remoteFolder.localId, updatedMetadata, token) |
| 90 | + .execute(client) |
| 91 | + .isSuccess |
| 92 | + ) |
| 93 | + |
| 94 | + // unlock |
| 95 | + TestCase.assertTrue( |
| 96 | + UnlockFileRemoteOperation(remoteFolder.localId, token).execute(client).isSuccess |
| 97 | + ) |
| 98 | + |
| 99 | + // verify metadata |
| 100 | + val retrievedMetadata2 = |
| 101 | + GetMetadataRemoteOperation(remoteFolder.localId) |
| 102 | + .execute(client) |
| 103 | + .getSingleData() as String? |
| 104 | + |
| 105 | + TestCase.assertEquals(updatedMetadata, retrievedMetadata2) |
| 106 | + } |
| 107 | + |
| 108 | + // @Test |
| 109 | + fun uploadAndModifyV2() { |
| 110 | + // tests only for NC19+ |
| 111 | + testOnlyOnServer(OwnCloudVersion.nextcloud_20) |
| 112 | + |
| 113 | + // E2E server app checks for official NC client with >=3.13.0, |
| 114 | + // and blocks all other clients, e.g. 3rd party apps using this lib |
| 115 | + OwnCloudClientManagerFactory.setUserAgent("Mozilla/5.0 (Android) Nextcloud-android/3.13.0") |
| 116 | + |
| 117 | + // create folder |
| 118 | + val folder = "/" + make(20) + "/" |
| 119 | + TestCase.assertTrue(CreateFolderRemoteOperation(folder, true).execute(client).isSuccess) |
| 120 | + val remoteFolder = |
| 121 | + ReadFileRemoteOperation(folder).execute(client).getSingleData() as RemoteFile? |
| 122 | + |
| 123 | + TestCase.assertNotNull(remoteFolder) |
| 124 | + |
| 125 | + // mark as encrypted |
| 126 | + TestCase.assertTrue( |
| 127 | + ToggleEncryptionRemoteOperation( |
| 128 | + remoteFolder!!.localId, |
| 129 | + remoteFolder.remotePath, |
| 130 | + true |
| 131 | + ).execute(client) |
| 132 | + .isSuccess |
| 133 | + ) |
| 134 | + |
| 135 | + // Lock |
| 136 | + var counter = 0 |
| 137 | + var token = |
| 138 | + LockFileRemoteOperation(remoteFolder.localId) |
| 139 | + .execute(client) |
| 140 | + .getResultData() |
| 141 | + Assert.assertFalse(TextUtils.isEmpty(token)) |
| 142 | + |
| 143 | + // add metadata |
| 144 | + val expectedMetadata = "metadata" |
| 145 | + var signature = "signature" |
| 146 | + |
| 147 | + TestCase.assertTrue( |
| 148 | + StoreMetadataV2RemoteOperation( |
| 149 | + remoteFolder.remoteId!!, |
| 150 | + expectedMetadata, |
| 151 | + token, |
| 152 | + signature |
| 153 | + ).execute(client) |
| 154 | + .isSuccess |
| 155 | + ) |
| 156 | + |
| 157 | + // unlock |
| 158 | + TestCase.assertTrue( |
| 159 | + UnlockFileRemoteOperation(remoteFolder.localId, token).execute(client).isSuccess |
| 160 | + ) |
| 161 | + |
| 162 | + // verify metadata |
| 163 | + var metadataResponse = |
| 164 | + GetMetadataRemoteOperation(remoteFolder.localId) |
| 165 | + .execute(client) |
| 166 | + .getResultData() |
| 167 | + |
| 168 | + TestCase.assertEquals(signature, metadataResponse.signature) |
| 169 | + TestCase.assertEquals(expectedMetadata, metadataResponse.metadata) |
| 170 | + |
| 171 | + // Lock |
| 172 | + counter += 1 |
| 173 | + token = |
| 174 | + LockFileRemoteOperation(remoteFolder.localId, counter.toLong(), defaultSessionTimeOut) |
| 175 | + .execute(client) |
| 176 | + .getResultData() |
| 177 | + Assert.assertFalse(TextUtils.isEmpty(token)) |
| 178 | + |
| 179 | + // update metadata |
| 180 | + val updatedMetadata = "metadata2" |
| 181 | + signature = "signature2" |
| 182 | + TestCase.assertTrue( |
| 183 | + UpdateMetadataV2RemoteOperation( |
| 184 | + remoteFolder.remoteId!!, |
| 185 | + updatedMetadata, |
| 186 | + token, |
| 187 | + signature |
| 188 | + ).execute(client) |
| 189 | + .isSuccess |
| 190 | + ) |
| 191 | + |
| 192 | + // unlock |
| 193 | + TestCase.assertTrue( |
| 194 | + UnlockFileRemoteOperation(remoteFolder.localId, token).execute(client).isSuccess |
| 195 | + ) |
| 196 | + |
| 197 | + // verify metadata |
| 198 | + metadataResponse = |
| 199 | + GetMetadataRemoteOperation(remoteFolder.localId) |
| 200 | + .execute(client) |
| 201 | + .getResultData() |
| 202 | + |
| 203 | + TestCase.assertEquals(signature, metadataResponse.signature) |
| 204 | + TestCase.assertEquals(updatedMetadata, metadataResponse.metadata) |
| 205 | + } |
| 206 | +} |
0 commit comments