auth.php 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. /**
  3. * @group pluggable
  4. */
  5. class Tests_Auth extends WP_UnitTestCase {
  6. var $user_id;
  7. function setUp() {
  8. parent::setUp();
  9. $this->user_id = $this->factory->user->create();
  10. $_SERVER['REQUEST_METHOD'] = null;
  11. }
  12. function test_auth_cookie_valid() {
  13. $cookie = wp_generate_auth_cookie( $this->user_id, time() + 3600, 'auth' );
  14. $this->assertEquals( $this->user_id, wp_validate_auth_cookie( $cookie, 'auth' ) );
  15. }
  16. function test_auth_cookie_invalid() {
  17. // 3600 or less and +3600 may occur in wp_validate_auth_cookie(),
  18. // as an ajax test may have defined DOING_AJAX, failing the test.
  19. $cookie = wp_generate_auth_cookie( $this->user_id, time() - 7200, 'auth' );
  20. $this->assertEquals( false, wp_validate_auth_cookie( $cookie, 'auth' ), 'expired cookie' );
  21. $cookie = wp_generate_auth_cookie( $this->user_id, time() + 3600, 'auth' );
  22. $this->assertEquals( false, wp_validate_auth_cookie( $cookie, 'logged_in' ), 'wrong auth scheme' );
  23. $cookie = wp_generate_auth_cookie( $this->user_id, time() + 3600, 'auth' );
  24. list($a, $b, $c) = explode('|', $cookie);
  25. $cookie = $a . '|' . ($b + 1) . '|' . $c;
  26. $this->assertEquals( false, wp_validate_auth_cookie( $this->user_id, 'auth' ), 'altered cookie' );
  27. }
  28. function test_auth_cookie_scheme() {
  29. // arbitrary scheme name
  30. $cookie = wp_generate_auth_cookie( $this->user_id, time() + 3600, 'foo' );
  31. $this->assertEquals( $this->user_id, wp_validate_auth_cookie( $cookie, 'foo' ) );
  32. // wrong scheme name - should fail
  33. $cookie = wp_generate_auth_cookie( $this->user_id, time() + 3600, 'foo' );
  34. $this->assertEquals( false, wp_validate_auth_cookie( $cookie, 'bar' ) );
  35. }
  36. /**
  37. * @ticket 23494
  38. */
  39. function test_password_trimming() {
  40. $another_user = $this->factory->user->create( array( 'user_login' => 'password-triming-tests' ) );
  41. $passwords_to_test = array(
  42. 'a password with no trailing or leading spaces',
  43. 'a password with trailing spaces ',
  44. ' a password with leading spaces',
  45. ' a password with trailing and leading spaces ',
  46. );
  47. foreach( $passwords_to_test as $password_to_test ) {
  48. wp_set_password( $password_to_test, $another_user );
  49. $authed_user = wp_authenticate( 'password-triming-tests', $password_to_test );
  50. $this->assertInstanceOf( 'WP_User', $authed_user );
  51. $this->assertEquals( $another_user, $authed_user->ID );
  52. }
  53. }
  54. /**
  55. * Test wp_hash_password trims whitespace
  56. *
  57. * This is similar to test_password_trimming but tests the "lower level"
  58. * wp_hash_password function
  59. *
  60. * @ticket 24973
  61. */
  62. function test_wp_hash_password_trimming() {
  63. $password = ' pass with leading whitespace';
  64. $this->assertTrue( wp_check_password( 'pass with leading whitespace', wp_hash_password( $password ) ) );
  65. $password = 'pass with trailing whitespace ';
  66. $this->assertTrue( wp_check_password( 'pass with trailing whitespace', wp_hash_password( $password ) ) );
  67. $password = ' pass with whitespace ';
  68. $this->assertTrue( wp_check_password( 'pass with whitespace', wp_hash_password( $password ) ) );
  69. $password = "pass with new line \n";
  70. $this->assertTrue( wp_check_password( 'pass with new line', wp_hash_password( $password ) ) );
  71. $password = "pass with vertial tab o_O\x0B";
  72. $this->assertTrue( wp_check_password( 'pass with vertial tab o_O', wp_hash_password( $password ) ) );
  73. }
  74. }