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; |
13
|
|
|
|
14
|
|
|
use Mes\Security\CryptoBundle\KeyManager; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class KeyManagerTest. |
18
|
|
|
*/ |
19
|
|
|
class KeyManagerTest extends \PHPUnit_Framework_TestCase |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject |
23
|
|
|
*/ |
24
|
|
|
private $keyStorage; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject |
28
|
|
|
*/ |
29
|
|
|
private $keyGenerator; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var KeyManager |
33
|
|
|
*/ |
34
|
|
|
private $keyManager; |
35
|
|
|
|
36
|
|
|
protected function setUp() |
37
|
|
|
{ |
38
|
|
|
$this->keyStorage = $this->getMockBuilder('Mes\Security\CryptoBundle\KeyStorage\KeyStorageInterface') |
39
|
|
|
->getMock(); |
40
|
|
|
|
41
|
|
|
$this->keyGenerator = $this->getMockBuilder('Mes\Security\CryptoBundle\KeyGenerator\KeyGeneratorInterface') |
42
|
|
|
->getMock(); |
43
|
|
|
|
44
|
|
|
$this->keyManager = new KeyManager($this->keyStorage, $this->keyGenerator); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
protected function tearDown() |
48
|
|
|
{ |
49
|
|
|
$this->keyGenerator = null; |
50
|
|
|
$this->keyStorage = null; |
51
|
|
|
$this->keyManager = null; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
View Code Duplication |
public function testGenerateCreatesKey() |
|
|
|
|
55
|
|
|
{ |
56
|
|
|
$this->keyGenerator->expects($this->once()) |
57
|
|
|
->method('generate') |
58
|
|
|
->with(null) |
59
|
|
|
->will($this->returnValue($this->getMockBuilder('Mes\Security\CryptoBundle\Model\KeyInterface')->getMock())); |
60
|
|
|
|
61
|
|
|
$key = $this->keyManager->generate(); |
62
|
|
|
|
63
|
|
|
$this->assertInstanceOf('Mes\Security\CryptoBundle\Model\KeyInterface', $key); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
View Code Duplication |
public function testGeneratedFromAsciiCreatesKeyFromAscii() |
|
|
|
|
67
|
|
|
{ |
68
|
|
|
$key_encoded = 'key_encoded'; |
69
|
|
|
$secret = 'ThisIsASecretPassword'; |
70
|
|
|
|
71
|
|
|
$this->keyGenerator->expects($this->once()) |
72
|
|
|
->method('generateFromAscii') |
73
|
|
|
->with($key_encoded, $secret) |
74
|
|
|
->will($this->returnValue($this->getMockBuilder('Mes\Security\CryptoBundle\Model\KeyInterface')->getMock())); |
75
|
|
|
|
76
|
|
|
$key = $this->keyManager->generateFromAscii($key_encoded, $secret); |
77
|
|
|
|
78
|
|
|
$this->assertInstanceOf('Mes\Security\CryptoBundle\Model\KeyInterface', $key); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function testGetKeyReadsNotExistingKey() |
82
|
|
|
{ |
83
|
|
|
$this->keyStorage->expects($this->once()) |
84
|
|
|
->method('getKey') |
85
|
|
|
->will($this->returnValue(null)); |
86
|
|
|
|
87
|
|
|
$this->keyGenerator->expects($this->once()) |
88
|
|
|
->method('generate') |
89
|
|
|
->with(null) |
90
|
|
|
->will($this->returnValue($this->getMockBuilder('Mes\Security\CryptoBundle\Model\KeyInterface') |
91
|
|
|
->getMock())); |
92
|
|
|
|
93
|
|
|
$this->keyStorage->expects($this->once()) |
94
|
|
|
->method('setKey'); |
95
|
|
|
|
96
|
|
|
$key = $this->keyManager->getKey(); |
97
|
|
|
|
98
|
|
|
$this->assertInstanceOf('Mes\Security\CryptoBundle\Model\KeyInterface', $key); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function testGetKeyReadsExistingKey() |
102
|
|
|
{ |
103
|
|
|
$this->keyStorage->expects($this->never()) |
104
|
|
|
->method('setKey'); |
105
|
|
|
|
106
|
|
|
$this->keyGenerator->expects($this->never()) |
107
|
|
|
->method('generate'); |
108
|
|
|
|
109
|
|
|
$this->keyStorage->expects($this->once()) |
110
|
|
|
->method('getKey') |
111
|
|
|
->will($this->returnValue($this->getMockBuilder('Mes\Security\CryptoBundle\Model\KeyInterface') |
112
|
|
|
->getMock())); |
113
|
|
|
|
114
|
|
|
$key = $this->keyManager->getKey(); |
115
|
|
|
|
116
|
|
|
$this->assertInstanceOf('Mes\Security\CryptoBundle\Model\KeyInterface', $key); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function testSetKeyStoresKey() |
120
|
|
|
{ |
121
|
|
|
$this->keyStorage->expects($this->once()) |
122
|
|
|
->method('setKey') |
123
|
|
|
->with($this->getMockBuilder('Mes\Security\CryptoBundle\Model\KeyInterface') |
124
|
|
|
->getMock()); |
125
|
|
|
|
126
|
|
|
$this->keyManager->setKey($this->getMockBuilder('Mes\Security\CryptoBundle\Model\KeyInterface') |
127
|
|
|
->getMock()); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
public function testSetSecretStoreSecret() |
131
|
|
|
{ |
132
|
|
|
$this->keyManager->setSecret('ThisIsASecret'); |
133
|
|
|
|
134
|
|
|
$this->assertSame('ThisIsASecret', $this->keyManager->getSecret()); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.