McryptTest.php 5.66 KB
Newer Older
Ketan's avatar
Ketan committed
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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

declare(strict_types=1);

/**
 * Test case for \Magento\Framework\Encryption\Adapter\Mcrypt
 */
namespace Magento\Framework\Encryption\Test\Unit\Adapter;

class McryptTest extends \PHPUnit\Framework\TestCase
{
    private $key;

    private static $cipherInfo;

    private const SUPPORTED_CIPHER_MODE_COMBINATIONS = [
        MCRYPT_BLOWFISH => [MCRYPT_MODE_ECB],
        MCRYPT_RIJNDAEL_128 => [MCRYPT_MODE_ECB],
        MCRYPT_RIJNDAEL_256 => [MCRYPT_MODE_CBC],
    ];

    protected function setUp()
    {
        $this->key = substr(__CLASS__, -32, 32);
    }

    protected function getRandomString(int $length): string
    {
        $result = '';

        do {
            $result .= sha1(microtime());
        } while (strlen($result) < $length);

        return substr($result, -$length);
    }

    private function requireCipherInfo()
    {
        $filename = __DIR__ . '/../Crypt/_files/_cipher_info.php';

        if (!self::$cipherInfo) {
            self::$cipherInfo = include $filename;
        }
    }

    private function getKeySize(string $cipherName, string $modeName): int
    {
        $this->requireCipherInfo();
        return self::$cipherInfo[$cipherName][$modeName]['key_size'];
    }

    private function getInitVectorSize(string $cipherName, string $modeName): int
    {
        $this->requireCipherInfo();
        return self::$cipherInfo[$cipherName][$modeName]['iv_size'];
    }

    public function getCipherModeCombinations(): array
    {
        $result = [];
        foreach (self::SUPPORTED_CIPHER_MODE_COMBINATIONS as $cipher => $modes) {
            /** @var array $modes */
            foreach ($modes as $mode) {
                $result[$cipher . '-' . $mode] = [$cipher, $mode];
            }
        }
        return $result;
    }

    /**
     * @dataProvider getCipherModeCombinations
     */
    public function testConstructor(string $cipher, string $mode)
    {
        /* Generate random init vector */
        $initVector = $this->getRandomString($this->getInitVectorSize($cipher, $mode));

        $crypt = new \Magento\Framework\Encryption\Adapter\Mcrypt($this->key, $cipher, $mode, $initVector);

        $this->assertEquals($cipher, $crypt->getCipher());
        $this->assertEquals($mode, $crypt->getMode());
        $this->assertEquals($initVector, $crypt->getInitVector());
    }

    public function getConstructorExceptionData(): array
    {
        $key = substr(__CLASS__, -32, 32);
        $result = [];
        foreach (self::SUPPORTED_CIPHER_MODE_COMBINATIONS as $cipher => $modes) {
            /** @var array $modes */
            foreach ($modes as $mode) {
                $tooLongKey = str_repeat('-', $this->getKeySize($cipher, $mode) + 1);
                $tooShortInitVector = str_repeat('-', $this->getInitVectorSize($cipher, $mode) - 1);
                $tooLongInitVector = str_repeat('-', $this->getInitVectorSize($cipher, $mode) + 1);
                $result['tooLongKey-' . $cipher . '-' . $mode . '-false'] = [$tooLongKey, $cipher, $mode, false];
                $keyPrefix = 'key-' . $cipher . '-' . $mode;
                $result[$keyPrefix . '-tooShortInitVector'] = [$key, $cipher, $mode, $tooShortInitVector];
                $result[$keyPrefix . '-tooLongInitVector'] = [$key, $cipher, $mode, $tooLongInitVector];
            }
        }
        return $result;
    }

    /**
     * @dataProvider getConstructorExceptionData
     * @expectedException \Magento\Framework\Exception\LocalizedException
     */
    public function testConstructorException(string $key, string $cipher, string $mode, ?string $initVector = null)
    {
        new \Magento\Framework\Encryption\Adapter\Mcrypt($key, $cipher, $mode, $initVector);
    }

    public function testConstructorDefaults()
    {
        $cryptExpected = new \Magento\Framework\Encryption\Adapter\Mcrypt(
            $this->key,
            MCRYPT_BLOWFISH,
            MCRYPT_MODE_ECB,
            null
        );
        $cryptActual = new \Magento\Framework\Encryption\Adapter\Mcrypt($this->key);

        $this->assertEquals($cryptExpected->getCipher(), $cryptActual->getCipher());
        $this->assertEquals($cryptExpected->getMode(), $cryptActual->getMode());
        $this->assertEquals($cryptExpected->getInitVector(), $cryptActual->getInitVector());
    }

    public function getCryptData(): array
    {
        $fixturesFilename = __DIR__ . '/../Crypt/_files/_crypt_fixtures.php';

        $result = include $fixturesFilename;
        /* Restore encoded string back to binary */
        foreach ($result as &$cryptParams) {
            $cryptParams[5] = base64_decode($cryptParams[5]);
        }
        unset($cryptParams);

        return $result;
    }

    /**
     * @dataProvider getCryptData
     */
    public function testDecrypt(
        string $key,
        string $cipher,
        string $mode,
        ?string $initVector,
        string $expectedData,
        string $inputData
    ) {
        $crypt = new \Magento\Framework\Encryption\Adapter\Mcrypt($key, $cipher, $mode, $initVector);
        $actualData = $crypt->decrypt($inputData);
        $this->assertEquals($expectedData, $actualData);
    }

    /**
     * @dataProvider getCipherModeCombinations
     */
    public function testInitVectorNone(string $cipher, string $mode)
    {
        $crypt = new \Magento\Framework\Encryption\Adapter\Mcrypt(
            $this->key,
            $cipher,
            $mode,
            null
        );
        $actualInitVector = $crypt->getInitVector();

        $expectedInitVector = str_repeat("\0", $this->getInitVectorSize($cipher, $mode));
        $this->assertEquals($expectedInitVector, $actualInitVector);
    }
}