‪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 
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->hashService = new ‪HashService();
33  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] = 'Testing';
34  }
35 
40  {
41  $hash = $this->hashService->generateHmac('asdf');
42  self::assertIsString($hash);
43  }
44 
49  {
50  $hash = $this->hashService->generateHmac('asdf');
51  self::assertNotEquals(sha1('asdf'), $hash);
52  }
53 
58  {
59  $hash1 = $this->hashService->generateHmac('asdf');
60  $hash2 = $this->hashService->generateHmac('blubb');
61  self::assertNotEquals($hash1, $hash2);
62  }
63 
67  public function ‪generatedHmacCanBeValidatedAgain(): void
68  {
69  $string = 'asdf';
70  $hash = $this->hashService->generateHmac($string);
71  self::assertTrue($this->hashService->validateHmac($string, $hash));
72  }
73 
78  {
79  $string = 'asdf';
80  $hash = 'myhash';
81  self::assertFalse($this->hashService->validateHmac($string, $hash));
82  }
83 
87  public function ‪appendHmacAppendsHmacToGivenString(): void
88  {
89  $string = 'This is some arbitrary string ';
90  $hashedString = $this->hashService->appendHmac($string);
91  self::assertSame($string, substr($hashedString, 0, -40));
92  }
93 
98  {
99  $this->expectException(InvalidArgumentForHashGenerationException::class);
100  $this->expectExceptionCode(1320830276);
101  $this->hashService->validateAndStripHmac('string with less than 40 characters');
102  }
103 
108  {
109  $this->expectException(InvalidHashException::class);
110  $this->expectExceptionCode(1320830018);
111  $this->hashService->validateAndStripHmac('string with exactly a length 40 of chars');
112  }
113 
118  {
119  $this->expectException(InvalidHashException::class);
120  $this->expectExceptionCode(1320830018);
121  $this->hashService->validateAndStripHmac('some Stringac43682075d36592d4cb320e69ff0aa515886eab');
122  }
123 
128  {
129  $string = ' Some arbitrary string with special characters: öäüß!"§$ ';
130  $hashedString = $this->hashService->appendHmac($string);
131  $actualResult = $this->hashService->validateAndStripHmac($hashedString);
132  self::assertSame($string, $actualResult);
133  }
134 }
‪TYPO3\CMS\Extbase\Tests\Unit\Security\Cryptography\HashServiceTest\validateAndStripHmacThrowsExceptionIfTheAppendedHashIsInvalid
‪validateAndStripHmacThrowsExceptionIfTheAppendedHashIsInvalid()
Definition: HashServiceTest.php:117
‪TYPO3\CMS\Extbase\Tests\Unit\Security\Cryptography\HashServiceTest\generatedHmacWillNotBeValidatedIfHashHasBeenChanged
‪generatedHmacWillNotBeValidatedIfHashHasBeenChanged()
Definition: HashServiceTest.php:77
‪TYPO3\CMS\Extbase\Tests\Unit\Security\Cryptography\HashServiceTest\validateAndStripHmacThrowsExceptionIfGivenStringIsTooShort
‪validateAndStripHmacThrowsExceptionIfGivenStringIsTooShort()
Definition: HashServiceTest.php:97
‪TYPO3\CMS\Extbase\Security\Exception\InvalidArgumentForHashGenerationException
Definition: InvalidArgumentForHashGenerationException.php:25
‪TYPO3\CMS\Extbase\Security\Cryptography\HashService
Definition: HashService.php:31
‪TYPO3\CMS\Extbase\Tests\Unit\Security\Cryptography
Definition: HashServiceTest.php:18
‪TYPO3\CMS\Extbase\Tests\Unit\Security\Cryptography\HashServiceTest\generateHmacReturnsDifferentHashStringsForDifferentInputStrings
‪generateHmacReturnsDifferentHashStringsForDifferentInputStrings()
Definition: HashServiceTest.php:57
‪TYPO3\CMS\Extbase\Tests\Unit\Security\Cryptography\HashServiceTest\generateHmacReturnsHashStringWhichContainsSomeSalt
‪generateHmacReturnsHashStringWhichContainsSomeSalt()
Definition: HashServiceTest.php:48
‪TYPO3\CMS\Extbase\Tests\Unit\Security\Cryptography\HashServiceTest\appendHmacAppendsHmacToGivenString
‪appendHmacAppendsHmacToGivenString()
Definition: HashServiceTest.php:87
‪TYPO3\CMS\Extbase\Tests\Unit\Security\Cryptography\HashServiceTest\setUp
‪setUp()
Definition: HashServiceTest.php:29
‪TYPO3\CMS\Extbase\Security\Exception\InvalidHashException
Definition: InvalidHashException.php:25
‪TYPO3\CMS\Extbase\Tests\Unit\Security\Cryptography\HashServiceTest
Definition: HashServiceTest.php:26
‪TYPO3\CMS\Extbase\Tests\Unit\Security\Cryptography\HashServiceTest\validateAndStripHmacReturnsTheStringWithoutHmac
‪validateAndStripHmacReturnsTheStringWithoutHmac()
Definition: HashServiceTest.php:127
‪TYPO3\CMS\Extbase\Tests\Unit\Security\Cryptography\HashServiceTest\generateHmacReturnsHashStringIfStringIsGiven
‪generateHmacReturnsHashStringIfStringIsGiven()
Definition: HashServiceTest.php:39
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Extbase\Tests\Unit\Security\Cryptography\HashServiceTest\$hashService
‪HashService $hashService
Definition: HashServiceTest.php:27
‪TYPO3\CMS\Extbase\Tests\Unit\Security\Cryptography\HashServiceTest\generatedHmacCanBeValidatedAgain
‪generatedHmacCanBeValidatedAgain()
Definition: HashServiceTest.php:67
‪TYPO3\CMS\Extbase\Tests\Unit\Security\Cryptography\HashServiceTest\validateAndStripHmacThrowsExceptionIfGivenStringHasNoHashAppended
‪validateAndStripHmacThrowsExceptionIfGivenStringHasNoHashAppended()
Definition: HashServiceTest.php:107