TYPO3 CMS  TYPO3_8-7
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 
22 class HashServiceTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
23 {
27  protected $hashService;
28 
29  protected function setUp()
30  {
31  $this->hashService = new \TYPO3\CMS\Extbase\Security\Cryptography\HashService();
32  $GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] = 'Testing';
33  }
34 
39  {
40  $hash = $this->hashService->generateHmac('asdf');
41  $this->assertTrue(is_string($hash));
42  }
43 
48  {
49  $hash = $this->hashService->generateHmac('asdf');
50  $this->assertNotEquals(sha1('asdf'), $hash);
51  }
52 
57  {
58  $hash1 = $this->hashService->generateHmac('asdf');
59  $hash2 = $this->hashService->generateHmac('blubb');
60  $this->assertNotEquals($hash1, $hash2);
61  }
62 
67  {
68  $this->expectException(InvalidArgumentForHashGenerationException::class);
69  $this->expectExceptionCode(1255069587);
70  $this->hashService->generateHmac(null);
71  }
72 
77  {
78  $string = 'asdf';
79  $hash = $this->hashService->generateHmac($string);
80  $this->assertTrue($this->hashService->validateHmac($string, $hash));
81  }
82 
87  {
88  $string = 'asdf';
89  $hash = 'myhash';
90  $this->assertFalse($this->hashService->validateHmac($string, $hash));
91  }
92 
97  {
98  $this->expectException(InvalidArgumentForHashGenerationException::class);
99  $this->expectExceptionCode(1255069587);
100  $this->hashService->appendHmac(null);
101  }
102 
107  {
108  $string = 'This is some arbitrary string ';
109  $hashedString = $this->hashService->appendHmac($string);
110  $this->assertSame($string, substr($hashedString, 0, -40));
111  }
112 
117  {
118  $this->expectException(InvalidArgumentForHashGenerationException::class);
119  $this->expectExceptionCode(1320829762);
120  $this->hashService->validateAndStripHmac(null);
121  }
122 
127  {
128  $this->expectException(InvalidArgumentForHashGenerationException::class);
129  $this->expectExceptionCode(1320830276);
130  $this->hashService->validateAndStripHmac('string with less than 40 characters');
131  }
132 
137  {
138  $this->expectException(InvalidHashException::class);
139  $this->expectExceptionCode(1320830018);
140  $this->hashService->validateAndStripHmac('string with exactly a length 40 of chars');
141  }
142 
147  {
148  $this->expectException(InvalidHashException::class);
149  $this->expectExceptionCode(1320830018);
150  $this->hashService->validateAndStripHmac('some Stringac43682075d36592d4cb320e69ff0aa515886eab');
151  }
152 
157  {
158  $string = ' Some arbitrary string with special characters: öäüß!"§$ ';
159  $hashedString = $this->hashService->appendHmac($string);
160  $actualResult = $this->hashService->validateAndStripHmac($hashedString);
161  $this->assertSame($string, $actualResult);
162  }
163 }
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']