You are here

class AcquiaSearchUnitTestCase in Acquia Search 6.3

Unit tests of the functionality of the Acquia Search module.

Hierarchy

Expanded class hierarchy of AcquiaSearchUnitTestCase

File

tests/acquia_search.test, line 13

View source
class AcquiaSearchUnitTestCase extends DrupalUnitTestCase {
  protected $id;
  protected $key;
  protected $salt;
  protected $derivedKey;
  public static function getInfo() {
    return array(
      'name' => 'Acquia Search unit tests',
      'description' => 'Tests the low level Acquia Search functions.',
      'group' => 'Acquia',
    );
  }

  /**
   * Overrides DrupalTestCase::setUp().
   */
  public function setUp() {
    parent::setUp();
    require_once dirname(dirname(__FILE__)) . '/acquia_search.module';

    // Generate and store a random set of credentials.
    // Make them as close to the production values as possible
    // Something like AAAA-1234
    $this->id = $this
      ->randomAcquiaSearchName(10);

    // Most of the keys and salts have a 32char lenght
    $this->key = $this
      ->randomAcquiaSearchName(32);
    $this->salt = $this
      ->randomAcquiaSearchName(32);

    // Create a derived key from these values
    $this->derivedKey = _acquia_search_create_derived_key($this->salt, $this->id, $this->key);
  }

  /**
   * Tests derived key generation.
   */
  public function testDerivedKey() {

    // Mimic the hashing code in the API function.
    $derivation_string = $this->id . 'solr' . $this->salt;

    // str_pad extends the string with the same string in this case
    // until it has filled 80 chars.
    $derived_key = hash_hmac('sha1', str_pad($derivation_string, 80, $derivation_string), $this->key);

    // $this->derivedKey is generated from the API function.
    // @see setUp()
    $this
      ->assertEqual($this->derivedKey, $derived_key, t('Derived key API function generates the expected hash.'), 'Acquia Search');
  }

  /**
   * Original randomName function of simpletest did not produce correct results
   * so we backported the Drupal 7 simpletest function to Drupal 6 to genuinely
   * test the exact same scenario.
   *
   * @param type $length
   * @return string
   */
  public static function randomAcquiaSearchName($length = 8) {
    $values = array_merge(range(65, 90), range(97, 122), range(48, 57));
    $max = count($values) - 1;
    $str = chr(mt_rand(97, 122));
    for ($i = 1; $i < $length; $i++) {
      $str .= chr($values[mt_rand(0, $max)]);
    }
    return $str;
  }

  /**
   * Tests HMAC generation.
   */
  public function testHMACCookie() {

    // Generate the expected hash.
    $time = REQUEST_TIME;
    $nonce = $this
      ->randomAcquiaSearchName(32);
    $string = $time . $nonce . $this
      ->randomAcquiaSearchName();
    $hmac = hash_hmac('sha1', $time . $nonce . $string, $this->derivedKey);

    // @todo Make the API function more testable.
    $authenticator = acquia_search_authenticator($string, $nonce, $this->derivedKey);
    preg_match('/acquia_solr_hmac=([a-zA-Z0-9]{40});/', $authenticator, $matches);
    $this
      ->assertEqual($hmac, $matches[1], t('HMAC API function generates the expected hmac hash.'), 'Acquia Search');
    preg_match('/acquia_solr_time=([0-9]{10});/', $authenticator, $matches);
    $this
      ->assertNotNull($matches, t('HMAC API function generates a timestamp.'), 'Acquia Search');
    preg_match('/acquia_solr_nonce=([a-zA-Z0-9]{32});/', $authenticator, $matches);
    $this
      ->assertEqual($nonce, $matches[1], t('HMAC API function generates the expected nonce.'), 'Acquia Search');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AcquiaSearchUnitTestCase::$derivedKey protected property
AcquiaSearchUnitTestCase::$id protected property
AcquiaSearchUnitTestCase::$key protected property
AcquiaSearchUnitTestCase::$salt protected property
AcquiaSearchUnitTestCase::getInfo public static function
AcquiaSearchUnitTestCase::randomAcquiaSearchName public static function Original randomName function of simpletest did not produce correct results so we backported the Drupal 7 simpletest function to Drupal 6 to genuinely test the exact same scenario.
AcquiaSearchUnitTestCase::setUp public function Overrides DrupalTestCase::setUp(). Overrides DrupalUnitTestCase::setUp
AcquiaSearchUnitTestCase::testDerivedKey public function Tests derived key generation.
AcquiaSearchUnitTestCase::testHMACCookie public function Tests HMAC generation.
DrupalTestCase::$assertions protected property Assertions thrown in that test case.
DrupalTestCase::$databasePrefix protected property The database prefix of this test run.
DrupalTestCase::$originalFileDirectory protected property The original file directory, before it was changed for testing purposes.
DrupalTestCase::$originalPrefix protected property The original database prefix, before it was changed for testing purposes.
DrupalTestCase::$results public property Current results of this test case.
DrupalTestCase::$skipClasses protected property This class is skipped when looking for the source of an assertion.
DrupalTestCase::$testId protected property The test run ID.
DrupalTestCase::$timeLimit protected property Time limit for the test.
DrupalTestCase::assert protected function Internal helper: stores the assert.
DrupalTestCase::assertEqual protected function Check to see if two values are equal.
DrupalTestCase::assertFalse protected function Check to see if a value is false (an empty string, 0, NULL, or FALSE).
DrupalTestCase::assertIdentical protected function Check to see if two values are identical.
DrupalTestCase::assertNotEqual protected function Check to see if two values are not equal.
DrupalTestCase::assertNotIdentical protected function Check to see if two values are not identical.
DrupalTestCase::assertNotNull protected function Check to see if a value is not NULL.
DrupalTestCase::assertNull protected function Check to see if a value is NULL.
DrupalTestCase::assertTrue protected function Check to see if a value is not false (not an empty string, 0, NULL, or FALSE).
DrupalTestCase::deleteAssert public static function Delete an assertion record by message ID.
DrupalTestCase::error protected function Fire an error assertion.
DrupalTestCase::errorHandler public function Handle errors during test runs.
DrupalTestCase::exceptionHandler protected function Handle exceptions.
DrupalTestCase::fail protected function Fire an assertion that is always negative.
DrupalTestCase::generatePermutations public static function Converts a list of possible parameters into a stack of permutations.
DrupalTestCase::getAssertionCall protected function Cycles through backtrace until the first non-assertion method is found.
DrupalTestCase::insertAssert public static function Store an assertion from outside the testing context.
DrupalTestCase::pass protected function Fire an assertion that is always positive.
DrupalTestCase::randomName public static function Generates a random string containing letters and numbers.
DrupalTestCase::randomString public static function Generates a random string of ASCII characters of codes 32 to 126.
DrupalTestCase::run public function Run all tests in this class.
DrupalTestCase::verbose protected function Logs verbose message in a text file.
DrupalUnitTestCase::tearDown protected function
DrupalUnitTestCase::__construct function Constructor for DrupalUnitTestCase. Overrides DrupalTestCase::__construct