forked from jeremykendall/php-domain-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDomainTest.php
More file actions
511 lines (464 loc) · 16.4 KB
/
Copy pathDomainTest.php
File metadata and controls
511 lines (464 loc) · 16.4 KB
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
<?php
declare(strict_types=1);
namespace Pdp;
use PHPUnit\Framework\TestCase;
use TypeError;
/**
* @coversDefaultClass \Pdp\Domain
*/
final class DomainTest extends TestCase
{
/**
* @covers \Pdp\SyntaxError
* @dataProvider invalidDomainProvider
*/
public function testToAsciiThrowsException(string $domain): void
{
$this->expectException(SyntaxError::class);
Domain::fromIDNA2008($domain);
}
/**
* @return iterable<string,array{0:string}>
*/
public function invalidDomainProvider(): iterable
{
return [
'invalid IDN domain' => ['a⒈com'],
'invalid IDN domain full size' => ['%00.com'],
'invalid IDN domain full size rawurlencode ' => ['%ef%bc%85%ef%bc%94%ef%bc%91.com'],
];
}
public function testToUnicodeThrowsException(): void
{
$this->expectException(SyntaxError::class);
Domain::fromIDNA2008('xn--a-ecp.ru')->toUnicode();
}
public function testDomainInternalPhpMethod(): void
{
$domain = Domain::fromIDNA2008('www.ulb.ac.be');
/** @var Domain $generateDomain */
$generateDomain = eval('return '.var_export($domain, true).';');
self::assertEquals($domain, $generateDomain);
self::assertSame(['be', 'ac', 'ulb', 'www'], iterator_to_array($domain));
self::assertEquals('"www.ulb.ac.be"', json_encode($domain));
self::assertSame('www.ulb.ac.be', $domain->toString());
}
/**
* @dataProvider countableProvider
*
* @param string[] $labels
* @param ?string $domain
*/
public function testCountable(?string $domain, int $nbLabels, array $labels): void
{
$domain = Domain::fromIDNA2008($domain);
self::assertCount($nbLabels, $domain);
self::assertSame($labels, iterator_to_array($domain));
}
/**
* @return iterable<string,array{0:string|null, 1:int, 2:array<string>}>
*/
public function countableProvider(): iterable
{
return [
'null' => [null, 0, []],
'empty string' => ['', 1, ['']],
'simple' => ['foo.bar.baz', 3, ['baz', 'bar', 'foo']],
'unicode' => ['www.食狮.公司.cn', 4, ['cn', '公司', '食狮', 'www']],
];
}
public function testGetLabel(): void
{
$domain = Domain::fromIDNA2008('master.example.com');
self::assertSame('com', $domain->label(0));
self::assertSame('example', $domain->label(1));
self::assertSame('master', $domain->label(-1));
self::assertNull($domain->label(23));
self::assertNull($domain->label(-23));
}
public function testOffsets(): void
{
$domain = Domain::fromIDNA2008('master.com.example.com');
self::assertSame([0, 2], $domain->keys('com'));
self::assertSame([], $domain->keys('toto'));
self::assertSame([0, 1, 2, 3], $domain->keys());
}
public function testLabels(): void
{
self::assertSame([
'com',
'example',
'com',
'master',
], Domain::fromIDNA2008('master.com.example.com')->labels());
self::assertSame([], Domain::fromIDNA2008(null)->labels());
}
/**
* @dataProvider toUnicodeProvider
*
* @param ?string $domain
* @param ?string $expectedDomain
* @param ?string $expectedIDNDomain
*/
public function testToIDN(
?string $domain,
?string $expectedDomain,
?string $expectedIDNDomain
): void {
$domain = Domain::fromIDNA2008($domain);
self::assertSame($expectedDomain, $domain->value());
/** @var Domain $domainIDN */
$domainIDN = $domain->toUnicode();
self::assertSame($expectedIDNDomain, $domainIDN->value());
}
/**
* @return iterable<string,array{domain:string|null, expectedDomain:string|null, expectedIDNDomain:string|null}>
*/
public function toUnicodeProvider(): iterable
{
return [
'simple domain' => [
'domain' => 'www.ulb.ac.be',
'expectedDomain' => 'www.ulb.ac.be',
'expectedIDNDomain' => 'www.ulb.ac.be',
],
'ASCII to IDN domain' => [
'domain' => 'www.xn--85x722f.xn--55qx5d.cn',
'expectedDomain' => 'www.xn--85x722f.xn--55qx5d.cn',
'expectedIDNDomain' => 'www.食狮.公司.cn',
],
'IDN to IDN domain' => [
'domain' => 'www.食狮.公司.cn',
'expectedDomain' => 'www.食狮.公司.cn',
'expectedIDNDomain' => 'www.食狮.公司.cn',
],
'empty string domain and null suffix' => [
'domain' => '',
'expectedDomain' => '',
'expectedIDNDomain' => '',
],
'null domain and suffix' => [
'domain' => null,
'expectedDomain' => null,
'expectedIDNDomain' => null,
],
'domain with null suffix' => [
'domain' => 'www.xn--85x722f.xn--55qx5d.cn',
'expectedDomain' => 'www.xn--85x722f.xn--55qx5d.cn',
'expectedIDNDomain' => 'www.食狮.公司.cn',
],
'domain with URLencoded data' => [
'domain' => 'b%C3%A9b%C3%A9.be',
'expectedDomain' => 'bébé.be',
'expectedIDNDomain' => 'bébé.be',
],
];
}
/**
* @dataProvider toAsciiProvider
* @param ?string $domain
* @param ?string $expectedDomain
* @param ?string $expectedAsciiDomain
*/
public function testToAscii(
?string $domain,
?string $expectedDomain,
?string $expectedAsciiDomain
): void {
$domain = Domain::fromIDNA2008($domain);
self::assertSame($expectedDomain, $domain->value());
/** @var Domain $domainIDN */
$domainIDN = $domain->toAscii();
self::assertSame($expectedAsciiDomain, $domainIDN->value());
}
/**
* @return iterable<string,array{domain:string|null, expectedDomain:string|null, expectedIDNDomain:string|null}>
*/
public function toAsciiProvider(): iterable
{
return [
'simple domain' => [
'domain' => 'www.ulb.ac.be',
'expectedDomain' => 'www.ulb.ac.be',
'expectedIDNDomain' => 'www.ulb.ac.be',
],
'ASCII to ASCII domain' => [
'domain' => 'www.xn--85x722f.xn--55qx5d.cn',
'expectedDomain' => 'www.xn--85x722f.xn--55qx5d.cn',
'expectedIDNDomain' => 'www.xn--85x722f.xn--55qx5d.cn',
],
'ASCII to IDN domain' => [
'domain' => 'www.食狮.公司.cn',
'expectedDomain' => 'www.食狮.公司.cn',
'expectedIDNDomain' => 'www.xn--85x722f.xn--55qx5d.cn',
],
'null domain and suffix' => [
'domain' => null,
'expectedDomain' => null,
'expectedIDNDomain' => null,
],
'domain with null suffix' => [
'domain' => 'www.食狮.公司.cn',
'expectedDomain' => 'www.食狮.公司.cn',
'expectedIDNDomain' => 'www.xn--85x722f.xn--55qx5d.cn',
],
];
}
/**
* @dataProvider withLabelWorksProvider
*
* @param ?string $expected
*/
public function testWithLabelWorks(DomainName $domain, int $key, string $label, ?string $expected): void
{
$result = $domain->withLabel($key, $label);
self::assertSame($expected, $result->value());
}
/**
* @return iterable<string,array{domain:DomainName, key:int, label:string, expected:string}>
*/
public function withLabelWorksProvider(): iterable
{
$base_domain = Domain::fromIDNA2008('www.example.com');
return [
'null domain' => [
'domain' => Domain::fromIDNA2008(null),
'key' => 0,
'label' => 'localhost',
'expected' => 'localhost',
],
'simple replace positive offset' => [
'domain' => $base_domain,
'key' => 2,
'label' => 'shop',
'expected' => 'shop.example.com',
],
'simple replace negative offset' => [
'domain' => $base_domain,
'key' => -1,
'label' => 'shop',
'expected' => 'shop.example.com',
],
'simple addition positive offset' => [
'domain' => $base_domain,
'key' => 3,
'label' => 'shop',
'expected' => 'shop.www.example.com',
],
'simple addition negative offset' => [
'domain' => $base_domain,
'key' => -4,
'label' => 'shop',
'expected' => 'www.example.com.shop',
],
'simple replace remove PSL info' => [
'domain' => $base_domain,
'key' => 0,
'label' => 'fr',
'expected' => 'www.example.fr',
],
'replace without any change' => [
'domain' => $base_domain,
'key' => 2,
'label' => 'www',
'expected' => 'www.example.com',
],
'simple update IDN (1)' => [
'domain' => $base_domain,
'key' => 2,
'label' => 'рф',
'expected' => 'xn--p1ai.example.com',
],
'simple update IDN (2)' => [
'domain' => Domain::fromIDNA2008('www.bébé.be'),
'key' => 2,
'label' => 'xn--p1ai',
'expected' => 'рф.bébé.be',
],
'replace a domain with multiple label' => [
'domain' => $base_domain,
'key' => -1,
'label' => 'www.shop',
'expected' => 'www.shop.example.com',
],
];
}
public function testWithLabelFailsWithTypeError(): void
{
$this->expectException(TypeError::class);
Domain::fromIDNA2008('example.com')->withLabel(1, new \stdClass());
}
public function testWithLabelFailsWithInvalidKey(): void
{
$this->expectException(SyntaxError::class);
Domain::fromIDNA2008('example.com')->withLabel(-4, 'www');
}
public function testWithLabelFailsWithInvalidLabel2(): void
{
$this->expectException(SyntaxError::class);
Domain::fromIDNA2008('example.com')->withLabel(-1, '');
}
/**
* @dataProvider validAppend
*/
public function testAppend(string $raw, string $append, string $expected): void
{
self::assertSame($expected, Domain::fromIDNA2008($raw)->append($append)->toString());
}
/**
* @return iterable<array-key, array{0:string, 1:string, 2:string}>
*/
public function validAppend(): iterable
{
return [
['secure.example.com', '8.8.8.8', 'secure.example.com.8.8.8.8'],
['secure.example.com', 'master', 'secure.example.com.master'],
['secure.example.com', 'master.', 'secure.example.com.master.'],
['example.com', '', 'example.com.'],
];
}
/**
* @dataProvider validPrepend
*/
public function testPrepend(string $raw, string $prepend, string $expected): void
{
self::assertSame($expected, Domain::fromIDNA2008($raw)->prepend($prepend)->toString());
}
/**
* @return iterable<array-key, array{0:string, 1:string, 2:string}>
*/
public function validPrepend(): iterable
{
return [
['secure.example.com', 'master', 'master.secure.example.com'],
['secure.example.com', '127.0.0.1', '127.0.0.1.secure.example.com'],
['secure.example.com.', 'master', 'master.secure.example.com.'],
];
}
/**
* @dataProvider withoutLabelWorksProvider
* @param ?string $expected
*/
public function testwithoutLabelWorks(DomainName $domain, int $key, ?string $expected): void
{
$result = $domain->withoutLabel($key);
self::assertSame($expected, $result->value());
}
/**
* @return iterable<string,array{domain:DomainName, key:int, expected:string}>
*/
public function withoutLabelWorksProvider(): iterable
{
$base_domain = Domain::fromIDNA2008('www.example.com');
return [
'simple removal positive offset' => [
'domain' => $base_domain,
'key' => 2,
'expected' => 'example.com',
],
'simple removal negative offset' => [
'domain' => $base_domain,
'key' => -1,
'expected' => 'example.com',
],
'simple removal strip PSL info positive offset' => [
'domain' => $base_domain,
'key' => 0,
'expected' => 'www.example',
],
'simple removal strip PSL info negative offset' => [
'domain' => $base_domain,
'key' => -3,
'expected' => 'www.example',
],
];
}
public function testwithoutLabelFailsWithInvalidKey(): void
{
$this->expectException(SyntaxError::class);
Domain::fromIDNA2008('example.com')->withoutLabel(-3);
}
public function testwithoutLabelWorksWithMultipleKeys(): void
{
self::assertNull(Domain::fromIDNA2008('www.example.com')->withoutLabel(0, 1, 2)->value());
}
/**
* @dataProvider resolveCustomIDNAOptionsProvider
* @param ?string $expectedContent
* @param ?string $expectedAscii
* @param ?string $expectedUnicode
* @param ?string $expectedWithLabel
*/
public function testResolveWorksWithCustomIDNAOptions(
string $domainName,
string $withLabel,
?string $expectedContent,
?string $expectedAscii,
?string $expectedUnicode,
?string $expectedWithLabel
): void {
$domain = Domain::fromIDNA2008($domainName);
self::assertSame($expectedContent, $domain->value());
self::assertSame($expectedAscii, $domain->toAscii()->value());
self::assertSame($expectedUnicode, $domain->toUnicode()->value());
self::assertSame($expectedWithLabel, $domain->withLabel(-1, $withLabel)->value());
}
/**
* @return iterable<string,array<string>>
*/
public function resolveCustomIDNAOptionsProvider(): iterable
{
return [
'without deviation characters' => [
'example.com',
'größe',
'example.com',
'example.com',
'example.com',
'xn--gre-6ka8i.com',
],
'without deviation characters with label' => [
'www.example.com',
'größe',
'www.example.com',
'www.example.com',
'www.example.com',
'xn--gre-6ka8i.example.com',
],
'with deviation in domain' => [
'www.faß.de',
'größe',
'www.faß.de',
'www.xn--fa-hia.de',
'www.faß.de',
'größe.faß.de',
],
'with deviation in label' => [
'faß.test.de',
'größe',
'faß.test.de',
'xn--fa-hia.test.de',
'faß.test.de',
'größe.test.de',
],
];
}
public function testWithIDNAOptions(): void
{
self::assertNotEquals(Domain::fromIDNA2003('example.com'), Domain::fromIDNA2008('example.com'));
}
public function testSlice(): void
{
$domain = Domain::fromIDNA2008('ulb.ac.be');
self::assertSame($domain->toString(), $domain->slice(-3)->toString());
self::assertSame($domain->toString(), $domain->slice(0)->toString());
self::assertSame('ulb.ac', $domain->slice(1)->toString());
self::assertSame('ulb', $domain->slice(-1)->toString());
self::assertSame('be', $domain->slice(-3, 1)->toString());
}
public function testSliceThrowsOnOverFlow(): void
{
$this->expectException(SyntaxError::class);
Domain::fromIDNA2008('ulb.ac.be')->slice(5);
}
}