4
4
5
5
class SimpleTest extends \PHPUnit_Framework_TestCase
6
6
{
7
+ /**
8
+ * @var Simple
9
+ */
7
10
private $ _instance ;
8
11
private $ _encryptionKey = 'nXA5gXtlOgHgxl6EZTfkfDmIzWaRqxZ1rq7DRNCIQ/Q= ' ;
9
12
private $ _macKey = 'K9iPmOMowXUvcQTd7ehfcxvvHd4OtzyztQp+wuQwb6U= ' ;
10
13
11
14
public function setUp () {
12
- $ this ->_instance = new \ PHPCrypt \ Simple ($ this ->_encryptionKey , $ this ->_macKey );
15
+ $ this ->_instance = new Simple ($ this ->_encryptionKey , $ this ->_macKey );
13
16
}
14
17
15
- public function testEncrypt () {
18
+ public function testCanEncryptPlaintext () {
16
19
$ ciphertext = $ this ->_instance ->encrypt ('FooBar ' , 'lAuCU7ft5tnHPKWRjF1IKV4J6V9/eCGQIisHZfuqMtY= ' );
17
-
18
- $ this ->assertEquals (
20
+
21
+ $ this ->assertSame (
19
22
'cmpkLTI1Ni1obWFjLXNoYTI1NnxsQXVDVTdmdDV0bkhQS1dSakYxSUtWNEo2VjkvZUNHUUlpc0haZnVxTXRhLzNnSmc3SWhIZ3h2YVVZNmlzUnlQY1JxK3gvclFmblB4WS9BMVhxWTJuQT09fDZNQkJDS0JiWWMrYVdMcG5rMU1RVlcyak01Sm56NW9IZlhuRHJpeUlMOVE9 ' ,
20
23
$ ciphertext
21
24
);
22
25
}
23
-
24
- public function testDecrypt () {
26
+
27
+ public function testCanDecryptCiphertext () {
25
28
$ plaintext = $ this ->_instance ->decrypt (
26
29
'cmpkLTI1Ni1obWFjLXNoYTI1NnxsQXVDVTdmdDV0bkhQS1dSakYxSUtWNEo2VjkvZUNHUUlpc0haZnVxTXRhLzNnSmc3SWhIZ3h2YVVZNmlzUnlQY1JxK3gvclFmblB4WS9BMVhxWTJuQT09fDZNQkJDS0JiWWMrYVdMcG5rMU1RVlcyak01Sm56NW9IZlhuRHJpeUlMOVE9 '
27
30
);
28
-
29
- $ this ->assertEquals ('FooBar ' , $ plaintext );
31
+
32
+ $ this ->assertSame ('FooBar ' , $ plaintext );
30
33
}
31
-
32
- public function testDecryptUnknownConstruction () {
33
- $ this ->setExpectedException ('Exception ' , 'Unknown construction, "rjd-256-hmac-sha256/128" ' );
34
- $ plaintext = $ this ->_instance ->decrypt (
34
+
35
+ /**
36
+ * @expectedException \RunTimeException
37
+ * @expectedExceptionMessage Unknown construction, "rjd-256-hmac-sha256/128"
38
+ */
39
+ public function testDecryptingSignedCiphertextWithUnknownConstructionThrowsException () {
40
+ $ this ->_instance ->decrypt (
35
41
'cmpkLTI1Ni1obWFjLXNoYTI1Ni8xMjh8bEF1Q1U3ZnQ1dG5IUEtXUmpGMUlLVjRKNlY5L2VDR1FJaXNIWmZ1cU10YS8zZ0pnN0loSGd4dmFVWTZpc1J5UGNScSt4L3JRZm5QeFkvQTFYcVkybkE9PXw2TUJCQ0tCYlljK2FXTHBuazFNUVZXMmpNNUpuejVvSGZYbkRyaXlJTDlRPQ== '
36
42
);
37
43
}
38
44
45
+ public function invalidCiphertextPartsData ()
46
+ {
47
+ return array (
48
+ 'Too few parts ' => array ('cGFydDF8cGFydDI= ' ), // 'part1|part2'
49
+ 'Too many parts ' => array ('cGFydDF8cGFydDJ8cGFydDN8cGFydDQ= ' ), // 'part1|part2|part3|part4'
50
+ );
51
+ }
52
+
53
+ /**
54
+ * @dataProvider invalidCiphertextPartsData
55
+ * @expectedException \RuntimeException
56
+ * @expectedExceptionMessage Invalid encoding
57
+ */
58
+ public function testDecryptingCiphertextWithWrongNumberOfPartsThrowsException ($ ciphertext ) {
59
+ $ this ->_instance ->decrypt ($ ciphertext );
60
+ }
61
+
39
62
/**
40
- /* This function encrypts the same string with randomized IVs and
41
- /* flips a single bit of the ciphertext
42
- */
63
+ * Encrypts the same string with randomized IVs and flips a single bit of
64
+ * the ciphertext.
65
+ */
43
66
public function invalidCiphertextData () {
44
67
$ this ->setUp ();
45
68
$ invalidCiphertexts = array ();
@@ -51,15 +74,15 @@ public function invalidCiphertextData() {
51
74
$ ciphertext = base64_decode ($ encodedCiphertext );
52
75
$ randomByte = rand (1 , strlen ($ ciphertext ));
53
76
$ mask = str_repeat ("\x00" , $ randomByte -1 ) . "\x01" . str_repeat ("\x00" , strlen ($ ciphertext ) - $ randomByte );
54
-
77
+
55
78
// SANITY CHECK: If this mask is removed, this test should fail every
56
79
// single run because the ciphertext should match.
57
80
$ invalidCiphertext = $ ciphertext ^ $ mask ;
58
81
59
82
// printf("Ciphertext: %s\n", bin2hex($ciphertext));
60
83
// printf("Mask: %s\n", bin2hex($mask));
61
84
// printf("Invalid Ciphertext: %s\n", bin2hex($invalidCiphertext));
62
-
85
+
63
86
$ encodedInvalidCiphertext = base64_encode ($ invalidCiphertext );
64
87
$ invalidCiphertexts [] = array (base64_encode ($ construction . '| ' . $ encodedInvalidCiphertext . '| ' . $ encodedSignature ));
65
88
}
@@ -69,16 +92,18 @@ public function invalidCiphertextData() {
69
92
70
93
/**
71
94
* @dataProvider invalidCiphertextData
95
+ * @expectedException \RuntimeException
96
+ * @expectedExceptionMessage Invalid signature
72
97
*/
73
- public function testDecryptInvalidCiphertext ($ signedCiphertext ) {
74
- $ this ->setExpectedException ('Exception ' , 'Invalid signature ' );
75
- $ plaintext = $ this ->_instance ->decrypt ($ signedCiphertext );
98
+ public function testDecryptingInvalidCiphertextThrowsException ($ signedCiphertext ) {
99
+ $ this ->_instance ->decrypt ($ signedCiphertext );
76
100
}
77
-
78
- public function testEncryptAndDecrypt () {
101
+
102
+ public function testEncryptedDataCanBeDecrypted () {
79
103
$ plaintext = 'Something ' ;
80
104
$ ciphertext = $ this ->_instance ->encrypt ('Something ' );
81
- $ this ->assertEquals ($ plaintext , $ this ->_instance ->decrypt ($ ciphertext ));
105
+
106
+ $ this ->assertSame ($ plaintext , $ this ->_instance ->decrypt ($ ciphertext ));
82
107
}
83
108
84
109
public function invalidKeyData () {
@@ -94,32 +119,39 @@ public function invalidKeyData() {
94
119
95
120
/**
96
121
* @dataProvider invalidKeyData
122
+ * @expectedException \InvalidArgumentException
123
+ * @expectedExceptionMessage Encryption key must be
97
124
*/
98
- public function testEncryptInvalidEncryptionKeySize ($ invalidKey ) {
99
- $ this ->setExpectedException ('Exception ' );
100
- $ instance = new \PHPCrypt \Simple ($ invalidKey , $ this ->_macKey );
125
+ public function testInvalidEncryptionKeyThrowsException ($ invalidKey ) {
126
+ new Simple ($ invalidKey , $ this ->_macKey );
101
127
}
102
-
128
+
103
129
/**
104
130
* @dataProvider invalidKeyData
131
+ * @expectedException \InvalidArgumentException
132
+ * @expectedExceptionMessage MAC key must be
105
133
*/
106
- public function testEncryptInvalidMacKeySize ($ invalidKey ) {
107
- $ this ->setExpectedException ('Exception ' );
108
- $ instance = new \PHPCrypt \Simple ($ this ->_encryptionKey , $ invalidKey );
134
+ public function testInvalidMacKeySizeThrowsException ($ invalidKey ) {
135
+ new Simple ($ this ->_encryptionKey , $ invalidKey );
109
136
}
110
137
111
- public function testEncryptInvalidIvLength () {
112
- $ this ->setExpectedException ('Exception ' );
138
+ /**
139
+ * @expectedException \InvalidArgumentException
140
+ * @expectedExceptionMessage IV must be exactly
141
+ */
142
+ public function testEncryptWithInvalidIvLengthThrowsException () {
143
+ $ this ->_instance ->encrypt ('FooBar ' , 'wrong-iv-length ' );
144
+ }
113
145
114
- $ ciphertext = $ this ->_instance ->encrypt (
115
- 'FooBar ' , $ this ->_encryptionKey , $ this ->macKey , 'Short IV ' );
146
+ public function testDifferentIvGeneratedOnEachRun () {
147
+ $ expected = 20 ;
148
+ $ ivs = array ();
149
+ for ($ i = 1 ; $ i <= $ expected ; $ i ++) {
150
+ $ ivs [] = $ this ->_instance ->generateIv ();
151
+ }
116
152
117
- $ this ->assertEquals ('This should never execute ' , base64_encode ($ ciphertext ));
118
- }
153
+ $ duplicatesRemoved = array_values ($ ivs );
119
154
120
- public function testGenerateIv () {
121
- $ iv = $ this ->_instance ->generateIv ();
122
- $ secondIv = $ this ->_instance ->generateIv ();
123
- $ this ->assertNotEquals ($ iv , $ secondIv );
155
+ $ this ->assertCount ($ expected , $ duplicatesRemoved );
124
156
}
125
157
}
0 commit comments