<commit_before><?php

/*
 * This file is part of the overtrue/wechat.
 *
 * (c) overtrue <<EMAIL>>
 *
 * This source file is subject to the MIT license that is bundled
 * with this source code in the file LICENSE.
 */

namespace EasyWeChat\Tests\Kernel;

use EasyWeChat\Kernel\Encryptor;
use EasyWeChat\Kernel\Exceptions\RuntimeException;
use EasyWeChat\Kernel\Support\XML;
use EasyWeChat\Tests\TestCase;

class EncryptorTest extends TestCase
{
    public function getEncryptor()
    {
        return new Encryptor('wxb11529c136998cb6', 'pamtest', 'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFG');
    }

    public function testErrorParams()
    {
        try {
            new Encryptor('', '', '');
            $this->fail('No exception thrown.');
        } catch (\Exception $e) {
            $this->assertSame('Mission config \'aes_key\'.', $e->getMessage());
        }

        try {
            new Encryptor('', '', '1111111111');
            $this->fail('No expected exception thrown.');
        } catch (\Exception $e) {
            $this->assertSame('The length of \'aes_key\' must be 43.', $e->getMessage());
        }
    }

    public function testDecrypt()
    {
        $encrypted = "<xml>\n    <ToUserName><![CDATA[asdasdasd]]></ToUserName>\n    <Encrypt><![CDATA[rTNFcsut4LfGuAFKEUVVpwcaCOTJzOd9twZdIW910jb3k+iicx2uvhttIZ3Qg9Qgty3BEF2xbOrz6boTfb30dMomcgrkTqdFPwnhqbk+kIQ7rZiwny9D7NUrTgA5kpX3KsZvrXzUZyP2x9YOlxbgm572lmxKvM7HAQQhIQ/p6HBmoY30bGXFK0BtIu1pW9TjhOYrLQoU18nWYjWqDA1ynkmOytpv7QRI1P1+0NoxL0q2zO1DgeSvnE8CZGo/o5Ap/WHK5W2RAsinpzN4/LjPnmB6U01I5XCoJoC0GK/yMZycd2Oh8Nq6+wBkC1U85oy0ktOY4nLvsQMLrourmMGdZHuTbqpeJ8Ao/5PRYJ+WBvRUwPfGKBL<KEY>ZN<KEY>+++yz+<KEY>==]]></Encrypt>\n</xml>\n";
        $decrypted = $this->getEncryptor()->decrypt('4f3ad57b6989f09f4eb392acce4f9e93942ed890', '260774613', '1458300676', $encrypted);
        $this->assertSame('asdasdasd', $decrypted['ToUserName']);
        $this->assertSame('asdasdasdsadasd', $decrypted['FromUserName']);
        $this->assertSame('1234567898', $decrypted['CreateTime']);
        $this->assertSame('hello', $decrypted['Content']);
    }

    public function testDecryptWithErrorSignature()
    {
        $this->expectExceptionMessage('Invalid Signature.');
        $encrypted = "<xml>\n    <ToUserName><![CDATA[asdasdasd]]></ToUserName>\n    <Encrypt><![CDATA[rTNFcsut4LfGuAFKEUVVpwcaCOTJzOd9twZdIW910jb3k+iicx2uvhttIZ3Qg9Qgty3BEF2xbOrz6boTfb30dMomcgrkTqdFPwnhqbk+kIQ7rZiwny9D7NUrTgA5kpX3KsZvrXzUZyP2x9YOlxbgm572lmxKvM7HAQQhIQ/<KEY>+++<KEY>]]></Encrypt>\n</xml>\n";
        $this->getEncryptor()->decrypt('invalid-signature', '260774613', '1458300676', $encrypted);
    }

