You are here

function AcquiaSearchTest::testHMAC in Acquia Connector 7

File

acquia_search/tests/acquia_search.test, line 20

Class

AcquiaSearchTest

Code

function testHMAC() {
  $this
    ->DrupalVariableSet('acquia_identifier', 'valid_identifier');
  $this
    ->DrupalVariableSet('acquia_key', 'valid_key');
  $this
    ->DrupalVariableSet('acquia_subscription_data', array(
    'active' => TRUE,
  ));
  $key = $this
    ->randomName();
  $time = REQUEST_TIME;
  $nonce = md5($this
    ->randomName());
  $string = $time . $nonce . $this
    ->randomName();
  $hash1 = hash_hmac('sha1', $string, $key);
  $hash2 = _acquia_search_hmac($key, $string);

  // The results of these two hmac function must be equal if
  // ours is correct.  We don't use the PHP built-in since for
  // the module since it may be missing depending on compile
  // options.
  $this
    ->assertEqual($hash1, $hash2, 'Same result using hash_hmac() and _acquia_search_hmac().');
  $derived_key = _acquia_search_derived_key();

  // The response is cehcked using the derived key, so construct another hmac.
  $hash3 = hash_hmac('sha1', $nonce . $string, $derived_key);
  $this
    ->assertTrue(acquia_search_valid_response($hash3, $nonce, $string), 'Response HMAC would be accepted as valid.');
}