/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-05 21:39:15 UTC
  • Revision ID: teddy@recompile.se-20190305213915-xm1vw00jyy3a5tfn
mandos-ctl: Add more tests, including tests for all commands

* mandos-ctl (Test_string_to_delta.test_handles_basic_rfc3339): Add a
                                                  few more test cases.
  (TestCmd.setUp.MockClient.Set, TestCmd.setUp.MockClient.Get): Don't
  append to self.calls, since nobody should use it to check for Set()
  or Get() calls; instead, the return value of Get() should be
  inspected, and the MockClient.attributes dict should be inspected
  after (implicitly) calling Set().
  (Unique): New; stand-in for unittest.mock.sentinel.
  (TestPropertyCmd): New; abstract class testing PropertyCmd classes.
  (TestBumpTimeoutCmd, TestStartCheckerCmd, TestStopCheckerCmd,
  TestApproveByDefaultCmd, TestDenyByDefaultCmd): New.
  (TestValueArgumentPropertyCmd): New; abstract class for testing
                                  those PropertyCmd classes which also
                                  inherit from ValueArgumentMixIn.
  (TestSetCheckerCmd, TestSetHostCmd, TestSetSecretCmd,
  TestSetTimeoutCmd, TestSetExtendedTimeoutCmd, TestSetIntervalCmd,
  TestSetApprovalDelayCmd, TestSetApprovalDurationCmd): New.

Show diffs side-by-side

added added

removed removed

Lines of Context:
746
746
 
747
747
class Test_string_to_delta(unittest.TestCase):
748
748
    def test_handles_basic_rfc3339(self):
 
749
        self.assertEqual(string_to_delta("PT0S"),
 
750
                         datetime.timedelta())
 
751
        self.assertEqual(string_to_delta("P0D"),
 
752
                         datetime.timedelta())
 
753
        self.assertEqual(string_to_delta("PT1S"),
 
754
                         datetime.timedelta(0, 1))
749
755
        self.assertEqual(string_to_delta("PT2H"),
750
756
                         datetime.timedelta(0, 7200))
751
757
    def test_falls_back_to_pre_1_6_1_with_warning(self):
786
792
                testcase.assertEqual(dbus_interface,
787
793
                                     dbus.PROPERTIES_IFACE)
788
794
                self.attributes[property] = value
789
 
                self.calls.append(("Set", (interface, property, value,
790
 
                                           dbus_interface)))
791
795
            def Get(self, interface, property, dbus_interface):
792
796
                testcase.assertEqual(interface, client_interface)
793
797
                testcase.assertEqual(dbus_interface,
794
798
                                     dbus.PROPERTIES_IFACE)
795
 
                self.calls.append(("Get", (interface, property,
796
 
                                           dbus_interface)))
797
799
                return self.attributes[property]
798
800
            def Approve(self, approve, dbus_interface):
799
801
                testcase.assertEqual(dbus_interface, client_interface)
1008
1010
        for client in self.clients:
1009
1011
            self.assertFalse(client.attributes["Enabled"])
1010
1012
 
 
1013
class Unique(object):
 
1014
    """Class for objects which exist only to be unique objects, since
 
1015
unittest.mock.sentinel only exists in Python 3.3"""
 
1016
 
 
1017
class TestPropertyCmd(TestCmd):
 
1018
    """Abstract class for tests of PropertyCmd classes"""
 
1019
    def runTest(self):
 
1020
        if not hasattr(self, "command"):
 
1021
            return
 
1022
        values_to_get = getattr(self, "values_to_get",
 
1023
                                self.values_to_set)
 
1024
        for value_to_set, value_to_get in zip(self.values_to_set,
 
1025
                                              values_to_get):
 
1026
            for client in self.clients:
 
1027
                old_value = client.attributes[self.property]
 
1028
                self.assertNotIsInstance(old_value, Unique)
 
1029
                client.attributes[self.property] = Unique()
 
1030
            self.run_command(value_to_set, self.clients)
 
1031
            for client in self.clients:
 
1032
                value = client.attributes[self.property]
 
1033
                self.assertNotIsInstance(value, Unique)
 
1034
                self.assertEqual(value, value_to_get)
 
1035
    def run_command(self, value, clients):
 
1036
        self.command().run(None, clients)
 
1037
 
 
1038
class TestBumpTimeoutCmd(TestPropertyCmd):
 
1039
    command = BumpTimeoutCmd
 
1040
    property = "LastCheckedOK"
 
