MediaWiki REL1_31
PasswordTestCase.php
Go to the documentation of this file.
1<?php
26abstract class PasswordTestCase extends MediaWikiTestCase {
31
32 protected function setUp() {
33 parent::setUp();
34
35 $this->passwordFactory = new PasswordFactory();
36 foreach ( $this->getTypeConfigs() as $type => $config ) {
37 $this->passwordFactory->register( $type, $config );
38 }
39 }
40
46 abstract protected function getTypeConfigs();
47
56 public static function providePasswordTests() {
57 throw new MWException( "Not implemented" );
58 }
59
63 public function testHashing( $shouldMatch, $hash, $password ) {
64 $hash = $this->passwordFactory->newFromCiphertext( $hash );
65 $password = $this->passwordFactory->newFromPlaintext( $password, $hash );
66 $this->assertSame( $shouldMatch, $hash->equals( $password ) );
67 }
68
72 public function testStringSerialization( $shouldMatch, $hash, $password ) {
73 $hashObj = $this->passwordFactory->newFromCiphertext( $hash );
74 $serialized = $hashObj->toString();
75 $unserialized = $this->passwordFactory->newFromCiphertext( $serialized );
76 $this->assertTrue( $hashObj->equals( $unserialized ) );
77 }
78
83 public function testInvalidUnequalNormal( $shouldMatch, $hash, $password ) {
84 $invalid = $this->passwordFactory->newFromCiphertext( null );
85 $normal = $this->passwordFactory->newFromCiphertext( $hash );
86
87 $this->assertFalse( $invalid->equals( $normal ) );
88 $this->assertFalse( $normal->equals( $invalid ) );
89 }
90
91 protected function getValidTypes() {
92 return array_keys( $this->getTypeConfigs() );
93 }
94
95 public function provideTypes( $type ) {
96 $params = [];
97 foreach ( $this->getValidTypes() as $type ) {
98 $params[] = [ $type ];
99 }
100 return $params;
101 }
102
106 public function testCrypt( $type ) {
107 $fromType = $this->passwordFactory->newFromType( $type );
108 $fromType->crypt( 'password' );
109 $fromPlaintext = $this->passwordFactory->newFromPlaintext( 'password', $fromType );
110 $this->assertTrue( $fromType->equals( $fromPlaintext ) );
111 }
112}
MediaWiki exception.
Factory class for creating and checking Password objects.
PasswordFactory $passwordFactory
getTypeConfigs()
Return an array of configs to be used for this class's password type.
testHashing( $shouldMatch, $hash, $password)
providePasswordTests
static providePasswordTests()
An array of tests in the form of (bool, string, string), where the first element is whether the secon...
testInvalidUnequalNormal( $shouldMatch, $hash, $password)
providePasswordTests InvalidPassword
testCrypt( $type)
provideTypes
testStringSerialization( $shouldMatch, $hash, $password)
providePasswordTests
$params
foreach( $res as $row) $serialized