SecurityTest.php 3.94 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace yiiunit\framework\base;

use yiiunit\TestCase;
use yii\base\Security;

/**
 * @group base
 */
class SecurityTest extends TestCase
{
    /**
     * @var Security
     */
    protected $security;

    protected function setUp()
    {
        parent::setUp();
        $this->security = new Security();
27
        $this->security->derivationIterations = 100; // speed up test running
28 29
    }

30 31
    // Tests :

32 33 34 35 36 37 38 39 40 41 42
    public function testHashData()
    {
        $data = 'known data';
        $key = 'secret';
        $hashedData = $this->security->hashData($data, $key);
        $this->assertFalse($data === $hashedData);
        $this->assertEquals($data, $this->security->validateData($hashedData, $key));
        $hashedData[strlen($hashedData) - 1] = 'A';
        $this->assertFalse($this->security->validateData($hashedData, $key));
    }

43 44 45 46
    /**
     * Data provider for [[testPasswordHash()]]
     * @return array test data
     */
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
    public function dataProviderPasswordHash()
    {
        return [
            [
                'crypt',
                false
            ],
            [
                'password_hash',
                !function_exists('password_hash')
            ],
        ];
    }

    /**
     * @dataProvider dataProviderPasswordHash
     *
     * @param string $passwordHashStrategy
     * @param boolean $isSkipped
     */
    public function testPasswordHash($passwordHashStrategy, $isSkipped)
    {
        if ($isSkipped) {
            $this->markTestSkipped("Unable to test '{$passwordHashStrategy}' password hash strategy");
            return;
        }
        $this->security->passwordHashStrategy = $passwordHashStrategy;

        $password = 'secret';
        $hash = $this->security->generatePasswordHash($password);
        $this->assertTrue($this->security->validatePassword($password, $hash));
        $this->assertFalse($this->security->validatePassword('test', $hash));
    }

81 82 83 84 85 86 87 88 89
    /**
     * Data provider for [[testEncrypt()]]
     * @return array test data
     */
    public function dataProviderEncrypt()
    {
        return [
            [
                'hmac',
90 91 92 93 94 95 96 97 98 99 100 101
                true,
                false,
            ],
            [
                'hmac',
                false,
                false,
            ],
            [
                'pbkdf2',
                true,
                !function_exists('hash_pbkdf2')
102 103 104
            ],
            [
                'pbkdf2',
105
                false,
106 107 108 109 110 111 112 113 114
                !function_exists('hash_pbkdf2')
            ],
        ];
    }

    /**
     * @dataProvider dataProviderEncrypt
     *
     * @param string $deriveKeyStrategy
115
     * @param boolean $useDeriveKeyUniqueSalt
116 117
     * @param boolean $isSkipped
     */
118
    public function testEncrypt($deriveKeyStrategy, $useDeriveKeyUniqueSalt, $isSkipped)
119
    {
120 121 122 123 124
        if ($isSkipped) {
            $this->markTestSkipped("Unable to test '{$deriveKeyStrategy}' derive key strategy");
            return;
        }
        $this->security->deriveKeyStrategy = $deriveKeyStrategy;
125
        $this->security->useDeriveKeyUniqueSalt = $useDeriveKeyUniqueSalt;
126

127 128 129 130 131 132 133
        $data = 'known data';
        $key = 'secret';
        $encryptedData = $this->security->encrypt($data, $key);
        $this->assertFalse($data === $encryptedData);
        $decryptedData = $this->security->decrypt($encryptedData, $key);
        $this->assertEquals($data, $decryptedData);
    }
134

135 136 137 138 139 140 141
    public function testGenerateRandomBytes()
    {
        $length = 21;
        $key = $this->security->generateRandomBytes($length);
        $this->assertEquals($length, strlen($key));
    }

142 143
    public function testGenerateRandomKey()
    {
144 145 146 147
        $length = 21;
        $key = $this->security->generateRandomKey($length);
        $this->assertEquals($length, strlen($key));
        $this->assertEquals(1, preg_match('/[A-Za-z0-9_.-]+/', $key));
148
    }
149
}