Completed
Push — update/gardening-action-extrac... ( 967522...8b9cf8 )
by Jeremy
24:34 queued 12:22
created

XMLRPC_Connector_Test   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 131
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A set_up_before_class() 0 15 1
A test_is_request_signed_by_jetpack_debugger() 0 17 5
A is_request_signed_by_jetpack_debugger_data() 0 52 1
1
<?php // phpcs:ignore WordPress.Files.FileName.NotHyphenatedLowercase
2
/**
3
 * Connection Manager functionality testing.
4
 *
5
 * @package automattic/jetpack-connection
6
 */
7
8
namespace Automattic\Jetpack\Connection;
9
10
use PHPUnit\Framework\TestCase;
11
12
/**
13
 * Text XMLRPC Connector
14
 */
15
class XMLRPC_Connector_Test extends TestCase {
16
17
	/**
18
	 * Public key to verify signature
19
	 *
20
	 * @var string
21
	 */
22
	public static $public_key;
23
24
	/**
25
	 * Base64 encoded signature
26
	 *
27
	 * @var string
28
	 */
29
	public static $signature;
30
31
	/**
32
	 * Timestamp of the signature
33
	 *
34
	 * @var integer
35
	 */
36
	public static $timestamp;
37
38
	/**
39
	 * Initialize tests
40
	 *
41
	 * @beforeClass
42
	 */
43
	public static function set_up_before_class() {
44
		$keys = openssl_pkey_new();
45
		openssl_pkey_export( $keys, $private_key );
46
		$public_key       = openssl_pkey_get_details( $keys );
47
		self::$public_key = $public_key['key'];
48
		self::$timestamp  = time();
49
		$url_parameters   = array(
50
			'rest_route' => '/jetpack/v4/connection/test/',
51
			'timestamp'  => self::$timestamp,
52
			'url'        => 'https://example.com',
53
		);
54
55
		openssl_sign( wp_json_encode( $url_parameters ), $signature, $private_key );
56
		self::$signature = ( base64_encode( $signature ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode
57
	}
58
59
	/**
60
	 * Tests is_request_signed_by_jetpack_debugger
61
	 *
62
	 * @param array $get_params The value of $_GET.
63
	 * @param bool  $expected The expected return of is_request_signed_by_jetpack_debugger.
64
	 *
65
	 * @dataProvider is_request_signed_by_jetpack_debugger_data
66
	 * @covers Automattic\Jetpack\Connection\REST_Connector::is_request_signed_by_jetpack_debugger
67
	 * @return void
68
	 */
69
	public function test_is_request_signed_by_jetpack_debugger( $get_params, $expected ) {
70
71
		if ( isset( $get_params['signature'] ) && '__VALID__' === $get_params['signature'] ) {
72
			$get_params['signature'] = self::$signature;
73
		}
74
75
		if ( isset( $get_params['timestamp'] ) && '__VALID__' === $get_params['timestamp'] ) {
76
			$get_params['timestamp'] = self::$timestamp;
77
		}
78
79
		$old_get  = $_GET; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
80
		$_GET     = $get_params;
81
		$response = REST_Connector::is_request_signed_by_jetpack_debugger( self::$public_key );
82
		$_GET     = $old_get;
83
84
		$this->assertSame( $expected, $response );
85
	}
86
87
	/**
88
	 * Data provider for test_is_request_signed_by_jetpack_debugger
89
	 *
90
	 * @return array
91
	 */
92
	public function is_request_signed_by_jetpack_debugger_data() {
93
		return array(
94
			'empty get'                    => array(
95
				array(),
96
				false,
97
			),
98
			'incomplete get'               => array(
99
				array(
100
					'timestamp' => '__VALID__',
101
					'url'       => 'https://example.com',
102
					'signature' => '__VALID__',
103
				),
104
				false,
105
			),
106
			'valid get'                    => array(
107
				array(
108
					'rest_route' => '/jetpack/v4/connection/test/',
109
					'timestamp'  => '__VALID__',
110
					'url'        => 'https://example.com',
111
					'signature'  => '__VALID__',
112
				),
113
				true,
114
			),
115
			'valid get, invalid signature' => array(
116
				array(
117
					'rest_route' => '/jetpack/v4/connection/test/',
118
					'timestamp'  => time(),
119
					'url'        => 'https://example.com',
120
					'signature'  => 'invalid',
121
				),
122
				false,
123
			),
124
			'valid get, invalid url'       => array(
125
				array(
126
					'rest_route' => '/jetpack/v4/connection/test/',
127
					'timestamp'  => '__VALID__',
128
					'url'        => 'https://bad-example.com',
129
					'signature'  => '__VALID__',
130
				),
131
				false,
132
			),
133
			'outdated signature'           => array(
134
				array(
135
					'signature'  => '__VALID__',
136
					'timestamp'  => time() - 400,
137
					'url'        => 'https://example.com',
138
					'rest_route' => '/jetpack/v4/connection/test/',
139
				),
140
				false,
141
			),
142
		);
143
	}
144
145
}
146