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