tempmanager.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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;
  20. protected function setUp() {
  21. parent::setUp();
  22. $this->baseDir = get_temp_dir() . $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. parent::tearDown();
  30. }
  31. /**
  32. * @param \OCP\ILogger $logger
  33. * @return \OC\TempManager
  34. */
  35. protected function getManager($logger = null) {
  36. if (!$logger) {
  37. $logger = new NullLogger();
  38. }
  39. return new \OC\TempManager($this->baseDir, $logger);
  40. }
  41. public function testGetFile() {
  42. $manager = $this->getManager();
  43. $file = $manager->getTemporaryFile('txt');
  44. $this->assertStringEndsWith('.txt', $file);
  45. $this->assertTrue(is_file($file));
  46. $this->assertTrue(is_writable($file));
  47. file_put_contents($file, 'bar');
  48. $this->assertEquals('bar', file_get_contents($file));
  49. }
  50. public function testGetFolder() {
  51. $manager = $this->getManager();
  52. $folder = $manager->getTemporaryFolder();
  53. $this->assertStringEndsWith('/', $folder);
  54. $this->assertTrue(is_dir($folder));
  55. $this->assertTrue(is_writable($folder));
  56. file_put_contents($folder . 'foo.txt', 'bar');
  57. $this->assertEquals('bar', file_get_contents($folder . 'foo.txt'));
  58. }
  59. public function testCleanFiles() {
  60. $manager = $this->getManager();
  61. $file1 = $manager->getTemporaryFile('txt');
  62. $file2 = $manager->getTemporaryFile('txt');
  63. $this->assertTrue(file_exists($file1));
  64. $this->assertTrue(file_exists($file2));
  65. $manager->clean();
  66. $this->assertFalse(file_exists($file1));
  67. $this->assertFalse(file_exists($file2));
  68. }
  69. public function testCleanFolder() {
  70. $manager = $this->getManager();
  71. $folder1 = $manager->getTemporaryFolder();
  72. $folder2 = $manager->getTemporaryFolder();
  73. touch($folder1 . 'foo.txt');
  74. touch($folder1 . 'bar.txt');
  75. $this->assertTrue(file_exists($folder1));
  76. $this->assertTrue(file_exists($folder2));
  77. $this->assertTrue(file_exists($folder1 . 'foo.txt'));
  78. $this->assertTrue(file_exists($folder1 . 'bar.txt'));
  79. $manager->clean();
  80. $this->assertFalse(file_exists($folder1));
  81. $this->assertFalse(file_exists($folder2));
  82. $this->assertFalse(file_exists($folder1 . 'foo.txt'));
  83. $this->assertFalse(file_exists($folder1 . 'bar.txt'));
  84. }
  85. public function testCleanOld() {
  86. $manager = $this->getManager();
  87. $oldFile = $manager->getTemporaryFile('txt');
  88. $newFile = $manager->getTemporaryFile('txt');
  89. $folder = $manager->getTemporaryFolder();
  90. $nonOcFile = $this->baseDir . '/foo.txt';
  91. file_put_contents($nonOcFile, 'bar');
  92. $past = time() - 2 * 3600;
  93. touch($oldFile, $past);
  94. touch($folder, $past);
  95. touch($nonOcFile, $past);
  96. $manager2 = $this->getManager();
  97. $manager2->cleanOld();
  98. $this->assertFalse(file_exists($oldFile));
  99. $this->assertFalse(file_exists($folder));
  100. $this->assertTrue(file_exists($nonOcFile));
  101. $this->assertTrue(file_exists($newFile));
  102. }
  103. public function testLogCantCreateFile() {
  104. if (\OC_Util::runningOnWindows()) {
  105. $this->markTestSkipped('[Windows] chmod() does not work as intended on Windows.');
  106. }
  107. $logger = $this->getMock('\Test\NullLogger');
  108. $manager = $this->getManager($logger);
  109. chmod($this->baseDir, 0500);
  110. $logger->expects($this->once())
  111. ->method('warning')
  112. ->with($this->stringContains('Can not create a temporary file in directory'));
  113. $this->assertFalse($manager->getTemporaryFile('txt'));
  114. }
  115. public function testLogCantCreateFolder() {
  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 folder in directory'));
  125. $this->assertFalse($manager->getTemporaryFolder());
  126. }
  127. public function testGeneratePathTraversal() {
  128. $logger = $this->getMock('\Test\NullLogger');
  129. $tmpManager = \Test_Helper::invokePrivate(
  130. $this->getManager($logger),
  131. 'generatePath',
  132. ['../Traversal\\../FileName']
  133. );
  134. $this->assertStringEndsNotWith('./Traversal\\../FileName', $tmpManager);
  135. $this->assertStringEndsWith('.Traversal..FileName', $tmpManager);
  136. }
  137. }