ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
ilBcryptPhpPasswordEncoderTest.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 1998-2016 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4require_once 'Services/Password/classes/encoders/class.ilBcryptPhpPasswordEncoder.php';
5require_once 'Services/Password/test/ilPasswordBaseTest.php';
6
13{
17 const VALID_COSTS = '08';
18
22 const PASSWORD = 'password';
23
27 const WRONG_PASSWORD = 'wrong_password';
28
32 protected function setUp()
33 {
34 }
35
40 {
41 if(version_compare(phpversion(), '5.5.0', '<'))
42 {
43 $this->markTestSkipped('Requires PHP >= 5.5.0');
44 }
45 }
46
50 public function costsProvider()
51 {
52 $data = array();
53 for($i = 4; $i <= 31; $i++)
54 {
55 $data[] = array($i);
56 }
57 return $data;
58 }
59
63 public function testInstanceCanBeCreated()
64 {
66
67 $default_costs_encoder = new ilBcryptPhpPasswordEncoder();
68 $this->assertTrue((int)$default_costs_encoder->getCosts() > 4 && (int)$default_costs_encoder->getCosts() < 32);
69
70 $encoder = new ilBcryptPhpPasswordEncoder(array(
71 'cost' => self::VALID_COSTS
72 ));
73 $this->assertInstanceOf('ilBcryptPhpPasswordEncoder', $encoder);
74 $this->assertEquals(self::VALID_COSTS, $encoder->getCosts());
75 return $encoder;
76 }
77
82 {
83 $encoder->setCosts(4);
84 $this->assertEquals(4, $encoder->getCosts());
85 }
86
92 {
93 $this->assertException(ilPasswordException::class);
94 $encoder->setCosts(32);
95 }
96
102 {
103 $this->assertException(ilPasswordException::class);
104 $encoder->setCosts(3);
105 }
106
111 public function testCostsCanBeSetInRange($costs, ilBcryptPhpPasswordEncoder $encoder)
112 {
113 $encoder->setCosts($costs);
114 }
115
120 {
121 $encoder->setCosts(self::VALID_COSTS);
122 $encoded_password = $encoder->encodePassword(self::PASSWORD, '');
123 $this->assertTrue($encoder->isPasswordValid($encoded_password, self::PASSWORD, ''));
124 $this->assertFalse($encoder->isPasswordValid($encoded_password, self::WRONG_PASSWORD, ''));
125 return $encoder;
126 }
127
133 {
134 $this->assertException(ilPasswordException::class);
135 $encoder->setCosts(self::VALID_COSTS);
136 $encoder->encodePassword(str_repeat('a', 5000), '');
137 }
138
143 {
144 $encoder->setCosts(self::VALID_COSTS);
145 $this->assertFalse($encoder->isPasswordValid('encoded', str_repeat('a', 5000), ''));
146 }
147
152 {
153 $this->assertEquals('bcryptphp', $encoder->getName());
154 }
155
160 {
161 $costs_default = $encoder->benchmarkCost();
162 $costs_target = $encoder->benchmarkCost(0.5);
163
164 $this->assertTrue($costs_default > 4 && $costs_default < 32);
165 $this->assertTrue($costs_target > 4 && $costs_target < 32);
166 $this->assertInternalType('int', $costs_default);
167 $this->assertInternalType('int', $costs_target);
168 $this->assertNotEquals($costs_default, $costs_target);
169 }
170
175 {
176 $this->assertFalse($encoder->requiresSalt());
177 }
178
183 {
184 $raw = self::PASSWORD;
185
186 $encoder->setCosts(8);
187 $encoded = $encoder->encodePassword($raw, '');
188 $encoder->setCosts(8);
189 $this->assertFalse($encoder->requiresReencoding($encoded));
190
191 $encoder->setCosts(9);
192 $this->assertTrue($encoder->requiresReencoding($encoded));
193 }
194}
An exception for terminatinating execution or to throw for unit testing.
requiresSalt()
{Returns whether or not the encoder requires a salt.boolean}
testCostsCanBeSetInRange($costs, ilBcryptPhpPasswordEncoder $encoder)
@depends testInstanceCanBeCreated @dataProvider costsProvider
testCostsCannotBeSetBelowRange(ilBcryptPhpPasswordEncoder $encoder)
@depends testInstanceCanBeCreated @expectedException ilPasswordException
testReencodingIsDetectedWhenNecessary(ilBcryptPhpPasswordEncoder $encoder)
@depends testInstanceCanBeCreated
testCostsCanBeDeterminedDynamically(ilBcryptPhpPasswordEncoder $encoder)
@depends testInstanceCanBeCreated
testExceptionIsRaisedIfThePasswordExceedsTheSupportedLengthOnEncoding(ilBcryptPhpPasswordEncoder $encoder)
@depends testInstanceCanBeCreated @expectedException ilPasswordException
testEncoderDoesNotRelyOnSalts(ilBcryptPhpPasswordEncoder $encoder)
@depends testInstanceCanBeCreated
testCostsCannotBeSetAboveRange(ilBcryptPhpPasswordEncoder $encoder)
@depends testInstanceCanBeCreated @expectedException ilPasswordException
testNameShouldBeBcryptPhp(ilBcryptPhpPasswordEncoder $encoder)
@depends testInstanceCanBeCreated
testCostsCanBeRetrievedWhenCostsAreSet(ilBcryptPhpPasswordEncoder $encoder)
@depends testInstanceCanBeCreated
testPasswordShouldBeCorrectlyEncodedAndVerified(ilBcryptPhpPasswordEncoder $encoder)
@depends testInstanceCanBeCreated
testPasswordVerificationShouldFailIfTheRawPasswordExceedsTheSupportedLength(ilBcryptPhpPasswordEncoder $encoder)
@depends testInstanceCanBeCreated
requiresReencoding($encoded)
{{Returns whether or not the a encoded password needs to be re-encoded.boolean}}
isPasswordValid($encoded, $raw, $salt)
{Checks a raw password against an encoded password.The raw password has to be injected into the encod...
encodePassword($raw, $salt)
{Encodes the raw password.string The encoded password}
assertException($exception_class)