3 namespace Friendica\Testsrc\Model\User;
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;
11 class CookieTest extends DatabaseTest
13 /** @var MockInterface|Configuration */
16 protected function setUp()
18 StaticCookie::clearStatic();
22 $this->config = \Mockery::mock(Configuration::class);
25 protected function tearDown()
27 StaticCookie::clearStatic();
31 * Test if we can create a basic cookie instance
33 public function testInstance()
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();
39 $cookie = new Cookie($this->config, []);
40 $this->assertInstanceOf(Cookie::class, $cookie);
43 public function dataGet()
48 Cookie::NAME => json_encode([
70 Cookie::NAME => 'test',
79 Cookie::NAME => json_encode([
93 * Test the get() method of the cookie class
95 * @dataProvider dataGet
97 public function testGet(array $cookieData, bool $hasValues, $uid, $hash, $ip)
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();
103 $cookie = new Cookie($this->config, [], $cookieData);
104 $this->assertInstanceOf(Cookie::class, $cookie);
106 $assertData = $cookie->getData();
109 $this->assertEmpty($assertData);
111 $this->assertNotEmpty($assertData);
113 $this->assertObjectHasAttribute('uid', $assertData);
114 $this->assertEquals($uid, $assertData->uid);
116 $this->assertObjectNotHasAttribute('uid', $assertData);
119 $this->assertObjectHasAttribute('hash', $assertData);
120 $this->assertEquals($hash, $assertData->hash);
122 $this->assertObjectNotHasAttribute('hash', $assertData);
125 $this->assertObjectHasAttribute('ip', $assertData);
126 $this->assertEquals($ip, $assertData->ip);
128 $this->assertObjectNotHasAttribute('ip', $assertData);
133 public function dataCheck()
137 'serverPrivateKey' => 'serverkey',
138 'userPrivateKey' => 'userkey',
139 'password' => 'test',
140 'assertHash' => 'e9b4eb16275a2907b5659d22905b248221d0517dde4a9d5c320b8fe051b1267b',
141 'assertTrue' => true,
144 'serverPrivateKey' => 'serverkey',
145 'userPrivateKey' => '',
148 'assertTrue' => false,
151 'serverPrivateKey' => 'serverkey',
152 'userPrivateKey' => 'bla',
153 'password' => 'nope',
154 'assertHash' => 'real wrong!',
155 'assertTrue' => false,
161 * Test the check() method of the cookie class
163 * @dataProvider dataCheck
165 public function testCheck(string $serverPrivateKey, string $userPrivateKey, string $password, string $assertHash, bool $assertTrue)
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();
171 $cookie = new Cookie($this->config, []);
172 $this->assertInstanceOf(Cookie::class, $cookie);
174 $this->assertEquals($assertTrue, $cookie->check($assertHash, $password, $userPrivateKey));
177 public function dataSet()
184 'privateKey' => '124',
185 'assertHash' => 'b657a15cfe7ed1f7289c9aa51af14a9a26c966f4ddd74e495fba103d8e872a39',
186 'remoteIp' => '0.0.0.0',
190 'withServerArray' => [
194 'privateKey' => '124',
195 'assertHash' => 'b657a15cfe7ed1f7289c9aa51af14a9a26c966f4ddd74e495fba103d8e872a39',
196 'remoteIp' => '1.2.3.4',
197 'serverArray' => ['REMOTE_ADDR' => '1.2.3.4',],
204 'privateKey' => '124',
205 'assertHash' => 'b657a15cfe7ed1f7289c9aa51af14a9a26c966f4ddd74e495fba103d8e872a39',
206 'remoteIp' => '1.2.3.4',
207 'serverArray' => ['REMOTE_ADDR' => '1.2.3.4',],
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,
223 public function assertCookie($uid, $hash, $remoteIp, $lifetime)
225 $this->assertArrayHasKey(Cookie::NAME, StaticCookie::$_COOKIE);
227 $data = json_decode(StaticCookie::$_COOKIE[Cookie::NAME]);
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);
236 if (isset($lifetime) && $lifetime !== 0) {
237 $this->assertLessThanOrEqual(time() + $lifetime, StaticCookie::$_EXPIRE);
239 $this->assertLessThanOrEqual(time() + Cookie::DEFAULT_EXPIRE * 24 * 60 * 60, StaticCookie::$_EXPIRE);
244 * Test the set() method of the cookie class
246 * @dataProvider dataSet
248 public function testSet($serverKey, $uid, $password, $privateKey, $assertHash, $remoteIp, $serverArray, $lifetime)
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();
254 $cookie = new StaticCookie($this->config, $serverArray);
255 $this->assertInstanceOf(Cookie::class, $cookie);
257 $cookie->set($uid, $password, $privateKey, $lifetime);
259 $this->assertCookie($uid, $assertHash, $remoteIp, $lifetime);
263 * Test two different set() of the cookie class (first set is invalid)
265 * @dataProvider dataSet
267 public function testDoubleSet($serverKey, $uid, $password, $privateKey, $assertHash, $remoteIp, $serverArray, $lifetime)
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();
273 $cookie = new StaticCookie($this->config, $serverArray);
274 $this->assertInstanceOf(Cookie::class, $cookie);
276 // Invalid set, should get overwritten
277 $cookie->set(-1, 'invalid', 'nothing', -234);
279 $cookie->set($uid, $password, $privateKey, $lifetime);
281 $this->assertCookie($uid, $assertHash, $remoteIp, $lifetime);
285 * Test the clear() method of the cookie class
287 public function testClear()
289 StaticCookie::$_COOKIE = [
290 Cookie::NAME => 'test'
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();
297 $cookie = new StaticCookie($this->config, []);
298 $this->assertInstanceOf(Cookie::class, $cookie);
300 $this->assertEquals('test', StaticCookie::$_COOKIE[Cookie::NAME]);
301 $this->assertEquals(null, StaticCookie::$_EXPIRE);
305 $this->assertEmpty(StaticCookie::$_COOKIE[Cookie::NAME]);
306 $this->assertEquals(-3600, StaticCookie::$_EXPIRE);