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 factor_token;
|
|
|
18 |
|
|
|
19 |
/**
|
|
|
20 |
* Tests for MFA manager class.
|
|
|
21 |
*
|
|
|
22 |
* @package factor_token
|
|
|
23 |
* @author Peter Burnett <peterburnett@catalyst-au.net>
|
|
|
24 |
* @author Kevin Pham <kevinpham@catalyst-au.net>
|
|
|
25 |
* @copyright Catalyst IT
|
|
|
26 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
27 |
*/
|
|
|
28 |
class factor_test extends \advanced_testcase {
|
|
|
29 |
|
|
|
30 |
/**
|
|
|
31 |
* Holds specific requested factor, which is token factor.
|
|
|
32 |
*
|
|
|
33 |
* @var \factor_token\factor $factor
|
|
|
34 |
*/
|
|
|
35 |
public \factor_token\factor $factor;
|
|
|
36 |
|
|
|
37 |
public function setUp(): void {
|
|
|
38 |
$this->resetAfterTest();
|
|
|
39 |
$this->factor = new \factor_token\factor('token');
|
|
|
40 |
}
|
|
|
41 |
|
|
|
42 |
/**
|
|
|
43 |
* Test calculating expiry time in general
|
|
|
44 |
*
|
|
|
45 |
* @covers ::calculate_expiry_time
|
|
|
46 |
* @return void
|
|
|
47 |
*/
|
11 |
efrain |
48 |
public function test_calculate_expiry_time_in_general(): void {
|
1 |
efrain |
49 |
$timestamp = 1642213800; // 1230 UTC.
|
|
|
50 |
|
|
|
51 |
set_config('expireovernight', 0, 'factor_token');
|
|
|
52 |
$method = new \ReflectionMethod($this->factor, 'calculate_expiry_time');
|
|
|
53 |
|
|
|
54 |
// Test that non-overnight timestamps are just exactly as configured.
|
|
|
55 |
// We don't need to care about 0 or negative ints, they will just make the cookie expire immediately.
|
|
|
56 |
$expiry = $method->invoke($this->factor, $timestamp);
|
|
|
57 |
$this->assertEquals(DAYSECS, $expiry[1]);
|
|
|
58 |
|
|
|
59 |
set_config('expiry', HOURSECS, 'factor_token');
|
|
|
60 |
$expiry = $method->invoke($this->factor, $timestamp);
|
|
|
61 |
$this->assertGreaterThan(HOURSECS - 30, $expiry[1]);
|
|
|
62 |
$this->assertLessThan(HOURSECS + 30, $expiry[1]);
|
|
|
63 |
|
|
|
64 |
set_config('expireovernight', 1, 'factor_token');
|
|
|
65 |
// Manually calculate the next reset time.
|
|
|
66 |
$reset = strtotime('tomorrow 0200', $timestamp);
|
|
|
67 |
$resetdelta = $reset - $timestamp;
|
|
|
68 |
// Confirm that a timestamp that doesnt reach reset time.
|
|
|
69 |
if ($timestamp + HOURSECS < $reset) {
|
|
|
70 |
$expiry = $method->invoke($this->factor, $timestamp);
|
|
|
71 |
$this->assertGreaterThan(HOURSECS - 30, $expiry[1]);
|
|
|
72 |
$this->assertLessThan(HOURSECS + 30, $expiry[1]);
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
set_config('expiry', 2 * DAYSECS, 'factor_token');
|
|
|
76 |
// Now confirm that the returned expiry is less than the absolute amount.
|
|
|
77 |
$expiry = $method->invoke($this->factor, $timestamp);
|
|
|
78 |
$this->assertGreaterThan(DAYSECS, $expiry[1]);
|
|
|
79 |
$this->assertLessThan(2 * DAYSECS, $expiry[1]);
|
|
|
80 |
$this->assertGreaterThan($resetdelta + DAYSECS - 30, $expiry[1]);
|
|
|
81 |
$this->assertLessThan($resetdelta + DAYSECS + 30, $expiry[1]);
|
|
|
82 |
}
|
|
|
83 |
|
|
|
84 |
/**
|
|
|
85 |
* Everything should end at 2am unless adding the hours lands it between
|
|
|
86 |
* 0 <= x < 2am, which in that case it should just expire using the raw
|
|
|
87 |
* value, provided it never goes past raw value expiry time, and when it
|
|
|
88 |
* needs to be 2am, it's 2am on the following morning.
|
|
|
89 |
*
|
|
|
90 |
* @covers ::calculate_expiry_time
|
|
|
91 |
* @param int $timestamp
|
|
|
92 |
* @dataProvider timestamp_provider
|
|
|
93 |
*/
|
11 |
efrain |
94 |
public function test_calculate_expiry_time_for_overnight_expiry_with_one_day_expiry($timestamp): void {
|
1 |
efrain |
95 |
// Setup configuration.
|
|
|
96 |
$method = new \ReflectionMethod($this->factor, 'calculate_expiry_time');
|
|
|
97 |
set_config('expireovernight', 1, 'factor_token');
|
|
|
98 |
set_config('expiry', DAYSECS, 'factor_token');
|
|
|
99 |
|
|
|
100 |
// All the results here, should be for 2am the following morning from the timestamp provided.
|
|
|
101 |
$expiry = $method->invoke($this->factor, $timestamp);
|
|
|
102 |
list($expiresat, $secondstillexpiry) = $expiry;
|
|
|
103 |
|
|
|
104 |
// Calculate the expected raw expiry if not considering 'overnight'.
|
|
|
105 |
$timezone = \core_date::get_user_timezone_object();
|
|
|
106 |
$datetime = new \DateTime();
|
|
|
107 |
$datetime->setTimezone($timezone);
|
|
|
108 |
|
|
|
109 |
$rawexpiry = $timestamp + DAYSECS;
|
|
|
110 |
$datetime->setTimestamp($rawexpiry);
|
|
|
111 |
$rawhour = $datetime->format('H');
|
|
|
112 |
$rawminute = $datetime->format('m');
|
|
|
113 |
|
|
|
114 |
// Sanity check, that the $secondstillexpiry is in the appropriate ranges.
|
|
|
115 |
$this->assertGreaterThan(0, $secondstillexpiry);
|
|
|
116 |
$this->assertLessThan(DAYSECS + 1, $secondstillexpiry);
|
|
|
117 |
|
|
|
118 |
if ($rawhour >= 0 && $rawhour < 2 || $rawhour == 2 && $rawminute == 0) {
|
|
|
119 |
// Should just use expiry time, if the hours will land between 0 and 2am.
|
|
|
120 |
$this->assertEquals($datetime->getTimestamp(), $expiresat);
|
|
|
121 |
// Ensure the $secondstillexpiry is calculated correctly.
|
|
|
122 |
$this->assertEquals($expiresat - $timestamp, $secondstillexpiry);
|
|
|
123 |
} else {
|
|
|
124 |
// Otherwise it should fall on 2am the following day.
|
|
|
125 |
$followingdayattwoam = strtotime('tomorrow 0200', $timestamp);
|
|
|
126 |
$this->assertEquals($followingdayattwoam, $expiresat);
|
|
|
127 |
// Ensure the $secondstillexpiry is calculated correctly.
|
|
|
128 |
$this->assertEquals($followingdayattwoam - $timestamp, $secondstillexpiry);
|
|
|
129 |
}
|
|
|
130 |
}
|
|
|
131 |
|
|
|
132 |
/**
|
|
|
133 |
* Everything should end at 2am unless adding the hours lands it between
|
|
|
134 |
* 0 <= x < 2am, which in that case it should just expire using the raw
|
|
|
135 |
* value, provided it never goes past raw value expiry time, and when it
|
|
|
136 |
* needs to be 2am, it's 2am on the morning after tomorrow.
|
|
|
137 |
*
|
|
|
138 |
* @covers ::calculate_expiry_time
|
|
|
139 |
* @param int $timestamp
|
|
|
140 |
* @dataProvider timestamp_provider
|
|
|
141 |
*/
|
11 |
efrain |
142 |
public function test_calculate_expiry_time_for_overnight_expiry_with_two_day_expiry($timestamp): void {
|
1 |
efrain |
143 |
// Setup configuration.
|
|
|
144 |
$method = new \ReflectionMethod($this->factor, 'calculate_expiry_time');
|
|
|
145 |
set_config('expireovernight', 1, 'factor_token');
|
|
|
146 |
set_config('expiry', 2 * DAYSECS, 'factor_token');
|
|
|
147 |
|
|
|
148 |
// All the results here, should be for 2am the following morning from the timestamp provided.
|
|
|
149 |
$expiry = $method->invoke($this->factor, $timestamp);
|
|
|
150 |
list($expiresat, $secondstillexpiry) = $expiry;
|
|
|
151 |
|
|
|
152 |
// Calculate the expected raw expiry if not considering 'overnight'.
|
|
|
153 |
$timezone = \core_date::get_user_timezone_object();
|
|
|
154 |
$datetime = new \DateTime();
|
|
|
155 |
$datetime->setTimezone($timezone);
|
|
|
156 |
|
|
|
157 |
$rawexpiry = $timestamp + (2 * DAYSECS);
|
|
|
158 |
$datetime->setTimestamp($rawexpiry);
|
|
|
159 |
$rawhour = $datetime->format('H');
|
|
|
160 |
$rawminute = $datetime->format('m');
|
|
|
161 |
|
|
|
162 |
// Sanity check, that the $secondstillexpiry is in the appropriate ranges.
|
|
|
163 |
$this->assertGreaterThan(0, $secondstillexpiry);
|
|
|
164 |
$this->assertLessThan((2 * DAYSECS) + 1, $secondstillexpiry);
|
|
|
165 |
|
|
|
166 |
if ($rawhour >= 0 && $rawhour < 2 || $rawhour == 2 && $rawminute == 0) {
|
|
|
167 |
// Should just use expiry time, if the hours will land between 0 and 2am.
|
|
|
168 |
$this->assertEquals($datetime->getTimestamp(), $expiresat);
|
|
|
169 |
// Ensure the $secondstillexpiry is calculated correctly.
|
|
|
170 |
$this->assertEquals($expiresat - $timestamp, $secondstillexpiry);
|
|
|
171 |
} else {
|
|
|
172 |
// Otherwise it should fall on 2am the following day after tomorrow.
|
|
|
173 |
$followingdayattwoam = strtotime('tomorrow 0200', $timestamp) + DAYSECS;
|
|
|
174 |
$this->assertEquals($followingdayattwoam, $expiresat);
|
|
|
175 |
// Ensure the $secondstillexpiry is calculated correctly.
|
|
|
176 |
$this->assertEquals($followingdayattwoam - $timestamp, $secondstillexpiry);
|
|
|
177 |
}
|
|
|
178 |
|
|
|
179 |
// Expiry should always be more than one day for an expiry duration of
|
|
|
180 |
// more than 1 day, but the overnight check should apply for the
|
|
|
181 |
// duration of the final night.
|
|
|
182 |
$this->assertGreaterThan(DAYSECS, $secondstillexpiry);
|
|
|
183 |
}
|
|
|
184 |
|
|
|
185 |
/**
|
|
|
186 |
* This should check if the 3am expiry is pushed back to 2am as expected, but everything else appears as expected
|
|
|
187 |
*
|
|
|
188 |
* @covers ::calculate_expiry_time
|
|
|
189 |
* @param int $timestamp
|
|
|
190 |
* @dataProvider timestamp_provider
|
|
|
191 |
*/
|
11 |
efrain |
192 |
public function test_calculate_expiry_time_for_overnight_expiry_with_three_hour_expiry($timestamp): void {
|
1 |
efrain |
193 |
// Setup configuration.
|
|
|
194 |
$method = new \ReflectionMethod($this->factor, 'calculate_expiry_time');
|
|
|
195 |
set_config('expireovernight', 1, 'factor_token');
|
|
|
196 |
set_config('expiry', 3 * HOURSECS, 'factor_token');
|
|
|
197 |
|
|
|
198 |
// All the results here, should be for 2am the following morning from the timestamp provided.
|
|
|
199 |
$expiry = $method->invoke($this->factor, $timestamp);
|
|
|
200 |
list($expiresat, $secondstillexpiry) = $expiry;
|
|
|
201 |
|
|
|
202 |
// Calculate the expected raw expiry if not considering 'overnight'.
|
|
|
203 |
$timezone = \core_date::get_user_timezone_object();
|
|
|
204 |
$datetime = new \DateTime();
|
|
|
205 |
$datetime->setTimezone($timezone);
|
|
|
206 |
|
|
|
207 |
$rawexpiry = $timestamp + (3 * HOURSECS);
|
|
|
208 |
$datetime->setTimestamp($rawexpiry);
|
|
|
209 |
|
|
|
210 |
// Sanity check, that the $secondstillexpiry is in the appropriate ranges.
|
|
|
211 |
$this->assertGreaterThan(0, $secondstillexpiry);
|
|
|
212 |
$this->assertLessThan((3 * HOURSECS) + 1, $secondstillexpiry);
|
|
|
213 |
|
|
|
214 |
// If the raw timestamp of the expiry, is less than tomorrow at 2am,
|
|
|
215 |
// then use the raw expiry time.
|
|
|
216 |
$followingdayattwoam = strtotime('tomorrow 0200', $timestamp);
|
|
|
217 |
if ($datetime->getTimestamp() < $followingdayattwoam) {
|
|
|
218 |
$this->assertEquals($datetime->getTimestamp(), $expiresat);
|
|
|
219 |
// Ensure the $secondstillexpiry is calculated correctly.
|
|
|
220 |
$this->assertEquals($expiresat - $timestamp, $secondstillexpiry);
|
|
|
221 |
} else {
|
|
|
222 |
// Otherwsie it should be pushed back to 2am.
|
|
|
223 |
$this->assertEquals($followingdayattwoam, $expiresat);
|
|
|
224 |
// Ensure the $secondstillexpiry is calculated correctly.
|
|
|
225 |
$this->assertEquals($followingdayattwoam - $timestamp, $secondstillexpiry);
|
|
|
226 |
}
|
|
|
227 |
}
|
|
|
228 |
|
|
|
229 |
/**
|
|
|
230 |
* Only relevant based on the hour padding used, which is currently set to 2 hours (2am).
|
|
|
231 |
*
|
|
|
232 |
* @covers ::calculate_expiry_time
|
|
|
233 |
* @param int $timestamp
|
|
|
234 |
* @dataProvider timestamp_provider
|
|
|
235 |
*/
|
11 |
efrain |
236 |
public function test_calculate_expiry_time_for_overnight_expiry_with_an_hour_expiry($timestamp): void {
|
1 |
efrain |
237 |
// Setup configuration.
|
|
|
238 |
$method = new \ReflectionMethod($this->factor, 'calculate_expiry_time');
|
|
|
239 |
set_config('expireovernight', 1, 'factor_token');
|
|
|
240 |
set_config('expiry', HOURSECS, 'factor_token');
|
|
|
241 |
|
|
|
242 |
// All the results here, should be for 2am the following morning from the timestamp provided.
|
|
|
243 |
$expiry = $method->invoke($this->factor, $timestamp);
|
|
|
244 |
list($expiresat, $secondstillexpiry) = $expiry;
|
|
|
245 |
|
|
|
246 |
// Calculate the expected raw expiry if not considering 'overnight'.
|
|
|
247 |
$timezone = \core_date::get_user_timezone_object();
|
|
|
248 |
$datetime = new \DateTime();
|
|
|
249 |
$datetime->setTimezone($timezone);
|
|
|
250 |
|
|
|
251 |
$rawexpiry = $timestamp + HOURSECS;
|
|
|
252 |
$datetime->setTimestamp($rawexpiry);
|
|
|
253 |
|
|
|
254 |
// Sanity check, that the $secondstillexpiry is in the appropriate ranges.
|
|
|
255 |
$this->assertGreaterThan(0, $secondstillexpiry);
|
|
|
256 |
$this->assertLessThan(HOURSECS + 1, $secondstillexpiry);
|
|
|
257 |
|
|
|
258 |
// If the raw timestamp of the expiry, is less than tomorrow at 2am,
|
|
|
259 |
// then use the raw expiry time.
|
|
|
260 |
$followingdayattwoam = strtotime('tomorrow 0200', $timestamp);
|
|
|
261 |
if ($datetime->getTimestamp() < $followingdayattwoam) {
|
|
|
262 |
$this->assertEquals($datetime->getTimestamp(), $expiresat);
|
|
|
263 |
// Ensure the $secondstillexpiry is calculated correctly.
|
|
|
264 |
$this->assertEquals($expiresat - $timestamp, $secondstillexpiry);
|
|
|
265 |
} else {
|
|
|
266 |
// Otherwsie it should be pushed back to 2am.
|
|
|
267 |
$this->assertEquals($followingdayattwoam, $expiresat);
|
|
|
268 |
// Ensure the $secondstillexpiry is calculated correctly.
|
|
|
269 |
$this->assertEquals($followingdayattwoam - $timestamp, $secondstillexpiry);
|
|
|
270 |
}
|
|
|
271 |
}
|
|
|
272 |
|
|
|
273 |
/**
|
|
|
274 |
* Timestamps for a 24 hour period starting from a fixed time.
|
|
|
275 |
* Increments by 30 minutes to cover half hour and hour cases.
|
|
|
276 |
* Starting timestamp: 2022-01-15 07:30:00 Australia/Melbourne time.
|
|
|
277 |
*/
|
|
|
278 |
public function timestamp_provider() {
|
|
|
279 |
$starttimestamp = 1642192200;
|
|
|
280 |
foreach (range(0, 23) as $i) {
|
|
|
281 |
$timestamps[] = [$starttimestamp + ($i * HOURSECS)];
|
|
|
282 |
$timestamps[] = [$starttimestamp + ($i * HOURSECS) + (30 * MINSECS)];
|
|
|
283 |
}
|
|
|
284 |
return $timestamps;
|
|
|
285 |
}
|
|
|
286 |
}
|