Home | History | Annotate | Download | only in cros
      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 import dbus
     11 from autotest_lib.client.common_lib.cros import dbus_send
     12 
     13 EXAMPLE_SHILL_GET_PROPERTIES_OUTPUT = (
     14 'method return sender=org.freedesktop.DBus -> destination=:1.37 serial=3 '
     15 'reply_serial=2\n'
     16 """
     17    array [
     18       dict entry(
     19          string "ActiveProfile"
     20          variant             string "/profile/default"
     21       )
     22       dict entry(
     23          string "ArpGateway"
     24          variant             boolean true
     25       )
     26       dict entry(
     27          string "AvailableTechnologies"
     28          variant             array [
     29                string "ethernet"
     30             ]
     31       )
     32       dict entry(
     33          string "CheckPortalList"
     34          variant             string "''"
     35       )
     36       dict entry(
     37          string "ConnectedTechnologies" variant             array [
     38                string "ethernet"
     39             ]
     40       )
     41       dict entry(
     42          string "ConnectionState"
     43          variant             string "online"
     44       )
     45       dict entry(
     46          string "Country"
     47          variant             string ""
     48       )
     49       dict entry(
     50          string "DefaultService"
     51          variant             object path "/service/2"
     52       )
     53       dict entry(
     54          string "DefaultTechnology"
     55          variant             string "ethernet"
     56       )
     57       dict entry(
     58          string "Devices"
     59          variant             array [
     60                object path "/device/eth0"
     61                object path "/device/eth1"
     62             ]
     63       )
     64       dict entry(
     65          string "DisableWiFiVHT"
     66          variant             boolean false
     67       )
     68       dict entry(
     69          string "EnabledTechnologies"
     70          variant             array [
     71                string "ethernet"
     72             ]
     73       )
     74       dict entry(
     75          string "HostName"
     76          variant             string ""
     77       )
     78       dict entry(
     79          string "IgnoredDNSSearchPaths"
     80          variant             string "gateway.2wire.net"
     81       )
     82       dict entry(
     83          string "LinkMonitorTechnologies"
     84          variant             string "wifi"
     85       )
     86       dict entry(
     87          string "NoAutoConnectTechnologies"
     88          variant             string ""
     89       )
     90       dict entry(
     91          string "OfflineMode"
     92          variant             boolean false
     93       )
     94       dict entry(
     95          string "PortalCheckInterval"
     96          variant             int32 30
     97       )
     98       dict entry(
     99          string "PortalURL"
    100          variant             string "http://www.gstatic.com/generate_204"
    101       )
    102       dict entry(
    103          string "Profiles"
    104          variant             array [
    105                object path "/profile/default"
    106             ]
    107       )
    108       dict entry(
    109          string "ProhibitedTechnologies"
    110          variant             string ""
    111       )
    112       dict entry(
    113          string "ServiceCompleteList"
    114          variant             array [
    115                object path "/service/2"
    116                object path "/service/1"
    117                object path "/service/0"
    118             ]
    119       )
    120       dict entry(
    121          string "ServiceWatchList"
    122          variant             array [
    123                object path "/service/2"
    124             ]
    125       )
    126       dict entry(
    127          string "Services"
    128          variant             array [
    129                object path "/service/2"
    130             ]
    131       )
    132       dict entry(
    133          string "State"
    134          variant             string "online"
    135       )
    136       dict entry(
    137          string "UninitializedTechnologies"
    138          variant             array [
    139             ]
    140       )
    141       dict entry(
    142          string "WakeOnLanEnabled"
    143          variant             boolean true
    144       )
    145    ]
    146 """)
    147 
    148 PARSED_SHILL_GET_PROPERTIES_OUTPUT = {
    149     'ActiveProfile': '/profile/default',
    150     'ArpGateway': True,
    151     'AvailableTechnologies': ['ethernet'],
    152     'CheckPortalList': "''",
    153     'ConnectedTechnologies': ['ethernet'],
    154     'ConnectionState': 'online',
    155     'Country': '',
    156     'DefaultService': '/service/2',
    157     'DefaultTechnology': 'ethernet',
    158     'Devices': ['/device/eth0', '/device/eth1'],
    159     'DisableWiFiVHT': False,
    160     'EnabledTechnologies': ['ethernet'],
    161     'HostName': '',
    162     'IgnoredDNSSearchPaths': 'gateway.2wire.net',
    163     'LinkMonitorTechnologies': 'wifi',
    164     'NoAutoConnectTechnologies': '',
    165     'OfflineMode': False,
    166     'PortalCheckInterval': 30,
    167     'PortalURL': 'http://www.gstatic.com/generate_204',
    168     'Profiles': ['/profile/default'],
    169     'ProhibitedTechnologies': '',
    170     'ServiceCompleteList': ['/service/2', '/service/1', '/service/0'],
    171     'ServiceWatchList': ['/service/2'],
    172     'Services': ['/service/2'],
    173     'State': 'online',
    174     'UninitializedTechnologies': [],
    175     'WakeOnLanEnabled': True,
    176 }
    177 
    178 EXAMPLE_AVAHI_GET_STATE_OUTPUT = (
    179 'method return sender=org.freedesktop.DBus -> destination=:1.40 serial=3 '
    180 'reply_serial=2\n'
    181 '   int32 2')
    182 
    183 class DBusSendTest(unittest.TestCase):
    184     """Check that we're correctly parsing dbus-send output."""
    185 
    186 
    187     def testAvahiGetState(self):
    188         """Test that extremely simple input works."""
    189         result = dbus_send._parse_dbus_send_output(
    190             EXAMPLE_AVAHI_GET_STATE_OUTPUT)
    191         assert result.sender == 'org.freedesktop.DBus', (
    192             'Sender == %r' % result.sender)
    193         assert result.responder == ':1.40', 'Responder == %r' % result.responder
    194         assert result.response == 2, 'Response == %r' % result.response
    195 
    196 
    197     def testShillManagerGetProperties(self):
    198         """Test that we correctly parse fairly complex output.
    199 
    200         We could simply write expected == actual, but this lends
    201         itself to debugging a little more.
    202 
    203         """
    204         result = dbus_send._parse_dbus_send_output(
    205             EXAMPLE_SHILL_GET_PROPERTIES_OUTPUT)
    206         assert result.sender == 'org.freedesktop.DBus', (
    207             'Sender == %r' % result.sender)
    208         assert result.responder == ':1.37', 'Responder == %r' % result.responder
    209         for k, v in PARSED_SHILL_GET_PROPERTIES_OUTPUT.iteritems():
    210             assert k in result.response, '%r not in response' % k
    211             actual_v = result.response.pop(k)
    212             assert actual_v == v, 'Expected %r, got %r' % (v, actual_v)
    213         assert len(result.response) == 0, (
    214             'Got extra response: %r' % result.response)
    215 
    216     def testBuildArgString(self):
    217         """Test that we correctly form argument strings from dbus.* types."""
    218         self.assertEquals(dbus_send._build_arg_string(
    219             [dbus.Int16(42)]),
    220             'int16:42')
    221         self.assertEquals(dbus_send._build_arg_string(
    222             [dbus.Int16(42), dbus.Boolean(True)]),
    223             'int16:42 boolean:true')
    224         self.assertEquals(dbus_send._build_arg_string(
    225             [dbus.Int16(42), dbus.Boolean(True), dbus.String("foo")]),
    226             'int16:42 boolean:true string:foo')
    227 
    228 
    229 if __name__ == "__main__":
    230     unittest.main()
    231