Cleanup r61694, move blocked check to after password check, global auth checks, etc...
[mediawiki.git] / maintenance / cdb-test.php
blobb6f23c4dce49183eb1ff441a598b83f159ec9c5e
1 <?php
3 /**
4 * Test the CDB reader/writer
5 */
7 require_once( dirname(__FILE__) . '/Maintenance.php' );
9 class CdbTest extends Maintenance {
10 public function __construct() {
11 parent::__construct();
12 $this->mDescription = "CDB read/write test";
15 public function execute() {
16 $this->output( "Write test...\n" );
18 $w1 = new CdbWriter_PHP( 'php.cdb' );
19 $w2 = new CdbWriter_DBA( 'dba.cdb' );
21 $data = array();
22 for ( $i = 0; $i < 100000; $i++ ) {
23 $key = $this->randomString();
24 $value = $this->randomString();
25 $w1->set( $key, $value );
26 $w2->set( $key, $value );
28 if ( !isset( $data[$key] ) ) {
29 $data[$key] = $value;
33 $w1->close();
34 $w2->close();
36 passthru( 'md5sum php.cdb dba.cdb' );
38 $this->output( "Read test...\n" );
40 $r1 = new CdbReader_PHP( 'php.cdb' );
41 $r2 = new CdbReader_DBA( 'dba.cdb' );
43 foreach ( $data as $key => $value ) {
44 if ( $key === '' ) {
45 // Known bug
46 continue;
48 $v1 = $r1->get( $key );
49 $v2 = $r2->get( $key );
51 $v1 = $v1 === false ? '(not found)' : $v1;
52 $v2 = $v2 === false ? '(not found)' : $v2;
54 #cdbAssert( 'Mismatch', $key, $v1, $v2 );
55 $this->cdbAssert( "PHP error", $key, $v1, $value );
56 $this->cdbAssert( "DBA error", $key, $v2, $value );
58 $this->output( "Done.\n" );
61 private function randomString() {
62 $len = mt_rand( 0, 10 );
63 $s = '';
64 for ( $j = 0; $j < $len; $j++ ) {
65 $s .= chr( mt_rand( 0, 255 ) );
67 return $s;
70 private function cdbAssert( $msg, $key, $v1, $v2 ) {
71 if ( $v1 !== $v2 ) {
72 $this->output( $msg . ', k=' . bin2hex( $key ) .
73 ', v1=' . bin2hex( $v1 ) .
74 ', v2=' . bin2hex( $v2 ) . "\n" );
75 return false;
76 } else {
77 return true;
82 $maintClass = "CdbTest";
83 require_once( DO_MAINTENANCE );