ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
ilWACTokenTest.php
Go to the documentation of this file.
1 <?php
2 require_once('./libs/composer/vendor/autoload.php');
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 use org\bovigo\vfs;
25 
38 
39  const ADDITIONAL_TIME = 0.5;
40  const LIFETIME = 1;
41  const SALT = 'SALT';
42  const CLIENT_NAME = 'client_name';
46  protected $backupGlobals = false;
50  protected $file_one;
62  protected $file_two;
66  protected $file_three;
70  protected $file_four;
74  protected $root;
75 
76 
80  protected function setUp() {
81  require_once('./Services/WebAccessChecker/classes/class.ilWACSignedPath.php');
82  require_once('./Services/WebAccessChecker/classes/class.ilWebAccessChecker.php');
83  require_once('./Services/WebAccessChecker/classes/class.ilWACSignedPath.php');
84  require_once('./Services/WebAccessChecker/classes/class.ilWACToken.php');
85  require_once('./Services/WebAccessChecker/test/Token/mock/class.ilWACDummyCookie.php');
86  require_once('./libs/composer/vendor/autoload.php');
87  $this->root = vfs\vfsStream::setup('ilias.de');
88  $this->file_one = vfs\vfsStream::newFile('data/client_name/mobs/mm_123/dummy.jpg')->at($this->root)->setContent('dummy');
89  $this->file_one_subfolder = vfs\vfsStream::newFile('data/client_name/mobs/mm_123/mobile/dummy.jpg')->at($this->root)->setContent('dummy');
90  $this->file_one_subfolder_two = vfs\vfsStream::newFile('data/client_name/mobs/mm_123/mobile/device/dummy.jpg')->at($this->root)
91  ->setContent('dummy');
92  $this->file_two = vfs\vfsStream::newFile('data/client_name/mobs/mm_123/dummy2.jpg')->at($this->root)->setContent('dummy2');
93  $this->file_three = vfs\vfsStream::newFile('data/client_name/mobs/mm_124/dummy.jpg')->at($this->root)->setContent('dummy');
94  $this->file_four = vfs\vfsStream::newFile('data/client_name/sec/ilBlog/mm_124/dummy.jpg')->at($this->root)->setContent('dummy');
95  ilWACToken::setSALT(self::SALT);
96  parent::setUp();
97  }
98 
99 
100  public function testWithoutSigning() {
101  $ilWACSignedPath = new ilWACSignedPath(new ilWACPath($this->file_one->url()), new ilWACDummyCookie());
102 
103  $this->assertFalse($ilWACSignedPath->isSignedPath());
104  $this->assertFalse($ilWACSignedPath->isSignedPathValid());
105  $this->assertFalse($ilWACSignedPath->isFolderSigned());
106  $this->assertFalse($ilWACSignedPath->isFolderTokenValid());
107  }
108 
109 
110  public function testSomeBasics() {
111  $query = 'myparam=1234';
112  $ilWACSignedPath = new ilWACSignedPath(new ilWACPath($this->file_four->url() . '?' . $query), new ilWACDummyCookie());
113 
114  $this->assertEquals('dummy.jpg', $ilWACSignedPath->getPathObject()->getFileName());
115  $this->assertEquals($query, $ilWACSignedPath->getPathObject()->getQuery());
116  $this->assertEquals('./data/' . self::CLIENT_NAME . '/sec/ilBlog/mm_124/', $ilWACSignedPath->getPathObject()->getSecurePath());
117  $this->assertEquals('ilBlog', $ilWACSignedPath->getPathObject()->getSecurePathId());
118  $this->assertFalse($ilWACSignedPath->getPathObject()->isStreamable());
119 
120  $this->assertEquals('ilWACLogDummy', get_class(ilWACLog::getInstance()));
121 
122  $this->assertFalse(ilWebAccessChecker::isDEBUG());
123  $this->assertFalse(ilWACToken::DEBUG);
124  }
125 
126 
127  public function testTokenGeneration() {
129  $ilWacPath = new ilWacPath($this->file_four->url());
130  $ilWACToken = new ilWACToken($ilWacPath->getPath(), self::CLIENT_NAME, 123456, 20);
131  $ilWACToken->setIp('127.0.0.1');
132  $ilWACToken->generateToken();
133  $this->assertEquals('SALT-127.0.0.1-client_name-123456-20', $ilWACToken->getRawToken());
134  $this->assertEquals('./data/client_name/sec/ilBlog/mm_124/dummy.jpg', $ilWACToken->getId());
135 
137  $_SERVER['REMOTE_ADDR'] = '127.0.0.1';
138  $this->assertEquals(self::SALT, ilWACToken::getSALT());
139  $ilWACToken = new ilWACToken($ilWacPath->getPath(), self::CLIENT_NAME, 123456, 20);
140  $this->assertEquals('cd5a43304b232c785ef4f9796053b8bf5d6d829a', $ilWACToken->getToken());
141  $this->assertEquals('e45b98f267dc891c8206c844f7df29ea', $ilWACToken->getHashedId());
142  }
143 
144 
145  public function testCookieGeneration() {
148  $_SERVER['REMOTE_ADDR'] = '127.0.0.1';
149  $ilWACCookieInterface = new ilWACDummyCookie();
150  ilWACSignedPath::signFolderOfStartFile($this->file_one->url(), $ilWACCookieInterface);
151  $expected_cookies = array(
152  '19ab58dae37d8d8cf931727c35514642',
153  '19ab58dae37d8d8cf931727c35514642ts',
154  '19ab58dae37d8d8cf931727c35514642ttl',
155  );
156  $this->assertEquals($expected_cookies, array_keys($ilWACCookieInterface->getAll()));
157  // in subfolder
159  $_SERVER['REMOTE_ADDR'] = '127.0.0.1';
160  $ilWACCookieInterface = new ilWACDummyCookie();
161  ilWACSignedPath::signFolderOfStartFile($this->file_one_subfolder->url(), $ilWACCookieInterface);
162  $this->assertEquals($expected_cookies, array_keys($ilWACCookieInterface->getAll()));
163  // in sub-subfolder
165  $_SERVER['REMOTE_ADDR'] = '127.0.0.1';
166  $ilWACCookieInterface = new ilWACDummyCookie();
167  ilWACSignedPath::signFolderOfStartFile($this->file_one_subfolder->url(), $ilWACCookieInterface);
168  $this->assertEquals($expected_cookies, array_keys($ilWACCookieInterface->getAll()));
169  }
170 
171 
172  public function testFileToken() {
175 
176  // Request within lifetime
177  $signed_path = ilWACSignedPath::signFile($this->file_one->url());
178  $ilWACSignedPath = new ilWACSignedPath(new ilWACPath($signed_path));
179 
180  $this->assertTrue($ilWACSignedPath->isSignedPath());
181  $this->assertTrue($ilWACSignedPath->isSignedPathValid());
182  $this->assertEquals($ilWACSignedPath->getPathObject()->getClient(), self::CLIENT_NAME);
183  $this->assertFalse($ilWACSignedPath->getPathObject()->isInSecFolder());
184  $this->assertTrue($ilWACSignedPath->getPathObject()->isImage());
185  $this->assertFalse($ilWACSignedPath->getPathObject()->isAudio());
186  $this->assertFalse($ilWACSignedPath->getPathObject()->isVideo());
187  $this->assertTrue($ilWACSignedPath->getPathObject()->hasTimestamp());
188  $this->assertTrue($ilWACSignedPath->getPathObject()->hasToken());
189 
190  // Request after lifetime
191  $signed_path = ilWACSignedPath::signFile($this->file_four->url());
192  sleep($lifetime + self::ADDITIONAL_TIME);
193  $ilWACSignedPath = new ilWACSignedPath(new ilWACPath($signed_path));
194  $this->assertTrue($ilWACSignedPath->isSignedPath());
195  $this->assertFalse($ilWACSignedPath->isSignedPathValid());
196  }
197 
198 
199  public function testFolderToken() {
203 
204  $signed_path = $this->file_one->url();
206 
207  $ilWACSignedPath = new ilWACSignedPath(new ilWACPath($signed_path), new ilWACDummyCookie());
208  $this->assertTrue($ilWACSignedPath->isFolderSigned());
209  $this->assertTrue($ilWACSignedPath->isFolderTokenValid());
210  $this->assertEquals($ilWACSignedPath->getPathObject()->getClient(), self::CLIENT_NAME);
211  $this->assertFalse($ilWACSignedPath->getPathObject()->isInSecFolder());
212  $this->assertTrue($ilWACSignedPath->getPathObject()->isImage());
213  $this->assertFalse($ilWACSignedPath->getPathObject()->isAudio());
214  $this->assertFalse($ilWACSignedPath->getPathObject()->isVideo());
215  $this->assertTrue($ilWACSignedPath->getPathObject()->hasTimestamp());
216  $this->assertTrue($ilWACSignedPath->getPathObject()->hasToken());
217 
218  // Request after lifetime
220  sleep($lifetime + self::ADDITIONAL_TIME);
221  $ilWACSignedPath = new ilWACSignedPath(new ilWACPath($signed_path), new ilWACDummyCookie());
222  $this->assertTrue($ilWACSignedPath->isFolderSigned());
223  $this->assertFalse($ilWACSignedPath->isFolderTokenValid());
224 
225  // Revalidating cookie
226  $ilWACSignedPath->revalidatingFolderToken();
227  $ilWACSignedPath = new ilWACSignedPath(new ilWACPath($signed_path), new ilWACDummyCookie());
228  $this->assertTrue($ilWACSignedPath->isFolderSigned());
229  $this->assertTrue($ilWACSignedPath->isFolderTokenValid());
230 
231  // Check other file
232  $ilWACSignedPath = new ilWACSignedPath(new ilWACPath($this->file_three->url()), new ilWACDummyCookie());
233  $this->assertTrue($ilWACSignedPath->isFolderSigned());
234  $this->assertTrue($ilWACSignedPath->isFolderTokenValid());
235  }
236 
237 
238  public function testFolderTokenWithSecondFile() {
241  // Sign File One
242  $ilWACCookieInterface = new ilWACDummyCookie();
243  ilWACSignedPath::signFolderOfStartFile($this->file_one->url(), $ilWACCookieInterface);
244  // Check File Two
245  $file_two = $this->file_two->url();
246  $ilWACSignedPath = new ilWACSignedPath(new ilWACPath($file_two), $ilWACCookieInterface);
247  $this->assertTrue($ilWACSignedPath->isFolderSigned());
248  $this->assertTrue($ilWACSignedPath->isFolderTokenValid());
249 
250  // Request after lifetime
251  ilWACSignedPath::signFolderOfStartFile($file_two, $ilWACCookieInterface);
252  sleep($lifetime + self::ADDITIONAL_TIME);
253  $ilWACSignedPath = new ilWACSignedPath(new ilWACPath($file_two), $ilWACCookieInterface);
254  $this->assertTrue($ilWACSignedPath->isFolderSigned());
255  $this->assertFalse($ilWACSignedPath->isFolderTokenValid());
256 
257  // Revalidating cookie
258  $ilWACSignedPath->revalidatingFolderToken();
259  $this->assertTrue($ilWACSignedPath->isFolderSigned());
260  $this->assertTrue($ilWACSignedPath->isFolderTokenValid());
261 
262  // Check other file
263  $ilWACSignedPath = new ilWACSignedPath(new ilWACPath($this->file_three->url()), $ilWACCookieInterface);
264  $this->assertTrue($ilWACSignedPath->isFolderSigned());
265  $this->assertTrue($ilWACSignedPath->isFolderTokenValid());
266  }
267 
268 
269  public function testModifiedTimestampNoMod() {
270  $ilWACSignedPath = new ilWACSignedPath(new ilWACPath($this->getModifiedSignedPath(0, 0)));
271  $this->assertTrue($ilWACSignedPath->isSignedPath());
272  $this->assertTrue($ilWACSignedPath->isSignedPathValid());
273  }
274 
275 
276  public function testModifiedTimestampAddTime() {
277  $ilWACSignedPath = new ilWACSignedPath(new ilWACPath($this->getModifiedSignedPath(self::ADDITIONAL_TIME, 0)));
278  $this->assertTrue($ilWACSignedPath->isSignedPath());
279  $this->assertFalse($ilWACSignedPath->isSignedPathValid());
280  }
281 
282 
283  public function testModifiedTimestampSubTime() {
284  $ilWACSignedPath = new ilWACSignedPath(new ilWACPath($this->getModifiedSignedPath(self::ADDITIONAL_TIME * - 1, 0)));
285  $this->assertTrue($ilWACSignedPath->isSignedPath());
286  $this->assertFalse($ilWACSignedPath->isSignedPathValid());
287  }
288 
289 
290  public function testModifiedTTL() {
291  $ilWACSignedPath = new ilWACSignedPath(new ilWACPath($this->getModifiedSignedPath(0, 1)));
292  $this->assertTrue($ilWACSignedPath->isSignedPath());
293  $this->assertFalse($ilWACSignedPath->isSignedPathValid());
294  }
295 
296 
297  public function testModifiedTTLAndTimestamp() {
298  $ilWACSignedPath = new ilWACSignedPath(new ilWACPath($this->getModifiedSignedPath(1, 1)));
299  $this->assertTrue($ilWACSignedPath->isSignedPath());
300  $this->assertFalse($ilWACSignedPath->isSignedPathValid());
301  }
302 
303 
304  public function testModifiedToken() {
305  $ilWACSignedPath = new ilWACSignedPath(new ilWACPath($this->getModifiedSignedPath(0, 0, md5('LOREM'))));
306  $this->assertTrue($ilWACSignedPath->isSignedPath());
307  $this->assertFalse($ilWACSignedPath->isSignedPathValid());
308  }
309 
310 
317  protected function getModifiedSignedPath($add_ttl = 0, $add_timestamp = 0, $override_token = null) {
319  $signed_path = ilWACSignedPath::signFile($this->file_one->url());
320 
321  $parts = parse_url($signed_path);
322  $path = $parts['path'];
323  $query = $parts['query'];
324  parse_str($query, $query_array);
325  $token = $override_token ? $override_token : $query_array['il_wac_token'];
326  $ttl = (int)$query_array['il_wac_ttl'];
327  $ts = (int)$query_array['il_wac_ts'];
328  $path_with_token = $path . '?il_wac_token=' . $token;
329 
330  $modified_ttl = $ttl + $add_ttl;
331  $modified_ts = $ts + $add_timestamp;
332 
333  return $path_with_token . '&il_wac_ttl=' . $modified_ttl . '&il_wac_ts=' . $modified_ts;
334  }
335 }
static setSALT($SALT)
$path
Definition: aliased.php:25
if((!isset($_SERVER['DOCUMENT_ROOT'])) OR(empty($_SERVER['DOCUMENT_ROOT']))) $_SERVER['DOCUMENT_ROOT']
static getCookieMaxLifetimeInSeconds()
getModifiedSignedPath($add_ttl=0, $add_timestamp=0, $override_token=null)
static signFolderOfStartFile($start_file_path, ilWACCookieInterface $ilWACCookieInterface=null)
static getInstance()
Class ilWACPath.
TestCase for the ilWACTokenTest.
static setCookieMaxLifetimeInSeconds($cookie_max_lifetime_in_seconds)
Class ilWACSignedPath.
static getSALT()
static signFile($path_to_file)
Class ilWACToken.
Create styles array
The data for the language used.
Class ilWACDummyCookie.
static setTokenMaxLifetimeInSeconds($token_max_lifetime_in_seconds)
static getTokenMaxLifetimeInSeconds()