function drupal_valid_test_ua

Returns the test prefix if this is an internal request from SimpleTest.

Parameters

string $new_prefix: Internal use only. A new prefix to be stored. Passed in by tests that use the test runner from within a test.

Return value

Either the simpletest prefix (the string "simpletest" followed by any number of digits) or FALSE if the user agent does not contain a valid HMAC and timestamp.

16 calls to drupal_valid_test_ua()
BrokenSetUpTest::setUp in drupal/core/modules/simpletest/lib/Drupal/simpletest/Tests/BrokenSetUpTest.php
Sets up a Drupal site for running functional and integration tests.
BrokenSetUpTest::tearDown in drupal/core/modules/simpletest/lib/Drupal/simpletest/Tests/BrokenSetUpTest.php
Delete created files and temporary files directory, delete the tables created by setUp(), and reset the database prefix.
BrokenSetUpTest::testBreakSetUp in drupal/core/modules/simpletest/lib/Drupal/simpletest/Tests/BrokenSetUpTest.php
Runs this test case from within the simpletest child site.
DrupalKernel::getClassName in drupal/core/lib/Drupal/Core/DrupalKernel.php
Returns the classname based on environment, debug and testing prefix.
drupal_handle_request in drupal/core/includes/bootstrap.inc
Handles an entire PHP request.

... See full list

File

drupal/core/includes/bootstrap.inc, line 2270
Functions that need to be loaded on every Drupal request.

Code

function drupal_valid_test_ua($new_prefix = NULL) {
  static $test_prefix;
  if (isset($new_prefix)) {
    $test_prefix = $new_prefix;
  }
  if (isset($test_prefix)) {
    return $test_prefix;
  }
  if (isset($_SERVER['HTTP_USER_AGENT']) && preg_match("/^(simpletest\\d+);(.+);(.+);(.+)\$/", $_SERVER['HTTP_USER_AGENT'], $matches)) {
    list(, $prefix, $time, $salt, $hmac) = $matches;
    $check_string = $prefix . ';' . $time . ';' . $salt;

    // We use the salt from settings.php to make the HMAC key, since
    // the database is not yet initialized and we can't access any Drupal variables.
    // The file properties add more entropy not easily accessible to others.
    $key = drupal_get_hash_salt() . filectime(__FILE__) . fileinode(__FILE__);
    $time_diff = REQUEST_TIME - $time;

    // We cant use Crypt::hmacBase64() yet because this can be called in very
    // early bootstrap when autoloader has not been initialized yet.
    $test_hmac = base64_encode(hash_hmac('sha256', $check_string, $key, TRUE));
    $test_hmac = strtr($test_hmac, array(
      '+' => '-',
      '/' => '_',
      '=' => '',
    ));

    // Since we are making a local request a 5 second time window is allowed,
    // and the HMAC must match.
    if ($time_diff >= 0 && $time_diff <= 5 && $hmac == $test_hmac) {
      $test_prefix = $prefix;
      _drupal_load_test_overrides($test_prefix);
      return $test_prefix;
    }
  }
  $test_prefix = FALSE;
  return $test_prefix;
}