‪TYPO3CMS  10.4
HashServiceTest.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
21 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
22 
26 class ‪HashServiceTest extends UnitTestCase
27 {
31  protected ‪$hashService;
32 
33  protected function ‪setUp(): void
34  {
35  parent::setUp();
36  $this->hashService = new ‪HashService();
37  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] = 'Testing';
38  }
39 
44  {
45  $hash = $this->hashService->generateHmac('asdf');
46  self::assertTrue(is_string($hash));
47  }
48 
53  {
54  $hash = $this->hashService->generateHmac('asdf');
55  self::assertNotEquals(sha1('asdf'), $hash);
56  }
57 
62  {
63  $hash1 = $this->hashService->generateHmac('asdf');
64  $hash2 = $this->hashService->generateHmac('blubb');
65  self::assertNotEquals($hash1, $hash2);
66  }
67 
71  public function ‪generatedHmacCanBeValidatedAgain()
72  {
73  $string = 'asdf';
74  $hash = $this->hashService->generateHmac($string);
75  self::assertTrue($this->hashService->validateHmac($string, $hash));
76  }
77 
82  {
83  $string = 'asdf';
84  $hash = 'myhash';
85  self::assertFalse($this->hashService->validateHmac($string, $hash));
86  }
87 
92  {
93  $string = 'This is some arbitrary string ';
94  $hashedString = $this->hashService->appendHmac($string);
95  self::assertSame($string, substr($hashedString, 0, -40));
96  }
97 
102  {
103  $this->expectException(InvalidArgumentForHashGenerationException::class);
104  $this->expectExceptionCode(1320830276);
105  $this->hashService->validateAndStripHmac('string with less than 40 characters');
106  }
107 
112  {
113  $this->expectException(InvalidHashException::class);
114  $this->expectExceptionCode(1320830018);
115  $this->hashService->validateAndStripHmac('string with exactly a length 40 of chars');
116  }
117 
122  {
123  $this->expectException(InvalidHashException::class);
124  $this->expectExceptionCode(1320830018);
125  $this->hashService->validateAndStripHmac('some Stringac43682075d36592d4cb320e69ff0aa515886eab');
126  }
127 
132  {
133  $string = ' Some arbitrary string with special characters: öäüß!"§$ ';
134  $hashedString = $this->hashService->appendHmac($string);
135  $actualResult = $this->hashService->validateAndStripHmac($hashedString);
136  self::assertSame($string, $actualResult);
137  }
138 }
‪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:26
‪TYPO3\CMS\Extbase\Security\Cryptography\HashService
Definition: HashService.php:31
‪TYPO3\CMS\Extbase\Tests\Unit\Security\Cryptography
Definition: HashServiceTest.php:16
‪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\Tests\Unit\Security\Cryptography\HashServiceTest\$hashService
‪TYPO3 CMS Extbase Security Cryptography HashService $hashService
Definition: HashServiceTest.php:30
‪TYPO3\CMS\Extbase\Security\Exception\InvalidHashException
Definition: InvalidHashException.php:26
‪TYPO3\CMS\Extbase\Tests\Unit\Security\Cryptography\HashServiceTest
Definition: HashServiceTest.php:27
‪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:5
‪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