File: PasswordFactoryTest.php

package info (click to toggle)
mediawiki 1%3A1.35.13-1%2Bdeb11u2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 274,932 kB
  • sloc: php: 677,563; javascript: 572,709; sql: 11,565; python: 4,447; xml: 3,145; sh: 892; perl: 788; ruby: 496; pascal: 365; makefile: 128
file content (122 lines) | stat: -rw-r--r-- 4,073 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php

/**
 * @covers PasswordFactory
 */
class PasswordFactoryTest extends MediaWikiUnitTestCase {
	public function testConstruct() {
		$pf = new PasswordFactory();
		$this->assertEquals( [ '' ], array_keys( $pf->getTypes() ) );
		$this->assertSame( '', $pf->getDefaultType() );

		$pf = new PasswordFactory( [
			'foo' => [ 'class' => 'FooPassword' ],
			'bar' => [ 'class' => 'BarPassword', 'baz' => 'boom' ],
		], 'foo' );
		$this->assertEquals( [ '', 'foo', 'bar' ], array_keys( $pf->getTypes() ) );
		$bar = $pf->getTypes()['bar'];
		$expected = [ 'class' => 'BarPassword', 'baz' => 'boom' ];
		$this->assertArrayEquals( $expected, array_intersect( $bar, $expected ) );
		$this->assertEquals( 'foo', $pf->getDefaultType() );
	}

	public function testRegister() {
		$pf = new PasswordFactory;
		$pf->register( 'foo', [ 'class' => InvalidPassword::class ] );
		$this->assertArrayHasKey( 'foo', $pf->getTypes() );
	}

	public function testSetDefaultType() {
		$pf = new PasswordFactory;
		$pf->register( '1', [ 'class' => InvalidPassword::class ] );
		$pf->register( '2', [ 'class' => InvalidPassword::class ] );
		$pf->setDefaultType( '1' );
		$this->assertSame( '1', $pf->getDefaultType() );
		$pf->setDefaultType( '2' );
		$this->assertSame( '2', $pf->getDefaultType() );
	}

	public function testSetDefaultTypeError() {
		$pf = new PasswordFactory;
		$this->expectException( Exception::class );
		$pf->setDefaultType( 'bogus' );
	}

	public function testInit() {
		$config = new HashConfig( [
			'PasswordConfig' => [
				'foo' => [ 'class' => InvalidPassword::class ],
			],
			'PasswordDefault' => 'foo'
		] );
		$pf = new PasswordFactory;
		$pf->init( $config );
		$this->assertSame( 'foo', $pf->getDefaultType() );
		$this->assertArrayHasKey( 'foo', $pf->getTypes() );
	}

	public function testNewFromCiphertext() {
		$pf = new PasswordFactory;
		$pf->register( 'B', [ 'class' => MWSaltedPassword::class ] );
		$pw = $pf->newFromCiphertext( ':B:salt:d529e941509eb9e9b9cfaeae1fe7ca23' );
		$this->assertInstanceOf( MWSaltedPassword::class, $pw );
	}

	public function provideNewFromCiphertextErrors() {
		return [ [ 'blah' ], [ ':blah:' ] ];
	}

	/**
	 * @dataProvider provideNewFromCiphertextErrors
	 */
	public function testNewFromCiphertextErrors( $hash ) {
		$pf = new PasswordFactory;
		$pf->register( 'B', [ 'class' => MWSaltedPassword::class ] );
		$this->expectException( PasswordError::class );
		$pf->newFromCiphertext( $hash );
	}

	public function testNewFromType() {
		$pf = new PasswordFactory;
		$pf->register( 'B', [ 'class' => MWSaltedPassword::class ] );
		$pw = $pf->newFromType( 'B' );
		$this->assertInstanceOf( MWSaltedPassword::class, $pw );
	}

	public function testNewFromTypeError() {
		$pf = new PasswordFactory;
		$pf->register( 'B', [ 'class' => MWSaltedPassword::class ] );
		$this->expectException( PasswordError::class );
		$pf->newFromType( 'bogus' );
	}

	public function testNewFromPlaintext() {
		$pf = new PasswordFactory;
		$pf->register( 'A', [ 'class' => MWOldPassword::class ] );
		$pf->register( 'B', [ 'class' => MWSaltedPassword::class ] );
		$pf->setDefaultType( 'A' );

		$this->assertInstanceOf( InvalidPassword::class, $pf->newFromPlaintext( null ) );
		$this->assertInstanceOf( MWOldPassword::class, $pf->newFromPlaintext( 'password' ) );
		$this->assertInstanceOf( MWSaltedPassword::class,
			$pf->newFromPlaintext( 'password', $pf->newFromType( 'B' ) ) );
	}

	public function testNeedsUpdate() {
		$pf = new PasswordFactory;
		$pf->register( 'A', [ 'class' => MWOldPassword::class ] );
		$pf->register( 'B', [ 'class' => MWSaltedPassword::class ] );
		$pf->setDefaultType( 'A' );

		$this->assertFalse( $pf->needsUpdate( $pf->newFromType( 'A' ) ) );
		$this->assertTrue( $pf->needsUpdate( $pf->newFromType( 'B' ) ) );
	}

	public function testGenerateRandomPasswordString() {
		$this->assertSame( 13, strlen( PasswordFactory::generateRandomPasswordString( 13 ) ) );
	}

	public function testNewInvalidPassword() {
		$this->assertInstanceOf( InvalidPassword::class, PasswordFactory::newInvalidPassword() );
	}
}