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.

proxy.php 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. /**
  3. * @author Björn Schießle <schiessle@owncloud.com>
  4. * @author Joas Schilling <nickvergessen@owncloud.com>
  5. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Sam Tuke <mail@samtuke.com>
  8. * @author Thomas Müller <thomas.mueller@tmit.eu>
  9. * @author Vincent Petry <pvince81@owncloud.com>
  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 Proxy
  30. * this class provide basic proxy app tests
  31. */
  32. class Proxy extends TestCase {
  33. const TEST_ENCRYPTION_PROXY_USER1 = "test-proxy-user1";
  34. public $userId;
  35. public $pass;
  36. /**
  37. * @var \OC\Files\View
  38. */
  39. public $view; // view in /data/user/files
  40. public $rootView; // view on /data/user
  41. public $data;
  42. public $dataLong;
  43. public $filename;
  44. public static function setUpBeforeClass() {
  45. parent::setUpBeforeClass();
  46. // create test user
  47. self::loginHelper(self::TEST_ENCRYPTION_PROXY_USER1, true);
  48. }
  49. protected function setUp() {
  50. parent::setUp();
  51. // set user id
  52. \OC_User::setUserId(self::TEST_ENCRYPTION_PROXY_USER1);
  53. $this->userId = self::TEST_ENCRYPTION_PROXY_USER1;
  54. $this->pass = self::TEST_ENCRYPTION_PROXY_USER1;
  55. // init filesystem view
  56. $this->view = new \OC\Files\View('/'. self::TEST_ENCRYPTION_PROXY_USER1 . '/files');
  57. $this->rootView = new \OC\Files\View('/'. self::TEST_ENCRYPTION_PROXY_USER1 );
  58. // init short data
  59. $this->data = 'hats';
  60. $this->dataLong = file_get_contents(__DIR__ . '/../lib/crypt.php');
  61. $this->filename = 'enc_proxy_tests-' . $this->getUniqueID() . '.txt';
  62. }
  63. public static function tearDownAfterClass() {
  64. // cleanup test user
  65. \OC_User::deleteUser(self::TEST_ENCRYPTION_PROXY_USER1);
  66. parent::tearDownAfterClass();
  67. }
  68. /**
  69. * @medium
  70. * test if postFileSize returns the unencrypted file size
  71. */
  72. function testPostFileSize() {
  73. $this->view->file_put_contents($this->filename, $this->dataLong);
  74. $size = strlen($this->dataLong);
  75. \OC_FileProxy::$enabled = false;
  76. $encryptedSize = $this->view->filesize($this->filename);
  77. \OC_FileProxy::$enabled = true;
  78. $unencryptedSize = $this->view->filesize($this->filename);
  79. $this->assertTrue($encryptedSize > $unencryptedSize);
  80. $this->assertSame($size, $unencryptedSize);
  81. // cleanup
  82. $this->view->unlink($this->filename);
  83. }
  84. function testPostFileSizeWithDirectory() {
  85. $this->view->file_put_contents($this->filename, $this->data);
  86. \OC_FileProxy::$enabled = false;
  87. // get root size, must match the file's unencrypted size
  88. $unencryptedSize = $this->view->filesize('');
  89. \OC_FileProxy::$enabled = true;
  90. $encryptedSize = $this->view->filesize('');
  91. $this->assertTrue($encryptedSize !== $unencryptedSize);
  92. // cleanup
  93. $this->view->unlink($this->filename);
  94. }
  95. /**
  96. * @dataProvider isExcludedPathProvider
  97. */
  98. function testIsExcludedPath($path, $expected) {
  99. $this->view->mkdir(dirname($path));
  100. $this->view->file_put_contents($path, "test");
  101. $result = \Test_Helper::invokePrivate(new \OCA\Files_Encryption\Proxy(), 'isExcludedPath', array($path));
  102. $this->assertSame($expected, $result);
  103. $this->view->deleteAll(dirname($path));
  104. }
  105. public function isExcludedPathProvider() {
  106. return array(
  107. array ('/' . self::TEST_ENCRYPTION_PROXY_USER1 . '/files/test.txt', false),
  108. array (self::TEST_ENCRYPTION_PROXY_USER1 . '/files/test.txt', false),
  109. array ('/files/test.txt', true),
  110. array ('/' . self::TEST_ENCRYPTION_PROXY_USER1 . '/files/versions/test.txt', false),
  111. array ('/' . self::TEST_ENCRYPTION_PROXY_USER1 . '/files_versions/test.txt', false),
  112. array ('/' . self::TEST_ENCRYPTION_PROXY_USER1 . '/files_trashbin/test.txt', true),
  113. array ('/' . self::TEST_ENCRYPTION_PROXY_USER1 . '/file/test.txt', true),
  114. );
  115. }
  116. }