1
0

TempManagerTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 bantu\IniGetWrapper\IniGetWrapper;
  10. use OCP\IConfig;
  11. use Psr\Log\LoggerInterface;
  12. class TempManagerTest extends \Test\TestCase {
  13. protected $baseDir = null;
  14. protected function setUp(): void {
  15. parent::setUp();
  16. $this->baseDir = $this->getManager()->getTempBaseDir() . $this->getUniqueID('/oc_tmp_test');
  17. if (!is_dir($this->baseDir)) {
  18. mkdir($this->baseDir);
  19. }
  20. }
  21. protected function tearDown(): void {
  22. if ($this->baseDir !== null) {
  23. \OC_Helper::rmdirr($this->baseDir);
  24. }
  25. $this->baseDir = null;
  26. parent::tearDown();
  27. }
  28. /**
  29. * @param ?LoggerInterface $logger
  30. * @param ?IConfig $config
  31. * @return \OC\TempManager
  32. */
  33. protected function getManager($logger = null, $config = null) {
  34. if (!$logger) {
  35. $logger = $this->createMock(LoggerInterface::class);
  36. }
  37. if (!$config) {
  38. $config = $this->createMock(IConfig::class);
  39. $config->method('getSystemValue')
  40. ->with('tempdirectory', null)
  41. ->willReturn('/tmp');
  42. }
  43. $iniGetWrapper = $this->createMock(IniGetWrapper::class);
  44. $manager = new \OC\TempManager($logger, $config, $iniGetWrapper);
  45. if ($this->baseDir) {
  46. $manager->overrideTempBaseDir($this->baseDir);
  47. }
  48. return $manager;
  49. }
  50. public function testGetFile() {
  51. $manager = $this->getManager();
  52. $file = $manager->getTemporaryFile('txt');
  53. $this->assertStringEndsWith('.txt', $file);
  54. $this->assertTrue(is_file($file));
  55. $this->assertTrue(is_writable($file));
  56. file_put_contents($file, 'bar');
  57. $this->assertEquals('bar', file_get_contents($file));
  58. }
  59. public function testGetFolder() {
  60. $manager = $this->getManager();
  61. $folder = $manager->getTemporaryFolder();
  62. $this->assertStringEndsWith('/', $folder);
  63. $this->assertTrue(is_dir($folder));
  64. $this->assertTrue(is_writable($folder));
  65. file_put_contents($folder . 'foo.txt', 'bar');
  66. $this->assertEquals('bar', file_get_contents($folder . 'foo.txt'));
  67. }
  68. public function testCleanFiles() {
  69. $manager = $this->getManager();
  70. $file1 = $manager->getTemporaryFile('txt');
  71. $file2 = $manager->getTemporaryFile('txt');
  72. $this->assertTrue(file_exists($file1));
  73. $this->assertTrue(file_exists($file2));
  74. $manager->clean();
  75. $this->assertFalse(file_exists($file1));
  76. $this->assertFalse(file_exists($file2));
  77. }
  78. public function testCleanFolder() {
  79. $manager = $this->getManager();
  80. $folder1 = $manager->getTemporaryFolder();
  81. $folder2 = $manager->getTemporaryFolder();
  82. touch($folder1 . 'foo.txt');
  83. touch($folder1 . 'bar.txt');
  84. $this->assertTrue(file_exists($folder1));
  85. $this->assertTrue(file_exists($folder2));
  86. $this->assertTrue(file_exists($folder1 . 'foo.txt'));
  87. $this->assertTrue(file_exists($folder1 . 'bar.txt'));
  88. $manager->clean();
  89. $this->assertFalse(file_exists($folder1));
  90. $this->assertFalse(file_exists($folder2));
  91. $this->assertFalse(file_exists($folder1 . 'foo.txt'));
  92. $this->assertFalse(file_exists($folder1 . 'bar.txt'));
  93. }
  94. public function testCleanOld() {
  95. $manager = $this->getManager();
  96. $oldFile = $manager->getTemporaryFile('txt');
  97. $newFile = $manager->getTemporaryFile('txt');
  98. $folder = $manager->getTemporaryFolder();
  99. $nonOcFile = $this->baseDir . '/foo.txt';
  100. file_put_contents($nonOcFile, 'bar');
  101. $past = time() - 2 * 3600;
  102. touch($oldFile, $past);
  103. touch($folder, $past);
  104. touch($nonOcFile, $past);
  105. $manager2 = $this->getManager();
  106. $manager2->cleanOld();
  107. $this->assertFalse(file_exists($oldFile));
  108. $this->assertFalse(file_exists($folder));
  109. $this->assertTrue(file_exists($nonOcFile));
  110. $this->assertTrue(file_exists($newFile));
  111. }
  112. public function testLogCantCreateFile() {
  113. $this->markTestSkipped('TODO: Disable because fails on drone');
  114. $logger = $this->createMock(LoggerInterface::class);
  115. $manager = $this->getManager($logger);
  116. chmod($this->baseDir, 0500);
  117. $logger->expects($this->once())
  118. ->method('warning')
  119. ->with($this->stringContains('Can not create a temporary file in directory'));
  120. $this->assertFalse($manager->getTemporaryFile('txt'));
  121. }
  122. public function testLogCantCreateFolder() {
  123. $this->markTestSkipped('TODO: Disable because fails on drone');
  124. $logger = $this->createMock(LoggerInterface::class);
  125. $manager = $this->getManager($logger);
  126. chmod($this->baseDir, 0500);
  127. $logger->expects($this->once())
  128. ->method('warning')
  129. ->with($this->stringContains('Can not create a temporary folder in directory'));
  130. $this->assertFalse($manager->getTemporaryFolder());
  131. }
  132. public function testBuildFileNameWithPostfix() {
  133. $logger = $this->createMock(LoggerInterface::class);
  134. $tmpManager = self::invokePrivate(
  135. $this->getManager($logger),
  136. 'buildFileNameWithSuffix',
  137. ['/tmp/myTemporaryFile', 'postfix']
  138. );
  139. $this->assertEquals('/tmp/myTemporaryFile-.postfix', $tmpManager);
  140. }
  141. public function testBuildFileNameWithoutPostfix() {
  142. $logger = $this->createMock(LoggerInterface::class);
  143. $tmpManager = self::invokePrivate(
  144. $this->getManager($logger),
  145. 'buildFileNameWithSuffix',
  146. ['/tmp/myTemporaryFile', '']
  147. );
  148. $this->assertEquals('/tmp/myTemporaryFile', $tmpManager);
  149. }
  150. public function testBuildFileNameWithSuffixPathTraversal() {
  151. $logger = $this->createMock(LoggerInterface::class);
  152. $tmpManager = self::invokePrivate(
  153. $this->getManager($logger),
  154. 'buildFileNameWithSuffix',
  155. ['foo', '../Traversal\\../FileName']
  156. );
  157. $this->assertStringEndsNotWith('./Traversal\\../FileName', $tmpManager);
  158. $this->assertStringEndsWith('.Traversal..FileName', $tmpManager);
  159. }
  160. public function testGetTempBaseDirFromConfig() {
  161. $dir = $this->getManager()->getTemporaryFolder();
  162. $config = $this->createMock(IConfig::class);
  163. $config->expects($this->once())
  164. ->method('getSystemValue')
  165. ->with('tempdirectory', null)
  166. ->willReturn($dir);
  167. $this->baseDir = null; // prevent override
  168. $tmpManager = $this->getManager(null, $config);
  169. $this->assertEquals($dir, $tmpManager->getTempBaseDir());
  170. }
  171. }