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