|
| 1 | +<?php |
| 2 | + |
| 3 | +use PHPUnit\Framework\TestCase; |
| 4 | + |
| 5 | +require_once __DIR__ . '/../tools/query-corpus-decoding.php'; |
| 6 | + |
| 7 | +/** |
| 8 | + * Tests for reading the lines of MySQL server test files as UTF-8 text. |
| 9 | + */ |
| 10 | +class WP_MySQL_Query_Corpus_Decoding_Tests extends TestCase { |
| 11 | + /** |
| 12 | + * @dataProvider data_lines_in_character_sets |
| 13 | + */ |
| 14 | + public function test_line_is_read_in_its_character_set( string $charset, string $line, string $expected ): void { |
| 15 | + $this->assertSame( |
| 16 | + $expected, |
| 17 | + convert_to_utf8( $line, array( QUERY_CORPUS_CHARSET_ENCODINGS[ $charset ] ) ) |
| 18 | + ); |
| 19 | + } |
| 20 | + |
| 21 | + public function test_line_is_read_in_the_first_valid_encoding(): void { |
| 22 | + $koi8r_bytes = "SELECT '\xD4\xC5\xD3\xD4'"; |
| 23 | + |
| 24 | + // The client character set goes first, even when it reads the bytes differently. |
| 25 | + $this->assertSame( "SELECT 'тест'", convert_to_utf8( $koi8r_bytes, array( 'KOI8-R', 'Windows-1251' ) ) ); |
| 26 | + $this->assertSame( "SELECT 'ФЕУФ'", convert_to_utf8( $koi8r_bytes, array( 'Windows-1251', 'KOI8-R' ) ) ); |
| 27 | + |
| 28 | + // Bytes that aren't valid in the client character set fall back to the next one. |
| 29 | + $this->assertSame( "SELECT 'тест'", convert_to_utf8( $koi8r_bytes, array( 'UTF-8', 'KOI8-R' ) ) ); |
| 30 | + $this->assertSame( "SELECT 'é'", convert_to_utf8( "SELECT 'é'", array( 'UTF-8', 'KOI8-R' ) ) ); |
| 31 | + |
| 32 | + // Latin1 takes any bytes, so nothing is tried after it. |
| 33 | + $this->assertSame( "SELECT 'é'", convert_to_utf8( "SELECT 'é'", array( 'Windows-1252', 'UTF-8' ) ) ); |
| 34 | + |
| 35 | + // Bytes that aren't valid in any of the encodings are read as latin1. |
| 36 | + $this->assertSame( "SELECT 'é'", convert_to_utf8( "SELECT '\xE9'", array( 'UTF-8', 'SJIS' ) ) ); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * @dataProvider data_introduced_literals |
| 41 | + */ |
| 42 | + public function test_introduced_literal_is_read_in_its_character_set( string $encoding, string $line, string $expected ): void { |
| 43 | + $this->assertSame( $expected, convert_to_utf8( $line, array( $encoding ) ) ); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * @return array<string,array{0:string,1:string,2:string}> |
| 48 | + */ |
| 49 | + public function data_introduced_literals(): array { |
| 50 | + return array( |
| 51 | + 'utf8mb3 in a koi8r line' => array( 'KOI8-R', "SELECT _utf8mb3'тест'", "SELECT _utf8mb3'тест'" ), |
| 52 | + 'koi8r in a UTF-8 line' => array( 'UTF-8', "SELECT _koi8r'\xD4\xC5\xD3\xD4'", "SELECT _koi8r'тест'" ), |
| 53 | + 'latin1 in a UTF-8 line' => array( 'UTF-8', "SELECT _latin1'é'", "SELECT _latin1'é'" ), |
| 54 | + 'whitespace is kept' => array( 'UTF-8', "SELECT _koi8r \t'\xD4'", "SELECT _koi8r \t'т'" ), |
| 55 | + 'uppercase introducer' => array( 'UTF-8', "SELECT _KOI8R'\xD4'", "SELECT _KOI8R'т'" ), |
| 56 | + 'double quotes' => array( 'UTF-8', "SELECT _koi8r\"\xD4\"", 'SELECT _koi8r"т"' ), |
| 57 | + 'rest of the line' => array( 'KOI8-R', "SELECT '\xD4', _utf8mb3'т', '\xD4'", "SELECT 'т', _utf8mb3'т', 'т'" ), |
| 58 | + 'several introducers' => array( 'UTF-8', "SELECT _koi8r'\xD4', _latin1'\xE9', 'é'", "SELECT _koi8r'т', _latin1'é', 'é'" ), |
| 59 | + 'escaped quote' => array( 'UTF-8', "SELECT _koi8r'\xD4\\'\xD4', 'é'", "SELECT _koi8r'т\\'т', 'é'" ), |
| 60 | + 'doubled quote' => array( 'UTF-8', "SELECT _koi8r'\xD4''\xD4', 'é'", "SELECT _koi8r'т''т', 'é'" ), |
| 61 | + 'escaped backslash at the end' => array( 'UTF-8', "SELECT _koi8r'\xD4\\\\', 'é'", "SELECT _koi8r'т\\\\', 'é'" ), |
| 62 | + |
| 63 | + // Bytes that aren't valid in the character set use the encodings of the line. |
| 64 | + 'invalid in its character set' => array( 'KOI8-R', "SELECT _utf8mb3'\xD4'", "SELECT _utf8mb3'т'" ), |
| 65 | + |
| 66 | + // These aren't literals with an introducer that this file can read. |
| 67 | + 'unsupported introducer' => array( 'KOI8-R', "SELECT _ucs2'\xD4'", "SELECT _ucs2'т'" ), |
| 68 | + 'end of a column name' => array( 'UTF-8', "SELECT a_koi8r'\xD4'", "SELECT a_koi8r'Ô'" ), |
| 69 | + 'hexadecimal literal' => array( 'UTF-8', "SELECT _koi8r x'D4', '\xD4'", "SELECT _koi8r x'D4', 'Ô'" ), |
| 70 | + 'unterminated literal' => array( 'UTF-8', "SELECT _koi8r'\xD4", "SELECT _koi8r'Ô" ), |
| 71 | + ); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * @dataProvider data_client_charset_statements |
| 76 | + */ |
| 77 | + public function test_read_client_charset( string $line, ?string $expected ): void { |
| 78 | + $this->assertSame( $expected, read_client_charset( $line ) ); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * @return array<string,array{0:string,1:?string}> |
| 83 | + */ |
| 84 | + public function data_client_charset_statements(): array { |
| 85 | + return array( |
| 86 | + 'SET NAMES' => array( 'SET NAMES latin1;', 'latin1' ), |
| 87 | + 'lowercase and spaces' => array( ' set names KOI8R ;', 'koi8r' ), |
| 88 | + 'single quotes' => array( "SET NAMES 'latin1';", 'latin1' ), |
| 89 | + 'double quotes' => array( 'SET NAMES "latin1";', 'latin1' ), |
| 90 | + 'custom delimiter' => array( 'set names koi8r|', 'koi8r' ), |
| 91 | + 'long custom delimiter' => array( 'set names koi8r//', 'koi8r' ), |
| 92 | + 'collation' => array( 'SET NAMES utf8mb3 COLLATE utf8mb3_bin;', 'utf8mb3' ), |
| 93 | + 'another variable' => array( 'SET NAMES utf8mb3, @@character_set_results=NULL;', 'utf8mb3' ), |
| 94 | + 'default' => array( 'SET NAMES DEFAULT;', 'default' ), |
| 95 | + 'SET CHARACTER SET' => array( 'SET CHARACTER SET koi8r;', 'koi8r' ), |
| 96 | + 'SET CHARSET' => array( 'SET CHARSET koi8r;', 'koi8r' ), |
| 97 | + 'character_set_client' => array( 'SET character_set_client = latin1;', 'latin1' ), |
| 98 | + 'character_set_client no space' => array( 'SET character_set_client=latin1;', 'latin1' ), |
| 99 | + 'quoted character_set_client' => array( "SET character_set_client= 'latin1';", 'latin1' ), |
| 100 | + 'SESSION' => array( 'SET SESSION character_set_client=latin1;', 'latin1' ), |
| 101 | + 'LOCAL' => array( 'SET LOCAL character_set_client=latin1;', 'latin1' ), |
| 102 | + 'system variable' => array( 'SET @@character_set_client= cp1251;', 'cp1251' ), |
| 103 | + 'system variable with SESSION' => array( 'SET @@session.character_set_client=cp1251;', 'cp1251' ), |
| 104 | + 'system variable with LOCAL' => array( 'SET @@local.character_set_client=cp1251;', 'cp1251' ), |
| 105 | + 'user variable' => array( 'SET character_set_client = @old_cs_client;', '@old_cs_client' ), |
| 106 | + |
| 107 | + 'not character_set_client' => array( 'SET character_set_results = koi8r;', null ), |
| 108 | + 'not a character set' => array( "SET names='foo2';", null ), |
| 109 | + 'user variable named names' => array( 'SET @names = 1;', null ), |
| 110 | + 'comment' => array( '# SET NAMES latin1;', null ), |
| 111 | + 'mysqltest command' => array( '--character_set latin1', null ), |
| 112 | + 'text in another statement' => array( "SELECT 'SET NAMES latin1';", null ), |
| 113 | + ); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * MySQL's latin1 keeps the bytes that Windows-1252 leaves undefined as C1 controls. |
| 118 | + * On PHP below 8.1, mbstring replaces them with U+FFFD or rejects them as invalid. |
| 119 | + * |
| 120 | + * @dataProvider data_charsets_with_undefined_latin1_bytes |
| 121 | + */ |
| 122 | + public function test_undefined_latin1_bytes_are_c1_controls( string $charset ): void { |
| 123 | + $this->assertSame( |
| 124 | + "SELECT '\u{81}\u{8D}\u{8F}\u{90}\u{9D}€'", |
| 125 | + convert_to_utf8( "SELECT '\x81\x8D\x8F\x90\x9D\x80'", array( QUERY_CORPUS_CHARSET_ENCODINGS[ $charset ] ) ) |
| 126 | + ); |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * @return array<string,array{0:string}> |
| 131 | + */ |
| 132 | + public function data_charsets_with_undefined_latin1_bytes(): array { |
| 133 | + return array( |
| 134 | + 'declared latin1' => array( 'latin1' ), |
| 135 | + 'fallback from utf8mb4' => array( 'utf8mb4' ), |
| 136 | + 'fallback from tis620' => array( 'tis620' ), |
| 137 | + ); |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * @return array<string,array{0:string,1:string,2:string}> |
| 142 | + */ |
| 143 | + public function data_lines_in_character_sets(): array { |
| 144 | + return array( |
| 145 | + 'ASCII is unchanged' => array( 'koi8r', "SELECT 'a\\'b'", "SELECT 'a\\'b'" ), |
| 146 | + 'big5' => array( 'big5', "SELECT '\xA4\x40'", "SELECT '一'" ), |
| 147 | + 'binary as UTF-8' => array( 'binary', "SELECT 'é'", "SELECT 'é'" ), |
| 148 | + 'binary that is not UTF-8' => array( 'binary', "SELECT '\xE9'", "SELECT 'é'" ), |
| 149 | + 'cp1251' => array( 'cp1251', "SELECT '\xF2\xE5\xF1\xF2'", "SELECT 'тест'" ), |
| 150 | + 'gb18030' => array( 'gb18030', "SELECT '\xC4\xE3'", "SELECT '你'" ), |
| 151 | + 'gbk' => array( 'gbk', "SELECT '\xC4\xE3'", "SELECT '你'" ), |
| 152 | + 'koi8r' => array( 'koi8r', "SELECT '\xD4\xC5\xD3\xD4'", "SELECT 'тест'" ), |
| 153 | + 'latin1' => array( 'latin1', "SELECT '\x80\xE9'", "SELECT 'ێ'" ), |
| 154 | + 'latin2' => array( 'latin2', "SELECT '\xB1'", "SELECT 'ą'" ), |
| 155 | + 'ujis' => array( 'ujis', "SELECT '\xA4\xA2'", "SELECT 'あ'" ), |
| 156 | + 'utf8' => array( 'utf8', "SELECT 'é'", "SELECT 'é'" ), |
| 157 | + 'utf8mb3' => array( 'utf8mb3', "SELECT 'é'", "SELECT 'é'" ), |
| 158 | + 'utf8mb4' => array( 'utf8mb4', "SELECT '🐬'", "SELECT '🐬'" ), |
| 159 | + |
| 160 | + // The second byte of this character is a backslash in ASCII. |
| 161 | + 'sjis with a backslash byte' => array( 'sjis', "SELECT '\x83\x5C'", "SELECT 'ソ'" ), |
| 162 | + |
| 163 | + // A line that isn't valid in its character set is read as latin1. |
| 164 | + 'invalid utf8mb4' => array( 'utf8mb4', "SELECT '\xE9'", "SELECT 'é'" ), |
| 165 | + 'invalid sjis' => array( 'sjis', "SELECT '\xFD'", "SELECT 'ý'" ), |
| 166 | + |
| 167 | + // TIS-620 maps to the Unicode Thai block, with four unassigned bytes. |
| 168 | + 'tis620' => array( 'tis620', "SELECT '\xBE\xB9\xD4\xB4\xD2'", "SELECT 'พนิดา'" ), |
| 169 | + 'first and last tis620 byte' => array( 'tis620', "SELECT '\xA1\xFB'", "SELECT 'ก๛'" ), |
| 170 | + 'unassigned tis620 byte' => array( 'tis620', "SELECT '\xA1\xDB'", "SELECT '¡Û'" ), |
| 171 | + 'tis620 byte below the Thai block' => array( 'tis620', "SELECT '\xA0\xA1'", "SELECT '\u{A0}¡'" ), |
| 172 | + 'tis620 byte above the Thai block' => array( 'tis620', "SELECT '\xFC\xA1'", "SELECT 'ü¡'" ), |
| 173 | + ); |
| 174 | + } |
| 175 | +} |
0 commit comments