]> git.mxchange.org Git - friendica.git/blob - tests/src/Model/User/CookieTest.php
Merge pull request #8142 from nupplaphil/task/di_config
[friendica.git] / tests / src / Model / User / CookieTest.php
1 <?php
2
3 namespace Friendica\Testsrc\Model\User;
4
5 use Friendica\App\BaseURL;
6 use Friendica\Core\Config\IConfig;
7 use Friendica\Model\User\Cookie;
8 use Friendica\Test\DatabaseTest;
9 use Friendica\Test\Util\StaticCookie;
10 use Mockery\MockInterface;
11
12 class CookieTest extends DatabaseTest
13 {
14         /** @var MockInterface|IConfig */
15         private $config;
16         /** @var MockInterface|BaseURL */
17         private $baseUrl;
18
19         protected function setUp()
20         {
21                 StaticCookie::clearStatic();
22
23                 parent::setUp();
24
25                 $this->config = \Mockery::mock(IConfig::class);
26                 $this->baseUrl = \Mockery::mock(BaseURL::class);
27         }
28
29         protected function tearDown()
30         {
31                 StaticCookie::clearStatic();
32         }
33
34         /**
35          * Test if we can create a basic cookie instance
36          */
37         public function testInstance()
38         {
39                 $this->baseUrl->shouldReceive('getSSLPolicy')->andReturn(true)->once();
40                 $this->config->shouldReceive('get')->with('system', 'site_prvkey')->andReturn('1235')->once();
41                 $this->config->shouldReceive('get')->with('system', 'auth_cookie_lifetime', Cookie::DEFAULT_EXPIRE)->andReturn('7')->once();
42
43                 $cookie = new Cookie($this->config, $this->baseUrl);
44                 $this->assertInstanceOf(Cookie::class, $cookie);
45         }
46
47         public function dataGet()
48         {
49                 return [
50                         'default'    => [
51                                 'cookieData' => [
52                                         Cookie::NAME => json_encode([
53                                                 'uid'  => -1,
54                                                 'hash' => 12345,
55                                                 'ip'   => '127.0.0.1',
56                                         ])
57                                 ],
58                                 'hasValues'  => true,
59                                 'uid'        => -1,
60                                 'hash'       => 12345,
61                                 'ip'         => '127.0.0.1',
62                         ],
63                         'missing'    => [
64                                 'cookieData' => [
65
66                                 ],
67                                 'hasValues'  => false,
68                                 'uid'        => null,
69                                 'hash'       => null,
70                                 'ip'         => null,
71                         ],
72                         'invalid'    => [
73                                 'cookieData' => [
74                                         Cookie::NAME => 'test',
75                                 ],
76                                 'hasValues'  => false,
77                                 'uid'        => null,
78                                 'hash'       => null,
79                                 'ip'         => null,
80                         ],
81                         'incomplete' => [
82                                 'cookieData' => [
83                                         Cookie::NAME => json_encode([
84                                                 'uid'  => -1,
85                                                 'hash' => 12345,
86                                         ])
87                                 ],
88                                 'hasValues'  => true,
89                                 'uid'        => -1,
90                                 'hash'       => 12345,
91                                 'ip'         => null,
92                         ],
93                 ];
94         }
95
96         /**
97          * Test the get() method of the cookie class
98          *
99          * @dataProvider dataGet
100          */
101         public function testGet(array $cookieData, bool $hasValues, $uid, $hash, $ip)
102         {
103                 $this->baseUrl->shouldReceive('getSSLPolicy')->andReturn(true)->once();
104                 $this->config->shouldReceive('get')->with('system', 'site_prvkey')->andReturn('1235')->once();
105                 $this->config->shouldReceive('get')->with('system', 'auth_cookie_lifetime', Cookie::DEFAULT_EXPIRE)->andReturn('7')->once();
106
107                 $cookie = new Cookie($this->config, $this->baseUrl, [], $cookieData);
108                 $this->assertInstanceOf(Cookie::class, $cookie);
109
110                 $assertData = $cookie->getData();
111
112                 if (!$hasValues) {
113                         $this->assertEmpty($assertData);
114                 } else {
115                         $this->assertNotEmpty($assertData);
116                         if (isset($uid)) {
117                                 $this->assertObjectHasAttribute('uid', $assertData);
118                                 $this->assertEquals($uid, $assertData->uid);
119                         } else {
120                                 $this->assertObjectNotHasAttribute('uid', $assertData);
121                         }
122                         if (isset($hash)) {
123                                 $this->assertObjectHasAttribute('hash', $assertData);
124                                 $this->assertEquals($hash, $assertData->hash);
125                         } else {
126                                 $this->assertObjectNotHasAttribute('hash', $assertData);
127                         }
128                         if (isset($ip)) {
129                                 $this->assertObjectHasAttribute('ip', $assertData);
130                                 $this->assertEquals($ip, $assertData->ip);
131                         } else {
132                                 $this->assertObjectNotHasAttribute('ip', $assertData);
133                         }
134                 }
135         }
136
137         public function dataCheck()
138         {
139                 return [
140                         'default'   => [
141                                 'serverPrivateKey' => 'serverkey',
142                                 'userPrivateKey'   => 'userkey',
143                                 'password'         => 'test',
144                                 'assertHash'       => 'e9b4eb16275a2907b5659d22905b248221d0517dde4a9d5c320b8fe051b1267b',
145                                 'assertTrue'       => true,
146                         ],
147                         'emptyUser' => [
148                                 'serverPrivateKey' => 'serverkey',
149                                 'userPrivateKey'   => '',
150                                 'password'         => '',
151                                 'assertHash'       => '',
152                                 'assertTrue'       => false,
153                         ],
154                         'invalid'   => [
155                                 'serverPrivateKey' => 'serverkey',
156                                 'userPrivateKey'   => 'bla',
157                                 'password'         => 'nope',
158                                 'assertHash'       => 'real wrong!',
159                                 'assertTrue'       => false,
160                         ]
161                 ];
162         }
163
164         /**
165          * Test the check() method of the cookie class
166          *
167          * @dataProvider dataCheck
168          */
169         public function testCheck(string $serverPrivateKey, string $userPrivateKey, string $password, string $assertHash, bool $assertTrue)
170         {
171                 $this->baseUrl->shouldReceive('getSSLPolicy')->andReturn(true)->once();
172                 $this->config->shouldReceive('get')->with('system', 'site_prvkey')->andReturn($serverPrivateKey)->once();
173                 $this->config->shouldReceive('get')->with('system', 'auth_cookie_lifetime', Cookie::DEFAULT_EXPIRE)->andReturn('7')->once();
174
175                 $cookie = new Cookie($this->config, $this->baseUrl);
176                 $this->assertInstanceOf(Cookie::class, $cookie);
177
178                 $this->assertEquals($assertTrue, $cookie->check($assertHash, $password, $userPrivateKey));
179         }
180
181         public function dataSet()
182         {
183                 return [
184                         'default'         => [
185                                 'serverKey'   => 23,
186                                 'uid'         => 0,
187                                 'password'    => '234',
188                                 'privateKey'  => '124',
189                                 'assertHash'  => 'b657a15cfe7ed1f7289c9aa51af14a9a26c966f4ddd74e495fba103d8e872a39',
190                                 'remoteIp'    => '0.0.0.0',
191                                 'serverArray' => [],
192                                 'lifetime'    => null,
193                         ],
194                         'withServerArray' => [
195                                 'serverKey'   => 23,
196                                 'uid'         => 0,
197                                 'password'    => '234',
198                                 'privateKey'  => '124',
199                                 'assertHash'  => 'b657a15cfe7ed1f7289c9aa51af14a9a26c966f4ddd74e495fba103d8e872a39',
200                                 'remoteIp'    => '1.2.3.4',
201                                 'serverArray' => ['REMOTE_ADDR' => '1.2.3.4',],
202                                 'lifetime'    => null,
203                         ],
204                         'withLifetime0'   => [
205                                 'serverKey'   => 23,
206                                 'uid'         => 0,
207                                 'password'    => '234',
208                                 'privateKey'  => '124',
209                                 'assertHash'  => 'b657a15cfe7ed1f7289c9aa51af14a9a26c966f4ddd74e495fba103d8e872a39',
210                                 'remoteIp'    => '1.2.3.4',
211                                 'serverArray' => ['REMOTE_ADDR' => '1.2.3.4',],
212                                 'lifetime'    => 0,
213                         ],
214                         'withLifetime'     => [
215                                 'serverKey'   => 23,
216                                 'uid'         => 0,
217                                 'password'    => '234',
218                                 'privateKey'  => '124',
219                                 'assertHash'  => 'b657a15cfe7ed1f7289c9aa51af14a9a26c966f4ddd74e495fba103d8e872a39',
220                                 'remoteIp'    => '1.2.3.4',
221                                 'serverArray' => ['REMOTE_ADDR' => '1.2.3.4',],
222                                 'lifetime'    => 2 * 24 * 60 * 60,
223                         ],
224                 ];
225         }
226
227         public function assertCookie($uid, $hash, $remoteIp, $lifetime)
228         {
229                 $this->assertArrayHasKey(Cookie::NAME, StaticCookie::$_COOKIE);
230
231                 $data = json_decode(StaticCookie::$_COOKIE[Cookie::NAME]);
232
233                 $this->assertObjectHasAttribute('uid', $data);
234                 $this->assertEquals($uid, $data->uid);
235                 $this->assertObjectHasAttribute('hash', $data);
236                 $this->assertEquals($hash, $data->hash);
237                 $this->assertObjectHasAttribute('ip', $data);
238                 $this->assertEquals($remoteIp, $data->ip);
239
240                 if (isset($lifetime) && $lifetime !== 0) {
241                         $this->assertLessThanOrEqual(time() + $lifetime, StaticCookie::$_EXPIRE);
242                 } else {
243                         $this->assertLessThanOrEqual(time() + Cookie::DEFAULT_EXPIRE * 24 * 60 * 60, StaticCookie::$_EXPIRE);
244                 }
245         }
246
247         /**
248          * Test the set() method of the cookie class
249          *
250          * @dataProvider dataSet
251          */
252         public function testSet($serverKey, $uid, $password, $privateKey, $assertHash, $remoteIp, $serverArray, $lifetime)
253         {
254                 $this->baseUrl->shouldReceive('getSSLPolicy')->andReturn(true)->once();
255                 $this->config->shouldReceive('get')->with('system', 'site_prvkey')->andReturn($serverKey)->once();
256                 $this->config->shouldReceive('get')->with('system', 'auth_cookie_lifetime', Cookie::DEFAULT_EXPIRE)->andReturn(Cookie::DEFAULT_EXPIRE)->once();
257
258                 $cookie = new StaticCookie($this->config, $this->baseUrl, $serverArray);
259                 $this->assertInstanceOf(Cookie::class, $cookie);
260
261                 $cookie->set($uid, $password, $privateKey, $lifetime);
262
263                 $this->assertCookie($uid, $assertHash, $remoteIp, $lifetime);
264         }
265
266         /**
267          * Test two different set() of the cookie class (first set is invalid)
268          *
269          * @dataProvider dataSet
270          */
271         public function testDoubleSet($serverKey, $uid, $password, $privateKey, $assertHash, $remoteIp, $serverArray, $lifetime)
272         {
273                 $this->baseUrl->shouldReceive('getSSLPolicy')->andReturn(true)->once();
274                 $this->config->shouldReceive('get')->with('system', 'site_prvkey')->andReturn($serverKey)->once();
275                 $this->config->shouldReceive('get')->with('system', 'auth_cookie_lifetime', Cookie::DEFAULT_EXPIRE)->andReturn(Cookie::DEFAULT_EXPIRE)->once();
276
277                 $cookie = new StaticCookie($this->config, $this->baseUrl, $serverArray);
278                 $this->assertInstanceOf(Cookie::class, $cookie);
279
280                 // Invalid set, should get overwritten
281                 $cookie->set(-1, 'invalid', 'nothing', -234);
282
283                 $cookie->set($uid, $password, $privateKey, $lifetime);
284
285                 $this->assertCookie($uid, $assertHash, $remoteIp, $lifetime);
286         }
287
288         /**
289          * Test the clear() method of the cookie class
290          */
291         public function testClear()
292         {
293                 StaticCookie::$_COOKIE = [
294                         Cookie::NAME => 'test'
295                 ];
296
297                 $this->baseUrl->shouldReceive('getSSLPolicy')->andReturn(true)->once();
298                 $this->config->shouldReceive('get')->with('system', 'site_prvkey')->andReturn(24)->once();
299                 $this->config->shouldReceive('get')->with('system', 'auth_cookie_lifetime', Cookie::DEFAULT_EXPIRE)->andReturn(Cookie::DEFAULT_EXPIRE)->once();
300
301                 $cookie = new StaticCookie($this->config, $this->baseUrl);
302                 $this->assertInstanceOf(Cookie::class, $cookie);
303
304                 $this->assertEquals('test', StaticCookie::$_COOKIE[Cookie::NAME]);
305                 $this->assertEquals(null, StaticCookie::$_EXPIRE);
306
307                 $cookie->clear();
308
309                 $this->assertEmpty(StaticCookie::$_COOKIE[Cookie::NAME]);
310                 $this->assertEquals(-3600, StaticCookie::$_EXPIRE);
311         }
312 }