3 * @copyright Copyright (C) 2020, Friendica
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Test\src\Model\User;
24 use Friendica\App\BaseURL;
25 use Friendica\Core\Config\IConfig;
26 use Friendica\Model\User\Cookie;
27 use Friendica\Test\MockedTest;
28 use Friendica\Test\Util\StaticCookie;
29 use Mockery\MockInterface;
31 class CookieTest extends MockedTest
33 /** @var MockInterface|IConfig */
35 /** @var MockInterface|BaseURL */
38 protected function setUp()
40 StaticCookie::clearStatic();
44 $this->config = \Mockery::mock(IConfig::class);
45 $this->baseUrl = \Mockery::mock(BaseURL::class);
48 protected function tearDown()
50 StaticCookie::clearStatic();
56 * Test if we can create a basic cookie instance
58 public function testInstance()
60 $this->baseUrl->shouldReceive('getSSLPolicy')->andReturn(true)->once();
61 $this->config->shouldReceive('get')->with('system', 'site_prvkey')->andReturn('1235')->once();
62 $this->config->shouldReceive('get')->with('system', 'auth_cookie_lifetime', Cookie::DEFAULT_EXPIRE)->andReturn('7')->once();
64 $cookie = new Cookie($this->config, $this->baseUrl);
65 self::assertInstanceOf(Cookie::class, $cookie);
68 public function dataGet()
73 Cookie::NAME => json_encode([
95 Cookie::NAME => 'test',
104 Cookie::NAME => json_encode([
118 * Test the get() method of the cookie class
120 * @dataProvider dataGet
122 public function testGet(array $cookieData, bool $hasValues, $uid, $hash, $ip)
124 $this->baseUrl->shouldReceive('getSSLPolicy')->andReturn(true)->once();
125 $this->config->shouldReceive('get')->with('system', 'site_prvkey')->andReturn('1235')->once();
126 $this->config->shouldReceive('get')->with('system', 'auth_cookie_lifetime', Cookie::DEFAULT_EXPIRE)->andReturn('7')->once();
128 $cookie = new Cookie($this->config, $this->baseUrl, [], $cookieData);
129 self::assertInstanceOf(Cookie::class, $cookie);
131 $assertData = $cookie->getData();
134 self::assertEmpty($assertData);
136 self::assertNotEmpty($assertData);
138 self::assertObjectHasAttribute('uid', $assertData);
139 self::assertEquals($uid, $assertData->uid);
141 self::assertObjectNotHasAttribute('uid', $assertData);
144 self::assertObjectHasAttribute('hash', $assertData);
145 self::assertEquals($hash, $assertData->hash);
147 self::assertObjectNotHasAttribute('hash', $assertData);
150 self::assertObjectHasAttribute('ip', $assertData);
151 self::assertEquals($ip, $assertData->ip);
153 self::assertObjectNotHasAttribute('ip', $assertData);
158 public function dataCheck()
162 'serverPrivateKey' => 'serverkey',
163 'userPrivateKey' => 'userkey',
164 'password' => 'test',
165 'assertHash' => 'e9b4eb16275a2907b5659d22905b248221d0517dde4a9d5c320b8fe051b1267b',
166 'assertTrue' => true,
169 'serverPrivateKey' => 'serverkey',
170 'userPrivateKey' => '',
173 'assertTrue' => false,
176 'serverPrivateKey' => 'serverkey',
177 'userPrivateKey' => 'bla',
178 'password' => 'nope',
179 'assertHash' => 'real wrong!',
180 'assertTrue' => false,
186 * Test the check() method of the cookie class
188 * @dataProvider dataCheck
190 public function testCheck(string $serverPrivateKey, string $userPrivateKey, string $password, string $assertHash, bool $assertTrue)
192 $this->baseUrl->shouldReceive('getSSLPolicy')->andReturn(true)->once();
193 $this->config->shouldReceive('get')->with('system', 'site_prvkey')->andReturn($serverPrivateKey)->once();
194 $this->config->shouldReceive('get')->with('system', 'auth_cookie_lifetime', Cookie::DEFAULT_EXPIRE)->andReturn('7')->once();
196 $cookie = new Cookie($this->config, $this->baseUrl);
197 self::assertInstanceOf(Cookie::class, $cookie);
199 self::assertEquals($assertTrue, $cookie->check($assertHash, $password, $userPrivateKey));
202 public function dataSet()
209 'privateKey' => '124',
210 'assertHash' => 'b657a15cfe7ed1f7289c9aa51af14a9a26c966f4ddd74e495fba103d8e872a39',
211 'remoteIp' => '0.0.0.0',
215 'withServerArray' => [
219 'privateKey' => '124',
220 'assertHash' => 'b657a15cfe7ed1f7289c9aa51af14a9a26c966f4ddd74e495fba103d8e872a39',
221 'remoteIp' => '1.2.3.4',
222 'serverArray' => ['REMOTE_ADDR' => '1.2.3.4',],
229 'privateKey' => '124',
230 'assertHash' => 'b657a15cfe7ed1f7289c9aa51af14a9a26c966f4ddd74e495fba103d8e872a39',
231 'remoteIp' => '1.2.3.4',
232 'serverArray' => ['REMOTE_ADDR' => '1.2.3.4',],
239 'privateKey' => '124',
240 'assertHash' => 'b657a15cfe7ed1f7289c9aa51af14a9a26c966f4ddd74e495fba103d8e872a39',
241 'remoteIp' => '1.2.3.4',
242 'serverArray' => ['REMOTE_ADDR' => '1.2.3.4',],
243 'lifetime' => 2 * 24 * 60 * 60,
248 public function assertCookie($uid, $hash, $remoteIp, $lifetime)
250 self::assertArrayHasKey(Cookie::NAME, StaticCookie::$_COOKIE);
252 $data = json_decode(StaticCookie::$_COOKIE[Cookie::NAME]);
254 self::assertObjectHasAttribute('uid', $data);
255 self::assertEquals($uid, $data->uid);
256 self::assertObjectHasAttribute('hash', $data);
257 self::assertEquals($hash, $data->hash);
258 self::assertObjectHasAttribute('ip', $data);
259 self::assertEquals($remoteIp, $data->ip);
261 if (isset($lifetime) && $lifetime !== 0) {
262 self::assertLessThanOrEqual(time() + $lifetime, StaticCookie::$_EXPIRE);
264 self::assertLessThanOrEqual(time() + Cookie::DEFAULT_EXPIRE * 24 * 60 * 60, StaticCookie::$_EXPIRE);
269 * Test the set() method of the cookie class
271 * @dataProvider dataSet
273 public function testSet($serverKey, $uid, $password, $privateKey, $assertHash, $remoteIp, $serverArray, $lifetime)
275 $this->baseUrl->shouldReceive('getSSLPolicy')->andReturn(true)->once();
276 $this->config->shouldReceive('get')->with('system', 'site_prvkey')->andReturn($serverKey)->once();
277 $this->config->shouldReceive('get')->with('system', 'auth_cookie_lifetime', Cookie::DEFAULT_EXPIRE)->andReturn(Cookie::DEFAULT_EXPIRE)->once();
279 $cookie = new StaticCookie($this->config, $this->baseUrl, $serverArray);
280 self::assertInstanceOf(Cookie::class, $cookie);
282 $cookie->set($uid, $password, $privateKey, $lifetime);
284 self::assertCookie($uid, $assertHash, $remoteIp, $lifetime);
288 * Test two different set() of the cookie class (first set is invalid)
290 * @dataProvider dataSet
292 public function testDoubleSet($serverKey, $uid, $password, $privateKey, $assertHash, $remoteIp, $serverArray, $lifetime)
294 $this->baseUrl->shouldReceive('getSSLPolicy')->andReturn(true)->once();
295 $this->config->shouldReceive('get')->with('system', 'site_prvkey')->andReturn($serverKey)->once();
296 $this->config->shouldReceive('get')->with('system', 'auth_cookie_lifetime', Cookie::DEFAULT_EXPIRE)->andReturn(Cookie::DEFAULT_EXPIRE)->once();
298 $cookie = new StaticCookie($this->config, $this->baseUrl, $serverArray);
299 self::assertInstanceOf(Cookie::class, $cookie);
301 // Invalid set, should get overwritten
302 $cookie->set(-1, 'invalid', 'nothing', -234);
304 $cookie->set($uid, $password, $privateKey, $lifetime);
306 self::assertCookie($uid, $assertHash, $remoteIp, $lifetime);
310 * Test the clear() method of the cookie class
312 public function testClear()
314 StaticCookie::$_COOKIE = [
315 Cookie::NAME => 'test'
318 $this->baseUrl->shouldReceive('getSSLPolicy')->andReturn(true)->once();
319 $this->config->shouldReceive('get')->with('system', 'site_prvkey')->andReturn(24)->once();
320 $this->config->shouldReceive('get')->with('system', 'auth_cookie_lifetime', Cookie::DEFAULT_EXPIRE)->andReturn(Cookie::DEFAULT_EXPIRE)->once();
322 $cookie = new StaticCookie($this->config, $this->baseUrl);
323 self::assertInstanceOf(Cookie::class, $cookie);
325 self::assertEquals('test', StaticCookie::$_COOKIE[Cookie::NAME]);
326 self::assertEquals(null, StaticCookie::$_EXPIRE);
330 self::assertEmpty(StaticCookie::$_COOKIE[Cookie::NAME]);
331 self::assertEquals(-3600, StaticCookie::$_EXPIRE);