diff options
Diffstat (limited to 'tests/files')
-rw-r--r-- | tests/files/type_foo.php | 31 | ||||
-rw-r--r-- | tests/files/types_base_test.php | 93 | ||||
-rw-r--r-- | tests/files/types_form_test.php | 174 | ||||
-rw-r--r-- | tests/files/types_local_test.php | 163 | ||||
-rw-r--r-- | tests/files/types_remote_test.php | 133 | ||||
-rw-r--r-- | tests/files/upload_test.php | 128 |
6 files changed, 722 insertions, 0 deletions
diff --git a/tests/files/type_foo.php b/tests/files/type_foo.php new file mode 100644 index 0000000000..95940b9d2f --- /dev/null +++ b/tests/files/type_foo.php @@ -0,0 +1,31 @@ +<?php +/** + * + * This file is part of the phpBB Forum Software package. + * + * @copyright (c) phpBB Limited <https://www.phpbb.com> + * @license GNU General Public License, version 2 (GPL-2.0) + * + * For full copyright and license information, please see + * the docs/CREDITS.txt file. + * + */ + +namespace phpbb\files\types; + +class foo extends \phpbb\files\types\remote +{ + static public $tempnam_path; +} + +function tempnam($one, $two) +{ + if (empty(foo::$tempnam_path)) + { + return \tempnam($one, $two); + } + else + { + return foo::$tempnam_path; + } +} diff --git a/tests/files/types_base_test.php b/tests/files/types_base_test.php new file mode 100644 index 0000000000..e630bf8c48 --- /dev/null +++ b/tests/files/types_base_test.php @@ -0,0 +1,93 @@ +<?php +/** + * + * This file is part of the phpBB Forum Software package. + * + * @copyright (c) phpBB Limited <https://www.phpbb.com> + * @license GNU General Public License, version 2 (GPL-2.0) + * + * For full copyright and license information, please see + * the docs/CREDITS.txt file. + * + */ + +class phpbb_files_types_base_test extends phpbb_test_case +{ + private $path; + + private $filesystem; + + /** @var \Symfony\Component\DependencyInjection\ContainerInterface */ + protected $container; + + /** @var \phpbb\files\factory */ + protected $factory; + + /** @var \bantu\IniGetWrapper\IniGetWrapper */ + protected $php_ini; + + /** @var \phpbb\language\language */ + protected $language; + + /** @var \phpbb\request\request_interface */ + protected $request; + + /** @var string phpBB root path */ + protected $phpbb_root_path; + + protected function setUp() + { + global $phpbb_root_path, $phpEx; + + $this->request = $this->getMock('\phpbb\request\request'); + + $this->filesystem = new \phpbb\filesystem\filesystem(); + $this->language = new \phpbb\language\language(new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx)); + $this->php_ini = new \bantu\IniGetWrapper\IniGetWrapper; + + $this->container = new phpbb_mock_container_builder($phpbb_root_path, $phpEx); + $this->container->set('files.filespec', new \phpbb\files\filespec( + $this->filesystem, + $this->language, + $this->php_ini, + new \FastImageSize\FastImageSize(), + $phpbb_root_path, + new \phpbb\mimetype\guesser(array( + 'mimetype.extension_guesser' => new \phpbb\mimetype\extension_guesser(), + )))); + $this->factory = new \phpbb\files\factory($this->container); + + $this->path = __DIR__ . '/fixture/'; + $this->phpbb_root_path = $phpbb_root_path; + } + + public function data_check_upload_size() + { + return array( + array('foo', '500KB', array()), + array('none', '500KB', array('PHP_SIZE_OVERRUN')), + array('none', '', array('PHP_SIZE_NA')), + ); + } + + /** + * @dataProvider data_check_upload_size + */ + public function test_check_upload_size($filename, $max_filesize, $expected) + { + $php_ini = $this->getMock('\bantu\IniGetWrapper\IniGetWrapper'); + $php_ini->expects($this->any()) + ->method('getString') + ->willReturn($max_filesize); + $type_form = new \phpbb\files\types\local($this->factory, $this->language, $php_ini, $this->request); + $file = $this->getMockBuilder('\phpbb\files\filespec') + ->disableOriginalConstructor() + ->getMock(); + $file->expects($this->any()) + ->method('get') + ->willReturn($filename); + $type_form->check_upload_size($file); + + $this->assertSame($expected, $file->error); + } +} diff --git a/tests/files/types_form_test.php b/tests/files/types_form_test.php new file mode 100644 index 0000000000..efcece49d1 --- /dev/null +++ b/tests/files/types_form_test.php @@ -0,0 +1,174 @@ +<?php +/** + * + * This file is part of the phpBB Forum Software package. + * + * @copyright (c) phpBB Limited <https://www.phpbb.com> + * @license GNU General Public License, version 2 (GPL-2.0) + * + * For full copyright and license information, please see + * the docs/CREDITS.txt file. + * + */ + +require_once dirname(__FILE__) . '/../../phpBB/includes/functions.php'; + +class phpbb_files_types_form_test extends phpbb_test_case +{ + private $path; + + private $filesystem; + + /** @var \Symfony\Component\DependencyInjection\ContainerInterface */ + protected $container; + + /** @var \phpbb\files\factory */ + protected $factory; + + /** @var \bantu\IniGetWrapper\IniGetWrapper */ + protected $php_ini; + + /** @var \phpbb\language\language */ + protected $language; + + /** @var \phpbb\request\request_interface */ + protected $request; + + /** @var \phpbb\plupload\plupload */ + protected $plupload; + + /** @var string phpBB root path */ + protected $phpbb_root_path; + + protected function setUp() + { + global $phpbb_root_path, $phpEx; + + $this->request = $this->getMock('\phpbb\request\request'); + $this->request->expects($this->any()) + ->method('file') + ->willReturn(array()); + + $this->filesystem = new \phpbb\filesystem\filesystem(); + $this->language = new \phpbb\language\language(new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx)); + $this->php_ini = new \bantu\IniGetWrapper\IniGetWrapper; + + $this->container = new phpbb_mock_container_builder($phpbb_root_path, $phpEx); + $this->container->set('files.filespec', new \phpbb\files\filespec( + $this->filesystem, + $this->language, + $this->php_ini, + new \FastImageSize\FastImageSize(), + $phpbb_root_path, + new \phpbb\mimetype\guesser(array( + 'mimetype.extension_guesser' => new \phpbb\mimetype\extension_guesser(), + )))); + $this->factory = new \phpbb\files\factory($this->container); + $this->plupload = $this->getMockBuilder('\phpbb\plupload\plupload') + ->disableOriginalConstructor() + ->getMock(); + $this->plupload->expects($this->any()) + ->method('handle_upload') + ->willReturn(array()); + + $this->path = __DIR__ . '/fixture/'; + $this->phpbb_root_path = $phpbb_root_path; + } + + public function data_upload_form() + { + return array( + array( + array(), + array(''), + ), + array( + array( + 'tmp_name' => 'foo', + 'name' => 'foo', + 'size' => 500, + 'type' => 'image/png', + 'error' => UPLOAD_ERR_PARTIAL, + ), + array('PARTIAL_UPLOAD'), + ), + array( + array( + 'tmp_name' => 'foo', + 'name' => 'foo', + 'size' => 500, + 'type' => 'image/png', + 'error' => -9, + ), + array('NOT_UPLOADED'), + ), + array( + array( + 'tmp_name' => 'foo', + 'name' => 'foo', + 'size' => 0, + 'type' => 'image/png', + ), + array('EMPTY_FILEUPLOAD'), + ), + array( + array( + 'tmp_name' => 'none', + 'name' => 'none', + 'size' => 50, + 'type' => 'image/png', + ), + array('PHP_SIZE_OVERRUN'), + ), + array( + array( + 'tmp_name' => 'tests/upload/fixture/png', + 'name' => 'foo.png', + 'size' => 500, + 'type' => 'image/png', + 'local_mode' => true, + ), + array(), + array('local_mode' => true), + ), + ); + } + + /** + * @dataProvider data_upload_form + */ + public function test_upload_form($upload, $expected, $plupload = array()) + { + $this->request = $this->getMock('\phpbb\request\request'); + $this->request->expects($this->any()) + ->method('file') + ->willReturn($upload); + $filespec = new \phpbb\files\filespec( + $this->filesystem, + $this->language, + $this->php_ini, + new \FastImageSize\FastImageSize(), + $this->phpbb_root_path, + new \phpbb\mimetype\guesser(array( + 'mimetype.extension_guesser' => new \phpbb\mimetype\extension_guesser(), + ))); + $this->container->set('files.filespec', $filespec); + $this->factory = new \phpbb\files\factory($this->container); + $this->plupload = $this->getMockBuilder('\phpbb\plupload\plupload') + ->disableOriginalConstructor() + ->getMock(); + $this->plupload->expects($this->any()) + ->method('handle_upload') + ->willReturn($plupload); + + $type_form = new \phpbb\files\types\form($this->factory, $this->language, $this->php_ini, $this->plupload, $this->request); + $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->php_ini, $this->request, $this->phpbb_root_path); + $upload->set_allowed_extensions(array('png')); + $type_form->set_upload($upload); + + + $file = $type_form->upload('foobar'); + $this->assertSame($expected, $file->error); + $this->assertInstanceOf('\phpbb\files\filespec', $file); + } +} diff --git a/tests/files/types_local_test.php b/tests/files/types_local_test.php new file mode 100644 index 0000000000..f4fa7fad3f --- /dev/null +++ b/tests/files/types_local_test.php @@ -0,0 +1,163 @@ +<?php +/** + * + * This file is part of the phpBB Forum Software package. + * + * @copyright (c) phpBB Limited <https://www.phpbb.com> + * @license GNU General Public License, version 2 (GPL-2.0) + * + * For full copyright and license information, please see + * the docs/CREDITS.txt file. + * + */ + +require_once dirname(__FILE__) . '/../../phpBB/includes/functions.php'; + +class phpbb_files_types_local_test extends phpbb_test_case +{ + private $path; + + private $filesystem; + + /** @var \Symfony\Component\DependencyInjection\ContainerInterface */ + protected $container; + + /** @var \phpbb\files\factory */ + protected $factory; + + /** @var \bantu\IniGetWrapper\IniGetWrapper */ + protected $php_ini; + + /** @var \phpbb\language\language */ + protected $language; + + /** @var \phpbb\request\request_interface */ + protected $request; + + /** @var \phpbb\plupload\plupload */ + protected $plupload; + + /** @var string phpBB root path */ + protected $phpbb_root_path; + + protected function setUp() + { + global $phpbb_root_path, $phpEx; + + $this->request = $this->getMock('\phpbb\request\request'); + $this->request->expects($this->any()) + ->method('file') + ->willReturn(array()); + + $this->filesystem = new \phpbb\filesystem\filesystem(); + $this->language = new \phpbb\language\language(new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx)); + $this->php_ini = new \bantu\IniGetWrapper\IniGetWrapper; + + $this->container = new phpbb_mock_container_builder($phpbb_root_path, $phpEx); + $this->container->set('files.filespec', new \phpbb\files\filespec( + $this->filesystem, + $this->language, + $this->php_ini, + new \FastImageSize\FastImageSize(), + $phpbb_root_path, + new \phpbb\mimetype\guesser(array( + 'mimetype.extension_guesser' => new \phpbb\mimetype\extension_guesser(), + )))); + $this->factory = new \phpbb\files\factory($this->container); + $this->plupload = $this->getMockBuilder('\phpbb\plupload\plupload') + ->disableOriginalConstructor() + ->getMock(); + $this->plupload->expects($this->any()) + ->method('handle_upload') + ->willReturn(array()); + + $this->path = __DIR__ . '/fixture/'; + $this->phpbb_root_path = $phpbb_root_path; + } + + public function test_upload_init_error() + { + $filespec = $this->getMockBuilder('\phpbb\files\filespec') + ->disableOriginalConstructor() + ->getMock(); + $filespec->expects($this->any()) + ->method('init_error') + ->willReturn(true); + $filespec->expects($this->any()) + ->method('set_upload_ary') + ->willReturnSelf(); + $filespec->expects($this->any()) + ->method('set_upload_namespace') + ->willReturnSelf(); + $this->container->set('files.filespec', $filespec); + $this->factory = new \phpbb\files\factory($this->container); + + $type_local = new \phpbb\files\types\local($this->factory, $this->language, $this->php_ini, $this->request); + + + $file = $type_local->upload('foo', false); + $this->assertSame(array(''), $file->error); + $this->assertInstanceOf('\phpbb\files\filespec', $file); + } + + public function data_upload_form() + { + return array( + array( + 'foo', + array( + 'tmp_name' => 'foo', + 'size' => 500, + 'type' => 'image/png', + ), + array('NOT_UPLOADED'), + ), + array( + 'none', + false, + array('PHP_SIZE_OVERRUN'), + ), + array( + 'tests/upload/fixture/png', + array( + 'realname' => 'foo.png', + 'size' => 500, + 'type' => 'image/png', + 'local_mode' => true, + ), + array(), + ), + ); + } + + /** + * @dataProvider data_upload_form + */ + public function test_upload_form($filename, $upload_ary, $expected) + { + $filespec = new \phpbb\files\filespec( + $this->filesystem, + $this->language, + $this->php_ini, + new \FastImageSize\FastImageSize(), + $this->phpbb_root_path, + new \phpbb\mimetype\guesser(array( + 'mimetype.extension_guesser' => new \phpbb\mimetype\extension_guesser(), + ))); + $filespec_local = new ReflectionProperty($filespec, 'local'); + $filespec_local->setAccessible(true); + $filespec_local->setValue($filespec, true); + $this->container->set('files.filespec', $filespec); + $this->factory = new \phpbb\files\factory($this->container); + + $type_local = new \phpbb\files\types\local($this->factory, $this->language, $this->php_ini, $this->request); + $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->php_ini, $this->request, $this->phpbb_root_path); + $upload->set_allowed_extensions(array('png')); + $type_local->set_upload($upload); + + + $file = $type_local->upload($filename, $upload_ary); + $this->assertSame($expected, $file->error); + $this->assertInstanceOf('\phpbb\files\filespec', $file); + } +} diff --git a/tests/files/types_remote_test.php b/tests/files/types_remote_test.php new file mode 100644 index 0000000000..476d8ed3ba --- /dev/null +++ b/tests/files/types_remote_test.php @@ -0,0 +1,133 @@ +<?php +/** + * + * This file is part of the phpBB Forum Software package. + * + * @copyright (c) phpBB Limited <https://www.phpbb.com> + * @license GNU General Public License, version 2 (GPL-2.0) + * + * For full copyright and license information, please see + * the docs/CREDITS.txt file. + * + */ + +require_once dirname(__FILE__) . '/../../phpBB/includes/functions.php'; +require_once dirname(__FILE__) . '/type_foo.php'; + +class phpbb_files_types_remote_test extends phpbb_test_case +{ + private $path; + + private $filesystem; + + /** @var \phpbb\config\config */ + protected $config; + + /** @var \Symfony\Component\DependencyInjection\ContainerInterface */ + protected $container; + + /** @var \phpbb\files\factory */ + protected $factory; + + /** @var \bantu\IniGetWrapper\IniGetWrapper */ + protected $php_ini; + + /** @var \phpbb\language\language */ + protected $language; + + /** @var \phpbb\request\request_interface */ + protected $request; + + /** @var string phpBB root path */ + protected $phpbb_root_path; + + protected function setUp() + { + global $config, $phpbb_root_path, $phpEx; + + $config = new \phpbb\config\config(array()); + $this->config = $config; + $this->config->set('remote_upload_verify', 0); + $this->request = $this->getMock('\phpbb\request\request'); + + $this->filesystem = new \phpbb\filesystem\filesystem(); + $this->language = new \phpbb\language\language(new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx)); + $this->php_ini = new \bantu\IniGetWrapper\IniGetWrapper; + + $this->container = new phpbb_mock_container_builder($phpbb_root_path, $phpEx); + $this->container->set('files.filespec', new \phpbb\files\filespec( + $this->filesystem, + $this->language, + $this->php_ini, + new \FastImageSize\FastImageSize(), + $phpbb_root_path, + new \phpbb\mimetype\guesser(array( + 'mimetype.extension_guesser' => new \phpbb\mimetype\extension_guesser(), + )))); + $this->factory = new \phpbb\files\factory($this->container); + + $this->path = __DIR__ . '/fixture/'; + $this->phpbb_root_path = $phpbb_root_path; + } + + public function test_upload_fsock_fail() + { + $type_remote = new \phpbb\files\types\remote($this->config, $this->factory, $this->language, $this->php_ini, $this->request, $this->phpbb_root_path); + $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->php_ini, $this->request, $this->phpbb_root_path); + $upload->set_allowed_extensions(array('png')); + $type_remote->set_upload($upload); + + $file = $type_remote->upload('https://bärföö.com/foo.png'); + + $this->assertSame(array('NOT_UPLOADED'), $file->error); + } + + public function data_get_max_file_size() + { + return array( + array('', 'http://example.com/foo/bar.png'), + array('2k', 'http://example.com/foo/bar.png'), + array('500k', 'http://example.com/foo/bar.png'), + array('500M', 'http://example.com/foo/bar.png'), + array('500m', 'http://example.com/foo/bar.png'), + array('500k', 'http://google.com/?.png', array('DISALLOWED_EXTENSION', 'DISALLOWED_CONTENT')), + array('1', 'http://google.com/?.png', array('WRONG_FILESIZE')), + array('500g', 'http://example.com/foo/bar.png'), + array('foobar', 'http://example.com/foo/bar.png'), + array('-5k', 'http://example.com/foo/bar.png'), + ); + } + + /** + * @dataProvider data_get_max_file_size + */ + public function test_get_max_file_size($max_file_size, $link, $expected = array('URL_NOT_FOUND')) + { + $php_ini = $this->getMock('\bantu\IniGetWrapper\IniGetWrapper', array('getString')); + $php_ini->expects($this->any()) + ->method('getString') + ->willReturn($max_file_size); + $type_remote = new \phpbb\files\types\remote($this->config, $this->factory, $this->language, $php_ini, $this->request, $this->phpbb_root_path); + $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->php_ini, $this->request, $this->phpbb_root_path); + $upload->set_allowed_extensions(array('png')); + $type_remote->set_upload($upload); + + $file = $type_remote->upload($link); + + $this->assertSame($expected, $file->error); + } + + public function test_upload_wrong_path() + { + $type_remote = new \phpbb\files\types\foo($this->config, $this->factory, $this->language, $this->php_ini, $this->request, $this->phpbb_root_path); + $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->php_ini, $this->request, $this->phpbb_root_path); + $upload->set_allowed_extensions(array('png')); + $type_remote->set_upload($upload); + $type_remote::$tempnam_path = $this->phpbb_root_path . 'cache/wrong/path'; + + $file = $type_remote->upload('http://google.com/?.png'); + + $this->assertSame(array('NOT_UPLOADED'), $file->error); + $type_remote::$tempnam_path = ''; + } +} diff --git a/tests/files/upload_test.php b/tests/files/upload_test.php new file mode 100644 index 0000000000..c41204a0d5 --- /dev/null +++ b/tests/files/upload_test.php @@ -0,0 +1,128 @@ +<?php +/** + * + * This file is part of the phpBB Forum Software package. + * + * @copyright (c) phpBB Limited <https://www.phpbb.com> + * @license GNU General Public License, version 2 (GPL-2.0) + * + * For full copyright and license information, please see + * the docs/CREDITS.txt file. + * + */ + +class phpbb_files_upload_test extends phpbb_test_case +{ + private $path; + + private $filesystem; + + /** @var \Symfony\Component\DependencyInjection\ContainerInterface */ + protected $container; + + /** @var \phpbb\files\factory */ + protected $factory; + + /** @var \bantu\IniGetWrapper\IniGetWrapper */ + protected $php_ini; + + /** @var \phpbb\language\language */ + protected $language; + + /** @var \phpbb\request\request_interface */ + protected $request; + + /** @var string phpBB root path */ + protected $phpbb_root_path; + + protected function setUp() + { + // Global $config required by unique_id + global $config, $phpbb_root_path, $phpEx; + + if (!is_array($config)) + { + $config = array(); + } + + $config['rand_seed'] = ''; + $config['rand_seed_last_update'] = time() + 600; + + $this->request = $this->getMock('\phpbb\request\request'); + + $this->filesystem = new \phpbb\filesystem\filesystem(); + $this->language = new \phpbb\language\language(new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx)); + $this->php_ini = new \bantu\IniGetWrapper\IniGetWrapper; + + $this->container = new phpbb_mock_container_builder($phpbb_root_path, $phpEx); + $this->container->set('files.filespec', new \phpbb\files\filespec( + $this->filesystem, + $this->language, + $this->php_ini, + new \FastImageSize\FastImageSize(), + $phpbb_root_path, + new \phpbb\mimetype\guesser(array( + 'mimetype.extension_guesser' => new \phpbb\mimetype\extension_guesser(), + )))); + $this->factory = new \phpbb\files\factory($this->container); + + $this->path = __DIR__ . '/fixture/'; + $this->phpbb_root_path = $phpbb_root_path; + } + + public function test_reset_vars() + { + $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->php_ini, $this->request, $this->phpbb_root_path); + $upload->set_max_filesize(500); + $this->assertEquals(500, $upload->max_filesize); + $upload->reset_vars(); + $this->assertEquals(0, $upload->max_filesize); + } + + public function test_set_disallowed_content() + { + $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->php_ini, $this->request, $this->phpbb_root_path); + $disallowed_content = new ReflectionProperty($upload, 'disallowed_content'); + $disallowed_content->setAccessible(true); + + $upload->set_disallowed_content(array('foo')); + $this->assertEquals(array('foo'), $disallowed_content->getValue($upload)); + $upload->set_disallowed_content(array('foo', 'bar', 'meh')); + $this->assertEquals(array('foo', 'bar', 'meh'), $disallowed_content->getValue($upload)); + $upload->set_disallowed_content(''); + $this->assertEquals(array('foo', 'bar', 'meh'), $disallowed_content->getValue($upload)); + $this->assertINstanceOf('\phpbb\files\upload', $upload->set_disallowed_content(array())); + $this->assertEquals(array(), $disallowed_content->getValue($upload)); + $upload->reset_vars(); + $this->assertEquals(array(), $disallowed_content->getValue($upload)); + } + + public function test_is_valid() + { + $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->php_ini, $this->request, $this->phpbb_root_path); + $this->assertFalse($upload->is_valid('foobar')); + } + + public function data_internal_error() + { + return array( + array(UPLOAD_ERR_INI_SIZE, 'PHP_SIZE_OVERRUN'), + array(UPLOAD_ERR_FORM_SIZE, 'WRONG_FILESIZE'), + array(UPLOAD_ERR_PARTIAL, 'PARTIAL_UPLOAD'), + array(UPLOAD_ERR_NO_FILE, 'NOT_UPLOADED'), + array(UPLOAD_ERR_NO_TMP_DIR, 'NO_TEMP_DIR'), + array(UPLOAD_ERR_CANT_WRITE, 'NO_TEMP_DIR'), + array(UPLOAD_ERR_EXTENSION, 'PHP_UPLOAD_STOPPED'), + array(9, false), + ); + } + + /** + * @dataProvider data_internal_error + */ + public function test_assign_internal_error($error_code, $expected) + { + $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->php_ini, $this->request, $this->phpbb_root_path); + $this->assertSame($expected, $upload->assign_internal_error($error_code)); + } +} |