summaryrefslogtreecommitdiff
path: root/www/wiki/tests/phpunit/includes/session/TokenTest.php
blob: 47976527ca281c0ab6eb9b1be8466bf26017ef64 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php

namespace MediaWiki\Session;

use MediaWikiTestCase;
use Wikimedia\TestingAccessWrapper;

/**
 * @group Session
 * @covers MediaWiki\Session\Token
 */
class TokenTest extends MediaWikiTestCase {

	public function testBasics() {
		$token = $this->getMockBuilder( Token::class )
			->setMethods( [ 'toStringAtTimestamp' ] )
			->setConstructorArgs( [ 'sekret', 'salty', true ] )
			->getMock();
		$token->expects( $this->any() )->method( 'toStringAtTimestamp' )
			->will( $this->returnValue( 'faketoken+\\' ) );

		$this->assertSame( 'faketoken+\\', $token->toString() );
		$this->assertSame( 'faketoken+\\', (string)$token );
		$this->assertTrue( $token->wasNew() );

		$token = new Token( 'sekret', 'salty', false );
		$this->assertFalse( $token->wasNew() );
	}

	public function testToStringAtTimestamp() {
		$token = TestingAccessWrapper::newFromObject( new Token( 'sekret', 'salty', false ) );

		$this->assertSame(
			'd9ade0c7d4349e9df9094e61c33a5a0d5644fde2+\\',
			$token->toStringAtTimestamp( 1447362018 )
		);
		$this->assertSame(
			'ee2f7a2488dea9176c224cfb400d43be5644fdea+\\',
			$token->toStringAtTimestamp( 1447362026 )
		);
	}

	public function testGetTimestamp() {
		$this->assertSame(
			1447362018, Token::getTimestamp( 'd9ade0c7d4349e9df9094e61c33a5a0d5644fde2+\\' )
		);
		$this->assertSame(
			1447362026, Token::getTimestamp( 'ee2f7a2488dea9176c224cfb400d43be5644fdea+\\' )
		);
		$this->assertNull( Token::getTimestamp( 'ee2f7a2488dea9176c224cfb400d43be5644fdea-\\' ) );
		$this->assertNull( Token::getTimestamp( 'ee2f7a2488dea9176c224cfb400d43be+\\' ) );

		$this->assertNull( Token::getTimestamp( 'ee2f7a2488dea9x76c224cfb400d43be5644fdea+\\' ) );
	}

	public function testMatch() {
		$token = TestingAccessWrapper::newFromObject( new Token( 'sekret', 'salty', false ) );

		$test = $token->toStringAtTimestamp( time() - 10 );
		$this->assertTrue( $token->match( $test ) );
		$this->assertTrue( $token->match( $test, 12 ) );
		$this->assertFalse( $token->match( $test, 8 ) );

		$this->assertFalse( $token->match( 'ee2f7a2488dea9176c224cfb400d43be5644fdea-\\' ) );
	}

}