HostsFileTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. namespace React\Tests\Dns\Config;
  3. use React\Tests\Dns\TestCase;
  4. use React\Dns\Config\HostsFile;
  5. class HostsFileTest extends TestCase
  6. {
  7. public function testLoadsFromDefaultPath()
  8. {
  9. $hosts = HostsFile::loadFromPathBlocking();
  10. $this->assertInstanceOf('React\Dns\Config\HostsFile', $hosts);
  11. }
  12. public function testDefaultShouldHaveLocalhostMapped()
  13. {
  14. if (DIRECTORY_SEPARATOR === '\\') {
  15. $this->markTestSkipped('Not supported on Windows');
  16. }
  17. $hosts = HostsFile::loadFromPathBlocking();
  18. $this->assertContains('127.0.0.1', $hosts->getIpsForHost('localhost'));
  19. }
  20. public function testLoadThrowsForInvalidPath()
  21. {
  22. $this->setExpectedException('RuntimeException');
  23. HostsFile::loadFromPathBlocking('does/not/exist');
  24. }
  25. public function testContainsSingleLocalhostEntry()
  26. {
  27. $hosts = new HostsFile('127.0.0.1 localhost');
  28. $this->assertEquals(array('127.0.0.1'), $hosts->getIpsForHost('localhost'));
  29. $this->assertEquals(array(), $hosts->getIpsForHost('example.com'));
  30. }
  31. public function testNonIpReturnsNothingForInvalidHosts()
  32. {
  33. $hosts = new HostsFile('a b');
  34. $this->assertEquals(array(), $hosts->getIpsForHost('a'));
  35. $this->assertEquals(array(), $hosts->getIpsForHost('b'));
  36. }
  37. public function testIgnoresIpv6ZoneId()
  38. {
  39. $hosts = new HostsFile('fe80::1%lo0 localhost');
  40. $this->assertEquals(array('fe80::1'), $hosts->getIpsForHost('localhost'));
  41. }
  42. public function testSkipsComments()
  43. {
  44. $hosts = new HostsFile('# start' . PHP_EOL .'#127.0.0.1 localhost' . PHP_EOL . '127.0.0.2 localhost # example.com');
  45. $this->assertEquals(array('127.0.0.2'), $hosts->getIpsForHost('localhost'));
  46. $this->assertEquals(array(), $hosts->getIpsForHost('example.com'));
  47. }
  48. public function testContainsSingleLocalhostEntryWithCaseIgnored()
  49. {
  50. $hosts = new HostsFile('127.0.0.1 LocalHost');
  51. $this->assertEquals(array('127.0.0.1'), $hosts->getIpsForHost('LOCALHOST'));
  52. }
  53. public function testEmptyFileContainsNothing()
  54. {
  55. $hosts = new HostsFile('');
  56. $this->assertEquals(array(), $hosts->getIpsForHost('example.com'));
  57. }
  58. public function testSingleEntryWithMultipleNames()
  59. {
  60. $hosts = new HostsFile('127.0.0.1 localhost example.com');
  61. $this->assertEquals(array('127.0.0.1'), $hosts->getIpsForHost('example.com'));
  62. $this->assertEquals(array('127.0.0.1'), $hosts->getIpsForHost('localhost'));
  63. }
  64. public function testMergesEntriesOverMultipleLines()
  65. {
  66. $hosts = new HostsFile("127.0.0.1 localhost\n127.0.0.2 localhost\n127.0.0.3 a localhost b\n127.0.0.4 a localhost");
  67. $this->assertEquals(array('127.0.0.1', '127.0.0.2', '127.0.0.3', '127.0.0.4'), $hosts->getIpsForHost('localhost'));
  68. }
  69. public function testMergesIpv4AndIpv6EntriesOverMultipleLines()
  70. {
  71. $hosts = new HostsFile("127.0.0.1 localhost\n::1 localhost");
  72. $this->assertEquals(array('127.0.0.1', '::1'), $hosts->getIpsForHost('localhost'));
  73. }
  74. public function testReverseLookup()
  75. {
  76. $hosts = new HostsFile('127.0.0.1 localhost');
  77. $this->assertEquals(array('localhost'), $hosts->getHostsForIp('127.0.0.1'));
  78. $this->assertEquals(array(), $hosts->getHostsForIp('192.168.1.1'));
  79. }
  80. public function testReverseSkipsComments()
  81. {
  82. $hosts = new HostsFile("# start\n#127.0.0.1 localhosted\n127.0.0.2\tlocalhost\t# example.com\n\t127.0.0.3\t\texample.org\t\t");
  83. $this->assertEquals(array(), $hosts->getHostsForIp('127.0.0.1'));
  84. $this->assertEquals(array('localhost'), $hosts->getHostsForIp('127.0.0.2'));
  85. $this->assertEquals(array('example.org'), $hosts->getHostsForIp('127.0.0.3'));
  86. }
  87. public function testReverseNonIpReturnsNothing()
  88. {
  89. $hosts = new HostsFile('127.0.0.1 localhost');
  90. $this->assertEquals(array(), $hosts->getHostsForIp('localhost'));
  91. $this->assertEquals(array(), $hosts->getHostsForIp('127.0.0.1.1'));
  92. }
  93. public function testReverseNonIpReturnsNothingForInvalidHosts()
  94. {
  95. $hosts = new HostsFile('a b');
  96. $this->assertEquals(array(), $hosts->getHostsForIp('a'));
  97. $this->assertEquals(array(), $hosts->getHostsForIp('b'));
  98. }
  99. public function testReverseLookupReturnsLowerCaseHost()
  100. {
  101. $hosts = new HostsFile('127.0.0.1 LocalHost');
  102. $this->assertEquals(array('localhost'), $hosts->getHostsForIp('127.0.0.1'));
  103. }
  104. public function testReverseLookupChecksNormalizedIpv6()
  105. {
  106. $hosts = new HostsFile('FE80::00a1 localhost');
  107. $this->assertEquals(array('localhost'), $hosts->getHostsForIp('fe80::A1'));
  108. }
  109. public function testReverseLookupIgnoresIpv6ZoneId()
  110. {
  111. $hosts = new HostsFile('fe80::1%lo0 localhost');
  112. $this->assertEquals(array('localhost'), $hosts->getHostsForIp('fe80::1'));
  113. }
  114. public function testReverseLookupReturnsMultipleHostsOverSingleLine()
  115. {
  116. $hosts = new HostsFile("::1 ip6-localhost ip6-loopback");
  117. $this->assertEquals(array('ip6-localhost', 'ip6-loopback'), $hosts->getHostsForIp('::1'));
  118. }
  119. public function testReverseLookupReturnsMultipleHostsOverMultipleLines()
  120. {
  121. $hosts = new HostsFile("::1 ip6-localhost\n::1 ip6-loopback");
  122. $this->assertEquals(array('ip6-localhost', 'ip6-loopback'), $hosts->getHostsForIp('::1'));
  123. }
  124. }