FileNameValidatorTests.kt 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * Nextcloud - Android Client
  3. *
  4. * SPDX-FileCopyrightText: 2024 Alper Ozturk <alper.ozturk@nextcloud.com>
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. package com.nextcloud.utils
  8. import com.nextcloud.utils.fileNameValidator.FileNameValidator
  9. import com.owncloud.android.AbstractOnServerIT
  10. import com.owncloud.android.R
  11. import com.owncloud.android.lib.resources.status.NextcloudVersion
  12. import com.owncloud.android.lib.resources.status.OCCapability
  13. import org.junit.Assert.assertEquals
  14. import org.junit.Assert.assertFalse
  15. import org.junit.Assert.assertNull
  16. import org.junit.Assert.assertTrue
  17. import org.junit.Before
  18. import org.junit.Test
  19. @Suppress("TooManyFunctions")
  20. class FileNameValidatorTests : AbstractOnServerIT() {
  21. private var capability: OCCapability = fileDataStorageManager.getCapability(account.name)
  22. @Before
  23. fun setup() {
  24. capability = capability.apply {
  25. forbiddenFilenamesJson = """[".htaccess",".htaccess"]"""
  26. forbiddenFilenameBaseNamesJson = """
  27. ["con", "prn", "aux", "nul", "com0", "com1", "com2", "com3", "com4",
  28. "com5", "com6", "com7", "com8", "com9", "com¹", "com²", "com³",
  29. "lpt0", "lpt1", "lpt2", "lpt3", "lpt4", "lpt5", "lpt6", "lpt7",
  30. "lpt8", "lpt9", "lpt¹", "lpt²", "lpt³"]
  31. """
  32. forbiddenFilenameExtensionJson = """[" ",".",".part",".part"]"""
  33. forbiddenFilenameCharactersJson = """["<", ">", ":", "\\\\", "/", "|", "?", "*", "&"]"""
  34. }
  35. }
  36. @Test
  37. fun testInvalidCharacter() {
  38. testOnlyOnServer(NextcloudVersion.nextcloud_30)
  39. val result = FileNameValidator.checkFileName("file<name", capability, targetContext)
  40. assertEquals(
  41. String.format(targetContext.getString(R.string.file_name_validator_error_invalid_character), "<"),
  42. result
  43. )
  44. }
  45. @Test
  46. fun testReservedName() {
  47. testOnlyOnServer(NextcloudVersion.nextcloud_30)
  48. val result = FileNameValidator.checkFileName("CON", capability, targetContext)
  49. assertEquals(targetContext.getString(R.string.file_name_validator_error_reserved_names, "CON"), result)
  50. }
  51. @Test
  52. fun testForbiddenFilenameExtension() {
  53. testOnlyOnServer(NextcloudVersion.nextcloud_30)
  54. val result = FileNameValidator.checkFileName("my_fav_file.filepart", capability, targetContext)
  55. assertEquals(
  56. targetContext.getString(R.string.file_name_validator_error_forbidden_file_extensions, "filepart"),
  57. result
  58. )
  59. }
  60. @Test
  61. fun testEndsWithSpaceOrPeriod() {
  62. val result = FileNameValidator.checkFileName("filename ", capability, targetContext)
  63. val result2 = FileNameValidator.checkFileName("filename.", capability, targetContext)
  64. if (capability.version.isOlderThan(NextcloudVersion.nextcloud_30)) {
  65. assertEquals(null, result)
  66. assertEquals(null, result2)
  67. } else {
  68. assertEquals(targetContext.getString(R.string.file_name_validator_error_ends_with_space_period), result)
  69. assertEquals(targetContext.getString(R.string.file_name_validator_error_ends_with_space_period), result2)
  70. }
  71. }
  72. @Test
  73. fun testEmptyFileName() {
  74. val result = FileNameValidator.checkFileName("", capability, targetContext)
  75. assertEquals(targetContext.getString(R.string.filename_empty), result)
  76. }
  77. @Test
  78. fun testFileAlreadyExists() {
  79. val existingFiles = setOf("existingFile")
  80. val result = FileNameValidator.checkFileName("existingFile", capability, targetContext, existingFiles)
  81. assertEquals(targetContext.getString(R.string.file_already_exists), result)
  82. }
  83. @Test
  84. fun testValidFileName() {
  85. val result = FileNameValidator.checkFileName("validFileName", capability, targetContext)
  86. assertNull(result)
  87. }
  88. @Test
  89. fun testIsFileHidden() {
  90. assertTrue(FileNameValidator.isFileHidden(".hiddenFile"))
  91. assertFalse(FileNameValidator.isFileHidden("visibleFile"))
  92. }
  93. @Test
  94. fun testIsFileNameAlreadyExist() {
  95. val existingFiles = setOf("existingFile")
  96. assertTrue(FileNameValidator.isFileNameAlreadyExist("existingFile", existingFiles))
  97. assertFalse(FileNameValidator.isFileNameAlreadyExist("newFile", existingFiles))
  98. }
  99. @Test
  100. fun testValidFolderAndFilePaths() {
  101. val folderPath = "validFolder"
  102. val filePaths = listOf("file1.txt", "file2.doc", "file3.jpg")
  103. val result = FileNameValidator.checkFolderAndFilePaths(folderPath, filePaths, capability, targetContext)
  104. assertTrue(result)
  105. }
  106. @Test
  107. fun testFolderPathWithReservedName() {
  108. testOnlyOnServer(NextcloudVersion.nextcloud_30)
  109. val folderPath = "CON"
  110. val filePaths = listOf("file1.txt", "file2.doc", "file3.jpg")
  111. val result = FileNameValidator.checkFolderAndFilePaths(folderPath, filePaths, capability, targetContext)
  112. assertFalse(result)
  113. }
  114. @Test
  115. fun testFilePathWithReservedName() {
  116. testOnlyOnServer(NextcloudVersion.nextcloud_30)
  117. val folderPath = "validFolder"
  118. val filePaths = listOf("file1.txt", "PRN.doc", "file3.jpg")
  119. val result = FileNameValidator.checkFolderAndFilePaths(folderPath, filePaths, capability, targetContext)
  120. assertFalse(result)
  121. }
  122. @Test
  123. fun testFolderPathWithInvalidCharacter() {
  124. testOnlyOnServer(NextcloudVersion.nextcloud_30)
  125. val folderPath = "invalid<Folder"
  126. val filePaths = listOf("file1.txt", "file2.doc", "file3.jpg")
  127. val result = FileNameValidator.checkFolderAndFilePaths(folderPath, filePaths, capability, targetContext)
  128. assertFalse(result)
  129. }
  130. @Test
  131. fun testFilePathWithInvalidCharacter() {
  132. testOnlyOnServer(NextcloudVersion.nextcloud_30)
  133. val folderPath = "validFolder"
  134. val filePaths = listOf("file1.txt", "file|2.doc", "file3.jpg")
  135. val result = FileNameValidator.checkFolderAndFilePaths(folderPath, filePaths, capability, targetContext)
  136. assertFalse(result)
  137. }
  138. @Test
  139. fun testFolderPathEndingWithSpace() {
  140. val folderPath = "folderWithSpace "
  141. val filePaths = listOf("file1.txt", "file2.doc", "file3.jpg")
  142. val result = FileNameValidator.checkFolderAndFilePaths(folderPath, filePaths, capability, targetContext)
  143. assertEquals(capability.version.isOlderThan(NextcloudVersion.nextcloud_30), result)
  144. }
  145. @Test
  146. fun testFilePathEndingWithPeriod() {
  147. val folderPath = "validFolder"
  148. val filePaths = listOf("file1.txt", "file2.doc", "file3.")
  149. val result = FileNameValidator.checkFolderAndFilePaths(folderPath, filePaths, capability, targetContext)
  150. assertEquals(capability.version.isOlderThan(NextcloudVersion.nextcloud_30), result)
  151. }
  152. @Test
  153. fun testFilePathWithNestedFolder() {
  154. testOnlyOnServer(NextcloudVersion.nextcloud_30)
  155. val folderPath = "validFolder\\secondValidFolder\\CON"
  156. val filePaths = listOf("file1.txt", "file2.doc", "file3.")
  157. val result = FileNameValidator.checkFolderAndFilePaths(folderPath, filePaths, capability, targetContext)
  158. assertFalse(result)
  159. }
  160. @Test
  161. fun testOnlyFolderPath() {
  162. val folderPath = "/A1/Aaaww/W/C2/"
  163. val result = FileNameValidator.checkFolderAndFilePaths(folderPath, listOf(), capability, targetContext)
  164. assertTrue(result)
  165. }
  166. @Test
  167. fun testOnlyFolderPathWithOneReservedName() {
  168. testOnlyOnServer(NextcloudVersion.nextcloud_30)
  169. val folderPath = "/A1/Aaaww/CON/W/C2/"
  170. val result = FileNameValidator.checkFolderAndFilePaths(folderPath, listOf(), capability, targetContext)
  171. assertFalse(result)
  172. }
  173. }