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