DocumentsStorageProviderIT.kt 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. package com.owncloud.android.providers
  2. import android.provider.DocumentsContract
  3. import android.util.Log
  4. import androidx.documentfile.provider.DocumentFile
  5. import com.owncloud.android.AbstractOnServerIT
  6. import com.owncloud.android.R
  7. import com.owncloud.android.datamodel.OCFile.ROOT_PATH
  8. import com.owncloud.android.providers.DocumentsProviderUtils.assertExistsOnServer
  9. import com.owncloud.android.providers.DocumentsProviderUtils.assertListFilesEquals
  10. import com.owncloud.android.providers.DocumentsProviderUtils.assertReadEquals
  11. import com.owncloud.android.providers.DocumentsProviderUtils.assertRecentlyModified
  12. import com.owncloud.android.providers.DocumentsProviderUtils.assertRegularFile
  13. import com.owncloud.android.providers.DocumentsProviderUtils.assertRegularFolder
  14. import com.owncloud.android.providers.DocumentsProviderUtils.findFileBlocking
  15. import com.owncloud.android.providers.DocumentsProviderUtils.getOCFile
  16. import com.owncloud.android.providers.DocumentsProviderUtils.listFilesBlocking
  17. import com.owncloud.android.providers.DocumentsStorageProvider.DOCUMENTID_SEPARATOR
  18. import kotlinx.coroutines.runBlocking
  19. import net.bytebuddy.utility.RandomString
  20. import org.apache.commons.httpclient.HttpStatus
  21. import org.apache.commons.httpclient.methods.ByteArrayRequestEntity
  22. import org.apache.jackrabbit.webdav.client.methods.PutMethod
  23. import org.junit.After
  24. import org.junit.Assert.assertEquals
  25. import org.junit.Assert.assertFalse
  26. import org.junit.Assert.assertTrue
  27. import org.junit.Before
  28. import org.junit.Test
  29. import kotlin.random.Random
  30. private const val MAX_FILE_NAME_LENGTH = 225
  31. class DocumentsStorageProviderIT : AbstractOnServerIT() {
  32. private val context = targetContext
  33. private val contentResolver = context.contentResolver
  34. private val authority = context.getString(R.string.document_provider_authority)
  35. private val rootFileId = storageManager.getFileByEncryptedRemotePath(ROOT_PATH).fileId
  36. private val documentId = "${user.hashCode()}${DOCUMENTID_SEPARATOR}$rootFileId"
  37. private val uri = DocumentsContract.buildTreeDocumentUri(authority, documentId)
  38. private val rootDir get() = DocumentFile.fromTreeUri(context, uri)!!
  39. @Before
  40. fun before() {
  41. // DocumentsProvider#onCreate() is called when the application is started
  42. // which is *after* AbstractOnServerIT adds the accounts (when the app is freshly installed).
  43. // So we need to query our roots here to ensure that the internal storage map is initialized.
  44. contentResolver.query(DocumentsContract.buildRootsUri(authority), null, null, null)
  45. assertTrue("Storage root does not exist", rootDir.exists())
  46. assertTrue(rootDir.isDirectory)
  47. }
  48. /**
  49. * Delete all files in [rootDir] after each test.
  50. *
  51. * We can't use [AbstractOnServerIT.after] as this is only deleting remote files.
  52. */
  53. @After
  54. override fun after() = runBlocking {
  55. rootDir.listFilesBlocking(context).forEach {
  56. Log.e("TEST", "Deleting ${it.name}...")
  57. it.delete()
  58. }
  59. }
  60. @Test
  61. fun testCreateDeleteFiles() = runBlocking {
  62. // no files in root initially
  63. assertListFilesEquals(emptyList(), rootDir.listFilesBlocking(context))
  64. // create first file
  65. val name1 = RandomString.make()
  66. val type1 = "text/html"
  67. val file1 = rootDir.createFile(type1, name1)!!
  68. // check assumptions
  69. @Suppress("ForbiddenComment")
  70. file1.assertRegularFile(name1, 0L, null/* FIXME: type1 */, rootDir)
  71. file1.assertRecentlyModified()
  72. // file1 is found in root
  73. assertListFilesEquals(listOf(file1), rootDir.listFilesBlocking(context).toList())
  74. // file1 was uploaded
  75. val ocFile1 = file1.getOCFile(storageManager)!!
  76. assertExistsOnServer(client, ocFile1.remotePath, true)
  77. // create second long file with long file name
  78. val name2 = RandomString.make(MAX_FILE_NAME_LENGTH)
  79. val type2 = "application/octet-stream"
  80. val file2 = rootDir.createFile(type2, name2)!!
  81. // file2 was uploaded
  82. val ocFile2 = file2.getOCFile(storageManager)!!
  83. assertExistsOnServer(client, ocFile2.remotePath, true)
  84. // check assumptions
  85. file2.assertRegularFile(name2, 0L, type2, rootDir)
  86. file2.assertRecentlyModified()
  87. // both files get listed in root
  88. assertListFilesEquals(listOf(file1, file2), rootDir.listFiles().toList())
  89. // delete first file
  90. assertTrue(file1.delete())
  91. assertFalse(file1.exists())
  92. assertExistsOnServer(client, ocFile1.remotePath, false)
  93. // only second file gets listed in root
  94. assertListFilesEquals(listOf(file2), rootDir.listFiles().toList())
  95. // delete also second file
  96. assertTrue(file2.delete())
  97. assertFalse(file2.exists())
  98. assertExistsOnServer(client, ocFile2.remotePath, false)
  99. // no more files in root
  100. assertListFilesEquals(emptyList(), rootDir.listFilesBlocking(context))
  101. }
  102. @Test
  103. fun testReadWriteFiles() {
  104. // create random file
  105. val file1 = rootDir.createFile("application/octet-stream", RandomString.make())!!
  106. file1.assertRegularFile(size = 0L)
  107. // write random bytes to file
  108. @Suppress("MagicNumber")
  109. val dataSize = Random.nextInt(1, 99) * 1024
  110. val data1 = Random.nextBytes(dataSize)
  111. contentResolver.openOutputStream(file1.uri, "wt").use {
  112. it!!.write(data1)
  113. }
  114. // read back random bytes
  115. assertReadEquals(data1, contentResolver.openInputStream(file1.uri))
  116. // file size was updated correctly
  117. file1.assertRegularFile(size = data1.size.toLong())
  118. }
  119. @Test
  120. fun testCreateDeleteFolders() = runBlocking {
  121. // create a new folder
  122. val dirName1 = RandomString.make()
  123. val dir1 = rootDir.createDirectory(dirName1)!!
  124. dir1.assertRegularFolder(dirName1, rootDir)
  125. // FIXME about a minute gets lost somewhere after CFO sets the correct time
  126. @Suppress("MagicNumber")
  127. assertTrue(System.currentTimeMillis() - dir1.lastModified() < 60_000)
  128. // dir1.assertRecentlyModified()
  129. // ensure folder was uploaded to server
  130. val ocDir1 = dir1.getOCFile(storageManager)!!
  131. assertExistsOnServer(client, ocDir1.remotePath, true)
  132. // create file in folder
  133. val file1 = dir1.createFile("text/html", RandomString.make())!!
  134. file1.assertRegularFile(parent = dir1)
  135. val ocFile1 = file1.getOCFile(storageManager)!!
  136. assertExistsOnServer(client, ocFile1.remotePath, true)
  137. // we find the new file in the created folder and get it in the list
  138. assertEquals(file1.uri.toString(), dir1.findFileBlocking(context, file1.name!!)!!.uri.toString())
  139. assertListFilesEquals(listOf(file1), dir1.listFilesBlocking(context))
  140. // delete folder
  141. dir1.delete()
  142. assertFalse(dir1.exists())
  143. assertExistsOnServer(client, ocDir1.remotePath, false)
  144. // ensure file got deleted with it
  145. assertFalse(file1.exists())
  146. assertExistsOnServer(client, ocFile1.remotePath, false)
  147. }
  148. @Test
  149. fun testServerChangedFileContent() {
  150. // create random file
  151. val file1 = rootDir.createFile("text/plain", RandomString.make())!!
  152. file1.assertRegularFile(size = 0L)
  153. val createdETag = file1.getOCFile(storageManager)!!.etagOnServer
  154. assertTrue(createdETag.isNotEmpty())
  155. val content1 = "initial content".toByteArray()
  156. // write content bytes to file
  157. contentResolver.openOutputStream(file1.uri, "wt").use {
  158. it!!.write(content1)
  159. }
  160. // refresh
  161. while (file1.getOCFile(storageManager)!!.etagOnServer == createdETag) {
  162. shortSleep()
  163. rootDir.listFiles()
  164. }
  165. val remotePath = file1.getOCFile(storageManager)!!.remotePath
  166. val content2 = "new content".toByteArray()
  167. // modify content on server side
  168. val putMethod = PutMethod(client.getFilesDavUri(remotePath))
  169. putMethod.requestEntity = ByteArrayRequestEntity(content2)
  170. assertEquals(HttpStatus.SC_NO_CONTENT, client.executeMethod(putMethod))
  171. client.exhaustResponse(putMethod.responseBodyAsStream)
  172. putMethod.releaseConnection() // let the connection available for other methods
  173. // read back content bytes
  174. val bytes = contentResolver.openInputStream(file1.uri)?.readBytes() ?: ByteArray(0)
  175. assertEquals(String(content2), String(bytes))
  176. }
  177. }