|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Neos\Cache\Tests\Functional\Backend; |
| 4 | + |
| 5 | +include_once(__DIR__ . '/../../BaseTestCase.php'); |
| 6 | + |
| 7 | +/* |
| 8 | + * This file is part of the Neos.Cache package. |
| 9 | + * |
| 10 | + * (c) Contributors of the Neos Project - www.neos.io |
| 11 | + * |
| 12 | + * This package is Open Source Software. For the full copyright and license |
| 13 | + * information, please view the LICENSE file which was distributed with this |
| 14 | + * source code. |
| 15 | + */ |
| 16 | + |
| 17 | +use Exception; |
| 18 | +use Neos\Cache\Backend\RedisBackend; |
| 19 | +use Neos\Cache\EnvironmentConfiguration; |
| 20 | +use Neos\Cache\Tests\BaseTestCase; |
| 21 | +use RedisException; |
| 22 | +use Redis; |
| 23 | + |
| 24 | +/** |
| 25 | + * Testcase for the redis cache backend |
| 26 | + * |
| 27 | + * These tests use an actual Redis instance and will place and remove keys in db 0! |
| 28 | + * Since all keys have the 'TestCache:' prefix, running the tests should have |
| 29 | + * no side effects on non-related cache entries. |
| 30 | + * |
| 31 | + * Tests require Redis listening on 127.0.0.1:6379. Furthermore, the following users are required |
| 32 | + * default:<no_password> |
| 33 | + * test_no_password:<no_password> |
| 34 | + * test_password:<secret_password> |
| 35 | + * |
| 36 | + * The users can be added by: |
| 37 | + * acl setuser test_no_password on > ~* &* +@all |
| 38 | + * acl setuser test_password on >secret_password ~* &* +@all |
| 39 | + * |
| 40 | + * @requires extension redis |
| 41 | + */ |
| 42 | +class RedisBackendAuthenticationTest extends BaseTestCase |
| 43 | +{ |
| 44 | + /** |
| 45 | + * @var Redis|null |
| 46 | + */ |
| 47 | + protected static ?Redis $redis = null; |
| 48 | + |
| 49 | + /** |
| 50 | + * Create required Redis users for the test suite |
| 51 | + */ |
| 52 | + public static function setUpBeforeClass(): void |
| 53 | + { |
| 54 | + try { |
| 55 | + self::$redis = new Redis(); |
| 56 | + self::$redis->connect('127.0.0.1', 6379); |
| 57 | + |
| 58 | + // clean state before |
| 59 | + @self::$redis->rawCommand('ACL', 'DELUSER', 'test_no_password'); |
| 60 | + @self::$redis->rawCommand('ACL', 'DELUSER', 'test_password'); |
| 61 | + |
| 62 | + // add users |
| 63 | + self::$redis->rawCommand('ACL', 'SETUSER', 'test_no_password', 'on', '>', '~*', '&*', '+@all'); |
| 64 | + self::$redis->rawCommand('ACL', 'SETUSER', 'test_password', 'on', '>secret_password', '~*', '&*', '+@all'); |
| 65 | + } catch (Exception $e) { |
| 66 | + self::markTestSkipped('Could not prepare Redis users: ' . $e->getMessage()); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Tear down Redis users after the suite has run |
| 72 | + */ |
| 73 | + public static function tearDownAfterClass(): void |
| 74 | + { |
| 75 | + if (self::$redis instanceof Redis) { |
| 76 | + try { |
| 77 | + self::$redis->rawCommand('ACL', 'DELUSER', 'test_no_password'); |
| 78 | + self::$redis->rawCommand('ACL', 'DELUSER', 'test_password'); |
| 79 | + } catch (Exception $e) { |
| 80 | + // ignore |
| 81 | + } |
| 82 | + self::$redis->close(); |
| 83 | + self::$redis = null; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Set up test case |
| 89 | + * |
| 90 | + * @return void |
| 91 | + */ |
| 92 | + protected function setUp(): void |
| 93 | + { |
| 94 | + $phpredisVersion = phpversion('redis'); |
| 95 | + if (version_compare($phpredisVersion, '5.0.0', '<')) { |
| 96 | + $this->markTestSkipped(sprintf('phpredis extension version %s is not supported. Please update to version 5.0.0+.', $phpredisVersion)); |
| 97 | + } |
| 98 | + try { |
| 99 | + if (!@fsockopen('127.0.0.1', 6379)) { |
| 100 | + $this->markTestSkipped('redis server not reachable'); |
| 101 | + } |
| 102 | + } catch (Exception $e) { |
| 103 | + $this->markTestSkipped('redis server not reachable'); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * @test |
| 109 | + */ |
| 110 | + public function defaultUserNoPassword() |
| 111 | + { |
| 112 | + $backend = new RedisBackend( |
| 113 | + new EnvironmentConfiguration('Redis a wonderful color Testing', '/some/path', PHP_MAXPATHLEN), |
| 114 | + ['hostname' => '127.0.0.1', 'database' => 0] |
| 115 | + ); |
| 116 | + $this->assertInstanceOf('Neos\Cache\Backend\RedisBackend', $backend); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * @test |
| 121 | + */ |
| 122 | + public function usernameNoPassword() |
| 123 | + { |
| 124 | + $backend = new RedisBackend( |
| 125 | + new EnvironmentConfiguration('Redis a wonderful color Testing', '/some/path', PHP_MAXPATHLEN), |
| 126 | + ['hostname' => '127.0.0.1', 'database' => 0, 'username' => 'test_no_password'] |
| 127 | + ); |
| 128 | + $this->assertInstanceOf('Neos\Cache\Backend\RedisBackend', $backend); |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * @test |
| 133 | + */ |
| 134 | + public function usernamePassword() |
| 135 | + { |
| 136 | + $backend = new RedisBackend( |
| 137 | + new EnvironmentConfiguration('Redis a wonderful color Testing', '/some/path', PHP_MAXPATHLEN), |
| 138 | + ['hostname' => '127.0.0.1', 'database' => 0, 'username' => 'test_password', 'password' => 'secret_password'] |
| 139 | + ); |
| 140 | + $this->assertInstanceOf('Neos\Cache\Backend\RedisBackend', $backend); |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * @test |
| 145 | + */ |
| 146 | + public function incorrectUsernamePassword() |
| 147 | + { |
| 148 | + $this->expectException(RedisException::class); |
| 149 | + $backend = new RedisBackend( |
| 150 | + new EnvironmentConfiguration('Redis a wonderful color Testing', '/some/path', PHP_MAXPATHLEN), |
| 151 | + ['hostname' => '127.0.0.1', 'database' => 0, 'username' => 'test_password', 'password' => 'incorrect_password'] |
| 152 | + ); |
| 153 | + } |
| 154 | +} |
0 commit comments