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
200
201
|
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Tests\Integration;
use Piwik\Http;
use Piwik\Piwik;
/**
* @group Core
*/
class EmailValidatorTest extends \PHPUnit_Framework_TestCase
{
protected function isValid($email)
{
return Piwik::isValidEmailString($email);
}
private function getAllTlds()
{
/** @var array $response */
$response = \Piwik\Http::sendHttpRequest("http://data.iana.org/TLD/tlds-alpha-by-domain.txt", 30, null, null, null, null, null, true);
$this->assertEquals("200", $response['status']);
$tlds = explode("\n", $response['data']);
foreach ($tlds as $key => $tld) {
if (strpos($tld, '#') !== false || $tld == "") {
unset($tlds[$key]);
}
}
return $tlds;
}
public function test_allCurrentTlds(){
$tlds = $this->getAllTlds();
if (count($tlds) === 0) {
$this->markTestSkipped("Couldn't get TLD list");
}
foreach ($tlds as $key => $tld) {
if (strpos(mb_strtolower($tld), 'xn--') !== 0) {
$tld = mb_strtolower($tld);
}
$this->assertTrue(
$this->isValid('test@example.' . idn_to_utf8($tld))
);
}
}
public function test_invalidTld(){
$tlds = [
strval(bin2hex(openssl_random_pseudo_bytes(64))), //generates 128 bit length string
'-tld-cannot-start-from-hypen',
'ąęśćżźł-there-is-no-such-idn',
'xn--fd67as67fdsa', //no such idn punycode
'!@#-inavlid-chars-in-tld',
'no spaces in tld allowed',
'no--double--hypens--allowed'
];
if (count($tlds) === 0) {
$this->markTestSkipped("Couldn't get TLD list");
}
foreach ($tlds as $key => $tld) {
if (strpos(mb_strtolower($tld), 'xn--') !== 0) {
$tld = mb_strtolower($tld);
}
$this->assertFalse(
$this->isValid('test@example.' . idn_to_utf8($tld))
);
}
}
public function test_isValid_validStandard()
{
$this->assertTrue($this->isValid('test@example.com'));
}
public function test_isValid_unknownTld()
{
$this->assertTrue($this->isValid('test@example.unknown'));
}
public function test_isValid_validUpperCaseLocalPart()
{
$this->assertTrue($this->isValid('TEST@example.com'));
}
public function test_isValid_validNumericLocalPart()
{
$this->assertTrue($this->isValid('1234567890@example.com'));
}
public function test_isValid_validTaggedLocalPart()
{
$this->assertTrue($this->isValid('test+test@example.com'));
}
public function test_isValid_validQmailLocalPart()
{
$this->assertTrue($this->isValid('test-test@example.com'));
}
public function test_isValid_validUnusualCharactersInLocalPart()
{
$this->assertTrue($this->isValid('t*est@example.com'));
$this->assertTrue($this->isValid('+1~1+@example.com'));
$this->assertTrue($this->isValid('{_test_}@example.com'));
}
public function test_isValid_validQuotedLocalPart()
{
$this->assertTrue($this->isValid('"[[ test ]]"@example.com'));
}
public function test_isValid_validAtomisedLocalPart()
{
$this->assertTrue($this->isValid('test.test@example.com'));
}
public function test_isValid_validQuotedAtLocalPart()
{
$this->assertTrue($this->isValid('"test@test"@example.com'));
}
public function test_isValid_validMultipleLabelDomain()
{
$this->assertTrue($this->isValid('test@example.example.com'));
$this->assertTrue($this->isValid('test@example.example.example.com'));
}
public function test_isValid_invalidTooLong()
{
$this->assertFalse($this->isValid('12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345@example.com'));
}
public function test_isValid_invalidTooShort()
{
$this->assertFalse($this->isValid('@a'));
}
public function test_isValid_invalidNoAtSymbol()
{
$this->assertFalse($this->isValid('test.example.com'));
}
public function test_isValid_invalidBlankAtomInLocalPart()
{
$this->assertFalse($this->isValid('test.@example.com'));
$this->assertFalse($this->isValid('test..test@example.com'));
$this->assertFalse($this->isValid('.test@example.com'));
}
public function test_isValid_invalidMultipleAtSymbols()
{
$this->assertFalse($this->isValid('test@test@example.com'));
$this->assertFalse($this->isValid('test@@example.com'));
}
public function test_isValid_invalidInvalidCharactersInLocalPart()
{
$this->assertFalse($this->isValid('-- test --@example.com'));
$this->assertFalse($this->isValid('[test]@example.com'));
$this->assertFalse($this->isValid('"test"test"@example.com'));
$this->assertFalse($this->isValid('()[]\;:,<>@example.com'));
}
public function test_isValid_invalidDomainLabelTooShort()
{
$this->assertFalse($this->isValid('test@.'));
$this->assertFalse($this->isValid('test@example.'));
$this->assertFalse($this->isValid('test@.org'));
}
public function test_isValid_invalidLocalPartTooLong()
{
$this->assertFalse($this->isValid('12345678901234567890123456789012345678901234567890123456789012345@example.com')); // 64 characters is maximum length for local part
}
public function test_isValid_invalidDomainLabelTooLong()
{
$this->assertFalse($this->isValid('test@123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012.com')); // 255 characters is maximum length for domain. This is 256.
}
public function test_isValid_invalidTooFewLabelsInDomain()
{
$this->assertFalse($this->isValid('test@example'));
}
public function test_isValid_invalidUnpartneredSquareBracketIp()
{
$this->assertFalse($this->isValid('test@[123.123.123.123'));
$this->assertFalse($this->isValid('test@123.123.123.123]'));
}
}
|