Debian Codegraph @

tests/phpunit/unit/includes/libs/StringUtilsTest.php

raw symbols SCIP
1 <?php
2
3 namespace Wikimedia\Tests;
4
5 use MediaWikiCoversValidator;
6 use PHPUnit\Framework\TestCase;
7 use StringUtils;
8
9 /**
10 * @covers \StringUtils
11 */
12 class StringUtilsTest extends TestCase {
13
14 use MediaWikiCoversValidator;
15
16 /**
17 * @dataProvider provideStringsForIsUtf8Check
18 */
19 public function testIsUtf8( bool $expected, string $string ) {
20 $this->assertEquals( $expected, StringUtils::isUtf8( $string ),
21 'Testing string "' . $this->escaped( $string ) . '"' );
22 }
23
24 /**
25 * Print high range characters as a hexadecimal
26 */
27 private function escaped( string $string ): string {
28 return preg_replace_callback( '/[\x80-\xFF]/',
29 static fn ( $m ) => '\x' . dechex( ord( $m[0] ) ),
30 $string
31 );
32 }
33
34 /**
35 * See also "UTF-8 decoder capability and stress test" by
36 * Markus Kuhn:
37 * http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt
38 */
39 public static function provideStringsForIsUtf8Check() {
40 // Expected return values for StringUtils::isUtf8()
41 $PASS = true;
42 $FAIL = false;
43
44 return [
45 'some ASCII' => [ $PASS, 'Some ASCII' ],
46 'euro sign' => [ $PASS, "Euro sign €" ],
47
48 'first possible sequence 1 byte' => [ $PASS, "\x00" ],
49 'first possible sequence 2 bytes' => [ $PASS, "\xc2\x80" ],
50 'first possible sequence 3 bytes' => [ $PASS, "\xe0\xa0\x80" ],
51 'first possible sequence 4 bytes' => [ $PASS, "\xf0\x90\x80\x80" ],
52 'first possible sequence 5 bytes' => [ $FAIL, "\xf8\x88\x80\x80\x80" ],
53 'first possible sequence 6 bytes' => [ $FAIL, "\xfc\x84\x80\x80\x80\x80" ],
54
55 'last possible sequence 1 byte' => [ $PASS, "\x7f" ],
56 'last possible sequence 2 bytes' => [ $PASS, "\xdf\xbf" ],
57 'last possible sequence 3 bytes' => [ $PASS, "\xef\xbf\xbf" ],
58 'last possible sequence 4 bytes (U+1FFFFF)' => [ $FAIL, "\xf7\xbf\xbf\xbf" ],
59 'last possible sequence 5 bytes' => [ $FAIL, "\xfb\xbf\xbf\xbf\xbf" ],
60 'last possible sequence 6 bytes' => [ $FAIL, "\xfd\xbf\xbf\xbf\xbf\xbf" ],
61
62 'boundary 1' => [ $PASS, "\xed\x9f\xbf" ],
63 'boundary 2' => [ $PASS, "\xee\x80\x80" ],
64 'boundary 3' => [ $PASS, "\xef\xbf\xbd" ],
65 'boundary 4' => [ $PASS, "\xf2\x80\x80\x80" ],
66 'boundary 5 (U+FFFFF)' => [ $PASS, "\xf3\xbf\xbf\xbf" ],
67 'boundary 6 (U+100000)' => [ $PASS, "\xf4\x80\x80\x80" ],
68 'boundary 7 (U+10FFFF)' => [ $PASS, "\xf4\x8f\xbf\xbf" ],
69 'boundary 8 (U+110000)' => [ $FAIL, "\xf4\x90\x80\x80" ],
70
71 'malformed 1' => [ $FAIL, "\x80" ],
72 'malformed 2' => [ $FAIL, "\xbf" ],
73 'malformed 3' => [ $FAIL, "\x80\xbf" ],
74 'malformed 4' => [ $FAIL, "\x80\xbf\x80" ],
75 'malformed 5' => [ $FAIL, "\x80\xbf\x80\xbf" ],
76 'malformed 6' => [ $FAIL, "\x80\xbf\x80\xbf\x80" ],
77 'malformed 7' => [ $FAIL, "\x80\xbf\x80\xbf\x80\xbf" ],
78 'malformed 8' => [ $FAIL, "\x80\xbf\x80\xbf\x80\xbf\x80" ],
79
80 'last byte missing 1' => [ $FAIL, "\xc0" ],
81 'last byte missing 2' => [ $FAIL, "\xe0\x80" ],
82 'last byte missing 3' => [ $FAIL, "\xf0\x80\x80" ],
83 'last byte missing 4' => [ $FAIL, "\xf8\x80\x80\x80" ],
84 'last byte missing 5' => [ $FAIL, "\xfc\x80\x80\x80\x80" ],
85 'last byte missing 6' => [ $FAIL, "\xdf" ],
86 'last byte missing 7' => [ $FAIL, "\xef\xbf" ],
87 'last byte missing 8' => [ $FAIL, "\xf7\xbf\xbf" ],
88 'last byte missing 9' => [ $FAIL, "\xfb\xbf\xbf\xbf" ],
89 'last byte missing 10' => [ $FAIL, "\xfd\xbf\xbf\xbf\xbf" ],
90
91 'extra continuation byte 1' => [ $FAIL, "e\xaf" ],
92 'extra continuation byte 2' => [ $FAIL, "\xc3\x89\xaf" ],
93 'extra continuation byte 3' => [ $FAIL, "\xef\xbc\xa5\xaf" ],
94 'extra continuation byte 4' => [ $FAIL, "\xf0\x9d\x99\xb4\xaf" ],
95
96 'impossible bytes 1' => [ $FAIL, "\xfe" ],
97 'impossible bytes 2' => [ $FAIL, "\xff" ],
98 'impossible bytes 3' => [ $FAIL, "\xfe\xfe\xff\xff" ],
99
100 'overlong sequences 1' => [ $FAIL, "\xc0\xaf" ],
101 'overlong sequences 2' => [ $FAIL, "\xc1\xaf" ],
102 'overlong sequences 3' => [ $FAIL, "\xe0\x80\xaf" ],
103 'overlong sequences 4' => [ $FAIL, "\xf0\x80\x80\xaf" ],
104 'overlong sequences 5' => [ $FAIL, "\xf8\x80\x80\x80\xaf" ],
105 'overlong sequences 6' => [ $FAIL, "\xfc\x80\x80\x80\x80\xaf" ],
106
107 'maximum overlong sequences 1' => [ $FAIL, "\xc1\xbf" ],
108 'maximum overlong sequences 2' => [ $FAIL, "\xe0\x9f\xbf" ],
109 'maximum overlong sequences 3' => [ $FAIL, "\xf0\x8f\xbf\xbf" ],
110 'maximum overlong sequences 4' => [ $FAIL, "\xf8\x87\xbf\xbf" ],
111 'maximum overlong sequences 5' => [ $FAIL, "\xfc\x83\xbf\xbf\xbf\xbf" ],
112
113 'surrogates 1 (U+D799)' => [ $PASS, "\xed\x9f\xbf" ],
114 'surrogates 2 (U+E000)' => [ $PASS, "\xee\x80\x80" ],
115 'surrogates 3 (U+D800)' => [ $FAIL, "\xed\xa0\x80" ],
116 'surrogates 4 (U+DBFF)' => [ $FAIL, "\xed\xaf\xbf" ],
117 'surrogates 5 (U+DC00)' => [ $FAIL, "\xed\xb0\x80" ],
118 'surrogates 6 (U+DFFF)' => [ $FAIL, "\xed\xbf\xbf" ],
119 'surrogates 7 (U+D800 U+DC00)' => [ $FAIL, "\xed\xa0\x80\xed\xb0\x80" ],
120
121 'noncharacters 1' => [ $PASS, "\xef\xbf\xbe" ],
122 'noncharacters 2' => [ $PASS, "\xef\xbf\xbf" ],
123 ];
124 }
125
126 /**
127 * @dataProvider provideRegexps
128 */
129 public function testIsValidPCRERegex( string $input, bool $expected ) {
130 $this->assertSame( $expected, StringUtils::isValidPCRERegex( $input ) );
131 }
132
133 public static function provideRegexps() {
134 return [
135 [ 'foo', false ],
136 [ '/foo/', true ],
137 [ '//', true ],
138 [ '/(foo/', false ],
139 [ '!(f[o]{2})!', true ],
140 [ '/foo\/', false ]
141 ];
142 }
143
144 /**
145 * @dataProvider provideRegexReplacements
146 */
147 public function testEscapeRegexReplacement( string $replacement ) {
148 $escaped = StringUtils::escapeRegexReplacement( $replacement );
149 $result = preg_replace( '/(placeholder)/', $escaped, 'placeholder' );
150 $this->assertSame( $replacement, $result );
151 }
152
153 public function provideRegexReplacements() {
154 return [
155 'meaningless characters' => [ ' "()*+-./?[]^a{|}ä' ],
156 'backslash' => [ '\\1' ],
157 'backslash, already escaped' => [ '\\\\1' ],
158 'dollar' => [ '$1' ],
159 'dollar, already escaped' => [ '\$1' ],
160 ];
161 }
162
163 }