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