1
|
|
|
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
2
|
|
|
/** |
3
|
|
|
* The SignatureTest class file. |
4
|
|
|
* |
5
|
|
|
* @package automattic/jetpack-connection |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Automattic\Jetpack\Connection; |
9
|
|
|
|
10
|
|
|
use PHPUnit\Framework\TestCase; |
11
|
|
|
use stdClass; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Provides unit tests for the methods in the Jetpack_Signature class. |
15
|
|
|
*/ |
16
|
|
|
class SignatureTest extends TestCase { |
17
|
|
|
/** |
18
|
|
|
* Tests the Jetpack_Signature->join_with_equal_sign() method. |
19
|
|
|
* |
20
|
|
|
* @covers Automattic\Jetpack\Connection\Jetpack_Signature->join_with_equal_sign |
21
|
|
|
* @dataProvider join_with_equal_sign_data_provider |
22
|
|
|
* |
23
|
|
|
* @param string $name Query string key value. |
24
|
|
|
* @param string|array $value Associated value for query string key. |
25
|
|
|
* @param string|array $expected_output The expected output of $signature->join_with_equal_sign. |
26
|
|
|
*/ |
27
|
|
|
public function test_join_with_equal_sign( $name, $value, $expected_output ) { |
28
|
|
|
$signature = new \Jetpack_Signature( 'some-secret', 0 ); |
|
|
|
|
29
|
|
|
$this->assertEquals( $expected_output, $signature->join_with_equal_sign( $name, $value ) ); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Data provider for test_join_with_equal_sign. |
34
|
|
|
* |
35
|
|
|
* The test data arrays have the format: |
36
|
|
|
* 'name' => The value that the constant will be set to. Null if the constant will not be set. |
37
|
|
|
* 'value' => The name of the constant. |
38
|
|
|
* 'expected_output' => The expected output of $signature->join_with_equal_sign. |
39
|
|
|
*/ |
40
|
|
|
public function join_with_equal_sign_data_provider() { |
41
|
|
|
return array( |
42
|
|
|
'string_value' => |
43
|
|
|
array( |
44
|
|
|
'name' => 'street', |
45
|
|
|
'value' => '1600 Pennsylvania Ave', |
46
|
|
|
'expected_output' => 'street=1600 Pennsylvania Ave', |
47
|
|
|
), |
48
|
|
|
'array_value' => |
49
|
|
|
array( |
50
|
|
|
'name' => 'first_names', |
51
|
|
|
'value' => array( 'Michael', 'Jim', 'Pam' ), |
52
|
|
|
'expected_output' => array( 'first_names[0]=Michael', 'first_names[1]=Jim', 'first_names[2]=Pam' ), |
53
|
|
|
), |
54
|
|
|
'associative_array_value' => |
55
|
|
|
array( |
56
|
|
|
'name' => 'numbers', |
57
|
|
|
'value' => array( |
58
|
|
|
'one' => 1, |
59
|
|
|
'two' => 2, |
60
|
|
|
), |
61
|
|
|
'expected_output' => array( 'numbers[one]=1', 'numbers[two]=2' ), |
62
|
|
|
), |
63
|
|
|
'nested_array_value' => |
64
|
|
|
array( |
65
|
|
|
'name' => 'numbers', |
66
|
|
|
'value' => array( array( 0, 1 ), array( 2, 3 ), array( 4, 5 ) ), |
67
|
|
|
'expected_output' => array( |
68
|
|
|
'numbers[0][0]=0', |
69
|
|
|
'numbers[0][1]=1', |
70
|
|
|
'numbers[1][0]=2', |
71
|
|
|
'numbers[1][1]=3', |
72
|
|
|
'numbers[2][0]=4', |
73
|
|
|
'numbers[2][1]=5', |
74
|
|
|
), |
75
|
|
|
), |
76
|
|
|
'nested_associative_array_value' => |
77
|
|
|
array( |
78
|
|
|
'name' => 'people', |
79
|
|
|
'value' => array( |
80
|
|
|
array( |
81
|
|
|
'last_name' => 'Scott', |
82
|
|
|
'first_name' => 'Michael', |
83
|
|
|
'city' => 'Boulder', |
84
|
|
|
), |
85
|
|
|
array( |
86
|
|
|
'first_name' => 'Jim', |
87
|
|
|
'state' => 'Texas', |
88
|
|
|
'last_name' => 'Halpert', |
89
|
|
|
), |
90
|
|
|
), |
91
|
|
|
// Note: Expected output is sorted. |
92
|
|
|
'expected_output' => array( |
93
|
|
|
'people[0][city]=Boulder', |
94
|
|
|
'people[0][first_name]=Michael', |
95
|
|
|
'people[0][last_name]=Scott', |
96
|
|
|
'people[1][first_name]=Jim', |
97
|
|
|
'people[1][last_name]=Halpert', |
98
|
|
|
'people[1][state]=Texas', |
99
|
|
|
), |
100
|
|
|
), |
101
|
|
|
); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Tests the Jetpack_Signature->normalized_query_parameters() method. |
106
|
|
|
* |
107
|
|
|
* @covers Automattic\Jetpack\Connection\Jetpack_Signature->normalized_query_parameters |
108
|
|
|
* @dataProvider normalized_query_parameters_data_provider |
109
|
|
|
* |
110
|
|
|
* @param string $query_string Query string key value. |
111
|
|
|
* @param string|array $expected_output The expected output of $signature->normalized_query_parameters. |
112
|
|
|
*/ |
113
|
|
|
public function test_normalized_query_parameters( $query_string, $expected_output ) { |
114
|
|
|
$signature = new \Jetpack_Signature( 'some-secret', 0 ); |
|
|
|
|
115
|
|
|
$this->assertEquals( $expected_output, $signature->normalized_query_parameters( $query_string ) ); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Data provider for test_join_with_equal_sign. |
120
|
|
|
* |
121
|
|
|
* The test data arrays have the format: |
122
|
|
|
* 'name' => The value that the constant will be set to. Null if the constant will not be set. |
123
|
|
|
* 'value' => The name of the constant. |
124
|
|
|
* 'expected_output' => The expected output of $signature->normalized_query_parameters(). |
125
|
|
|
*/ |
126
|
|
|
public function normalized_query_parameters_data_provider() { |
127
|
|
|
return array( |
128
|
|
|
'signature_omitted' => |
129
|
|
|
array( |
130
|
|
|
'query_string' => 'size=10&signature=super-secret', |
131
|
|
|
'expected_output' => array( |
132
|
|
|
'size=10', |
133
|
|
|
), |
134
|
|
|
), |
135
|
|
|
'query_key_sort' => |
136
|
|
|
array( |
137
|
|
|
'query_string' => 'size=10&highlight_fields%5B0%5D=title&highlight_fields%5B1%5D=content&aggregations%5Btaxonomy_1%5D%5Bterms%5D%5Bfield%5D=taxonomy.xposts.slug_slash_name&aggregations%5Btaxonomy_1%5D%5Bterms%5D%5Bsize%5D=5&fields%5B0%5D=date&fields%5B1%5D=permalink.url.raw&query=journey', |
138
|
|
|
'expected_output' => array( |
139
|
|
|
'query=journey', |
140
|
|
|
// Note that size has been sorted below query. |
141
|
|
|
'size=10', |
142
|
|
|
array( |
143
|
|
|
'aggregations[taxonomy_1][terms][field]=taxonomy.xposts.slug_slash_name', |
144
|
|
|
'aggregations[taxonomy_1][terms][size]=5', |
145
|
|
|
), |
146
|
|
|
array( |
147
|
|
|
'fields[0]=date', |
148
|
|
|
'fields[1]=permalink.url.raw', |
149
|
|
|
), |
150
|
|
|
// Note that highlight_fields has been sorted below aggregations and fields. |
151
|
|
|
array( |
152
|
|
|
'highlight_fields[0]=title', |
153
|
|
|
'highlight_fields[1]=content', |
154
|
|
|
), |
155
|
|
|
), |
156
|
|
|
), |
157
|
|
|
); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Test sanitize_host_post method |
162
|
|
|
* |
163
|
|
|
* @group leo |
164
|
|
|
* @dataProvider sanitize_host_post_data_provider |
165
|
|
|
* |
166
|
|
|
* @param mixed $input the input to the method. |
167
|
|
|
* @param string $expected the expected output. |
168
|
|
|
* @return void |
169
|
|
|
*/ |
170
|
|
|
public function test_sanitize_host_post( $input, $expected ) { |
171
|
|
|
$signature = new \Jetpack_Signature( 'some-secret', 0 ); |
|
|
|
|
172
|
|
|
$this->assertSame( $expected, $signature->sanitize_host_post( $input ) ); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Data provider for test_sanitize_host_post |
177
|
|
|
* |
178
|
|
|
* @return array |
179
|
|
|
*/ |
180
|
|
|
public function sanitize_host_post_data_provider() { |
181
|
|
|
return array( |
182
|
|
|
array( |
183
|
|
|
'', |
184
|
|
|
'', |
185
|
|
|
), |
186
|
|
|
array( |
187
|
|
|
null, |
188
|
|
|
'', |
189
|
|
|
), |
190
|
|
|
array( |
191
|
|
|
false, |
192
|
|
|
'', |
193
|
|
|
), |
194
|
|
|
array( |
195
|
|
|
array(), |
196
|
|
|
'', |
197
|
|
|
), |
198
|
|
|
array( |
199
|
|
|
new stdClass(), |
200
|
|
|
'', |
201
|
|
|
), |
202
|
|
|
array( |
203
|
|
|
'string', |
204
|
|
|
'', |
205
|
|
|
), |
206
|
|
|
array( |
207
|
|
|
13, |
208
|
|
|
'13', |
209
|
|
|
), |
210
|
|
|
array( |
211
|
|
|
'13', |
212
|
|
|
'13', |
213
|
|
|
), |
214
|
|
|
); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Tests the get_current_request_port method. |
219
|
|
|
* |
220
|
|
|
* Also used by @see self::test_request_port_constants |
221
|
|
|
* |
222
|
|
|
* @param mixed $http_x_forwarded_port value of $_SERVER[ 'HTTP_X_FORWARDED_PORT' ]. |
223
|
|
|
* @param mixed $server_port value of $_SERVER[ 'SERVER_PORT' ]. Null will unset the value. |
224
|
|
|
* @param string $expeceted The expected output. Null will unset the value. |
225
|
|
|
* @param boolean $ssl Whether to consider current request using SSL or not. |
226
|
|
|
* |
227
|
|
|
* @dataProvider get_request_port_data_provider |
228
|
|
|
*/ |
229
|
|
|
public function test_get_request_port( $http_x_forwarded_port, $server_port, $expeceted, $ssl = false ) { |
230
|
|
|
|
231
|
|
|
$original_server = $_SERVER; |
232
|
|
|
|
233
|
|
|
$_SERVER['HTTP_X_FORWARDED_PORT'] = $http_x_forwarded_port; |
234
|
|
|
$_SERVER['SERVER_PORT'] = $server_port; |
235
|
|
|
|
236
|
|
|
if ( $ssl ) { |
237
|
|
|
$_SERVER['HTTPS'] = 'on'; // is_ssl will return true. |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
if ( is_null( $_SERVER['HTTP_X_FORWARDED_PORT'] ) ) { |
241
|
|
|
unset( $_SERVER['HTTP_X_FORWARDED_PORT'] ); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
if ( is_null( $_SERVER['HTTP_X_FORWARDED_PORT'] ) ) { |
245
|
|
|
unset( $_SERVER['HTTP_X_FORWARDED_PORT'] ); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
$signature = new \Jetpack_Signature( 'some-secret', 0 ); |
|
|
|
|
249
|
|
|
$port = $signature->get_current_request_port(); |
250
|
|
|
|
251
|
|
|
$_SERVER = $original_server; |
252
|
|
|
|
253
|
|
|
$this->assertSame( $expeceted, $port ); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* Data provider for test_get_request_port |
258
|
|
|
* |
259
|
|
|
* @return array |
260
|
|
|
*/ |
261
|
|
|
public function get_request_port_data_provider() { |
262
|
|
|
return array( |
263
|
|
|
array( |
264
|
|
|
'', |
265
|
|
|
80, |
266
|
|
|
'', |
267
|
|
|
), |
268
|
|
|
array( |
269
|
|
|
'', |
270
|
|
|
'80', |
271
|
|
|
'', |
272
|
|
|
), |
273
|
|
|
array( |
274
|
|
|
'', |
275
|
|
|
null, |
276
|
|
|
'', |
277
|
|
|
), |
278
|
|
|
array( |
279
|
|
|
null, |
280
|
|
|
null, |
281
|
|
|
'', |
282
|
|
|
), |
283
|
|
|
array( |
284
|
|
|
'', |
285
|
|
|
81, |
286
|
|
|
'81', |
287
|
|
|
), |
288
|
|
|
array( |
289
|
|
|
'', |
290
|
|
|
'81', |
291
|
|
|
'81', |
292
|
|
|
), |
293
|
|
|
array( |
294
|
|
|
82, |
295
|
|
|
'81', |
296
|
|
|
'82', |
297
|
|
|
), |
298
|
|
|
array( |
299
|
|
|
'82', |
300
|
|
|
'81', |
301
|
|
|
'82', |
302
|
|
|
), |
303
|
|
|
array( |
304
|
|
|
82, |
305
|
|
|
'', |
306
|
|
|
'82', |
307
|
|
|
), |
308
|
|
|
|
309
|
|
|
// SSL. |
310
|
|
|
array( |
311
|
|
|
'', |
312
|
|
|
443, |
313
|
|
|
'', |
314
|
|
|
true, |
315
|
|
|
), |
316
|
|
|
array( |
317
|
|
|
'', |
318
|
|
|
'443', |
319
|
|
|
'', |
320
|
|
|
true, |
321
|
|
|
), |
322
|
|
|
array( |
323
|
|
|
null, |
324
|
|
|
'443', |
325
|
|
|
'', |
326
|
|
|
true, |
327
|
|
|
), |
328
|
|
|
array( |
329
|
|
|
null, |
330
|
|
|
null, |
331
|
|
|
'', |
332
|
|
|
true, |
333
|
|
|
), |
334
|
|
|
array( |
335
|
|
|
'', |
336
|
|
|
444, |
337
|
|
|
'444', |
338
|
|
|
true, |
339
|
|
|
), |
340
|
|
|
array( |
341
|
|
|
'', |
342
|
|
|
'444', |
343
|
|
|
'444', |
344
|
|
|
true, |
345
|
|
|
), |
346
|
|
|
array( |
347
|
|
|
445, |
348
|
|
|
'444', |
349
|
|
|
'445', |
350
|
|
|
true, |
351
|
|
|
), |
352
|
|
|
array( |
353
|
|
|
'445', |
354
|
|
|
'444', |
355
|
|
|
'445', |
356
|
|
|
true, |
357
|
|
|
), |
358
|
|
|
array( |
359
|
|
|
445, |
360
|
|
|
'', |
361
|
|
|
'445', |
362
|
|
|
true, |
363
|
|
|
), |
364
|
|
|
|
365
|
|
|
// Invalid values. |
366
|
|
|
array( |
367
|
|
|
'', |
368
|
|
|
new stdClass(), |
369
|
|
|
'', |
370
|
|
|
), |
371
|
|
|
array( |
372
|
|
|
'', |
373
|
|
|
'string', |
374
|
|
|
'', |
375
|
|
|
), |
376
|
|
|
array( |
377
|
|
|
'', |
378
|
|
|
array( 'string' ), |
379
|
|
|
'', |
380
|
|
|
), |
381
|
|
|
|
382
|
|
|
); |
383
|
|
|
} |
384
|
|
|
|
385
|
|
|
/** |
386
|
|
|
* Runs isolated tests to check the behavior of constants |
387
|
|
|
* |
388
|
|
|
* Uses @see self::test_get_request_port |
389
|
|
|
* |
390
|
|
|
* @runInSeparateProcess |
391
|
|
|
* @preserveGlobalState disabled |
392
|
|
|
*/ |
393
|
|
View Code Duplication |
public function test_request_port_constants() { |
394
|
|
|
define( 'JETPACK_SIGNATURE__HTTP_PORT', 81 ); |
395
|
|
|
$this->test_get_request_port( 81, '', '' ); |
396
|
|
|
$this->test_get_request_port( '81', '', '' ); |
397
|
|
|
$this->test_get_request_port( 81, '82', '' ); |
398
|
|
|
$this->test_get_request_port( 82, '81', '82' ); |
399
|
|
|
$this->test_get_request_port( '82', '81', '82' ); |
400
|
|
|
|
401
|
|
|
define( 'JETPACK_SIGNATURE__HTTPS_PORT', 444 ); |
402
|
|
|
$this->test_get_request_port( 444, '', '', true ); |
403
|
|
|
$this->test_get_request_port( '444', '', '', true ); |
404
|
|
|
$this->test_get_request_port( 444, '445', '', true ); |
405
|
|
|
$this->test_get_request_port( 445, '444', '445', true ); |
406
|
|
|
$this->test_get_request_port( '445', '444', '445', true ); |
407
|
|
|
} |
408
|
|
|
|
409
|
|
|
/** |
410
|
|
|
* Runs isolated tests to check the behavior of constants when declared as strings |
411
|
|
|
* |
412
|
|
|
* Uses @see self::test_get_request_port |
413
|
|
|
* |
414
|
|
|
* @runInSeparateProcess |
415
|
|
|
* @preserveGlobalState disabled |
416
|
|
|
*/ |
417
|
|
View Code Duplication |
public function test_request_port_constants_as_strings() { |
418
|
|
|
define( 'JETPACK_SIGNATURE__HTTP_PORT', '81' ); |
419
|
|
|
$this->test_get_request_port( 81, '', '' ); |
420
|
|
|
$this->test_get_request_port( '81', '', '' ); |
421
|
|
|
$this->test_get_request_port( 81, '82', '' ); |
422
|
|
|
$this->test_get_request_port( 82, '81', '82' ); |
423
|
|
|
$this->test_get_request_port( '82', '81', '82' ); |
424
|
|
|
|
425
|
|
|
define( 'JETPACK_SIGNATURE__HTTPS_PORT', '444' ); |
426
|
|
|
$this->test_get_request_port( 444, '', '', true ); |
427
|
|
|
$this->test_get_request_port( '444', '', '', true ); |
428
|
|
|
$this->test_get_request_port( 444, '445', '', true ); |
429
|
|
|
$this->test_get_request_port( 445, '444', '445', true ); |
430
|
|
|
$this->test_get_request_port( '445', '444', '445', true ); |
431
|
|
|
} |
432
|
|
|
|
433
|
|
|
/** |
434
|
|
|
* Runs isolated tests to check the behavior of constants with invalid values |
435
|
|
|
* |
436
|
|
|
* Uses @see self::test_get_request_port |
437
|
|
|
* |
438
|
|
|
* @runInSeparateProcess |
439
|
|
|
* @preserveGlobalState disabled |
440
|
|
|
*/ |
441
|
|
|
public function test_request_port_constants_invalid_valies() { |
442
|
|
|
define( 'JETPACK_SIGNATURE__HTTP_PORT', 'string' ); |
443
|
|
|
$this->test_get_request_port( 81, '', '81' ); |
444
|
|
|
$this->test_get_request_port( '80', '', '' ); |
445
|
|
|
|
446
|
|
|
define( 'JETPACK_SIGNATURE__HTTPS_PORT', false ); |
447
|
|
|
$this->test_get_request_port( 444, '', '444', true ); |
448
|
|
|
$this->test_get_request_port( '443', '', '', true ); |
449
|
|
|
} |
450
|
|
|
|
451
|
|
|
} |
452
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: