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.

stream.php 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. /**
  3. * @author Björn Schießle <schiessle@owncloud.com>
  4. * @author Christopher Schäpers <kondou@ts.unde.re>
  5. * @author Florin Peter <github@florin-peter.de>
  6. * @author Joas Schilling <nickvergessen@owncloud.com>
  7. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Thomas Müller <thomas.mueller@tmit.eu>
  10. *
  11. * @copyright Copyright (c) 2015, ownCloud, Inc.
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OCA\Files_Encryption\Tests;
  28. /**
  29. * Class Stream
  30. * this class provide basic stream tests
  31. */
  32. class Stream extends TestCase {
  33. const TEST_ENCRYPTION_STREAM_USER1 = "test-stream-user1";
  34. public $userId;
  35. public $pass;
  36. /**
  37. * @var \OC\Files\View
  38. */
  39. public $view;
  40. public $dataShort;
  41. public $stateFilesTrashbin;
  42. public static function setUpBeforeClass() {
  43. parent::setUpBeforeClass();
  44. // create test user
  45. self::loginHelper(self::TEST_ENCRYPTION_STREAM_USER1, true);
  46. }
  47. protected function setUp() {
  48. parent::setUp();
  49. // set user id
  50. \OC_User::setUserId(self::TEST_ENCRYPTION_STREAM_USER1);
  51. $this->userId = self::TEST_ENCRYPTION_STREAM_USER1;
  52. $this->pass = self::TEST_ENCRYPTION_STREAM_USER1;
  53. // init filesystem view
  54. $this->view = new \OC\Files\View('/');
  55. // init short data
  56. $this->dataShort = 'hats';
  57. // remember files_trashbin state
  58. $this->stateFilesTrashbin = \OC_App::isEnabled('files_trashbin');
  59. // we don't want to tests with app files_trashbin enabled
  60. \OC_App::disable('files_trashbin');
  61. }
  62. protected function tearDown() {
  63. // reset app files_trashbin
  64. if ($this->stateFilesTrashbin) {
  65. \OC_App::enable('files_trashbin');
  66. }
  67. else {
  68. \OC_App::disable('files_trashbin');
  69. }
  70. parent::tearDown();
  71. }
  72. public static function tearDownAfterClass() {
  73. // cleanup test user
  74. \OC_User::deleteUser(self::TEST_ENCRYPTION_STREAM_USER1);
  75. parent::tearDownAfterClass();
  76. }
  77. function testStreamOptions() {
  78. $filename = '/tmp-' . $this->getUniqueID();
  79. $view = new \OC\Files\View('/' . $this->userId . '/files');
  80. // Save short data as encrypted file using stream wrapper
  81. $cryptedFile = $view->file_put_contents($filename, $this->dataShort);
  82. // Test that data was successfully written
  83. $this->assertTrue(is_int($cryptedFile));
  84. $handle = $view->fopen($filename, 'r');
  85. // check if stream is at position zero
  86. $this->assertEquals(0, ftell($handle));
  87. // set stream options
  88. $this->assertTrue(flock($handle, LOCK_SH));
  89. $this->assertTrue(flock($handle, LOCK_UN));
  90. fclose($handle);
  91. // tear down
  92. $view->unlink($filename);
  93. }
  94. function testStreamSetBlocking() {
  95. $filename = '/tmp-' . $this->getUniqueID();
  96. $view = new \OC\Files\View('/' . $this->userId . '/files');
  97. // Save short data as encrypted file using stream wrapper
  98. $cryptedFile = $view->file_put_contents($filename, $this->dataShort);
  99. // Test that data was successfully written
  100. $this->assertTrue(is_int($cryptedFile));
  101. $handle = $view->fopen($filename, 'r');
  102. if (\OC_Util::runningOnWindows()) {
  103. fclose($handle);
  104. $view->unlink($filename);
  105. $this->markTestSkipped('[Windows] stream_set_blocking() does not work as expected on Windows.');
  106. }
  107. // set stream options
  108. $this->assertTrue(stream_set_blocking($handle, 1));
  109. fclose($handle);
  110. // tear down
  111. $view->unlink($filename);
  112. }
  113. /**
  114. * @medium
  115. */
  116. function testStreamSetTimeout() {
  117. $filename = '/tmp-' . $this->getUniqueID();
  118. $view = new \OC\Files\View('/' . $this->userId . '/files');
  119. // Save short data as encrypted file using stream wrapper
  120. $cryptedFile = $view->file_put_contents($filename, $this->dataShort);
  121. // Test that data was successfully written
  122. $this->assertTrue(is_int($cryptedFile));
  123. $handle = $view->fopen($filename, 'r');
  124. // set stream options
  125. $this->assertFalse(stream_set_timeout($handle, 1));
  126. fclose($handle);
  127. // tear down
  128. $view->unlink($filename);
  129. }
  130. function testStreamSetWriteBuffer() {
  131. $filename = '/tmp-' . $this->getUniqueID();
  132. $view = new \OC\Files\View('/' . $this->userId . '/files');
  133. // Save short data as encrypted file using stream wrapper
  134. $cryptedFile = $view->file_put_contents($filename, $this->dataShort);
  135. // Test that data was successfully written
  136. $this->assertTrue(is_int($cryptedFile));
  137. $handle = $view->fopen($filename, 'r');
  138. // set stream options
  139. $this->assertEquals(0, stream_set_write_buffer($handle, 1024));
  140. fclose($handle);
  141. // tear down
  142. $view->unlink($filename);
  143. }
  144. /**
  145. * @medium
  146. * test if stream wrapper can read files outside from the data folder
  147. */
  148. function testStreamFromLocalFile() {
  149. $filename = '/' . $this->userId . '/files/' . 'tmp-' . $this->getUniqueID().'.txt';
  150. $tmpFilename = "/tmp/" . $this->getUniqueID() . ".txt";
  151. // write an encrypted file
  152. $cryptedFile = $this->view->file_put_contents($filename, $this->dataShort);
  153. // Test that data was successfully written
  154. $this->assertTrue(is_int($cryptedFile));
  155. // create a copy outside of the data folder in /tmp
  156. $proxyStatus = \OC_FileProxy::$enabled;
  157. \OC_FileProxy::$enabled = false;
  158. $encryptedContent = $this->view->file_get_contents($filename);
  159. \OC_FileProxy::$enabled = $proxyStatus;
  160. file_put_contents($tmpFilename, $encryptedContent);
  161. \OCA\Files_Encryption\Helper::addTmpFileToMapper($tmpFilename, $filename);
  162. // try to read the file from /tmp
  163. $handle = fopen("crypt://".$tmpFilename, "r");
  164. $contentFromTmpFile = stream_get_contents($handle);
  165. // check if it was successful
  166. $this->assertEquals($this->dataShort, $contentFromTmpFile);
  167. fclose($handle);
  168. // clean up
  169. unlink($tmpFilename);
  170. $this->view->unlink($filename);
  171. }
  172. }