/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-17 21:29:32 UTC
  • Revision ID: teddy@recompile.se-20190317212932-r3libgz33mkb85rw
mandos-ctl: Refactor

* mandos-ctl: For Python 2, use StringIO.StringIO as a replacement for
              io.StringIO, since Python 2's io.StringIO won't work
              with print redirection.
  (Output.run, Output.output): Remove.
  (DumpJSON.output): Rename to "run" and change signature to match.
                     Also change code to print instead of returning
                     string.
  (PrintTable.output): - '' -

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):
725
728
                                seconds=td.seconds % 60))
726
729
 
727
730
 
728
 
    class PropertySetter(Base):
 
731
    class Property(Base):
729
732
        "Abstract class for Actions for setting one client property"
730
733
 
731
734
        def run_on_one_client(self, client, properties):
746
749
            raise NotImplementedError()
747
750
 
748
751
 
749
 
    class Enable(PropertySetter):
 
752
    class Enable(Property):
750
753
        propname = "Enabled"
751
754
        value_to_set = dbus.Boolean(True)
752
755
 
753
756
 
754
 
    class Disable(PropertySetter):
 
757
    class Disable(Property):
755
758
        propname = "Enabled"
756
759
        value_to_set = dbus.Boolean(False)
757
760
 
758
761
 
759
 
    class BumpTimeout(PropertySetter):
 
762
    class BumpTimeout(Property):
760
763
        propname = "LastCheckedOK"
761
764
        value_to_set = ""
762
765
 
763
766
 
764
 
    class StartChecker(PropertySetter):
765
 
        propname = "CheckerRunning"
766
 
        value_to_set = dbus.Boolean(True)
767
 
 
768
 
 
769
 
    class StopChecker(PropertySetter):
770
 
        propname = "CheckerRunning"
771
 
        value_to_set = dbus.Boolean(False)
772
 
 
773
 
 
774
 
    class ApproveByDefault(PropertySetter):
775
 
        propname = "ApprovedByDefault"
776
 
        value_to_set = dbus.Boolean(True)
777
 
 
778
 
 
779
 
    class DenyByDefault(PropertySetter):
780
 
        propname = "ApprovedByDefault"
781
 
        value_to_set = dbus.Boolean(False)
782
 
 
783
 
 
784
 
    class PropertySetterValue(PropertySetter):
785
 
        """Abstract class for PropertySetter recieving a value as
786
 
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"
787
789
        def __init__(self, value):
788
790
            self.value_to_set = value
789
791
 
790
792
 
791
 
    class SetChecker(PropertySetterValue):
 
793
    class SetChecker(PropertyValue):
792
794
        propname = "Checker"
793
795
 
794
796
 
795
 
    class SetHost(PropertySetterValue):
 
797
    class SetHost(PropertyValue):
796
798
        propname = "Host"
797
799
 
798
800
 
799
 
    class SetSecret(PropertySetterValue):
 
801
    class SetSecret(PropertyValue):
800
802
        propname = "Secret"
801
803
 
802
804
        @property
810
812
            value.close()
811
813
 
812
814
 
813
 
    class PropertySetterValueMilliseconds(PropertySetterValue):
814
 
        """Abstract class for PropertySetterValue taking a value
815
 
