|
12 | 12 | namespaceSymfony\Component\Security\Csrf\Tests; |
13 | 13 |
|
14 | 14 | usePHPUnit\Framework\TestCase; |
| 15 | +useSymfony\Component\HttpFoundation\Request; |
| 16 | +useSymfony\Component\HttpFoundation\RequestStack; |
15 | 17 | useSymfony\Component\Security\Csrf\CsrfToken; |
16 | 18 | useSymfony\Component\Security\Csrf\CsrfTokenManager; |
17 | 19 |
|
|
21 | 23 | class CsrfTokenManagerTestextends TestCase |
22 | 24 | { |
23 | 25 | /** |
24 | | - * @var \PHPUnit_Framework_MockObject_MockObject |
| 26 | + * @dataProvider getManagerGeneratorAndStorage |
25 | 27 | */ |
26 | | -private$generator; |
27 | | - |
28 | | -/** |
29 | | - * @var \PHPUnit_Framework_MockObject_MockObject |
30 | | - */ |
31 | | -private$storage; |
32 | | - |
33 | | -/** |
34 | | - * @var CsrfTokenManager |
35 | | - */ |
36 | | -private$manager; |
37 | | - |
38 | | -protectedfunctionsetUp() |
39 | | - { |
40 | | -$this->generator =$this->getMockBuilder('Symfony\Component\Security\Csrf\TokenGenerator\TokenGeneratorInterface')->getMock(); |
41 | | -$this->storage =$this->getMockBuilder('Symfony\Component\Security\Csrf\TokenStorage\TokenStorageInterface')->getMock(); |
42 | | -$this->manager =newCsrfTokenManager($this->generator,$this->storage); |
43 | | - } |
44 | | - |
45 | | -protectedfunctiontearDown() |
46 | | - { |
47 | | -$this->generator =null; |
48 | | -$this->storage =null; |
49 | | -$this->manager =null; |
50 | | - } |
51 | | - |
52 | | -publicfunctiontestGetNonExistingToken() |
| 28 | +publicfunctiontestGetNonExistingToken($namespace,$manager,$storage,$generator) |
53 | 29 | { |
54 | | -$this->storage->expects($this->once()) |
| 30 | +$storage->expects($this->once()) |
55 | 31 | ->method('hasToken') |
56 | | - ->with('token_id') |
| 32 | + ->with($namespace.'token_id') |
57 | 33 | ->will($this->returnValue(false)); |
58 | 34 |
|
59 | | -$this->generator->expects($this->once()) |
| 35 | +$generator->expects($this->once()) |
60 | 36 | ->method('generateToken') |
61 | 37 | ->will($this->returnValue('TOKEN')); |
62 | 38 |
|
63 | | -$this->storage->expects($this->once()) |
| 39 | +$storage->expects($this->once()) |
64 | 40 | ->method('setToken') |
65 | | - ->with('token_id','TOKEN'); |
| 41 | + ->with($namespace.'token_id','TOKEN'); |
66 | 42 |
|
67 | | -$token =$this->manager->getToken('token_id'); |
| 43 | +$token =$manager->getToken('token_id'); |
68 | 44 |
|
69 | 45 | $this->assertInstanceOf('Symfony\Component\Security\Csrf\CsrfToken',$token); |
70 | 46 | $this->assertSame('token_id',$token->getId()); |
71 | 47 | $this->assertSame('TOKEN',$token->getValue()); |
72 | 48 | } |
73 | 49 |
|
74 | | -publicfunctiontestUseExistingTokenIfAvailable() |
| 50 | +/** |
| 51 | + * @dataProvider getManagerGeneratorAndStorage |
| 52 | + */ |
| 53 | +publicfunctiontestUseExistingTokenIfAvailable($namespace,$manager,$storage) |
75 | 54 | { |
76 | | -$this->storage->expects($this->once()) |
| 55 | +$storage->expects($this->once()) |
77 | 56 | ->method('hasToken') |
78 | | - ->with('token_id') |
| 57 | + ->with($namespace.'token_id') |
79 | 58 | ->will($this->returnValue(true)); |
80 | 59 |
|
81 | | -$this->storage->expects($this->once()) |
| 60 | +$storage->expects($this->once()) |
82 | 61 | ->method('getToken') |
83 | | - ->with('token_id') |
| 62 | + ->with($namespace.'token_id') |
84 | 63 | ->will($this->returnValue('TOKEN')); |
85 | 64 |
|
86 | | -$token =$this->manager->getToken('token_id'); |
| 65 | +$token =$manager->getToken('token_id'); |
87 | 66 |
|
88 | 67 | $this->assertInstanceOf('Symfony\Component\Security\Csrf\CsrfToken',$token); |
89 | 68 | $this->assertSame('token_id',$token->getId()); |
90 | 69 | $this->assertSame('TOKEN',$token->getValue()); |
91 | 70 | } |
92 | 71 |
|
93 | | -publicfunctiontestRefreshTokenAlwaysReturnsNewToken() |
| 72 | +/** |
| 73 | + * @dataProvider getManagerGeneratorAndStorage |
| 74 | + */ |
| 75 | +publicfunctiontestRefreshTokenAlwaysReturnsNewToken($namespace,$manager,$storage,$generator) |
94 | 76 | { |
95 | | -$this->storage->expects($this->never()) |
| 77 | +$storage->expects($this->never()) |
96 | 78 | ->method('hasToken'); |
97 | 79 |
|
98 | | -$this->generator->expects($this->once()) |
| 80 | +$generator->expects($this->once()) |
99 | 81 | ->method('generateToken') |
100 | 82 | ->will($this->returnValue('TOKEN')); |
101 | 83 |
|
102 | | -$this->storage->expects($this->once()) |
| 84 | +$storage->expects($this->once()) |
103 | 85 | ->method('setToken') |
104 | | - ->with('token_id','TOKEN'); |
| 86 | + ->with($namespace.'token_id','TOKEN'); |
105 | 87 |
|
106 | | -$token =$this->manager->refreshToken('token_id'); |
| 88 | +$token =$manager->refreshToken('token_id'); |
107 | 89 |
|
108 | 90 | $this->assertInstanceOf('Symfony\Component\Security\Csrf\CsrfToken',$token); |
109 | 91 | $this->assertSame('token_id',$token->getId()); |
110 | 92 | $this->assertSame('TOKEN',$token->getValue()); |
111 | 93 | } |
112 | 94 |
|
113 | | -publicfunctiontestMatchingTokenIsValid() |
| 95 | +/** |
| 96 | + * @dataProvider getManagerGeneratorAndStorage |
| 97 | + */ |
| 98 | +publicfunctiontestMatchingTokenIsValid($namespace,$manager,$storage) |
114 | 99 | { |
115 | | -$this->storage->expects($this->once()) |
| 100 | +$storage->expects($this->once()) |
116 | 101 | ->method('hasToken') |
117 | | - ->with('token_id') |
| 102 | + ->with($namespace.'token_id') |
118 | 103 | ->will($this->returnValue(true)); |
119 | 104 |
|
120 | | -$this->storage->expects($this->once()) |
| 105 | +$storage->expects($this->once()) |
121 | 106 | ->method('getToken') |
122 | | - ->with('token_id') |
| 107 | + ->with($namespace.'token_id') |
123 | 108 | ->will($this->returnValue('TOKEN')); |
124 | 109 |
|
125 | | -$this->assertTrue($this->manager->isTokenValid(newCsrfToken('token_id','TOKEN'))); |
| 110 | +$this->assertTrue($manager->isTokenValid(newCsrfToken('token_id','TOKEN'))); |
126 | 111 | } |
127 | 112 |
|
128 | | -publicfunctiontestNonMatchingTokenIsNotValid() |
| 113 | +/** |
| 114 | + * @dataProvider getManagerGeneratorAndStorage |
| 115 | + */ |
| 116 | +publicfunctiontestNonMatchingTokenIsNotValid($namespace,$manager,$storage) |
129 | 117 | { |
130 | | -$this->storage->expects($this->once()) |
| 118 | +$storage->expects($this->once()) |
131 | 119 | ->method('hasToken') |
132 | | - ->with('token_id') |
| 120 | + ->with($namespace.'token_id') |
133 | 121 | ->will($this->returnValue(true)); |
134 | 122 |
|
135 | | -$this->storage->expects($this->once()) |
| 123 | +$storage->expects($this->once()) |
136 | 124 | ->method('getToken') |
137 | | - ->with('token_id') |
| 125 | + ->with($namespace.'token_id') |
138 | 126 | ->will($this->returnValue('TOKEN')); |
139 | 127 |
|
140 | | -$this->assertFalse($this->manager->isTokenValid(newCsrfToken('token_id','FOOBAR'))); |
| 128 | +$this->assertFalse($manager->isTokenValid(newCsrfToken('token_id','FOOBAR'))); |
141 | 129 | } |
142 | 130 |
|
143 | | -publicfunctiontestNonExistingTokenIsNotValid() |
| 131 | +/** |
| 132 | + * @dataProvider getManagerGeneratorAndStorage |
| 133 | + */ |
| 134 | +publicfunctiontestNonExistingTokenIsNotValid($namespace,$manager,$storage) |
144 | 135 | { |
145 | | -$this->storage->expects($this->once()) |
| 136 | +$storage->expects($this->once()) |
146 | 137 | ->method('hasToken') |
147 | | - ->with('token_id') |
| 138 | + ->with($namespace.'token_id') |
148 | 139 | ->will($this->returnValue(false)); |
149 | 140 |
|
150 | | -$this->storage->expects($this->never()) |
| 141 | +$storage->expects($this->never()) |
151 | 142 | ->method('getToken'); |
152 | 143 |
|
153 | | -$this->assertFalse($this->manager->isTokenValid(newCsrfToken('token_id','FOOBAR'))); |
| 144 | +$this->assertFalse($manager->isTokenValid(newCsrfToken('token_id','FOOBAR'))); |
154 | 145 | } |
155 | 146 |
|
156 | | -publicfunctiontestRemoveToken() |
| 147 | +/** |
| 148 | + * @dataProvider getManagerGeneratorAndStorage |
| 149 | + */ |
| 150 | +publicfunctiontestRemoveToken($namespace,$manager,$storage) |
157 | 151 | { |
158 | | -$this->storage->expects($this->once()) |
| 152 | +$storage->expects($this->once()) |
159 | 153 | ->method('removeToken') |
160 | | - ->with('token_id') |
| 154 | + ->with($namespace.'token_id') |
161 | 155 | ->will($this->returnValue('REMOVED_TOKEN')); |
162 | 156 |
|
163 | | -$this->assertSame('REMOVED_TOKEN',$this->manager->removeToken('token_id')); |
| 157 | +$this->assertSame('REMOVED_TOKEN',$manager->removeToken('token_id')); |
| 158 | + } |
| 159 | + |
| 160 | +publicfunctiontestNamespaced() |
| 161 | + { |
| 162 | +$generator =$this->getMockBuilder('Symfony\Component\Security\Csrf\TokenGenerator\TokenGeneratorInterface')->getMock(); |
| 163 | +$storage =$this->getMockBuilder('Symfony\Component\Security\Csrf\TokenStorage\TokenStorageInterface')->getMock(); |
| 164 | + |
| 165 | +$requestStack =newRequestStack(); |
| 166 | +$requestStack->push(newRequest(array(),array(),array(),array(),array(),array('HTTPS' =>'on'))); |
| 167 | + |
| 168 | +$manager =newCsrfTokenManager($generator,$storage,null,$requestStack); |
| 169 | + |
| 170 | +$token =$manager->getToken('foo'); |
| 171 | +$this->assertSame('foo',$token->getId()); |
| 172 | + } |
| 173 | + |
| 174 | +publicfunctiongetManagerGeneratorAndStorage() |
| 175 | + { |
| 176 | +$data =array(); |
| 177 | + |
| 178 | +list($generator,$storage) =$this->getGeneratorAndStorage(); |
| 179 | +$data[] =array('',newCsrfTokenManager($generator,$storage,''),$storage,$generator); |
| 180 | + |
| 181 | +list($generator,$storage) =$this->getGeneratorAndStorage(); |
| 182 | +$data[] =array('https-',newCsrfTokenManager($generator,$storage),$storage,$generator); |
| 183 | + |
| 184 | +list($generator,$storage) =$this->getGeneratorAndStorage(); |
| 185 | +$data[] =array('aNamespace-',newCsrfTokenManager($generator,$storage,'aNamespace-'),$storage,$generator); |
| 186 | + |
| 187 | +$requestStack =newRequestStack(); |
| 188 | +$requestStack->push(newRequest(array(),array(),array(),array(),array(),array('HTTPS' =>'on'))); |
| 189 | +list($generator,$storage) =$this->getGeneratorAndStorage(); |
| 190 | +$data[] =array('https-',newCsrfTokenManager($generator,$storage,$requestStack),$storage,$generator); |
| 191 | + |
| 192 | +list($generator,$storage) =$this->getGeneratorAndStorage(); |
| 193 | +$data[] =array('generated-',newCsrfTokenManager($generator,$storage,function () { |
| 194 | +return'generated-'; |
| 195 | + }),$storage,$generator); |
| 196 | + |
| 197 | +$requestStack =newRequestStack(); |
| 198 | +$requestStack->push(newRequest()); |
| 199 | +list($generator,$storage) =$this->getGeneratorAndStorage(); |
| 200 | +$data[] =array('',newCsrfTokenManager($generator,$storage,$requestStack),$storage,$generator); |
| 201 | + |
| 202 | +return$data; |
| 203 | + } |
| 204 | + |
| 205 | +privatefunctiongetGeneratorAndStorage() |
| 206 | + { |
| 207 | +returnarray( |
| 208 | +$this->getMockBuilder('Symfony\Component\Security\Csrf\TokenGenerator\TokenGeneratorInterface')->getMock(), |
| 209 | +$this->getMockBuilder('Symfony\Component\Security\Csrf\TokenStorage\TokenStorageInterface')->getMock(), |
| 210 | + ); |
| 211 | + } |
| 212 | + |
| 213 | +publicfunctionsetUp() |
| 214 | + { |
| 215 | +$_SERVER['HTTPS'] ='on'; |
| 216 | + } |
| 217 | + |
| 218 | +publicfunctiontearDown() |
| 219 | + { |
| 220 | +parent::tearDown(); |
| 221 | + |
| 222 | + unset($_SERVER['HTTPS']); |
164 | 223 | } |
165 | 224 | } |