Upstream version 8.37.180.0
[platform/framework/web/crosswalk.git] / src / third_party / webpagereplay / platformsettings_test.py
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 Win7Settings(platformsettings._WindowsPlatformSettings):
139   @classmethod
140   def _ipconfig(cls, *args):
141     if args == ('/all',):
142       return WINDOWS_7_IPCONFIG
143     raise RuntimeError
144
145 class WinXpSettings(platformsettings._WindowsPlatformSettings):
146   @classmethod
147   def _ipconfig(cls, *args):
148     if args == ('/all',):
149       return WINDOWS_XP_IPCONFIG
150     raise RuntimeError
151
152
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))
157
158   def test_get_mac_address_7(self):
159     self.assertEqual(WINDOWS_7_MAC,
160                      Win7Settings()._get_mac_address(WINDOWS_7_IP))
161
162
163 class OsxSettings(platformsettings._OsxPlatformSettings):
164   def __init__(self):
165     super(OsxSettings, self).__init__()
166     self.ipv4_state = OSX_IPV4_STATE
167     self.dns_state = None  # varies by test
168
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)
175
176
177 class OsxPlatformSettingsTest(unittest.TestCase):
178   def setUp(self):
179     self.settings = OsxSettings()
180
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())
184
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())
188
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)
194
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)
199
200
201 if __name__ == '__main__':
202   unittest.main()