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