MediaWiki REL1_31
PasswordFactoryTest.php
Go to the documentation of this file.
1<?php
2
7 public function testRegister() {
8 $pf = new PasswordFactory;
9 $pf->register( 'foo', [ 'class' => InvalidPassword::class ] );
10 $this->assertArrayHasKey( 'foo', $pf->getTypes() );
11 }
12
13 public function testSetDefaultType() {
14 $pf = new PasswordFactory;
15 $pf->register( '1', [ 'class' => InvalidPassword::class ] );
16 $pf->register( '2', [ 'class' => InvalidPassword::class ] );
17 $pf->setDefaultType( '1' );
18 $this->assertSame( '1', $pf->getDefaultType() );
19 $pf->setDefaultType( '2' );
20 $this->assertSame( '2', $pf->getDefaultType() );
21 }
22
26 public function testSetDefaultTypeError() {
27 $pf = new PasswordFactory;
28 $pf->setDefaultType( 'bogus' );
29 }
30
31 public function testInit() {
32 $config = new HashConfig( [
33 'PasswordConfig' => [
34 'foo' => [ 'class' => InvalidPassword::class ],
35 ],
36 'PasswordDefault' => 'foo'
37 ] );
38 $pf = new PasswordFactory;
39 $pf->init( $config );
40 $this->assertSame( 'foo', $pf->getDefaultType() );
41 $this->assertArrayHasKey( 'foo', $pf->getTypes() );
42 }
43
44 public function testNewFromCiphertext() {
45 $pf = new PasswordFactory;
46 $pf->register( 'B', [ 'class' => MWSaltedPassword::class ] );
47 $pw = $pf->newFromCiphertext( ':B:salt:d529e941509eb9e9b9cfaeae1fe7ca23' );
48 $this->assertInstanceOf( MWSaltedPassword::class, $pw );
49 }
50
52 return [ [ 'blah' ], [ ':blah:' ] ];
53 }
54
59 public function testNewFromCiphertextErrors( $hash ) {
60 $pf = new PasswordFactory;
61 $pf->register( 'B', [ 'class' => MWSaltedPassword::class ] );
62 $pf->newFromCiphertext( $hash );
63 }
64
65 public function testNewFromType() {
66 $pf = new PasswordFactory;
67 $pf->register( 'B', [ 'class' => MWSaltedPassword::class ] );
68 $pw = $pf->newFromType( 'B' );
69 $this->assertInstanceOf( MWSaltedPassword::class, $pw );
70 }
71
75 public function testNewFromTypeError() {
76 $pf = new PasswordFactory;
77 $pf->register( 'B', [ 'class' => MWSaltedPassword::class ] );
78 $pf->newFromType( 'bogus' );
79 }
80
81 public function testNewFromPlaintext() {
82 $pf = new PasswordFactory;
83 $pf->register( 'A', [ 'class' => MWOldPassword::class ] );
84 $pf->register( 'B', [ 'class' => MWSaltedPassword::class ] );
85 $pf->setDefaultType( 'A' );
86
87 $this->assertInstanceOf( InvalidPassword::class, $pf->newFromPlaintext( null ) );
88 $this->assertInstanceOf( MWOldPassword::class, $pf->newFromPlaintext( 'password' ) );
89 $this->assertInstanceOf( MWSaltedPassword::class,
90 $pf->newFromPlaintext( 'password', $pf->newFromType( 'B' ) ) );
91 }
92
93 public function testNeedsUpdate() {
94 $pf = new PasswordFactory;
95 $pf->register( 'A', [ 'class' => MWOldPassword::class ] );
96 $pf->register( 'B', [ 'class' => MWSaltedPassword::class ] );
97 $pf->setDefaultType( 'A' );
98
99 $this->assertFalse( $pf->needsUpdate( $pf->newFromType( 'A' ) ) );
100 $this->assertTrue( $pf->needsUpdate( $pf->newFromType( 'B' ) ) );
101 }
102
104 $this->assertSame( 13, strlen( PasswordFactory::generateRandomPasswordString( 13 ) ) );
105 }
106
107 public function testNewInvalidPassword() {
108 $this->assertInstanceOf( InvalidPassword::class, PasswordFactory::newInvalidPassword() );
109 }
110}
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)
Initialize the internal static variables using the global variables.