View source
<?php
namespace Drupal\Tests\acquia_search\Unit;
use Drupal\acquia_connector\CryptConnector;
use Drupal\acquia_search\EventSubscriber\SearchSubscriber;
use Drupal\Tests\UnitTestCase;
class AcquiaSearchTest extends UnitTestCase {
protected $id;
protected $key;
protected $salt;
protected $derivedKey;
protected $searchSubscriber;
protected function setUp() {
$this->id = $this
->randomMachineName(10);
$this->key = $this
->randomMachineName(32);
$this->salt = $this
->randomMachineName(32);
$dirs = drupal_phpunit_contrib_extension_directory_roots();
$extensions = [];
foreach ($dirs as $path) {
$extensions += drupal_phpunit_find_extension_directories($path);
}
unset($extensions);
$this->searchSubscriber = new SearchSubscriber();
$this->derivedKey = CryptConnector::createDerivedKey($this->salt, $this->id, $this->key);
}
public function testCreateDerivedKey() {
$derivation_string = $this->id . 'solr' . $this->salt;
$derived_key = hash_hmac('sha1', str_pad($derivation_string, 80, $derivation_string), $this->key);
$this
->assertEquals($derived_key, $this->derivedKey);
}
public function testCalculateAuthCookie() {
$time = 1577635946;
$nonce = $this
->randomMachineName(32);
$string = $time . $nonce . $this
->randomMachineName();
$hmac = hash_hmac('sha1', $time . $nonce . $string, $this->derivedKey);
$calculateAuthCookie = $this
->getMockBuilder('Drupal\\acquia_search\\EventSubscriber\\SearchSubscriber')
->setMethods([
'getDerivedKey',
])
->getMock();
$calculateAuthCookie
->expects($this
->any())
->method('getDerivedKey')
->willReturn($this->derivedKey);
$authenticator = $calculateAuthCookie
->calculateAuthCookie($string, $nonce, $time, $this->derivedKey, $time);
preg_match('/acquia_solr_hmac=([a-zA-Z0-9]{40});/', $authenticator, $matches);
$this
->assertEquals($hmac, $matches[1], 'HMAC API function generates the expected hmac hash.');
preg_match('/acquia_solr_time=([0-9]{10});/', $authenticator, $matches);
$this
->assertNotNull($matches, 'HMAC API function generates a timestamp.', 'Acquia Search');
preg_match('/acquia_solr_nonce=([a-zA-Z0-9]{32});/', $authenticator, $matches);
$this
->assertEquals($nonce, $matches[1], 'HMAC API function generates the expected nonce.');
}
public function testValidResponse() {
$nonce = $this
->randomMachineName(32);
$string = $this
->randomMachineName(32);
$hmac = hash_hmac('sha1', $nonce . $string, $this->derivedKey);
$valid = $this->searchSubscriber
->validateResponse($hmac, $nonce, $string, $this->derivedKey);
$this
->assertTrue($valid, 'Response flagged as valid when the expected hash is passed.');
$bad_hmac = $hmac . 'invalidateHash';
$invalid_hmac = $this->searchSubscriber
->validateResponse($bad_hmac, $nonce, $string, $this->derivedKey);
$this
->assertFalse($invalid_hmac, 'Response flagged as invalid when a malformed hash is passed.');
$bad_nonce = $nonce . 'invalidateString';
$invalid_nonce = $this->searchSubscriber
->validateResponse($hmac, $bad_nonce, $string, $this->derivedKey);
$this
->assertFalse($invalid_nonce, 'Response flagged as invalid when a malformed nonce is passed.');
$bad_string = $string . 'invalidateString';
$invalid_string = $this->searchSubscriber
->validateResponse($hmac, $nonce, $bad_string, $this->derivedKey);
$this
->assertFalse($invalid_string, 'Response flagged as invalid when a malformed string is passed.');
$bad_key = $this->derivedKey . 'invalidateKey';
$invalid_key = $this->searchSubscriber
->validateResponse($hmac, $nonce, $string, $bad_key);
$this
->assertFalse($invalid_key, 'Response flagged as invalid when a malformed derived key is passed.');
}
public function testExtractHmacHeader() {
$nonce = $this
->randomMachineName(32);
$string = $this
->randomMachineName(32);
$hmac = hash_hmac('sha1', $nonce . $string, $this->derivedKey);
$headers = [
'pragma/hmac_digest=' . $hmac . ';',
];
$extracted = $this->searchSubscriber
->extractHmac($headers);
$this
->assertEquals($hmac, $extracted, 'The HMAC digest was extracted from the response header.');
$bad_headers1 = [
'pragma/' . $this
->randomMachineName(),
];
$bad_extracted1 = $this->searchSubscriber
->extractHmac($bad_headers1);
$this
->assertEquals('', $bad_extracted1, 'Empty string returned by HMAC extraction function when an invalid pragma is passed.');
$bad_extracted2 = $this->searchSubscriber
->extractHmac($this
->randomMachineName());
$this
->assertEquals('', $bad_extracted2, 'Empty string returned by HMAC extraction function when an invalid header is passed.');
}
}