1 #!/usr/bin/python 2 # 3 # Copyright 2015 The Chromium OS Authors. All rights reserved. 4 # Use of this source code is governed by a BSD-style license that can be 5 # found in the LICENSE file. 6 7 import unittest 8 9 import common 10 from autotest_lib.client.common_lib.cros import dbus_send 11 12 EXAMPLE_SHILL_GET_PROPERTIES_OUTPUT = \ 13 """method return sender=:1.12 -> dest=:1.37 reply_serial=2 14 array [ 15 dict entry( 16 string "ActiveProfile" 17 variant string "/profile/default" 18 ) 19 dict entry( 20 string "ArpGateway" 21 variant boolean true 22 ) 23 dict entry( 24 string "AvailableTechnologies" 25 variant array [ 26 string "ethernet" 27 ] 28 ) 29 dict entry( 30 string "CheckPortalList" 31 variant string "''" 32 ) 33 dict entry( 34 string "ConnectedTechnologies" variant array [ 35 string "ethernet" 36 ] 37 ) 38 dict entry( 39 string "ConnectionState" 40 variant string "online" 41 ) 42 dict entry( 43 string "Country" 44 variant string "" 45 ) 46 dict entry( 47 string "DefaultService" 48 variant object path "/service/2" 49 ) 50 dict entry( 51 string "DefaultTechnology" 52 variant string "ethernet" 53 ) 54 dict entry( 55 string "Devices" 56 variant array [ 57 object path "/device/eth0" 58 object path "/device/eth1" 59 ] 60 ) 61 dict entry( 62 string "DisableWiFiVHT" 63 variant boolean false 64 ) 65 dict entry( 66 string "EnabledTechnologies" 67 variant array [ 68 string "ethernet" 69 ] 70 ) 71 dict entry( 72 string "HostName" 73 variant string "" 74 ) 75 dict entry( 76 string "IgnoredDNSSearchPaths" 77 variant string "gateway.2wire.net" 78 ) 79 dict entry( 80 string "LinkMonitorTechnologies" 81 variant string "wifi" 82 ) 83 dict entry( 84 string "NoAutoConnectTechnologies" 85 variant string "" 86 ) 87 dict entry( 88 string "OfflineMode" 89 variant boolean false 90 ) 91 dict entry( 92 string "PortalCheckInterval" 93 variant int32 30 94 ) 95 dict entry( 96 string "PortalURL" 97 variant string "http://www.gstatic.com/generate_204" 98 ) 99 dict entry( 100 string "Profiles" 101 variant array [ 102 object path "/profile/default" 103 ] 104 ) 105 dict entry( 106 string "ProhibitedTechnologies" 107 variant string "" 108 ) 109 dict entry( 110 string "ServiceCompleteList" 111 variant array [ 112 object path "/service/2" 113 object path "/service/1" 114 object path "/service/0" 115 ] 116 ) 117 dict entry( 118 string "ServiceWatchList" 119 variant array [ 120 object path "/service/2" 121 ] 122 ) 123 dict entry( 124 string "Services" 125 variant array [ 126 object path "/service/2" 127 ] 128 ) 129 dict entry( 130 string "State" 131 variant string "online" 132 ) 133 dict entry( 134 string "UninitializedTechnologies" 135 variant array [ 136 ] 137 ) 138 dict entry( 139 string "WakeOnLanEnabled" 140 variant boolean true 141 ) 142 ] 143 """ 144 145 PARSED_SHILL_GET_PROPERTIES_OUTPUT = { 146 'ActiveProfile': '/profile/default', 147 'ArpGateway': True, 148 'AvailableTechnologies': ['ethernet'], 149 'CheckPortalList': "''", 150 'ConnectedTechnologies': ['ethernet'], 151 'ConnectionState': 'online', 152 'Country': '', 153 'DefaultService': '/service/2', 154 'DefaultTechnology': 'ethernet', 155 'Devices': ['/device/eth0', '/device/eth1'], 156 'DisableWiFiVHT': False, 157 'EnabledTechnologies': ['ethernet'], 158 'HostName': '', 159 'IgnoredDNSSearchPaths': 'gateway.2wire.net', 160 'LinkMonitorTechnologies': 'wifi', 161 'NoAutoConnectTechnologies': '', 162 'OfflineMode': False, 163 'PortalCheckInterval': 30, 164 'PortalURL': 'http://www.gstatic.com/generate_204', 165 'Profiles': ['/profile/default'], 166 'ProhibitedTechnologies': '', 167 'ServiceCompleteList': ['/service/2', '/service/1', '/service/0'], 168 'ServiceWatchList': ['/service/2'], 169 'Services': ['/service/2'], 170 'State': 'online', 171 'UninitializedTechnologies': [], 172 'WakeOnLanEnabled': True, 173 } 174 175 EXAMPLE_AVAHI_GET_STATE_OUTPUT = \ 176 """method return sender=:1.30 -> dest=:1.40 reply_serial=2 177 int32 2 178 """ 179 180 class DBusSendTest(unittest.TestCase): 181 """Check that we're correctly parsing dbus-send output.""" 182 183 184 def testAvahiGetState(self): 185 """Test that extremely simple input works.""" 186 token_stream = dbus_send._build_token_stream( 187 EXAMPLE_AVAHI_GET_STATE_OUTPUT.splitlines()[1:]) 188 parsed_output = dbus_send._parse_value(token_stream) 189 assert parsed_output == 2, 'Actual == %r' % parsed_output 190 191 192 def testShillManagerGetProperties(self): 193 """Test that we correctly parse fairly complex output. 194 195 We could simply write expected == actual, but this lends 196 itself to debugging a little more. 197 198 """ 199 token_stream = dbus_send._build_token_stream( 200 EXAMPLE_SHILL_GET_PROPERTIES_OUTPUT.splitlines()[1:]) 201 parsed_output = dbus_send._parse_value(token_stream) 202 for k,v in PARSED_SHILL_GET_PROPERTIES_OUTPUT.iteritems(): 203 assert k in parsed_output, '%r not in parsed output' % k 204 actual_v = parsed_output.pop(k) 205 assert actual_v == v, 'Expected %r, got %r' % (v, actual_v) 206 207 assert len(parsed_output) == 0, ('Got extra parsed output: %r' % 208 parsed_output) 209