TempManagerTest.php 5.9 KB

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