PageRenderTime 18ms CodeModel.GetById 1ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/Zend/Validate/HostnameTest.php

https://bitbucket.org/dbaltas/zend-framework-1.x-on-git
PHP | 465 lines | 295 code | 42 blank | 128 comment | 1 complexity | 2d88ee7186df5365f30f02a246aab8fe MD5 | raw file
  1<?php
  2/**
  3 * Zend Framework
  4 *
  5 * LICENSE
  6 *
  7 * This source file is subject to the new BSD license that is bundled
  8 * with this package in the file LICENSE.txt.
  9 * It is also available through the world-wide-web at this URL:
 10 * http://framework.zend.com/license/new-bsd
 11 * If you did not receive a copy of the license and are unable to
 12 * obtain it through the world-wide-web, please send an email
 13 * to license@zend.com so we can send you a copy immediately.
 14 *
 15 * @category   Zend
 16 * @package    Zend_Validate
 17 * @subpackage UnitTests
 18 * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
 19 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 20 * @version    $Id: HostnameTest.php 25060 2012-11-02 21:23:09Z rob $
 21 */
 22
 23/**
 24 * @see Zend_Validate_Hostname
 25 */
 26require_once 'Zend/Validate/Hostname.php';
 27
 28
 29/**
 30 * @category   Zend
 31 * @package    Zend_Validate
 32 * @subpackage UnitTests
 33 * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
 34 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 35 * @group      Zend_Validate
 36 */
 37class Zend_Validate_HostnameTest extends PHPUnit_Framework_TestCase
 38{
 39    /**
 40     * Default instance created for all test methods
 41     *
 42     * @var Zend_Validate_Hostname
 43     */
 44    protected $_validator;
 45
 46    /**
 47     * Creates a new Zend_Validate_Hostname object for each test method
 48     *
 49     * @return void
 50     */
 51    public function setUp()
 52    {
 53        $this->_origEncoding = iconv_get_encoding('internal_encoding');
 54        $this->_validator = new Zend_Validate_Hostname();
 55    }
 56
 57    /**
 58     * Reset iconv
 59     */
 60    public function tearDown()
 61    {
 62        iconv_set_encoding('internal_encoding', $this->_origEncoding);
 63    }
 64
 65    /**
 66     * Ensures that the validator follows expected behavior
 67     *
 68     * @return void
 69     */
 70    public function testBasic()
 71    {
 72        $valuesExpected = array(
 73            array(Zend_Validate_Hostname::ALLOW_IP, true, array('1.2.3.4', '10.0.0.1', '255.255.255.255')),
 74            array(Zend_Validate_Hostname::ALLOW_IP, false, array('1.2.3.4.5', '0.0.0.256')),
 75            array(Zend_Validate_Hostname::ALLOW_DNS, true, array('example.com', 'example.museum', 'd.hatena.ne.jp')),
 76            array(Zend_Validate_Hostname::ALLOW_DNS, false, array('localhost', 'localhost.localdomain', '1.2.3.4', 'domain.invalid')),
 77            array(Zend_Validate_Hostname::ALLOW_LOCAL, true, array('localhost', 'localhost.localdomain', 'example.com')),
 78            array(Zend_Validate_Hostname::ALLOW_ALL, true, array('localhost', 'example.com', '1.2.3.4')),
 79            array(Zend_Validate_Hostname::ALLOW_LOCAL, false, array('local host', 'example,com', 'exam_ple.com'))
 80        );
 81        foreach ($valuesExpected as $element) {
 82            $validator = new Zend_Validate_Hostname($element[0]);
 83            foreach ($element[2] as $input) {
 84                $this->assertEquals($element[1], $validator->isValid($input), implode("\n", $validator->getMessages()) . $input);
 85            }
 86        }
 87    }
 88
 89    public function testCombination()
 90    {
 91        $valuesExpected = array(
 92            array(Zend_Validate_Hostname::ALLOW_DNS | Zend_Validate_Hostname::ALLOW_LOCAL, true, array('domain.com', 'localhost', 'local.localhost')),
 93            array(Zend_Validate_Hostname::ALLOW_DNS | Zend_Validate_Hostname::ALLOW_LOCAL, false, array('1.2.3.4', '255.255.255.255')),
 94            array(Zend_Validate_Hostname::ALLOW_DNS | Zend_Validate_Hostname::ALLOW_IP, true, array('1.2.3.4', '255.255.255.255')),
 95            array(Zend_Validate_Hostname::ALLOW_DNS | Zend_Validate_Hostname::ALLOW_IP, false, array('localhost', 'local.localhost'))
 96            );
 97        foreach ($valuesExpected as $element) {
 98            $validator = new Zend_Validate_Hostname($element[0]);
 99            foreach ($element[2] as $input) {
100                $this->assertEquals($element[1], $validator->isValid($input), implode("\n", $validator->getMessages()) . $input);
101            }
102        }
103    }
104
105    /**
106     * Ensure the dash character tests work as expected
107     *
108     */
109    public function testDashes()
110    {
111        $valuesExpected = array(
112            array(Zend_Validate_Hostname::ALLOW_DNS, true, array('domain.com', 'doma-in.com')),
113            array(Zend_Validate_Hostname::ALLOW_DNS, false, array('-domain.com', 'domain-.com', 'do--main.com'))
114            );
115        foreach ($valuesExpected as $element) {
116            $validator = new Zend_Validate_Hostname($element[0]);
117            foreach ($element[2] as $input) {
118                $this->assertEquals($element[1], $validator->isValid($input), implode("\n", $validator->getMessages()) . $input);
119            }
120        }
121    }
122
123    /**
124     * Ensures that getMessages() returns expected default value
125     *
126     * @return void
127     */
128    public function testGetMessages()
129    {
130        $this->assertEquals(array(), $this->_validator->getMessages());
131    }
132
133    /**
134     * Ensure the IDN check works as expected
135     *
136     */
137    public function testIDN()
138    {
139        $validator = new Zend_Validate_Hostname();
140
141        // Check IDN matching
142        $valuesExpected = array(
143            array(true, array('bürger.de', 'hãllo.de', 'hållo.se')),
144            array(true, array('bÜrger.de', 'hÃllo.de', 'hÅllo.se')),
145            array(false, array('hãllo.se', 'bürger.lt', 'hãllo.uk'))
146            );
147        foreach ($valuesExpected as $element) {
148            foreach ($element[1] as $input) {
149                $this->assertEquals($element[0], $validator->isValid($input), implode("\n", $validator->getMessages()) . $input);
150            }
151        }
152
153        // Check no IDN matching
154        $validator->setValidateIdn(false);
155        $valuesExpected = array(
156            array(false, array('bürger.de', 'hãllo.de', 'hållo.se'))
157            );
158        foreach ($valuesExpected as $element) {
159            foreach ($element[1] as $input) {
160                $this->assertEquals($element[0], $validator->isValid($input), implode("\n", $validator->getMessages()) . $input);
161            }
162        }
163
164        // Check setting no IDN matching via constructor
165        unset($validator);
166        $validator = new Zend_Validate_Hostname(Zend_Validate_Hostname::ALLOW_DNS, false);
167        $valuesExpected = array(
168            array(false, array('bürger.de', 'hãllo.de', 'hållo.se'))
169            );
170        foreach ($valuesExpected as $element) {
171            foreach ($element[1] as $input) {
172                $this->assertEquals($element[0], $validator->isValid($input), implode("\n", $validator->getMessages()) . $input);
173            }
174        }
175    }
176
177    /**
178     * Ensure the IDN check works on ressource files as expected
179     *
180     */
181    public function testRessourceIDN()
182    {
183        $validator = new Zend_Validate_Hostname();
184
185        // Check IDN matching
186        $valuesExpected = array(
187            array(true, array('bürger.com', 'hãllo.com', 'hållo.com')),
188            array(true, array('bÜrger.com', 'hÃllo.com', 'hÅllo.com')),
189            array(false, array('hãllo.lt', 'bürger.lt', 'hãllo.lt'))
190            );
191        foreach ($valuesExpected as $element) {
192            foreach ($element[1] as $input) {
193                $this->assertEquals($element[0], $validator->isValid($input), implode("\n", $validator->getMessages()) . $input);
194            }
195        }
196
197        // Check no IDN matching
198        $validator->setValidateIdn(false);
199        $valuesExpected = array(
200            array(false, array('bürger.com', 'hãllo.com', 'hållo.com'))
201            );
202        foreach ($valuesExpected as $element) {
203            foreach ($element[1] as $input) {
204                $this->assertEquals($element[0], $validator->isValid($input), implode("\n", $validator->getMessages()) . $input);
205            }
206        }
207
208        // Check setting no IDN matching via constructor
209        unset($validator);
210        $validator = new Zend_Validate_Hostname(Zend_Validate_Hostname::ALLOW_DNS, false);
211        $valuesExpected = array(
212            array(false, array('bürger.com', 'hãllo.com', 'hållo.com'))
213            );
214        foreach ($valuesExpected as $element) {
215            foreach ($element[1] as $input) {
216                $this->assertEquals($element[0], $validator->isValid($input), implode("\n", $validator->getMessages()) . $input);
217            }
218        }
219    }
220
221    /**
222     * Ensure the TLD check works as expected
223     *
224     */
225    public function testTLD()
226    {
227        $validator = new Zend_Validate_Hostname();
228
229        // Check TLD matching
230        $valuesExpected = array(
231            array(true, array('domain.co.uk', 'domain.uk.com', 'domain.tl', 'domain.zw')),
232            array(false, array('domain.xx', 'domain.zz', 'domain.madeup'))
233            );
234        foreach ($valuesExpected as $element) {
235            foreach ($element[1] as $input) {
236                $this->assertEquals($element[0], $validator->isValid($input), implode("\n", $validator->getMessages()) . $input);
237            }
238        }
239
240        // Check no TLD matching
241        $validator->setValidateTld(false);
242        $valuesExpected = array(
243            array(true, array('domain.xx', 'domain.zz', 'domain.madeup'))
244            );
245        foreach ($valuesExpected as $element) {
246            foreach ($element[1] as $input) {
247                $this->assertEquals($element[0], $validator->isValid($input), implode("\n", $validator->getMessages()) . $input);
248            }
249        }
250
251        // Check setting no TLD matching via constructor
252        unset($validator);
253        $validator = new Zend_Validate_Hostname(Zend_Validate_Hostname::ALLOW_DNS, true, false);
254        $valuesExpected = array(
255            array(true, array('domain.xx', 'domain.zz', 'domain.madeup'))
256            );
257        foreach ($valuesExpected as $element) {
258            foreach ($element[1] as $input) {
259                $this->assertEquals($element[0], $validator->isValid($input), implode("\n", $validator->getMessages()) . $input);
260            }
261        }
262    }
263
264    /**
265     * Ensures that getAllow() returns expected default value
266     *
267     * @return void
268     */
269    public function testGetAllow()
270    {
271        $this->assertEquals(Zend_Validate_Hostname::ALLOW_DNS, $this->_validator->getAllow());
272    }
273
274    /**
275     * Test changed with ZF-6676, as IP check is only involved when IP patterns match
276     *
277     * @group ZF-2861
278     * @group ZF-6676
279     */
280    public function testValidatorMessagesShouldBeTranslated()
281    {
282        require_once 'Zend/Translate.php';
283        $translations = array(
284            'hostnameInvalidLocalName' => 'this is the IP error message',
285        );
286        $translator = new Zend_Translate('array', $translations);
287        $this->_validator->setTranslator($translator);
288
289        $this->_validator->isValid('0.239,512.777');
290        $messages = $this->_validator->getMessages();
291        $found = false;
292        foreach ($messages as $code => $message) {
293            if (array_key_exists($code, $translations)) {
294                $found = true;
295                break;
296            }
297        }
298
299        $this->assertTrue($found);
300        $this->assertEquals($translations[$code], $message);
301    }
302
303    /**
304     * @group ZF-6033
305     */
306    public function testNumberNames()
307    {
308        $validator = new Zend_Validate_Hostname();
309
310        // Check TLD matching
311        $valuesExpected = array(
312            array(true, array('www.danger1.com', 'danger.com', 'www.danger.com')),
313            array(false, array('www.danger1com', 'dangercom', 'www.dangercom'))
314            );
315        foreach ($valuesExpected as $element) {
316            foreach ($element[1] as $input) {
317                $this->assertEquals($element[0], $validator->isValid($input), implode("\n", $validator->getMessages()) . $input);
318            }
319        }
320    }
321
322    /**
323     * @group ZF-6133
324     */
325    public function testPunycodeDecoding()
326    {
327        $validator = new Zend_Validate_Hostname();
328
329        // Check TLD matching
330        $valuesExpected = array(
331            array(true, array('xn--brger-kva.com')),
332            array(false, array('xn--brger-x45d2va.com', 'xn--bürger.com', 'xn--'))
333            );
334        foreach ($valuesExpected as $element) {
335            foreach ($element[1] as $input) {
336                $this->assertEquals($element[0], $validator->isValid($input), implode("\n", $validator->getMessages()) . $input);
337            }
338        }
339    }
340
341    /**
342     * @ZF-4352
343     */
344    public function testNonStringValidation()
345    {
346        $this->assertFalse($this->_validator->isValid(array(1 => 1)));
347    }
348
349    /**
350     * @ZF-7323
351     */
352    public function testLatinSpecialChars()
353    {
354        $this->assertFalse($this->_validator->isValid('place@yah&oo.com'));
355        $this->assertFalse($this->_validator->isValid('place@y*ahoo.com'));
356        $this->assertFalse($this->_validator->isValid('ya#hoo'));
357    }
358
359    /**
360     * @group ZF-7277
361     */
362    public function testDifferentIconvEncoding()
363    {
364        iconv_set_encoding('internal_encoding', 'ISO8859-1');
365        $validator = new Zend_Validate_Hostname();
366
367        $valuesExpected = array(
368            array(true, array('bürger.com', 'hãllo.com', 'hållo.com')),
369            array(true, array('bÜrger.com', 'hÃllo.com', 'hÅllo.com')),
370            array(false, array('hãllo.lt', 'bürger.lt', 'hãllo.lt'))
371            );
372        foreach ($valuesExpected as $element) {
373            foreach ($element[1] as $input) {
374                $this->assertEquals($element[0], $validator->isValid($input), implode("\n", $validator->getMessages()) . $input);
375            }
376        }
377    }
378
379    /**
380     * @ZF-8312
381     */
382    public function testInvalidDoubledIdn()
383    {
384        $this->assertFalse($this->_validator->isValid('test.com / http://www.test.com'));
385    }
386
387    /**
388     * @group ZF-10267
389     */
390    public function testURI()
391    {
392        $valuesExpected = array(
393            array(Zend_Validate_Hostname::ALLOW_URI, true, array('localhost', 'example.com', '~ex%20ample')),
394            array(Zend_Validate_Hostname::ALLOW_URI, false, array('§bad', 'don?t.know', 'thisisaverylonghostnamewhichextendstwohundredfiftysixcharactersandthereforshouldnotbeallowedbythisvalidatorbecauserfc3986limitstheallowedcharacterstoalimitoftwohunderedfiftysixcharactersinsumbutifthistestwouldfailthenitshouldreturntruewhichthrowsanexceptionbytheunittest')),
395        );
396        foreach ($valuesExpected as $element) {
397            $validator = new Zend_Validate_Hostname($element[0]);
398            foreach ($element[2] as $input) {
399                $this->assertEquals($element[1], $validator->isValid($input), implode("\n", $validator->getMessages()) . $input);
400            }
401        }
402    }
403
404    /**
405     * Ensure that a trailing "." in a local hostname is permitted
406     *
407     * @group ZF-6363
408     */
409    public function testTrailingDot()
410    {
411        $valuesExpected = array(
412            array(Zend_Validate_Hostname::ALLOW_ALL, true, array('example.', 'example.com.', '~ex%20ample.com.')),
413            array(Zend_Validate_Hostname::ALLOW_ALL, false, array('example..',)),
414            array(Zend_Validate_Hostname::ALLOW_ALL, true, array('1.2.3.4.')),
415            array(Zend_Validate_Hostname::ALLOW_DNS, false, array('example..', '~ex%20ample..')),
416            array(Zend_Validate_Hostname::ALLOW_LOCAL, true, array('example.', 'example.com.')),
417        );
418
419        foreach ($valuesExpected as $element) {
420            $validator = new Zend_Validate_Hostname($element[0]);
421            foreach ($element[2] as $input) {
422                $this->assertEquals($element[1], $validator->isValid($input), implode("\n", $validator->getMessages()) . $input);
423            }
424        }
425    }
426    
427    /**
428     * @group ZF-11334
429     * @see http://www.ietf.org/rfc/rfc2732.txt
430     */
431    public function testSupportsIpv6AddressesWhichContainHexDigitF()
432    {
433        $validator = new Zend_Validate_Hostname(Zend_Validate_Hostname::ALLOW_ALL);
434        
435        $this->assertTrue($validator->isValid('FEDC:BA98:7654:3210:FEDC:BA98:7654:3210'));
436        $this->assertTrue($validator->isValid('1080:0:0:0:8:800:200C:417A'));
437        $this->assertTrue($validator->isValid('3ffe:2a00:100:7031::1'));
438        $this->assertTrue($validator->isValid('1080::8:800:200C:417A'));
439        $this->assertTrue($validator->isValid('::192.9.5.5'));
440        $this->assertTrue($validator->isValid('::FFFF:129.144.52.38'));
441        $this->assertTrue($validator->isValid('2010:836B:4179::836B:4179'));
442    }
443    
444    /**
445     * @group ZF-11796
446     */
447    public function testIDNSI()
448    {
449        $validator = new Zend_Validate_Hostname(Zend_Validate_Hostname::ALLOW_ALL);
450        
451        $this->assertTrue($validator->isValid('Test123.si'));
452        $this->assertTrue($validator->isValid('țest123.si'));
453        $this->assertTrue($validator->isValid('tĕst123.si'));
454        $this->assertTrue($validator->isValid('tàrø.si'));
455        $this->assertFalse($validator->isValid('رات.si'));
456    }
457    /**
458     * @ZF-12314
459     */
460    public function testDKSpecialChars()
461    {
462        $this->assertTrue($this->_validator->isValid('testæøå.dk'));
463    }
464
465}