MediaWiki REL1_33
PasswordFactoryTest.php
Go to the documentation of this file.
1<?php
2
7 public function testConstruct() {
8 $pf = new PasswordFactory();
9 $this->assertEquals( [ '' ], array_keys( $pf->getTypes() ) );
10 $this->assertEquals( '', $pf->getDefaultType() );
11
12 $pf = new PasswordFactory( [
13 'foo' => [ 'class' => 'FooPassword' ],
14 'bar' => [ 'class' => 'BarPassword', 'baz' => 'boom' ],
15 ], 'foo' );
16 $this->assertEquals( [ '', 'foo', 'bar' ], array_keys( $pf->getTypes() ) );
17 $this->assertArraySubset( [ 'class' => 'BarPassword', 'baz' => 'boom' ], $pf->getTypes()['bar'] );
18 $this->assertEquals( 'foo', $pf->getDefaultType() );
19 }
20
21 public function testRegister() {
22 $pf = new PasswordFactory;
23 $pf->register( 'foo', [ 'class' => InvalidPassword::class ] );
24 $this->assertArrayHasKey( 'foo', $pf->getTypes() );
25 }
26
27 public function testSetDefaultType() {
28 $pf = new PasswordFactory;
29 $pf->register( '1', [ 'class' => InvalidPassword::class ] );
30 $pf->register( '2', [ 'class' => InvalidPassword::class ] );
31 $pf->setDefaultType( '1' );
32 $this->assertSame( '1', $pf->getDefaultType() );
33 $pf->setDefaultType( '2' );
34 $this->assertSame( '2', $pf->getDefaultType() );
35 }
36
40 public function testSetDefaultTypeError() {
41 $pf = new PasswordFactory;
42 $pf->setDefaultType( 'bogus' );
43 }
44
45 public function testInit() {
46 $config = new HashConfig( [
47 'PasswordConfig' => [
48 'foo' => [ 'class' => InvalidPassword::class ],
49 ],
50 'PasswordDefault' => 'foo'
51 ] );
52 $pf = new PasswordFactory;
53 $pf->init( $config );
54 $this->assertSame( 'foo', $pf->getDefaultType() );
55 $this->assertArrayHasKey( 'foo', $pf->getTypes() );
56 }
57
58 public function testNewFromCiphertext() {
59 $pf = new PasswordFactory;
60 $pf->register( 'B', [ 'class' => MWSaltedPassword::class ] );
61 $pw = $pf->newFromCiphertext( ':B:salt:d529e941509eb9e9b9cfaeae1fe7ca23' );
62 $this->assertInstanceOf( MWSaltedPassword::class, $pw );
63 }
64
66 return [ [ 'blah' ], [ ':blah:' ] ];
67 }
68
73 public function testNewFromCiphertextErrors( $hash ) {
74 $pf = new PasswordFactory;
75 $pf->register( 'B', [ 'class' => MWSaltedPassword::class ] );
76 $pf->newFromCiphertext( $hash );
77 }
78
79 public function testNewFromType() {
80 $pf = new PasswordFactory;
81 $pf->register( 'B', [ 'class' => MWSaltedPassword::class ] );
82 $pw = $pf->newFromType( 'B' );
83 $this->assertInstanceOf( MWSaltedPassword::class, $pw );
84 }
85
89 public function testNewFromTypeError() {
90 $pf = new PasswordFactory;
91 $pf->register( 'B', [ 'class' => MWSaltedPassword::class ] );
92 $pf->newFromType( 'bogus' );
93 }
94
95 public function testNewFromPlaintext() {
96 $pf = new PasswordFactory;
97 $pf->register( 'A', [ 'class' => MWOldPassword::class ] );
98 $pf->register( 'B', [ 'class' => MWSaltedPassword::class ] );
99 $pf->setDefaultType( 'A' );
100
101 $this->assertInstanceOf( InvalidPassword::class, $pf->newFromPlaintext( null ) );
102 $this->assertInstanceOf( MWOldPassword::class, $pf->newFromPlaintext( 'password' ) );
103 $this->assertInstanceOf( MWSaltedPassword::class,
104 $pf->newFromPlaintext( 'password', $pf->newFromType( 'B' ) ) );
105 }
106
107 public function testNeedsUpdate() {
108 $pf = new PasswordFactory;
109 $pf->register( 'A', [ 'class' => MWOldPassword::class ] );
110 $pf->register( 'B', [ 'class' => MWSaltedPassword::class ] );
111 $pf->setDefaultType( 'A' );
112
113 $this->assertFalse( $pf->needsUpdate( $pf->newFromType( 'A' ) ) );
114 $this->assertTrue( $pf->needsUpdate( $pf->newFromType( 'B' ) ) );
115 }
116
118 $this->assertSame( 13, strlen( PasswordFactory::generateRandomPasswordString( 13 ) ) );
119 }
120
121 public function testNewInvalidPassword() {
122 $this->assertInstanceOf( InvalidPassword::class, PasswordFactory::newInvalidPassword() );
123 }
124}
and that you know you can do these things To protect your we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights These restrictions translate to certain responsibilities for you if you distribute copies of the or if you modify it For if you distribute copies of such a whether gratis or for a you must give the recipients all the rights that you have You must make sure that receive or can get the source code And you must show them these terms so they know their rights We protect your rights with two and(2) offer you this license which gives you legal permission to copy
A Config instance which stores all settings as a member variable.
testNewFromCiphertextErrors( $hash)
provideNewFromCiphertextErrors PasswordError
testNewFromTypeError()
PasswordError.
testSetDefaultTypeError()
Exception.
Factory class for creating and checking Password objects.
register( $type, array $config)
Register a new type of password hash.
setDefaultType( $type)
Set the default password type.
init(Config $config)