MediaWiki REL1_32
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
55 public static function providePasswordTests() {
56 throw new MWException( "Not implemented" );
57 }
58
62 public function testHashing( $shouldMatch, $hash, $password ) {
63 $hash = $this->passwordFactory->newFromCiphertext( $hash );
64 $password = $this->passwordFactory->newFromPlaintext( $password, $hash );
65 $this->assertSame( $shouldMatch, $hash->equals( $password ) );
66 }
67
71 public function testStringSerialization( $shouldMatch, $hash, $password ) {
72 $hashObj = $this->passwordFactory->newFromCiphertext( $hash );
73 $serialized = $hashObj->toString();
74 $unserialized = $this->passwordFactory->newFromCiphertext( $serialized );
75 $this->assertTrue( $hashObj->equals( $unserialized ) );
76 }
77
82 public function testInvalidUnequalNormal( $shouldMatch, $hash, $password ) {
83 $invalid = $this->passwordFactory->newFromCiphertext( null );
84 $normal = $this->passwordFactory->newFromCiphertext( $hash );
85
86 $this->assertFalse( $invalid->equals( $normal ) );
87 $this->assertFalse( $normal->equals( $invalid ) );
88 }
89
90 protected function getValidTypes() {
91 return array_keys( $this->getTypeConfigs() );
92 }
93
94 public function provideTypes( $type ) {
95 $params = [];
96 foreach ( $this->getValidTypes() as $type ) {
97 $params[] = [ $type ];
98 }
99 return $params;
100 }
101
105 public function testCrypt( $type ) {
106 $fromType = $this->passwordFactory->newFromType( $type );
107 $fromType->crypt( 'password' );
108 $fromPlaintext = $this->passwordFactory->newFromPlaintext( 'password', $fromType );
109 $this->assertTrue( $fromType->equals( $fromPlaintext ) );
110 }
111}
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