[!!!][TASK] Extract testing framework for TYPO3
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / Security / Cryptography / HashServiceTest.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Tests\Unit\Security\Cryptography;
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 */
16 use TYPO3\CMS\Extbase\Security\Exception\InvalidArgumentForHashGenerationException;
17 use TYPO3\CMS\Extbase\Security\Exception\InvalidHashException;
18
19 /**
20 * Test case
21 */
22 class HashServiceTest extends \TYPO3\CMS\Components\TestingFramework\Core\UnitTestCase
23 {
24 /**
25 * @var \TYPO3\CMS\Extbase\Security\Cryptography\HashService
26 */
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
35 /**
36 * @test
37 */
38 public function generateHmacReturnsHashStringIfStringIsGiven()
39 {
40 $hash = $this->hashService->generateHmac('asdf');
41 $this->assertTrue(is_string($hash));
42 }
43
44 /**
45 * @test
46 */
47 public function generateHmacReturnsHashStringWhichContainsSomeSalt()
48 {
49 $hash = $this->hashService->generateHmac('asdf');
50 $this->assertNotEquals(sha1('asdf'), $hash);
51 }
52
53 /**
54 * @test
55 */
56 public function generateHmacReturnsDifferentHashStringsForDifferentInputStrings()
57 {
58 $hash1 = $this->hashService->generateHmac('asdf');
59 $hash2 = $this->hashService->generateHmac('blubb');
60 $this->assertNotEquals($hash1, $hash2);
61 }
62
63 /**
64 * @test
65 */
66 public function generateHmacThrowsExceptionIfNoStringGiven()
67 {
68 $this->expectException(InvalidArgumentForHashGenerationException::class);
69 $this->expectExceptionCode(1255069587);
70 $this->hashService->generateHmac(null);
71 }
72
73 /**
74 * @test
75 */
76 public function generatedHmacCanBeValidatedAgain()
77 {
78 $string = 'asdf';
79 $hash = $this->hashService->generateHmac($string);
80 $this->assertTrue($this->hashService->validateHmac($string, $hash));
81 }
82
83 /**
84 * @test
85 */
86 public function generatedHmacWillNotBeValidatedIfHashHasBeenChanged()
87 {
88 $string = 'asdf';
89 $hash = 'myhash';
90 $this->assertFalse($this->hashService->validateHmac($string, $hash));
91 }
92
93 /**
94 * @test
95 */
96 public function appendHmacThrowsExceptionIfNoStringGiven()
97 {
98 $this->expectException(InvalidArgumentForHashGenerationException::class);
99 $this->expectExceptionCode(1255069587);
100 $this->hashService->appendHmac(null);
101 }
102
103 /**
104 * @test
105 */
106 public function appendHmacAppendsHmacToGivenString()
107 {
108 $string = 'This is some arbitrary string ';
109 $hashedString = $this->hashService->appendHmac($string);
110 $this->assertSame($string, substr($hashedString, 0, -40));
111 }
112
113 /**
114 * @test
115 */
116 public function validateAndStripHmacThrowsExceptionIfNoStringGiven()
117 {
118 $this->expectException(InvalidArgumentForHashGenerationException::class);
119 $this->expectExceptionCode(1320829762);
120 $this->hashService->validateAndStripHmac(null);
121 }
122
123 /**
124 * @test
125 */
126 public function validateAndStripHmacThrowsExceptionIfGivenStringIsTooShort()
127 {
128 $this->expectException(InvalidArgumentForHashGenerationException::class);
129 $this->expectExceptionCode(1320830276);
130 $this->hashService->validateAndStripHmac('string with less than 40 characters');
131 }
132
133 /**
134 * @test
135 */
136 public function validateAndStripHmacThrowsExceptionIfGivenStringHasNoHashAppended()
137 {
138 $this->expectException(InvalidHashException::class);
139 $this->expectExceptionCode(1320830018);
140 $this->hashService->validateAndStripHmac('string with exactly a length 40 of chars');
141 }
142
143 /**
144 * @test
145 */
146 public function validateAndStripHmacThrowsExceptionIfTheAppendedHashIsInvalid()
147 {
148 $this->expectException(InvalidHashException::class);
149 $this->expectExceptionCode(1320830018);
150 $this->hashService->validateAndStripHmac('some Stringac43682075d36592d4cb320e69ff0aa515886eab');
151 }
152
153 /**
154 * @test
155 */
156 public function validateAndStripHmacReturnsTheStringWithoutHmac()
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 }