‪TYPO3CMS  9.5
HashServiceTest.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
18 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
19 
23 class ‪HashServiceTest extends UnitTestCase
24 {
28  protected ‪$hashService;
29 
30  protected function ‪setUp()
31  {
32  $this->hashService = new \TYPO3\CMS\Extbase\Security\Cryptography\HashService();
33  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] = 'Testing';
34  }
35 
40  {
41  $hash = $this->hashService->generateHmac('asdf');
42  $this->assertTrue(is_string($hash));
43  }
44 
49  {
50  $hash = $this->hashService->generateHmac('asdf');
51  $this->assertNotEquals(sha1('asdf'), $hash);
52  }
53 
58  {
59  $hash1 = $this->hashService->generateHmac('asdf');
60  $hash2 = $this->hashService->generateHmac('blubb');
61  $this->assertNotEquals($hash1, $hash2);
62  }
63 
68  {
69  $this->expectException(InvalidArgumentForHashGenerationException::class);
70  $this->expectExceptionCode(1255069587);
71  $this->hashService->generateHmac(null);
72  }
73 
77  public function ‪generatedHmacCanBeValidatedAgain()
78  {
79  $string = 'asdf';
80  $hash = $this->hashService->generateHmac($string);
81  $this->assertTrue($this->hashService->validateHmac($string, $hash));
82  }
83 
88  {
89  $string = 'asdf';
90  $hash = 'myhash';
91  $this->assertFalse($this->hashService->validateHmac($string, $hash));
92  }
93 
98  {
99  $this->expectException(InvalidArgumentForHashGenerationException::class);
100  $this->expectExceptionCode(1255069587);
101  $this->hashService->appendHmac(null);
102  }
103 
107  public function ‪appendHmacAppendsHmacToGivenString()
108  {
109  $string = 'This is some arbitrary string ';
110  $hashedString = $this->hashService->appendHmac($string);
111  $this->assertSame($string, substr($hashedString, 0, -40));
112  }
113 
118  {
119  $this->expectException(InvalidArgumentForHashGenerationException::class);
120  $this->expectExceptionCode(1320829762);
121  $this->hashService->validateAndStripHmac(null);
122  }
123 
128  {
129  $this->expectException(InvalidArgumentForHashGenerationException::class);
130  $this->expectExceptionCode(1320830276);
131  $this->hashService->validateAndStripHmac('string with less than 40 characters');
132  }
133 
138  {
139  $this->expectException(InvalidHashException::class);
140  $this->expectExceptionCode(1320830018);
141  $this->hashService->validateAndStripHmac('string with exactly a length 40 of chars');
142  }
143 
148  {
149  $this->expectException(InvalidHashException::class);
150  $this->expectExceptionCode(1320830018);
151  $this->hashService->validateAndStripHmac('some Stringac43682075d36592d4cb320e69ff0aa515886eab');
152  }
153 
158  {
159  $string = ' Some arbitrary string with special characters: öäüß!"§$ ';
160  $hashedString = $this->hashService->appendHmac($string);
161  $actualResult = $this->hashService->validateAndStripHmac($hashedString);
162  $this->assertSame($string, $actualResult);
163  }
164 }
‪TYPO3\CMS\Extbase\Tests\Unit\Security\Cryptography\HashServiceTest\validateAndStripHmacThrowsExceptionIfTheAppendedHashIsInvalid
‪validateAndStripHmacThrowsExceptionIfTheAppendedHashIsInvalid()
Definition: HashServiceTest.php:146
‪TYPO3\CMS\Extbase\Tests\Unit\Security\Cryptography\HashServiceTest\generatedHmacWillNotBeValidatedIfHashHasBeenChanged
‪generatedHmacWillNotBeValidatedIfHashHasBeenChanged()
Definition: HashServiceTest.php:86
‪TYPO3\CMS\Extbase\Tests\Unit\Security\Cryptography\HashServiceTest\validateAndStripHmacThrowsExceptionIfGivenStringIsTooShort
‪validateAndStripHmacThrowsExceptionIfGivenStringIsTooShort()
Definition: HashServiceTest.php:126
‪TYPO3\CMS\Extbase\Security\Exception\InvalidArgumentForHashGenerationException
Definition: InvalidArgumentForHashGenerationException.php:21
‪TYPO3\CMS\Extbase\Tests\Unit\Security\Cryptography
Definition: HashServiceTest.php:2
‪TYPO3\CMS\Extbase\Tests\Unit\Security\Cryptography\HashServiceTest\validateAndStripHmacThrowsExceptionIfNoStringGiven
‪validateAndStripHmacThrowsExceptionIfNoStringGiven()
Definition: HashServiceTest.php:116
‪TYPO3\CMS\Extbase\Tests\Unit\Security\Cryptography\HashServiceTest\generateHmacReturnsDifferentHashStringsForDifferentInputStrings
‪generateHmacReturnsDifferentHashStringsForDifferentInputStrings()
Definition: HashServiceTest.php:56
‪TYPO3\CMS\Extbase\Tests\Unit\Security\Cryptography\HashServiceTest\generateHmacReturnsHashStringWhichContainsSomeSalt
‪generateHmacReturnsHashStringWhichContainsSomeSalt()
Definition: HashServiceTest.php:47
‪TYPO3\CMS\Extbase\Tests\Unit\Security\Cryptography\HashServiceTest\appendHmacAppendsHmacToGivenString
‪appendHmacAppendsHmacToGivenString()
Definition: HashServiceTest.php:106
‪TYPO3\CMS\Extbase\Tests\Unit\Security\Cryptography\HashServiceTest\setUp
‪setUp()
Definition: HashServiceTest.php:29
‪TYPO3\CMS\Extbase\Tests\Unit\Security\Cryptography\HashServiceTest\appendHmacThrowsExceptionIfNoStringGiven
‪appendHmacThrowsExceptionIfNoStringGiven()
Definition: HashServiceTest.php:96
‪TYPO3\CMS\Extbase\Tests\Unit\Security\Cryptography\HashServiceTest\$hashService
‪TYPO3 CMS Extbase Security Cryptography HashService $hashService
Definition: HashServiceTest.php:27
‪TYPO3\CMS\Extbase\Security\Exception\InvalidHashException
Definition: InvalidHashException.php:21
‪TYPO3\CMS\Extbase\Tests\Unit\Security\Cryptography\HashServiceTest
Definition: HashServiceTest.php:24
‪TYPO3\CMS\Extbase\Tests\Unit\Security\Cryptography\HashServiceTest\validateAndStripHmacReturnsTheStringWithoutHmac
‪validateAndStripHmacReturnsTheStringWithoutHmac()
Definition: HashServiceTest.php:156
‪TYPO3\CMS\Extbase\Tests\Unit\Security\Cryptography\HashServiceTest\generateHmacReturnsHashStringIfStringIsGiven
‪generateHmacReturnsHashStringIfStringIsGiven()
Definition: HashServiceTest.php:38
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Extbase\Tests\Unit\Security\Cryptography\HashServiceTest\generateHmacThrowsExceptionIfNoStringGiven
‪generateHmacThrowsExceptionIfNoStringGiven()
Definition: HashServiceTest.php:66
‪TYPO3\CMS\Extbase\Tests\Unit\Security\Cryptography\HashServiceTest\generatedHmacCanBeValidatedAgain
‪generatedHmacCanBeValidatedAgain()
Definition: HashServiceTest.php:76
‪TYPO3\CMS\Extbase\Tests\Unit\Security\Cryptography\HashServiceTest\validateAndStripHmacThrowsExceptionIfGivenStringHasNoHashAppended
‪validateAndStripHmacThrowsExceptionIfGivenStringHasNoHashAppended()
Definition: HashServiceTest.php:136