colly_wyx
2018-05-18 50a53f7db63c5729b0ddbc93367117cf35ccf03c
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
<?php
/**
 * PhpUnderControl_PhalApiCryptRSAMultiPri2Pub_Test
 *
 * 针对 ../../../PhalApi/Crypt/RSA/MultiPri2Pub.php PhalApi_Crypt_RSA_MultiPri2Pub 类的PHPUnit单元测试
 *
 * @author: dogstar 20150314
 */
 
require_once dirname(__FILE__) . '/../../test_env.php';
 
if (!class_exists('PhalApi_Crypt_RSA_MultiPri2Pub')) {
    require dirname(__FILE__) . '/../../../PhalApi/Crypt/RSA/MultiPri2Pub.php';
}
 
class PhpUnderControl_PhalApiCryptRSAMultiPri2Pub_Test extends PHPUnit_Framework_TestCase
{
    public $phalApiCryptRSAMultiPri2Pub;
 
    public $privkey;
 
    public $pubkey;
 
    protected function setUp()
    {
        parent::setUp();
 
        /**
        $res = openssl_pkey_new();
        openssl_pkey_export($res, $privkey);
        $this->privkey = $privkey;
 
        $pubkey = openssl_pkey_get_details($res);
        $this->pubkey = $pubkey["key"];
         */
 
        $keyG = new PhalApi_Crypt_RSA_KeyGenerator();
        $this->privkey = $keyG->getPriKey();
        $this->pubkey = $keyG->getPubKey();
 
        $this->phalApiCryptRSAMultiPri2Pub = new PhalApi_Crypt_RSA_MultiPri2Pub();
    }
 
    protected function tearDown()
    {
    }
 
 
    /**
     * @group testEncrypt
     */ 
    public function testEncrypt()
    {
        $data = 'something important here ...';
        $key = $this->privkey;
 
        $rs = $this->phalApiCryptRSAMultiPri2Pub->encrypt($data, $key);
 
        $this->assertNotEmpty($rs);
 
        return $rs;
    }
 
    /**
     * @group testDecrypt
     */ 
    public function testDecrypt()
    {
        //we need to encrypt the data again, since pubkey is different every time
        $data = $this->phalApiCryptRSAMultiPri2Pub->encrypt('something important here ...', $this->privkey);
 
        $key = $this->pubkey;
 
        $rs = $this->phalApiCryptRSAMultiPri2Pub->decrypt($data, $key);
 
        $this->assertEquals('something important here ...', $rs);
    }
 
    /**
     * demo
     */
    public function testDecryptAfterEncrypt()
    {
        $keyG = new PhalApi_Crypt_RSA_KeyGenerator();
        $privkey = $keyG->getPriKey();
        $pubkey = $keyG->getPubKey();
 
        DI()->crypt = new PhalApi_Crypt_RSA_MultiPri2Pub();
 
        $data = 'AHA! I have $2.22 dollars!';
 
        $encryptData = DI()->crypt->encrypt($data, $privkey);
 
        $decryptData = DI()->crypt->decrypt($encryptData, $pubkey);
 
        $this->assertEquals($data, $decryptData);
    }
 
    /**
     * @dataProvider provideComplicateData
     */
    public function testWorkWithMoreComplicateData($data)
    {
        $encryptData = $this->phalApiCryptRSAMultiPri2Pub->encrypt($data, $this->privkey);
 
        $decryptData = $this->phalApiCryptRSAMultiPri2Pub->decrypt($encryptData, $this->pubkey);
        $this->assertNotNull($decryptData);
        $this->assertEquals($data, $decryptData);
 
        $wrongDecryptData = $this->phalApiCryptRSAMultiPri2Pub->decrypt($encryptData, 'whatever');
        $this->assertNotSame($data, $wrongDecryptData);
    }
 
    public function provideComplicateData()
    {
        return array(
            array(''),
            array(' '),
            array('0'),
            array(0),
            array(1),
            array('12#d_'),
            array(12345678),
            array('来点中文行不行?'),
            array('843435Jhe*&混合'),
            array(json_encode(array('name' => 'dogstar', 'ext' => '来点中文行不行?'))),
            array('something important here ...'),
            array(str_repeat('something long long here ...', 130)),
            );
    }
 
}