1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SimpleSAML\Module\Monitor\TestCase\Network; |
4
|
|
|
|
5
|
|
|
use SimpleSAML\Module\Monitor\State; |
6
|
|
|
use SimpleSAML\Module\Monitor\TestData; |
7
|
|
|
use SimpleSAML\Module\Monitor\TestResult; |
8
|
|
|
|
9
|
|
|
final class ConnectUri extends \SimpleSAML\Module\Monitor\TestCaseFactory |
10
|
|
|
{ |
11
|
|
|
/** @var integer */ |
12
|
|
|
private $timeout; |
13
|
|
|
|
14
|
|
|
/** @var resource */ |
15
|
|
|
private $context; |
16
|
|
|
|
17
|
|
|
/** @var string */ |
18
|
|
|
private $uri; |
19
|
|
|
|
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param \SimpleSAML\Module\Monitor\TestData $testData |
23
|
|
|
* |
24
|
|
|
* @return void |
25
|
|
|
*/ |
26
|
|
|
protected function initialize(TestData $testData): void |
27
|
|
|
{ |
28
|
|
|
$uri = $testData->getInputItem('uri'); |
29
|
|
|
$context = $testData->getInputItem('context'); |
30
|
|
|
|
31
|
|
|
if (is_null($context)) { |
32
|
|
|
$context = stream_context_create(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
$timeout = $testData->getInputItem('timeout'); |
36
|
|
|
$timeout = is_null($timeout) ? (int)ini_get("default_socket_timeout") : $timeout; |
37
|
|
|
|
38
|
|
|
assert(is_string($uri)); |
39
|
|
|
assert(is_resource($context)); |
40
|
|
|
assert(is_int($timeout)); |
41
|
|
|
|
42
|
|
|
$this->setUri($uri); |
43
|
|
|
$this->setContext($context); |
44
|
|
|
$this->setTimeout($timeout); |
45
|
|
|
|
46
|
|
|
parent::initialize($testData); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param string $uri |
52
|
|
|
* |
53
|
|
|
* @return void |
54
|
|
|
*/ |
55
|
|
|
private function setUri(string $uri): void |
56
|
|
|
{ |
57
|
|
|
$this->uri = $uri; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param resource $context |
63
|
|
|
* |
64
|
|
|
* @return void |
65
|
|
|
*/ |
66
|
|
|
private function setContext($context): void |
67
|
|
|
{ |
68
|
|
|
$this->context = $context; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param integer $timeout |
74
|
|
|
* |
75
|
|
|
* @return void |
76
|
|
|
*/ |
77
|
|
|
private function setTimeout(int $timeout): void |
78
|
|
|
{ |
79
|
|
|
$this->timeout = $timeout; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return void |
85
|
|
|
*/ |
86
|
|
|
public function invokeTest(): void |
87
|
|
|
{ |
88
|
|
|
list($errno, $errstr) = [0, '']; |
89
|
|
|
$connection = @stream_socket_client( |
90
|
|
|
$this->uri, |
91
|
|
|
$errno, |
92
|
|
|
$errstr, |
93
|
|
|
$this->timeout, |
94
|
|
|
STREAM_CLIENT_CONNECT, |
95
|
|
|
$this->context |
96
|
|
|
); |
97
|
|
|
|
98
|
|
|
$testResult = new TestResult('Network connection', $this->uri); |
99
|
|
|
|
100
|
|
|
if (is_resource($connection)) { |
101
|
|
|
$params = stream_context_get_params($connection); |
102
|
|
|
|
103
|
|
|
$testResult->addOutput($connection, 'connection'); |
104
|
|
|
if (isset($params['options']['ssl']['peer_certificate'])) { |
105
|
|
|
$certData = openssl_x509_parse($params['options']['ssl']['peer_certificate']); |
106
|
|
|
$testResult->addOutput($certData, 'certData'); |
107
|
|
|
} |
108
|
|
|
$testResult->setState(State::OK); |
109
|
|
|
$testResult->setMessage('Connection established'); |
110
|
|
|
} else { |
111
|
|
|
$testResult->setState(State::ERROR); |
112
|
|
|
$testResult->setMessage($errstr . ' (' . $errno . ')'); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$this->setTestResult($testResult); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|