Skip to content

Commit

Permalink
Moved DefaultClock to DefaultClock.php file to fix PHPUnit autoload f…
Browse files Browse the repository at this point in the history
…or tests

WE2-868

Signed-off-by: Mihkel Kivisild [email protected]
  • Loading branch information
Mihkel Kivisild committed Jul 15, 2024
1 parent 083cbec commit b2fde86
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 47 deletions.
35 changes: 0 additions & 35 deletions src/util/DateAndTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,38 +78,3 @@ public static function toUtcString(?DateTime $date): string
return ((clone $date)->setTimezone(new DateTimeZone("UTC")))->format("Y-m-d H:i:s e");
}
}

/**
* @copyright 2022 Petr Muzikant [email protected]
*/
final class DefaultClock
{
private static DefaultClock $instance;
private DateTime $mockedClock;

public static function getInstance()
{
if (!isset(self::$instance)) {
self::$instance = new DefaultClock();
}
return self::$instance;
}

public function now(): DateTime
{
if (isset($this->mockedClock)) {
return $this->mockedClock;
}
return new DateTime();
}

public function setClock(DateTime $mockedClock): void
{
$this->mockedClock = $mockedClock;
}

public function resetClock(): void
{
unset($this->mockedClock);
}
}
62 changes: 62 additions & 0 deletions src/util/DefaultClock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

/*
* Copyright (c) 2022-2024 Estonian Information System Authority
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

namespace web_eid\web_eid_authtoken_validation_php\util;

use DateTime;

/**
* @copyright 2022 Petr Muzikant [email protected]
*/
final class DefaultClock
{
private static DefaultClock $instance;
private DateTime $mockedClock;

public static function getInstance()
{
if (!isset(self::$instance)) {
self::$instance = new DefaultClock();
}
return self::$instance;
}

public function now(): DateTime
{
if (isset($this->mockedClock)) {
return $this->mockedClock;
}
return new DateTime();
}

public function setClock(DateTime $mockedClock): void
{
$this->mockedClock = $mockedClock;
}

public function resetClock(): void
{
unset($this->mockedClock);
}
}
2 changes: 0 additions & 2 deletions src/validator/ocsp/OcspClientImpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,6 @@ public function request(Uri $uri, string $encodedOcspRequest): OcspResponse
$responseJson = json_encode($response->getResponse(), JSON_INVALID_UTF8_IGNORE);
$this->logger?->debug("OCSP response: " . $responseJson);

echo $uri;

if ($info["content_type"] !== self::OCSP_RESPONSE_TYPE) {
throw new UserCertificateOCSPCheckFailedException("OCSP response content type is not " . self::OCSP_RESPONSE_TYPE);
}
Expand Down
1 change: 0 additions & 1 deletion src/validator/ocsp/OcspResponseValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@

use BadFunctionCallException;
use DateInterval;
use DateTime;
use web_eid\web_eid_authtoken_validation_php\exceptions\OCSPCertificateException;
use phpseclib3\File\X509;
use web_eid\ocsp_php\OcspBasicResponse;
Expand Down
21 changes: 12 additions & 9 deletions tests/validator/ocsp/OcspResponseValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@
use DateInterval;
use PHPUnit\Framework\TestCase;
use web_eid\ocsp_php\OcspBasicResponse;
use web_eid\web_eid_authtoken_validation_php\util\DateAndTime;
use web_eid\web_eid_authtoken_validation_php\util\DefaultClock;
use web_eid\web_eid_authtoken_validation_php\validator\AuthTokenValidationConfiguration;
use web_eid\web_eid_authtoken_validation_php\exceptions\UserCertificateOCSPCheckFailedException;
use web_eid\web_eid_authtoken_validation_php\util\DateAndTime;

class OcspResponseValidatorTest extends TestCase
{
Expand All @@ -51,7 +52,7 @@ public function testWhenThisAndNextUpdateWithinSkewThenValidationSucceeds(): voi
{
$this->expectNotToPerformAssertions();

$now = new DateTime();
$now = DefaultClock::getInstance()->now();
$thisUpdateWithinAgeLimit = self::getThisUpdateWithinAgeLimit($now);
$nextUpdateWithinAgeLimit = (clone $now)->add(new DateInterval('PT' . 2 . 'S'))->sub(new DateInterval('PT' . self::$maxThisUpdateAge . 'M'));
$response = [];
Expand All @@ -67,7 +68,7 @@ public function testWhenThisAndNextUpdateWithinSkewThenValidationSucceeds(): voi

public function testWhenNextUpdateBeforeThisUpdateThenThrows(): void
{
$now = new DateTime();
$now = DefaultClock::getInstance()->now();
$thisUpdateWithinAgeLimit = self::getThisUpdateWithinAgeLimit($now);
$beforeThisUpdate = (clone $thisUpdateWithinAgeLimit)->sub(new DateInterval('PT1S'));
$response = [];
Expand All @@ -87,8 +88,9 @@ public function testWhenNextUpdateBeforeThisUpdateThenThrows(): void
OcspResponseValidator::validateCertificateStatusUpdateTime($mockBasicResponse, self::$timeSkew, self::$maxThisUpdateAge);
}

public function testWhenThisUpdateHalfHourBeforeNowThenThrows(): void {
$now = new DateTime();
public function testWhenThisUpdateHalfHourBeforeNowThenThrows(): void
{
$now = DefaultClock::getInstance()->now();
$halfHourBeforeNow = (clone $now)->sub(new DateInterval('PT30M'));
$response = [];
$response['tbsResponseData']['responses'] = [];
Expand All @@ -106,8 +108,9 @@ public function testWhenThisUpdateHalfHourBeforeNowThenThrows(): void {
OcspResponseValidator::validateCertificateStatusUpdateTime($mockBasicResponse, self::$timeSkew, self::$maxThisUpdateAge);
}

public function testWhenThisUpdateHalfHourAfterNowThenThrows(): void {
$now = new DateTime();
public function testWhenThisUpdateHalfHourAfterNowThenThrows(): void
{
$now = DefaultClock::getInstance()->now();
$halfHourAfterNow = (clone $now)->add(new DateInterval('PT30M'));
$response = [];
$response['tbsResponseData']['responses'] = [];
Expand All @@ -127,7 +130,7 @@ public function testWhenThisUpdateHalfHourAfterNowThenThrows(): void {

public function testWhenNextUpdateHalfHourBeforeNowThenThrows(): void
{
$now = new DateTime();
$now = DefaultClock::getInstance()->now();
$thisUpdateWithinAgeLimit = self::getThisUpdateWithinAgeLimit($now);
$halfHourBeforeNow = (clone $now)->sub(new DateInterval('PT30M'));
$response = [];
Expand All @@ -149,6 +152,6 @@ public function testWhenNextUpdateHalfHourBeforeNowThenThrows(): void

private static function getThisUpdateWithinAgeLimit(DateTime $now): DateTime
{
return (clone $now)->add(new DateInterval('PT' . 1 . 'S'))->sub(new DateInterval('PT' . self::$maxThisUpdateAge . 'M'));
return (clone $now)->add(new DateInterval('PT' . 1 . 'S'))->sub(new DateInterval('PT' . self::$maxThisUpdateAge . 'M'));
}
}

0 comments on commit b2fde86

Please sign in to comment.