aboutsummaryrefslogtreecommitdiffstats
path: root/tests/regex/ipv6_test.php
blob: d24217b346f0d955d88d82a8e56bcfd564b9432b (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
<?php
/**
*
* @package testing
* @copyright (c) 2010 phpBB Group
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
*
*/

require_once dirname(__FILE__) . '/../../phpBB/includes/functions.php';

class phpbb_regex_ipv6_test extends phpbb_test_case
{
	protected $regex;

	public function setUp()
	{
		$this->regex = get_preg_expression('ipv6');
	}

	public function positive_match_data()
	{
		return array(
			// Full length IPv6 address
			array('2001:0db8:85a3:0000:0000:8a2e:0370:1337'),
			array('0000:0000:0000:0000:0000:0000:0000:0001'),
			array('3FFE:0b00:0000:0000:0001:0000:0000:000a'),
			array('3ffe:0b00:0000:0000:0001:0000:0000:000a'),
			array('2002:0db8:0000:0000:0000:dead:1337:d00d'),

			// No leading zeroes in the group
			array('2001:db8:85a3:0:0:8a2e:370:1337'),
			array('2001:db8:85a3:c:d:8a2e:370:1337'),

			// Consecutive all-zero groups
			array('2001:db8:85a3::8a2e:370:1337'),
			array('1::2:3:4:5:6:7'),
			array('1::2:3:4:5:6'),
			array('1::2:3:4:5'),
			array('1::2:3:4'),
			array('1::2:3'),
			array('1::2'),

			// Last 32bit in dotted quad notation
			array('2001:db8:0:1::192.168.0.2'),

			// IPv4-compatible IPv6 address
			array('::13.1.68.3'),
			array('0:0:0:0:0:0:13.1.68.3'),

			// IPv4-mapped IPv6 address
			array('::ffff:c000:280'),
			array('::ffff:c000:0280'),
			array('::ffff:192.0.2.128'),
			array('0:0:0:0:0:ffff:c000:280'),
			array('0:0:0:0:0:ffff:c000:0280'),
			array('0:0:0:0:0:ffff:192.0.2.128'),
			array('0000:0000:0000:0000:0000:ffff:c000:280'),
			array('0000:0000:0000:0000:0000:ffff:c000:0280'),
			array('0000:0000:0000:0000:0000:ffff:192.0.2.128'),

			// No trailing zeroes
			array('fe80::'),
			array('2002::'),
			array('2001:db8::'),
			array('2001:0db8:1234::'),
			array('1:2:3:4:5:6::'),
			array('1:2:3:4:5::'),
			array('1:2:3:4::'),
			array('1:2:3::'),
			array('1:2::'),

			// No leading zeroes
			array('::2:3:4:5:6:7:8'),
			array('::2:3:4:5:6:7'),
			array('::2:3:4:5:6'),
			array('::2:3:4:5'),
			array('::2:3:4'),
			array('::2:3'),
			array('::1'),
			array('::8'),
			array('::c'),
			array('::abcd'),

			// All zeroes
			array('::'),
			array('0:0:0:0:0:0:0:0'),
			array('0000:0000:0000:0000:0000:0000:0000:0000'),

			// More tests
			array('2::10'),
			array('0:0::0:0:1'),
			array('0:0:0:0:0:0:0:1'),
			array('::ffff:0:0'),
		);
	}

	public function negative_match_data()
	{
		return array(
			// Empty address
			array(''),

			// IPv4 address
			array('192.168.0.2'),

			// Out of scope
			array('abcd:efgh:0000::0'),
			array('::ffff:192.168.255.256'),

			// Double ::
			array('2001::23de::2002'),
			array('3ffe:b00::1::b'),
			array('::1111:2222:3333:4444:5555:6666::'),

			// Too many blocks
			array('2001:0db8:85a3:08d3:1319:8a2e:0370:1337:4430'),

			// More tests
			array('02001:0000:1234:0000:0000:C1C0:ABCD:9876'),
			array('2001:0000:1234: 0000:0000:C1C0:ABCD:9876'),
			array('::ffff:192x168.255.255'),
		);
	}

	/**
	* @dataProvider positive_match_data
	*/
	public function test_positive_match($address)
	{
		$this->assertEquals(1, preg_match($this->regex, $address));
	}

	/**
	* @dataProvider negative_match_data
	*/
	public function test_negative_match($address)
	{
		$this->assertEquals(0, preg_match($this->regex, $address));
	}
}