1 #!/usr/bin/env python 2 # Copyright 2011 Google Inc. All Rights Reserved. 3 # 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 7 # 8 # http://www.apache.org/licenses/LICENSE-2.0 9 # 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. 15 16 """Unit tests for platformsettings. 17 18 Usage: 19 $ ./platformsettings_test.py 20 """ 21 22 import unittest 23 24 import platformsettings 25 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 30 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 37 another.example.com 38 39 Ethernet adapter Local Area Connection: 40 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 54 172.11.25.254 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} 59 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 64 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 71 72 Ethernet adapter Local Area Connection 2: 73 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 84 8.8.8.8 85 8.8.4.4 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} 89 90 91 # scutil show State:/Network/Global/IPv4 92 OSX_IPV4_STATE = """ 93 <dictionary> { 94 PrimaryInterface : en1 95 PrimaryService : 8824452C-FED4-4C09-9256-40FB146739E0 96 Router : 192.168.1.1 97 } 98 """ 99 100 # scutil show State:/Network/Service/[PRIMARY_SERVICE_KEY]/DNS 101 OSX_DNS_STATE_LION = """ 102 <dictionary> { 103 DomainName : mtv.corp.google.com 104 SearchDomains : <array> { 105 0 : mtv.corp.google.com 106 1 : corp.google.com 107 2 : prod.google.com 108 3 : prodz.google.com 109 4 : google.com 110 } 111 ServerAddresses : <array> { 112 0 : 172.72.255.1 113 1 : 172.49.117.57 114 2 : 172.54.116.57 115 } 116 } 117 """ 118 119 OSX_DNS_STATE_SNOW_LEOPARD = """ 120 <dictionary> { 121 ServerAddresses : <array> { 122 0 : 172.27.1.1 123 1 : 172.94.117.57 124 2 : 172.45.116.57 125 } 126 DomainName : mtv.corp.google.com 127 SearchDomains : <array> { 128 0 : mtv.corp.google.com 129 1 : corp.google.com 130 2 : prod.google.com 131 3 : prodz.google.com 132 4 : google.com 133 } 134 } 135 """ 136 137 138 class SystemProxyTest(unittest.TestCase): 139 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) 145 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) 151 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) 157 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) 163 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) 169 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) 175 176 177 class HasSniTest(unittest.TestCase): 178 def test_has_sni(self): 179 # Check that no exception is raised. 180 platformsettings.HasSniSupport() 181 182 183 # pylint: disable=abstract-method 184 class Win7Settings(platformsettings._WindowsPlatformSettings): 185 @classmethod 186 def _ipconfig(cls, *args): 187 if args == ('/all',): 188 return WINDOWS_7_IPCONFIG 189 raise RuntimeError 190 191 class WinXpSettings(platformsettings._WindowsPlatformSettings): 192 @classmethod 193 def _ipconfig(cls, *args): 194 if args == ('/all',): 195 return WINDOWS_XP_IPCONFIG 196 raise RuntimeError 197 198 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)) 203 204 def test_get_mac_address_7(self): 205 self.assertEqual(WINDOWS_7_MAC, 206 Win7Settings()._get_mac_address(WINDOWS_7_IP)) 207 208 209 class OsxSettings(platformsettings._OsxPlatformSettings): 210 def __init__(self): 211 super(OsxSettings, self).__init__() 212 self.ipv4_state = OSX_IPV4_STATE 213 self.dns_state = None # varies by test 214 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) 221 222 223 class OsxPlatformSettingsTest(unittest.TestCase): 224 def setUp(self): 225 self.settings = OsxSettings() 226 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()) 230 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()) 234 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) 240 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) 245 246 247 if __name__ == '__main__': 248 unittest.main() 249