|
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
|
|
|
array( |
|
215
|
|
|
'65536', |
|
216
|
|
|
'', |
|
217
|
|
|
), |
|
218
|
|
|
); |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* Tests the get_current_request_port method. |
|
223
|
|
|
* |
|
224
|
|
|
* Also used by @see self::test_request_port_constants |
|
225
|
|
|
* |
|
226
|
|
|
* @param mixed $http_x_forwarded_port value of $_SERVER[ 'HTTP_X_FORWARDED_PORT' ]. |
|
227
|
|
|
* @param mixed $server_port value of $_SERVER[ 'SERVER_PORT' ]. Null will unset the value. |
|
228
|
|
|
* @param string $expeceted The expected output. Null will unset the value. |
|
229
|
|
|
* @param boolean $ssl Whether to consider current request using SSL or not. |
|
230
|
|
|
* |
|
231
|
|
|
* @dataProvider get_request_port_data_provider |
|
232
|
|
|
*/ |
|
233
|
|
|
public function test_get_request_port( $http_x_forwarded_port, $server_port, $expeceted, $ssl = false ) { |
|
234
|
|
|
|
|
235
|
|
|
$original_server = $_SERVER; |
|
236
|
|
|
|
|
237
|
|
|
$_SERVER['HTTP_X_FORWARDED_PORT'] = $http_x_forwarded_port; |
|
238
|
|
|
$_SERVER['SERVER_PORT'] = $server_port; |
|
239
|
|
|
|
|
240
|
|
|
if ( $ssl ) { |
|
241
|
|
|
$_SERVER['HTTPS'] = 'on'; // is_ssl will return true. |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
if ( is_null( $_SERVER['HTTP_X_FORWARDED_PORT'] ) ) { |
|
245
|
|
|
unset( $_SERVER['HTTP_X_FORWARDED_PORT'] ); |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
if ( is_null( $_SERVER['HTTP_X_FORWARDED_PORT'] ) ) { |
|
249
|
|
|
unset( $_SERVER['HTTP_X_FORWARDED_PORT'] ); |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
$signature = new \Jetpack_Signature( 'some-secret', 0 ); |
|
|
|
|
|
|
253
|
|
|
$port = $signature->get_current_request_port(); |
|
254
|
|
|
|
|
255
|
|
|
$_SERVER = $original_server; |
|
256
|
|
|
|
|
257
|
|
|
$this->assertSame( $expeceted, $port ); |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
/** |
|
261
|
|
|
* Data provider for test_get_request_port |
|
262
|
|
|
* |
|
263
|
|
|
* @return array |
|
264
|
|
|
*/ |
|
265
|
|
|
public function get_request_port_data_provider() { |
|
266
|
|
|
return array( |
|
267
|
|
|
array( |
|
268
|
|
|
'', |
|
269
|
|
|
80, |
|
270
|
|
|
'', |
|
271
|
|
|
), |
|
272
|
|
|
array( |
|
273
|
|
|
'', |
|
274
|
|
|
'80', |
|
275
|
|
|
'', |
|
276
|
|
|
), |
|
277
|
|
|
array( |
|
278
|
|
|
'', |
|
279
|
|
|
null, |
|
280
|
|
|
'', |
|
281
|
|
|
), |
|
282
|
|
|
array( |
|
283
|
|
|
null, |
|
284
|
|
|
null, |
|
285
|
|
|
'', |
|
286
|
|
|
), |
|
287
|
|
|
array( |
|
288
|
|
|
'', |
|
289
|
|
|
81, |
|
290
|
|
|
'81', |
|
291
|
|
|
), |
|
292
|
|
|
array( |
|
293
|
|
|
'', |
|
294
|
|
|
'81', |
|
295
|
|
|
'81', |
|
296
|
|
|
), |
|
297
|
|
|
array( |
|
298
|
|
|
82, |
|
299
|
|
|
'81', |
|
300
|
|
|
'82', |
|
301
|
|
|
), |
|
302
|
|
|
array( |
|
303
|
|
|
'82', |
|
304
|
|
|
'81', |
|
305
|
|
|
'82', |
|
306
|
|
|
), |
|
307
|
|
|
array( |
|
308
|
|
|
82, |
|
309
|
|
|
'', |
|
310
|
|
|
'82', |
|
311
|
|
|
), |
|
312
|
|
|
|
|
313
|
|
|
// SSL. |
|
314
|
|
|
array( |
|
315
|
|
|
'', |
|
316
|
|
|
443, |
|
317
|
|
|
'', |
|
318
|
|
|
true, |
|
319
|
|
|
), |
|
320
|
|
|
array( |
|
321
|
|
|
'', |
|
322
|
|
|
'443', |
|
323
|
|
|
'', |
|
324
|
|
|
true, |
|
325
|
|
|
), |
|
326
|
|
|
array( |
|
327
|
|
|
null, |
|
328
|
|
|
'443', |
|
329
|
|
|
'', |
|
330
|
|
|
true, |
|
331
|
|
|
), |
|
332
|
|
|
array( |
|
333
|
|
|
null, |
|
334
|
|
|
null, |
|
335
|
|
|
'', |
|
336
|
|
|
true, |
|
337
|
|
|
), |
|
338
|
|
|
array( |
|
339
|
|
|
'', |
|
340
|
|
|
444, |
|
341
|
|
|
'444', |
|
342
|
|
|
true, |
|
343
|
|
|
), |
|
344
|
|
|
array( |
|
345
|
|
|
'', |
|
346
|
|
|
'444', |
|
347
|
|
|
'444', |
|
348
|
|
|
true, |
|
349
|
|
|
), |
|
350
|
|
|
array( |
|
351
|
|
|
445, |
|
352
|
|
|
'444', |
|
353
|
|
|
'445', |
|
354
|
|
|
true, |
|
355
|
|
|
), |
|
356
|
|
|
array( |
|
357
|
|
|
'445', |
|
358
|
|
|
'444', |
|
359
|
|
|
'445', |
|
360
|
|
|
true, |
|
361
|
|
|
), |
|
362
|
|
|
array( |
|
363
|
|
|
445, |
|
364
|
|
|
'', |
|
365
|
|
|
'445', |
|
366
|
|
|
true, |
|
367
|
|
|
), |
|
368
|
|
|
|
|
369
|
|
|
// Invalid values. |
|
370
|
|
|
array( |
|
371
|
|
|
'', |
|
372
|
|
|
new stdClass(), |
|
373
|
|
|
'', |
|
374
|
|
|
), |
|
375
|
|
|
array( |
|
376
|
|
|
'', |
|
377
|
|
|
'string', |
|
378
|
|
|
'', |
|
379
|
|
|
), |
|
380
|
|
|
array( |
|
381
|
|
|
'', |
|
382
|
|
|
array( 'string' ), |
|
383
|
|
|
'', |
|
384
|
|
|
), |
|
385
|
|
|
|
|
386
|
|
|
); |
|
387
|
|
|
} |
|
388
|
|
|
|
|
389
|
|
|
/** |
|
390
|
|
|
* Runs isolated tests to check the behavior of constants |
|
391
|
|
|
* |
|
392
|
|
|
* Uses @see self::test_get_request_port |
|
393
|
|
|
* |
|
394
|
|
|
* @runInSeparateProcess |
|
395
|
|
|
* @preserveGlobalState disabled |
|
396
|
|
|
*/ |
|
397
|
|
|
public function test_request_port_constants() { |
|
398
|
|
|
define( 'JETPACK_SIGNATURE__HTTP_PORT', 81 ); // http as integer. |
|
399
|
|
|
$this->test_get_request_port( 81, '', '' ); |
|
400
|
|
|
$this->test_get_request_port( '81', '', '' ); |
|
401
|
|
|
$this->test_get_request_port( 81, '82', '' ); |
|
402
|
|
|
$this->test_get_request_port( 82, '81', '82' ); |
|
403
|
|
|
$this->test_get_request_port( '82', '81', '82' ); |
|
404
|
|
|
|
|
405
|
|
|
define( 'JETPACK_SIGNATURE__HTTPS_PORT', '444' ); // https as string. |
|
406
|
|
|
$this->test_get_request_port( 444, '', '', true ); |
|
407
|
|
|
$this->test_get_request_port( '444', '', '', true ); |
|
408
|
|
|
$this->test_get_request_port( 444, '445', '', true ); |
|
409
|
|
|
$this->test_get_request_port( 445, '444', '445', true ); |
|
410
|
|
|
$this->test_get_request_port( '445', '444', '445', true ); |
|
411
|
|
|
} |
|
412
|
|
|
|
|
413
|
|
|
} |
|
414
|
|
|
|
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: