ソースを参照

Add tests

Signed-off-by: alperozturk <alper_ozturk@proton.me>
alperozturk 1 年間 前
コミット
f55d61c05a

+ 32 - 0
app/src/androidTest/java/com/owncloud/android/EncryptionIT.kt

@@ -0,0 +1,32 @@
+/*
+ * Nextcloud - Android Client
+ *
+ * SPDX-FileCopyrightText: 2024 Your Name <your@email.com>
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+package com.owncloud.android
+
+import com.owncloud.android.datamodel.OCFile
+import java.security.SecureRandom
+
+open class EncryptionIT : AbstractIT() {
+
+    fun testFolder(): OCFile {
+        val rootPath = "/"
+        val folderPath = "/TestFolder/"
+
+        OCFile(rootPath).apply {
+            storageManager.saveFile(this)
+        }
+
+        return OCFile(folderPath).apply {
+            decryptedRemotePath = folderPath
+            isEncrypted = true
+            fileLength = SecureRandom().nextLong()
+            setFolder()
+            parentId = storageManager.getFileByDecryptedRemotePath(rootPath)!!.fileId
+            storageManager.saveFile(this)
+        }
+    }
+}

+ 28 - 2
app/src/androidTest/java/com/owncloud/android/utils/EncryptionUtilsIT.kt

@@ -7,13 +7,18 @@
  */
 package com.owncloud.android.utils
 
-import com.owncloud.android.AbstractIT
+import com.owncloud.android.EncryptionIT
 import com.owncloud.android.datamodel.ArbitraryDataProviderImpl
+import com.owncloud.android.datamodel.e2e.v1.decrypted.Data
+import com.owncloud.android.datamodel.e2e.v1.decrypted.DecryptedFile
+import com.owncloud.android.datamodel.e2e.v1.decrypted.DecryptedFolderMetadataFileV1
+import com.owncloud.android.datamodel.e2e.v1.decrypted.DecryptedMetadata
 import com.owncloud.android.lib.resources.e2ee.CsrHelper
+import com.owncloud.android.operations.RefreshFolderOperation
 import org.junit.Assert.assertEquals
 import org.junit.Test
 
-class EncryptionUtilsIT : AbstractIT() {
+class EncryptionUtilsIT : EncryptionIT() {
     @Throws(
         java.security.NoSuchAlgorithmException::class,
         java.io.IOException::class,
@@ -30,4 +35,25 @@ class EncryptionUtilsIT : AbstractIT() {
 
         assertEquals(key, EncryptionUtils.getPublicKey(user, e2eUser, arbitraryDataProvider))
     }
+
+    @Test
+    @Throws(Exception::class)
+    fun testUpdateFileNameForEncryptedFileV1() {
+        val folder = testFolder()
+
+        val decryptedFilename = "image.png"
+        val mockEncryptedFilename = "encrypted_file_name.png"
+
+        val decryptedMetadata = DecryptedMetadata()
+        val filesData = DecryptedFile().apply {
+            encrypted = Data().apply {
+                filename = decryptedFilename
+            }
+        }
+        val files = mapOf(mockEncryptedFilename to filesData)
+        val metadata = DecryptedFolderMetadataFileV1(decryptedMetadata, files)
+
+        RefreshFolderOperation.updateFileNameForEncryptedFileV1(storageManager, metadata, folder)
+        assertEquals(folder.decryptedRemotePath.contains("null"), false)
+    }
 }

+ 10 - 38
app/src/androidTest/java/com/owncloud/android/utils/EncryptionUtilsV2IT.kt

@@ -10,7 +10,7 @@ package com.owncloud.android.utils
 import com.google.gson.reflect.TypeToken
 import com.nextcloud.client.account.MockUser
 import com.nextcloud.common.User
-import com.owncloud.android.AbstractIT
+import com.owncloud.android.EncryptionIT
 import com.owncloud.android.datamodel.OCFile
 import com.owncloud.android.datamodel.e2e.v1.decrypted.Data
 import com.owncloud.android.datamodel.e2e.v1.decrypted.DecryptedFolderMetadataFileV1
@@ -27,9 +27,8 @@ import junit.framework.TestCase.assertEquals
 import junit.framework.TestCase.assertTrue
 import org.junit.Assert.assertNotEquals
 import org.junit.Test
-import java.security.SecureRandom
 
-class EncryptionUtilsV2IT : AbstractIT() {
+class EncryptionUtilsV2IT : EncryptionIT() {
     private val encryptionTestUtils = EncryptionTestUtils()
     private val encryptionUtilsV2 = EncryptionUtilsV2()
 
@@ -785,44 +784,17 @@ class EncryptionUtilsV2IT : AbstractIT() {
 
     @Test
     @Throws(Exception::class)
-    fun testUpdateFileNameForEncryptedFileWhenEncryptedFileUploadRemotePathShouldSetAsEncrypted() {
-        val rootPath = "/"
-        val folderPath = "/TestFolder/"
+    fun testUpdateFileNameForEncryptedFile() {
+        val folder = testFolder()
 
-        OCFile(rootPath).apply {
-            storageManager.saveFile(this)
-        }
-
-        OCFile(folderPath).apply {
-            decryptedRemotePath = folderPath
-            isEncrypted = true
-            fileLength = SecureRandom().nextLong()
-            setFolder()
-            parentId = storageManager.getFileByDecryptedRemotePath(rootPath)!!.fileId
-            storageManager.saveFile(this)
-        }
-
-        val decryptedFilename = "image.png"
-        val mockEncryptedFilename = "encrypted_file_name.png"
-
-        val imageFile = OCFile(folderPath + decryptedFilename).apply {
-            mimeType = "image/png"
-            fileName = mockEncryptedFilename
-            isEncrypted = true
-            fileLength = 1024000
-            modificationTimestamp = 1188206955000
-            parentId = storageManager.getFileByEncryptedRemotePath(folderPath).fileId
-            storageManager.saveFile(this)
-        }
-
-        val decryptedMetadata = DecryptedMetadata().apply {
-            folders = mutableMapOf(mockEncryptedFilename to decryptedFilename)
-        }
-        val metadata = DecryptedFolderMetadataFile(decryptedMetadata)
+        val metadata = EncryptionTestUtils().generateFolderMetadataV2(
+            client.userId,
+            EncryptionTestIT.publicKey
+        )
 
-        RefreshFolderOperation.updateFileNameForEncryptedFile(storageManager, metadata, imageFile)
+        RefreshFolderOperation.updateFileNameForEncryptedFile(storageManager, metadata, folder)
 
-        assertNotEquals(decryptedFilename, imageFile.fileName)
+        assertEquals(folder.decryptedRemotePath.contains("null"), false)
     }
 
     /**

+ 1 - 1
app/src/main/java/com/owncloud/android/datamodel/e2e/v2/decrypted/DecryptedMetadata.kt

@@ -13,7 +13,7 @@ data class DecryptedMetadata(
     val keyChecksums: MutableList<String> = mutableListOf(),
     val deleted: Boolean = false,
     var counter: Long = 0,
-    var folders: MutableMap<String, String> = mutableMapOf(),
+    val folders: MutableMap<String, String> = mutableMapOf(),
     val files: MutableMap<String, DecryptedFile> = mutableMapOf(),
     @Transient
     var metadataKey: ByteArray = EncryptionUtils.generateKey()

+ 0 - 1
app/src/main/java/com/owncloud/android/operations/RefreshFolderOperation.java

@@ -601,7 +601,6 @@ public class RefreshFolderOperation extends RemoteOperation {
         return metadata;
     }
 
-    // TODO write test for decryptedRemotePath existence...
     public static void updateFileNameForEncryptedFileV1(FileDataStorageManager storageManager,
                                                         @NonNull DecryptedFolderMetadataFileV1 metadata,
                                                         OCFile updatedFile) {