PathVerificationTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. /**
  3. * Copyright (c) 2015 Thomas Müller <deepdiver@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. namespace Test\Files;
  8. use OC\Files\Storage\Local;
  9. use OC\Files\View;
  10. use OCP\Files\InvalidPathException;
  11. /**
  12. * Class PathVerificationTest
  13. *
  14. * @group DB
  15. *
  16. * @package Test\Files
  17. */
  18. class PathVerificationTest extends \Test\TestCase {
  19. /**
  20. * @var \OC\Files\View
  21. */
  22. private $view;
  23. protected function setUp() {
  24. parent::setUp();
  25. $this->view = new View();
  26. }
  27. /**
  28. * @expectedException \OCP\Files\InvalidPathException
  29. * @expectedExceptionMessage File name is too long
  30. */
  31. public function testPathVerificationFileNameTooLong() {
  32. $fileName = str_repeat('a', 500);
  33. $this->view->verifyPath('', $fileName);
  34. }
  35. /**
  36. * @dataProvider providesEmptyFiles
  37. * @expectedException \OCP\Files\InvalidPathException
  38. * @expectedExceptionMessage Empty filename is not allowed
  39. */
  40. public function testPathVerificationEmptyFileName($fileName) {
  41. $this->view->verifyPath('', $fileName);
  42. }
  43. public function providesEmptyFiles() {
  44. return [
  45. [''],
  46. [' '],
  47. ];
  48. }
  49. /**
  50. * @dataProvider providesDotFiles
  51. * @expectedException \OCP\Files\InvalidPathException
  52. * @expectedExceptionMessage Dot files are not allowed
  53. */
  54. public function testPathVerificationDotFiles($fileName) {
  55. $this->view->verifyPath('', $fileName);
  56. }
  57. public function providesDotFiles() {
  58. return [
  59. ['.'],
  60. ['..'],
  61. [' .'],
  62. [' ..'],
  63. ['. '],
  64. ['.. '],
  65. [' . '],
  66. [' .. '],
  67. ];
  68. }
  69. /**
  70. * @dataProvider providesAstralPlane
  71. */
  72. public function testPathVerificationAstralPlane($fileName) {
  73. $connection = \OC::$server->getDatabaseConnection();
  74. if (!$connection->supports4ByteText()) {
  75. $this->expectException(InvalidPathException::class);
  76. $this->expectExceptionMessage('File name contains at least one invalid character');
  77. } else {
  78. $this->addToAssertionCount(1);
  79. }
  80. $this->view->verifyPath('', $fileName);
  81. }
  82. public function providesAstralPlane() {
  83. return [
  84. // this is the monkey emoji - http://en.wikipedia.org/w/index.php?title=%F0%9F%90%B5&redirect=no
  85. ['🐵'],
  86. ['🐵.txt'],
  87. ['txt.💩'],
  88. ['💩🐵.txt'],
  89. ['💩🐵'],
  90. ];
  91. }
  92. /**
  93. * @dataProvider providesInvalidCharsPosix
  94. * @expectedException \OCP\Files\InvalidCharacterInPathException
  95. */
  96. public function testPathVerificationInvalidCharsPosix($fileName) {
  97. $storage = new Local(['datadir' => '']);
  98. $fileName = " 123{$fileName}456 ";
  99. self::invokePrivate($storage, 'verifyPosixPath', [$fileName]);
  100. }
  101. public function providesInvalidCharsPosix() {
  102. return [
  103. [\chr(0)],
  104. [\chr(1)],
  105. [\chr(2)],
  106. [\chr(3)],
  107. [\chr(4)],
  108. [\chr(5)],
  109. [\chr(6)],
  110. [\chr(7)],
  111. [\chr(8)],
  112. [\chr(9)],
  113. [\chr(10)],
  114. [\chr(11)],
  115. [\chr(12)],
  116. [\chr(13)],
  117. [\chr(14)],
  118. [\chr(15)],
  119. [\chr(16)],
  120. [\chr(17)],
  121. [\chr(18)],
  122. [\chr(19)],
  123. [\chr(20)],
  124. [\chr(21)],
  125. [\chr(22)],
  126. [\chr(23)],
  127. [\chr(24)],
  128. [\chr(25)],
  129. [\chr(26)],
  130. [\chr(27)],
  131. [\chr(28)],
  132. [\chr(29)],
  133. [\chr(30)],
  134. [\chr(31)],
  135. ['/'],
  136. ['\\'],
  137. ];
  138. }
  139. /**
  140. * @dataProvider providesValidPosixPaths
  141. */
  142. public function testPathVerificationValidPaths($fileName) {
  143. $storage = new Local(['datadir' => '']);
  144. self::invokePrivate($storage, 'verifyPosixPath', [$fileName]);
  145. // nothing thrown
  146. $this->addToAssertionCount(1);
  147. }
  148. public function providesValidPosixPaths() {
  149. return [
  150. ['simple'],
  151. ['simple.txt'],
  152. ['\''],
  153. ['`'],
  154. ['%'],
  155. ['()'],
  156. ['[]'],
  157. ['!'],
  158. ['$'],
  159. ['_'],
  160. ];
  161. }
  162. }