tempmanager.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?php
  2. /**
  3. * Copyright (c) 2014 Robin Appelman <icewind@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. namespace Test;
  9. use OC\Log;
  10. class NullLogger extends Log {
  11. public function __construct($logger = null) {
  12. //disable original constructor
  13. }
  14. public function log($level, $message, array $context = array()) {
  15. //noop
  16. }
  17. }
  18. class TempManager extends \Test\TestCase {
  19. protected $baseDir = null;
  20. protected function setUp() {
  21. parent::setUp();
  22. $this->baseDir = $this->getManager()->getTempBaseDir() . $this->getUniqueID('/oc_tmp_test');
  23. if (!is_dir($this->baseDir)) {
  24. mkdir($this->baseDir);
  25. }
  26. }
  27. protected function tearDown() {
  28. \OC_Helper::rmdirr($this->baseDir);
  29. $this->baseDir = null;
  30. parent::tearDown();
  31. }
  32. /**
  33. * @param \OCP\ILogger $logger
  34. * @param \OCP\IConfig $config
  35. * @return \OC\TempManager
  36. */
  37. protected function getManager($logger = null, $config = null) {
  38. if (!$logger) {
  39. $logger = new NullLogger();
  40. }
  41. if (!$config) {
  42. $config = $this->getMock('\OCP\IConfig');
  43. $config->method('getSystemValue')
  44. ->with('tempdirectory', null)
  45. ->willReturn('/tmp');
  46. }
  47. $manager = new \OC\TempManager($logger, $config);
  48. if ($this->baseDir) {
  49. $manager->overrideTempBaseDir($this->baseDir);
  50. }
  51. return $manager;
  52. }
  53. public function testGetFile() {
  54. $manager = $this->getManager();
  55. $file = $manager->getTemporaryFile('txt');
  56. $this->assertStringEndsWith('.txt', $file);
  57. $this->assertTrue(is_file($file));
  58. $this->assertTrue(is_writable($file));
  59. file_put_contents($file, 'bar');
  60. $this->assertEquals('bar', file_get_contents($file));
  61. }
  62. public function testGetFolder() {
  63. $manager = $this->getManager();
  64. $folder = $manager->getTemporaryFolder();
  65. $this->assertStringEndsWith('/', $folder);
  66. $this->assertTrue(is_dir($folder));
  67. $this->assertTrue(is_writable($folder));
  68. file_put_contents($folder . 'foo.txt', 'bar');
  69. $this->assertEquals('bar', file_get_contents($folder . 'foo.txt'));
  70. }
  71. public function testCleanFiles() {
  72. $manager = $this->getManager();
  73. $file1 = $manager->getTemporaryFile('txt');
  74. $file2 = $manager->getTemporaryFile('txt');
  75. $this->assertTrue(file_exists($file1));
  76. $this->assertTrue(file_exists($file2));
  77. $manager->clean();
  78. $this->assertFalse(file_exists($file1));
  79. $this->assertFalse(file_exists($file2));
  80. }
  81. public function testCleanFolder() {
  82. $manager = $this->getManager();
  83. $folder1 = $manager->getTemporaryFolder();
  84. $folder2 = $manager->getTemporaryFolder();
  85. touch($folder1 . 'foo.txt');
  86. touch($folder1 . 'bar.txt');
  87. $this->assertTrue(file_exists($folder1));
  88. $this->assertTrue(file_exists($folder2));
  89. $this->assertTrue(file_exists($folder1 . 'foo.txt'));
  90. $this->assertTrue(file_exists($folder1 . 'bar.txt'));
  91. $manager->clean();
  92. $this->assertFalse(file_exists($folder1));
  93. $this->assertFalse(file_exists($folder2));
  94. $this->assertFalse(file_exists($folder1 . 'foo.txt'));
  95. $this->assertFalse(file_exists($folder1 . 'bar.txt'));
  96. }
  97. public function testCleanOld() {
  98. $manager = $this->getManager();
  99. $oldFile = $manager->getTemporaryFile('txt');
  100. $newFile = $manager->getTemporaryFile('txt');
  101. $folder = $manager->getTemporaryFolder();
  102. $nonOcFile = $this->baseDir . '/foo.txt';
  103. file_put_contents($nonOcFile, 'bar');
  104. $past = time() - 2 * 3600;
  105. touch($oldFile, $past);
  106. touch($folder, $past);
  107. touch($nonOcFile, $past);
  108. $manager2 = $this->getManager();
  109. $manager2->cleanOld();
  110. $this->assertFalse(file_exists($oldFile));
  111. $this->assertFalse(file_exists($folder));
  112. $this->assertTrue(file_exists($nonOcFile));
  113. $this->assertTrue(file_exists($newFile));
  114. }
  115. public function testLogCantCreateFile() {
  116. if (\OC_Util::runningOnWindows()) {
  117. $this->markTestSkipped('[Windows] chmod() does not work as intended on Windows.');
  118. }
  119. $logger = $this->getMock('\Test\NullLogger');
  120. $manager = $this->getManager($logger);
  121. chmod($this->baseDir, 0500);
  122. $logger->expects($this->once())
  123. ->method('warning')
  124. ->with($this->stringContains('Can not create a temporary file in directory'));
  125. $this->assertFalse($manager->getTemporaryFile('txt'));
  126. }
  127. public function testLogCantCreateFolder() {
  128. if (\OC_Util::runningOnWindows()) {
  129. $this->markTestSkipped('[Windows] chmod() does not work as intended on Windows.');
  130. }
  131. $logger = $this->getMock('\Test\NullLogger');
  132. $manager = $this->getManager($logger);
  133. chmod($this->baseDir, 0500);
  134. $logger->expects($this->once())
  135. ->method('warning')
  136. ->with($this->stringContains('Can not create a temporary folder in directory'));
  137. $this->assertFalse($manager->getTemporaryFolder());
  138. }
  139. public function testBuildFileNameWithPostfix() {
  140. $logger = $this->getMock('\Test\NullLogger');
  141. $tmpManager = self::invokePrivate(
  142. $this->getManager($logger),
  143. 'buildFileNameWithSuffix',
  144. ['/tmp/myTemporaryFile', 'postfix']
  145. );
  146. $this->assertEquals('/tmp/myTemporaryFile-.postfix', $tmpManager);
  147. }
  148. public function testBuildFileNameWithoutPostfix() {
  149. $logger = $this->getMock('\Test\NullLogger');
  150. $tmpManager = self::invokePrivate(
  151. $this->getManager($logger),
  152. 'buildFileNameWithSuffix',
  153. ['/tmp/myTemporaryFile', '']
  154. );
  155. $this->assertEquals('/tmp/myTemporaryFile', $tmpManager);
  156. }
  157. public function testBuildFileNameWithSuffixPathTraversal() {
  158. $logger = $this->getMock('\Test\NullLogger');
  159. $tmpManager = self::invokePrivate(
  160. $this->getManager($logger),
  161. 'buildFileNameWithSuffix',
  162. ['foo', '../Traversal\\../FileName']
  163. );
  164. $this->assertStringEndsNotWith('./Traversal\\../FileName', $tmpManager);
  165. $this->assertStringEndsWith('.Traversal..FileName', $tmpManager);
  166. }
  167. public function testGetTempBaseDirFromConfig() {
  168. $dir = $this->getManager()->getTemporaryFolder();
  169. $config = $this->getMock('\OCP\IConfig');
  170. $config->expects($this->once())
  171. ->method('getSystemValue')
  172. ->with('tempdirectory', null)
  173. ->willReturn($dir);
  174. $this->baseDir = null; // prevent override
  175. $tmpManager = $this->getManager(null, $config);
  176. $this->assertEquals($dir, $tmpManager->getTempBaseDir());
  177. }
  178. }