    public function testDecryptWithErrorAppId()
    {
        $this->expectExceptionMessage('Invalid appId.');
        $encrypted = "<xml>\n    <ToUserName><![CDATA[asdasdasd]]></ToUserName>\n    <Encrypt><![CDATA[<KEY>]]></Encrypt>\n</xml>\n";
        $encryptor = new Encryptor('invalid appid', 'pamtest', 'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFG');
        $encryptor->decrypt('4f3ad57b6989f09f4eb392acce4f9e93942ed890', '260774613', '1458300676', $encrypted);
    }

    public function testEncryptAndDecrypt()
    {
        $raw = [
            'ToUserName' => '测试中文',
            'FromUserName' => 'gh_7f083739789a',
            'CreateTime' => '1407743423',
            'MsgType' => 'video',
            'Video' => [
                'MediaId' => 'eYJ1MbwPRJtOvIEabaxHs7TX2D-HV71s79GUxqdUkjm6Gs2Ed1KF3ulAOA9H1xG0',
                'Title' => 'testCallBackReplyVideo',
                'Description' => 'testCallBackReplyVideo',
            ], ];
        $xml = XML::build($raw);
        $encrypted = $this->getEncryptor()->encrypt($xml, 'xxxxxx', '1407743423');
        $array = XML::parse($encrypted);
        $this->assertSame('1407743423', $array['TimeStamp']);
        $this->assertSame('xxxxxx', $array['Nonce']);
        $this->assertNotEmpty($array['Encrypt']);
        $this->assertNotEmpty($array['MsgSignature']);
        $this->assertSame($raw, $this->getEncryptor()->decrypt($array['MsgSignature'], $array['Nonce'], $array['TimeStamp'], $encrypted));
    }

    public function testPkcs7padWithToolongBlockSize()
    {
        $this->expectException(RuntimeException::class);
        $this->expectExceptionMessage('$blockSize may not be more than 256');
        $this->getEncryptor()->pkcs7Pad('xxx', 257);
    }
}<commit_msg>Add getToken test.<commit_after><?php

/*
 * This file is part of the overtrue/wechat.
 *
 * (c) overtrue <<EMAIL>>
 *
 * This source file is subject to the MIT license that is bundled
 * with this source code in the file LICENSE.
 */

namespace EasyWeChat\Tests\Kernel;

use EasyWeChat\Kernel\Encryptor;
use EasyWeChat\Kernel\Exceptions\RuntimeException;
use EasyWeChat\Kernel\Support\XML;
use EasyWeChat\Tests\TestCase;

class EncryptorTest extends TestCase
{
    public function getEncryptor()
    {
        return new Encryptor('wxb11529c136998cb6', 'pamtest', 'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFG');
    }

    public function testErrorParams()
    {
        try {
            new Encryptor('', '', '');
            $this->fail('No exception thrown.');
        } catch (\Exception $e) {
            $this->assertSame('Mission config \'aes_key\'.', $e->getMessage());
        }

        try {
            new Encryptor('', '', '1111111111');
            $this->fail('No expected exception thrown.');
        } catch (\Exception $e) {
            $this->assertSame('The length of \'aes_key\' must be 43.', $e->getMessage());
        }
    }

