Welcome to mirror list, hosted at ThFree Co, Russian Federation.

IPTest.php « Unit « PHPUnit « tests - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 73c90af0d47d9fb171fff779624e3760f8bf954b (plain)
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php
/**
 * Matomo - free/libre analytics platform
 *
 * @backupGlobals enabled
 *
 * @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\IP;

/**
 * @group Core
 */
class IPTest extends \PHPUnit\Framework\TestCase
{
    /**
     * Dataprovider for long2ip test
     */
    public function getLong2IPTestData()
    {
        // a valid network address is either 4 or 16 bytes; those lines are intentionally left blank ;)
        return array(
            // invalid
            array(null, '0.0.0.0'),
            array("", '0.0.0.0'),

            // IPv4
            array("\x7f\x00\x00\x01", '127.0.0.1'),

            // IPv4-compatible (this transitional format is deprecated in RFC 4291, section 2.5.5.1)
            array("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xc0\xa8\x01\x01", '192.168.1.1'),

            // IPv4-mapped (RFC 4291, 2.5.5.2)
            array("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\xc0\xa8\x01\x02", '192.168.1.2'),

            // other IPv6 address
            array("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\x00\xc0\xa8\x01\x03", '0.0.0.0'),
            array("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\xc0\xa8\x01\x04", '0.0.0.0'),
            array("\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xc0\xa8\x01\x05", '0.0.0.0'),

            /*
             * We assume all stored IP addresses (pre-Piwik 1.4) were converted from UNSIGNED INT to VARBINARY.
             * The following is just for informational purposes.
             */

            // 192.168.1.0
            array('-1062731520', '0.0.0.0'),
            array('3232235776', '0.0.0.0'),

            // 10.10.10.10
            array('168430090', '0.0.0.0'),

            // 0.0.39.15 - this is the ambiguous case (i.e., 4 char string)
            array('9999', '57.57.57.57'),
            array("\x39\x39\x39\x39", '57.57.57.57'),

            // 0.0.3.231
            array('999', '0.0.0.0'),
            array("\x39\x39\x39", '0.0.0.0'),
        );
    }

    /**
     * Dataprovider for ip from header tests
     */
    public function getIpFromHeaderTestData()
    {
        return array(
            array('localhost inside LAN', array('127.0.0.1', '', null, null, '127.0.0.1')),
            array('outside LAN, no proxy', array('128.252.135.4', '', null, null, '128.252.135.4')),
            array('outside LAN, no (trusted) proxy', array('128.252.135.4', '137.18.2.13, 128.252.135.4', '', null, '128.252.135.4')),
            array('outside LAN, one trusted proxy', array('137.18.2.13', '137.18.2.13, 128.252.135.4, 192.168.1.10', 'HTTP_X_FORWARDED_FOR', null, '128.252.135.4')),
            array('outside LAN, proxy', array('192.168.1.10', '128.252.135.4, 192.168.1.10', 'HTTP_X_FORWARDED_FOR', null, '128.252.135.4')),
            array('outside LAN, misconfigured proxy', array('192.168.1.10', '128.252.135.4, 192.168.1.10, 192.168.1.10', 'HTTP_X_FORWARDED_FOR', null, '128.252.135.4')),
            array('outside LAN, multiple proxies', array('192.168.1.10', '128.252.135.4, 192.168.1.20, 192.168.1.10', 'HTTP_X_FORWARDED_FOR', '192.168.1.*', '128.252.135.4')),
            array('outside LAN, multiple proxies', array('[::ffff:7f00:10]', '128.252.135.4, [::ffff:7f00:20], [::ffff:7f00:10]', 'HTTP_X_FORWARDED_FOR', '::ffff:7f00:0/120', '128.252.135.4')),
        );
    }

    /**
     * @dataProvider getIpFromHeaderTestData
     */
    public function testGetIpFromHeader($description, $test)
    {
        $_SERVER['REMOTE_ADDR'] = $test[0];
        $_SERVER['HTTP_X_FORWARDED_FOR'] = $test[1];
        Config::getInstance()->General['proxy_client_headers'] = array($test[2]);
        Config::getInstance()->General['proxy_ips'] = array($test[3]);
        $this->assertEquals($test[4], IP::getIpFromHeader(), $description);
    }

