You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UploadTest.php 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. /**
  3. * @author Joas Schilling <nickvergessen@owncloud.com>
  4. * @author Robin Appelman <icewind@owncloud.com>
  5. * @author Thomas Müller <thomas.mueller@tmit.eu>
  6. *
  7. * @copyright Copyright (c) 2016, ownCloud, Inc.
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace OCA\DAV\Tests\unit\Connector\Sabre\RequestTest;
  24. use OC\Connector\Sabre\Exception\FileLocked;
  25. use OCP\AppFramework\Http;
  26. use OCP\Lock\ILockingProvider;
  27. /**
  28. * Class UploadTest
  29. *
  30. * @group DB
  31. *
  32. * @package OCA\DAV\Tests\unit\Connector\Sabre\RequestTest
  33. */
  34. class UploadTest extends RequestTest {
  35. public function testBasicUpload() {
  36. $user = $this->getUniqueID();
  37. $view = $this->setupUser($user, 'pass');
  38. $this->assertFalse($view->file_exists('foo.txt'));
  39. $response = $this->request($view, $user, 'pass', 'PUT', '/foo.txt', 'asd');
  40. $this->assertEquals(Http::STATUS_CREATED, $response->getStatus());
  41. $this->assertTrue($view->file_exists('foo.txt'));
  42. $this->assertEquals('asd', $view->file_get_contents('foo.txt'));
  43. $info = $view->getFileInfo('foo.txt');
  44. $this->assertInstanceOf('\OC\Files\FileInfo', $info);
  45. $this->assertEquals(3, $info->getSize());
  46. }
  47. public function testUploadOverWrite() {
  48. $user = $this->getUniqueID();
  49. $view = $this->setupUser($user, 'pass');
  50. $view->file_put_contents('foo.txt', 'foobar');
  51. $response = $this->request($view, $user, 'pass', 'PUT', '/foo.txt', 'asd');
  52. $this->assertEquals(Http::STATUS_NO_CONTENT, $response->getStatus());
  53. $this->assertEquals('asd', $view->file_get_contents('foo.txt'));
  54. $info = $view->getFileInfo('foo.txt');
  55. $this->assertInstanceOf('\OC\Files\FileInfo', $info);
  56. $this->assertEquals(3, $info->getSize());
  57. }
  58. /**
  59. * @expectedException \OCA\DAV\Connector\Sabre\Exception\FileLocked
  60. */
  61. public function testUploadOverWriteReadLocked() {
  62. $user = $this->getUniqueID();
  63. $view = $this->setupUser($user, 'pass');
  64. $view->file_put_contents('foo.txt', 'bar');
  65. $view->lockFile('/foo.txt', ILockingProvider::LOCK_SHARED);
  66. $this->request($view, $user, 'pass', 'PUT', '/foo.txt', 'asd');
  67. }
  68. /**
  69. * @expectedException \OCA\DAV\Connector\Sabre\Exception\FileLocked
  70. */
  71. public function testUploadOverWriteWriteLocked() {
  72. $user = $this->getUniqueID();
  73. $view = $this->setupUser($user, 'pass');
  74. $view->file_put_contents('foo.txt', 'bar');
  75. $view->lockFile('/foo.txt', ILockingProvider::LOCK_EXCLUSIVE);
  76. $this->request($view, $user, 'pass', 'PUT', '/foo.txt', 'asd');
  77. }
  78. public function testChunkedUpload() {
  79. $user = $this->getUniqueID();
  80. $view = $this->setupUser($user, 'pass');
  81. $this->assertFalse($view->file_exists('foo.txt'));
  82. $response = $this->request($view, $user, 'pass', 'PUT', '/foo.txt-chunking-123-2-0', 'asd', ['OC-Chunked' => '1']);
  83. $this->assertEquals(201, $response->getStatus());
  84. $this->assertFalse($view->file_exists('foo.txt'));
  85. $response = $this->request($view, $user, 'pass', 'PUT', '/foo.txt-chunking-123-2-1', 'bar', ['OC-Chunked' => '1']);
  86. $this->assertEquals(Http::STATUS_CREATED, $response->getStatus());
  87. $this->assertTrue($view->file_exists('foo.txt'));
  88. $this->assertEquals('asdbar', $view->file_get_contents('foo.txt'));
  89. $info = $view->getFileInfo('foo.txt');
  90. $this->assertInstanceOf('\OC\Files\FileInfo', $info);
  91. $this->assertEquals(6, $info->getSize());
  92. }
  93. public function testChunkedUploadOverWrite() {
  94. $user = $this->getUniqueID();
  95. $view = $this->setupUser($user, 'pass');
  96. $view->file_put_contents('foo.txt', 'bar');
  97. $response = $this->request($view, $user, 'pass', 'PUT', '/foo.txt-chunking-123-2-0', 'asd', ['OC-Chunked' => '1']);
  98. $this->assertEquals(Http::STATUS_CREATED, $response->getStatus());
  99. $this->assertEquals('bar', $view->file_get_contents('foo.txt'));
  100. $response = $this->request($view, $user, 'pass', 'PUT', '/foo.txt-chunking-123-2-1', 'bar', ['OC-Chunked' => '1']);
  101. $this->assertEquals(Http::STATUS_CREATED, $response->getStatus());
  102. $this->assertEquals('asdbar', $view->file_get_contents('foo.txt'));
  103. $info = $view->getFileInfo('foo.txt');
  104. $this->assertInstanceOf('\OC\Files\FileInfo', $info);
  105. $this->assertEquals(6, $info->getSize());
  106. }
  107. public function testChunkedUploadOutOfOrder() {
  108. $user = $this->getUniqueID();
  109. $view = $this->setupUser($user, 'pass');
  110. $this->assertFalse($view->file_exists('foo.txt'));
  111. $response = $this->request($view, $user, 'pass', 'PUT', '/foo.txt-chunking-123-2-1', 'bar', ['OC-Chunked' => '1']);
  112. $this->assertEquals(Http::STATUS_CREATED, $response->getStatus());
  113. $this->assertFalse($view->file_exists('foo.txt'));
  114. $response = $this->request($view, $user, 'pass', 'PUT', '/foo.txt-chunking-123-2-0', 'asd', ['OC-Chunked' => '1']);
  115. $this->assertEquals(201, $response->getStatus());
  116. $this->assertTrue($view->file_exists('foo.txt'));
  117. $this->assertEquals('asdbar', $view->file_get_contents('foo.txt'));
  118. $info = $view->getFileInfo('foo.txt');
  119. $this->assertInstanceOf('\OC\Files\FileInfo', $info);
  120. $this->assertEquals(6, $info->getSize());
  121. }
  122. /**
  123. * @expectedException \OCA\DAV\Connector\Sabre\Exception\FileLocked
  124. */
  125. public function testChunkedUploadOutOfOrderReadLocked() {
  126. $user = $this->getUniqueID();
  127. $view = $this->setupUser($user, 'pass');
  128. $this->assertFalse($view->file_exists('foo.txt'));
  129. $view->lockFile('/foo.txt', ILockingProvider::LOCK_SHARED);
  130. try {
  131. $response = $this->request($view, $user, 'pass', 'PUT', '/foo.txt-chunking-123-2-1', 'bar', ['OC-Chunked' => '1']);
  132. } catch (\OCA\DAV\Connector\Sabre\Exception\FileLocked $e) {
  133. $this->fail('Didn\'t expect locked error for the first chunk on read lock');
  134. return;
  135. }
  136. $this->assertEquals(Http::STATUS_CREATED, $response->getStatus());
  137. $this->assertFalse($view->file_exists('foo.txt'));
  138. // last chunk should trigger the locked error since it tries to assemble
  139. $this->request($view, $user, 'pass', 'PUT', '/foo.txt-chunking-123-2-0', 'asd', ['OC-Chunked' => '1']);
  140. }
  141. /**
  142. * @expectedException \OCA\DAV\Connector\Sabre\Exception\FileLocked
  143. */
  144. public function testChunkedUploadOutOfOrderWriteLocked() {
  145. $user = $this->getUniqueID();
  146. $view = $this->setupUser($user, 'pass');
  147. $this->assertFalse($view->file_exists('foo.txt'));
  148. $view->lockFile('/foo.txt', ILockingProvider::LOCK_EXCLUSIVE);
  149. try {
  150. $response = $this->request($view, $user, 'pass', 'PUT', '/foo.txt-chunking-123-2-1', 'bar', ['OC-Chunked' => '1']);
  151. } catch (\OCA\DAV\Connector\Sabre\Exception\FileLocked $e) {
  152. $this->fail('Didn\'t expect locked error for the first chunk on write lock'); // maybe forbid this in the future for write locks only?
  153. return;
  154. }
  155. $this->assertEquals(Http::STATUS_CREATED, $response->getStatus());
  156. $this->assertFalse($view->file_exists('foo.txt'));
  157. // last chunk should trigger the locked error since it tries to assemble
  158. $this->request($view, $user, 'pass', 'PUT', '/foo.txt-chunking-123-2-0', 'asd', ['OC-Chunked' => '1']);
  159. }
  160. }