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