    /**
     * Dataprovider
     * @return array
     */
    public function getIpTestData()
    {
        return array(
            array('0.0.0.0'),
            array('72.14.204.99'),
            array('127.0.0.1'),
            array('169.254.0.1'),
            array('208.80.152.2'),
            array('224.0.0.1'),
        );
    }

    /**
     * @dataProvider getIpTestData
     */
    public function testGetNonProxyIpFromHeader($ip)
    {
        $this->assertEquals($ip, IP::getNonProxyIpFromHeader($ip, array()));
    }

    /**
     * @dataProvider getIpTestData
     */
    public function testGetNonProxyIpFromHeader2($ip)
    {
        // 1.1.1.1 is not a trusted proxy
        $_SERVER['REMOTE_ADDR'] = '1.1.1.1';
        $_SERVER['HTTP_X_FORWARDED_FOR'] = '';
        $this->assertEquals('1.1.1.1', IP::getNonProxyIpFromHeader('1.1.1.1', array('HTTP_X_FORWARDED_FOR')));
    }

    /**
     * @dataProvider getIpTestData
     */
    public function testGetNonProxyIpFromHeader3($ip)
    {
        // 1.1.1.1 is a trusted proxy
        $_SERVER['REMOTE_ADDR'] = '1.1.1.1';

        $_SERVER['HTTP_X_FORWARDED_FOR'] = $ip;
        $this->assertEquals($ip, IP::getNonProxyIpFromHeader('1.1.1.1', array('HTTP_X_FORWARDED_FOR')), 'case 1');

        $_SERVER['HTTP_X_FORWARDED_FOR'] = '1.2.3.4, ' . $ip;
        $this->assertEquals('1.2.3.4', IP::getNonProxyIpFromHeader('1.1.1.1', array('HTTP_X_FORWARDED_FOR')), 'case 2');

        // misconfiguration
        $_SERVER['HTTP_X_FORWARDED_FOR'] = $ip . ', 1.1.1.1';
        $this->assertEquals($ip, IP::getNonProxyIpFromHeader('1.1.1.1', array('HTTP_X_FORWARDED_FOR')), 'case 3');
    }

    /**
     * See https://github.com/piwik/piwik/issues/8721
     */
    public function testGetNonProxyIpFromHeader4_ShouldReturnDefaultIp_IfDefaultIpIsGivenMultipleTimes()
    {
        // 1.1.1.1 is a trusted proxy
        $_SERVER['REMOTE_ADDR'] = '1.1.1.1';
        $_SERVER['HTTP_X_FORWARDED_FOR'] = $_SERVER['REMOTE_ADDR'];
        $_SERVER['HTTP_CF_CONNECTING_IP'] = $_SERVER['REMOTE_ADDR'];

        $this->assertEquals('1.1.1.1', IP::getNonProxyIpFromHeader('1.1.1.1', array('HTTP_X_FORWARDED_FOR', 'HTTP_CF_CONNECTING_IP')));
        unset($_SERVER['HTTP_CF_CONNECTING_IP']);
    }

    /**
     * Dataprovider for testGetFirstIpFromList
     */
    public function getFirstIpFromListTestData()
    {
        return array(
            array('', ''),
            array('127.0.0.1', '127.0.0.1'),
            array(' 127.0.0.1 ', '127.0.0.1'),
            array(' 192.168.1.1, 127.0.0.1', '192.168.1.1'),
            array('192.168.1.1 ,127.0.0.1 ', '192.168.1.1'),
            array('2001:db8:cafe::17 , 192.168.1.1', '2001:db8:cafe::17'),
            array('192.168.1.1,', '192.168.1.1'),
            array(',192.168.1.1,', '192.168.1.1'),
        );
    }

    /**
     * @dataProvider getFirstIpFromListTestData
     */
    public function testGetFirstIpFromList($csv, $expected)
    {
        // without excluded IPs
        $this->assertEquals($expected, IP::getFirstIpFromList($csv));

        // with excluded Ips
        $this->assertEquals($expected, IP::getFirstIpFromList($csv . ', 10.10.10.10', array('10.10.10.10')));
    }

    public function testGetFirstIpFromList_shouldReturnAnEmptyString_IfMultipleIpsAreGivenButAllAreExcluded()
    {
        // with excluded Ips
        $this->assertEquals('', IP::getFirstIpFromList('10.10.10.10, 10.10.10.10', array('10.10.10.10')));
    }
}