/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-03-18 22:29:25 UTC
  • Revision ID: teddy@recompile.se-20190318222925-jvhek84dgcfgj6g3
mandos-ctl: Refactor tests

* mandos-ctl: Where the clients names "foo" and "barbar" do not refer
              to the actual mock clients in the TestCommand class,
              change all occurrences of these names to "client1" and
              "client2" (or just "client" when only one is used) .
              Also change all test doubles to use correct terminology;
              some things called mocks are actually stubs or spies,
              and rename all true mocks to have "mock" in their names.
              Also eliminate duplicate values in tests; derive values
              from previously defined values whenever possible.

Show diffs side-by-side

added added

removed removed

Lines of Context:
83
83
 
84
84
def main():
85
85
    parser = argparse.ArgumentParser()
 
86
 
86
87
    add_command_line_options(parser)
87
88
 
88
89
    options = parser.parse_args()
 
90
 
89
91
    check_option_syntax(parser, options)
90
92
 
91
93
    clientnames = options.client
460
462
 
461
463
    def __enter__(self):
462
464
        self.logger.addFilter(self.nullfilter)
 
465
        return self
463
466
 
464
467
    class NullFilter(logging.Filter):
465
468
        def filter(self, record):
556
559
but commands which want to operate on all clients at the same time can
557
560
override this run() method instead.
558
561
"""
 
562
            self.mandos = mandos
559
563
            for clientpath, properties in clients.items():
560
564
                log.debug("D-Bus: Connect to: (busname=%r, path=%r)",
561
565
                          dbus_busname, str(clientpath))
592
596
 
593
597
 
594
598
    class Remove(Base):
595
 
        def run(self, clients, bus, mandos):
596
 
            for clientpath in clients.keys():
597
 
                log.debug("D-Bus: %s:%s:%s.RemoveClient(%r)",
598
 
                          dbus_busname, server_dbus_path,
599
 
                          server_dbus_interface, clientpath)
600
 
                mandos.RemoveClient(clientpath)
 
599
        def run_on_one_client(self, client, properties):
 
600
            log.debug("D-Bus: %s:%s:%s.RemoveClient(%r)",
 
601
                      dbus_busname, server_dbus_path,
 
602
                      server_dbus_interface,
 
603
                      str(client.__dbus_object_path__))
 
604
            self.mandos.RemoveClient(client.__dbus_object_path__)
601
605
 
602
606
 
603
607
    class Output(Base):
724
728
                                seconds=td.seconds % 60))
725
729
 
726
730
 
727
 
    class PropertySetter(Base):
 
731
    class Property(Base):
728
732
        "Abstract class for Actions for setting one client property"
729
733
 
730
734
        def run_on_one_client(self, client, properties):
745
749
            raise NotImplementedError()
746
750
 
747
751
 
748
 
    class Enable(PropertySetter):
 
752
    class Enable(Property):
749
753
        propname = "Enabled"
750
754
        value_to_set = dbus.Boolean(True)
751
755
 
752
756
 
753
 
    class Disable(PropertySetter):
 
757
    class Disable(Property):
754
758
        propname = "Enabled"
755
759
        value_to_set = dbus.Boolean(False)
756
760
 
757
761
 
758
 
    class BumpTimeout(PropertySetter):
 
762
    class BumpTimeout(Property):
759
763
        propname = "LastCheckedOK"
760
764
        value_to_set = ""
761
765
 
762
766
 
763
 
    class StartChecker(PropertySetter):
764
 
        propname = "CheckerRunning"
765
 
        value_to_set = dbus.Boolean(True)
766
 
 
767
 
 
768
 
    class StopChecker(PropertySetter):
769
 
        propname = "CheckerRunning"
770
 
        value_to_set = dbus.Boolean(False)
771
 
 
772
 
 
773
 
    class ApproveByDefault(PropertySetter):
774
 
        propname = "ApprovedByDefault"
775
 
        value_to_set = dbus.Boolean(True)
776
 
 
777
 
 
778
 
    class DenyByDefault(PropertySetter):
779
 
        propname = "ApprovedByDefault"
780
 
        value_to_set = dbus.Boolean(False)
781
 
 
782
 
 
783
 
    class PropertySetterValue(PropertySetter):
784
 
        """Abstract class for PropertySetter recieving a value as
785
 
