|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Unit tests covering _wp_scan_utf8(). |
| 4 | + * |
| 5 | + * @package WordPress |
| 6 | + * @subpackage Charset |
| 7 | + * |
| 8 | + * @group compat |
| 9 | + * |
| 10 | + * @covers ::_wp_scan_utf8 |
| 11 | + */ |
| 12 | +class Tests_Compat_wpScanUtf8 extends WP_UnitTestCase { |
| 13 | + |
| 14 | + /** |
| 15 | + * Ensures that valid UTF-8 is scanned to the end, counting every code point. |
| 16 | + * |
| 17 | + * @ticket 65817 |
| 18 | + * |
| 19 | + * @dataProvider data_valid_utf8 |
| 20 | + * |
| 21 | + * @param string $text Valid UTF-8 text without noncharacters. |
| 22 | + */ |
| 23 | + public function test_counts_code_points_in_valid_utf8( string $text ) { |
| 24 | + $at = 0; |
| 25 | + $invalid_length = 0; |
| 26 | + $has_noncharacters = null; |
| 27 | + |
| 28 | + $count = _wp_scan_utf8( $text, $at, $invalid_length, null, null, $has_noncharacters ); |
| 29 | + |
| 30 | + $this->assertSame( mb_strlen( $text, 'UTF-8' ), $count, 'Should have counted every code point.' ); |
| 31 | + $this->assertSame( strlen( $text ), $at, 'Should have scanned to the end of the text.' ); |
| 32 | + $this->assertSame( 0, $invalid_length, 'Should not have found any invalid bytes.' ); |
| 33 | + $this->assertFalse( $has_noncharacters, 'Should not have found any noncharacters.' ); |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Data provider for {@see self::test_counts_code_points_in_valid_utf8()}. |
| 38 | + * |
| 39 | + * @return array<non-falsy-string, array{ 0: string }> |
| 40 | + */ |
| 41 | + public static function data_valid_utf8(): array { |
| 42 | + return array( |
| 43 | + 'Empty string' => array( '' ), |
| 44 | + 'US-ASCII' => array( 'Hello, World!' ), |
| 45 | + 'Two-byte sequences' => array( "Pi\u{00F1}a \u{00E9}t\u{00E9}" ), |
| 46 | + 'Three-byte sequences' => array( "\u{65E5}\u{672C}\u{8A9E}" ), |
| 47 | + 'Four-byte sequences' => array( "\u{1F600}\u{1F170}\u{10348}" ), |
| 48 | + 'Mixed sequence lengths' => array( "a\u{00F1}\u{65E5}\u{1F600}z" ), |
| 49 | + 'Boundaries between lengths' => array( "\u{007F}\u{0080}\u{07FF}\u{0800}\u{FFFD}\u{10000}\u{10FFFD}" ), |
| 50 | + 'Combining character' => array( "e\u{0301}" ), |
| 51 | + 'Hangul syllable as conjoining jamo' => array( "\u{1112}\u{1161}\u{11AB}" ), |
| 52 | + 'Devanagari grapheme clusters' => array( "\u{0928}\u{092E}\u{0938}\u{094D}\u{0924}\u{0947}" ), |
| 53 | + 'Regional indicator pair (flag)' => array( "\u{1F1E7}\u{1F1E9}" ), |
| 54 | + 'Emoji with skin tone modifier' => array( "\u{1F44D}\u{1F3FD}" ), |
| 55 | + 'Keycap sequence' => array( "1\u{FE0F}\u{20E3}" ), |
| 56 | + 'ZWJ sequence' => array( "\u{1F469}\u{200D}\u{1F4BB}" ), |
| 57 | + 'ZWJ family sequence' => array( "\u{1F468}\u{200D}\u{1F469}\u{200D}\u{1F467}\u{200D}\u{1F466}" ), |
| 58 | + 'ZWJ sequence with VS-16' => array( "\u{1F3F3}\u{FE0F}\u{200D}\u{1F308}" ), |
| 59 | + ); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Ensures that each maximal subpart of an invalid byte sequence is reported |
| 64 | + * as its own invalid span, so that it can be replaced by a single U+FFFD. |
| 65 | + * |
| 66 | + * @ticket 65817 |
| 67 | + * |
| 68 | + * @dataProvider data_invalid_utf8 |
| 69 | + * |
| 70 | + * @param string $text Text containing invalid UTF-8 bytes. |
| 71 | + * @param list<positive-int> $expected_spans Byte length of each invalid span, in order. |
| 72 | + * @param positive-int $expected_code_points Code points when each invalid span counts as one. |
| 73 | + */ |
| 74 | + public function test_finds_maximal_subparts_of_invalid_bytes( string $text, array $expected_spans, int $expected_code_points ) { |
| 75 | + $end = strlen( $text ); |
| 76 | + $at = 0; |
| 77 | + $invalid_length = 0; |
| 78 | + $invalid_spans = array(); |
| 79 | + $code_points = 0; |
| 80 | + |
| 81 | + while ( $at < $end ) { |
| 82 | + $code_points += _wp_scan_utf8( $text, $at, $invalid_length ); |
| 83 | + |
| 84 | + if ( 0 === $invalid_length ) { |
| 85 | + break; |
| 86 | + } |
| 87 | + |
| 88 | + $invalid_spans[] = $invalid_length; |
| 89 | + ++$code_points; |
| 90 | + $at += $invalid_length; |
| 91 | + } |
| 92 | + |
| 93 | + $this->assertSame( $expected_spans, $invalid_spans, 'Should have found the maximal subpart of each invalid span.' ); |
| 94 | + $this->assertSame( $expected_code_points, $code_points, 'Should have counted one code point for each invalid span.' ); |
| 95 | + $this->assertSame( $end, $at, 'Should have scanned to the end of the text.' ); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Data provider for {@see self::test_finds_maximal_subparts_of_invalid_bytes()}. |
| 100 | + * |
| 101 | + * @return array<non-falsy-string, array{ 0: string, 1: list<positive-int>, 2: positive-int }> |
| 102 | + */ |
| 103 | + public static function data_invalid_utf8(): array { |
| 104 | + return array( |
| 105 | + 'Lone continuation byte' => array( "a\x80b", array( 1 ), 3 ), |
| 106 | + 'Run of continuation bytes' => array( "a\x80\xBF\x80b", array( 1, 1, 1 ), 5 ), |
| 107 | + 'Truncated two-byte sequence' => array( "a\xC3b", array( 1 ), 3 ), |
| 108 | + 'Truncated three-byte sequence' => array( "a\xE2\x82b", array( 2 ), 3 ), |
| 109 | + 'Truncated four-byte sequence' => array( "a\xF0\x9F\x98b", array( 3 ), 3 ), |
| 110 | + 'Truncated sequence at the end' => array( "abc\xF0\x9F\x98", array( 3 ), 4 ), |
| 111 | + 'Overlong two-byte sequence' => array( "a\xC0\xAFb", array( 1, 1 ), 4 ), |
| 112 | + 'Overlong three-byte sequence' => array( "a\xE0\x80\xAFb", array( 1, 1, 1 ), 5 ), |
| 113 | + 'UTF-16 surrogate half' => array( "a\xED\xA0\x80b", array( 1, 1, 1 ), 5 ), |
| 114 | + 'Code point above U+10FFFF' => array( "a\xF4\x90\x80\x80b", array( 1, 1, 1, 1 ), 6 ), |
| 115 | + 'Byte that never appears in UTF-8' => array( "a\xFFb", array( 1 ), 3 ), |
| 116 | + 'Windows-1252 encoded text' => array( "Pi\xF1a", array( 1 ), 4 ), |
| 117 | + 'Truncated sequences back to back' => array( "\xE2\x82\xF0\x9F\x98", array( 2, 3 ), 2 ), |
| 118 | + 'Invalid span between valid multibyte' => array( "\u{1F600}\xE2\x82\u{65E5}", array( 2 ), 3 ), |
| 119 | + ); |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Tests scanning UTF-8 string containing noncharacters. |
| 124 | + * |
| 125 | + * @ticket 65817 |
| 126 | + * |
| 127 | + * @dataProvider data_strings_with_noncharacters |
| 128 | + * |
| 129 | + * @param string $text UTF-8 string containing a noncharacter. |
| 130 | + */ |
| 131 | + public function test_scan_string_with_noncharacters( string $text ) { |
| 132 | + $at = 0; |
| 133 | + $invalid_length = 0; |
| 134 | + $has_noncharacters = null; |
| 135 | + |
| 136 | + _wp_scan_utf8( $text, $at, $invalid_length, null, null, $has_noncharacters ); |
| 137 | + |
| 138 | + $this->assertTrue( $has_noncharacters ); |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Data provider for {@see self::test_scan_string_with_noncharacters()}. |
| 143 | + * |
| 144 | + * @return array<non-falsy-string, array{ 0: string }> |
| 145 | + */ |
| 146 | + public static function data_strings_with_noncharacters(): array { |
| 147 | + return array( |
| 148 | + 'U+FFFF (3-byte noncharacter)' => array( "test \xEF\xBF\xBF end" ), |
| 149 | + 'U+FFFE (3-byte noncharacter)' => array( "test \xEF\xBF\xBE end" ), |
| 150 | + 'U+FDD0 (3-byte noncharacter)' => array( "test \xEF\xB7\x90 end" ), |
| 151 | + 'U+1FFFF (4-byte noncharacter)' => array( "test \xF0\x9F\xBF\xBF end" ), |
| 152 | + ); |
| 153 | + } |
| 154 | +} |
0 commit comments