ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
All Data Structures Namespaces Files Functions Variables Modules Pages
ilWACTokenTest.php
Go to the documentation of this file.
1 <?php
2 // declare(strict_types=1);
3 /*
4  +-----------------------------------------------------------------------------+
5  | ILIAS open source |
6  +-----------------------------------------------------------------------------+
7  | Copyright (c) 1998-2009 ILIAS open source, University of Cologne |
8  | |
9  | This program is free software; you can redistribute it and/or |
10  | modify it under the terms of the GNU General Public License |
11  | as published by the Free Software Foundation; either version 2 |
12  | of the License, or (at your option) any later version. |
13  | |
14  | This program is distributed in the hope that it will be useful, |
15  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
16  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17  | GNU General Public License for more details. |
18  | |
19  | You should have received a copy of the GNU General Public License |
20  | along with this program; if not, write to the Free Software |
21  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
22  +-----------------------------------------------------------------------------+
23 */
24 require_once('./libs/composer/vendor/autoload.php');
25 
26 require_once('./Services/WebAccessChecker/classes/class.ilWACSignedPath.php');
27 require_once('./Services/WebAccessChecker/classes/class.ilWebAccessChecker.php');
28 require_once('./Services/WebAccessChecker/classes/class.ilWACSignedPath.php');
29 require_once('./Services/WebAccessChecker/classes/class.ilWACToken.php');
30 
38 use org\bovigo\vfs;
42 
54 class ilWACTokenTest extends MockeryTestCase
55 {
56  const ADDITIONAL_TIME = 1;
57  const LIFETIME = 2;
58  const SALT = 'SALT';
59  const CLIENT_NAME = 'client_name';
63  protected $backupGlobals = false;
67  protected $file_one;
79  protected $file_two;
83  protected $file_three;
87  protected $file_four;
91  protected $root;
95  private $http;
99  private $cookieFactory;
100 
101 
105  protected function setUp() : void
106  {
107  parent::setUp();
108 
109  $this->root = vfs\vfsStream::setup('ilias.de');
110  $this->file_one = vfs\vfsStream::newFile('data/client_name/mobs/mm_123/dummy.jpg')
111  ->at($this->root)->setContent('dummy');
112  $this->file_one_subfolder = vfs\vfsStream::newFile('data/client_name/mobs/mm_123/mobile/dummy.jpg')
113  ->at($this->root)->setContent('dummy');
114  $this->file_one_subfolder_two = vfs\vfsStream::newFile('data/client_name/mobs/mm_123/mobile/device/dummy.jpg')
115  ->at($this->root)->setContent('dummy');
116  $this->file_two = vfs\vfsStream::newFile('data/client_name/mobs/mm_123/dummy2.jpg')
117  ->at($this->root)->setContent('dummy2');
118  $this->file_three = vfs\vfsStream::newFile('data/client_name/mobs/mm_124/dummy.jpg')
119  ->at($this->root)->setContent('dummy');
120  $this->file_four = vfs\vfsStream::newFile('data/client_name/sec/ilBlog/mm_124/dummy.jpg')
121  ->at($this->root)->setContent('dummy');
122 
123  //setup container for HttpServiceAware classes
124  $container = new \ILIAS\DI\Container();
125  $container['http'] = function ($c) {
126  return Mockery::mock(GlobalHttpState::class);
127  };
128 
129  $this->http = $container['http'];
130 
131 
132  $GLOBALS["DIC"] = $container;
133 
134  $this->cookieFactory = Mockery::mock(CookieFactoryImpl::class);
135 
136  //because the cookie have no logic except cloning it self therefore it should be no problem to defer the function calls
137  $this->cookieFactory->shouldDeferMissing();
138 
139  ilWACToken::setSALT(self::SALT);
140  }
141 
142 
143  public function testWithoutSigning()
144  {
145  $ilWACSignedPath = new ilWACSignedPath(new ilWACPath($this->file_one->url()), $this->http, $this->cookieFactory);
146 
147  $cookieJar = Mockery::mock(CookieJar::class);
148 
149  $cookieJar
150  ->shouldReceive('getAll')
151  ->times(2)
152  ->withAnyArgs()
153  ->andReturn([]);
154 
155  $this->http->shouldReceive('cookieJar')
156  ->twice()
157  ->withNoArgs()
158  ->andReturn($cookieJar);
159 
160  $request = Mockery::mock(Psr\Http\Message\RequestInterface::class);
161  $request->shouldReceive('getCookieParams')
162  ->andReturn([]);
163 
164  $this->http->shouldReceive('request')
165  ->withNoArgs()
166  ->andReturn($request);
167 
168  $this->assertFalse($ilWACSignedPath->isSignedPath());
169  $this->assertFalse($ilWACSignedPath->isSignedPathValid());
170  $this->assertFalse($ilWACSignedPath->isFolderSigned());
171  $this->assertFalse($ilWACSignedPath->isFolderTokenValid());
172  }
173 
174 
175  public function testSomeBasics()
176  {
177  $query = 'myparam=1234';
178  $ilWACSignedPath = new ilWACSignedPath(new ilWACPath($this->file_four->url() . '?'
179  . $query), $this->http, $this->cookieFactory);
180 
181  $this->assertEquals('dummy.jpg', $ilWACSignedPath->getPathObject()->getFileName());
182  $this->assertEquals($query, $ilWACSignedPath->getPathObject()->getQuery());
183  $this->assertEquals('./data/' . self::CLIENT_NAME
184  . '/sec/ilBlog/mm_124/', $ilWACSignedPath->getPathObject()
185  ->getSecurePath());
186  $this->assertEquals('ilBlog', $ilWACSignedPath->getPathObject()->getSecurePathId());
187  $this->assertFalse($ilWACSignedPath->getPathObject()->isStreamable());
188  }
189 
190 
191  public function testTokenGeneration()
192  {
193  $ilWacPath = new ilWacPath($this->file_four->url());
194  $ilWACToken = new ilWACToken($ilWacPath->getPath(), self::CLIENT_NAME, 123456, 20);
195  $ilWACToken->generateToken();
196  $this->assertEquals('SALT-client_name-123456-20', $ilWACToken->getRawToken());
197  $this->assertEquals('./data/client_name/sec/ilBlog/mm_124/dummy.jpg', $ilWACToken->getId());
198 
199  $this->assertEquals(self::SALT, ilWACToken::getSALT());
200  $ilWACToken = new ilWACToken($ilWacPath->getPath(), self::CLIENT_NAME, 123456, 20);
201  $this->assertEquals('b541e2bae42ee222f9be959b7ad2ab8844cbb05b', $ilWACToken->getToken());
202  $this->assertEquals('e45b98f267dc891c8206c844f7df29ea', $ilWACToken->getHashedId());
203  }
204 
205 
206  public function testCookieGeneration()
207  {
208  $this->markTestSkipped('unable to use http cookies at this point');
209  $expected_cookies = [
210  '19ab58dae37d8d8cf931727c35514642',
211  '19ab58dae37d8d8cf931727c35514642ts',
212  '19ab58dae37d8d8cf931727c35514642ttl',
213  ];
214 
215  $cookieJar = Mockery::mock(CookieJar::class);
216 
217  $response = Mockery::mock(ResponseInterface::class);
218 
219  $this->http
220  ->shouldReceive('response')
221  ->times(3)
222  ->withNoArgs()
223  ->andReturn($response)
224  ->getMock();
225 
226  $cookieJar
227  ->shouldReceive('with')
228  ->times(3)
229  ->with(new CookieWrapper(SetCookie::create('')))
230  ->andReturnSelf()
231  ->getMock()
232 
233  ->shouldReceive('with')
234  ->times(3)
235  ->with(new CookieWrapper(SetCookie::create('')))
236  ->andReturnSelf()
237  ->getMock()
238 
239  ->shouldReceive('with')
240  ->times(3)
241  ->with(new CookieWrapper(SetCookie::create('')))
242  ->andReturnSelf()
243  ->getMock();
244 
245  $this->http->shouldReceive('cookieJar')
246  ->withNoArgs()
247  ->andReturn($cookieJar);
248 
249  ilWACSignedPath::signFolderOfStartFile($this->file_one->url());
250 
251  // in subfolder
252  ilWACSignedPath::signFolderOfStartFile($this->file_one_subfolder->url());
253 
254  // in sub-subfolder
255  ilWACSignedPath::signFolderOfStartFile($this->file_one_subfolder->url());
256  }
257 
258 
259  public function testFileToken()
260  {
263 
264  // Request within lifetime
265  $signed_path = ilWACSignedPath::signFile($this->file_one->url());
266  $ilWACSignedPath = new ilWACSignedPath(new ilWACPath($signed_path), $this->http, $this->cookieFactory);
267 
268  $this->assertTrue($ilWACSignedPath->isSignedPath());
269  $this->assertTrue($ilWACSignedPath->isSignedPathValid());
270  $this->assertEquals($ilWACSignedPath->getPathObject()->getClient(), self::CLIENT_NAME);
271  $this->assertFalse($ilWACSignedPath->getPathObject()->isInSecFolder());
272  $this->assertTrue($ilWACSignedPath->getPathObject()->isImage());
273  $this->assertFalse($ilWACSignedPath->getPathObject()->isAudio());
274  $this->assertFalse($ilWACSignedPath->getPathObject()->isVideo());
275  $this->assertTrue($ilWACSignedPath->getPathObject()->hasTimestamp());
276  $this->assertTrue($ilWACSignedPath->getPathObject()->hasToken());
277 
278  // Request after lifetime
279  $signed_path = ilWACSignedPath::signFile($this->file_four->url());
280  sleep($lifetime + self::ADDITIONAL_TIME);
281  $ilWACSignedPath = new ilWACSignedPath(new ilWACPath($signed_path), $this->http, $this->cookieFactory);
282  $this->assertTrue($ilWACSignedPath->isSignedPath());
283  $this->assertFalse($ilWACSignedPath->isSignedPathValid());
284  }
285 
286 
287 
291  public function testModifiedTimestampNoMod()
292  {
293  // self::markTestSkipped("WIP");
294  // return;
295  $ilWACSignedPath = new ilWACSignedPath(new ilWACPath($this->getModifiedSignedPath(0, 0)), $this->http, $this->cookieFactory);
296  $this->assertTrue($ilWACSignedPath->isSignedPath());
297  $this->assertTrue($ilWACSignedPath->isSignedPathValid());
298  }
299 
300 
305  {
306  // self::markTestSkipped("WIP");
307  // return;
308  $ilWACSignedPath = new ilWACSignedPath(new ilWACPath($this->getModifiedSignedPath(self::ADDITIONAL_TIME, 0)), $this->http, $this->cookieFactory);
309  $this->assertTrue($ilWACSignedPath->isSignedPath());
310  $this->assertFalse($ilWACSignedPath->isSignedPathValid());
311  }
312 
313 
315  {
316  // self::markTestSkipped("WIP");
317  // return;
318  $ilWACSignedPath = new ilWACSignedPath(new ilWACPath($this->getModifiedSignedPath(self::ADDITIONAL_TIME
319  * -1, 0)), $this->http, $this->cookieFactory);
320  $this->assertTrue($ilWACSignedPath->isSignedPath());
321  $this->assertFalse($ilWACSignedPath->isSignedPathValid());
322  }
323 
324 
325  public function testModifiedTTL()
326  {
327  // self::markTestSkipped("WIP");
328  // return;
329  $ilWACSignedPath = new ilWACSignedPath(new ilWACPath($this->getModifiedSignedPath(0, 1)), $this->http, $this->cookieFactory);
330  $this->assertTrue($ilWACSignedPath->isSignedPath());
331  $this->assertFalse($ilWACSignedPath->isSignedPathValid());
332  }
333 
334 
335  public function testModifiedTTLAndTimestamp()
336  {
337  // self::markTestSkipped("WIP");
338  // return;
339  $ilWACSignedPath = new ilWACSignedPath(new ilWACPath($this->getModifiedSignedPath(1, 1)), $this->http, $this->cookieFactory);
340  $this->assertTrue($ilWACSignedPath->isSignedPath());
341  $this->assertFalse($ilWACSignedPath->isSignedPathValid());
342  }
343 
344 
345  public function testModifiedToken()
346  {
347  // self::markTestSkipped("WIP");
348  // return;
349  $ilWACSignedPath = new ilWACSignedPath(new ilWACPath($this->getModifiedSignedPath(0, 0, md5('LOREM'))), $this->http, $this->cookieFactory);
350  $this->assertTrue($ilWACSignedPath->isSignedPath());
351  $this->assertFalse($ilWACSignedPath->isSignedPathValid());
352  }
353 
354 
361  protected function getModifiedSignedPath($add_ttl = 0, $add_timestamp = 0, $override_token = null)
362  {
364  $signed_path = ilWACSignedPath::signFile($this->file_one->url());
365 
366  $parts = parse_url($signed_path);
367  $path = $parts['path'];
368  $query = $parts['query'];
369  parse_str($query, $query_array);
370  $token = $override_token ? $override_token : $query_array['il_wac_token'];
371  $ttl = (int) $query_array['il_wac_ttl'];
372  $ts = (int) $query_array['il_wac_ts'];
373  $path_with_token = $path . '?il_wac_token=' . $token;
374 
375  $modified_ttl = $ttl + $add_ttl;
376  $modified_ts = $ts + $add_timestamp;
377 
378  return $path_with_token . '&il_wac_ttl=' . $modified_ttl . '&il_wac_ts=' . $modified_ts;
379  }
380 }
getModifiedSignedPath($add_ttl=0, $add_timestamp=0, $override_token=null)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$container
Definition: wac.php:13
TestCase for the ilWACTokenTest.
static signFolderOfStartFile($start_file_path)
static http()
Fetches the global http state from ILIAS.
$token
Definition: xapitoken.php:57
Class ilWACSignedPath.
if(!defined('PATH_SEPARATOR')) $GLOBALS['_PEAR_default_error_mode']
Definition: PEAR.php:64
static getSALT()
$query
static signFile($path_to_file)
Class ilWACToken.
static setSALT($salt)
static setTokenMaxLifetimeInSeconds($token_max_lifetime_in_seconds)
static getTokenMaxLifetimeInSeconds()
$response