    public function testDecrypt()
    {
        $encrypted = "<xml>\n    <ToUserName><![CDATA[asdasdasd]]></ToUserName>\n    <Encrypt><![CDATA[rTNFcsut4LfGuAFKEUVVpwcaCOTJzOd9twZdIW910jb3k+iicx2uvhttIZ3Qg9Qgty3BEF2xbOrz6boTfb30dMomcgrkTqdFPwnhqbk+kIQ7rZiwny9D7NUrTgA5kpX3KsZvrXzUZyP2x9YOlxbgm572lmxKvM7HAQQhIQ/<KEY>GXFK0BtIu1pW9TjhOYrLQoU18nWYjWqDA1ynkmOytpv7QRI1P1+0NoxL0q2zO1DgeSvnE8CZGo/o5Ap/WHK5W2RAsinpzN4/LjPnmB6U01I5XCoJoC0GK/yMZycd2Oh8Nq6+wBkC1U85oy0ktOY4nLvsQMLrourmMGdZHuTbqpeJ8Ao/5PRYJ+WBvRUwPfGKBL2+2IKZF49vAJqkcGWSHGE76ZN2erXeuNazf/o9o3lIE3q739o4c8t9QGPe31GT2Go/rOz1BsrASwvauNulCh+++<KEY>==]]></Encrypt>\n</xml>\n";
        $decrypted = $this->getEncryptor()->decrypt('4f3ad57b6989f09f4eb392acce4f9e93942ed890', '260774613', '1458300676', $encrypted);
        $this->assertSame('asdasdasd', $decrypted['ToUserName']);
        $this->assertSame('asdasdasdsadasd', $decrypted['FromUserName']);
        $this->assertSame('1234567898', $decrypted['CreateTime']);
        $this->assertSame('hello', $decrypted['Content']);
    }

    public function testDecryptWithErrorSignature()
    {
        $this->expectExceptionMessage('Invalid Signature.');
        $encrypted = "<xml>\n    <ToUserName><![CDATA[asdasdasd]]></ToUserName>\n    <Encrypt><![CDATA[<KEY>]]></Encrypt>\n</xml>\n";
        $this->getEncryptor()->decrypt('invalid-signature', '260774613', '1458300676', $encrypted);
    }

    public function testDecryptWithErrorAppId()
    {
        $this->expectExceptionMessage('Invalid appId.');
        $encrypted = "<xml>\n    <ToUserName><![CDATA[asdasdasd]]></ToUserName>\n    <Encrypt><![CDATA[<KEY>+++<KEY>]]></Encrypt>\n</xml>\n";
        $encryptor = new Encryptor('invalid appid', 'pamtest', 'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFG');
        $encryptor->decrypt('4f3ad57b6989f09f4eb392acce4f9e93942ed890', '260774613', '1458300676', $encrypted);
    }

    public function testEncryptAndDecrypt()
    {
        $raw = [
            'ToUserName' => '测试中文',
            'FromUserName' => 'gh_7f083739789a',
            'CreateTime' => '1407743423',
            'MsgType' => 'video',
            'Video' => [
                'MediaId' => 'eYJ1MbwPRJtOvIEabaxHs7TX2D-HV71s79GUxqdUkjm6Gs2Ed1KF3ulAOA9H1xG0',
                'Title' => 'testCallBackReplyVideo',
                'Description' => 'testCallBackReplyVideo',
            ], ];
        $xml = XML::build($raw);
        $encrypted = $this->getEncryptor()->encrypt($xml, 'xxxxxx', '1407743423');
        $array = XML::parse($encrypted);
        $this->assertSame('1407743423', $array['TimeStamp']);
        $this->assertSame('xxxxxx', $array['Nonce']);
        $this->assertNotEmpty($array['Encrypt']);
        $this->assertNotEmpty($array['MsgSignature']);
        $this->assertSame($raw, $this->getEncryptor()->decrypt($array['MsgSignature'], $array['Nonce'], $array['TimeStamp'], $encrypted));
    }

    public function testPkcs7padWithToolongBlockSize()
    {
        $this->expectException(RuntimeException::class);
        $this->expectExceptionMessage('$blockSize may not be more than 256');
        $this->getEncryptor()->pkcs7Pad('xxx', 257);
    }

    public function testGetToken()
    {
        $this->assertSame('pamtest', $this->getEncryptor()->getToken());
    }
}
{'fraction_non_alphanumeric': 0.15760190757352105, 'fraction_numerical': 0.06880890200976496, 'mean_word_length': 3.781758957654723, 'pattern_counts': {'":': 0, '<': 68, '<?xml version=': 0, '>': 145, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 16}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4098323', 'n_tokens_mistral': 3682, 'n_tokens_neox': 3291, 'n_words': 389}