1041
    values_to_set = [""]
 
1042
 
 
1043
class TestStartCheckerCmd(TestPropertyCmd):
 
1044
    command = StartCheckerCmd
 
1045
    property = "CheckerRunning"
 
1046
    values_to_set = [dbus.Boolean(True)]
 
1047
 
 
1048
class TestStopCheckerCmd(TestPropertyCmd):
 
1049
    command = StopCheckerCmd
 
1050
    property = "CheckerRunning"
 
1051
    values_to_set = [dbus.Boolean(False)]
 
1052
 
 
1053
class TestApproveByDefaultCmd(TestPropertyCmd):
 
1054
    command = ApproveByDefaultCmd
 
1055
    property = "ApprovedByDefault"
 
1056
    values_to_set = [dbus.Boolean(True)]
 
1057
 
 
1058
class TestDenyByDefaultCmd(TestPropertyCmd):
 
1059
    command = DenyByDefaultCmd
 
1060
    property = "ApprovedByDefault"
 
1061
    values_to_set = [dbus.Boolean(False)]
 
1062
 
 
1063
class TestValueArgumentPropertyCmd(TestPropertyCmd):
 
1064
    """Abstract class for tests of PropertyCmd classes using the
 
1065
ValueArgumentMixIn"""
 
1066
    def runTest(self):
 
1067
        if type(self) is TestValueArgumentPropertyCmd:
 
1068
            return
 
1069
        return super(TestValueArgumentPropertyCmd, self).runTest()
 
1070
    def run_command(self, value, clients):
 
1071
        self.command(value).run(None, clients)
 
1072
 
 
1073
class TestSetCheckerCmd(TestValueArgumentPropertyCmd):
 
1074
    command = SetCheckerCmd
 
1075
    property = "Checker"
 
1076
    values_to_set = ["", ":", "fping -q -- %s"]
 
1077
 
 
1078
class TestSetHostCmd(TestValueArgumentPropertyCmd):
 
1079
    command = SetHostCmd
 
1080
    property = "Host"
 
1081
    values_to_set = ["192.0.2.3", "foo.example.org"]
 
1082
 
 
1083
class TestSetSecretCmd(TestValueArgumentPropertyCmd):
 
1084
    command = SetSecretCmd
 
1085
    property = "Secret"
 
1086
    values_to_set = [b"", b"secret"]
 
1087
 
 
1088
class TestSetTimeoutCmd(TestValueArgumentPropertyCmd):
 
1089
    command = SetTimeoutCmd
 
1090
    property = "Timeout"
 
1091
    values_to_set = ["P0D", "PT5M", "PT1S", "PT120S", "P1Y"]
 
1092
    values_to_get = [0, 300000, 1000, 120000, 31449600000]
 
1093
 
 
1094
class TestSetExtendedTimeoutCmd(TestValueArgumentPropertyCmd):
 
1095
    command = SetExtendedTimeoutCmd
 
1096
    property = "ExtendedTimeout"
 
1097
    values_to_set = ["P0D", "PT5M", "PT1S", "PT120S", "P1Y"]
 
1098
    values_to_get = [0, 300000, 1000, 120000, 31449600000]
 
1099
 
 
1100
class TestSetIntervalCmd(TestValueArgumentPropertyCmd):
 
1101
    command = SetIntervalCmd
 
1102
    property = "Interval"
 
1103
    values_to_set = ["P0D", "PT5M", "PT1S", "PT120S", "P1Y"]
 
1104
    values_to_get = [0, 300000, 1000, 120000, 31449600000]
 
1105
 
 
1106
class TestSetApprovalDelayCmd(TestValueArgumentPropertyCmd):
 
1107
    command = SetApprovalDelayCmd
 
1108
    property = "ApprovalDelay"
 
1109
    values_to_set = ["P0D", "PT5M", "PT1S", "PT120S", "P1Y"]
 
1110
    values_to_get = [0, 300000, 1000, 120000, 31449600000]
 
1111
 
 
1112
class TestSetApprovalDurationCmd(TestValueArgumentPropertyCmd):
 
1113
    command = SetApprovalDurationCmd
 
1114
    property = "ApprovalDuration"
 
1115
    values_to_set = ["P0D", "PT5M", "PT1S", "PT120S", "P1Y"]
 
1116
    values_to_get = [0, 300000, 1000, 120000, 31449600000]
 
1117
 
1011
1118
 
1012
1119
 
1013
1120
def should_only_run_tests():