PageRenderTime 51ms CodeModel.GetById 3ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/upload/fileupload_test.php

http://github.com/phpbb/phpbb
PHP | 239 lines | 187 code | 33 blank | 19 comment | 1 complexity | 7ae1025e688355a3f8483988b0d382ea MD5 | raw file
Possible License(s): GPL-3.0, AGPL-1.0
  1. <?php
  2. /**
  3. *
  4. * This file is part of the phpBB Forum Software package.
  5. *
  6. * @copyright (c) phpBB Limited <https://www.phpbb.com>
  7. * @license GNU General Public License, version 2 (GPL-2.0)
  8. *
  9. * For full copyright and license information, please see
  10. * the docs/CREDITS.txt file.
  11. *
  12. */
  13. require_once __DIR__ . '/../mock/filespec.php';
  14. class phpbb_fileupload_test extends phpbb_test_case
  15. {
  16. private $path;
  17. private $filesystem;
  18. /** @var \Symfony\Component\DependencyInjection\ContainerInterface */
  19. protected $container;
  20. /** @var \phpbb\files\factory */
  21. protected $factory;
  22. /** @var \bantu\IniGetWrapper\IniGetWrapper */
  23. protected $php_ini;
  24. /** @var \phpbb\language\language */
  25. protected $language;
  26. /** @var \phpbb\request\request_interface */
  27. protected $request;
  28. /** @var string phpBB root path */
  29. protected $phpbb_root_path;
  30. protected function setUp(): void
  31. {
  32. // Global $config required by unique_id
  33. global $config, $phpbb_root_path, $phpEx;
  34. if (!is_array($config))
  35. {
  36. $config = new \phpbb\config\config(array());
  37. }
  38. $config['rand_seed'] = '';
  39. $config['rand_seed_last_update'] = time() + 600;
  40. $this->request = $this->createMock('\phpbb\request\request');
  41. $this->php_ini = new \bantu\IniGetWrapper\IniGetWrapper;
  42. $this->filesystem = new \phpbb\filesystem\filesystem();
  43. $this->language = new \phpbb\language\language(new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx));
  44. $guessers = array(
  45. new \Symfony\Component\HttpFoundation\File\MimeType\FileinfoMimeTypeGuesser(),
  46. new \Symfony\Component\HttpFoundation\File\MimeType\FileBinaryMimeTypeGuesser(),
  47. new \phpbb\mimetype\content_guesser(),
  48. new \phpbb\mimetype\extension_guesser(),
  49. );
  50. $guessers[2]->set_priority(-2);
  51. $guessers[3]->set_priority(-2);
  52. $this->mimetype_guesser = new \phpbb\mimetype\guesser($guessers);
  53. $this->container = new phpbb_mock_container_builder($phpbb_root_path, $phpEx);
  54. $this->container->set('files.filespec', new \phpbb\files\filespec(
  55. $this->filesystem,
  56. $this->language,
  57. $this->php_ini,
  58. new \FastImageSize\FastImageSize(),
  59. $phpbb_root_path,
  60. new \phpbb\mimetype\guesser(array(
  61. 'mimetype.extension_guesser' => new \phpbb\mimetype\extension_guesser(),
  62. ))));
  63. $this->factory = new \phpbb\files\factory($this->container);
  64. $plupload = new \phpbb\plupload\plupload($phpbb_root_path, $config, $this->request, new \phpbb\user($this->language, '\phpbb\datetime'), $this->php_ini, $this->mimetype_guesser);
  65. $this->container->set('files.types.form', new \phpbb\files\types\form(
  66. $this->factory,
  67. $this->language,
  68. $this->php_ini,
  69. $plupload,
  70. $this->request
  71. ));
  72. $this->container->set('files.types.local', new \phpbb\files\types\local(
  73. $this->factory,
  74. $this->language,
  75. $this->php_ini,
  76. $this->request
  77. ));
  78. $this->path = __DIR__ . '/fixture/';
  79. $this->phpbb_root_path = $phpbb_root_path;
  80. }
  81. private function gen_valid_filespec()
  82. {
  83. $filespec = new phpbb_mock_filespec();
  84. $filespec->filesize = 1;
  85. $filespec->extension = 'jpg';
  86. $filespec->realname = 'valid';
  87. $filespec->width = 2;
  88. $filespec->height = 2;
  89. return $filespec;
  90. }
  91. protected function tearDown(): void
  92. {
  93. // Clear globals
  94. global $config, $user;
  95. $config = array();
  96. $user = null;
  97. }
  98. public function test_common_checks_invalid_extension()
  99. {
  100. $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->php_ini, $this->request, $this->phpbb_root_path);
  101. $upload->set_allowed_extensions(array('png'))
  102. ->set_max_filesize(100);
  103. $file = $this->gen_valid_filespec();
  104. $upload->common_checks($file);
  105. $this->assertEquals('DISALLOWED_EXTENSION', $file->error[0]);
  106. }
  107. public function test_common_checks_disallowed_content()
  108. {
  109. $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->php_ini, $this->request, $this->phpbb_root_path);
  110. $upload->set_allowed_extensions(array('jpg'))
  111. ->set_max_filesize(1000);
  112. $file = new \phpbb\files\filespec($this->filesystem, $this->language, $this->php_ini, new \FastImageSize\FastImageSize(), $this->phpbb_root_path);
  113. $file->set_upload_ary(array(
  114. 'size' => 50,
  115. 'tmp_name' => dirname(__FILE__) . '/fixture/disallowed',
  116. 'name' => 'disallowed.jpg',
  117. 'type' => 'image/jpg'
  118. ))
  119. ->set_upload_namespace($upload);
  120. file_put_contents(dirname(__FILE__) . '/fixture/disallowed', '<body>' . file_get_contents(dirname(__FILE__) . '/fixture/jpg'));
  121. $upload->common_checks($file);
  122. $this->assertEquals('DISALLOWED_CONTENT', $file->error[0]);
  123. unlink(dirname(__FILE__) . '/fixture/disallowed');
  124. }
  125. public function test_common_checks_invalid_filename()
  126. {
  127. $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->php_ini, $this->request, $this->phpbb_root_path);
  128. $upload->set_allowed_extensions(array('jpg'))
  129. ->set_max_filesize(100);
  130. $file = $this->gen_valid_filespec();
  131. $file->realname = 'invalid?';
  132. $upload->common_checks($file);
  133. $this->assertEquals('INVALID_FILENAME', $file->error[0]);
  134. }
  135. public function test_common_checks_too_large()
  136. {
  137. $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->php_ini, $this->request, $this->phpbb_root_path);
  138. $upload->set_allowed_extensions(array('jpg'))
  139. ->set_max_filesize(100);
  140. $file = $this->gen_valid_filespec();
  141. $file->filesize = 1000;
  142. $upload->common_checks($file);
  143. $this->assertEquals('WRONG_FILESIZE', $file->error[0]);
  144. }
  145. public function test_common_checks_valid_file()
  146. {
  147. $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->php_ini, $this->request, $this->phpbb_root_path);
  148. $upload->set_allowed_extensions(array('jpg'))
  149. ->set_max_filesize(1000);
  150. $file = $this->gen_valid_filespec();
  151. $upload->common_checks($file);
  152. $this->assertEquals(0, count($file->error));
  153. }
  154. public function test_local_upload()
  155. {
  156. $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->php_ini, $this->request, $this->phpbb_root_path);
  157. $upload->set_allowed_extensions(array('jpg'))
  158. ->set_max_filesize(1000);
  159. copy($this->path . 'jpg', $this->path . 'jpg.jpg');
  160. $file = $upload->handle_upload('files.types.local', $this->path . 'jpg.jpg');
  161. $this->assertEquals(0, count($file->error));
  162. $this->assertFalse($file->additional_checks());
  163. $this->assertTrue($file->move_file('../tests/upload/fixture/copies', true));
  164. $file->remove();
  165. }
  166. public function test_move_existent_file()
  167. {
  168. $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->php_ini, $this->request, $this->phpbb_root_path);
  169. $upload->set_allowed_extensions(array('jpg'))
  170. ->set_max_filesize(1000);
  171. copy($this->path . 'jpg', $this->path . 'jpg.jpg');
  172. $file = $upload->handle_upload('files.types.local', $this->path . 'jpg.jpg');
  173. $this->assertEquals(0, count($file->error));
  174. $this->assertFalse($file->move_file('../tests/upload/fixture'));
  175. $this->assertFalse($file->get('file_moved'));
  176. $this->assertEquals(1, count($file->error));
  177. }
  178. public function test_move_existent_file_overwrite()
  179. {
  180. $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->php_ini, $this->request, $this->phpbb_root_path);
  181. $upload->set_allowed_extensions(array('jpg'))
  182. ->set_max_filesize(1000);
  183. copy($this->path . 'jpg', $this->path . 'jpg.jpg');
  184. copy($this->path . 'jpg', $this->path . 'copies/jpg.jpg');
  185. $file = $upload->handle_upload('files.types.local', $this->path . 'jpg.jpg');
  186. $this->assertEquals(0, count($file->error));
  187. $file->move_file('../tests/upload/fixture/copies', true);
  188. $this->assertEquals(0, count($file->error));
  189. unlink($this->path . 'copies/jpg.jpg');
  190. }
  191. public function test_valid_dimensions()
  192. {
  193. $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->php_ini, $this->request, $this->phpbb_root_path);
  194. $upload->set_allowed_extensions(false)
  195. ->set_max_filesize(false)
  196. ->set_allowed_dimensions(1, 1, 100, 100);
  197. $file1 = $this->gen_valid_filespec();
  198. $file2 = $this->gen_valid_filespec();
  199. $file2->height = 101;
  200. $file3 = $this->gen_valid_filespec();
  201. $file3->width = 0;
  202. $this->assertTrue($upload->valid_dimensions($file1));
  203. $this->assertFalse($upload->valid_dimensions($file2));
  204. $this->assertFalse($upload->valid_dimensions($file3));
  205. }
  206. }