aboutsummaryrefslogtreecommitdiffhomepage
path: root/assets/php/vendor/react/dns/tests/Config/ConfigTest.php
blob: 802040870dbb0bb9a4f6dfa1745e09b654178bf8 (plain) (blame)
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
<?php

namespace React\Tests\Dns\Config;

use React\Tests\Dns\TestCase;
use React\Dns\Config\Config;

class ConfigTest extends TestCase
{
    public function testLoadsSystemDefault()
    {
        $config = Config::loadSystemConfigBlocking();

        $this->assertInstanceOf('React\Dns\Config\Config', $config);
    }

    public function testLoadsDefaultPath()
    {
        if (DIRECTORY_SEPARATOR === '\\') {
            $this->markTestSkipped('Not supported on Windows');
        }

        $config = Config::loadResolvConfBlocking();

        $this->assertInstanceOf('React\Dns\Config\Config', $config);
    }

    public function testLoadsFromExplicitPath()
    {
        $config = Config::loadResolvConfBlocking(__DIR__ . '/../Fixtures/etc/resolv.conf');

        $this->assertEquals(array('8.8.8.8'), $config->nameservers);
    }

    /**
     * @expectedException RuntimeException
     */
    public function testLoadThrowsWhenPathIsInvalid()
    {
        Config::loadResolvConfBlocking(__DIR__ . '/invalid.conf');
    }

    public function testParsesSingleEntryFile()
    {
        $contents = 'nameserver 8.8.8.8';
        $expected = array('8.8.8.8');

        $config = Config::loadResolvConfBlocking('data://text/plain;base64,' . base64_encode($contents));
        $this->assertEquals($expected, $config->nameservers);
    }

    public function testParsesNameserverEntriesFromAverageFileCorrectly()
    {
        $contents = '#
# Mac OS X Notice
#
# This file is not used by the host name and address resolution
# or the DNS query routing mechanisms used by most processes on
# this Mac OS X system.
#
# This file is automatically generated.
#
domain v.cablecom.net
nameserver 127.0.0.1
nameserver ::1
';
        $expected = array('127.0.0.1', '::1');

        $config = Config::loadResolvConfBlocking('data://text/plain;base64,' . base64_encode($contents));
        $this->assertEquals($expected, $config->nameservers);
    }

    public function testParsesEmptyFileWithoutNameserverEntries()
    {
        $contents = '';
        $expected = array();

        $config = Config::loadResolvConfBlocking('data://text/plain;base64,');
        $this->assertEquals($expected, $config->nameservers);
    }

    public function testParsesFileAndIgnoresCommentsAndInvalidNameserverEntries()
    {
        $contents = '
# nameserver 1.2.3.4
; nameserver 2.3.4.5

nameserver 3.4.5.6 # nope
nameserver 4.5.6.7 5.6.7.8
  nameserver 6.7.8.9
NameServer 7.8.9.10
';
        $expected = array();

        $config = Config::loadResolvConfBlocking('data://text/plain;base64,' . base64_encode($contents));
        $this->assertEquals($expected, $config->nameservers);
    }

    public function testLoadsFromWmicOnWindows()
    {
        if (DIRECTORY_SEPARATOR !== '\\') {
            $this->markTestSkipped('Only on Windows');
        }

        $config = Config::loadWmicBlocking();

        $this->assertInstanceOf('React\Dns\Config\Config', $config);
    }

    public function testLoadsSingleEntryFromWmicOutput()
    {
        $contents = '
Node,DNSServerSearchOrder
ACE,
ACE,{192.168.2.1}
ACE,
';
        $expected = array('192.168.2.1');

        $config = Config::loadWmicBlocking($this->echoCommand($contents));

        $this->assertEquals($expected, $config->nameservers);
    }

    public function testLoadsEmptyListFromWmicOutput()
    {
        $contents = '
Node,DNSServerSearchOrder
ACE,
';
        $expected = array();

        $config = Config::loadWmicBlocking($this->echoCommand($contents));

        $this->assertEquals($expected, $config->nameservers);
    }

    public function testLoadsSingleEntryForMultipleNicsFromWmicOutput()
    {
        $contents = '
Node,DNSServerSearchOrder
ACE,
ACE,{192.168.2.1}
ACE,
ACE,{192.168.2.2}
ACE,
';
        $expected = array('192.168.2.1', '192.168.2.2');

        $config = Config::loadWmicBlocking($this->echoCommand($contents));

        $this->assertEquals($expected, $config->nameservers);
    }

    public function testLoadsMultipleEntriesForSingleNicWithSemicolonFromWmicOutput()
    {
        $contents = '
Node,DNSServerSearchOrder
ACE,
ACE,{192.168.2.1;192.168.2.2}
ACE,
';
        $expected = array('192.168.2.1', '192.168.2.2');

        $config = Config::loadWmicBlocking($this->echoCommand($contents));

        $this->assertEquals($expected, $config->nameservers);
    }

    public function testLoadsMultipleEntriesForSingleNicWithQuotesFromWmicOutput()
    {
        $contents = '
Node,DNSServerSearchOrder
ACE,
ACE,{"192.168.2.1","192.168.2.2"}
ACE,
';
        $expected = array('192.168.2.1', '192.168.2.2');

        $config = Config::loadWmicBlocking($this->echoCommand($contents));

        $this->assertEquals($expected, $config->nameservers);
    }

    private function echoCommand($output)
    {
        return 'echo ' . escapeshellarg($output);
    }
}