markTestSkipped( 'PHP must be compiled with --enable-bcmath or --with-gmp.' ); } } public function tearDown() { } public function testCreateLibraryWrongValue() { try { $lib = ezcAuthenticationMath::createBignumLibrary( 'wrong value' ); $this->fail( "Expected exception was not thrown." ); } catch ( ezcBaseValueException $e ) { $this->assertEquals( "The value 'wrong value' that you were trying to assign to setting 'library' is invalid. Allowed values are: \"gmp\" || \"bcmath\" || null.", $e->getMessage() ); } } public function testApr1() { $val = 'asdfgh'; $salt = md5( $val ); $result = ezcAuthenticationMath::apr1( $val, $salt ); $hashFromHtpasswd = '$apr1$a152e841$qDLZqN37bdBiQGa7SnsIM1'; $this->assertEquals( $result, $hashFromHtpasswd ); $this->assertEquals( true, ezcAuthenticationMath::apr1( $val, $hashFromHtpasswd ) === $hashFromHtpasswd ); } public function testGmpTables() { if ( !ezcBaseFeatures::hasExtensionSupport( 'gmp' ) ) { $this->markTestSkipped( 'PHP must be compiled with --with-gmp.' ); } $lib = ezcAuthenticationMath::createBignumLibrary( 'gmp' ); foreach ( self::$tables as $table ) { foreach ( self::$$table as $i => $line ) { $result = $lib->toString( $lib->$table( $lib->init( $line[0] ), ( $table === 'pow' ) ? $line[1] : $lib->init( $line[1] ) ) ); $expected = $lib->toString( $lib->init( $line[2] ) ); $message = get_class( $lib ) . ": {$table}( {$line[0]}, {$line[1]} ) produced {$result}, expected {$expected}."; $this->assertEquals( $expected, $result, $message ); } } } public function testGmpPowmod() { if ( !ezcBaseFeatures::hasExtensionSupport( 'gmp' ) ) { $this->markTestSkipped( 'PHP must be compiled with --with-gmp.' ); } $lib = ezcAuthenticationMath::createBignumLibrary( 'gmp' ); foreach ( self::$powmod as $i => $line ) { $result = $lib->toString( $lib->powmod( $lib->init( $line[0] ), $lib->init( $line[1] ), $lib->init( $line[2] ) ) ); $expected = $lib->toString( $lib->init( $line[3] ) ); $message = get_class( $lib ) . ": powmod( {$line[0]}, {$line[1]}, {$line[2]} ) produced {$result}, expected {$expected}."; $this->assertEquals( $expected, $result, $message ); } } public function testGmpGcd() { if ( !ezcBaseFeatures::hasExtensionSupport( 'gmp' ) ) { $this->markTestSkipped( 'PHP must be compiled with --with-gmp.' ); } $lib = ezcAuthenticationMath::createBignumLibrary( 'gmp' ); foreach ( self::$gcd as $i => $line ) { $result = $lib->gcd( $lib->init( $line[0] ), $lib->init( $line[1] ) ); $result = array( $lib->toString( $result[0] ), $lib->toString( $result[1] ), $lib->toString( $result[2] ) ); $expected = $line[2]; $message = get_class( $lib ) . ": gcd( {$line[0]}, {$line[1]} ) produced (" . implode( ',', $result ) . "), expected (" . implode( ',', $expected ) . ")."; $this->assertEquals( $expected, $result, $message ); } } public function testGmpBtwoc() { if ( !ezcBaseFeatures::hasExtensionSupport( 'gmp' ) ) { $this->markTestSkipped( 'PHP must be compiled with --with-gmp.' ); } $lib = ezcAuthenticationMath::createBignumLibrary( 'gmp' ); $n = $lib->binToDec( $lib->btwoc( self::$p ) ); $this->assertEquals( self::$p, $n ); } public function testGmpBtwocNegative() { if ( !ezcBaseFeatures::hasExtensionSupport( 'gmp' ) ) { $this->markTestSkipped( 'PHP must be compiled with --with-gmp.' ); } $lib = ezcAuthenticationMath::createBignumLibrary( 'gmp' ); $n = $lib->btwoc( -1 ); $this->assertEquals( null, $n ); } public function testGmpBtwocZero() { if ( !ezcBaseFeatures::hasExtensionSupport( 'gmp' ) ) { $this->markTestSkipped( 'PHP must be compiled with --with-gmp.' ); } $lib = ezcAuthenticationMath::createBignumLibrary( 'gmp' ); $n = $lib->btwoc( 0 ); $this->assertEquals( "\x00", $n ); } public function testBcmathTables() { if ( !ezcBaseFeatures::hasExtensionSupport( 'bcmath' ) ) { $this->markTestSkipped( 'PHP must be compiled with --enable-bcmath.' ); } $lib = ezcAuthenticationMath::createBignumLibrary( 'bcmath' ); foreach ( self::$tables as $table ) { foreach ( self::$$table as $i => $line ) { $result = $lib->toString( $lib->$table( $lib->init( $line[0] ), $lib->init( $line[1] ) ) ); $expected = $lib->toString( $lib->init( $line[2] ) ); $message = get_class( $lib ) . ": {$table}( {$line[0]}, {$line[1]} ) produced {$result}, expected {$expected}."; $this->assertEquals( $expected, $result, $message ); } } } public function testBcmathPowmod() { if ( !ezcBaseFeatures::hasExtensionSupport( 'bcmath' ) ) { $this->markTestSkipped( 'PHP must be compiled with --enable-bcmath.' ); } $lib = ezcAuthenticationMath::createBignumLibrary( 'bcmath' ); foreach ( self::$powmod as $i => $line ) { $result = $lib->toString( $lib->powmod( $lib->init( $line[0] ), $lib->init( $line[1] ), $lib->init( $line[2] ) ) ); $expected = $lib->toString( $lib->init( $line[3] ) ); $message = get_class( $lib ) . ": powmod( {$line[0]}, {$line[1]}, {$line[2]} ) produced {$result}, expected {$expected}."; $this->assertEquals( $expected, $result, $message ); } } public function testBcmathGcd() { if ( !ezcBaseFeatures::hasExtensionSupport( 'bcmath' ) ) { $this->markTestSkipped( 'PHP must be compiled with --enable-bcmath.' ); } $lib = ezcAuthenticationMath::createBignumLibrary( 'bcmath' ); foreach ( self::$gcd as $i => $line ) { $result = $lib->gcd( $lib->init( $line[0] ), $lib->init( $line[1] ) ); $result = array( $lib->toString( $result[0] ), $lib->toString( $result[1] ), $lib->toString( $result[2] ) ); $expected = $line[2]; $message = get_class( $lib ) . ": gcd( {$line[0]}, {$line[1]} ) produced (" . implode( ',', $result ) . "), expected (" . implode( ',', $expected ) . ")."; $this->assertEquals( $expected, $result, $message ); } } public function testBcmathBtwoc() { if ( !ezcBaseFeatures::hasExtensionSupport( 'bcmath' ) ) { $this->markTestSkipped( 'PHP must be compiled with --enable-bcmath.' ); } $lib = ezcAuthenticationMath::createBignumLibrary( 'bcmath' ); $n = $lib->binToDec( $lib->btwoc( self::$p ) ); $this->assertEquals( self::$p, $n ); } public function testBcmathBtwocNegative() { if ( !ezcBaseFeatures::hasExtensionSupport( 'bcmath' ) ) { $this->markTestSkipped( 'PHP must be compiled with --enable-bcmath.' ); } $lib = ezcAuthenticationMath::createBignumLibrary( 'bcmath' ); $n = $lib->btwoc( -1 ); $this->assertEquals( null, $n ); } public function testBcmathBtwocZero() { if ( !ezcBaseFeatures::hasExtensionSupport( 'bcmath' ) ) { $this->markTestSkipped( 'PHP must be compiled with --enable-bcmath.' ); } $lib = ezcAuthenticationMath::createBignumLibrary( 'bcmath' ); $n = $lib->btwoc( 0 ); $this->assertEquals( "\x00", $n ); } } ?>