constructor argument instead of a class attribute."""
 
767
    class StartChecker(Property):
 
768
        propname = "CheckerRunning"
 
769
        value_to_set = dbus.Boolean(True)
 
770
 
 
771
 
 
772
    class StopChecker(Property):
 
773
        propname = "CheckerRunning"
 
774
        value_to_set = dbus.Boolean(False)
 
775
 
 
776
 
 
777
    class ApproveByDefault(Property):
 
778
        propname = "ApprovedByDefault"
 
779
        value_to_set = dbus.Boolean(True)
 
780
 
 
781
 
 
782
    class DenyByDefault(Property):
 
783
        propname = "ApprovedByDefault"
 
784
        value_to_set = dbus.Boolean(False)
 
785
 
 
786
 
 
787
    class PropertyValue(Property):
 
788
        "Abstract class for Property recieving a value as argument"
786
789
        def __init__(self, value):
787
790
            self.value_to_set = value
788
791
 
789
792
 
790
 
    class SetChecker(PropertySetterValue):
 
793
    class SetChecker(PropertyValue):
791
794
        propname = "Checker"
792
795
 
793
796
 
794
 
    class SetHost(PropertySetterValue):
 
797
    class SetHost(PropertyValue):
795
798
        propname = "Host"
796
799
 
797
800
 
798
 
    class SetSecret(PropertySetterValue):
 
801
    class SetSecret(PropertyValue):
799
802
        propname = "Secret"
800
803
 
801
804
        @property
809
812
            value.close()
810
813
 
811
814
 
812
 
    class PropertySetterValueMilliseconds(PropertySetterValue):
813
 
        """Abstract class for PropertySetterValue taking a value
814
 
argument as a datetime.timedelta() but should store it as
815
 
milliseconds."""
 
815
    class MillisecondsPropertyValueArgument(PropertyValue):
 
816
        """Abstract class for PropertyValue taking a value argument as
 
