rdbms: make getCPInfoFromCookieValue() stricter about allowed values
[lhc/web/wiklou.git] / tests / phpunit / includes / db / LBFactoryTest.php
index 6e23e53..82ca66a 100644 (file)
@@ -393,6 +393,8 @@ class LBFactoryTest extends MediaWikiTestCase {
                $cp->shutdown( null, 'sync', $cpIndex );
 
                $this->assertEquals( null, $cpIndex, "CP write index retained" );
+
+               $this->assertEquals( '45e93a9c215c031d38b7c42d8e4700ca', $cp->getClientId() );
        }
 
        private function newLBFactoryMulti( array $baseOverride = [], array $serverOverride = [] ) {
@@ -419,7 +421,8 @@ class LBFactoryTest extends MediaWikiTestCase {
                                'test-db1' => $wgDBserver,
                        ],
                        'loadMonitorClass' => LoadMonitorNull::class,
-                       'localDomain' => new DatabaseDomain( $wgDBname, null, $wgDBprefix )
+                       'localDomain' => new DatabaseDomain( $wgDBname, null, $wgDBprefix ),
+                       'agent' => 'MW-UNIT-TESTS'
                ] );
        }
 
@@ -606,4 +609,74 @@ class LBFactoryTest extends MediaWikiTestCase {
                        return $db->addIdentifierQuotes( $table );
                }
        }
+
+       /**
+        * @covers \Wikimedia\Rdbms\LBFactory::makeCookieValueFromCPIndex()
+        * @covers \Wikimedia\Rdbms\LBFactory::getCPInfoFromCookieValue()
+        */
+       public function testCPPosIndexCookieValues() {
+               $time = 1526522031;
+               $agentId = md5( 'Ramsey\'s Loyal Presa Canario' );
+
+               $lbFactory = $this->newLBFactoryMulti();
+               $this->assertEquals(
+                       '3@542#c47dcfb0566e7d7bc110a6128a45c93a',
+                       LBFactory::makeCookieValueFromCPIndex( 3, 542, $agentId )
+               );
+
+               $lbFactory = $this->newLBFactoryMulti();
+               $lbFactory->setRequestInfo( [ 'IPAddress' => '10.64.24.52', 'UserAgent' => 'meow' ] );
+               $this->assertEquals(
+                       '1@542#c47dcfb0566e7d7bc110a6128a45c93a',
+                       LBFactory::makeCookieValueFromCPIndex( 1, 542, $agentId )
+               );
+
+               $this->assertSame(
+                       null,
+                       LBFactory::getCPInfoFromCookieValue( "5#$agentId", $time - 10 )['index'],
+                       'No time set'
+               );
+               $this->assertSame(
+                       null,
+                       LBFactory::getCPInfoFromCookieValue( "5@$time", $time - 10 )['index'],
+                       'No agent set'
+               );
+               $this->assertSame(
+                       null,
+                       LBFactory::getCPInfoFromCookieValue( "0@$time#$agentId", $time - 10 )['index'],
+                       'Bad index'
+               );
+
+               $this->assertSame(
+                       2,
+                       LBFactory::getCPInfoFromCookieValue( "2@$time#$agentId", $time - 10 )['index'],
+                       'Fresh'
+               );
+               $this->assertSame(
+                       2,
+                       LBFactory::getCPInfoFromCookieValue( "2@$time#$agentId", $time + 9 - 10 )['index'],
+                       'Almost stale'
+               );
+               $this->assertSame(
+                       null,
+                       LBFactory::getCPInfoFromCookieValue( "0@$time#$agentId", $time + 9 - 10 )['index'],
+                       'Almost stale; bad index'
+               );
+               $this->assertSame(
+                       null,
+                       LBFactory::getCPInfoFromCookieValue( "2@$time#$agentId", $time + 11 - 10 )['index'],
+                       'Stale'
+               );
+
+               $this->assertSame(
+                       $agentId,
+                       LBFactory::getCPInfoFromCookieValue( "5@$time#$agentId", $time - 10 )['clientId'],
+                       'Live (client ID)'
+               );
+               $this->assertSame(
+                       null,
+                       LBFactory::getCPInfoFromCookieValue( "5@$time#$agentId", $time + 11 - 10 )['clientId'],
+                       'Stale (client ID)'
+               );
+       }
 }