1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the MesCryptoBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) Francesco Cartenì <http://www.multimediaexperiencestudio.it/> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Mes\Security\CryptoBundle\Tests\Loader; |
13
|
|
|
|
14
|
|
|
use Mes\Security\CryptoBundle\KeyGenerator\KeyGenerator; |
15
|
|
|
use Mes\Security\CryptoBundle\Loader\CryptoLoader; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class CryptoLoaderTest. |
19
|
|
|
*/ |
20
|
|
|
class CryptoLoaderTest extends \PHPUnit_Framework_TestCase |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var CryptoLoader |
24
|
|
|
*/ |
25
|
|
|
private $loader; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var KeyGenerator |
29
|
|
|
*/ |
30
|
|
|
private $keyGenerator; |
31
|
|
|
|
32
|
|
|
private $tempCryptoFile; |
33
|
|
|
|
34
|
|
|
protected function setUp() |
35
|
|
|
{ |
36
|
|
|
$this->loader = new CryptoLoader(); |
37
|
|
|
$this->loader->setResource(__DIR__.'/../key.crypto'); |
38
|
|
|
$this->keyGenerator = new KeyGenerator(); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
protected function tearDown() |
42
|
|
|
{ |
43
|
|
|
$this->loader = null; |
44
|
|
|
$this->keyGenerator = null; |
45
|
|
|
@unlink($this->tempCryptoFile); |
|
|
|
|
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function testLoadKeyLoadsEncodedKey() |
49
|
|
|
{ |
50
|
|
|
$encodedKey = $this->loader->loadKey(); |
51
|
|
|
|
52
|
|
|
$key = $this->keyGenerator->generateFromAscii($encodedKey, $this->loader->loadSecret()); |
53
|
|
|
|
54
|
|
|
$this->assertInstanceOf('Mes\\Security\\CryptoBundle\\Model\\KeyInterface', $key); |
55
|
|
|
$this->assertTrue(ctype_print($encodedKey), ' is printable'); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @expectedException \UnexpectedValueException |
60
|
|
|
*/ |
61
|
|
View Code Duplication |
public function testLoadKeyThrowsExceptionBecauseKeyIsMissing() |
|
|
|
|
62
|
|
|
{ |
63
|
|
|
$this->tempCryptoFile = __DIR__.'/../invalid_key.crypto'; |
64
|
|
|
|
65
|
|
|
$handle = fopen($this->tempCryptoFile, 'w+'); |
66
|
|
|
fwrite($handle, <<<'EOF' |
67
|
|
|
secret=ThisIsASecret |
68
|
|
|
EOF |
69
|
|
|
); |
70
|
|
|
fclose($handle); |
71
|
|
|
|
72
|
|
|
$loader = new CryptoLoader(); |
73
|
|
|
$loader->setResource($this->tempCryptoFile); |
74
|
|
|
$loader->loadKey(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @expectedException \UnexpectedValueException |
79
|
|
|
*/ |
80
|
|
View Code Duplication |
public function testLoadKeyThrowsExceptionBecauseSecretIsMissing() |
|
|
|
|
81
|
|
|
{ |
82
|
|
|
$this->tempCryptoFile = __DIR__.'/../invalid_key.crypto'; |
83
|
|
|
|
84
|
|
|
$handle = fopen($this->tempCryptoFile, 'w+'); |
85
|
|
|
fwrite($handle, <<<'EOF' |
86
|
|
|
key=ThisIsAnEncodedKey |
87
|
|
|
EOF |
88
|
|
|
); |
89
|
|
|
fclose($handle); |
90
|
|
|
|
91
|
|
|
$loader = new CryptoLoader(); |
92
|
|
|
$loader->setResource($this->tempCryptoFile); |
93
|
|
|
$loader->loadSecret(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function testLoadSecretLoadsSecret() |
97
|
|
|
{ |
98
|
|
|
$secret = $this->loader->loadSecret(); |
99
|
|
|
|
100
|
|
|
$this->assertSame('ThisIsASecret', $secret); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @expectedException \InvalidArgumentException |
105
|
|
|
*/ |
106
|
|
|
public function test__ConstructFailsBecauseResourceNotExists() |
107
|
|
|
{ |
108
|
|
|
$fakeFile = '/var/www/fake'; |
109
|
|
|
|
110
|
|
|
$loader = new CryptoLoader(); |
111
|
|
|
$loader->setResource($fakeFile); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @expectedException \RuntimeException |
116
|
|
|
*/ |
117
|
|
View Code Duplication |
public function test__ConstructFailsBecauseResourceIsBadFormatted() |
|
|
|
|
118
|
|
|
{ |
119
|
|
|
$this->tempCryptoFile = __DIR__.'/../invalid_key.crypto'; |
120
|
|
|
|
121
|
|
|
$handle = fopen($this->tempCryptoFile, 'w+'); |
122
|
|
|
fwrite($handle, <<<'EOF' |
123
|
|
|
key:ThisIsAKey |
124
|
|
|
secret:ThisIsASecret |
125
|
|
|
EOF |
126
|
|
|
); |
127
|
|
|
fclose($handle); |
128
|
|
|
|
129
|
|
|
$loader = new CryptoLoader(); |
130
|
|
|
$loader->setResource($this->tempCryptoFile); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: