@@ -17,17 +17,17 @@ final class IpVerificationTest extends TestCase
17
17
/** @var LoggerInterface */
18
18
private $ logger ;
19
19
20
- /** @var bool */
20
+ /** @var bool */
21
21
private $ useCurl ;
22
22
23
- /** @var bool */
23
+ /** @var bool */
24
24
private $ useTls ;
25
25
26
26
protected function setUp (): void
27
27
{
28
28
$ this ->logger = TestHelpers::createLogger ();
29
- $ this ->useCurl = (bool ) getenv ('USE_CURL ' );
30
- $ this ->useTls = (string ) getenv ('BOUNCER_TLS_PATH ' );
29
+ $ this ->useCurl = (bool )getenv ('USE_CURL ' );
30
+ $ this ->useTls = (string )getenv ('BOUNCER_TLS_PATH ' );
31
31
$ this ->watcherClient = new WatcherClient (['use_curl ' => $ this ->useCurl ], $ this ->logger );
32
32
}
33
33
@@ -36,6 +36,43 @@ public function cacheAdapterConfigProvider(): array
36
36
return TestHelpers::cacheAdapterConfigProvider ();
37
37
}
38
38
39
+ private function cacheAdapterCheck ($ cacheAdapter , $ origCacheName )
40
+ {
41
+ switch ($ origCacheName ) {
42
+ case 'PhpFilesAdapter ' :
43
+ $ this ->assertEquals (
44
+ 'Symfony\Component\Cache\Adapter\TagAwareAdapter ' ,
45
+ get_class ($ cacheAdapter ),
46
+ 'Tested adapter should be correct '
47
+ );
48
+ break ;
49
+ case 'MemcachedAdapter ' :
50
+ $ this ->assertEquals (
51
+ 'CrowdSecBouncer\Fixes\Memcached\TagAwareAdapter ' ,
52
+ get_class ($ cacheAdapter ),
53
+ 'Tested adapter should be correct '
54
+ );
55
+ break ;
56
+ case 'RedisAdapter ' :
57
+ $ this ->assertEquals (
58
+ 'Symfony\Component\Cache\Adapter\RedisTagAwareAdapter ' ,
59
+ get_class ($ cacheAdapter ),
60
+ 'Tested adapter should be correct '
61
+ );
62
+ break ;
63
+ default :
64
+ break ;
65
+ }
66
+ }
67
+
68
+ private function addTlsConfig (&$ bouncerConfigs , $ tlsPath )
69
+ {
70
+ $ bouncerConfigs ['tls_cert_path ' ] = $ tlsPath . '/bouncer.pem ' ;
71
+ $ bouncerConfigs ['tls_key_path ' ] = $ tlsPath . '/bouncer-key.pem ' ;
72
+ $ bouncerConfigs ['tls_ca_cert_path ' ] = $ tlsPath . '/ca-chain.pem ' ;
73
+ $ bouncerConfigs ['tls_verify_peer ' ] = true ;
74
+ }
75
+
39
76
/**
40
77
* @group integration
41
78
* @dataProvider cacheAdapterConfigProvider
@@ -54,47 +91,19 @@ public function testCanVerifyIpInLiveModeWithCacheSystem($cacheAdapterName, $ori
54
91
'api_user_agent ' => TestHelpers::UNIT_TEST_AGENT_PREFIX . '/ ' . Constants::BASE_USER_AGENT ,
55
92
'cache_system ' => $ cacheAdapterName ,
56
93
'redis_dsn ' => getenv ('REDIS_DSN ' ),
57
- 'memcached_dsn ' => getenv ('MEMCACHED_DSN ' ),
94
+ 'memcached_dsn ' => getenv ('MEMCACHED_DSN ' ),
58
95
'fs_cache_path ' => TestHelpers::PHP_FILES_CACHE_ADAPTER_DIR
59
96
];
60
97
if ($ this ->useTls ) {
61
- $ bouncerConfigs ['tls_cert_path ' ] = $ this ->useTls . '/bouncer.pem ' ;
62
- $ bouncerConfigs ['tls_key_path ' ] = $ this ->useTls . '/bouncer-key.pem ' ;
63
- $ bouncerConfigs ['tls_ca_cert_path ' ] = $ this ->useTls . '/ca-chain.pem ' ;
64
- $ bouncerConfigs ['tls_verify_peer ' ] = true ;
98
+ $ this ->addTlsConfig ($ bouncerConfigs , $ this ->useTls );
65
99
}
66
100
67
101
$ bouncer = new Bouncer ($ bouncerConfigs , $ this ->logger );
68
102
69
103
// Test cache adapter
70
104
$ cacheAdapter = $ bouncer ->getCacheAdapter ();
71
105
$ cacheAdapter ->clear ();
72
-
73
- switch ($ origCacheName ) {
74
- case 'PhpFilesAdapter ' :
75
- $ this ->assertEquals (
76
- 'Symfony\Component\Cache\Adapter\TagAwareAdapter ' ,
77
- get_class ($ cacheAdapter ),
78
- 'Tested adapter should be correct '
79
- );
80
- break ;
81
- case 'MemcachedAdapter ' :
82
- $ this ->assertEquals (
83
- 'CrowdSecBouncer\Fixes\Memcached\TagAwareAdapter ' ,
84
- get_class ($ cacheAdapter ),
85
- 'Tested adapter should be correct '
86
- );
87
- break ;
88
- case 'RedisAdapter ' :
89
- $ this ->assertEquals (
90
- 'Symfony\Component\Cache\Adapter\RedisTagAwareAdapter ' ,
91
- get_class ($ cacheAdapter ),
92
- 'Tested adapter should be correct '
93
- );
94
- break ;
95
- default :
96
- break ;
97
- }
106
+ $ this ->cacheAdapterCheck ($ cacheAdapter , $ origCacheName );
98
107
99
108
$ this ->assertEquals (
100
109
'ban ' ,
@@ -143,8 +152,13 @@ public function testCanVerifyIpInLiveModeWithCacheSystem($cacheAdapterName, $ori
143
152
144
153
$ this ->logger ->info ('' , ['message ' => 'set "Large IPV4 range banned" state ' ]);
145
154
$ this ->watcherClient ->deleteAllDecisions ();
146
- $ this ->watcherClient ->addDecision (new \DateTime (), '24h ' , WatcherClient::HOURS24 , TestHelpers::BAD_IP . '/ '
147
- . TestHelpers::LARGE_IPV4_RANGE , 'ban ' );
155
+ $ this ->watcherClient ->addDecision (
156
+ new \DateTime (),
157
+ '24h ' ,
158
+ WatcherClient::HOURS24 ,
159
+ TestHelpers::BAD_IP . '/ ' . TestHelpers::LARGE_IPV4_RANGE ,
160
+ 'ban '
161
+ );
148
162
$ cappedRemediation = $ bouncer ->getRemediationForIp (TestHelpers::BAD_IP );
149
163
$ this ->assertEquals (
150
164
'ban ' ,
@@ -187,46 +201,18 @@ public function testCanVerifyIpInStreamModeWithCacheSystem($cacheAdapterName, $o
187
201
'use_curl ' => $ this ->useCurl ,
188
202
'cache_system ' => $ cacheAdapterName ,
189
203
'redis_dsn ' => getenv ('REDIS_DSN ' ),
190
- 'memcached_dsn ' => getenv ('MEMCACHED_DSN ' ),
204
+ 'memcached_dsn ' => getenv ('MEMCACHED_DSN ' ),
191
205
'fs_cache_path ' => TestHelpers::PHP_FILES_CACHE_ADAPTER_DIR
192
206
];
193
207
if ($ this ->useTls ) {
194
- $ bouncerConfigs ['tls_cert_path ' ] = $ this ->useTls . '/bouncer.pem ' ;
195
- $ bouncerConfigs ['tls_key_path ' ] = $ this ->useTls . '/bouncer-key.pem ' ;
196
- $ bouncerConfigs ['tls_ca_cert_path ' ] = $ this ->useTls . '/ca-chain.pem ' ;
197
- $ bouncerConfigs ['tls_verify_peer ' ] = true ;
208
+ $ this ->addTlsConfig ($ bouncerConfigs , $ this ->useTls );
198
209
}
199
210
200
211
$ bouncer = new Bouncer ($ bouncerConfigs , $ this ->logger );
201
212
// Test cache adapter
202
213
$ cacheAdapter = $ bouncer ->getCacheAdapter ();
203
214
$ cacheAdapter ->clear ();
204
-
205
- switch ($ origCacheName ) {
206
- case 'PhpFilesAdapter ' :
207
- $ this ->assertEquals (
208
- 'Symfony\Component\Cache\Adapter\TagAwareAdapter ' ,
209
- get_class ($ cacheAdapter ),
210
- 'Tested adapter should be correct '
211
- );
212
- break ;
213
- case 'MemcachedAdapter ' :
214
- $ this ->assertEquals (
215
- 'CrowdSecBouncer\Fixes\Memcached\TagAwareAdapter ' ,
216
- get_class ($ cacheAdapter ),
217
- 'Tested adapter should be correct '
218
- );
219
- break ;
220
- case 'RedisAdapter ' :
221
- $ this ->assertEquals (
222
- 'Symfony\Component\Cache\Adapter\RedisTagAwareAdapter ' ,
223
- get_class ($ cacheAdapter ),
224
- 'Tested adapter should be correct '
225
- );
226
- break ;
227
- default :
228
- break ;
229
- }
215
+ $ this ->cacheAdapterCheck ($ cacheAdapter , $ origCacheName );
230
216
// As we are in stream mode, no live call should be done to the API.
231
217
// Warm BlockList cache up
232
218
@@ -270,7 +256,6 @@ public function testCanVerifyIpInStreamModeWithCacheSystem($cacheAdapterName, $o
270
256
$ this ->logger ->debug ('' , ['message ' => 'Refresh 2nd time the cache. Nothing should append. ' ]);
271
257
$ bouncer ->refreshBlocklistCache ();
272
258
273
-
274
259
$ this ->assertEquals (
275
260
'ban ' ,
276
261
$ bouncer ->getRemediationForIp (TestHelpers::NEWLY_BAD_IP ),
@@ -293,7 +278,7 @@ public function testCanVerifyIpInStreamModeWithCacheSystem($cacheAdapterName, $o
293
278
'api_user_agent ' => TestHelpers::UNIT_TEST_AGENT_PREFIX . '/ ' . Constants::BASE_USER_AGENT ,
294
279
'cache_system ' => $ cacheAdapterName ,
295
280
'redis_dsn ' => getenv ('REDIS_DSN ' ),
296
- 'memcached_dsn ' => getenv ('MEMCACHED_DSN ' ),
281
+ 'memcached_dsn ' => getenv ('MEMCACHED_DSN ' ),
297
282
'fs_cache_path ' => TestHelpers::PHP_FILES_CACHE_ADAPTER_DIR
298
283
];
299
284
if ($ this ->useTls ) {
0 commit comments