‪TYPO3CMS  11.5
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 
28 class ‪HashServiceTest extends UnitTestCase
29 {
31 
32  protected function ‪setUp(): void
33  {
34  parent::setUp();
35  $this->hashService = new ‪HashService();
36  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] = 'Testing';
37  }
38 
43  {
44  $hash = $this->hashService->generateHmac('asdf');
45  self::assertIsString($hash);
46  }
47 
52  {
53  $hash = $this->hashService->generateHmac('asdf');
54  self::assertNotEquals(sha1('asdf'), $hash);
55  }
56 
61  {
62  $hash1 = $this->hashService->generateHmac('asdf');
63  $hash2 = $this->hashService->generateHmac('blubb');
64  self::assertNotEquals($hash1, $hash2);
65  }
66 
70  public function ‪generatedHmacCanBeValidatedAgain(): void
71  {
72  $string = 'asdf';
73  $hash = $this->hashService->generateHmac($string);
74  self::assertTrue($this->hashService->validateHmac($string, $hash));
75  }
76 
81  {
82  $string = 'asdf';
83  $hash = 'myhash';
84  self::assertFalse($this->hashService->validateHmac($string, $hash));
85  }
86 
90  public function ‪appendHmacAppendsHmacToGivenString(): void
91  {
92  $string = 'This is some arbitrary string ';
93  $hashedString = $this->hashService->appendHmac($string);
94  self::assertSame($string, substr($hashedString, 0, -40));
95  }
96 
101  {
102  $this->expectException(InvalidArgumentForHashGenerationException::class);
103  $this->expectExceptionCode(1320830276);
104  $this->hashService->validateAndStripHmac('string with less than 40 characters');
105  }
106 
111  {
112  $this->expectException(InvalidHashException::class);
113  $this->expectExceptionCode(1320830018);
114  $this->hashService->validateAndStripHmac('string with exactly a length 40 of chars');
115  }
116 
121  {
122  $this->expectException(InvalidHashException::class);
123  $this->expectExceptionCode(1320830018);
124  $this->hashService->validateAndStripHmac('some Stringac43682075d36592d4cb320e69ff0aa515886eab');
125  }
126 
131  {
132  $string = ' Some arbitrary string with special characters: öäüß!"§$ ';
133  $hashedString = $this->hashService->appendHmac($string);
134  $actualResult = $this->hashService->validateAndStripHmac($hashedString);
135  self::assertSame($string, $actualResult);
136  }
137 }
‪TYPO3\CMS\Extbase\Tests\Unit\Security\Cryptography\HashServiceTest\validateAndStripHmacThrowsExceptionIfTheAppendedHashIsInvalid
‪validateAndStripHmacThrowsExceptionIfTheAppendedHashIsInvalid()
Definition: HashServiceTest.php:120
‪TYPO3\CMS\Extbase\Tests\Unit\Security\Cryptography\HashServiceTest\generatedHmacWillNotBeValidatedIfHashHasBeenChanged
‪generatedHmacWillNotBeValidatedIfHashHasBeenChanged()
Definition: HashServiceTest.php:80
‪TYPO3\CMS\Extbase\Tests\Unit\Security\Cryptography\HashServiceTest\validateAndStripHmacThrowsExceptionIfGivenStringIsTooShort
‪validateAndStripHmacThrowsExceptionIfGivenStringIsTooShort()
Definition: HashServiceTest.php:100
‪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:60
‪TYPO3\CMS\Extbase\Tests\Unit\Security\Cryptography\HashServiceTest\generateHmacReturnsHashStringWhichContainsSomeSalt
‪generateHmacReturnsHashStringWhichContainsSomeSalt()
Definition: HashServiceTest.php:51
‪TYPO3\CMS\Extbase\Tests\Unit\Security\Cryptography\HashServiceTest\appendHmacAppendsHmacToGivenString
‪appendHmacAppendsHmacToGivenString()
Definition: HashServiceTest.php:90
‪TYPO3\CMS\Extbase\Tests\Unit\Security\Cryptography\HashServiceTest\setUp
‪setUp()
Definition: HashServiceTest.php:32
‪TYPO3\CMS\Extbase\Security\Exception\InvalidHashException
Definition: InvalidHashException.php:25
‪TYPO3\CMS\Extbase\Tests\Unit\Security\Cryptography\HashServiceTest
Definition: HashServiceTest.php:29
‪TYPO3\CMS\Extbase\Tests\Unit\Security\Cryptography\HashServiceTest\validateAndStripHmacReturnsTheStringWithoutHmac
‪validateAndStripHmacReturnsTheStringWithoutHmac()
Definition: HashServiceTest.php:130
‪TYPO3\CMS\Extbase\Tests\Unit\Security\Cryptography\HashServiceTest\generateHmacReturnsHashStringIfStringIsGiven
‪generateHmacReturnsHashStringIfStringIsGiven()
Definition: HashServiceTest.php:42
‪$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:30
‪TYPO3\CMS\Extbase\Tests\Unit\Security\Cryptography\HashServiceTest\generatedHmacCanBeValidatedAgain
‪generatedHmacCanBeValidatedAgain()
Definition: HashServiceTest.php:70
‪TYPO3\CMS\Extbase\Tests\Unit\Security\Cryptography\HashServiceTest\validateAndStripHmacThrowsExceptionIfGivenStringHasNoHashAppended
‪validateAndStripHmacThrowsExceptionIfGivenStringHasNoHashAppended()
Definition: HashServiceTest.php:110