‪TYPO3CMS  10.4
GeneralUtilityTest.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
18 use Prophecy\Argument;
19 use Psr\Http\Message\ResponseInterface;
20 use Psr\Http\Message\StreamInterface;
23 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
24 
28 class ‪GeneralUtilityTest extends UnitTestCase
29 {
33  protected ‪$resetSingletonInstances = true;
34 
41  public function ‪idnaEncodeConvertsUnicodeCharsToASCIIString($actual, $expected)
42  {
43  $result = ‪GeneralUtility::idnaEncode($actual);
44  self::assertSame($expected, $result);
45  }
46 
53  public function ‪idnaEncodeDataProvider()
54  {
55  return [
56  'empty string' => [
57  '',
58  ''
59  ],
60  'null value' => [
61  null,
62  ''
63  ],
64  'string with ascii chars' => [
65  'example',
66  'example'
67  ],
68  'domain (1) with utf8 chars' => [
69  'dömäin.example',
70  'xn--dmin-moa0i.example'
71  ],
72  'domain (2) with utf8 chars' => [
73  'äaaa.example',
74  'xn--aaa-pla.example'
75  ],
76  'domain (3) with utf8 chars' => [
77  'déjà.vu.example',
78  'xn--dj-kia8a.vu.example'
79  ],
80  'domain (4) with utf8 chars' => [
81  'foo.âbcdéf.example',
82  'foo.xn--bcdf-9na9b.example'
83  ],
84  'domain with utf8 char (german umlaut)' => [
85  'exömple.com',
86  'xn--exmple-xxa.com'
87  ],
88  'email with utf8 char (german umlaut)' => [
89  'joe.doe@dömäin.de',
90  'joe.doe@xn--dmin-moa0i.de'
91  ]
92  ];
93  }
94 
98  public function ‪deniedFilesWithoutDenyPatternDataProvider(): array
99  {
100  return [
101  'Nul character in file' => ['image' . "\0" . '.gif'],
102  'Nul character in file with .php' => ['image.php' . "\0" . '.gif'],
103  'Nul character and UTF-8 in file' => ['Ссылка' . "\0" . '.gif'],
104  'Nul character and Latin-1 in file' => ['ÉÐØ' . "\0" . '.gif'],
105  ];
106  }
107 
115  public function ‪verifyNulCharacterFilesAgainstPatternWithoutFileDenyPattern(string $deniedFile)
116  {
117  ‪$GLOBALS['TYPO3_CONF_VARS']['BE']['fileDenyPattern'] = '';
118  self::assertFalse(GeneralUtility::verifyFilenameAgainstDenyPattern($deniedFile));
119  }
120 
124  public function ‪deniedFilesWithDefaultDenyPatternDataProvider(): array
125  {
126  $data = [
127  'Nul character in file' => ['image' . "\0", '.gif'],
128  'Nul character in file with .php' => ['image.php' . "\0", '.gif'],
129  'Nul character and UTF-8 in file' => ['Ссылка' . "\0", '.gif'],
130  'Nul character and Latin-1 in file' => ['ÉÐØ' . "\0", '.gif'],
131  'Lower umlaut .php file' => ['üWithFile', '.php'],
132  'Upper umlaut .php file' => ['fileWithÜ', '.php'],
133  'invalid UTF-8-sequence' => ["\xc0" . 'file', '.php'],
134  'Could be overlong NUL in some UTF-8 implementations, invalid in RFC3629' => ["\xc0\x80" . 'file', '.php'],
135  'Regular .php file' => ['file' , '.php'],
136  'Regular .php3 file' => ['file', '.php3'],
137  'Regular .php5 file' => ['file', '.php5'],
138  'Regular .php7 file' => ['file', '.php7'],
139  'Regular .phpsh file' => ['file', '.phpsh'],
140  'Regular .phtml file' => ['file', '.phtml'],
141  'Regular .pht file' => ['file', '.pht'],
142  'Regular .phar file' => ['file', '.phar'],
143  'Regular .shtml file' => ['file', '.shtml'],
144  'Regular .cgi file' => ['file', '.cgi'],
145  'Regular .pl file' => ['file', '.pl'],
146  'Wrapped .php file ' => ['file', '.php.txt'],
147  'Wrapped .php3 file' => ['file', '.php3.txt'],
148  'Wrapped .php5 file' => ['file', '.php5.txt'],
149  'Wrapped .php7 file' => ['file', '.php7.txt'],
150  'Wrapped .phpsh file' => ['file', '.phpsh.txt'],
151  'Wrapped .phtml file' => ['file', '.phtml.txt'],
152  'Wrapped .pht file' => ['file', '.pht.txt'],
153  'Wrapped .phar file' => ['file', '.phar.txt'],
154  'Wrapped .shtml file' => ['file', '.shtml.txt'],
155  'Wrapped .cgi file' => ['file', '.cgi.txt'],
156  // allowed "Wrapped .pl file" in order to allow language specific files containing ".pl."
157  '.htaccess file' => ['', '.htaccess'],
158  ];
159 
160  // Mixing with regular utf-8
161  $utf8Characters = 'Ссылка';
162  foreach ($data as $key => $value) {
163  if ($value[0] === '') {
164  continue;
165  }
166  $data[$key . ' with UTF-8 characters prepended'] = [$utf8Characters . $value[0], $value[1]];
167  $data[$key . ' with UTF-8 characters appended'] = [$value[0] . $utf8Characters, $value[1]];
168  }
169 
170  // combine to single value
171  $data = array_map(
172  function (array $values): array {
173  return [implode('', $values)];
174  },
175  $data
176  );
177 
178  // Encoding with UTF-16
179  foreach ($data as $key => $value) {
180  $data[$key . ' encoded with UTF-16'] = [mb_convert_encoding($value[0], 'UTF-16')];
181  }
182 
183  return $data;
184  }
185 
194  {
195  self::assertFalse(GeneralUtility::verifyFilenameAgainstDenyPattern($deniedFile));
196  }
197 
201  public function ‪allowedFilesDataProvider(): array
202  {
203  return [
204  'Regular .gif file' => ['image.gif'],
205  'Regular uppercase .gif file' => ['IMAGE.gif'],
206  'UTF-8 .gif file' => ['Ссылка.gif'],
207  'Lower umlaut .jpg file' => ['üWithFile.jpg'],
208  'Upper umlaut .png file' => ['fileWithÜ.png'],
209  'Latin-1 .gif file' => ['ÉÐØ.gif'],
210  'Wrapped .pl file' => ['file.pl.txt'],
211  ];
212  }
213 
221  public function ‪verifyFilenameAgainstDenyPatternAcceptAllowedFiles(string $allowedFile)
222  {
223  self::assertTrue(GeneralUtility::verifyFilenameAgainstDenyPattern($allowedFile));
224  }
225 
226  public function ‪splitHeaderLinesDataProvider(): array
227  {
228  return [
229  'multi-line headers' => [
230  ['Content-Type' => 'multipart/form-data; boundary=something', 'Content-Language' => 'de-DE, en-CA'],
231  ['Content-Type' => 'multipart/form-data; boundary=something', 'Content-Language' => 'de-DE, en-CA'],
232  ]
233  ];
234  }
235 
242  public function ‪splitHeaderLines(array $headers, array $expectedHeaders): void
243  {
244  $stream = $this->prophesize(StreamInterface::class);
245  $response = $this->prophesize(ResponseInterface::class);
246  $response->getBody()->willReturn($stream);
247  $requestFactory = $this->prophesize(RequestFactory::class);
248  $requestFactory->request(Argument::cetera())->willReturn($response);
249 
250  GeneralUtility::addInstance(RequestFactory::class, $requestFactory->reveal());
251  ‪GeneralUtility::getUrl('http://example.com', 0, $headers);
252 
253  $requestFactory->request(Argument::any(), Argument::any(), ['headers' => $expectedHeaders])->shouldHaveBeenCalled();
254  }
255 
261  public static function ‪IPv6Hex2BinDataProviderCorrect()
262  {
263  return [
264  'empty 1' => ['::', str_pad('', 16, "\x00")],
265  'empty 2, already normalized' => ['0000:0000:0000:0000:0000:0000:0000:0000', str_pad('', 16, "\x00")],
266  'already normalized' => ['0102:0304:0000:0000:0000:0000:0506:0078', "\x01\x02\x03\x04" . str_pad('', 8, "\x00") . "\x05\x06\x00\x78"],
267  'expansion in middle 1' => ['1::2', "\x00\x01" . str_pad('', 12, "\x00") . "\x00\x02"],
268  'expansion in middle 2' => ['beef::fefa', "\xbe\xef" . str_pad('', 12, "\x00") . "\xfe\xfa"],
269  ];
270  }
271 
276  public function ‪IPv6Hex2BinCorrectlyConvertsAddresses($hex, $binary)
277  {
278  self::assertTrue(GeneralUtility::IPv6Hex2Bin($hex) === $binary);
279  }
280 
286  public static function ‪IPv6Bin2HexDataProviderCorrect()
287  {
288  return [
289  'empty' => [str_pad('', 16, "\x00"), '::'],
290  'non-empty front' => ["\x01" . str_pad('', 15, "\x00"), '100::'],
291  'non-empty back' => [str_pad('', 15, "\x00") . "\x01", '::1'],
292  'normalized' => ["\x01\x02\x03\x04" . str_pad('', 8, "\x00") . "\x05\x06\x00\x78", '102:304::506:78'],
293  'expansion in middle 1' => ["\x00\x01" . str_pad('', 12, "\x00") . "\x00\x02", '1::2'],
294  'expansion in middle 2' => ["\xbe\xef" . str_pad('', 12, "\x00") . "\xfe\xfa", 'beef::fefa'],
295  ];
296  }
297 
302  public function ‪IPv6Bin2HexCorrectlyConvertsAddresses($binary, $hex)
303  {
304  self::assertEquals(GeneralUtility::IPv6Bin2Hex($binary), $hex);
305  }
306 
312  public static function ‪normalizeCompressIPv6DataProviderCorrect()
313  {
314  return [
315  'empty' => ['::', '0000:0000:0000:0000:0000:0000:0000:0000'],
316  'localhost' => ['::1', '0000:0000:0000:0000:0000:0000:0000:0001'],
317  'expansion in middle 1' => ['1::2', '0001:0000:0000:0000:0000:0000:0000:0002'],
318  'expansion in middle 2' => ['1:2::3', '0001:0002:0000:0000:0000:0000:0000:0003'],
319  'expansion in middle 3' => ['1::2:3', '0001:0000:0000:0000:0000:0000:0002:0003'],
320  'expansion in middle 4' => ['1:2::3:4:5', '0001:0002:0000:0000:0000:0003:0004:0005']
321  ];
322  }
323 
328  public function ‪compressIPv6CorrectlyCompressesAddresses($compressed, $normalized)
329  {
330  self::assertEquals($compressed, GeneralUtility::compressIPv6($normalized));
331  }
332 
337  {
338  if (strtolower(PHP_OS) === 'darwin') {
339  self::markTestSkipped('This test does not work on OSX / Darwin OS.');
340  }
341  self::assertEquals('::f0f', GeneralUtility::compressIPv6('0000:0000:0000:0000:0000:0000:0000:0f0f'));
342  }
343 }
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Utility\GeneralUtilityTest\IPv6Bin2HexCorrectlyConvertsAddresses
‪IPv6Bin2HexCorrectlyConvertsAddresses($binary, $hex)
Definition: GeneralUtilityTest.php:301
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Utility\GeneralUtilityTest\idnaEncodeDataProvider
‪idnaEncodeDataProvider()
Definition: GeneralUtilityTest.php:52
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Utility\GeneralUtilityTest
Definition: GeneralUtilityTest.php:29
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Utility\GeneralUtilityTest\deniedFilesWithDefaultDenyPatternDataProvider
‪array deniedFilesWithDefaultDenyPatternDataProvider()
Definition: GeneralUtilityTest.php:123
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Utility\GeneralUtilityTest\allowedFilesDataProvider
‪array allowedFilesDataProvider()
Definition: GeneralUtilityTest.php:200
‪TYPO3\CMS\Core\Utility\GeneralUtility\getUrl
‪static mixed getUrl($url, $includeHeader=0, $requestHeaders=null, &$report=null)
Definition: GeneralUtility.php:1748
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Utility\GeneralUtilityTest\verifyNulCharacterFilesAgainstPatternWithoutFileDenyPattern
‪verifyNulCharacterFilesAgainstPatternWithoutFileDenyPattern(string $deniedFile)
Definition: GeneralUtilityTest.php:114
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Utility\GeneralUtilityTest\verifyFilenameAgainstDenyPatternAcceptAllowedFiles
‪verifyFilenameAgainstDenyPatternAcceptAllowedFiles(string $allowedFile)
Definition: GeneralUtilityTest.php:220
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Utility\GeneralUtilityTest\IPv6Hex2BinCorrectlyConvertsAddresses
‪IPv6Hex2BinCorrectlyConvertsAddresses($hex, $binary)
Definition: GeneralUtilityTest.php:275
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Utility\GeneralUtilityTest\splitHeaderLinesDataProvider
‪splitHeaderLinesDataProvider()
Definition: GeneralUtilityTest.php:225
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Utility\GeneralUtilityTest\splitHeaderLines
‪splitHeaderLines(array $headers, array $expectedHeaders)
Definition: GeneralUtilityTest.php:241
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Utility\GeneralUtilityTest\compressIPv6CorrectlyCompressesAddresses
‪compressIPv6CorrectlyCompressesAddresses($compressed, $normalized)
Definition: GeneralUtilityTest.php:327
‪TYPO3\CMS\Core\Utility\GeneralUtility\idnaEncode
‪static string idnaEncode($value)
Definition: GeneralUtility.php:863
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Utility\GeneralUtilityTest\verifyFilenameAgainstDenyPatternDetectsNotAllowedFiles
‪verifyFilenameAgainstDenyPatternDetectsNotAllowedFiles($deniedFile)
Definition: GeneralUtilityTest.php:192
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Utility\GeneralUtilityTest\$resetSingletonInstances
‪bool $resetSingletonInstances
Definition: GeneralUtilityTest.php:32
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Utility
Definition: GeneralUtilityTest.php:16
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Utility\GeneralUtilityTest\deniedFilesWithoutDenyPatternDataProvider
‪array deniedFilesWithoutDenyPatternDataProvider()
Definition: GeneralUtilityTest.php:97
‪TYPO3\CMS\Core\Http\RequestFactory
Definition: RequestFactory.php:31
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Utility\GeneralUtilityTest\normalizeCompressIPv6DataProviderCorrect
‪static array normalizeCompressIPv6DataProviderCorrect()
Definition: GeneralUtilityTest.php:311
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Utility\GeneralUtilityTest\IPv6Bin2HexDataProviderCorrect
‪static array IPv6Bin2HexDataProviderCorrect()
Definition: GeneralUtilityTest.php:285
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Utility\GeneralUtilityTest\idnaEncodeConvertsUnicodeCharsToASCIIString
‪idnaEncodeConvertsUnicodeCharsToASCIIString($actual, $expected)
Definition: GeneralUtilityTest.php:40
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Utility\GeneralUtilityTest\compressIPv6CorrectlyCompressesAddressWithSomeAddressOnRightSide
‪compressIPv6CorrectlyCompressesAddressWithSomeAddressOnRightSide()
Definition: GeneralUtilityTest.php:335
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Utility\GeneralUtilityTest\IPv6Hex2BinDataProviderCorrect
‪static array IPv6Hex2BinDataProviderCorrect()
Definition: GeneralUtilityTest.php:260