UploadTest.php 7.4 KB

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