| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
use PHPUnit\Framework\TestCase; |
| 25 |
use Wikimedia\ObjectCache\HashBagOStuff; |
| 26 |
use Wikimedia\Rdbms\ChronologyProtector; |
| 27 |
use Wikimedia\Rdbms\ILoadBalancer; |
| 28 |
use Wikimedia\Rdbms\MySQLPrimaryPos; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
class ChronologyProtectorTest extends TestCase { |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
public function testClientId( array $client, $secret, $expectedId ) { |
| 41 |
$bag = new HashBagOStuff(); |
| 42 |
$cp = new ChronologyProtector( $bag, $secret, false ); |
| 43 |
$cp->setRequestInfo( $client ); |
| 44 |
|
| 45 |
$this->assertEquals( $expectedId, $cp->getClientId() ); |
| 46 |
} |
| 47 |
|
| 48 |
public static function clientIdProvider() { |
| 49 |
return [ |
| 50 |
[ |
| 51 |
[ |
| 52 |
'IPAddress' => '127.0.0.1', |
| 53 |
'UserAgent' => "Totally-Not-FireFox" |
| 54 |
], |
| 55 |
'', |
| 56 |
'45e93a9c215c031d38b7c42d8e4700ca', |
| 57 |
], |
| 58 |
[ |
| 59 |
[ |
| 60 |
'IPAddress' => '127.0.0.7', |
| 61 |
'UserAgent' => "Totally-Not-FireFox" |
| 62 |
], |
| 63 |
'', |
| 64 |
'b1d604117b51746c35c3df9f293c84dc' |
| 65 |
], |
| 66 |
[ |
| 67 |
[ |
| 68 |
'IPAddress' => '127.0.0.1', |
| 69 |
'UserAgent' => "Totally-FireFox" |
| 70 |
], |
| 71 |
'', |
| 72 |
'731b4e06a65e2346b497fc811571c4d7' |
| 73 |
], |
| 74 |
[ |
| 75 |
[ |
| 76 |
'IPAddress' => '127.0.0.1', |
| 77 |
'UserAgent' => "Totally-Not-FireFox" |
| 78 |
], |
| 79 |
'secret', |
| 80 |
'defff51ded73cd901253d874c9b2077d' |
| 81 |
] |
| 82 |
]; |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
public function testPositionMarshalling() { |
| 90 |
$replicationPos = '1-2-3'; |
| 91 |
$time = 100; |
| 92 |
|
| 93 |
$lb = $this->createMock( ILoadBalancer::class ); |
| 94 |
$lb->method( 'getClusterName' )->willReturn( 'test' ); |
| 95 |
$lb->method( 'getServerName' )->willReturn( 'primary' ); |
| 96 |
$lb->method( 'hasOrMadeRecentPrimaryChanges' )->willReturn( true ); |
| 97 |
$lb->method( 'hasStreamingReplicaServers' )->willReturn( true ); |
| 98 |
$lb->method( 'getPrimaryPos' )->willReturnCallback( |
| 99 |
static function () use ( &$replicationPos, &$time ) { |
| 100 |
return new MySQLPrimaryPos( $replicationPos, $time ); |
| 101 |
} |
| 102 |
); |
| 103 |
|
| 104 |
$client = [ |
| 105 |
'IPAddress' => '127.0.0.1', |
| 106 |
'UserAgent' => "Burninator", |
| 107 |
'ChronologyClientId' => 'random id' |
| 108 |
]; |
| 109 |
|
| 110 |
$secret = '0815'; |
| 111 |
|
| 112 |
$bag = new HashBagOStuff(); |
| 113 |
$cp = new ChronologyProtector( $bag, $secret, false ); |
| 114 |
$cp->setRequestInfo( $client ); |
| 115 |
|
| 116 |
$clientPostIndex = 0; |
| 117 |
$cp->stageSessionPrimaryPos( $lb ); |
| 118 |
$cp->persistSessionReplicationPositions( $clientPostIndex ); |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
$replicationPos = '3-4-5'; |
| 123 |
$time++; |
| 124 |
$cp->stageSessionPrimaryPos( $lb ); |
| 125 |
$cp->persistSessionReplicationPositions( $clientPostIndex ); |
| 126 |
|
| 127 |
$waitForPos = $cp->getSessionPrimaryPos( $lb ); |
| 128 |
$this->assertNotNull( $waitForPos ); |
| 129 |
$this->assertSame( $time, $waitForPos->asOfTime() ); |
| 130 |
$this->assertSame( "$replicationPos", "$waitForPos" ); |
| 131 |
} |
| 132 |
|
| 133 |
public function testCPPosIndexCookieValues() { |
| 134 |
$time = 1526522031; |
| 135 |
$agentId = md5( 'Ramsey\'s Loyal Presa Canario' ); |
| 136 |
|
| 137 |
$this->assertEquals( |
| 138 |
'3@542#c47dcfb0566e7d7bc110a6128a45c93a', |
| 139 |
ChronologyProtector::makeCookieValueFromCPIndex( 3, 542, $agentId ) |
| 140 |
); |
| 141 |
|
| 142 |
$this->assertEquals( |
| 143 |
'1@542#c47dcfb0566e7d7bc110a6128a45c93a', |
| 144 |
ChronologyProtector::makeCookieValueFromCPIndex( 1, 542, $agentId ) |
| 145 |
); |
| 146 |
|
| 147 |
$this->assertSame( |
| 148 |
null, |
| 149 |
ChronologyProtector::getCPInfoFromCookieValue( "5#$agentId", $time - 10 )['index'], |
| 150 |
'No time set' |
| 151 |
); |
| 152 |
$this->assertSame( |
| 153 |
null, |
| 154 |
ChronologyProtector::getCPInfoFromCookieValue( "5@$time", $time - 10 )['index'], |
| 155 |
'No agent set' |
| 156 |
); |
| 157 |
$this->assertSame( |
| 158 |
null, |
| 159 |
ChronologyProtector::getCPInfoFromCookieValue( "0@$time#$agentId", $time - 10 )['index'], |
| 160 |
'Bad index' |
| 161 |
); |
| 162 |
|
| 163 |
$this->assertSame( |
| 164 |
2, |
| 165 |
ChronologyProtector::getCPInfoFromCookieValue( "2@$time#$agentId", $time - 10 )['index'], |
| 166 |
'Fresh' |
| 167 |
); |
| 168 |
$this->assertSame( |
| 169 |
2, |
| 170 |
ChronologyProtector::getCPInfoFromCookieValue( "2@$time#$agentId", $time + 9 - 10 )['index'], |
| 171 |
'Almost stale' |
| 172 |
); |
| 173 |
$this->assertSame( |
| 174 |
null, |
| 175 |
ChronologyProtector::getCPInfoFromCookieValue( "0@$time#$agentId", $time + 9 - 10 )['index'], |
| 176 |
'Almost stale; bad index' |
| 177 |
); |
| 178 |
$this->assertSame( |
| 179 |
null, |
| 180 |
ChronologyProtector::getCPInfoFromCookieValue( "2@$time#$agentId", $time + 11 - 10 )['index'], |
| 181 |
'Stale' |
| 182 |
); |
| 183 |
|
| 184 |
$this->assertSame( |
| 185 |
$agentId, |
| 186 |
ChronologyProtector::getCPInfoFromCookieValue( "5@$time#$agentId", $time - 10 )['clientId'], |
| 187 |
'Live (client ID)' |
| 188 |
); |
| 189 |
$this->assertSame( |
| 190 |
null, |
| 191 |
ChronologyProtector::getCPInfoFromCookieValue( "5@$time#$agentId", $time + 11 - 10 )['clientId'], |
| 192 |
'Stale (client ID)' |
| 193 |
); |
| 194 |
} |
| 195 |
} |