FileNameValidatorTests.kt 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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.isValid("file<name", capability, targetContext)
  32. assertEquals("File name contains invalid characters: <", result)
  33. }
  34. @Test
  35. fun testReservedName() {
  36. val result = FileNameValidator.isValid("CON", capability, targetContext)
  37. assertEquals(targetContext.getString(R.string.file_name_validator_error_reserved_names, "CON"), result)
  38. }
  39. @Test
  40. fun testEndsWithSpaceOrPeriod() {
  41. val result = FileNameValidator.isValid("filename ", capability, targetContext)
  42. assertEquals(targetContext.getString(R.string.file_name_validator_error_ends_with_space_period), result)
  43. val result2 = FileNameValidator.isValid("filename.", capability, targetContext)
  44. assertEquals(targetContext.getString(R.string.file_name_validator_error_ends_with_space_period), result2)
  45. }
  46. @Test
  47. fun testEmptyFileName() {
  48. val result = FileNameValidator.isValid("", capability, targetContext)
  49. assertEquals(targetContext.getString(R.string.filename_empty), result)
  50. }
  51. @Test
  52. fun testFileAlreadyExists() {
  53. val existingFiles = mutableSetOf("existingFile")
  54. val result = FileNameValidator.isValid("existingFile", capability, targetContext, existingFiles)
  55. assertEquals(targetContext.getString(R.string.file_already_exists), result)
  56. }
  57. @Test
  58. fun testValidFileName() {
  59. val result = FileNameValidator.isValid("validFileName", capability, targetContext)
  60. assertNull(result)
  61. }
  62. @Test
  63. fun testIsFileHidden() {
  64. assertTrue(FileNameValidator.isFileHidden(".hiddenFile"))
  65. assertFalse(FileNameValidator.isFileHidden("visibleFile"))
  66. }
  67. @Test
  68. fun testIsFileNameAlreadyExist() {
  69. val existingFiles = mutableSetOf("existingFile")
  70. assertTrue(FileNameValidator.isFileNameAlreadyExist("existingFile", existingFiles))
  71. assertFalse(FileNameValidator.isFileNameAlreadyExist("newFile", existingFiles))
  72. }
  73. @Test
  74. fun testValidFolderAndFilePaths() {
  75. val folderPath = "validFolder"
  76. val filePaths = listOf("file1.txt", "file2.doc", "file3.jpg")
  77. val result = FileNameValidator.checkPath(folderPath, filePaths, capability, targetContext)
  78. assertTrue(result)
  79. }
  80. @Test
  81. fun testFolderPathWithReservedName() {
  82. val folderPath = "CON"
  83. val filePaths = listOf("file1.txt", "file2.doc", "file3.jpg")
  84. val result = FileNameValidator.checkPath(folderPath, filePaths, capability, targetContext)
  85. assertFalse(result)
  86. }
  87. @Test
  88. fun testFilePathWithReservedName() {
  89. val folderPath = "validFolder"
  90. val filePaths = listOf("file1.txt", "PRN.doc", "file3.jpg")
  91. val result = FileNameValidator.checkPath(folderPath, filePaths, capability, targetContext)
  92. assertFalse(result)
  93. }
  94. @Test
  95. fun testFolderPathWithInvalidCharacter() {
  96. val folderPath = "invalid<Folder"
  97. val filePaths = listOf("file1.txt", "file2.doc", "file3.jpg")
  98. val result = FileNameValidator.checkPath(folderPath, filePaths, capability, targetContext)
  99. assertFalse(result)
  100. }
  101. @Test
  102. fun testFilePathWithInvalidCharacter() {
  103. val folderPath = "validFolder"
  104. val filePaths = listOf("file1.txt", "file|2.doc", "file3.jpg")
  105. val result = FileNameValidator.checkPath(folderPath, filePaths, capability, targetContext)
  106. assertFalse(result)
  107. }
  108. @Test
  109. fun testFolderPathEndingWithSpace() {
  110. val folderPath = "folderWithSpace "
  111. val filePaths = listOf("file1.txt", "file2.doc", "file3.jpg")
  112. val result = FileNameValidator.checkPath(folderPath, filePaths, capability, targetContext)
  113. assertFalse(result)
  114. }
  115. @Test
  116. fun testFilePathEndingWithPeriod() {
  117. val folderPath = "validFolder"
  118. val filePaths = listOf("file1.txt", "file2.doc", "file3.")
  119. val result = FileNameValidator.checkPath(folderPath, filePaths, capability, targetContext)
  120. assertFalse(result)
  121. }
  122. @Test
  123. fun testFilePathWithNestedFolder() {
  124. val folderPath = "validFolder\\secondValidFolder\\CON"
  125. val filePaths = listOf("file1.txt", "file2.doc", "file3.")
  126. val result = FileNameValidator.checkPath(folderPath, filePaths, capability, targetContext)
  127. assertFalse(result)
  128. }
  129. @Test
  130. fun testOnlyFolderPath() {
  131. val folderPath = "/A1/Aaaww/W/C2/"
  132. val result = FileNameValidator.checkPath(folderPath, listOf(), capability, targetContext)
  133. assertTrue(result)
  134. }
  135. @Test
  136. fun testOnlyFolderPathWithOneReservedName() {
  137. val folderPath = "/A1/Aaaww/CON/W/C2/"
  138. val result = FileNameValidator.checkPath(folderPath, listOf(), capability, targetContext)
  139. assertFalse(result)
  140. }
  141. }