/mandos/release

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

« back to all changes in this revision

Viewing changes to mandos-ctl

  • Committer: Teddy Hogeborn
  • Date: 2019-03-23 14:59:54 UTC
  • mto: This revision was merged to the branch mainline in revision 382.
  • Revision ID: teddy@recompile.se-20190323145954-latqk2bg6xt989d5
mandos: Refactor; use classes, not instances, as namespaces

* mandos (Avahi): Rename to "avahi".
  (avahi.string_array_to_txt_array): Make static method.
  (GnuTLS): Rename to "gnutls".
  (gnutls.__init__): Remove; move version check up to class body.
  (gnutls.Error): Don't use class name anymore.

Show diffs side-by-side

added added

removed removed

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