817
a datetime.timedelta() but should store it as milliseconds."""
816
818
 
817
819
        @property
818
820
        def value_to_set(self):
824
826
            self._vts = int(round(value.total_seconds() * 1000))
825
827
 
826
828
 
827
 
    class SetTimeout(PropertySetterValueMilliseconds):
 
829
    class SetTimeout(MillisecondsPropertyValueArgument):
828
830
        propname = "Timeout"
829
831
 
830
832
 
831
 
    class SetExtendedTimeout(PropertySetterValueMilliseconds):
 
833
    class SetExtendedTimeout(MillisecondsPropertyValueArgument):
832
834
        propname = "ExtendedTimeout"
833
835
 
834
836
 
835
 
    class SetInterval(PropertySetterValueMilliseconds):
 
837
    class SetInterval(MillisecondsPropertyValueArgument):
836
838
        propname = "Interval"
837
839
 
838
840
 
839
 
    class SetApprovalDelay(PropertySetterValueMilliseconds):
 
841
    class SetApprovalDelay(MillisecondsPropertyValueArgument):
840
842
        propname = "ApprovalDelay"
841
843
 
842
844
 
843
 
    class SetApprovalDuration(PropertySetterValueMilliseconds):
 
845
    class SetApprovalDuration(MillisecondsPropertyValueArgument):
844
846
        propname = "ApprovalDuration"
845
847
 
846
848
 
1697
1699
        self.assertEqual(expected_output, buffer.getvalue())
1698
1700
 
1699
1701
 
1700
 
class TestPropertySetterCmd(TestCommand):
1701
 
    """Abstract class for tests of command.PropertySetter classes"""
 
1702
class TestPropertyCmd(TestCommand):
 
1703
    """Abstract class for tests of command.Property classes"""
1702
1704
    def runTest(self):
1703
1705
        if not hasattr(self, "command"):
1704
1706
            return
1725
1727
        self.command().run(clients, self.bus)
1726
1728
 
1727
1729
 
1728
 
class TestEnableCmd(TestPropertySetterCmd):
 
1730
class TestEnableCmd(TestPropertyCmd):
1729
1731
    command = command.Enable
1730
1732
    propname = "Enabled"
1731
1733
    values_to_set = [dbus.Boolean(True)]
1732
1734
 
1733
1735
 
1734
 
class TestDisableCmd(TestPropertySetterCmd):
 
1736
class TestDisableCmd(TestPropertyCmd):
1735
1737
    command = command.Disable
1736
1738
    propname = "Enabled"
1737
1739
    values_to_set = [dbus.Boolean(False)]
1738
1740
 
1739
1741
 
1740
 
class TestBumpTimeoutCmd(TestPropertySetterCmd):
 
1742
class TestBumpTimeoutCmd(TestPropertyCmd):
1741
1743
    command = command.BumpTimeout
1742
1744
    propname = "LastCheckedOK"
1743
1745
    values_to_set = [""]
1744
1746
 
1745
1747
 
1746
 
class TestStartCheckerCmd(TestPropertySetterCmd):
 
1748
class TestStartCheckerCmd(TestPropertyCmd):
1747
1749
    command = command.StartChecker
1748
1750
    propname = "CheckerRunning"
1749
1751
    values_to_set = [dbus.Boolean(True)]
1750
1752
 
1751
1753
 
1752
 
class TestStopCheckerCmd(TestPropertySetterCmd):
 
1754
class TestStopCheckerCmd(TestPropertyCmd):
1753
1755
    command = command.StopChecker
1754
1756
    propname = "CheckerRunning"
1755
1757
    values_to_set = [dbus.Boolean(False)]
1756
1758
 
1757
1759
 
1758
 
class TestApproveByDefaultCmd(TestPropertySetterCmd):
 
1760
class TestApproveByDefaultCmd(TestPropertyCmd):
1759
1761
    command = command.ApproveByDefault
1760
1762
    propname = "ApprovedByDefault"
1761
1763
    values_to_set = [dbus.Boolean(True)]
1762
1764
 
1763
1765
 
1764
 
class TestDenyByDefaultCmd(TestPropertySetterCmd):
 
1766
class TestDenyByDefaultCmd(TestPropertyCmd):
1765
1767
    command = command.DenyByDefault
1766
1768
    propname = "ApprovedByDefault"
1767
1769
    values_to_set = [dbus.Boolean(False)]
1768
1770
 
1769
1771
 
1770
 
class TestPropertySetterValueCmd(TestPropertySetterCmd):
1771
 
    """Abstract class for tests of PropertySetterValueCmd classes"""
 
1772
class TestPropertyValueCmd(TestPropertyCmd):
 
1773
    """Abstract class for tests of PropertyValueCmd classes"""
1772
1774
 
1773
1775
    def runTest(self):
1774
 
        if type(self) is TestPropertySetterValueCmd:
 
1776
        if type(self) is TestPropertyValueCmd:
1775
1777
            return
1776
 
        return super(TestPropertySetterValueCmd, self).runTest()
 
1778
        return super(TestPropertyValueCmd, self).runTest()
1777
1779
 
1778
1780
    def run_command(self, value, clients):
1779
1781
        self.command(value).run(clients, self.bus)
1780
1782
 
1781
1783
 
1782
 
class TestSetCheckerCmd(TestPropertySetterValueCmd):
 
1784
class TestSetCheckerCmd(TestPropertyValueCmd):
1783
1785
    command = command.SetChecker
1784
1786
    propname = "Checker"
1785
1787
    values_to_set = ["", ":", "fping -q -- %s"]
1786
1788
 
1787
1789
 
1788
 
class TestSetHostCmd(TestPropertySetterValueCmd):
 
1790
class TestSetHostCmd(TestPropertyValueCmd):
1789
1791
    command = command.SetHost
1790
1792
    propname = "Host"
1791
1793
    values_to_set = ["192.0.2.3", "client.example.org"]
1792
1794
 
1793
1795
 
1794
 
class TestSetSecretCmd(TestPropertySetterValueCmd):
 
1796
class TestSetSecretCmd(TestPropertyValueCmd):
1795
1797
    command = command.SetSecret
1796
1798
    propname = "Secret"
1797
1799
    values_to_set = [io.BytesIO(b""),
1799
1801
    values_to_get = [f.getvalue() for f in values_to_set]
1800
1802
 
1801
1803
 
1802
 
class TestSetTimeoutCmd(TestPropertySetterValueCmd):
 
1804
class TestSetTimeoutCmd(TestPropertyValueCmd):
1803
1805
    command = command.SetTimeout
1804
1806
    propname = "Timeout"
1805
1807
    values_to_set = [datetime.timedelta(),
1810
1812
    values_to_get = [dt.total_seconds()*1000 for dt in values_to_set]
1811
1813
 
1812
1814
 
1813
 
class TestSetExtendedTimeoutCmd(TestPropertySetterValueCmd):
 
1815
class TestSetExtendedTimeoutCmd(TestPropertyValueCmd):
1814
1816
    command = command.SetExtendedTimeout
1815
1817
    propname = "ExtendedTimeout"
1816
1818
    values_to_set = [datetime.timedelta(),
1821
1823
    values_to_get = [dt.total_seconds()*1000 for dt in values_to_set]
1822
1824
 
1823
1825
 
1824
 
class TestSetIntervalCmd(TestPropertySetterValueCmd):
 
1826
class TestSetIntervalCmd(TestPropertyValueCmd):
1825
1827
    command = command.SetInterval
1826
1828
    propname = "Interval"
1827
1829
    values_to_set = [datetime.timedelta(),
1832
1834
    values_to_get = [dt.total_seconds()*1000 for dt in values_to_set]
1833
1835
 
1834
1836
 
1835
 
class TestSetApprovalDelayCmd(TestPropertySetterValueCmd):
 
1837
class TestSetApprovalDelayCmd(TestPropertyValueCmd):
1836
1838
    command = command.SetApprovalDelay
1837
1839
    propname = "ApprovalDelay"
1838
1840
    values_to_set = [datetime.timedelta(),
1843
1845
    values_to_get = [dt.total_seconds()*1000 for dt in values_to_set]
1844
1846
 
1845
1847
 
1846
 
class TestSetApprovalDurationCmd(TestPropertySetterValueCmd):
 
1848
class TestSetApprovalDurationCmd(TestPropertyValueCmd):
1847
1849
    command = command.SetApprovalDuration
1848
1850
    propname = "ApprovalDuration"
1849
1851
    values_to_set = [datetime.timedelta(),