SecurityTest.php 1.3 KB
Newer Older
Qiang Xue committed
1 2 3 4 5 6 7 8 9 10 11 12
<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace yiiunit\framework\helpers;

use yiiunit\TestCase;
use yii\helpers\Security;

13 14 15
/**
 * @group helpers
 */
Qiang Xue committed
16 17
class SecurityTest extends TestCase
{
18 19 20 21 22 23 24
    public function testPasswordHash()
    {
        $password = 'secret';
        $hash = Security::generatePasswordHash($password);
        $this->assertTrue(Security::validatePassword($password, $hash));
        $this->assertFalse(Security::validatePassword('test', $hash));
    }
Qiang Xue committed
25

26 27 28 29 30 31 32 33 34 35
    public function testHashData()
    {
        $data = 'known data';
        $key = 'secret';
        $hashedData = Security::hashData($data, $key);
        $this->assertFalse($data === $hashedData);
        $this->assertEquals($data, Security::validateData($hashedData, $key));
        $hashedData[strlen($hashedData) - 1] = 'A';
        $this->assertFalse(Security::validateData($hashedData, $key));
    }
Qiang Xue committed
36

37 38 39 40 41 42 43 44 45
    public function testEncrypt()
    {
        $data = 'known data';
        $key = 'secret';
        $encryptedData = Security::encrypt($data, $key);
        $this->assertFalse($data === $encryptedData);
        $decryptedData = Security::decrypt($encryptedData, $key);
        $this->assertEquals($data, $decryptedData);
    }
Qiang Xue committed
46
}