[TASK][WIP] Move non class code back to source files
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / Security / Cryptography / HashServiceTest.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009 Sebastian Kurfürst <sebastian@typo3.org>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 *
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
24 /**
25 * Testcase for the Hash Service
26 *
27 * @version $Id: HashService_testcase.php 1729 2009-11-25 21:37:20Z stucki $
28 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser Public License, version 3 or later
29 */
30 class Tx_Extbase_Tests_Unit_Security_Cryptography_HashServiceTest extends Tx_Extbase_Tests_Unit_BaseTestCase {
31
32 protected $hashService;
33
34 public function setUp() {
35 $this->hashService = new Tx_Extbase_Security_Cryptography_HashService();
36 }
37
38 /**
39 * @test
40 * @author Sebastian Kurfürst
41 */
42 public function generateHashReturnsHashStringIfStringIsGiven() {
43 $hash = $this->hashService->generateHash('asdf');
44 $this->assertTrue(is_string($hash));
45 }
46
47 /**
48 * @test
49 * @author Sebastian Kurfürst
50 */
51 public function generateHashReturnsHashStringWhichContainsSomeSalt() {
52 $hash = $this->hashService->generateHash('asdf');
53 $this->assertNotEquals(sha1('asdf'), $hash);
54 }
55
56 /**
57 * @test
58 * @author Sebastian Kurfürst
59 */
60 public function generateHashReturnsDifferentHashStringsForDifferentInputStrings() {
61 $hash1 = $this->hashService->generateHash('asdf');
62 $hash2 = $this->hashService->generateHash('blubb');
63 $this->assertNotEquals($hash1, $hash2);
64 }
65
66 /**
67 * @test
68 * @expectedException Tx_Extbase_Security_Exception_InvalidArgumentForHashGeneration
69 * @author Sebastian Kurfürst
70 */
71 public function generateHashThrowsExceptionIfNoStringGiven() {
72 $hash = $this->hashService->generateHash(NULL);
73 }
74
75 /**
76 * @test
77 * @author Sebastian Kurfürst <sebastian@typo3.org>
78 */
79 public function generatedHashCanBeValidatedAgain() {
80 $string = 'asdf';
81 $hash = $this->hashService->generateHash($string);
82 $this->assertTrue($this->hashService->validateHash($string, $hash));
83 }
84
85 /**
86 * @test
87 * @author Sebastian Kurfürst <sebastian@typo3.org>
88 */
89 public function generatedHashWillNotBeValidatedIfHashHasBeenChanged() {
90 $string = 'asdf';
91 $hash = 'myhash';
92 $this->assertFalse($this->hashService->validateHash($string, $hash));
93 }
94
95 /**
96 * @test
97 * @expectedException Tx_Extbase_Security_Exception_InvalidArgumentForHashGeneration
98 */
99 public function appendHmacThrowsExceptionIfNoStringGiven() {
100 $this->hashService->appendHmac(NULL);
101 }
102
103 /**
104 * @test
105 */
106 public function appendHmacAppendsHmacToGivenString() {
107 $string = 'This is some arbitrary string ';
108 $hashedString = $this->hashService->appendHmac($string);
109 $this->assertSame($string, substr($hashedString, 0, -40));
110 }
111
112 /**
113 * @test
114 * @expectedException Tx_Extbase_Security_Exception_InvalidArgumentForHashGeneration
115 */
116 public function validateAndStripHmacThrowsExceptionIfNoStringGiven() {
117 $this->hashService->validateAndStripHmac(NULL);
118 }
119
120 /**
121 * @test
122 * @expectedException Tx_Extbase_Security_Exception_InvalidArgumentForHashGeneration
123 */
124 public function validateAndStripHmacThrowsExceptionIfGivenStringIsTooShort() {
125 $this->hashService->validateAndStripHmac('string with less than 40 characters');
126 }
127
128 /**
129 * @test
130 * @expectedException Tx_Extbase_Security_Exception_InvalidHash
131 */
132 public function validateAndStripHmacThrowsExceptionIfGivenStringHasNoHashAppended() {
133 $this->hashService->validateAndStripHmac('string with exactly a length 40 of chars');
134 }
135
136 /**
137 * @test
138 * @expectedException Tx_Extbase_Security_Exception_InvalidHash
139 */
140 public function validateAndStripHmacThrowsExceptionIfTheAppendedHashIsInvalid() {
141 $this->hashService->validateAndStripHmac('some Stringac43682075d36592d4cb320e69ff0aa515886eab');
142 }
143
144 /**
145 * @test
146 */
147 public function validateAndStripHmacReturnsTheStringWithoutHmac() {
148 $string = ' Some arbitrary string with special characters: öäüß!"§$ ';
149 $hashedString = $this->hashService->appendHmac($string);
150 $actualResult = $this->hashService->validateAndStripHmac($hashedString);
151 $this->assertSame($string, $actualResult);
152 }
153
154 }
155
156 ?>