ConfigTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. namespace React\Tests\Dns\Config;
  3. use React\Tests\Dns\TestCase;
  4. use React\Dns\Config\Config;
  5. class ConfigTest extends TestCase
  6. {
  7. public function testLoadsSystemDefault()
  8. {
  9. $config = Config::loadSystemConfigBlocking();
  10. $this->assertInstanceOf('React\Dns\Config\Config', $config);
  11. }
  12. public function testLoadsDefaultPath()
  13. {
  14. if (DIRECTORY_SEPARATOR === '\\') {
  15. $this->markTestSkipped('Not supported on Windows');
  16. }
  17. $config = Config::loadResolvConfBlocking();
  18. $this->assertInstanceOf('React\Dns\Config\Config', $config);
  19. }
  20. public function testLoadsFromExplicitPath()
  21. {
  22. $config = Config::loadResolvConfBlocking(__DIR__ . '/../Fixtures/etc/resolv.conf');
  23. $this->assertEquals(array('8.8.8.8'), $config->nameservers);
  24. }
  25. public function testLoadThrowsWhenPathIsInvalid()
  26. {
  27. $this->setExpectedException('RuntimeException');
  28. Config::loadResolvConfBlocking(__DIR__ . '/invalid.conf');
  29. }
  30. public function testParsesSingleEntryFile()
  31. {
  32. $contents = 'nameserver 8.8.8.8';
  33. $expected = array('8.8.8.8');
  34. $config = Config::loadResolvConfBlocking('data://text/plain;base64,' . base64_encode($contents));
  35. $this->assertEquals($expected, $config->nameservers);
  36. }
  37. public function testParsesNameserverWithoutIpv6ScopeId()
  38. {
  39. $contents = 'nameserver ::1%lo';
  40. $expected = array('::1');
  41. $config = Config::loadResolvConfBlocking('data://text/plain;base64,' . base64_encode($contents));
  42. $this->assertEquals($expected, $config->nameservers);
  43. }
  44. public function testParsesNameserverEntriesFromAverageFileCorrectly()
  45. {
  46. $contents = '#
  47. # Mac OS X Notice
  48. #
  49. # This file is not used by the host name and address resolution
  50. # or the DNS query routing mechanisms used by most processes on
  51. # this Mac OS X system.
  52. #
  53. # This file is automatically generated.
  54. #
  55. domain v.cablecom.net
  56. nameserver 127.0.0.1
  57. nameserver ::1
  58. ';
  59. $expected = array('127.0.0.1', '::1');
  60. $config = Config::loadResolvConfBlocking('data://text/plain;base64,' . base64_encode($contents));
  61. $this->assertEquals($expected, $config->nameservers);
  62. }
  63. public function testParsesEmptyFileWithoutNameserverEntries()
  64. {
  65. $expected = array();
  66. $config = Config::loadResolvConfBlocking('data://text/plain;base64,');
  67. $this->assertEquals($expected, $config->nameservers);
  68. }
  69. public function testParsesFileAndIgnoresCommentsAndInvalidNameserverEntries()
  70. {
  71. $contents = '
  72. # nameserver 1.2.3.4
  73. ; nameserver 2.3.4.5
  74. nameserver 3.4.5.6 # nope
  75. nameserver 4.5.6.7 5.6.7.8
  76. nameserver 6.7.8.9
  77. NameServer 7.8.9.10
  78. nameserver localhost
  79. ';
  80. $expected = array();
  81. $config = Config::loadResolvConfBlocking('data://text/plain;base64,' . base64_encode($contents));
  82. $this->assertEquals($expected, $config->nameservers);
  83. }
  84. public function testLoadsFromWmicOnWindows()
  85. {
  86. if (DIRECTORY_SEPARATOR !== '\\') {
  87. // WMIC is Windows-only tool and not supported on other platforms
  88. // Unix is our main platform, so we don't want to report a skipped test here (yellow)
  89. // $this->markTestSkipped('Only on Windows');
  90. $this->expectOutputString('');
  91. return;
  92. }
  93. $config = Config::loadWmicBlocking();
  94. $this->assertInstanceOf('React\Dns\Config\Config', $config);
  95. }
  96. public function testLoadsSingleEntryFromWmicOutput()
  97. {
  98. $contents = '
  99. Node,DNSServerSearchOrder
  100. ACE,
  101. ACE,{192.168.2.1}
  102. ACE,
  103. ';
  104. $expected = array('192.168.2.1');
  105. $config = Config::loadWmicBlocking($this->echoCommand($contents));
  106. $this->assertEquals($expected, $config->nameservers);
  107. }
  108. public function testLoadsEmptyListFromWmicOutput()
  109. {
  110. $contents = '
  111. Node,DNSServerSearchOrder
  112. ACE,
  113. ';
  114. $expected = array();
  115. $config = Config::loadWmicBlocking($this->echoCommand($contents));
  116. $this->assertEquals($expected, $config->nameservers);
  117. }
  118. public function testLoadsSingleEntryForMultipleNicsFromWmicOutput()
  119. {
  120. $contents = '
  121. Node,DNSServerSearchOrder
  122. ACE,
  123. ACE,{192.168.2.1}
  124. ACE,
  125. ACE,{192.168.2.2}
  126. ACE,
  127. ';
  128. $expected = array('192.168.2.1', '192.168.2.2');
  129. $config = Config::loadWmicBlocking($this->echoCommand($contents));
  130. $this->assertEquals($expected, $config->nameservers);
  131. }
  132. public function testLoadsMultipleEntriesForSingleNicWithSemicolonFromWmicOutput()
  133. {
  134. $contents = '
  135. Node,DNSServerSearchOrder
  136. ACE,
  137. ACE,{192.168.2.1;192.168.2.2}
  138. ACE,
  139. ';
  140. $expected = array('192.168.2.1', '192.168.2.2');
  141. $config = Config::loadWmicBlocking($this->echoCommand($contents));
  142. $this->assertEquals($expected, $config->nameservers);
  143. }
  144. public function testLoadsMultipleEntriesForSingleNicWithQuotesFromWmicOutput()
  145. {
  146. $contents = '
  147. Node,DNSServerSearchOrder
  148. ACE,
  149. ACE,{"192.168.2.1","192.168.2.2"}
  150. ACE,
  151. ';
  152. $expected = array('192.168.2.1', '192.168.2.2');
  153. $config = Config::loadWmicBlocking($this->echoCommand($contents));
  154. $this->assertEquals($expected, $config->nameservers);
  155. }
  156. private function echoCommand($output)
  157. {
  158. return 'echo ' . escapeshellarg($output);
  159. }
  160. }