FilesTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /**
  3. * @author Robin McCorkell <rmccorkell@karoshi.org.uk>
  4. *
  5. * @copyright Copyright (c) 2015, ownCloud, Inc.
  6. * @license AGPL-3.0
  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. namespace Test;
  23. class FilesTest extends \Test\TestCase {
  24. const UPLOAD_LIMIT_DEFAULT_STR = '511M';
  25. const UPLOAD_LIMIT_SETTING_STR = '2M';
  26. const UPLOAD_LIMIT_SETTING_BYTES = 2097152;
  27. /** @var array $tmpDirs */
  28. private $tmpDirs = [];
  29. /**
  30. * @return array
  31. */
  32. private function getUploadLimitTestFiles() {
  33. $dir = \OC::$server->getTempManager()->getTemporaryFolder();
  34. $this->tmpDirs[] = $dir;
  35. $result = [
  36. '.htaccess' => $dir . '/htaccess',
  37. '.user.ini' => $dir . '/user.ini'
  38. ];
  39. copy(\OC::$SERVERROOT . '/tests/data/setUploadLimit/htaccess', $result['.htaccess']);
  40. copy(\OC::$SERVERROOT . '/tests/data/setUploadLimit/user.ini', $result['.user.ini']);
  41. return $result;
  42. }
  43. protected function tearDown() {
  44. foreach ($this->tmpDirs as $dir) {
  45. \OC_Helper::rmdirr($dir);
  46. }
  47. parent::tearDown();
  48. }
  49. public function testSetUploadLimitSizeSanity() {
  50. $this->assertFalse(\OC_Files::setUploadLimit(PHP_INT_MAX + 10));
  51. $this->assertFalse(\OC_Files::setUploadLimit(\OC_Files::UPLOAD_MIN_LIMIT_BYTES - 10));
  52. $this->assertFalse(\OC_Files::setUploadLimit('foobar'));
  53. }
  54. public function setUploadLimitWriteProvider() {
  55. return [
  56. [
  57. // both files writable
  58. true, true,
  59. self::UPLOAD_LIMIT_SETTING_BYTES, self::UPLOAD_LIMIT_SETTING_BYTES,
  60. self::UPLOAD_LIMIT_SETTING_STR, self::UPLOAD_LIMIT_SETTING_STR
  61. ],
  62. [
  63. // neither file writable
  64. false, false,
  65. self::UPLOAD_LIMIT_SETTING_BYTES, false,
  66. self::UPLOAD_LIMIT_DEFAULT_STR, self::UPLOAD_LIMIT_DEFAULT_STR
  67. ],
  68. [
  69. // only .htaccess writable
  70. true, false,
  71. self::UPLOAD_LIMIT_SETTING_BYTES, false,
  72. self::UPLOAD_LIMIT_SETTING_STR, self::UPLOAD_LIMIT_DEFAULT_STR
  73. ],
  74. [
  75. // only .user.ini writable
  76. false, true,
  77. self::UPLOAD_LIMIT_SETTING_BYTES, false,
  78. self::UPLOAD_LIMIT_DEFAULT_STR, self::UPLOAD_LIMIT_SETTING_STR
  79. ],
  80. [
  81. // test rounding of values
  82. true, true,
  83. self::UPLOAD_LIMIT_SETTING_BYTES + 20, self::UPLOAD_LIMIT_SETTING_BYTES,
  84. self::UPLOAD_LIMIT_SETTING_STR, self::UPLOAD_LIMIT_SETTING_STR
  85. ]
  86. ];
  87. }
  88. /**
  89. * @dataProvider setUploadLimitWriteProvider
  90. */
  91. public function testSetUploadLimitWrite(
  92. $htaccessWritable, $userIniWritable,
  93. $setSize, $expectedSize,
  94. $htaccessStr, $userIniStr
  95. ) {
  96. $this->markTestSkipped('TODO: Disable because fails on drone');
  97. $files = $this->getUploadLimitTestFiles();
  98. chmod($files['.htaccess'], ($htaccessWritable ? 0644 : 0444));
  99. chmod($files['.user.ini'], ($userIniWritable ? 0644 : 0444));
  100. $htaccessSize = filesize($files['.htaccess']);
  101. $userIniSize = filesize($files['.user.ini']);
  102. $htaccessSizeMod = 2*(strlen($htaccessStr) - strlen(self::UPLOAD_LIMIT_DEFAULT_STR));
  103. $userIniSizeMod = 2*(strlen($userIniStr) - strlen(self::UPLOAD_LIMIT_DEFAULT_STR));
  104. $this->assertEquals($expectedSize, \OC_Files::setUploadLimit($setSize, $files));
  105. // check file contents
  106. $htaccess = file_get_contents($files['.htaccess']);
  107. $this->assertEquals(1,
  108. preg_match('/php_value upload_max_filesize '.$htaccessStr.'/', $htaccess)
  109. );
  110. $this->assertEquals(1,
  111. preg_match('/php_value post_max_size '.$htaccessStr.'/', $htaccess)
  112. );
  113. $this->assertEquals($htaccessSize + $htaccessSizeMod, filesize($files['.htaccess']));
  114. $userIni = file_get_contents($files['.user.ini']);
  115. $this->assertEquals(1,
  116. preg_match('/upload_max_filesize='.$userIniStr.'/', $userIni)
  117. );
  118. $this->assertEquals(1,
  119. preg_match('/post_max_size='.$userIniStr.'/', $userIni)
  120. );
  121. $this->assertEquals($userIniSize + $userIniSizeMod, filesize($files['.user.ini']));
  122. }
  123. }