stream.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * @author Florin Peter
  6. * @copyright 2013 Florin Peter <owncloud@florin-peter.de>
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public
  19. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. require_once __DIR__ . '/../../../lib/base.php';
  23. require_once __DIR__ . '/../lib/crypt.php';
  24. require_once __DIR__ . '/../lib/keymanager.php';
  25. require_once __DIR__ . '/../lib/proxy.php';
  26. require_once __DIR__ . '/../lib/stream.php';
  27. require_once __DIR__ . '/../lib/util.php';
  28. require_once __DIR__ . '/../appinfo/app.php';
  29. require_once __DIR__ . '/util.php';
  30. use OCA\Encryption;
  31. /**
  32. * Class Test_Encryption_Stream
  33. * @brief this class provide basic stream tests
  34. */
  35. class Test_Encryption_Stream extends \PHPUnit_Framework_TestCase {
  36. const TEST_ENCRYPTION_STREAM_USER1 = "test-stream-user1";
  37. public $userId;
  38. public $pass;
  39. /**
  40. * @var \OC_FilesystemView
  41. */
  42. public $view;
  43. public $dataShort;
  44. public $stateFilesTrashbin;
  45. public static function setUpBeforeClass() {
  46. // reset backend
  47. \OC_User::clearBackends();
  48. \OC_User::useBackend('database');
  49. // Filesystem related hooks
  50. \OCA\Encryption\Helper::registerFilesystemHooks();
  51. // clear and register hooks
  52. \OC_FileProxy::clearProxies();
  53. \OC_FileProxy::register(new OCA\Encryption\Proxy());
  54. // create test user
  55. \Test_Encryption_Util::loginHelper(\Test_Encryption_Stream::TEST_ENCRYPTION_STREAM_USER1, true);
  56. }
  57. function setUp() {
  58. // set user id
  59. \OC_User::setUserId(\Test_Encryption_Stream::TEST_ENCRYPTION_STREAM_USER1);
  60. $this->userId = \Test_Encryption_Stream::TEST_ENCRYPTION_STREAM_USER1;
  61. $this->pass = \Test_Encryption_Stream::TEST_ENCRYPTION_STREAM_USER1;
  62. // init filesystem view
  63. $this->view = new \OC_FilesystemView('/');
  64. // init short data
  65. $this->dataShort = 'hats';
  66. // remember files_trashbin state
  67. $this->stateFilesTrashbin = OC_App::isEnabled('files_trashbin');
  68. // we don't want to tests with app files_trashbin enabled
  69. \OC_App::disable('files_trashbin');
  70. }
  71. function tearDown() {
  72. // reset app files_trashbin
  73. if ($this->stateFilesTrashbin) {
  74. OC_App::enable('files_trashbin');
  75. }
  76. else {
  77. OC_App::disable('files_trashbin');
  78. }
  79. }
  80. public static function tearDownAfterClass() {
  81. // cleanup test user
  82. \OC_User::deleteUser(\Test_Encryption_Stream::TEST_ENCRYPTION_STREAM_USER1);
  83. }
  84. function testStreamOptions() {
  85. $filename = '/tmp-' . time();
  86. $view = new \OC\Files\View('/' . $this->userId . '/files');
  87. // Save short data as encrypted file using stream wrapper
  88. $cryptedFile = $view->file_put_contents($filename, $this->dataShort);
  89. // Test that data was successfully written
  90. $this->assertTrue(is_int($cryptedFile));
  91. $handle = $view->fopen($filename, 'r');
  92. // check if stream is at position zero
  93. $this->assertEquals(0, ftell($handle));
  94. // set stream options
  95. $this->assertTrue(flock($handle, LOCK_SH));
  96. $this->assertTrue(flock($handle, LOCK_UN));
  97. // tear down
  98. $view->unlink($filename);
  99. }
  100. function testStreamSetBlocking() {
  101. $filename = '/tmp-' . time();
  102. $view = new \OC\Files\View('/' . $this->userId . '/files');
  103. // Save short data as encrypted file using stream wrapper
  104. $cryptedFile = $view->file_put_contents($filename, $this->dataShort);
  105. // Test that data was successfully written
  106. $this->assertTrue(is_int($cryptedFile));
  107. $handle = $view->fopen($filename, 'r');
  108. // set stream options
  109. $this->assertTrue(stream_set_blocking($handle, 1));
  110. // tear down
  111. $view->unlink($filename);
  112. }
  113. /**
  114. * @medium
  115. */
  116. function testStreamSetTimeout() {
  117. $filename = '/tmp-' . time();
  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. // tear down
  127. $view->unlink($filename);
  128. }
  129. function testStreamSetWriteBuffer() {
  130. $filename = '/tmp-' . time();
  131. $view = new \OC\Files\View('/' . $this->userId . '/files');
  132. // Save short data as encrypted file using stream wrapper
  133. $cryptedFile = $view->file_put_contents($filename, $this->dataShort);
  134. // Test that data was successfully written
  135. $this->assertTrue(is_int($cryptedFile));
  136. $handle = $view->fopen($filename, 'r');
  137. // set stream options
  138. $this->assertEquals(0, stream_set_write_buffer($handle, 1024));
  139. // tear down
  140. $view->unlink($filename);
  141. }
  142. }