1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Tests\Unit;
use Piwik\Config;
use Piwik\Nonce;
use Piwik\Url;
/**
* @backupGlobals enabled
*/
class NonceTest extends \PHPUnit\Framework\TestCase
{
/**
* Dataprovider for acceptable origins test
*/
public function getAcceptableOriginsTestData()
{
return array(
// HTTP_HOST => expected
array('example.com', array('http://example.com', 'https://example.com', 'http://example.com:80', 'https://example.com:443', )),
array('example.com:80', array('http://example.com', 'https://example.com', 'http://example.com:80', 'https://example.com:80')),
array('example.com:443', array('http://example.com', 'https://example.com', 'https://example.com:443')),
array('example.com:8080', array('http://example.com', 'https://example.com', 'http://example.com:8080', 'https://example.com:8080')),
);
}
/**
* @dataProvider getAcceptableOriginsTestData
* @group Core
*/
public function test_getAcceptableOrigins($host, $expected)
{
Config::getInstance()->General['enable_trusted_host_check'] = 0;
Url::setHost($host);
Config::getInstance()->General['trusted_hosts'] = array('example.com');
$this->assertEquals($expected, Nonce::getAcceptableOrigins(), $host);
}
/**
* @dataProvider getTestDataForIsReferrerHostValid
* @group Core
*/
public function test_isReferrerHostValid($referrer, $expectedHost, $expectedResult)
{
$result = Nonce::isReferrerHostValid($referrer, $expectedHost);
$this->assertEquals($expectedResult, $result);
}
public function getTestDataForIsReferrerHostValid()
{
return [
['http://referrer.com', 'someotherreferrer.com', false],
['http://referrer.com/referrer/path', 'referrer.com', true],
['http://areferrer.com', 'referrer.com', false],
['http://sub.referrer.com', 'referrer.com', true],
['http://sub.referrer.com', 'sub.referrer.com', true],
['http://sub.referrer.com', 'a.sub.referrer.com', false],
['http://sub.referrer.com', 'sub2.referrer.com', false],
];
}
}
|