| 1 |
efrain |
1 |
<?php
|
|
|
2 |
// This file is part of Moodle - http://moodle.org/
|
|
|
3 |
//
|
|
|
4 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
5 |
// it under the terms of the GNU General Public License as published by
|
|
|
6 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
7 |
// (at your option) any later version.
|
|
|
8 |
//
|
|
|
9 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
12 |
// GNU General Public License for more details.
|
|
|
13 |
//
|
|
|
14 |
// You should have received a copy of the GNU General Public License
|
|
|
15 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
16 |
|
|
|
17 |
namespace core;
|
|
|
18 |
|
|
|
19 |
/**
|
|
|
20 |
* Unit tests for sessionlib.php file.
|
|
|
21 |
*
|
|
|
22 |
* @package core
|
|
|
23 |
* @category test
|
|
|
24 |
* @author Petr Skoda <petr.skoda@totaralms.com>
|
|
|
25 |
* @copyright 2014 Totara Learning Solutions Ltd {@link http://www.totaralms.com/}
|
|
|
26 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
27 |
*/
|
| 1441 |
ariadna |
28 |
final class sessionlib_test extends \advanced_testcase {
|
| 1 |
efrain |
29 |
|
|
|
30 |
/**
|
|
|
31 |
* Test provided for secure cookie
|
|
|
32 |
*
|
|
|
33 |
* @return array of config and secure result
|
|
|
34 |
*/
|
| 1441 |
ariadna |
35 |
public static function moodle_cookie_secure_provider(): array {
|
| 1 |
efrain |
36 |
return array(
|
|
|
37 |
array(
|
|
|
38 |
// Non ssl, not set.
|
|
|
39 |
'config' => array(
|
|
|
40 |
'wwwroot' => 'http://example.com',
|
|
|
41 |
'sslproxy' => null,
|
|
|
42 |
'cookiesecure' => null,
|
|
|
43 |
),
|
|
|
44 |
'secure' => false,
|
|
|
45 |
),
|
|
|
46 |
array(
|
|
|
47 |
// Non ssl, off and ignored.
|
|
|
48 |
'config' => array(
|
|
|
49 |
'wwwroot' => 'http://example.com',
|
|
|
50 |
'sslproxy' => null,
|
|
|
51 |
'cookiesecure' => false,
|
|
|
52 |
),
|
|
|
53 |
'secure' => false,
|
|
|
54 |
),
|
|
|
55 |
array(
|
|
|
56 |
// Non ssl, on and ignored.
|
|
|
57 |
'config' => array(
|
|
|
58 |
'wwwroot' => 'http://example.com',
|
|
|
59 |
'sslproxy' => null,
|
|
|
60 |
'cookiesecure' => true,
|
|
|
61 |
),
|
|
|
62 |
'secure' => false,
|
|
|
63 |
),
|
|
|
64 |
array(
|
|
|
65 |
// SSL via proxy, off.
|
|
|
66 |
'config' => array(
|
|
|
67 |
'wwwroot' => 'http://example.com',
|
|
|
68 |
'sslproxy' => true,
|
|
|
69 |
'cookiesecure' => false,
|
|
|
70 |
),
|
|
|
71 |
'secure' => false,
|
|
|
72 |
),
|
|
|
73 |
array(
|
|
|
74 |
// SSL via proxy, on.
|
|
|
75 |
'config' => array(
|
|
|
76 |
'wwwroot' => 'http://example.com',
|
|
|
77 |
'sslproxy' => true,
|
|
|
78 |
'cookiesecure' => true,
|
|
|
79 |
),
|
|
|
80 |
'secure' => true,
|
|
|
81 |
),
|
|
|
82 |
array(
|
|
|
83 |
// SSL and off.
|
|
|
84 |
'config' => array(
|
|
|
85 |
'wwwroot' => 'https://example.com',
|
|
|
86 |
'sslproxy' => null,
|
|
|
87 |
'cookiesecure' => false,
|
|
|
88 |
),
|
|
|
89 |
'secure' => false,
|
|
|
90 |
),
|
|
|
91 |
array(
|
|
|
92 |
// SSL and on.
|
|
|
93 |
'config' => array(
|
|
|
94 |
'wwwroot' => 'https://example.com',
|
|
|
95 |
'sslproxy' => null,
|
|
|
96 |
'cookiesecure' => true,
|
|
|
97 |
),
|
|
|
98 |
'secure' => true,
|
|
|
99 |
),
|
|
|
100 |
);
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
/**
|
|
|
104 |
* Test for secure cookie
|
|
|
105 |
*
|
|
|
106 |
* @dataProvider moodle_cookie_secure_provider
|
|
|
107 |
*
|
|
|
108 |
* @param array $config Array of key value config settings
|
|
|
109 |
* @param bool $secure Wether cookies should be secure or not
|
|
|
110 |
*/
|
| 11 |
efrain |
111 |
public function test_is_moodle_cookie_secure($config, $secure): void {
|
| 1 |
efrain |
112 |
global $CFG;
|
|
|
113 |
$this->resetAfterTest();
|
|
|
114 |
foreach ($config as $key => $value) {
|
|
|
115 |
$CFG->$key = $value;
|
|
|
116 |
}
|
|
|
117 |
$this->assertEquals($secure, is_moodle_cookie_secure());
|
|
|
118 |
}
|
|
|
119 |
|
| 11 |
efrain |
120 |
public function test_sesskey(): void {
|
| 1 |
efrain |
121 |
global $USER;
|
|
|
122 |
$this->resetAfterTest();
|
|
|
123 |
|
|
|
124 |
$user = $this->getDataGenerator()->create_user();
|
|
|
125 |
|
|
|
126 |
\core\session\manager::init_empty_session();
|
|
|
127 |
$this->assertObjectNotHasProperty('sesskey', $USER);
|
|
|
128 |
|
|
|
129 |
$sesskey = sesskey();
|
|
|
130 |
$this->assertNotEmpty($sesskey);
|
|
|
131 |
$this->assertSame($sesskey, $USER->sesskey);
|
|
|
132 |
$this->assertSame($GLOBALS['USER'], $_SESSION['USER']);
|
|
|
133 |
$this->assertSame($GLOBALS['USER'], $USER);
|
|
|
134 |
|
|
|
135 |
$this->assertSame($sesskey, sesskey());
|
|
|
136 |
|
|
|
137 |
// Test incomplete session init - the sesskeys should return random values.
|
|
|
138 |
$_SESSION = array();
|
|
|
139 |
unset($GLOBALS['USER']);
|
|
|
140 |
unset($GLOBALS['SESSION']);
|
|
|
141 |
|
|
|
142 |
$this->assertFalse(sesskey());
|
|
|
143 |
$this->assertArrayNotHasKey('USER', $GLOBALS);
|
|
|
144 |
$this->assertFalse(sesskey());
|
|
|
145 |
}
|
|
|
146 |
|
| 11 |
efrain |
147 |
public function test_confirm_sesskey(): void {
|
| 1 |
efrain |
148 |
$this->resetAfterTest();
|
|
|
149 |
|
|
|
150 |
$sesskey = sesskey();
|
|
|
151 |
|
|
|
152 |
try {
|
|
|
153 |
confirm_sesskey();
|
|
|
154 |
$this->fail('Exception expected when sesskey not present');
|
|
|
155 |
} catch (\moodle_exception $e) {
|
|
|
156 |
$this->assertSame('missingparam', $e->errorcode);
|
|
|
157 |
}
|
|
|
158 |
|
|
|
159 |
$this->assertTrue(confirm_sesskey($sesskey));
|
|
|
160 |
$this->assertFalse(confirm_sesskey('blahblah'));
|
|
|
161 |
|
|
|
162 |
$_GET['sesskey'] = $sesskey;
|
|
|
163 |
$this->assertTrue(confirm_sesskey());
|
|
|
164 |
|
|
|
165 |
$_GET['sesskey'] = 'blah';
|
|
|
166 |
$this->assertFalse(confirm_sesskey());
|
|
|
167 |
}
|
|
|
168 |
|
| 11 |
efrain |
169 |
public function test_require_sesskey(): void {
|
| 1 |
efrain |
170 |
$this->resetAfterTest();
|
|
|
171 |
|
|
|
172 |
$sesskey = sesskey();
|
|
|
173 |
|
|
|
174 |
try {
|
|
|
175 |
require_sesskey();
|
|
|
176 |
$this->fail('Exception expected when sesskey not present');
|
|
|
177 |
} catch (\moodle_exception $e) {
|
|
|
178 |
$this->assertSame('missingparam', $e->errorcode);
|
|
|
179 |
}
|
|
|
180 |
|
|
|
181 |
$_GET['sesskey'] = $sesskey;
|
|
|
182 |
require_sesskey();
|
|
|
183 |
|
|
|
184 |
$_GET['sesskey'] = 'blah';
|
|
|
185 |
try {
|
|
|
186 |
require_sesskey();
|
|
|
187 |
$this->fail('Exception expected when sesskey not incorrect');
|
|
|
188 |
} catch (\moodle_exception $e) {
|
|
|
189 |
$this->assertSame('invalidsesskey', $e->errorcode);
|
|
|
190 |
}
|
|
|
191 |
}
|
|
|
192 |
}
|