‪TYPO3CMS  ‪main
HashServiceTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use PHPUnit\Framework\Attributes\Test;
23 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
24 
25 final class ‪HashServiceTest extends UnitTestCase
26 {
28 
29  protected function ‪setUp(): void
30  {
31  parent::setUp();
32  $this->subject = new ‪HashService();
33  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] = '';
34  }
35 
36  #[Test]
38  {
39  $this->expectException(\LogicException::class);
40 
41  // @phpstan-ignore-next-line We are explicitly testing a contract violation here.
42  $this->subject->hmac('message', '');
43  }
44 
45  #[Test]
46  public function ‪hmacReturnsHashOfProperLength(): void
47  {
48  $hmac = $this->subject->hmac('message', 'additional-secret');
49  self::assertSame(strlen($hmac), 40);
50  }
51 
52  #[Test]
54  {
55  $additionalSecret = 'additional-secret';
56  $string1 = 'input';
57  $string2 = 'input';
58  self::assertSame($this->subject->hmac($string1, $additionalSecret), $this->subject->hmac($string2, $additionalSecret));
59  }
60 
61  #[Test]
63  {
64  $additionalSecret = 'additional-secret';
65  $string1 = 'input1';
66  $string2 = 'input2';
67  self::assertNotEquals($this->subject->hmac($string1, $additionalSecret), $this->subject->hmac($string2, $additionalSecret));
68  }
69 
70  #[Test]
71  public function ‪generatedHmacCanBeValidatedAgain(): void
72  {
73  $string = 'some input';
74  $additionalSecret = 'additional-secret';
75  $hash = $this->subject->hmac($string, $additionalSecret);
76  self::assertTrue($this->subject->validateHmac($string, $additionalSecret, $hash));
77  }
78 
79  #[Test]
81  {
82  $string = 'some input';
83  $additionalSecret = 'additional-secret';
84  $hash = 'myhash';
85  self::assertFalse($this->subject->validateHmac($string, $additionalSecret, $hash));
86  }
87 
88  #[Test]
89  public function ‪appendHmacAppendsHmacToGivenString(): void
90  {
91  $string = 'This is some arbitrary string ';
92  $additionalSecret = 'additional-secret';
93  $hashedString = $this->subject->appendHmac($string, $additionalSecret);
94  self::assertSame($string, substr($hashedString, 0, -40));
95  }
96 
97  #[Test]
99  {
100  $additionalSecret = 'additional-secret';
101  $this->expectException(InvalidHashStringException::class);
102  $this->expectExceptionCode(1704454152);
103  $this->subject->validateAndStripHmac('string with less than 40 characters', $additionalSecret);
104  }
105 
106  #[Test]
108  {
109  $additionalSecret = 'additional-secret';
110  $this->expectException(InvalidHashStringException::class);
111  $this->expectExceptionCode(1704454157);
112  $this->subject->validateAndStripHmac('string with exactly a length 40 of chars', $additionalSecret);
113  }
114 
115  #[Test]
117  {
118  $additionalSecret = 'additional-secret';
119  $this->expectException(InvalidHashStringException::class);
120  $this->expectExceptionCode(1704454157);
121  $this->subject->validateAndStripHmac('some Stringac43682075d36592d4cb320e69ff0aa515886eab', $additionalSecret);
122  }
123 
124  #[Test]
126  {
127  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] = 'Testing';
128  $additionalSecret = 'additional-secret';
129  $string = ' Some arbitrary string with special characters: öäüß!"§$ ';
130  $hashedString = $this->subject->appendHmac($string, $additionalSecret);
131  $actualResult = $this->subject->validateAndStripHmac($hashedString, $additionalSecret);
132  self::assertSame($string, $actualResult);
133  }
134 }
‪TYPO3\CMS\Core\Tests\Unit\Crypto\HashServiceTest\hmacThrowsExceptionIfEmptyAdditionalSecretProvided
‪hmacThrowsExceptionIfEmptyAdditionalSecretProvided()
Definition: HashServiceTest.php:37
‪TYPO3\CMS\Core\Tests\Unit\Crypto\HashServiceTest\validateAndStripHmacThrowsExceptionIfGivenStringHasNoHashAppended
‪validateAndStripHmacThrowsExceptionIfGivenStringHasNoHashAppended()
Definition: HashServiceTest.php:107
‪TYPO3\CMS\Core\Tests\Unit\Crypto\HashServiceTest\generatedHmacValidationFailsIfHashIsInvalid
‪generatedHmacValidationFailsIfHashIsInvalid()
Definition: HashServiceTest.php:80
‪TYPO3\CMS\Core\Tests\Unit\Crypto\HashServiceTest
Definition: HashServiceTest.php:26
‪TYPO3\CMS\Core\Tests\Unit\Crypto\HashServiceTest\appendHmacAppendsHmacToGivenString
‪appendHmacAppendsHmacToGivenString()
Definition: HashServiceTest.php:89
‪TYPO3\CMS\Core\Tests\Unit\Crypto\HashServiceTest\hmacReturnsHashOfProperLength
‪hmacReturnsHashOfProperLength()
Definition: HashServiceTest.php:46
‪TYPO3\CMS\Core\Tests\Unit\Crypto\HashServiceTest\setUp
‪setUp()
Definition: HashServiceTest.php:29
‪TYPO3\CMS\Core\Tests\Unit\Crypto\HashServiceTest\validateAndStripHmacThrowsExceptionIfTheAppendedHashIsInvalid
‪validateAndStripHmacThrowsExceptionIfTheAppendedHashIsInvalid()
Definition: HashServiceTest.php:116
‪TYPO3\CMS\Core\Tests\Unit\Crypto\HashServiceTest\validateAndStripHmacReturnsTheStringWithoutHmac
‪validateAndStripHmacReturnsTheStringWithoutHmac()
Definition: HashServiceTest.php:125
‪TYPO3\CMS\Core\Tests\Unit\Crypto\HashServiceTest\generatedHmacCanBeValidatedAgain
‪generatedHmacCanBeValidatedAgain()
Definition: HashServiceTest.php:71
‪TYPO3\CMS\Core\Tests\Unit\Crypto\HashServiceTest\$subject
‪HashService $subject
Definition: HashServiceTest.php:27
‪TYPO3\CMS\Core\Tests\Unit\Crypto
Definition: HashServiceTest.php:18
‪TYPO3\CMS\Core\Tests\Unit\Crypto\HashServiceTest\validateAndStripHmacThrowsExceptionIfGivenStringIsTooShort
‪validateAndStripHmacThrowsExceptionIfGivenStringIsTooShort()
Definition: HashServiceTest.php:98
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Tests\Unit\Crypto\HashServiceTest\hmacReturnsEqualHashesForEqualInput
‪hmacReturnsEqualHashesForEqualInput()
Definition: HashServiceTest.php:53
‪TYPO3\CMS\Core\Exception\Crypto\InvalidHashStringException
Definition: InvalidHashStringException.php:25
‪TYPO3\CMS\Core\Crypto\HashService
Definition: HashService.php:27
‪TYPO3\CMS\Core\Tests\Unit\Crypto\HashServiceTest\hmacReturnsNoEqualHashesForNonEqualInput
‪hmacReturnsNoEqualHashesForNonEqualInput()
Definition: HashServiceTest.php:62