[CLEANUP] Adjust code to coding guidelines
[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 * Testcase for the Hash Service
28 *
29 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser Public License, version 3 or later
30 */
31 class HashServiceTest extends \TYPO3\CMS\Extbase\Tests\Unit\BaseTestCase {
32
33 protected $hashService;
34
35 public function setUp() {
36 $this->hashService = new \TYPO3\CMS\Extbase\Security\Cryptography\HashService();
37 }
38
39 /**
40 * @test
41 * @author Sebastian Kurfürst
42 */
43 public function generateHashReturnsHashStringIfStringIsGiven() {
44 $hash = $this->hashService->generateHash('asdf');
45 $this->assertTrue(is_string($hash));
46 }
47
48 /**
49 * @test
50 * @author Sebastian Kurfürst
51 */
52 public function generateHashReturnsHashStringWhichContainsSomeSalt() {
53 $hash = $this->hashService->generateHash('asdf');
54 $this->assertNotEquals(sha1('asdf'), $hash);
55 }
56
57 /**
58 * @test
59 * @author Sebastian Kurfürst
60 */
61 public function generateHashReturnsDifferentHashStringsForDifferentInputStrings() {
62 $hash1 = $this->hashService->generateHash('asdf');
63 $hash2 = $this->hashService->generateHash('blubb');
64 $this->assertNotEquals($hash1, $hash2);
65 }
66
67 /**
68 * @test
69 * @expectedException \TYPO3\CMS\Extbase\Security\Exception\InvalidArgumentForHashGenerationException
70 * @author Sebastian Kurfürst
71 */
72 public function generateHashThrowsExceptionIfNoStringGiven() {
73 $hash = $this->hashService->generateHash(NULL);
74 }
75
76 /**
77 * @test
78 * @author Sebastian Kurfürst <sebastian@typo3.org>
79 */
80 public function generatedHashCanBeValidatedAgain() {
81 $string = 'asdf';
82 $hash = $this->hashService->generateHash($string);
83 $this->assertTrue($this->hashService->validateHash($string, $hash));
84 }
85
86 /**
87 * @test
88 * @author Sebastian Kurfürst <sebastian@typo3.org>
89 */
90 public function generatedHashWillNotBeValidatedIfHashHasBeenChanged() {
91 $string = 'asdf';
92 $hash = 'myhash';
93 $this->assertFalse($this->hashService->validateHash($string, $hash));
94 }
95
96 /**
97 * @test
98 * @expectedException \TYPO3\CMS\Extbase\Security\Exception\InvalidArgumentForHashGenerationException
99 */
100 public function appendHmacThrowsExceptionIfNoStringGiven() {
101 $this->hashService->appendHmac(NULL);
102 }
103
104 /**
105 * @test
106 */
107 public function appendHmacAppendsHmacToGivenString() {
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 * @expectedException \TYPO3\CMS\Extbase\Security\Exception\InvalidArgumentForHashGenerationException
116 */
117 public function validateAndStripHmacThrowsExceptionIfNoStringGiven() {
118 $this->hashService->validateAndStripHmac(NULL);
119 }
120
121 /**
122 * @test
123 * @expectedException \TYPO3\CMS\Extbase\Security\Exception\InvalidArgumentForHashGenerationException
124 */
125 public function validateAndStripHmacThrowsExceptionIfGivenStringIsTooShort() {
126 $this->hashService->validateAndStripHmac('string with less than 40 characters');
127 }
128
129 /**
130 * @test
131 * @expectedException \TYPO3\CMS\Extbase\Security\Exception\InvalidHashException
132 */
133 public function validateAndStripHmacThrowsExceptionIfGivenStringHasNoHashAppended() {
134 $this->hashService->validateAndStripHmac('string with exactly a length 40 of chars');
135 }
136
137 /**
138 * @test
139 * @expectedException \TYPO3\CMS\Extbase\Security\Exception\InvalidHashException
140 */
141 public function validateAndStripHmacThrowsExceptionIfTheAppendedHashIsInvalid() {
142 $this->hashService->validateAndStripHmac('some Stringac43682075d36592d4cb320e69ff0aa515886eab');
143 }
144
145 /**
146 * @test
147 */
148 public function validateAndStripHmacReturnsTheStringWithoutHmac() {
149 $string = ' Some arbitrary string with special characters: öäüß!"§$ ';
150 $hashedString = $this->hashService->appendHmac($string);
151 $actualResult = $this->hashService->validateAndStripHmac($hashedString);
152 $this->assertSame($string, $actualResult);
153 }
154 }
155
156 ?>