You are here

class CryptTest in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/tests/Drupal/Tests/Component/Utility/CryptTest.php \Drupal\Tests\Component\Utility\CryptTest

Tests random byte generation.

@group Utility

@coversDefaultClass \Drupal\Component\Utility\Crypt

Hierarchy

  • class \Drupal\Tests\UnitTestCase extends \Drupal\Tests\PHPUnit_Framework_TestCase
    • class \Drupal\Tests\Component\Utility\CryptTest

Expanded class hierarchy of CryptTest

File

core/tests/Drupal/Tests/Component/Utility/CryptTest.php, line 20
Contains \Drupal\Tests\Component\Utility\CryptTest.

Namespace

Drupal\Tests\Component\Utility
View source
class CryptTest extends UnitTestCase {

  /**
   * Tests random byte generation.
   *
   * @covers ::randomBytes
   */
  public function testRandomBytes() {
    for ($i = 1; $i < 10; $i++) {
      $count = rand(10, 10000);

      // Check that different values are being generated.
      $this
        ->assertNotEquals(Crypt::randomBytes($count), Crypt::randomBytes($count));

      // Check the length.
      $this
        ->assertEquals(strlen(Crypt::randomBytes($count)), $count);
    }
  }

  /**
   * Tests hash generation.
   *
   * @dataProvider providerTestHashBase64
   * @covers ::hashBase64
   *
   * @param string $data
   *   Data to hash.
   * @param string $expected_hash
   *   Expected result from hashing $data.
   */
  public function testHashBase64($data, $expected_hash) {
    $hash = Crypt::hashBase64($data);
    $this
      ->assertEquals($expected_hash, $hash, 'The correct hash was not calculated.');
  }

  /**
   * Tests HMAC generation.
   *
   * @dataProvider providerTestHmacBase64
   * @covers ::hmacBase64
   *
   * @param string $data
   *   Data to hash.
   * @param string $key
   *   Key to use in hashing process.
   * @param string $expected_hmac
   *   Expected result from hashing $data using $key.
   */
  public function testHmacBase64($data, $key, $expected_hmac) {
    $hmac = Crypt::hmacBase64($data, $key);
    $this
      ->assertEquals($expected_hmac, $hmac, 'The correct hmac was not calculated.');
  }

  /**
   * Tests the hmacBase64 method with invalid parameters.
   *
   * @dataProvider providerTestHmacBase64Invalid
   * @expectedException InvalidArgumentException
   * @covers ::hmacBase64
   *
   * @param string $data
   *   Data to hash.
   * @param string $key
   *   Key to use in hashing process.
   */
  public function testHmacBase64Invalid($data, $key) {
    Crypt::hmacBase64($data, $key);
  }

  /**
   * Provides data for self::testHashBase64().
   *
   * @return array Test data.
   */
  public function providerTestHashBase64() {
    return array(
      array(
        'data' => 'The SHA (Secure Hash Algorithm) is one of a number of cryptographic hash functions. A cryptographic hash is like a signature for a text or a data file. SHA-256 algorithm generates an almost-unique, fixed size 256-bit (32-byte) hash. Hash is a one way function – it cannot be decrypted back. This makes it suitable for password validation, challenge hash authentication, anti-tamper, digital signatures.',
        'expectedHash' => '034rT6smZAVRxpq8O98cFFNLIVx_Ph1EwLZQKcmRR_s',
      ),
      array(
        'data' => 'SHA-256 is one of the successor hash functions to SHA-1, and is one of the strongest hash functions available.',
        'expected_hash' => 'yuqkDDYqprL71k4xIb6K6D7n76xldO4jseRhEkEE6SI',
      ),
    );
  }

  /**
   * Provides data for self::testHmacBase64().
   *
   * @return array Test data.
   */
  public function providerTestHmacBase64() {
    return array(
      array(
        'data' => 'Calculates a base-64 encoded, URL-safe sha-256 hmac.',
        'key' => 'secret-key',
        'expected_hmac' => '2AaH63zwjhekWZlEpAiufyfhAHIzbQhl9Hd9oCi3_c8',
      ),
    );
  }

  /**
   * Provides data for self::testHmacBase64().
   *
   * @return array Test data.
   */
  public function providerTestHmacBase64Invalid() {
    return array(
      array(
        new \stdClass(),
        new \stdClass(),
      ),
      array(
        new \stdClass(),
        'string',
      ),
      array(
        new \stdClass(),
        1,
      ),
      array(
        new \stdClass(),
        0,
      ),
      array(
        NULL,
        new \stdClass(),
      ),
      array(
        'string',
        new \stdClass(),
      ),
      array(
        1,
        new \stdClass(),
      ),
      array(
        0,
        new \stdClass(),
      ),
      array(
        array(),
        array(),
      ),
      array(
        array(),
        NULL,
      ),
      array(
        array(),
        'string',
      ),
      array(
        array(),
        1,
      ),
      array(
        array(),
        0,
      ),
      array(
        NULL,
        array(),
      ),
      array(
        1,
        array(),
      ),
      array(
        0,
        array(),
      ),
      array(
        'string',
        array(),
      ),
      array(
        array(),
        NULL,
      ),
      array(
        NULL,
        NULL,
      ),
      array(
        NULL,
        'string',
      ),
      array(
        NULL,
        1,
      ),
      array(
        NULL,
        0,
      ),
      array(
        1,
        NULL,
      ),
      array(
        0,
        NULL,
      ),
      array(
        'string',
        NULL,
      ),
    );
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CryptTest::providerTestHashBase64 public function Provides data for self::testHashBase64().
CryptTest::providerTestHmacBase64 public function Provides data for self::testHmacBase64().
CryptTest::providerTestHmacBase64Invalid public function Provides data for self::testHmacBase64().
CryptTest::testHashBase64 public function Tests hash generation.
CryptTest::testHmacBase64 public function Tests HMAC generation.
CryptTest::testHmacBase64Invalid public function Tests the hmacBase64 method with invalid parameters.
CryptTest::testRandomBytes public function Tests random byte generation.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root.
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName protected function Mocks a block with a block plugin.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed in array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.
UnitTestCase::setUp protected function 259