1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SimpleSAML\Module\monitor\TestSuite; |
4
|
|
|
|
5
|
|
|
use \SimpleSAML\Module\monitor\TestConfiguration as TestConfiguration; |
6
|
|
|
use \SimpleSAML\Module\monitor\State as State; |
7
|
|
|
use \SimpleSAML\Module\monitor\TestCase as TestCase; |
8
|
|
|
use \SimpleSAML\Module\monitor\TestData as TestData; |
9
|
|
|
use \SimpleSAML\Logger as Logger; |
10
|
|
|
|
11
|
|
|
final class Store extends \SimpleSAML\Module\monitor\TestSuiteFactory |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* var string|null |
15
|
|
|
*/ |
16
|
|
|
private $store = null; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @param TestConfiguration $configuration |
20
|
|
|
*/ |
21
|
|
|
public function __construct($configuration) |
22
|
|
|
{ |
23
|
|
|
$globalConfig = $configuration->getGlobalConfig(); |
24
|
|
|
$this->store = $globalConfig->getString('store.type', 'phpsession'); |
25
|
|
|
$this->setCategory('Session store'); |
26
|
|
|
|
27
|
|
|
parent::__construct($configuration); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @return void |
32
|
|
|
*/ |
33
|
|
|
public function invokeTest() |
34
|
|
|
{ |
35
|
|
|
$configuration = $this->getConfiguration(); |
36
|
|
|
|
37
|
|
|
if ($this->store === 'phpsession') { |
38
|
|
|
$results = $this->testPhpSession(); |
39
|
|
|
} else { |
40
|
|
|
$results = $this->testSspSession($configuration); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
foreach ($results as $result) { |
44
|
|
|
$this->addTestResult($result); |
45
|
|
|
} |
46
|
|
|
$this->calculateState(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return array |
51
|
|
|
*/ |
52
|
|
|
private function testSspSession($configuration) |
53
|
|
|
{ |
54
|
|
|
$results = array(); |
55
|
|
|
|
56
|
|
|
switch ($this->store) { |
57
|
|
|
case 'memcache': |
58
|
|
|
$test = new Store\Memcache($configuration); |
59
|
|
|
$results = $test->getTestResults(); |
60
|
|
|
break; |
61
|
|
|
// case 'redis': |
|
|
|
|
62
|
|
|
// case 'redissentinel': |
63
|
|
|
// $test = new Store\Redis($configuration); |
64
|
|
|
// break; |
65
|
|
|
case 'sql': |
66
|
|
|
$test = new Store\Sql($configuration); |
67
|
|
|
$results = $test->getTestResults(); |
68
|
|
|
break; |
69
|
|
|
default: |
70
|
|
|
Logger::warning("Not implemented; $this->store - Skipping Store TestSuite."); |
71
|
|
|
break; |
72
|
|
|
} |
73
|
|
|
return $results; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return array |
78
|
|
|
*/ |
79
|
|
|
private function testPhpSession() |
80
|
|
|
{ |
81
|
|
|
$results = array(); |
82
|
|
|
switch (ini_get('session.save_handler')) { |
83
|
|
|
case 'files': |
84
|
|
|
$input = [ |
85
|
|
|
'path' => session_save_path(), |
86
|
|
|
'category' => 'Session storage' |
87
|
|
|
]; |
88
|
|
|
$testData = new TestData($input); |
89
|
|
|
$test = new TestCase\FileSystem\FreeSpace($testData); |
90
|
|
|
$results[] = $test->getTestResult(); |
91
|
|
|
break; |
92
|
|
|
case 'memcache': |
93
|
|
|
case 'memcached': |
94
|
|
|
$configuration = \SimpleSAML_Configuration::setPreLoadedConfig( |
|
|
|
|
95
|
|
|
\SimpleSAML_Configuration::loadFromArray( |
96
|
|
|
array( |
97
|
|
|
'memcache_store.servers' => $this->parsePhpMemcachedConfiguration(session_save_path()) |
98
|
|
|
) |
99
|
|
|
) |
100
|
|
|
); |
101
|
|
|
|
102
|
|
|
$test = new Store\Memcache($configuration); |
103
|
|
|
$results = $test->getTestResults(); |
104
|
|
|
break; |
105
|
|
|
// case 'sqlite': |
|
|
|
|
106
|
|
|
// case 'mm': |
107
|
|
|
default: |
108
|
|
|
Logger::warning("Not implemented; $this->store - Skipping Store TestSuite."); |
109
|
|
|
break; |
110
|
|
|
} |
111
|
|
|
return $results; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param string $spec |
116
|
|
|
* |
117
|
|
|
* @return array |
118
|
|
|
*/ |
119
|
|
|
private function parsePhpMemcachedConfiguration($spec) |
120
|
|
|
{ |
121
|
|
|
$servers = preg_split('/\s*,\s*/', $spec); |
122
|
|
|
|
123
|
|
|
$results = array(); |
124
|
|
|
foreach ($servers as $server) { |
125
|
|
|
$result = array(); |
126
|
|
|
list($host, $params) = explode('?', $server); |
127
|
|
|
list($hostname, $port) = explode(':', $host); |
128
|
|
|
|
129
|
|
|
// Strip protocol when possible (memcache) |
130
|
|
|
$prefix = 'tcp://'; |
131
|
|
|
if (substr($hostname, 0, 6) === $prefix) { |
132
|
|
|
$hostname = substr($hostname, 6); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
$result['hostname'] = $hostname; |
136
|
|
|
if ($port !== null) { |
137
|
|
|
$result['port'] = $port; |
138
|
|
|
} |
139
|
|
|
parse_str($params, $tmp); |
140
|
|
|
$results[] = array_merge($result, $tmp); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return array($results); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.