/mandos/trunk

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/trunk

« back to all changes in this revision

Viewing changes to mandos-ctl

  • Committer: Teddy Hogeborn
  • Date: 2019-06-06 19:21:43 UTC
  • Revision ID: teddy@recompile.se-20190606192143-pcmqbj98zmnmpgfp
.bzrignore: Add mandos-client-iprouteadddel and .tramp_history

* .bzrignore (plugin-helpers/mandos-client-iprouteadddel): Add.
  (.tramp_history): - '' -

Show diffs side-by-side

added added

removed removed

Lines of Context:
78
78
 
79
79
locale.setlocale(locale.LC_ALL, "")
80
80
 
81
 
version = "1.8.3"
 
81
version = "1.8.4"
82
82
 
83
83
 
84
84
def main():
571
571
                        for key, subval in value.items()}
572
572
            return value
573
573
 
 
574
        def set_client_property(self, objectpath, key, value):
 
575
            if key == "Secret":
 
576
                if not isinstance(value, bytes):
 
577
                    value = value.encode("utf-8")
 
578
                value = self.dbus_python.ByteArray(value)
 
579
            return self.set_property(self.busname, objectpath,
 
580
                                     self.client_interface, key,
 
581
                                     value)
574
582
 
575
583
    class SilenceLogger(object):
576
584
        "Simple context manager to silence a particular logger"
1312
1320
                @staticmethod
1313
1321
                def get_object(busname, objectpath):
1314
1322
                    DBusObject = collections.namedtuple(
1315
 
                        "DBusObject", ("methodname",))
 
1323
                        "DBusObject", ("methodname", "Set"))
1316
1324
                    def method(*args, **kwargs):
1317
1325
                        self.assertEqual({"dbus_interface":
1318
1326
                                          "interface"},
1319
1327
                                         kwargs)
1320
1328
                        return func(*args)
1321
 
                    return DBusObject(methodname=method)
 
1329
                    def set_property(interface, key, value,
 
1330
                                     dbus_interface=None):
 
1331
                        self.assertEqual(
 
1332
                            "org.freedesktop.DBus.Properties",
 
1333
                            dbus_interface)
 
1334
                        self.assertEqual("Secret", key)
 
1335
                        return func(interface, key, value,
 
1336
                                    dbus_interface=dbus_interface)
 
1337
                    return DBusObject(methodname=method,
 
1338
                                      Set=set_property)
1322
1339
            class Boolean(object):
1323
1340
                def __init__(self, value):
1324
1341
                    self.value = bool(value)
1330
1347
                pass
1331
1348
            class Dictionary(dict):
1332
1349
                pass
 
1350
            class ByteArray(bytes):
 
1351
                pass
1333
1352
        return mock_dbus_python
1334
1353
 
1335
1354
    def call_method(self, bus, methodname, busname, objectpath,
1512
1531
        # Make sure the dbus logger was suppressed
1513
1532
        self.assertEqual(0, counting_handler.count)
1514
1533
 
 
1534
    def test_Set_Secret_sends_bytearray(self):
 
1535
        ret = [None]
 
1536
        def func(*args, **kwargs):
 
1537
            ret[0] = (args, kwargs)
 
1538
        mock_dbus_python = self.MockDBusPython_func(func)
 
1539
        bus = dbus_python_adapter.SystemBus(mock_dbus_python)
 
1540
        bus.set_client_property("objectpath", "Secret", "value")
 
1541
        expected_call = (("se.recompile.Mandos.Client", "Secret",
 
1542
                          mock_dbus_python.ByteArray(b"value")),
 
1543
                         {"dbus_interface":
 
1544
                          "org.freedesktop.DBus.Properties"})
 
1545
        self.assertEqual(expected_call, ret[0])
 
1546
        if sys.version_info.major == 2:
 
1547
            self.assertIsInstance(ret[0][0][-1],
 
1548
                                  mock_dbus_python.ByteArray)
 
1549
 
1515
1550
    def test_get_object_converts_to_correct_exception(self):
1516
1551
        bus = dbus_python_adapter.SystemBus(
1517
1552
            self.fake_dbus_python_raises_exception_on_connect)
1745
1780
        self.assert_command_from_args(["--is-enabled", "client"],
1746
1781
                                      command.IsEnabled)
1747
1782
 
1748
 
    def assert_command_from_args(self, args, command_cls,
1749
 
                                 **cmd_attrs):
 
1783
    def assert_command_from_args(self, args, command_cls, length=1,
 
1784
                                 clients=None, **cmd_attrs):
1750
1785
        """Assert that parsing ARGS should result in an instance of
1751
1786
COMMAND_CLS with (optionally) all supplied attributes (CMD_ATTRS)."""
1752
1787
        options = self.parser.parse_args(args)
1753
1788
        check_option_syntax(self.parser, options)
1754
1789
        commands = commands_from_options(options)
1755
 
        self.assertEqual(1, len(commands))
1756
 
        command = commands[0]
1757
 
        self.assertIsInstance(command, command_cls)
 
1790
        self.assertEqual(length, len(commands))
 
1791
        for command in commands:
 
1792
            if isinstance(command, command_cls):
 
1793
                break
 
1794
        else:
 
1795
            self.assertIsInstance(command, command_cls)
 
1796
        if clients is not None:
 
1797
            self.assertEqual(clients, options.client)
1758
1798
        for key, value in cmd_attrs.items():
1759
1799
            self.assertEqual(value, getattr(command, key))
1760
1800
 
 
1801
    def assert_commands_from_args(self, args, commands, clients=None):
 
1802
        for cmd in commands:
 
1803
            self.assert_command_from_args(args, cmd,
 
1804
                                          length=len(commands),
 
1805
                                          clients=clients)
 
1806
 
1761
1807
    def test_is_enabled_short(self):
1762
1808
        self.assert_command_from_args(["-V", "client"],
1763
1809
                                      command.IsEnabled)
1954
2000
                                      verbose=True)
1955
2001
 
1956
2002
 
 
2003
    def test_manual_page_example_1(self):
 
2004
        self.assert_command_from_args("--verbose".split(),
 
2005
                                      command.PrintTable,
 
2006
                                      clients=[],
 
2007
                                      verbose=True)
 
2008
 
 
2009
    def test_manual_page_example_2(self):
 
2010
        self.assert_command_from_args(
 
2011
            "--verbose foo1.example.org foo2.example.org".split(),
 
2012
            command.PrintTable, clients=["foo1.example.org",
 
2013
                                         "foo2.example.org"],
 
2014
            verbose=True)
 
2015
 
 
2016
    def test_manual_page_example_3(self):
 
2017
        self.assert_command_from_args("--enable --all".split(),
 
2018
                                      command.Enable,
 
2019
                                      clients=[])
 
2020
 
 
2021
    def test_manual_page_example_4(self):
 
2022
        self.assert_commands_from_args(
 
2023
            ("--timeout=PT5M --interval=PT1M foo1.example.org"
 
2024
             " foo2.example.org").split(),
 
2025
            [command.SetTimeout, command.SetInterval],
 
2026
            clients=["foo1.example.org", "foo2.example.org"])
 
2027
 
 
2028
    def test_manual_page_example_5(self):
 
2029
        self.assert_command_from_args("--approve --all".split(),
 
2030
                                      command.Approve,
 
2031
                                      clients=[])
 
2032
 
 
2033
 
1957
2034
class TestCommand(unittest.TestCase):
1958
2035
    """Abstract class for tests of command classes"""
1959
2036