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 Win7Settings(platformsettings._WindowsPlatformSettings):
140 def _ipconfig(cls, *args):
141 if args == ('/all',):
142 return WINDOWS_7_IPCONFIG
145 class WinXpSettings(platformsettings._WindowsPlatformSettings):
147 def _ipconfig(cls, *args):
148 if args == ('/all',):
149 return WINDOWS_XP_IPCONFIG
153 class WindowsPlatformSettingsTest(unittest.TestCase):
154 def test_get_mac_address_xp(self):
155 self.assertEqual(WINDOWS_XP_MAC,
156 WinXpSettings()._get_mac_address(WINDOWS_XP_IP))
158 def test_get_mac_address_7(self):
159 self.assertEqual(WINDOWS_7_MAC,
160 Win7Settings()._get_mac_address(WINDOWS_7_IP))
163 class OsxSettings(platformsettings._OsxPlatformSettings):
165 super(OsxSettings, self).__init__()
166 self.ipv4_state = OSX_IPV4_STATE
167 self.dns_state = None # varies by test
169 def _scutil(self, cmd):
170 if cmd == 'show State:/Network/Global/IPv4':
171 return self.ipv4_state
172 elif cmd.startswith('show State:/Network/Service/'):
173 return self.dns_state
174 raise RuntimeError("Unrecognized cmd: %s", cmd)
177 class OsxPlatformSettingsTest(unittest.TestCase):
179 self.settings = OsxSettings()
181 def test_get_primary_nameserver_lion(self):
182 self.settings.dns_state = OSX_DNS_STATE_LION
183 self.assertEqual('172.72.255.1', self.settings._get_primary_nameserver())
185 def test_get_primary_nameserver_snow_leopard(self):
186 self.settings.dns_state = OSX_DNS_STATE_SNOW_LEOPARD
187 self.assertEqual('172.27.1.1', self.settings._get_primary_nameserver())
189 def test_get_primary_nameserver_unexpected_ipv4_state_raises(self):
190 self.settings.ipv4_state = 'Some error'
191 self.settings.dns_state = OSX_DNS_STATE_SNOW_LEOPARD
192 self.assertRaises(platformsettings.DnsReadError,
193 self.settings._get_primary_nameserver)
195 def test_get_primary_nameserver_unexpected_dns_state_raises(self):
196 self.settings.dns_state = 'Some other error'
197 self.assertRaises(platformsettings.DnsReadError,
198 self.settings._get_primary_nameserver)
201 if __name__ == '__main__':