argument as a datetime.timedelta() but should store it as
816
 
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."""
817
818
 
818
819
        @property
819
820
        def value_to_set(self):
825
826
            self._vts = int(round(value.total_seconds() * 1000))
826
827
 
827
828
 
828
 
    class SetTimeout(PropertySetterValueMilliseconds):
 
829
    class SetTimeout(MillisecondsPropertyValueArgument):
829
830
        propname = "Timeout"
830
831
 
831
832
 
832
 
    class SetExtendedTimeout(PropertySetterValueMilliseconds):
 
833
    class SetExtendedTimeout(MillisecondsPropertyValueArgument):
833
834
        propname = "ExtendedTimeout"
834
835
 
835
836
 
836
 
    class SetInterval(PropertySetterValueMilliseconds):
 
837
    class SetInterval(MillisecondsPropertyValueArgument):
837
838
        propname = "Interval"
838
839
 
839
840
 
840
 
    class SetApprovalDelay(PropertySetterValueMilliseconds):
 
841
    class SetApprovalDelay(MillisecondsPropertyValueArgument):
841
842
        propname = "ApprovalDelay"
842
843
 
843
844
 
844
 
    class SetApprovalDuration(PropertySetterValueMilliseconds):
 
845
    class SetApprovalDuration(MillisecondsPropertyValueArgument):
845
846
        propname = "ApprovalDuration"
846
847
 
847
848
 
982
983
            options = self.parser.parse_args()
983
984
            setattr(options, action, value)
984
985
            options.verbose = True
985
 
            options.client = ["client"]
 
986
            options.client = ["foo"]
986
987
            with self.assertParseError():
987
988
                self.check_option_syntax(options)
988
989
 
1018
1019
        for action, value in self.actions.items():
1019
1020
            options = self.parser.parse_args()
1020
1021
            setattr(options, action, value)
1021
 
            options.client = ["client"]
 
1022
            options.client = ["foo"]
1022
1023
            self.check_option_syntax(options)
1023
1024
 
1024
1025
    def test_one_client_with_all_actions_except_is_enabled(self):
1027
1028
            if action == "is_enabled":
1028
1029
                continue
1029
1030
            setattr(options, action, value)
1030
 
        options.client = ["client"]
 
1031
        options.client = ["foo"]
1031
1032
        self.check_option_syntax(options)
1032
1033
 
1033
1034
    def test_two_clients_with_all_actions_except_is_enabled(self):
1036
1037
            if action == "is_enabled":
1037
1038
                continue
1038
1039
            setattr(options, action, value)
1039
 
        options.client = ["client1", "client2"]
 
1040
        options.client = ["foo", "barbar"]
1040
1041
        self.check_option_syntax(options)
1041
1042
 
1042
1043
    def test_two_clients_are_ok_with_actions_except_is_enabled(self):
1045
1046
                continue
1046
1047
            options = self.parser.parse_args()
1047
1048
            setattr(options, action, value)
1048
 
            options.client = ["client1", "client2"]
 
1049
            options.client = ["foo", "barbar"]
1049
1050
            self.check_option_syntax(options)
1050
1051
 
1051
1052
    def test_is_enabled_fails_without_client(self):
1057
1058
    def test_is_enabled_fails_with_two_clients(self):
1058
1059
        options = self.parser.parse_args()
1059
1060
        options.is_enabled = True
1060
 
        options.client = ["client1", "client2"]
 
1061
        options.client = ["foo", "barbar"]
1061
1062
        with self.assertParseError():
1062
1063
            self.check_option_syntax(options)
1063
1064
 
1090
1091
        self.assertTrue(mockbus.called)
1091
1092
 
1092
1093
    def test_logs_and_exits_on_dbus_error(self):
1093
 
        class FailingBusStub(object):
 
1094
        class MockBusFailing(object):
1094
1095
            def get_object(self, busname, dbus_path):
1095
1096
                raise dbus.exceptions.DBusException("Test")
1096
1097
 
1097
1098
        with self.assertLogs(log, logging.CRITICAL):
1098
1099
            with self.assertRaises(SystemExit) as e:
1099
 
                bus = get_mandos_dbus_object(bus=FailingBusStub())
 
1100
                bus = get_mandos_dbus_object(bus=MockBusFailing())
1100
1101
 
1101
1102
        if isinstance(e.exception.code, int):
1102
1103
            self.assertNotEqual(0, e.exception.code)
1106
1107
 
1107
1108
class Test_get_managed_objects(TestCaseWithAssertLogs):
1108
1109
    def test_calls_and_returns_GetManagedObjects(self):
1109
 
        managed_objects = {"/clients/client": { "Name": "client"}}
1110
 
        class ObjectManagerStub(object):
 
1110
        managed_objects = {"/clients/foo": { "Name": "foo"}}
 
1111
        class MockObjectManager(object):
1111
1112
            def GetManagedObjects(self):
1112
1113
                return managed_objects
1113
 
        retval = get_managed_objects(ObjectManagerStub())
 
1114
        retval = get_managed_objects(MockObjectManager())
1114
1115
        self.assertDictEqual(managed_objects, retval)
1115
1116
 
1116
1117
    def test_logs_and_exits_on_dbus_error(self):
1117
1118
        dbus_logger = logging.getLogger("dbus.proxies")
1118
1119
 
1119
 
        class ObjectManagerFailingStub(object):
 
1120
        class MockObjectManagerFailing(object):
1120
1121
            def GetManagedObjects(self):
1121
1122
                dbus_logger.error("Test")
1122
1123
                raise dbus.exceptions.DBusException("Test")
1133
1134
        try:
1134
1135
            with self.assertLogs(log, logging.CRITICAL) as watcher:
1135
1136
                with self.assertRaises(SystemExit) as e:
1136
 
                    get_managed_objects(ObjectManagerFailingStub())
 
1137
                    get_managed_objects(MockObjectManagerFailing())
1137
1138
        finally:
1138
1139
            dbus_logger.removeFilter(counting_handler)
1139
1140
 
1156
1157
        add_command_line_options(self.parser)
1157
1158
 
1158
1159
    def test_is_enabled(self):
1159
 
        self.assert_command_from_args(["--is-enabled", "client"],
 
1160
        self.assert_command_from_args(["--is-enabled", "foo"],
1160
1161
                                      command.IsEnabled)
1161
1162
 
1162
1163
    def assert_command_from_args(self, args, command_cls,
1173
1174
            self.assertEqual(value, getattr(command, key))
1174
1175
 
1175
1176
    def test_is_enabled_short(self):
1176
 
        self.assert_command_from_args(["-V", "client"],
 
1177
        self.assert_command_from_args(["-V", "foo"],
1177
1178
                                      command.IsEnabled)
1178
1179
 
1179
1180
    def test_approve(self):
1180
 
        self.assert_command_from_args(["--approve", "client"],
 
1181
        self.assert_command_from_args(["--approve", "foo"],
1181
1182
                                      command.Approve)
1182
1183
 
1183
1184
    def test_approve_short(self):
1184
 
        self.assert_command_from_args(["-A", "client"],
1185
 
                                      command.Approve)
 
1185
        self.assert_command_from_args(["-A", "foo"], command.Approve)
1186
1186
 
1187
1187
    def test_deny(self):
1188
 
        self.assert_command_from_args(["--deny", "client"],
1189
 
                                      command.Deny)
 
1188
        self.assert_command_from_args(["--deny", "foo"], command.Deny)
1190
1189
 
1191
1190
    def test_deny_short(self):
1192
 
        self.assert_command_from_args(["-D", "client"], command.Deny)
 
1191
        self.assert_command_from_args(["-D", "foo"], command.Deny)
1193
1192
 
1194
1193
    def test_remove(self):
1195
 
        self.assert_command_from_args(["--remove", "client"],
 
1194
        self.assert_command_from_args(["--remove", "foo"],
1196
1195
                                      command.Remove)
1197
1196
 
1198
1197
    def test_deny_before_remove(self):
1199
1198
        options = self.parser.parse_args(["--deny", "--remove",
1200
 
                                          "client"])
 
1199
                                          "foo"])
1201
1200
        check_option_syntax(self.parser, options)
1202
1201
        commands = commands_from_options(options)
1203
1202
        self.assertEqual(2, len(commands))
1214
1213
        self.assertIsInstance(commands[1], command.Remove)
1215
1214
 
1216
1215
    def test_remove_short(self):
1217
 
        self.assert_command_from_args(["-r", "client"],
1218
 
                                      command.Remove)
 
1216
        self.assert_command_from_args(["-r", "foo"], command.Remove)
1219
1217
 
1220
1218
    def test_dump_json(self):
1221
1219
        self.assert_command_from_args(["--dump-json"],
1222
1220
                                      command.DumpJSON)
1223
1221
 
1224
1222
    def test_enable(self):
1225
 
        self.assert_command_from_args(["--enable", "client"],
 
1223
        self.assert_command_from_args(["--enable", "foo"],
1226
1224
                                      command.Enable)
1227
1225
 
1228
1226
    def test_enable_short(self):
1229
 
        self.assert_command_from_args(["-e", "client"],
1230
 
                                      command.Enable)
 
1227
        self.assert_command_from_args(["-e", "foo"], command.Enable)
1231
1228
 
1232
1229
    def test_disable(self):
1233
 
        self.assert_command_from_args(["--disable", "client"],
 
1230
        self.assert_command_from_args(["--disable", "foo"],
1234
1231
                                      command.Disable)
1235
1232
 
1236
1233
    def test_disable_short(self):
1237
 
        self.assert_command_from_args(["-d", "client"],
1238
 
                                      command.Disable)
 
1234
        self.assert_command_from_args(["-d", "foo"], command.Disable)
1239
1235
 
1240
1236
    def test_bump_timeout(self):
1241
 
        self.assert_command_from_args(["--bump-timeout", "client"],
 
1237
        self.assert_command_from_args(["--bump-timeout", "foo"],
1242
1238
                                      command.BumpTimeout)
1243
1239
 
1244
1240
    def test_bump_timeout_short(self):
1245
 
        self.assert_command_from_args(["-b", "client"],
 
1241
        self.assert_command_from_args(["-b", "foo"],
1246
1242
                                      command.BumpTimeout)
1247
1243
 
1248
1244
    def test_start_checker(self):
1249
 
        self.assert_command_from_args(["--start-checker", "client"],
 
1245
        self.assert_command_from_args(["--start-checker", "foo"],
1250
1246
                                      command.StartChecker)
1251
1247
 
1252
1248
    def test_stop_checker(self):
1253
 
        self.assert_command_from_args(["--stop-checker", "client"],
 
1249
        self.assert_command_from_args(["--stop-checker", "foo"],
1254
1250
                                      command.StopChecker)
1255
1251
 
1256
1252
    def test_approve_by_default(self):
1257
 
        self.assert_command_from_args(["--approve-by-default",
1258
 
                                       "client"],
 
1253
        self.assert_command_from_args(["--approve-by-default", "foo"],
1259
1254
                                      command.ApproveByDefault)
1260
1255
 
1261
1256
    def test_deny_by_default(self):
1262
 
        self.assert_command_from_args(["--deny-by-default", "client"],
 
1257
        self.assert_command_from_args(["--deny-by-default", "foo"],
1263
1258
                                      command.DenyByDefault)
1264
1259
 
1265
1260
    def test_checker(self):
1266
 
        self.assert_command_from_args(["--checker", ":", "client"],
 
1261
        self.assert_command_from_args(["--checker", ":", "foo"],
1267
1262
                                      command.SetChecker,
1268
1263
                                      value_to_set=":")
1269
1264
 
1270
1265
    def test_checker_empty(self):
1271
 
        self.assert_command_from_args(["--checker", "", "client"],
 
1266
        self.assert_command_from_args(["--checker", "", "foo"],
1272
1267
                                      command.SetChecker,
1273
1268
                                      value_to_set="")
1274
1269
 
1275
1270
    def test_checker_short(self):
1276
 
        self.assert_command_from_args(["-c", ":", "client"],
 
1271
        self.assert_command_from_args(["-c", ":", "foo"],
1277
1272
                                      command.SetChecker,
1278
1273
                                      value_to_set=":")
1279
1274
 
1280
1275
    def test_host(self):
1281
 
        self.assert_command_from_args(
1282
 
            ["--host", "client.example.org", "client"],
1283
 
            command.SetHost, value_to_set="client.example.org")
 
1276
        self.assert_command_from_args(["--host", "foo.example.org",
 
1277
                                       "foo"], command.SetHost,
 
1278
                                      value_to_set="foo.example.org")
1284
1279
 
1285
1280
    def test_host_short(self):
1286
 
        self.assert_command_from_args(
1287
 
            ["-H", "client.example.org", "client"], command.SetHost,
1288
 
            value_to_set="client.example.org")
 
1281
        self.assert_command_from_args(["-H", "foo.example.org",
 
1282
                                       "foo"], command.SetHost,
 
1283
                                      value_to_set="foo.example.org")
1289
1284
 
1290
1285
    def test_secret_devnull(self):
1291
1286
        self.assert_command_from_args(["--secret", os.path.devnull,
1292
 
                                       "client"], command.SetSecret,
 
1287
                                       "foo"], command.SetSecret,
1293
1288
                                      value_to_set=b"")
1294
1289
 
1295
1290
    def test_secret_tempfile(self):
1298
1293
            f.write(value)
1299
1294
            f.seek(0)
1300
1295
            self.assert_command_from_args(["--secret", f.name,
1301
 
                                           "client"],
1302
 
                                          command.SetSecret,
 
1296
                                           "foo"], command.SetSecret,
1303
1297
                                          value_to_set=value)
1304
1298
 
1305
1299
    def test_secret_devnull_short(self):
1306
 
        self.assert_command_from_args(["-s", os.path.devnull,
1307
 
                                       "client"], command.SetSecret,
 
1300
        self.assert_command_from_args(["-s", os.path.devnull, "foo"],
 
1301
                                      command.SetSecret,
1308
1302
                                      value_to_set=b"")
1309
1303
 
1310
1304
    def test_secret_tempfile_short(self):
1312
1306
            value = b"secret\0xyzzy\nbar"
1313
1307
            f.write(value)
1314
1308
            f.seek(0)
1315
 
            self.assert_command_from_args(["-s", f.name, "client"],
 
1309
            self.assert_command_from_args(["-s", f.name, "foo"],
1316
1310
                                          command.SetSecret,
1317
1311
                                          value_to_set=value)
1318
1312
 
1319
1313
    def test_timeout(self):
1320
 
        self.assert_command_from_args(["--timeout", "PT5M", "client"],
 
1314
        self.assert_command_from_args(["--timeout", "PT5M", "foo"],
1321
1315
                                      command.SetTimeout,
1322
1316
                                      value_to_set=300000)
1323
1317
 
1324
1318
    def test_timeout_short(self):
1325
 
        self.assert_command_from_args(["-t", "PT5M", "client"],
 
1319
        self.assert_command_from_args(["-t", "PT5M", "foo"],
1326
1320
                                      command.SetTimeout,
1327
1321
                                      value_to_set=300000)
1328
1322
 
1329
1323
    def test_extended_timeout(self):
1330
1324
        self.assert_command_from_args(["--extended-timeout", "PT15M",
1331
 
                                       "client"],
 
1325
                                       "foo"],
1332
1326
                                      command.SetExtendedTimeout,
1333
1327
                                      value_to_set=900000)
1334
1328
 
1335
1329
    def test_interval(self):
1336
 
        self.assert_command_from_args(["--interval", "PT2M",
1337
 
                                       "client"], command.SetInterval,
 
1330
        self.assert_command_from_args(["--interval", "PT2M", "foo"],
 
1331
                                      command.SetInterval,
1338
1332
                                      value_to_set=120000)
1339
1333
 
1340
1334
    def test_interval_short(self):
1341
 
        self.assert_command_from_args(["-i", "PT2M", "client"],
 
1335
        self.assert_command_from_args(["-i", "PT2M", "foo"],
1342
1336
                                      command.SetInterval,
1343
1337
                                      value_to_set=120000)
1344
1338
 
1345
1339
    def test_approval_delay(self):
1346
1340
        self.assert_command_from_args(["--approval-delay", "PT30S",
1347
 
                                       "client"],
 
1341
                                       "foo"],
1348
1342
                                      command.SetApprovalDelay,
1349
1343
                                      value_to_set=30000)
1350
1344
 
1351
1345
    def test_approval_duration(self):
1352
1346
        self.assert_command_from_args(["--approval-duration", "PT1S",
1353
 
                                       "client"],
 
1347
                                       "foo"],
1354
1348
                                      command.SetApprovalDuration,
1355
1349
                                      value_to_set=1000)
1356
1350
 
1439
1433
            LastCheckerStatus=-2)
1440
1434
        self.clients =  collections.OrderedDict(
1441
1435
            [
1442
 
                (self.client.__dbus_object_path__,
1443
 
                 self.client.attributes),
1444
 
                (self.other_client.__dbus_object_path__,
1445
 
                 self.other_client.attributes),
 
1436
                ("/clients/foo", self.client.attributes),
 
1437
                ("/clients/barbar", self.other_client.attributes),
1446
1438
            ])
1447
 
        self.one_client = {self.client.__dbus_object_path__:
1448
 
                           self.client.attributes}
 
1439
        self.one_client = {"/clients/foo": self.client.attributes}
1449
1440
 
1450
1441
    @property
1451
1442
    def bus(self):
1452
 
        class MockBus(object):
 
1443
        class Bus(object):
1453
1444
            @staticmethod
1454
1445
            def get_object(client_bus_name, path):
1455
1446
                self.assertEqual(dbus_busname, client_bus_name)
1456
 
                # Note: "self" here is the TestCmd instance, not the
1457
 
                # MockBus instance, since this is a static method!
1458
 
                if path == self.client.__dbus_object_path__:
1459
 
                    return self.client
1460
 
                elif path == self.other_client.__dbus_object_path__:
1461
 
                    return self.other_client
1462
 
        return MockBus()
 
1447
                return {
 
1448
                    # Note: "self" here is the TestCmd instance, not
 
1449
                    # the Bus instance, since this is a static method!
 
1450
                    "/clients/foo": self.client,
 
1451
                    "/clients/barbar": self.other_client,
 
1452
                }[path]
 
1453
        return Bus()
1463
1454
 
1464
1455
 
1465
1456
class TestBaseCommands(TestCommand):
1496
1487
                          client.calls)
1497
1488
 
1498
1489
    def test_Remove(self):
1499
 
        class MandosSpy(object):
 
1490
        class MockMandos(object):
1500
1491
            def __init__(self):
1501
1492
                self.calls = []
1502
1493
            def RemoveClient(self, dbus_path):
1503
1494
                self.calls.append(("RemoveClient", (dbus_path,)))
1504
 
        mandos = MandosSpy()
 
1495
        mandos = MockMandos()
1505
1496
        command.Remove().run(self.clients, self.bus, mandos)
1506
1497
        for clientpath in self.clients:
1507
1498
            self.assertIn(("RemoveClient", (clientpath,)),
1698
1689
        self.assertEqual(expected_output, buffer.getvalue())
1699
1690
 
1700
1691
 
1701
 
class TestPropertySetterCmd(TestCommand):
1702
 
    """Abstract class for tests of command.PropertySetter classes"""
 
1692
class TestPropertyCmd(TestCommand):
 
1693
    """Abstract class for tests of command.Property classes"""
1703
1694
    def runTest(self):
1704
1695
        if not hasattr(self, "command"):
1705
1696
            return
1726
1717
        self.command().run(clients, self.bus)
1727
1718
 
1728
1719
 
1729
 
class TestEnableCmd(TestPropertySetterCmd):
 
1720
class TestEnableCmd(TestPropertyCmd):
1730
1721
    command = command.Enable
1731
1722
    propname = "Enabled"
1732
1723
    values_to_set = [dbus.Boolean(True)]
1733
1724
 
1734
1725
 
1735
 
class TestDisableCmd(TestPropertySetterCmd):
 
1726
class TestDisableCmd(TestPropertyCmd):
1736
1727
    command = command.Disable
1737
1728
    propname = "Enabled"
1738
1729
    values_to_set = [dbus.Boolean(False)]
1739
1730
 
1740
1731
 
1741
 
class TestBumpTimeoutCmd(TestPropertySetterCmd):
 
1732
class TestBumpTimeoutCmd(TestPropertyCmd):
1742
1733
    command = command.BumpTimeout
1743
1734
    propname = "LastCheckedOK"
1744
1735
    values_to_set = [""]
1745
1736
 
1746
1737
 
1747
 
class TestStartCheckerCmd(TestPropertySetterCmd):
 
1738
class TestStartCheckerCmd(TestPropertyCmd):
1748
1739
    command = command.StartChecker
1749
1740
    propname = "CheckerRunning"
1750
1741
    values_to_set = [dbus.Boolean(True)]
1751
1742
 
1752
1743
 
1753
 
class TestStopCheckerCmd(TestPropertySetterCmd):
 
1744
class TestStopCheckerCmd(TestPropertyCmd):
1754
1745
    command = command.StopChecker
1755
1746
    propname = "CheckerRunning"
1756
1747
    values_to_set = [dbus.Boolean(False)]
1757
1748
 
1758
1749
 
1759
 
class TestApproveByDefaultCmd(TestPropertySetterCmd):
 
1750
class TestApproveByDefaultCmd(TestPropertyCmd):
1760
1751
    command = command.ApproveByDefault
1761
1752
    propname = "ApprovedByDefault"
1762
1753
    values_to_set = [dbus.Boolean(True)]
1763
1754
 
1764
1755
 
1765
 
class TestDenyByDefaultCmd(TestPropertySetterCmd):
 
1756
class TestDenyByDefaultCmd(TestPropertyCmd):
1766
1757
    command = command.DenyByDefault
1767
1758
    propname = "ApprovedByDefault"
1768
1759
    values_to_set = [dbus.Boolean(False)]
1769
1760
 
1770
1761
 
1771
 
class TestPropertySetterValueCmd(TestPropertySetterCmd):
1772
 
    """Abstract class for tests of PropertySetterValueCmd classes"""
 
1762
class TestPropertyValueCmd(TestPropertyCmd):
 
1763
    """Abstract class for tests of PropertyValueCmd classes"""
1773
1764
 
1774
1765
    def runTest(self):
1775
 
        if type(self) is TestPropertySetterValueCmd:
 
1766
        if type(self) is TestPropertyValueCmd:
1776
1767
            return
1777
 
        return super(TestPropertySetterValueCmd, self).runTest()
 
1768
        return super(TestPropertyValueCmd, self).runTest()
1778
1769
 
1779
1770
    def run_command(self, value, clients):
1780
1771
        self.command(value).run(clients, self.bus)
1781
1772
 
1782
1773
 
1783
 
class TestSetCheckerCmd(TestPropertySetterValueCmd):
 
1774
class TestSetCheckerCmd(TestPropertyValueCmd):
1784
1775
    command = command.SetChecker
1785
1776
    propname = "Checker"
1786
1777
    values_to_set = ["", ":", "fping -q -- %s"]
1787
1778
 
1788
1779
 
1789
 
class TestSetHostCmd(TestPropertySetterValueCmd):
 
1780
class TestSetHostCmd(TestPropertyValueCmd):
1790
1781
    command = command.SetHost
1791
1782
    propname = "Host"
1792
 
    values_to_set = ["192.0.2.3", "client.example.org"]
1793
 
 
1794
 
 
1795
 
class TestSetSecretCmd(TestPropertySetterValueCmd):
 
1783
    values_to_set = ["192.0.2.3", "foo.example.org"]
 
1784
 
 
1785
 
 
1786
class TestSetSecretCmd(TestPropertyValueCmd):
1796
1787
    command = command.SetSecret
1797
1788
    propname = "Secret"
1798
1789
    values_to_set = [io.BytesIO(b""),
1799
1790
                     io.BytesIO(b"secret\0xyzzy\nbar")]
1800
 
    values_to_get = [f.getvalue() for f in values_to_set]
1801
 
 
1802
 
 
1803
 
class TestSetTimeoutCmd(TestPropertySetterValueCmd):
 
1791
    values_to_get = [b"", b"secret\0xyzzy\nbar"]
 
1792
 
 
1793
 
 
1794
class TestSetTimeoutCmd(TestPropertyValueCmd):
1804
1795
    command = command.SetTimeout
1805
1796
    propname = "Timeout"
1806
1797
    values_to_set = [datetime.timedelta(),
1808
1799
                     datetime.timedelta(seconds=1),
1809
1800
                     datetime.timedelta(weeks=1),
1810
1801
                     datetime.timedelta(weeks=52)]
1811
 
    values_to_get = [dt.total_seconds()*1000 for dt in values_to_set]
1812
 
 
1813
 
 
1814
 
class TestSetExtendedTimeoutCmd(TestPropertySetterValueCmd):
 
1802
    values_to_get = [0, 300000, 1000, 604800000, 31449600000]
 
1803
 
 
1804
 
 
1805
class TestSetExtendedTimeoutCmd(TestPropertyValueCmd):
1815
1806
    command = command.SetExtendedTimeout
1816
1807
    propname = "ExtendedTimeout"
1817
1808
    values_to_set = [datetime.timedelta(),
1819
1810
                     datetime.timedelta(seconds=1),
1820
1811
                     datetime.timedelta(weeks=1),
1821
1812
                     datetime.timedelta(weeks=52)]
1822
 
    values_to_get = [dt.total_seconds()*1000 for dt in values_to_set]
1823
 
 
1824
 
 
1825
 
class TestSetIntervalCmd(TestPropertySetterValueCmd):
 
1813
    values_to_get = [0, 300000, 1000, 604800000, 31449600000]
 
1814
 
 
1815
 
 
1816
class TestSetIntervalCmd(TestPropertyValueCmd):
1826
1817
    command = command.SetInterval
1827
1818
    propname = "Interval"
1828
1819
    values_to_set = [datetime.timedelta(),
1830
1821
                     datetime.timedelta(seconds=1),
1831
1822
                     datetime.timedelta(weeks=1),
1832
1823
                     datetime.timedelta(weeks=52)]
1833
 
    values_to_get = [dt.total_seconds()*1000 for dt in values_to_set]
1834
 
 
1835
 
 
1836
 
class TestSetApprovalDelayCmd(TestPropertySetterValueCmd):
 
1824
    values_to_get = [0, 300000, 1000, 604800000, 31449600000]
 
1825
 
 
1826
 
 
1827
class TestSetApprovalDelayCmd(TestPropertyValueCmd):
1837
1828
    command = command.SetApprovalDelay
1838
1829
    propname = "ApprovalDelay"
1839
1830
    values_to_set = [datetime.timedelta(),
1841
1832
                     datetime.timedelta(seconds=1),
1842
1833
                     datetime.timedelta(weeks=1),
1843
1834
                     datetime.timedelta(weeks=52)]
1844
 
    values_to_get = [dt.total_seconds()*1000 for dt in values_to_set]
1845
 
 
1846
 
 
1847
 
class TestSetApprovalDurationCmd(TestPropertySetterValueCmd):
 
1835
    values_to_get = [0, 300000, 1000, 604800000, 31449600000]
 
1836
 
 
1837
 
 
1838
class TestSetApprovalDurationCmd(TestPropertyValueCmd):
1848
1839
    command = command.SetApprovalDuration
1849
1840
    propname = "ApprovalDuration"
1850
1841
    values_to_set = [datetime.timedelta(),
1852
1843
                     datetime.timedelta(seconds=1),
1853
1844
                     datetime.timedelta(weeks=1),
1854
1845
                     datetime.timedelta(weeks=52)]
1855
 
    values_to_get = [dt.total_seconds()*1000 for dt in values_to_set]
 
1846
    values_to_get = [0, 300000, 1000, 604800000, 31449600000]
1856
1847
 
1857
1848
 
1858
1849