• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2017 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import dbus
6
7from autotest_lib.client.bin import test
8from autotest_lib.client.common_lib import error
9from autotest_lib.client.cros import debugd_util
10
11_GENERIC_PPD = 'GenericPostScript.ppd.gz'
12
13# Values are from platform/system_api/dbus/debugd/dbus-constants.h.
14_CUPS_SUCCESS = 0
15_CUPS_INVALID_PPD_ERROR = 2
16_CUPS_LPADMIN_ERROR = 3
17_CUPS_AUTOCONF_FAILURE = 4
18
19
20class platform_DebugDaemonCupsAddPrinters(test.test):
21    """
22    Exercise CupsAddManuallyConfiguredPrinter from debugd.
23
24    Exercise the various add printer conditions and verify that the
25    error codes are correct.
26
27    """
28    version = 1
29
30    def load_ppd(self, file_name):
31        """
32        Returns the contents of a file as a dbus.ByteArray.
33
34        @param file_name: The name of the file.
35
36        """
37        abs_path = '%s/%s' % (self.srcdir, file_name)
38        with open(abs_path, 'rb') as f:
39            content = dbus.ByteArray(f.read())
40        return content
41
42    def test_autoconf(self):
43        """
44        Attempt to add an unreachable autoconfigured printer.
45
46        Verifies that upon autoconf failure, the error code is
47        CUPS_AUTOCONF_FAILURE.
48
49        @raises TestFail: If the test failed.
50
51        """
52        autoconfig_result = debugd_util.iface().CupsAddAutoConfiguredPrinter(
53                            'AutoconfPrinter', 'ipp://127.0.0.1/ipp/print')
54        # There's no printer at this address.  Autoconf failure expected.
55        # CUPS_AUTOCONF_FAILURE.
56        if autoconfig_result != _CUPS_AUTOCONF_FAILURE:
57            raise error.TestFail('Incorrect error code received: %i' %
58                                 autoconfig_result)
59
60    def test_ppd_error(self):
61        """
62        Validates that malformed PPDs are rejected.
63
64        The expected error code is CUPS_INVALID_PPD error.
65
66        @raises TestFail: If the test failed.
67
68        """
69        ppd_contents = dbus.ByteArray('This is not a valid ppd')
70        result = debugd_util.iface().CupsAddManuallyConfiguredPrinter(
71                'ManualPrinterBreaks', 'socket://127.0.0.1/ipp/fake_printer',
72                ppd_contents)
73        # PPD is invalid.  Expect a CUPS_INVALID_PPD error.
74        if result != _CUPS_INVALID_PPD_ERROR:
75            raise error.TestFail('Incorrect error code received %d' % result)
76
77    def test_valid_config(self):
78        """
79        Validates that a printer can be installed.
80
81        Verifies that given a valid configuration and a well formed PPD,
82        DebugDaemon reports a CUPS_SUCCESS error code indicating
83        success.
84
85        @raises TestFail: If the result from debugd was not CUPS_SUCCESS.
86
87        """
88        ppd_contents = self.load_ppd(_GENERIC_PPD)
89        result = debugd_util.iface().CupsAddManuallyConfiguredPrinter(
90                 'ManualPrinterGood', 'socket://127.0.0.1/ipp/fake_printer',
91                 ppd_contents)
92        # PPD is valid.  Printer doesn't need to be reachable.  This is
93        # expected to pass with CUPS_SUCCESS.
94        if result != _CUPS_SUCCESS:
95            raise error.TestFail('Could not setup valid printer %d' % result)
96
97    def test_lpadmin(self):
98        """
99        Verify the error for a failure in lpadmin.
100
101        The failure is reported as CUPS_LPADMIN_FAILURE.
102
103        @raises TestFail: If the error code from debugd is incorrect.
104
105        """
106        ppd_contents = self.load_ppd(_GENERIC_PPD)
107        result = debugd_util.iface().CupsAddManuallyConfiguredPrinter(
108                 'CUPS rejects names with spaces',
109                 'socket://127.0.0.1/ipp/fake_printer',
110                 ppd_contents)
111        if result != _CUPS_LPADMIN_ERROR:
112            raise error.TestFail(
113                'Names with spaces should be rejected by CUPS %d' % result)
114
115        result = debugd_util.iface().CupsAddManuallyConfiguredPrinter(
116                 'UnrecognizedProtocol',
117                 'badbadbad://127.0.0.1/ipp/fake_printer',
118                  ppd_contents)
119        if result != _CUPS_LPADMIN_ERROR:
120            raise error.TestFail(
121                  'Unrecognized protocols should be rejected by CUPS. %d' %
122                  result)
123
124    def run_once(self, situation):
125        """
126        Runs tests based on the designated situation.
127
128        @raises TestError: If an unrecognized situation was used.
129
130        """
131        if situation == 'valid_config':
132            self.test_valid_config()
133        elif situation == 'lpadmin':
134            self.test_lpadmin()
135        elif situation == 'ppd_error':
136            self.test_ppd_error()
137        elif situation == 'autoconf':
138            self.test_autoconf()
139        else:
140            raise error.TestError('situation must be autoconf, valid_config, '
141                                  'lpadmin, or ppd_error')
142