FileNameValidatorTests.kt 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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.AbstractIT
  10. import com.owncloud.android.R
  11. import com.owncloud.android.lib.resources.status.CapabilityBooleanType
  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. class FileNameValidatorTests : AbstractIT() {
  20. private var capability: OCCapability = fileDataStorageManager.getCapability(account.name)
  21. @Before
  22. fun setup() {
  23. capability = capability.apply {
  24. forbiddenFilenames = CapabilityBooleanType.TRUE
  25. forbiddenFilenameExtension = CapabilityBooleanType.TRUE
  26. forbiddenFilenameCharacters = CapabilityBooleanType.TRUE
  27. }
  28. }
  29. @Test
  30. fun testInvalidCharacter() {
  31. val result = FileNameValidator.checkFileName("file<name", capability, targetContext)
  32. assertEquals("File name contains invalid characters: <", result)
  33. }
  34. @Test
  35. fun testReservedName() {
  36. val result = FileNameValidator.checkFileName("CON", capability, targetContext)
  37. assertEquals(targetContext.getString(R.string.file_name_validator_error_reserved_names, "CON"), result)
  38. }
  39. @Test
  40. fun testForbiddenFilenameExtension() {
  41. val result = FileNameValidator.checkFileName("my_fav_file.filepart", capability, targetContext)
  42. assertEquals(
  43. targetContext.getString(R.string.file_name_validator_error_forbidden_file_extensions, "filepart"),
  44. result
  45. )
  46. }
  47. @Test
  48. fun testEndsWithSpaceOrPeriod() {
  49. val result = FileNameValidator.checkFileName("filename ", capability, targetContext)
  50. assertEquals(targetContext.getString(R.string.file_name_validator_error_ends_with_space_period), result)
  51. val result2 = FileNameValidator.checkFileName("filename.", capability, targetContext)
  52. assertEquals(targetContext.getString(R.string.file_name_validator_error_ends_with_space_period), result2)
  53. }
  54. @Test
  55. fun testEmptyFileName() {
  56. val result = FileNameValidator.checkFileName("", capability, targetContext)
  57. assertEquals(targetContext.getString(R.string.filename_empty), result)
  58. }
  59. @Test
  60. fun testFileAlreadyExists() {
  61. val existingFiles = mutableSetOf("existingFile")
  62. val result = FileNameValidator.checkFileName("existingFile", capability, targetContext, existingFiles)
  63. assertEquals(targetContext.getString(R.string.file_already_exists), result)
  64. }
  65. @Test
  66. fun testValidFileName() {
  67. val result = FileNameValidator.checkFileName("validFileName", capability, targetContext)
  68. assertNull(result)
  69. }
  70. @Test
  71. fun testIsFileHidden() {
  72. assertTrue(FileNameValidator.isFileHidden(".hiddenFile"))
  73. assertFalse(FileNameValidator.isFileHidden("visibleFile"))
  74. }
  75. @Test
  76. fun testIsFileNameAlreadyExist() {
  77. val existingFiles = mutableSetOf("existingFile")
  78. assertTrue(FileNameValidator.isFileNameAlreadyExist("existingFile", existingFiles))
  79. assertFalse(FileNameValidator.isFileNameAlreadyExist("newFile", existingFiles))
  80. }
  81. @Test
  82. fun testValidFolderAndFilePaths() {
  83. val folderPath = "validFolder"
  84. val filePaths = listOf("file1.txt", "file2.doc", "file3.jpg")
  85. val result = FileNameValidator.checkFolderAndFilePaths(folderPath, filePaths, capability, targetContext)
  86. assertTrue(result)
  87. }
  88. @Test
  89. fun testFolderPathWithReservedName() {
  90. val folderPath = "CON"
  91. val filePaths = listOf("file1.txt", "file2.doc", "file3.jpg")
  92. val result = FileNameValidator.checkFolderAndFilePaths(folderPath, filePaths, capability, targetContext)
  93. assertFalse(result)
  94. }
  95. @Test
  96. fun testFilePathWithReservedName() {
  97. val folderPath = "validFolder"
  98. val filePaths = listOf("file1.txt", "PRN.doc", "file3.jpg")
  99. val result = FileNameValidator.checkFolderAndFilePaths(folderPath, filePaths, capability, targetContext)
  100. assertFalse(result)
  101. }
  102. @Test
  103. fun testFolderPathWithInvalidCharacter() {
  104. val folderPath = "invalid<Folder"
  105. val filePaths = listOf("file1.txt", "file2.doc", "file3.jpg")
  106. val result = FileNameValidator.checkFolderAndFilePaths(folderPath, filePaths, capability, targetContext)
  107. assertFalse(result)
  108. }
  109. @Test
  110. fun testFilePathWithInvalidCharacter() {
  111. val folderPath = "validFolder"
  112. val filePaths = listOf("file1.txt", "file|2.doc", "file3.jpg")
  113. val result = FileNameValidator.checkFolderAndFilePaths(folderPath, filePaths, capability, targetContext)
  114. assertFalse(result)
  115. }
  116. @Test
  117. fun testFolderPathEndingWithSpace() {
  118. val folderPath = "folderWithSpace "
  119. val filePaths = listOf("file1.txt", "file2.doc", "file3.jpg")
  120. val result = FileNameValidator.checkFolderAndFilePaths(folderPath, filePaths, capability, targetContext)
  121. assertFalse(result)
  122. }
  123. @Test
  124. fun testFilePathEndingWithPeriod() {
  125. val folderPath = "validFolder"
  126. val filePaths = listOf("file1.txt", "file2.doc", "file3.")
  127. val result = FileNameValidator.checkFolderAndFilePaths(folderPath, filePaths, capability, targetContext)
  128. assertFalse(result)
  129. }
  130. @Test
  131. fun testFilePathWithNestedFolder() {
  132. val folderPath = "validFolder\\secondValidFolder\\CON"
  133. val filePaths = listOf("file1.txt", "file2.doc", "file3.")
  134. val result = FileNameValidator.checkFolderAndFilePaths(folderPath, filePaths, capability, targetContext)
  135. assertFalse(result)
  136. }
  137. @Test
  138. fun testOnlyFolderPath() {
  139. val folderPath = "/A1/Aaaww/W/C2/"
  140. val result = FileNameValidator.checkFolderAndFilePaths(folderPath, listOf(), capability, targetContext)
  141. assertTrue(result)
  142. }
  143. @Test
  144. fun testOnlyFolderPathWithOneReservedName() {
  145. val folderPath = "/A1/Aaaww/CON/W/C2/"
  146. val result = FileNameValidator.checkFolderAndFilePaths(folderPath, listOf(), capability, targetContext)
  147. assertFalse(result)
  148. }
  149. }