1
0

PathVerificationTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. }
  78. $this->view->verifyPath('', $fileName);
  79. }
  80. public function providesAstralPlane() {
  81. return [
  82. // this is the monkey emoji - http://en.wikipedia.org/w/index.php?title=%F0%9F%90%B5&redirect=no
  83. ['🐵'],
  84. ['🐵.txt'],
  85. ['txt.💩'],
  86. ['💩🐵.txt'],
  87. ['💩🐵'],
  88. ];
  89. }
  90. /**
  91. * @dataProvider providesInvalidCharsPosix
  92. * @expectedException \OCP\Files\InvalidCharacterInPathException
  93. */
  94. public function testPathVerificationInvalidCharsPosix($fileName) {
  95. $storage = new Local(['datadir' => '']);
  96. $fileName = " 123{$fileName}456 ";
  97. self::invokePrivate($storage, 'verifyPosixPath', [$fileName]);
  98. }
  99. public function providesInvalidCharsPosix() {
  100. return [
  101. [\chr(0)],
  102. [\chr(1)],
  103. [\chr(2)],
  104. [\chr(3)],
  105. [\chr(4)],
  106. [\chr(5)],
  107. [\chr(6)],
  108. [\chr(7)],
  109. [\chr(8)],
  110. [\chr(9)],
  111. [\chr(10)],
  112. [\chr(11)],
  113. [\chr(12)],
  114. [\chr(13)],
  115. [\chr(14)],
  116. [\chr(15)],
  117. [\chr(16)],
  118. [\chr(17)],
  119. [\chr(18)],
  120. [\chr(19)],
  121. [\chr(20)],
  122. [\chr(21)],
  123. [\chr(22)],
  124. [\chr(23)],
  125. [\chr(24)],
  126. [\chr(25)],
  127. [\chr(26)],
  128. [\chr(27)],
  129. [\chr(28)],
  130. [\chr(29)],
  131. [\chr(30)],
  132. [\chr(31)],
  133. ['/'],
  134. ['\\'],
  135. ];
  136. }
  137. /**
  138. * @dataProvider providesValidPosixPaths
  139. */
  140. public function testPathVerificationValidPaths($fileName) {
  141. $storage = new Local(['datadir' => '']);
  142. self::invokePrivate($storage, 'verifyPosixPath', [$fileName]);
  143. // nothing thrown
  144. $this->assertTrue(true);
  145. }
  146. public function providesValidPosixPaths() {
  147. return [
  148. ['simple'],
  149. ['simple.txt'],
  150. ['\''],
  151. ['`'],
  152. ['%'],
  153. ['()'],
  154. ['[]'],
  155. ['!'],
  156. ['$'],
  157. ['_'],
  158. ];
  159. }
  160. }