2 # Copyright 2011 Google Inc. All Rights Reserved.
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
16 """Unit tests for platformsettings.
19 $ ./platformsettings_test.py
24 import platformsettings
26 WINDOWS_7_IP
= '172.11.25.170'
27 WINDOWS_7_MAC
= '00-1A-44-DA-88-C0'
28 WINDOWS_7_IPCONFIG
= """
29 Windows IP Configuration
31 Host Name . . . . . . . . . . . . : THEHOST1-W
32 Primary Dns Suffix . . . . . . . : something.example.com
33 Node Type . . . . . . . . . . . . : Hybrid
34 IP Routing Enabled. . . . . . . . : No
35 WINS Proxy Enabled. . . . . . . . : No
36 DNS Suffix Search List. . . . . . : example.com
39 Ethernet adapter Local Area Connection:
41 Connection-specific DNS Suffix . : somethingexample.com
42 Description . . . . . . . . . . . : Int PRO/1000 MT Network Connection
43 Physical Address. . . . . . . . . : %(mac_addr)s
44 DHCP Enabled. . . . . . . . . . . : Yes
45 Autoconfiguration Enabled . . . . : Yes
46 IPv6 Address. . . . . . . . . . . : 1234:0:1000:1200:839f:d256:3a6c:210(Preferred)
47 Temporary IPv6 Address. . . . . . : 2143:0:2100:1800:38f9:2d65:a3c6:120(Preferred)
48 Link-local IPv6 Address . . . . . : abcd::1234:1a33:b2cc:238%%18(Preferred)
49 IPv4 Address. . . . . . . . . . . : %(ip_addr)s(Preferred)
50 Subnet Mask . . . . . . . . . . . : 255.255.248.0
51 Lease Obtained. . . . . . . . . . : Thursday, April 28, 2011 9:40:22 PM
52 Lease Expires . . . . . . . . . . : Tuesday, May 10, 2011 12:15:48 PM
53 Default Gateway . . . . . . . . . : abcd::2:37ee:ef70:56%%18
55 DHCP Server . . . . . . . . . . . : 172.11.22.33
56 DNS Servers . . . . . . . . . . . : 8.8.4.4
57 NetBIOS over Tcpip. . . . . . . . : Enabled
58 """ % {'ip_addr': WINDOWS_7_IP
, 'mac_addr': WINDOWS_7_MAC
}
60 WINDOWS_XP_IP
= '172.1.2.3'
61 WINDOWS_XP_MAC
= '00-34-B8-1F-FA-70'
62 WINDOWS_XP_IPCONFIG
= """
63 Windows IP Configuration
65 Host Name . . . . . . . . . . . . : HOSTY-0
66 Primary Dns Suffix . . . . . . . :
67 Node Type . . . . . . . . . . . . : Unknown
68 IP Routing Enabled. . . . . . . . : No
69 WINS Proxy Enabled. . . . . . . . : No
70 DNS Suffix Search List. . . . . . : example.com
72 Ethernet adapter Local Area Connection 2:
74 Connection-specific DNS Suffix . : example.com
75 Description . . . . . . . . . . . : Int Adapter (PILA8470B)
76 Physical Address. . . . . . . . . : %(mac_addr)s
77 Dhcp Enabled. . . . . . . . . . . : Yes
78 Autoconfiguration Enabled . . . . : Yes
79 IP Address. . . . . . . . . . . . : %(ip_addr)s
80 Subnet Mask . . . . . . . . . . . : 255.255.254.0
81 Default Gateway . . . . . . . . . : 172.1.2.254
82 DHCP Server . . . . . . . . . . . : 172.1.3.241
83 DNS Servers . . . . . . . . . . . : 172.1.3.241
86 Lease Obtained. . . . . . . . . . : Thursday, April 07, 2011 9:14:55 AM
87 Lease Expires . . . . . . . . . . : Thursday, April 07, 2011 1:14:55 PM
88 """ % {'ip_addr': WINDOWS_XP_IP
, 'mac_addr': WINDOWS_XP_MAC
}
91 # scutil show State:/Network/Global/IPv4
94 PrimaryInterface : en1
95 PrimaryService : 8824452C-FED4-4C09-9256-40FB146739E0
100 # scutil show State:/Network/Service/[PRIMARY_SERVICE_KEY]/DNS
101 OSX_DNS_STATE_LION
= """
103 DomainName : mtv.corp.google.com
104 SearchDomains : <array> {
105 0 : mtv.corp.google.com
111 ServerAddresses : <array> {
119 OSX_DNS_STATE_SNOW_LEOPARD
= """
121 ServerAddresses : <array> {
126 DomainName : mtv.corp.google.com
127 SearchDomains : <array> {
128 0 : mtv.corp.google.com
138 class SystemProxyTest(unittest
.TestCase
):
140 def test_basic(self
):
141 system_proxy
= platformsettings
.SystemProxy(None, None)
142 self
.assertEqual(None, system_proxy
.host
)
143 self
.assertEqual(None, system_proxy
.port
)
144 self
.assertFalse(system_proxy
)
146 def test_from_url_empty(self
):
147 system_proxy
= platformsettings
.SystemProxy
.from_url('')
148 self
.assertEqual(None, system_proxy
.host
)
149 self
.assertEqual(None, system_proxy
.port
)
150 self
.assertFalse(system_proxy
)
152 def test_from_url_basic(self
):
153 system_proxy
= platformsettings
.SystemProxy
.from_url('http://pxy.com:8888/')
154 self
.assertEqual('pxy.com', system_proxy
.host
)
155 self
.assertEqual(8888, system_proxy
.port
)
156 self
.assertTrue(system_proxy
)
158 def test_from_url_no_port(self
):
159 system_proxy
= platformsettings
.SystemProxy
.from_url('http://pxy.com/')
160 self
.assertEqual('pxy.com', system_proxy
.host
)
161 self
.assertEqual(None, system_proxy
.port
)
162 self
.assertTrue(system_proxy
)
164 def test_from_url_empty_string(self
):
165 system_proxy
= platformsettings
.SystemProxy
.from_url('')
166 self
.assertEqual(None, system_proxy
.host
)
167 self
.assertEqual(None, system_proxy
.port
)
168 self
.assertFalse(system_proxy
)
170 def test_from_url_bad_string(self
):
171 system_proxy
= platformsettings
.SystemProxy
.from_url('foo:80')
172 self
.assertEqual(None, system_proxy
.host
)
173 self
.assertEqual(None, system_proxy
.port
)
174 self
.assertFalse(system_proxy
)
177 class HasSniTest(unittest
.TestCase
):
178 def test_has_sni(self
):
179 # Check that no exception is raised.
180 platformsettings
.HasSniSupport()
183 # pylint: disable=abstract-method
184 class Win7Settings(platformsettings
._WindowsPlatformSettings
):
186 def _ipconfig(cls
, *args
):
187 if args
== ('/all',):
188 return WINDOWS_7_IPCONFIG
191 class WinXpSettings(platformsettings
._WindowsPlatformSettings
):
193 def _ipconfig(cls
, *args
):
194 if args
== ('/all',):
195 return WINDOWS_XP_IPCONFIG
199 class WindowsPlatformSettingsTest(unittest
.TestCase
):
200 def test_get_mac_address_xp(self
):
201 self
.assertEqual(WINDOWS_XP_MAC
,
202 WinXpSettings()._get
_mac
_address
(WINDOWS_XP_IP
))
204 def test_get_mac_address_7(self
):
205 self
.assertEqual(WINDOWS_7_MAC
,
206 Win7Settings()._get
_mac
_address
(WINDOWS_7_IP
))
209 class OsxSettings(platformsettings
._OsxPlatformSettings
):
211 super(OsxSettings
, self
).__init
__()
212 self
.ipv4_state
= OSX_IPV4_STATE
213 self
.dns_state
= None # varies by test
215 def _scutil(self
, cmd
):
216 if cmd
== 'show State:/Network/Global/IPv4':
217 return self
.ipv4_state
218 elif cmd
.startswith('show State:/Network/Service/'):
219 return self
.dns_state
220 raise RuntimeError("Unrecognized cmd: %s", cmd
)
223 class OsxPlatformSettingsTest(unittest
.TestCase
):
225 self
.settings
= OsxSettings()
227 def test_get_primary_nameserver_lion(self
):
228 self
.settings
.dns_state
= OSX_DNS_STATE_LION
229 self
.assertEqual('172.72.255.1', self
.settings
._get
_primary
_nameserver
())
231 def test_get_primary_nameserver_snow_leopard(self
):
232 self
.settings
.dns_state
= OSX_DNS_STATE_SNOW_LEOPARD
233 self
.assertEqual('172.27.1.1', self
.settings
._get
_primary
_nameserver
())
235 def test_get_primary_nameserver_unexpected_ipv4_state_raises(self
):
236 self
.settings
.ipv4_state
= 'Some error'
237 self
.settings
.dns_state
= OSX_DNS_STATE_SNOW_LEOPARD
238 self
.assertRaises(platformsettings
.DnsReadError
,
239 self
.settings
._get
_primary
_nameserver
)
241 def test_get_primary_nameserver_unexpected_dns_state_raises(self
):
242 self
.settings
.dns_state
= 'Some other error'
243 self
.assertRaises(platformsettings
.DnsReadError
,
244 self
.settings
._get
_primary
_nameserver
)
247 if __name__
== '__main__':