| 1 |
<?php |
| 2 |
|
| 3 |
namespace Test\Parsoid\Utils; |
| 4 |
|
| 5 |
use PHPUnit\Framework\TestCase; |
| 6 |
use Wikimedia\Parsoid\Utils\PHPUtils; |
| 7 |
|
| 8 |
/** |
| 9 |
* @coversDefaultClass \Wikimedia\Parsoid\Utils\PHPUtils |
| 10 |
*/ |
| 11 |
class PHPUtilsTest extends TestCase { |
| 12 |
/** |
| 13 |
* @covers \Wikimedia\Parsoid\Utils\PHPUtils::reStrip |
| 14 |
* @dataProvider provideReStrip |
| 15 |
*/ |
| 16 |
public function testReStrip( $re, $delimiter, $expected ) { |
| 17 |
$actual = PHPUtils::reStrip( $re, $delimiter ); |
| 18 |
$this->assertEquals( $expected, $actual ); |
| 19 |
} |
| 20 |
|
| 21 |
public function provideReStrip() { |
| 22 |
return [ |
| 23 |
[ '/abc/', null, 'abc' ], // easy case |
| 24 |
[ ' /abc/mA', null, 'abc' ], // flags and leading whitespace |
| 25 |
[ '~ab/de~x', '/', 'ab\\/de' ], // new delimiter |
| 26 |
[ '~ab\/de~x', '/', 'ab\/de' ], // already escaped |
| 27 |
[ "~\\\u{1F4A9}~u", '/', "\\\u{1F4A9}" ], // unicode safe |
| 28 |
]; |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* @covers \Wikimedia\Parsoid\Utils\PHPUtils::safeSubstr |
| 33 |
* @covers \Wikimedia\Parsoid\Utils\PHPUtils::assertValidUTF8 |
| 34 |
* @dataProvider provideSafeSubstr |
| 35 |
*/ |
| 36 |
public function testSafeSubstr( string $s, int $start, int $end, bool $expectedOk ) { |
| 37 |
// The full string needs to be valid UTF-8 |
| 38 |
PHPUtils::assertValidUTF8( $s ); |
| 39 |
PHPUtils::safeSubstr( $s, 0, null, true ); |
| 40 |
|
| 41 |
// Fast verification |
| 42 |
$s1 = false; |
| 43 |
try { |
| 44 |
$s1 = PHPUtils::safeSubstr( $s, $start, $end - $start ); |
| 45 |
$ok = true; |
| 46 |
} catch ( \Exception $e ) { |
| 47 |
$ok = false; |
| 48 |
} |
| 49 |
$this->assertEquals( $expectedOk, $ok ); |
| 50 |
|
| 51 |
// Now try full string verification |
| 52 |
$s2 = substr( $s, $start, $end - $start ); |
| 53 |
try { |
| 54 |
PHPUtils::assertValidUTF8( $s2 ); |
| 55 |
$ok = true; |
| 56 |
} catch ( \Exception $e ) { |
| 57 |
$ok = false; |
| 58 |
} |
| 59 |
$this->assertEquals( $expectedOk, $ok ); |
| 60 |
if ( $ok ) { |
| 61 |
$this->assertEquals( $s1, $s2 ); |
| 62 |
} |
| 63 |
|
| 64 |
// Full string verification, take 2 |
| 65 |
$s3 = false; |
| 66 |
try { |
| 67 |
$s3 = PHPUtils::safeSubstr( $s, $start, $end - $start, true ); |
| 68 |
$ok = true; |
| 69 |
} catch ( \Exception $e ) { |
| 70 |
$ok = false; |
| 71 |
} |
| 72 |
$this->assertEquals( $expectedOk, $ok ); |
| 73 |
$this->assertEquals( $s1, $s3 ); |
| 74 |
} |
| 75 |
|
| 76 |
public function provideSafeSubstr() { |
| 77 |
$cases = [ |
| 78 |
// Simple case |
| 79 |
[ 'input' => 'abc', 'indices' => [ |
| 80 |
[ 0, 3, true ], [ 1, 3, true ], [ 2, 3, true ], |
| 81 |
[ 0, 2, true ], [ 0, 1, true ], [ 0, 0, true ], |
| 82 |
] ], |
| 83 |
// 1-byte codepoint, then 2-byte codepoint, then 3-byte codepoint, |
| 84 |
// then 4-byte codepoint = 1+2+3+4 = 10 bytes long |
| 85 |
[ 'input' => "A\u{0531}\u{4EBA}\u{1F4A9}", 'indices' => [ |
| 86 |
// truncating the head |
| 87 |
[ 0, 10, true ], [ 1, 10, true ], [ 2, 10, false ], |
| 88 |
[ 3, 10, true ], [ 4, 10, false ], [ 5, 10, false ], |
| 89 |
[ 6, 10, true ], [ 7, 10, false ], [ 8, 10, false ], |
| 90 |
[ 9, 10, false ], [ 10, 10, true ], |
| 91 |
// truncating the tail |
| 92 |
[ 0, 0, true ], [ 0, 1, true ], [ 0, 2, false ], |
| 93 |
[ 0, 3, true ], [ 0, 4, false ], [ 0, 5, false ], |
| 94 |
[ 0, 6, true ], [ 0, 7, false ], [ 0, 8, false ], |
| 95 |
[ 0, 9, false ], [ 0, 10, true ], |
| 96 |
// three examples from the middle |
| 97 |
[ 1, 6, true ], |
| 98 |
[ 2, 6, false ], |
| 99 |
[ 1, 7, false ], |
| 100 |
] ], |
| 101 |
]; |
| 102 |
// expand these by duplicating the input string for better |
| 103 |
// diagnostics if things fail |
| 104 |
foreach ( $cases as $c ) { |
| 105 |
foreach ( $c['indices'] as $idx ) { |
| 106 |
yield [ $c['input'], $idx[0], $idx[1], $idx[2] ]; |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* @covers ::assertValidUTF8 |
| 113 |
* @dataProvider provideAssertValidUTF8 |
| 114 |
*/ |
| 115 |
public function testAssertValidUTF8( string $s, bool $expectedOk ) { |
| 116 |
if ( !$expectedOk ) { |
| 117 |
$this->expectException( \Exception::class ); |
| 118 |
} |
| 119 |
PHPUtils::assertValidUTF8( $s ); |
| 120 |
// This assertion doesn't do anything, but it increases the |
| 121 |
// assertion counter so PHPUnit knows something was checked. |
| 122 |
$this->assertTrue( $expectedOk ); |
| 123 |
} |
| 124 |
|
| 125 |
public function provideAssertValidUTF8() { |
| 126 |
// Our UTF8 validity checker uses `//u` as an optimization, which |
| 127 |
// relies on PHP's pcre extension to pass PCRE2_UTF and not |
| 128 |
// PCRE2_NO_UTF_CHECK or PCRE2_MATCH_INVALID_UTF8 through to pcre, |
| 129 |
// and then requires pcre to actually implement a thorough check; |
| 130 |
// https://www.pcre.org/current/doc/html/pcre2unicode.html mentions |
| 131 |
// "the check is applied only to that part of the subject that could |
| 132 |
// be inspected during matching" *when called with a non-zero offset*; |
| 133 |
// if this optimization were in the future extended to calls with |
| 134 |
// a zero offset (the way we use pcre_match) or PHP were to optimize |
| 135 |
// string representation such that the low-level string passed into |
| 136 |
// pcre were to have a non-zero offset, then conceivable PCRE2 could |
| 137 |
// skip the check entirely on a zero-length match like `//u`. |
| 138 |
// Anyway, these test cases *attempt* to protect against future |
| 139 |
// regressions of this sort. |
| 140 |
return [ |
| 141 |
// valid input should be valid |
| 142 |
[ 'abcdef', true ], |
| 143 |
// bad input at start and end should be caught |
| 144 |
[ "\x80", false ], |
| 145 |
[ "abcdef\x80", false ], |
| 146 |
// two byte sequences |
| 147 |
[ "xx\xC2\x80xx", true ], // U+0080, valid |
| 148 |
[ "xx\xC0\x80xx", false ], // invalid (would encode U+0000) |
| 149 |
[ "xx\xC1\xBFxx", false ], // invalid (would encode U+007F) |
| 150 |
[ "xx\xC2\xC2xx", false ], |
| 151 |
[ "xx\xC2\x80\x80xx", false ], |
| 152 |
[ "xx\xC2", false ], // at end of string |
| 153 |
// three byte sequences |
| 154 |
[ "xx\xE0\xA0\x80xx", true ], // U+0800, valid |
| 155 |
[ "xx\xE0\x80\x80xx", false ], // invalid (would encode U+0000) |
| 156 |
[ "xx\xE0\x9F\xBFxx", false ], // invalid (would encode U+07FF) |
| 157 |
[ "xx\xE0\xA0\xA0\xA0xx", false ], |
| 158 |
[ "xx\xE0x\xA0xxx", false ], |
| 159 |
[ "xx\xE0\xA0xxxx", false ], |
| 160 |
[ "xx\xE0\xA0", false ], // at end of string |
| 161 |
[ "xx\xE0", false ], |
| 162 |
// four byte sequences |
| 163 |
[ "xx\xF0\x90\x80\x80xxx", true ], // U+10000, valid |
| 164 |
[ "xx\xF0\x80\x80\x80xxx", false ], // invalid (would encode U+0000) |
| 165 |
[ "xx\xF0\x8F\xBF\xBFxxx", false ], // invalid (would encode U+FFFF) |
| 166 |
[ "xx\xF0x\x90\x90xxx", false ], |
| 167 |
[ "xx\xF0\x90x\x90xxx", false ], |
| 168 |
[ "xx\xF0\x90\x90xxxx", false ], |
| 169 |
[ "xx\xF0\x90\x90", false ], // at end of string |
| 170 |
[ "xx\xF0\x90", false ], |
| 171 |
[ "xx\xF0", false ], |
| 172 |
// five byte sequences (obsolete RFC2279, should be caught) |
| 173 |
[ "xx\xF8\x88\x80\x80\x80xx", false ], // U+20 0000 |
| 174 |
[ "xx\xFB\xBF\xBF\xBF\xBFxx", false ], // U+3FF FFFF |
| 175 |
[ "xx\xF8\x80\x80\x80\x80xx", false ], // (would encode U+0000) |
| 176 |
// six byte sequences (obsolete RFC2279, should be caught) |
| 177 |
[ "xx\xFC\x84\x80\x80\x80\x80xx", false ], // U+400 0000 |
| 178 |
[ "xx\xFC\x80\x80\x80\x80\x80xx", false ], // (would encode U+0000) |
| 179 |
// characters above 10FFFF should be caught |
| 180 |
[ "xx\xF4\x8F\xBF\xBFxx", true ], // U+10FFFF |
| 181 |
[ "xx\xF4\x90\x80\x80xx", false ], // U+110000 |
| 182 |
[ "xx\xF4\x9F\xBF\xBFxx", false ], // U+11FFFF |
| 183 |
[ "xx\xF7\xBF\xBF\xBFxx", false ], // U+1FFFFF |
| 184 |
// surrogate characters in the range D800-DFFF should be caught |
| 185 |
[ "xx\xED\x9F\xBFxx", true ], // U+D7FF |
| 186 |
[ "xx\xED\xA0\x80xx", false ], // U+D800 |
| 187 |
[ "xx\xED\xAF\xBFxx", false ], // U+DBFF |
| 188 |
[ "xx\xED\xB0\x80xx", false ], // U+DC00 |
| 189 |
[ "xx\xED\xBF\xBFxx", false ], // U+DFFF |
| 190 |
// and make sure it's not just unpaired surrogates which are |
| 191 |
// being caught, try some valid surrogate pairs as well |
| 192 |
[ "xx\xED\xA0\x81\xED\xB0\xB7xx", false ], // U+10437 -> U+D801 U+DC37 |
| 193 |
[ "xx\xED\xA1\x92\xED\xBD\xA2xx", false ], // U+24B62 -> U+D852 U+DF62 |
| 194 |
]; |
| 195 |
} |
| 196 |
} |