pathverificationtest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. class PathVerification extends \Test\TestCase {
  11. /**
  12. * @var \OC\Files\View
  13. */
  14. private $view;
  15. protected function setUp() {
  16. parent::setUp();
  17. $this->view = new View();
  18. }
  19. /**
  20. * @expectedException \OCP\Files\InvalidPathException
  21. * @expectedExceptionMessage File name is too long
  22. */
  23. public function testPathVerificationFileNameTooLong() {
  24. $fileName = str_repeat('a', 500);
  25. $this->view->verifyPath('', $fileName);
  26. }
  27. /**
  28. * @dataProvider providesEmptyFiles
  29. * @expectedException \OCP\Files\InvalidPathException
  30. * @expectedExceptionMessage Empty filename is not allowed
  31. */
  32. public function testPathVerificationEmptyFileName($fileName) {
  33. $this->view->verifyPath('', $fileName);
  34. }
  35. public function providesEmptyFiles() {
  36. return [
  37. [''],
  38. [' '],
  39. ];
  40. }
  41. /**
  42. * @dataProvider providesDotFiles
  43. * @expectedException \OCP\Files\InvalidPathException
  44. * @expectedExceptionMessage Dot files are not allowed
  45. */
  46. public function testPathVerificationDotFiles($fileName) {
  47. $this->view->verifyPath('', $fileName);
  48. }
  49. public function providesDotFiles() {
  50. return [
  51. ['.'],
  52. ['..'],
  53. [' .'],
  54. [' ..'],
  55. ['. '],
  56. ['.. '],
  57. [' . '],
  58. [' .. '],
  59. ];
  60. }
  61. /**
  62. * @dataProvider providesAstralPlane
  63. * @expectedException \OCP\Files\InvalidPathException
  64. * @expectedExceptionMessage 4-byte characters are not supported in file names
  65. */
  66. public function testPathVerificationAstralPlane($fileName) {
  67. $this->view->verifyPath('', $fileName);
  68. }
  69. public function providesAstralPlane() {
  70. return [
  71. // this is the monkey emoji - http://en.wikipedia.org/w/index.php?title=%F0%9F%90%B5&redirect=no
  72. ['🐵'],
  73. ];
  74. }
  75. /**
  76. * @dataProvider providesInvalidCharsWindows
  77. * @expectedException \OCP\Files\InvalidCharacterInPathException
  78. */
  79. public function testPathVerificationInvalidCharsWindows($fileName) {
  80. $storage = new Local(['datadir' => '']);
  81. $fileName = " 123{$fileName}456 ";
  82. \Test_Helper::invokePrivate($storage, 'verifyWindowsPath', [$fileName]);
  83. }
  84. public function providesInvalidCharsWindows() {
  85. return [
  86. [\chr(0)],
  87. [\chr(1)],
  88. [\chr(2)],
  89. [\chr(3)],
  90. [\chr(4)],
  91. [\chr(5)],
  92. [\chr(6)],
  93. [\chr(7)],
  94. [\chr(8)],
  95. [\chr(9)],
  96. [\chr(10)],
  97. [\chr(11)],
  98. [\chr(12)],
  99. [\chr(13)],
  100. [\chr(14)],
  101. [\chr(15)],
  102. [\chr(16)],
  103. [\chr(17)],
  104. [\chr(18)],
  105. [\chr(19)],
  106. [\chr(20)],
  107. [\chr(21)],
  108. [\chr(22)],
  109. [\chr(23)],
  110. [\chr(24)],
  111. [\chr(25)],
  112. [\chr(26)],
  113. [\chr(27)],
  114. [\chr(28)],
  115. [\chr(29)],
  116. [\chr(30)],
  117. [\chr(31)],
  118. ['<'],
  119. ['>'],
  120. [':'],
  121. ['"'],
  122. ['/'],
  123. ['\\'],
  124. ['|'],
  125. ['?'],
  126. ['*'],
  127. ];
  128. }
  129. /**
  130. * @dataProvider providesInvalidCharsPosix
  131. * @expectedException \OCP\Files\InvalidCharacterInPathException
  132. */
  133. public function testPathVerificationInvalidCharsPosix($fileName) {
  134. $storage = new Local(['datadir' => '']);
  135. $fileName = " 123{$fileName}456 ";
  136. \Test_Helper::invokePrivate($storage, 'verifyWindowsPath', [$fileName]);
  137. }
  138. public function providesInvalidCharsPosix() {
  139. return [
  140. [\chr(0)],
  141. [\chr(1)],
  142. [\chr(2)],
  143. [\chr(3)],
  144. [\chr(4)],
  145. [\chr(5)],
  146. [\chr(6)],
  147. [\chr(7)],
  148. [\chr(8)],
  149. [\chr(9)],
  150. [\chr(10)],
  151. [\chr(11)],
  152. [\chr(12)],
  153. [\chr(13)],
  154. [\chr(14)],
  155. [\chr(15)],
  156. [\chr(16)],
  157. [\chr(17)],
  158. [\chr(18)],
  159. [\chr(19)],
  160. [\chr(20)],
  161. [\chr(21)],
  162. [\chr(22)],
  163. [\chr(23)],
  164. [\chr(24)],
  165. [\chr(25)],
  166. [\chr(26)],
  167. [\chr(27)],
  168. [\chr(28)],
  169. [\chr(29)],
  170. [\chr(30)],
  171. [\chr(31)],
  172. ['/'],
  173. ['\\'],
  174. ];
  175. }
  176. /**
  177. * @dataProvider providesReservedNamesWindows
  178. * @expectedException \OCP\Files\ReservedWordException
  179. */
  180. public function testPathVerificationReservedNamesWindows($fileName) {
  181. $storage = new Local(['datadir' => '']);
  182. \Test_Helper::invokePrivate($storage, 'verifyWindowsPath', [$fileName]);
  183. }
  184. public function providesReservedNamesWindows() {
  185. return [
  186. [' CON '],
  187. ['prn '],
  188. ['AUX'],
  189. ['NUL'],
  190. ['COM1'],
  191. ['COM2'],
  192. ['COM3'],
  193. ['COM4'],
  194. ['COM5'],
  195. ['COM6'],
  196. ['COM7'],
  197. ['COM8'],
  198. ['COM9'],
  199. ['LPT1'],
  200. ['LPT2'],
  201. ['LPT3'],
  202. ['LPT4'],
  203. ['LPT5'],
  204. ['LPT6'],
  205. ['LPT7'],
  206. ['LPT8'],
  207. ['LPT9']
  208. ];
  209. }
  210. /**
  211. * @dataProvider providesValidPosixPaths
  212. */
  213. public function testPathVerificationValidPaths($fileName) {
  214. $storage = new Local(['datadir' => '']);
  215. \Test_Helper::invokePrivate($storage, 'verifyPosixPath', [$fileName]);
  216. \Test_Helper::invokePrivate($storage, 'verifyWindowsPath', [$fileName]);
  217. // nothing thrown
  218. $this->assertTrue(true);
  219. }
  220. public function providesValidPosixPaths() {
  221. return [
  222. ['simple'],
  223. ['simple.txt'],
  224. ['\''],
  225. ['`'],
  226. ['%'],
  227. ['()'],
  228. ['[]'],
  229. ['!'],
  230. ['$'],
  231. ['_'],
  232. ];
  233. }
  234. }