|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Unit; |
| 4 | + |
| 5 | +use PDO; |
| 6 | +use Tests\TestCase; |
| 7 | + |
| 8 | +class DatabaseSslConfigurationTest extends TestCase |
| 9 | +{ |
| 10 | + public function testMysqlSslConfigurationWithPaasMode(): void |
| 11 | + { |
| 12 | + config([ |
| 13 | + 'database.connections.mysql.options' => null |
| 14 | + ]); |
| 15 | + |
| 16 | + // Test PAAS mode SSL configuration |
| 17 | + config([ |
| 18 | + 'database.connections.mysql.options' => $this->getSslOptions(true, false, true) // isPaas=true, verifyServer=false, sslEnabled=true |
| 19 | + ]); |
| 20 | + |
| 21 | + $options = config('database.connections.mysql.options'); |
| 22 | + |
| 23 | + $this->assertArrayHasKey(PDO::MYSQL_ATTR_SSL_CA, $options); |
| 24 | + $this->assertArrayHasKey(PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT, $options); |
| 25 | + |
| 26 | + // PAAS mode should not include client certificate attributes |
| 27 | + $this->assertArrayNotHasKey(PDO::MYSQL_ATTR_SSL_KEY, $options); |
| 28 | + $this->assertArrayNotHasKey(PDO::MYSQL_ATTR_SSL_CERT, $options); |
| 29 | + $this->assertArrayNotHasKey(PDO::MYSQL_ATTR_SSL_CIPHER, $options); |
| 30 | + } |
| 31 | + |
| 32 | + public function testMysqlSslConfigurationWithNonPaasMode(): void |
| 33 | + { |
| 34 | + config([ |
| 35 | + 'database.connections.mysql.options' => null |
| 36 | + ]); |
| 37 | + |
| 38 | + // Test non-PAAS mode SSL configuration |
| 39 | + config([ |
| 40 | + 'database.connections.mysql.options' => $this->getSslOptions(false, false, true) // isPaas=false, verifyServer=false, sslEnabled=true |
| 41 | + ]); |
| 42 | + |
| 43 | + $options = config('database.connections.mysql.options'); |
| 44 | + |
| 45 | + // Non-PAAS mode should include all SSL attributes |
| 46 | + $this->assertArrayHasKey(PDO::MYSQL_ATTR_SSL_CA, $options); |
| 47 | + $this->assertArrayHasKey(PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT, $options); |
| 48 | + $this->assertArrayHasKey(PDO::MYSQL_ATTR_SSL_KEY, $options); |
| 49 | + $this->assertArrayHasKey(PDO::MYSQL_ATTR_SSL_CERT, $options); |
| 50 | + $this->assertArrayHasKey(PDO::MYSQL_ATTR_SSL_CIPHER, $options); |
| 51 | + } |
| 52 | + |
| 53 | + public function testMysqlSslConfigurationWithoutSsl(): void |
| 54 | + { |
| 55 | + config([ |
| 56 | + 'database.connections.mysql.options' => null |
| 57 | + ]); |
| 58 | + |
| 59 | + // Test SSL disabled configuration |
| 60 | + config([ |
| 61 | + 'database.connections.mysql.options' => $this->getSslOptions(true, false, false) // isPaas=true, verifyServer=false, sslEnabled=false |
| 62 | + ]); |
| 63 | + |
| 64 | + $options = config('database.connections.mysql.options'); |
| 65 | + |
| 66 | + // When SSL is disabled, options should be empty |
| 67 | + $this->assertEmpty($options); |
| 68 | + } |
| 69 | + |
| 70 | + public function testSslVerifyServerDefaultsToFalse(): void |
| 71 | + { |
| 72 | + // Test that SSL_VERIFY_SERVER defaults to false when not explicitly set |
| 73 | + $options = $this->getSslOptions(true, null, true); // isPaas=true, verifyServer=null, sslEnabled=true |
| 74 | + |
| 75 | + $this->assertArrayHasKey(PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT, $options); |
| 76 | + $this->assertFalse($options[PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT]); |
| 77 | + } |
| 78 | + |
| 79 | + private function getSslOptions(bool $isPaas, ?bool $verifyServer=false, bool $sslEnabled=true): array |
| 80 | + { |
| 81 | + // simulates the SSL options logic from database.php |
| 82 | + if (!$sslEnabled) { |
| 83 | + return []; |
| 84 | + } |
| 85 | + |
| 86 | + if ($isPaas) { |
| 87 | + return [ |
| 88 | + PDO::MYSQL_ATTR_SSL_CA => '/path/to/ca.pem', |
| 89 | + PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT => $verifyServer ?? false, |
| 90 | + ]; |
| 91 | + } |
| 92 | + |
| 93 | + return [ |
| 94 | + PDO::MYSQL_ATTR_SSL_KEY => '/path/to/key.pem', |
| 95 | + PDO::MYSQL_ATTR_SSL_CERT => '/path/to/cert.pem', |
| 96 | + PDO::MYSQL_ATTR_SSL_CA => '/path/to/ca.pem', |
| 97 | + PDO::MYSQL_ATTR_SSL_CIPHER => null, |
| 98 | + PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT => $verifyServer ?? false, |
| 99 | + ]; |
| 100 | + } |
| 101 | +} |
0 commit comments