Repository navigation
Expand file tree
/
Copy pathWP_MySQL_Lexer_Tests.php
More file actions
863 lines (763 loc) · 36.4 KB
/
Copy pathWP_MySQL_Lexer_Tests.php
File metadata and controls
863 lines (763 loc) · 36.4 KB
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
<?php
use PHPUnit\Framework\TestCase;
/**
* Tests for the MySQL lexer: the token iterator API, the scanner behavior
* (identifiers, numbers, quoted strings), and the Bison token stream
* produced by WP_MySQL_Lexer::remaining_tokens().
*/
class WP_MySQL_Lexer_Tests extends TestCase {
public function test_tokenize_valid_input(): void {
$lexer = new WP_MySQL_Lexer( 'SELECT id FROM users' );
// SELECT
$this->assertTrue( $lexer->next_token() );
$this->assertSame( WP_MySQL_Lexer::KEYWORDS['SELECT'], $lexer->get_token()->id );
// id
$this->assertTrue( $lexer->next_token() );
$this->assertSame( WP_MySQL_Lexer::IDENT, $lexer->get_token()->id );
// FROM
$this->assertTrue( $lexer->next_token() );
$this->assertSame( WP_MySQL_Lexer::KEYWORDS['FROM'], $lexer->get_token()->id );
// users
$this->assertTrue( $lexer->next_token() );
$this->assertSame( WP_MySQL_Lexer::IDENT, $lexer->get_token()->id );
// The stream ends with END_OF_INPUT followed by Bison's end marker.
$this->assertTrue( $lexer->next_token() );
$this->assertSame( WP_MySQL_Lexer::END_OF_INPUT, $lexer->get_token()->id );
$this->assertTrue( $lexer->next_token() );
$this->assertSame( WP_MySQL_Lexer::YYEOF, $lexer->get_token()->id );
// No more tokens.
$this->assertFalse( $lexer->next_token() );
$this->assertNull( $lexer->get_token() );
// Again, no more tokens.
$this->assertFalse( $lexer->next_token() );
$this->assertNull( $lexer->get_token() );
}
public function test_tokenize_invalid_input(): void {
$lexer = new WP_MySQL_Lexer( "SELECT x'ab01xyz'" );
// SELECT
$this->assertTrue( $lexer->next_token() );
$this->assertSame( WP_MySQL_Lexer::KEYWORDS['SELECT'], $lexer->get_token()->id );
// Invalid input.
$this->assertFalse( $lexer->next_token() );
$this->assertNull( $lexer->get_token() );
// No more tokens.
$this->assertFalse( $lexer->next_token() );
$this->assertNull( $lexer->get_token() );
// Again, no more tokens.
$this->assertFalse( $lexer->next_token() );
$this->assertNull( $lexer->get_token() );
}
/**
* Get the Bison terminal names of a tokenized input.
*
* @param string $sql The SQL payload to tokenize.
* @param string[] $sql_modes The SQL modes to activate.
* @return string[] Terminal names, in input order.
*/
private static function token_names( string $sql, array $sql_modes = array() ): array {
$tokens = ( new WP_MySQL_Lexer( $sql, 80410, $sql_modes ) )->remaining_tokens();
$names = array();
foreach ( $tokens as $token ) {
$names[] = $token->get_name();
}
return $names;
}
public function test_emits_bison_terminals_with_end_markers(): void {
$this->assertSame(
array( 'SELECT', 'IDENT', 'FROM', 'IDENT', 'END_OF_INPUT', 'YYEOF' ),
self::token_names( 'SELECT id FROM users' )
);
}
public function test_ignore_space_preserves_function_name_ranges(): void {
// Without "(", COUNT is an identifier whose range excludes trailing whitespace.
$sql = 'SELECT COUNT FROM t';
$tokens = ( new WP_MySQL_Lexer( $sql, 80410, array( 'IGNORE_SPACE' ) ) )->remaining_tokens();
$this->assertSame( 'IDENT', $tokens[1]->get_name(), $sql );
$this->assertSame( 'COUNT', $tokens[1]->get_value(), $sql );
$this->assertSame( 5, $tokens[1]->length, $sql );
$this->assertSame( 13, $tokens[2]->start, $sql );
$sql = "SELECT COUNT\t\n FROM t";
$tokens = ( new WP_MySQL_Lexer( $sql, 80410, array( 'IGNORE_SPACE' ) ) )->remaining_tokens();
$this->assertSame( 'IDENT', $tokens[1]->get_name(), $sql );
$this->assertSame( 'COUNT', $tokens[1]->get_value(), $sql );
$this->assertSame( 5, $tokens[1]->length, $sql );
$this->assertSame( 15, $tokens[2]->start, $sql );
// With "(", COUNT is a function whose range still excludes the whitespace.
$tokens = ( new WP_MySQL_Lexer( 'SELECT COUNT (1)', 80410, array( 'IGNORE_SPACE' ) ) )->remaining_tokens();
$this->assertSame( 5, $tokens[1]->length );
$this->assertSame( 'COUNT', $tokens[1]->get_name() );
$this->assertSame( 'LEFT_PAREN', $tokens[2]->get_name() );
$this->assertSame( 13, $tokens[2]->start );
$sql = "SELECT COUNT \t\n";
$tokens = ( new WP_MySQL_Lexer( $sql, 80410, array( 'IGNORE_SPACE' ) ) )->remaining_tokens();
$this->assertSame( 'IDENT', $tokens[1]->get_name(), $sql );
$this->assertSame( 'COUNT', $tokens[1]->get_value(), $sql );
$this->assertSame( 5, $tokens[1]->length, $sql );
$this->assertSame( 'END_OF_INPUT', $tokens[2]->get_name(), $sql );
$this->assertSame( strlen( $sql ), $tokens[2]->start, $sql );
}
/**
* @dataProvider data_version_comments
*/
public function test_version_comments( string $sql, int $mysql_version, array $expected_bytes ): void {
$tokens = ( new WP_MySQL_Lexer( $sql, $mysql_version ) )->remaining_tokens();
$this->assertSame( 'YYEOF', array_pop( $tokens )->get_name() );
$this->assertSame( 'END_OF_INPUT', array_pop( $tokens )->get_name() );
$this->assertSame(
$expected_bytes,
array_map(
static function ( WP_MySQL_Token $token ): string {
return $token->get_bytes();
},
$tokens
)
);
}
public function data_version_comments(): array {
$cases = array(
'five digits below version' => array( 'SELECT /*!80038 1 + */ 2', 80037, array( 'SELECT', '2' ) ),
'five digits at version' => array( 'SELECT /*!80038 1 + */ 2', 80038, array( 'SELECT', '1', '+', '2' ) ),
'six digits on MySQL 5.7' => array( 'SELECT /*!080005 + */ 1', 50744, array( 'SELECT', '5', '+', '1' ) ),
'six digits on MySQL 8.0' => array( 'SELECT /*!080005 + */ 1', 80038, array( 'SELECT', '5', '+', '1' ) ),
'six digits before 8.1' => array( 'SELECT /*!080005 + */ 1', 80099, array( 'SELECT', '5', '+', '1' ) ),
'six digits on MySQL 8.1' => array( 'SELECT /*!080005 + */ 1', 80100, array( 'SELECT', '+', '1' ) ),
'six digits on MySQL 8.4' => array( 'SELECT /*!080400 1 + */ 2', 80400, array( 'SELECT', '1', '+', '2' ) ),
'six digits future major' => array( 'SELECT /*!100000 1 + */ 2', 80400, array( 'SELECT', '2' ) ),
'six digits at major' => array( 'SELECT /*!100000 1 + */ 2', 100000, array( 'SELECT', '1', '+', '2' ) ),
'six digits without space' => array( 'SELECT /*!080100+ */ 1', 80100, array( 'SELECT', '0', '+', '1' ) ),
'six digits at comment end' => array( 'SELECT /*!080100*/ + 1', 80100, array( 'SELECT', '0', '+', '1' ) ),
'seven digits' => array( 'SELECT /*!0801007 + */ 1', 80100, array( 'SELECT', '07', '+', '1' ) ),
);
foreach ( array(
'space' => ' ',
'tab' => "\t",
'LF' => "\n",
'CR' => "\r",
'FF' => "\f",
'VT' => "\v",
) as $name => $space ) {
$cases[ "six digits before 8.1 with $name" ] = array(
"SELECT /*!080100$space+ */ 1",
80038,
array( 'SELECT', '0', '+', '1' ),
);
$cases[ "six digits at version with $name" ] = array(
"SELECT /*!080100$space+ */ 1",
80100,
array( 'SELECT', '+', '1' ),
);
$cases[ "six digits above version with $name" ] = array(
"SELECT /*!080101$space+ */ 1",
80100,
array( 'SELECT', '1' ),
);
}
// Without five leading digits, the entire comment body remains SQL.
foreach ( array( 80038, 80100 ) as $version ) {
$cases[ "no version on $version" ] = array( 'SELECT /*! + */ 2', $version, array( 'SELECT', '+', '2' ) );
foreach ( array( '1', '12', '123', '1234' ) as $digits ) {
$cases[ "$digits as content on $version" ] = array(
"SELECT /*!$digits + */ 2",
$version,
array( 'SELECT', $digits, '+', '2' ),
);
}
}
return $cases;
}
public function test_mysql_version_defaults_to_mysql_8_4_10(): void {
$lexer = new WP_MySQL_Lexer( 'SELECT /*!080400 1 + */ 2' );
$this->assertSame( 80410, $lexer->get_mysql_version() );
// A six-digit MySQL 8.4 version comment is executed by default.
$this->assertSame(
array( 'SELECT', 'NUM', 'PLUS', 'NUM', 'END_OF_INPUT', 'YYEOF' ),
array_map(
static function ( WP_MySQL_Token $token ): string {
return $token->get_name();
},
$lexer->remaining_tokens()
)
);
}
public function test_at_name_splits_into_at_and_ident(): void {
$tokens = ( new WP_MySQL_Lexer( 'SELECT @var1' ) )->remaining_tokens();
$this->assertSame( 'AT_SIGN', $tokens[1]->get_name() );
$this->assertSame( 'IDENT', $tokens[2]->get_name() );
$this->assertSame( 'var1', $tokens[2]->get_value() );
$this->assertSame( 7, $tokens[1]->start );
$this->assertSame( 1, $tokens[1]->length );
$this->assertSame( 8, $tokens[2]->start );
$this->assertSame( 4, $tokens[2]->length );
}
public function test_at_at_splits_into_two_at_signs(): void {
$this->assertSame(
array( 'SELECT', 'AT_SIGN', 'AT_SIGN', 'IDENT', 'END_OF_INPUT', 'YYEOF' ),
self::token_names( 'SELECT @@sql_mode' )
);
}
public function test_bare_at_emits_empty_name(): void {
// MySQL's lexer emits an empty LEX_HOSTNAME after a bare "@", making
// "user1@" (an empty host part) and "SELECT @" valid.
$tokens = ( new WP_MySQL_Lexer( 'SELECT @' ) )->remaining_tokens();
$this->assertSame( 'AT_SIGN', $tokens[1]->get_name() );
$this->assertSame( 'IDENT', $tokens[2]->get_name() );
$this->assertSame( 0, $tokens[2]->length );
$this->assertSame( '', $tokens[2]->get_value() );
$this->assertSame(
array( 'CREATE', 'USER', 'IDENT', 'AT_SIGN', 'IDENT', 'END_OF_INPUT', 'YYEOF' ),
self::token_names( 'CREATE USER user1@' )
);
}
public function test_bare_at_before_quote_stands_alone(): void {
// In "@'name'" the quoted text supplies the name itself.
$this->assertSame(
array( 'SET', 'AT_SIGN', 'TEXT_STRING', '=', 'NUM', 'END_OF_INPUT', 'YYEOF' ),
self::token_names( "SET @'v' = 1" )
);
}
public function test_with_rollup_is_contracted(): void {
$names = self::token_names( 'SELECT 1 FROM t GROUP BY a WITH ROLLUP' );
$this->assertContains( 'WITH_ROLLUP_SYM', $names );
$this->assertNotContains( 'WITH', $names );
}
public function test_with_rollup_contracts_across_comments(): void {
$tokens = ( new WP_MySQL_Lexer( 'SELECT 1 FROM t GROUP BY a WITH /* c */ ROLLUP' ) )->remaining_tokens();
$rollup = null;
foreach ( $tokens as $token ) {
if ( 'WITH_ROLLUP_SYM' === $token->get_name() ) {
$rollup = $token;
}
}
$this->assertNotNull( $rollup );
$this->assertSame( 'WITH /* c */ ROLLUP', $rollup->get_bytes() );
}
public function test_lone_with_is_emitted(): void {
$this->assertSame(
array( 'WITH', 'IDENT', 'AS', 'LEFT_PAREN', 'SELECT', 'NUM', 'RIGHT_PAREN', 'SELECT', 'ASTERISK', 'FROM', 'IDENT', 'END_OF_INPUT', 'YYEOF' ),
self::token_names( 'WITH c AS (SELECT 1) SELECT * FROM c' )
);
// A statement ending on WITH still emits it before the end markers.
$this->assertSame(
array( 'SELECT', 'NUM', 'WITH', 'END_OF_INPUT', 'YYEOF' ),
self::token_names( 'SELECT 1 WITH' )
);
}
public function test_invalid_input_returns_partial_stream_without_end_markers(): void {
$names = self::token_names( "SELECT 1 WITH \x01" );
$this->assertSame( array( 'SELECT', 'NUM', 'WITH' ), $names );
}
public function test_system_variable_name_must_not_be_empty(): void {
// As in MySQL, the name after "@@" can't be empty, and only backticks
// can quote it. Otherwise, the stream ends after the "@@".
$at_at = array( 'AT_SIGN', 'AT_SIGN' );
$this->assertSame( $at_at, self::token_names( '@@' ) );
$this->assertSame( $at_at, self::token_names( '@@ version' ) );
$this->assertSame( $at_at, self::token_names( "@@'version'" ) );
$this->assertSame( $at_at, self::token_names( '@@"version"' ) );
$this->assertSame( $at_at, self::token_names( '@@"version"', array( 'ANSI_QUOTES' ) ) );
$this->assertSame(
array_merge( $at_at, array( 'IDENT_QUOTED', 'END_OF_INPUT', 'YYEOF' ) ),
self::token_names( '@@`version`' )
);
}
public function test_high_not_precedence_emits_not2(): void {
$names = self::token_names( 'SELECT NOT 1', array( 'HIGH_NOT_PRECEDENCE' ) );
$this->assertContains( 'NOT2_SYM', $names );
$names = self::token_names( 'SELECT NOT 1' );
$this->assertContains( 'NOT', $names );
}
public function test_end_of_input_word_is_an_identifier(): void {
// "end_of_input" is not a MySQL keyword; it must not truncate the stream.
$this->assertSame(
array( 'SELECT', 'IDENT', 'FROM', 'IDENT', 'END_OF_INPUT', 'YYEOF' ),
self::token_names( 'SELECT end_of_input FROM t' )
);
}
public function test_current_date_is_a_keyword_without_parentheses(): void {
// CURRENT_DATE/CURRENT_TIME are plain reserved keywords in MySQL 8.4
// (lex.h SYM), unlike CURDATE/CURTIME which require parentheses.
$this->assertSame(
array( 'SELECT', 'CURRENT_DATE', 'END_OF_INPUT', 'YYEOF' ),
self::token_names( 'SELECT CURRENT_DATE' )
);
$this->assertSame(
array( 'SELECT', 'IDENT', 'END_OF_INPUT', 'YYEOF' ),
self::token_names( 'SELECT curdate' )
);
}
public function test_json_aggregates_are_keywords_only_before_parenthesis(): void {
$this->assertSame(
array( 'SELECT', 'IDENT', 'FROM', 'IDENT', 'END_OF_INPUT', 'YYEOF' ),
self::token_names( 'SELECT json_objectagg FROM t' )
);
$names = self::token_names( 'SELECT JSON_OBJECTAGG(a, b) FROM t' );
$this->assertContains( 'JSON_OBJECTAGG', $names );
}
public function test_number_tokens_follow_mysql_magnitude_classes(): void {
$this->assertSame( array( 'SELECT', 'NUM', 'END_OF_INPUT', 'YYEOF' ), self::token_names( 'SELECT 2147483647' ) );
$this->assertSame( array( 'SELECT', 'LONG_NUM', 'END_OF_INPUT', 'YYEOF' ), self::token_names( 'SELECT 2147483648' ) );
$this->assertSame( array( 'SELECT', 'ULONGLONG_NUM', 'END_OF_INPUT', 'YYEOF' ), self::token_names( 'SELECT 18446744073709551615' ) );
$this->assertSame( array( 'SELECT', 'DECIMAL_NUM', 'END_OF_INPUT', 'YYEOF' ), self::token_names( 'SELECT 18446744073709551616' ) );
}
/**
* Test that the whole U+0080 to U+FFFF UTF-8 range is valid in an identifier.
* The validity is checked against PCRE with the "u" (PCRE_UTF8) modifier set.
*/
public function test_identifier_utf8_range(): void {
for ( $i = 0x80; $i < 0xffff; $i += 1 ) {
$value = mb_chr( $i, 'UTF-8' );
$lexer = new WP_MySQL_Lexer( $value );
$this->assertTrue( $lexer->next_token() );
$type = $lexer->get_token()->id;
$is_valid = preg_match( '/^[\x{0080}-\x{ffff}]$/u', $value );
if ( $is_valid ) {
$this->assertSame( WP_MySQL_Lexer::IDENT, $type );
} else {
// A surrogate codepoint renders as an empty string, so the lexer
// emits no identifier — only the end-of-input terminal.
$this->assertSame( WP_MySQL_Lexer::END_OF_INPUT, $type );
}
}
}
/**
* Test all valid and invalid 2-byte UTF-8 sequences in an identifier.
* The validity is checked against PCRE with the "u" (PCRE_UTF8) modifier set.
*
* Start both bytes from 128 and go up to 255 to include all invalid 2-byte
* UTF-8 sequences as well, and ensure that they won't match as identifiers.
*/
public function test_identifier_utf8_two_byte_sequences(): void {
for ( $byte_1 = 128; $byte_1 <= 255; $byte_1 += 1 ) {
for ( $byte_2 = 128; $byte_2 <= 255; $byte_2 += 1 ) {
$value = chr( $byte_1 ) . chr( $byte_2 );
$lexer = new WP_MySQL_Lexer( $value );
$result = $lexer->next_token();
$token = $lexer->get_token();
$is_valid = preg_match( '/^[\x{0080}-\x{ffff}]$/u', $value );
if ( $is_valid ) {
$this->assertTrue( $result );
$this->assertSame( WP_MySQL_Lexer::IDENT, $token->id );
} else {
$this->assertFalse( $result );
$this->assertNull( $token );
}
}
}
}
/**
* Test all valid and invalid 3-byte UTF-8 sequences in an identifier.
* The validity is checked against PCRE with the "u" (PCRE_UTF8) modifier set.
*
* Start the first byte from 0xE0 to mark the beginning of a 3-byte sequence.
* Start bytes 2 and 3 from 128 and go up to 255 to include all invalid 3-byte
* UTF-8 sequences as well, and ensure that they won't match as identifiers.
*/
public function test_identifier_utf8_three_byte_sequences(): void {
for ( $byte_1 = 0xE0; $byte_1 <= 0xFF; $byte_1 += 1 ) {
for ( $byte_2 = 128; $byte_2 <= 255; $byte_2 += 1 ) {
for ( $byte_3 = 128; $byte_3 <= 255; $byte_3 += 1 ) {
$value = chr( $byte_1 ) . chr( $byte_2 ) . chr( $byte_3 );
$lexer = new WP_MySQL_Lexer( $value );
$result = $lexer->next_token();
$token = $lexer->get_token();
$is_valid = preg_match( '/^[\x{0080}-\x{ffff}]$/u', $value );
if ( $is_valid ) {
$this->assertTrue( $result );
$this->assertSame( WP_MySQL_Lexer::IDENT, $token->id );
} else {
$this->assertFalse( $result );
$this->assertNull( $token );
}
}
}
}
}
/**
* A charset-introducer-like name used as a qualified member (after a dot)
* must lex as an identifier. A real charset introducer only appears before
* a string literal, never as the member of a qualified reference.
*
* @dataProvider data_underscore_charset_after_dot
*/
public function test_underscore_charset_name_after_dot_is_identifier( string $sql, int $token_index, int $expected_id ): void {
$tokens = ( new WP_MySQL_Lexer( $sql ) )->remaining_tokens();
$this->assertSame(
WP_MySQL_Lexer::get_token_name( $expected_id ),
$tokens[ $token_index ]->get_name(),
$sql
);
}
/**
* @return array<string,array{0:string,1:int,2:int}>
*/
public function data_underscore_charset_after_dot(): array {
return array(
// `t . _utf8` - the member name must be an identifier, not a charset.
'charset name after dot is identifier' => array( 't._utf8', 2, WP_MySQL_Lexer::IDENT ),
'other charset name after dot' => array( 'a._binary', 2, WP_MySQL_Lexer::IDENT ),
// A genuine charset introducer (before a string) stays a charset.
'charset introducer before string' => array( "_utf8'x'", 0, WP_MySQL_Lexer::UNDERSCORE_CHARSET ),
// A non-charset underscore name after a dot stays an identifier.
'non-charset underscore name after dot' => array( 't._foo', 2, WP_MySQL_Lexer::IDENT ),
);
}
/**
* Qualified names and system variable names are lexed as in MySQL.
* All cases are verified against MySQL 8.4.
*
* @dataProvider data_qualified_name_tokens
*/
public function test_qualified_name_tokens( string $sql, array $expected ): void {
$this->assertSame(
array_merge( $expected, array( 'END_OF_INPUT', 'YYEOF' ) ),
self::token_names( $sql )
);
}
/**
* @return array<string,array{0:string,1:string[]}>
*/
public function data_qualified_name_tokens(): array {
$qualified = array( 'IDENT', 'DOT', 'IDENT' );
return array(
// A qualified name part can start with digits.
'digits only' => array( 't.1', $qualified ),
'digit-led identifier' => array( 't.1st', $qualified ),
'float-like identifier' => array( 't.1e5', $qualified ),
'part ends before a sign' => array( 't.1e+5', array_merge( $qualified, array( 'PLUS', 'NUM' ) ) ),
'after a digit-led qualifier' => array( '1ea10.1', $qualified ),
'three parts' => array( 'd.1.2', array_merge( $qualified, array( 'DOT', 'IDENT' ) ) ),
// Elsewhere, ".N" is a number.
'after a quoted identifier' => array( '`t`.1', array( 'IDENT_QUOTED', 'DECIMAL_NUM' ) ),
'after whitespace' => array( 't .1', array( 'IDENT', 'DECIMAL_NUM' ) ),
'after a separator dot' => array( 't..1', array( 'IDENT', 'DOT', 'DECIMAL_NUM' ) ),
'after a decimal' => array( '1.5.6', array( 'DECIMAL_NUM', 'DECIMAL_NUM' ) ),
'after a float' => array( '1e5.1', array( 'FLOAT_NUM', 'DECIMAL_NUM' ) ),
'after a hex number' => array( '0x1F.1', array( 'HEX_NUM', 'DECIMAL_NUM' ) ),
'after a binary number' => array( '0b1.1', array( 'BIN_NUM', 'DECIMAL_NUM' ) ),
'after "\N"' => array( '\N.1', array( 'NULL', 'DECIMAL_NUM' ) ),
'after a version comment' => array( '/*!50000.5*/', array( 'DECIMAL_NUM' ) ),
// A user variable name includes the dot.
'in a user variable' => array( '@a.1', array( 'AT_SIGN', 'IDENT' ) ),
// A keyword before a dot and a name is an identifier.
'keyword before a name' => array( 'order.id', $qualified ),
'keyword before digits' => array( 'select.1', $qualified ),
'keyword before non-ASCII' => array( 'order.ñ', $qualified ),
'keyword before "$"' => array( 'order.$x', $qualified ),
'keyword in three parts' => array( 'order.t.id', array_merge( $qualified, array( 'DOT', 'IDENT' ) ) ),
// A name right after a "." token is always an identifier.
'keyword after a dot' => array( '`t`.order', array( 'IDENT_QUOTED', 'DOT', 'IDENT' ) ),
'literal prefix after a dot' => array( "t.x'41'", array_merge( $qualified, array( 'TEXT_STRING' ) ) ),
// Otherwise, it stays a keyword or a charset.
'keyword after a decimal' => array( '1.FROM', array( 'DECIMAL_NUM', 'FROM' ) ),
'keyword before a backtick' => array( 'order.`id`', array( 'ORDER', 'DOT', 'IDENT_QUOTED' ) ),
'keyword before an asterisk' => array( 'order.*', array( 'ORDER', 'DOT', 'ASTERISK' ) ),
'keyword before whitespace' => array( 'order .id', array( 'ORDER', 'DOT', 'IDENT' ) ),
'system variable scope' => array( '@@session.sql_mode', array( 'AT_SIGN', 'AT_SIGN', 'SESSION', 'DOT', 'IDENT' ) ),
'charset introducer' => array( '_utf8mb4.x', array( 'UNDERSCORE_CHARSET', 'DOT', 'IDENT' ) ),
// After "@@", the name is never a number, a literal, or a charset.
'system variable digits' => array( '@@123', array( 'AT_SIGN', 'AT_SIGN', 'IDENT' ) ),
'system variable float-like' => array( '@@1e5', array( 'AT_SIGN', 'AT_SIGN', 'IDENT' ) ),
'system variable before text' => array( "@@b'01'", array( 'AT_SIGN', 'AT_SIGN', 'IDENT', 'TEXT_STRING' ) ),
'system variable charset' => array( '@@_utf8mb4', array( 'AT_SIGN', 'AT_SIGN', 'IDENT' ) ),
);
}
/**
* @dataProvider data_integer_types
*/
public function test_integer_types( $input, $expected ): void {
$lexer = new WP_MySQL_Lexer( $input );
$this->assertTrue( $lexer->next_token() );
$this->assertSame( $expected, $lexer->get_token()->id );
}
public function data_integer_types(): array {
return array(
array( '0', WP_MySQL_Lexer::NUM ),
array( '123', WP_MySQL_Lexer::NUM ),
array( '2147483647', WP_MySQL_Lexer::NUM ),
array( '00000000001', WP_MySQL_Lexer::NUM ),
array( '00000000002147483647', WP_MySQL_Lexer::NUM ),
array( '2147483648', WP_MySQL_Lexer::LONG_NUM ),
array( '123456789123456789', WP_MySQL_Lexer::LONG_NUM ),
array( '9223372036854775807', WP_MySQL_Lexer::LONG_NUM ),
array( '00000000002147483648', WP_MySQL_Lexer::LONG_NUM ),
array( '00000000009223372036854775807', WP_MySQL_Lexer::LONG_NUM ),
array( '9223372036854775808', WP_MySQL_Lexer::ULONGLONG_NUM ),
array( '12345678912345678912', WP_MySQL_Lexer::ULONGLONG_NUM ),
array( '18446744073709551615', WP_MySQL_Lexer::ULONGLONG_NUM ),
array( '00000000000000000009223372036854775808', WP_MySQL_Lexer::ULONGLONG_NUM ),
array( '000000000000000000018446744073709551615', WP_MySQL_Lexer::ULONGLONG_NUM ),
array( '18446744073709551616', WP_MySQL_Lexer::DECIMAL_NUM ),
array( '23456789123456789123', WP_MySQL_Lexer::DECIMAL_NUM ),
array( '123456789123456789123456789', WP_MySQL_Lexer::DECIMAL_NUM ),
array( '0000000000000000000018446744073709551616', WP_MySQL_Lexer::DECIMAL_NUM ),
array( '00000000000000000000123456789123456789123456789', WP_MySQL_Lexer::DECIMAL_NUM ),
);
}
/**
* Numbers vs. identifiers:
*
* In MySQL, when an input matches both a number and an identifier, the number always wins.
* However, when the number is followed by a non-numeric identifier-like character, it is
* considered an identifier... unless it's a float number, which ignores subsequent input.
*
* @dataProvider data_identifier_or_number
*/
public function test_identifier_or_number( $input, $expected ): void {
$lexer = new WP_MySQL_Lexer( $input );
$actual = array_map(
function ( $token ) {
return $token->id;
},
$lexer->remaining_tokens()
);
// Compare token names to get more readable error messages.
$this->assertSame(
$this->get_token_names( $expected ),
$this->get_token_names( $actual )
);
}
public function data_identifier_or_number(): array {
$end = array( WP_MySQL_Lexer::END_OF_INPUT, WP_MySQL_Lexer::YYEOF );
return array(
// integer
array( '123', array_merge( array( WP_MySQL_Lexer::NUM ), $end ) ),
array( '123abc', array_merge( array( WP_MySQL_Lexer::IDENT ), $end ) ), // identifier
// binary
array( '0b01', array_merge( array( WP_MySQL_Lexer::BIN_NUM ), $end ) ),
array( '0b01xyz', array_merge( array( WP_MySQL_Lexer::IDENT ), $end ) ), // identifier
array( '0b', array_merge( array( WP_MySQL_Lexer::IDENT ), $end ) ), // identifier
array( "b'01'", array_merge( array( WP_MySQL_Lexer::BIN_NUM ), $end ) ),
array( "b'01xyz'", array() ), // invalid input
array( "b''", array_merge( array( WP_MySQL_Lexer::BIN_NUM ), $end ) ),
array( "b'", array() ), // invalid input
array( "b'01", array() ), // invalid input
// hex
array( '0xab01', array_merge( array( WP_MySQL_Lexer::HEX_NUM ), $end ) ),
array( '0xab01xyz', array_merge( array( WP_MySQL_Lexer::IDENT ), $end ) ), // identifier
array( '0x', array_merge( array( WP_MySQL_Lexer::IDENT ), $end ) ), // identifier
array( "x'ab01'", array_merge( array( WP_MySQL_Lexer::HEX_NUM ), $end ) ),
array( "x'ab01xyz'", array() ), // invalid input
array( "x''", array_merge( array( WP_MySQL_Lexer::HEX_NUM ), $end ) ),
array( "x'", array() ), // invalid input
array( "x'ab", array() ), // invalid input
// decimal
array( '123.456', array_merge( array( WP_MySQL_Lexer::DECIMAL_NUM ), $end ) ),
array( '.123', array_merge( array( WP_MySQL_Lexer::DECIMAL_NUM ), $end ) ),
array( '123.', array_merge( array( WP_MySQL_Lexer::DECIMAL_NUM ), $end ) ),
array( '123.456abc', array_merge( array( WP_MySQL_Lexer::DECIMAL_NUM, WP_MySQL_Lexer::IDENT ), $end ) ), // not identifier
array( '.123abc', array_merge( array( WP_MySQL_Lexer::DECIMAL_NUM, WP_MySQL_Lexer::IDENT ), $end ) ), // not identifier
array( '123.abc', array_merge( array( WP_MySQL_Lexer::DECIMAL_NUM, WP_MySQL_Lexer::IDENT ), $end ) ), // not identifier
// float
array( '1e10', array_merge( array( WP_MySQL_Lexer::FLOAT_NUM ), $end ) ),
array( '1e+10', array_merge( array( WP_MySQL_Lexer::FLOAT_NUM ), $end ) ),
array( '1e-10', array_merge( array( WP_MySQL_Lexer::FLOAT_NUM ), $end ) ),
array( '.1e10', array_merge( array( WP_MySQL_Lexer::FLOAT_NUM ), $end ) ),
array( '.1e+10', array_merge( array( WP_MySQL_Lexer::FLOAT_NUM ), $end ) ),
array( '.1e-10', array_merge( array( WP_MySQL_Lexer::FLOAT_NUM ), $end ) ),
array( '1.1e10', array_merge( array( WP_MySQL_Lexer::FLOAT_NUM ), $end ) ),
array( '1.1e-10', array_merge( array( WP_MySQL_Lexer::FLOAT_NUM ), $end ) ),
array( '1.1e+10', array_merge( array( WP_MySQL_Lexer::FLOAT_NUM ), $end ) ),
array( '1e10abc', array_merge( array( WP_MySQL_Lexer::FLOAT_NUM, WP_MySQL_Lexer::IDENT ), $end ) ), // not identifier (this differs from INT/BIN/HEX numbers)
array( '1e+10abc', array_merge( array( WP_MySQL_Lexer::FLOAT_NUM, WP_MySQL_Lexer::IDENT ), $end ) ), // not identifier
array( '1e-10abc', array_merge( array( WP_MySQL_Lexer::FLOAT_NUM, WP_MySQL_Lexer::IDENT ), $end ) ), // not identifier
array( '.1e10abc', array_merge( array( WP_MySQL_Lexer::FLOAT_NUM, WP_MySQL_Lexer::IDENT ), $end ) ), // not identifier
array( '.1e+10abc', array_merge( array( WP_MySQL_Lexer::FLOAT_NUM, WP_MySQL_Lexer::IDENT ), $end ) ), // not identifier
array( '.1e-10abc', array_merge( array( WP_MySQL_Lexer::FLOAT_NUM, WP_MySQL_Lexer::IDENT ), $end ) ), // not identifier
array( '1.1e10abc', array_merge( array( WP_MySQL_Lexer::FLOAT_NUM, WP_MySQL_Lexer::IDENT ), $end ) ), // not identifier
array( '1.1e+10abc', array_merge( array( WP_MySQL_Lexer::FLOAT_NUM, WP_MySQL_Lexer::IDENT ), $end ) ), // not identifier
array( '1.1e-10abc', array_merge( array( WP_MySQL_Lexer::FLOAT_NUM, WP_MySQL_Lexer::IDENT ), $end ) ), // not identifier
// non-numbers
array( '.SELECT', array_merge( array( WP_MySQL_Lexer::DOT, WP_MySQL_Lexer::IDENT ), $end ) ), // not decimal or float
array( '1+e10', array_merge( array( WP_MySQL_Lexer::NUM, WP_MySQL_Lexer::PLUS, WP_MySQL_Lexer::IDENT ), $end ) ), // not float
array( '1-e10', array_merge( array( WP_MySQL_Lexer::NUM, WP_MySQL_Lexer::MINUS, WP_MySQL_Lexer::IDENT ), $end ) ), // not float
);
}
/**
* Test that unclosed quoted strings with trailing backslashes do not
* cause out-of-bounds string access in read_quoted_text().
*
* The backslash-counting loop walks backward from the closing-quote
* candidate position. When the closing quote is missing and the last
* byte is a backslash, the loop must not treat the absent quote as
* escaped and advance past the end of the string, which would access
* an invalid string offset, triggering "Uninitialized string offset"
* warnings.
*
* @dataProvider data_unclosed_strings_with_backslashes
*/
public function test_unclosed_string_with_trailing_backslash( string $sql ): void {
set_error_handler(
function ( $severity, $message, $file, $line ) {
throw new \ErrorException( $message, 0, $severity, $file, $line );
},
E_WARNING | E_NOTICE
);
try {
$lexer = new WP_MySQL_Lexer( $sql );
while ( $lexer->next_token() ) {
// Consume all tokens.
}
} finally {
restore_error_handler();
}
// If we reach here without an ErrorException, no OOB access occurred.
$this->assertNull( $lexer->get_token() );
}
public function data_unclosed_strings_with_backslashes(): array {
return array(
'single-quoted trailing backslash' => array( "SELECT '\\" ),
'double-quoted trailing backslash' => array( 'SELECT "\\' ),
'even trailing backslashes' => array( "SELECT '\\\\" ),
'odd trailing backslashes' => array( "SELECT '\\\\\\" ),
'backslash-only single-quoted' => array( "'\\" ),
'backslash-only double-quoted' => array( '"\\' ),
);
}
/**
* Regression: valid strings with escapes must still tokenize correctly.
*
* @dataProvider data_valid_escaped_strings
*/
public function test_valid_escaped_string( string $sql, int $expected_token_id ): void {
$lexer = new WP_MySQL_Lexer( $sql );
$this->assertTrue( $lexer->next_token() );
$this->assertSame( $expected_token_id, $lexer->get_token()->id );
}
public function data_valid_escaped_strings(): array {
return array(
'escaped single quote' => array( "'it\\'s'", WP_MySQL_Lexer::TEXT_STRING ),
'trailing escaped backslash' => array( "'path\\\\'", WP_MySQL_Lexer::TEXT_STRING ),
'doubled single quote' => array( "'it''s'", WP_MySQL_Lexer::TEXT_STRING ),
'empty single-quoted string' => array( "''", WP_MySQL_Lexer::TEXT_STRING ),
'escaped double quote' => array( '"col\\"name"', WP_MySQL_Lexer::TEXT_STRING ),
'backtick identifier' => array( '`my_column`', WP_MySQL_Lexer::IDENT_QUOTED ),
);
}
/**
* Test that a chunk boundary splitting a quoted string with a trailing
* backslash does not cause an out-of-bounds string access.
*
* This simulates streaming SQL processing where a buffer boundary falls
* inside a string literal right after a backslash escape character.
*/
public function test_chunk_boundary_inside_escaped_string(): void {
set_error_handler(
function ( $severity, $message, $file, $line ) {
throw new \ErrorException( $message, 0, $severity, $file, $line );
},
E_WARNING | E_NOTICE
);
try {
// Build a SQL string where a backslash falls at the chunk boundary.
// The string content before the boundary is padded to place the
// backslash at exactly position $chunk_size - 1.
$chunk_size = 8192;
// "SELECT '" = 8 bytes, so we need chunk_size - 8 - 1 bytes of
// padding before the trailing backslash to place '\' at the last
// byte of the chunk.
$padding = str_repeat( 'A', $chunk_size - 8 - 1 );
$sql = "SELECT '" . $padding . '\\';
// The chunk is exactly $chunk_size bytes. The last byte is '\'.
// The lexer should handle this as an unclosed string without OOB.
$this->assertSame( $chunk_size, strlen( $sql ) );
$lexer = new WP_MySQL_Lexer( $sql );
while ( $lexer->next_token() ) {
// Consume all tokens.
}
} finally {
restore_error_handler();
}
$this->assertNull( $lexer->get_token() );
}
/**
* @dataProvider data_ansi_quotes_tokenization
*/
public function test_ansi_quotes_changes_double_quote_tokenization(
string $sql,
array $sql_modes,
string $expected_token_name
): void {
$tokens = ( new WP_MySQL_Lexer( $sql, 80410, $sql_modes ) )->remaining_tokens();
$this->assertSame( $expected_token_name, $tokens[0]->get_name(), $sql );
}
public function data_ansi_quotes_tokenization(): array {
return array(
'double quote is a string by default' => array( '"foo"', array(), 'TEXT_STRING' ),
'double quote is an identifier in ANSI_QUOTES' => array( '"foo"', array( 'ANSI_QUOTES' ), 'IDENT_QUOTED' ),
'ANSI_QUOTES is case-insensitive' => array( '"foo"', array( 'ansi_quotes' ), 'IDENT_QUOTED' ),
'ANSI_QUOTES with other modes' => array( '"foo"', array( 'STRICT_ALL_TABLES', 'ANSI_QUOTES' ), 'IDENT_QUOTED' ),
'single quote stays a string in ANSI_QUOTES' => array( "'foo'", array( 'ANSI_QUOTES' ), 'TEXT_STRING' ),
'backtick stays an identifier in ANSI_QUOTES' => array( '`foo`', array( 'ANSI_QUOTES' ), 'IDENT_QUOTED' ),
);
}
/**
* @dataProvider data_national_strings
*/
public function test_national_strings( string $sql, array $expected ): void {
$this->assertSame( array_merge( $expected, array( 'END_OF_INPUT', 'YYEOF' ) ), self::token_names( $sql ) );
}
public function data_national_strings(): array {
return array(
'uppercase prefix' => array( "N'abc'", array( 'NCHAR_STRING' ) ),
'lowercase prefix' => array( "n'abc'", array( 'NCHAR_STRING' ) ),
'escaped quote' => array( "N'a''b'", array( 'NCHAR_STRING' ) ),
'double quote is not a literal' => array( 'N"abc"', array( 'IDENT', 'TEXT_STRING' ) ),
);
}
public function test_composite_ansi_mode_expands_to_lexer_component_modes(): void {
// ANSI_QUOTES: a double-quoted sequence is a quoted identifier.
$lexer = new WP_MySQL_Lexer( '"foo"', 80410, array( 'ANSI' ) );
$this->assertTrue( $lexer->next_token() );
$this->assertSame( WP_MySQL_Lexer::IDENT_QUOTED, $lexer->get_token()->id );
// PIPES_AS_CONCAT: "||" is the string concatenation operator.
$lexer = new WP_MySQL_Lexer( '||', 80410, array( 'ANSI' ) );
$this->assertTrue( $lexer->next_token() );
$this->assertSame( WP_MySQL_Lexer::OR_OR_SYM, $lexer->get_token()->id );
// IGNORE_SPACE: whitespace is permitted between a function name and "(".
$lexer = new WP_MySQL_Lexer( 'COUNT (1)', 80410, array( 'ANSI' ) );
$this->assertTrue( $lexer->next_token() );
$this->assertSame( WP_MySQL_Lexer::KEYWORDS['COUNT'], $lexer->get_token()->id );
}
/**
* Quoted identifiers escape delimiters by doubling, not with backslashes.
*
* @dataProvider data_quoted_identifier_backslash
*/
public function test_quoted_identifiers_treat_backslash_literally(
string $sql,
array $sql_modes,
int $expected_token_id,
string $expected_value
): void {
$lexer = new WP_MySQL_Lexer( $sql, 80410, $sql_modes );
$this->assertTrue( $lexer->next_token() );
$token = $lexer->get_token();
$this->assertSame( $expected_token_id, $token->id, $sql );
$this->assertSame( $expected_value, $token->get_value(), $sql );
}
public function data_quoted_identifier_backslash(): array {
$bs = chr( 92 ); // A single backslash.
$bt = '`';
$dq = '"';
$sq = "'";
return array(
// Backtick identifiers: backslash is a literal character.
'backtick keeps backslash-n literal' => array( $bt . 'a' . $bs . 'nb' . $bt, array(), WP_MySQL_Lexer::IDENT_QUOTED, 'a' . $bs . 'nb' ),
'backtick allows trailing backslash' => array( $bt . 'a' . $bs . $bt, array(), WP_MySQL_Lexer::IDENT_QUOTED, 'a' . $bs ),
'backtick escapes only by doubling' => array( $bt . 'a' . $bt . $bt . 'b' . $bt, array(), WP_MySQL_Lexer::IDENT_QUOTED, 'a' . $bt . 'b' ),
// ANSI-quoted identifiers behave like backtick identifiers.
'ansi-quoted keeps backslash literal' => array( $dq . 'a' . $bs . 'nb' . $dq, array( 'ANSI_QUOTES' ), WP_MySQL_Lexer::IDENT_QUOTED, 'a' . $bs . 'nb' ),
'ansi-quoted allows trailing backslash' => array( $dq . 'a' . $bs . $dq, array( 'ANSI_QUOTES' ), WP_MySQL_Lexer::IDENT_QUOTED, 'a' . $bs ),
// String literals still process backslash escapes.
'single-quoted string escapes newline' => array( $sq . 'a' . $bs . 'nb' . $sq, array(), WP_MySQL_Lexer::TEXT_STRING, "a\nb" ),
);
}
private function get_token_names( array $token_types ): array {
return array_map(
function ( $token_type ) {
return WP_MySQL_Lexer::get_token_name( $token_type );
},
$token_types
);
}
}