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