1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
|
<?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\mimetype;
require_once dirname(__FILE__) . '/null_guesser.php';
require_once dirname(__FILE__) . '/incorrect_guesser.php';
function function_exists($name)
{
return guesser_test::$function_exists;
}
class guesser_test extends \phpbb_test_case
{
public static $function_exists = false;
protected $fileinfo_supported = false;
public function setUp(): void
{
global $phpbb_root_path;
$guessers = array(
new \Symfony\Component\HttpFoundation\File\MimeType\FileinfoMimeTypeGuesser(),
new \Symfony\Component\HttpFoundation\File\MimeType\FileBinaryMimeTypeGuesser(),
new \phpbb\mimetype\extension_guesser,
new \phpbb\mimetype\content_guesser,
);
// Check if any guesser except the extension_guesser is available
$this->fileinfo_supported = $guessers[0]->isSupported() | $guessers[1]->isSupported() | $guessers[3]->is_supported();
// Also create a guesser that emulates not having fileinfo available
$this->guesser_no_fileinfo = new \phpbb\mimetype\guesser(array($guessers[2]));
$this->guesser = new \phpbb\mimetype\guesser($guessers);
$this->path = dirname(__FILE__);
$this->jpg_file = $this->path . '/fixtures/jpg';
$this->phpbb_root_path = $phpbb_root_path;
}
public function data_guess_files()
{
return array(
array('image/gif', 'gif'),
array('image/png', 'png'),
array('image/jpeg', 'jpg'),
array('image/tiff', 'tif'),
array('text/html', 'txt'),
array(false, 'foobar'),
);
}
/**
* @dataProvider data_guess_files
*/
public function test_guess_files($expected, $file)
{
// We will always get application/octet-stream as mimetype if only the
// extension guesser is supported
if (!$this->fileinfo_supported)
{
$this->markTestSkipped('Unable to run tests depending on fileinfo if it is not available');
}
$this->assertEquals($expected, $this->guesser->guess($this->path . '/../upload/fixture/' . $file));
}
public function data_guess_files_no_fileinfo()
{
return array(
array('application/octet-stream', 'gif'),
array('application/octet-stream', 'txt'),
array(false, 'foobar'),
);
}
/**
* @dataProvider data_guess_files_no_fileinfo
*/
public function test_guess_files_no_fileinfo($expected, $file)
{
$this->assertEquals($expected, $this->guesser_no_fileinfo->guess($this->path . '/../upload/fixture/' . $file));
}
public function test_file_not_readable()
{
@chmod($this->jpg_file, 0000);
if (is_readable($this->jpg_file))
{
@chmod($this->jpg_file, 0644);
$this->markTestSkipped('is_readable always returns true if user is superuser or chmod does not work');
}
$this->assertEquals(false, $this->guesser->guess($this->jpg_file));
@chmod($this->jpg_file, 0644);
$this->assertEquals('image/jpeg', $this->guesser->guess($this->jpg_file));
}
public function test_null_guess()
{
$guesser = new \phpbb\mimetype\guesser(array(new \phpbb\mimetype\null_guesser));
$this->assertEquals('application/octet-stream', $guesser->guess($this->jpg_file));
}
public function data_incorrect_guessers()
{
return array(
array(array(new \phpbb\mimetype\incorrect_guesser)),
array(array(new \phpbb\mimetype\null_guesser(false))),
array(array()),
);
}
/**
* @dataProvider data_incorrect_guessers
*
* @expectedException \LogicException
*/
public function test_incorrect_guesser($guessers)
{
$guesser = new \phpbb\mimetype\guesser($guessers);
}
public function data_content_guesser()
{
return array(
array(
array(
'image/jpeg',
'image/jpeg',
),
array(new \phpbb\mimetype\content_guesser),
false,
),
array(
array(
'application/octet-stream',
'application/octet-stream',
),
array(new \phpbb\mimetype\content_guesser),
true,
),
array(
array(
'application/octet-stream',
'image/jpeg',
),
array(new \phpbb\mimetype\extension_guesser),
),
);
}
/**
* @dataProvider data_content_guesser
*/
public function test_content_guesser($expected, $guessers, $overload = false)
{
$supported = false;
self::$function_exists = !$overload;
if (!\function_exists('mime_content_type'))
{
$this->markTestSkipped('Emulating supported mime_content_type() when it is not supported will cause a fatal error');
}
// Cover possible LogicExceptions
foreach ($guessers as $cur_guesser)
{
$supported += $cur_guesser->is_supported();
}
if (!$supported)
{
$this->expectException('\LogicException');
}
$guesser = new \phpbb\mimetype\guesser($guessers);
$this->assertEquals($expected[0], $guesser->guess($this->jpg_file));
$this->assertEquals($expected[1], $guesser->guess($this->jpg_file, $this->jpg_file . '.jpg'));
@copy($this->jpg_file, $this->jpg_file . '.jpg');
$this->assertEquals($expected[1], $guesser->guess($this->jpg_file . '.jpg'));
@unlink($this->jpg_file . '.jpg');
}
public function test_sort_priority()
{
$guessers = array(
'FileinfoMimeTypeGuesser' => new \Symfony\Component\HttpFoundation\File\MimeType\FileinfoMimeTypeGuesser,
'extension_guesser' => new \phpbb\mimetype\extension_guesser,
'FileBinaryMimeTypeGuesser' => new \Symfony\Component\HttpFoundation\File\MimeType\FileBinaryMimeTypeGuesser,
'content_guesser' => new \phpbb\mimetype\content_guesser,
);
$guessers['content_guesser']->set_priority(5);
$guessers['extension_guesser']->set_priority(-5);
usort($guessers, array($this->guesser, 'sort_priority'));
$this->assertInstanceOf('\phpbb\mimetype\content_guesser', $guessers[0]);
$this->assertInstanceOf('\phpbb\mimetype\extension_guesser', $guessers[3]);
}
public function data_choose_mime_type()
{
return array(
array('application/octet-stream', 'application/octet-stream', null),
array('application/octet-stream', 'application/octet-stream', 'application/octet-stream'),
array('binary', 'application/octet-stream', 'binary'),
array('image/jpeg', 'application/octet-stream', 'image/jpeg'),
array('image/jpeg', 'binary', 'image/jpeg'),
array('image/jpeg', 'image/jpg', 'image/jpeg'),
array('image/jpeg', 'image/jpeg', 'binary'),
);
}
/**
* @dataProvider data_choose_mime_type
*/
public function test_choose_mime_type($expected, $mime_type, $guess)
{
$this->assertSame($expected, $this->guesser->choose_mime_type($mime_type, $guess));
}
}
|