[BUGFIX] Do not load LocalConfiguration in unit test bootstrap
[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 * Copyright notice
6 *
7 * (c) 2009 Sebastian Kurfürst <sebastian@typo3.org>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26
27 /**
28 * Test case
29 */
30 class HashServiceTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
31
32 /**
33 * @var \TYPO3\CMS\Extbase\Security\Cryptography\HashService
34 */
35 protected $hashService;
36
37 public function setUp() {
38 $this->hashService = new \TYPO3\CMS\Extbase\Security\Cryptography\HashService();
39 $GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] = 'Testing';
40 }
41
42 /**
43 * @test
44 * @author Sebastian Kurfürst
45 */
46 public function generateHmacReturnsHashStringIfStringIsGiven() {
47 $hash = $this->hashService->generateHmac('asdf');
48 $this->assertTrue(is_string($hash));
49 }
50
51 /**
52 * @test
53 * @author Sebastian Kurfürst
54 */
55 public function generateHmacReturnsHashStringWhichContainsSomeSalt() {
56 $hash = $this->hashService->generateHmac('asdf');
57 $this->assertNotEquals(sha1('asdf'), $hash);
58 }
59
60 /**
61 * @test
62 * @author Sebastian Kurfürst
63 */
64 public function generateHmacReturnsDifferentHashStringsForDifferentInputStrings() {
65 $hash1 = $this->hashService->generateHmac('asdf');
66 $hash2 = $this->hashService->generateHmac('blubb');
67 $this->assertNotEquals($hash1, $hash2);
68 }
69
70 /**
71 * @test
72 * @expectedException \TYPO3\CMS\Extbase\Security\Exception\InvalidArgumentForHashGenerationException
73 * @author Sebastian Kurfürst
74 */
75 public function generateHmacThrowsExceptionIfNoStringGiven() {
76 $hash = $this->hashService->generateHmac(NULL);
77 }
78
79 /**
80 * @test
81 * @author Sebastian Kurfürst <sebastian@typo3.org>
82 */
83 public function generatedHmacCanBeValidatedAgain() {
84 $string = 'asdf';
85 $hash = $this->hashService->generateHmac($string);
86 $this->assertTrue($this->hashService->validateHmac($string, $hash));
87 }
88
89 /**
90 * @test
91 * @author Sebastian Kurfürst <sebastian@typo3.org>
92 */
93 public function generatedHmacWillNotBeValidatedIfHashHasBeenChanged() {
94 $string = 'asdf';
95 $hash = 'myhash';
96 $this->assertFalse($this->hashService->validateHmac($string, $hash));
97 }
98
99 /**
100 * @test
101 * @expectedException \TYPO3\CMS\Extbase\Security\Exception\InvalidArgumentForHashGenerationException
102 */
103 public function appendHmacThrowsExceptionIfNoStringGiven() {
104 $this->hashService->appendHmac(NULL);
105 }
106
107 /**
108 * @test
109 */
110 public function appendHmacAppendsHmacToGivenString() {
111 $string = 'This is some arbitrary string ';
112 $hashedString = $this->hashService->appendHmac($string);
113 $this->assertSame($string, substr($hashedString, 0, -40));
114 }
115
116 /**
117 * @test
118 * @expectedException \TYPO3\CMS\Extbase\Security\Exception\InvalidArgumentForHashGenerationException
119 */
120 public function validateAndStripHmacThrowsExceptionIfNoStringGiven() {
121 $this->hashService->validateAndStripHmac(NULL);
122 }
123
124 /**
125 * @test
126 * @expectedException \TYPO3\CMS\Extbase\Security\Exception\InvalidArgumentForHashGenerationException
127 */
128 public function validateAndStripHmacThrowsExceptionIfGivenStringIsTooShort() {
129 $this->hashService->validateAndStripHmac('string with less than 40 characters');
130 }
131
132 /**
133 * @test
134 * @expectedException \TYPO3\CMS\Extbase\Security\Exception\InvalidHashException
135 */
136 public function validateAndStripHmacThrowsExceptionIfGivenStringHasNoHashAppended() {
137 $this->hashService->validateAndStripHmac('string with exactly a length 40 of chars');
138 }
139
140 /**
141 * @test
142 * @expectedException \TYPO3\CMS\Extbase\Security\Exception\InvalidHashException
143 */
144 public function validateAndStripHmacThrowsExceptionIfTheAppendedHashIsInvalid() {
145 $this->hashService->validateAndStripHmac('some Stringac43682075d36592d4cb320e69ff0aa515886eab');
146 }
147
148 /**
149 * @test
150 */
151 public function validateAndStripHmacReturnsTheStringWithoutHmac() {
152 $string = ' Some arbitrary string with special characters: öäüß!"§$ ';
153 $hashedString = $this->hashService->appendHmac($string);
154 $actualResult = $this->hashService->validateAndStripHmac($hashedString);
155 $this->assertSame($string, $actualResult);
156 }
157 }