94
94
log.setLevel(logging.DEBUG)
96
bus = dbus.SystemBus()
98
mandos_dbus_object = get_mandos_dbus_object(bus)
100
mandos_serv = dbus.Interface(
101
mandos_dbus_object, dbus_interface=server_dbus_interface)
97
bus = dbus.SystemBus()
98
log.debug("D-Bus: Connect to: (busname=%r, path=%r)",
99
dbus_busname, server_dbus_path)
100
mandos_dbus_objc = bus.get_object(dbus_busname,
102
except dbus.exceptions.DBusException:
103
log.critical("Could not connect to Mandos server")
106
mandos_serv = dbus.Interface(mandos_dbus_objc,
107
dbus_interface=server_dbus_interface)
102
108
mandos_serv_object_manager = dbus.Interface(
103
mandos_dbus_object, dbus_interface=dbus.OBJECT_MANAGER_IFACE)
105
managed_objects = get_managed_objects(mandos_serv_object_manager)
108
for path, ifs_and_props in managed_objects.items():
110
all_clients[path] = ifs_and_props[client_dbus_interface]
114
# Compile dict of (clientpath: properties) to process
109
mandos_dbus_objc, dbus_interface=dbus.OBJECT_MANAGER_IFACE)
111
# Filter out log message from dbus module
112
dbus_logger = logging.getLogger("dbus.proxies")
113
class NullFilter(logging.Filter):
114
def filter(self, record):
116
dbus_filter = NullFilter()
118
dbus_logger.addFilter(dbus_filter)
119
log.debug("D-Bus: %s:%s:%s.GetManagedObjects()", dbus_busname,
120
server_dbus_path, dbus.OBJECT_MANAGER_IFACE)
121
mandos_clients = {path: ifs_and_props[client_dbus_interface]
122
for path, ifs_and_props in
123
mandos_serv_object_manager
124
.GetManagedObjects().items()
125
if client_dbus_interface in ifs_and_props}
126
except dbus.exceptions.DBusException as e:
127
log.critical("Failed to access Mandos server through D-Bus:"
131
# restore dbus logger
132
dbus_logger.removeFilter(dbus_filter)
134
# Compile dict of (clients: properties) to process
115
137
if not clientnames:
116
clients = all_clients
138
clients = {objpath: properties
139
for objpath, properties in mandos_clients.items()}
119
141
for name in clientnames:
120
for objpath, properties in all_clients.items():
142
for objpath, properties in mandos_clients.items():
121
143
if properties["Name"] == name:
122
144
clients[objpath] = properties
424
446
options.remove = True
427
def get_mandos_dbus_object(bus):
428
log.debug("D-Bus: Connect to: (busname=%r, path=%r)",
429
dbus_busname, server_dbus_path)
430
with if_dbus_exception_log_with_exception_and_exit(
431
"Could not connect to Mandos server: %s"):
432
mandos_dbus_object = bus.get_object(dbus_busname,
434
return mandos_dbus_object
437
@contextlib.contextmanager
438
def if_dbus_exception_log_with_exception_and_exit(*args, **kwargs):
441
except dbus.exceptions.DBusException as e:
442
log.critical(*(args + (e,)), **kwargs)
446
def get_managed_objects(object_manager):
447
log.debug("D-Bus: %s:%s:%s.GetManagedObjects()", dbus_busname,
448
server_dbus_path, dbus.OBJECT_MANAGER_IFACE)
449
with if_dbus_exception_log_with_exception_and_exit(
450
"Failed to access Mandos server through D-Bus:\n%s"):
451
with SilenceLogger("dbus.proxies"):
452
managed_objects = object_manager.GetManagedObjects()
453
return managed_objects
456
class SilenceLogger(object):
457
"Simple context manager to silence a particular logger"
458
def __init__(self, loggername):
459
self.logger = logging.getLogger(loggername)
462
self.logger.addFilter(self.nullfilter)
465
class NullFilter(logging.Filter):
466
def filter(self, record):
469
nullfilter = NullFilter()
471
def __exit__(self, exc_type, exc_val, exc_tb):
472
self.logger.removeFilter(self.nullfilter)
475
449
def commands_from_options(options):
594
568
self.mandos.RemoveClient(client.__dbus_object_path__)
597
class OutputCmd(Command):
598
"""Abstract class for commands outputting client details"""
571
class PrintCmd(Command):
572
"""Abstract class for commands printing client details"""
599
573
all_keywords = ("Name", "Enabled", "Timeout", "LastCheckedOK",
600
574
"Created", "Interval", "Host", "KeyID",
601
575
"Fingerprint", "CheckerRunning", "LastEnabled",
603
577
"LastApprovalRequest", "ApprovalDelay",
604
578
"ApprovalDuration", "Checker", "ExtendedTimeout",
605
579
"Expires", "LastCheckerStatus")
607
580
def run(self, clients, bus=None, mandos=None):
608
581
print(self.output(clients.values()))
610
582
def output(self, clients):
611
583
raise NotImplementedError()
614
class DumpJSONCmd(OutputCmd):
586
class DumpJSONCmd(PrintCmd):
615
587
def output(self, clients):
616
588
data = {client["Name"]:
617
589
{key: self.dbus_boolean_to_bool(client[key])
618
590
for key in self.all_keywords}
619
for client in clients}
591
for client in clients.values()}
620
592
return json.dumps(data, indent=4, separators=(',', ': '))
623
594
def dbus_boolean_to_bool(value):
624
595
if isinstance(value, dbus.Boolean):
693
666
def string_from_client(self, client, key):
694
667
return self.valuetostring(client[key], key)
697
def valuetostring(cls, value, keyword):
670
def valuetostring(value, keyword):
698
671
if isinstance(value, dbus.Boolean):
699
672
return "Yes" if value else "No"
700
673
if keyword in ("Timeout", "Interval", "ApprovalDelay",
701
674
"ApprovalDuration", "ExtendedTimeout"):
702
return cls.milliseconds_to_string(value)
675
return milliseconds_to_string(value)
703
676
return str(value)
705
678
def header_line(self, format_string):
710
683
**{key: self.string_from_client(client, key)
711
684
for key in self.keywords})
714
def milliseconds_to_string(ms):
715
td = datetime.timedelta(0, 0, 0, ms)
716
return ("{days}{hours:02}:{minutes:02}:{seconds:02}"
717
.format(days="{}T".format(td.days)
719
hours=td.seconds // 3600,
720
minutes=(td.seconds % 3600) // 60,
721
seconds=td.seconds % 60))
687
def milliseconds_to_string(ms):
688
td = datetime.timedelta(0, 0, 0, ms)
689
return ("{days}{hours:02}:{minutes:02}:{seconds:02}"
690
.format(days="{}T".format(td.days) if td.days else "",
691
hours=td.seconds // 3600,
692
minutes=(td.seconds % 3600) // 60,
693
seconds=td.seconds % 60))
724
696
class PropertyCmd(Command):
725
697
"""Abstract class for Actions for setting one client property"""
727
698
def run_on_one_client(self, client, properties):
728
699
"""Set the Client's D-Bus property"""
729
700
log.debug("D-Bus: %s:%s:%s.Set(%r, %r, %r)", dbus_busname,
842
class TestCaseWithAssertLogs(unittest.TestCase):
843
"""unittest.TestCase.assertLogs only exists in Python 3.4"""
845
if not hasattr(unittest.TestCase, "assertLogs"):
846
@contextlib.contextmanager
847
def assertLogs(self, logger, level=logging.INFO):
848
capturing_handler = self.CapturingLevelHandler(level)
849
old_level = logger.level
850
old_propagate = logger.propagate
851
logger.addHandler(capturing_handler)
852
logger.setLevel(level)
853
logger.propagate = False
855
yield capturing_handler.watcher
857
logger.propagate = old_propagate
858
logger.removeHandler(capturing_handler)
859
logger.setLevel(old_level)
860
self.assertGreater(len(capturing_handler.watcher.records),
863
class CapturingLevelHandler(logging.Handler):
864
def __init__(self, level, *args, **kwargs):
865
logging.Handler.__init__(self, *args, **kwargs)
866
self.watcher = self.LoggingWatcher([], [])
867
def emit(self, record):
868
self.watcher.records.append(record)
869
self.watcher.output.append(self.format(record))
871
LoggingWatcher = collections.namedtuple("LoggingWatcher",
875
class Test_string_to_delta(TestCaseWithAssertLogs):
808
class Test_string_to_delta(unittest.TestCase):
876
809
def test_handles_basic_rfc3339(self):
877
810
self.assertEqual(string_to_delta("PT0S"),
878
811
datetime.timedelta())
882
815
datetime.timedelta(0, 1))
883
816
self.assertEqual(string_to_delta("PT2H"),
884
817
datetime.timedelta(0, 7200))
886
818
def test_falls_back_to_pre_1_6_1_with_warning(self):
887
with self.assertLogs(log, logging.WARNING):
888
value = string_to_delta("2h")
819
# assertLogs only exists in Python 3.4
820
if hasattr(self, "assertLogs"):
821
with self.assertLogs(log, logging.WARNING):
822
value = string_to_delta("2h")
824
class WarningFilter(logging.Filter):
825
"""Don't show, but record the presence of, warnings"""
826
def filter(self, record):
827
is_warning = record.levelno >= logging.WARNING
828
self.found = is_warning or getattr(self, "found",
830
return not is_warning
831
warning_filter = WarningFilter()
832
log.addFilter(warning_filter)
834
value = string_to_delta("2h")
836
log.removeFilter(warning_filter)
837
self.assertTrue(getattr(warning_filter, "found", False))
889
838
self.assertEqual(value, datetime.timedelta(0, 7200))
892
841
class Test_check_option_syntax(unittest.TestCase):
894
self.parser = argparse.ArgumentParser()
895
add_command_line_options(self.parser)
897
def test_actions_requires_client_or_all(self):
898
for action, value in self.actions.items():
899
options = self.parser.parse_args()
900
setattr(options, action, value)
901
with self.assertParseError():
902
self.check_option_syntax(options)
904
842
# This mostly corresponds to the definition from has_actions() in
905
843
# check_option_syntax()
954
896
def check_option_syntax(self, options):
955
897
check_option_syntax(self.parser, options)
957
def test_actions_all_conflicts_with_verbose(self):
899
def test_actions_requires_client_or_all(self):
958
900
for action, value in self.actions.items():
959
901
options = self.parser.parse_args()
960
902
setattr(options, action, value)
962
options.verbose = True
963
903
with self.assertParseError():
964
904
self.check_option_syntax(options)
966
def test_actions_with_client_conflicts_with_verbose(self):
906
def test_actions_conflicts_with_verbose(self):
967
907
for action, value in self.actions.items():
968
908
options = self.parser.parse_args()
969
909
setattr(options, action, value)
970
910
options.verbose = True
971
options.client = ["foo"]
972
911
with self.assertParseError():
973
912
self.check_option_syntax(options)
1000
939
options.all = True
1001
940
self.check_option_syntax(options)
1003
def test_any_action_is_ok_with_one_client(self):
1004
for action, value in self.actions.items():
1005
options = self.parser.parse_args()
1006
setattr(options, action, value)
1007
options.client = ["foo"]
1008
self.check_option_syntax(options)
1010
def test_actions_except_is_enabled_are_ok_with_two_clients(self):
1011
for action, value in self.actions.items():
1012
if action == "is_enabled":
1014
options = self.parser.parse_args()
1015
setattr(options, action, value)
1016
options.client = ["foo", "barbar"]
1017
self.check_option_syntax(options)
1019
942
def test_is_enabled_fails_without_client(self):
1020
943
options = self.parser.parse_args()
1021
944
options.is_enabled = True
1022
945
with self.assertParseError():
1023
946
self.check_option_syntax(options)
948
def test_is_enabled_works_with_one_client(self):
949
options = self.parser.parse_args()
950
options.is_enabled = True
951
options.client = ["foo"]
952
self.check_option_syntax(options)
1025
954
def test_is_enabled_fails_with_two_clients(self):
1026
955
options = self.parser.parse_args()
1027
956
options.is_enabled = True
1041
970
self.check_option_syntax(options)
1044
class Test_get_mandos_dbus_object(TestCaseWithAssertLogs):
1045
def test_calls_and_returns_get_object_on_bus(self):
1046
class MockBus(object):
1048
def get_object(mockbus_self, busname, dbus_path):
1049
# Note that "self" is still the testcase instance,
1050
# this MockBus instance is in "mockbus_self".
1051
self.assertEqual(busname, dbus_busname)
1052
self.assertEqual(dbus_path, server_dbus_path)
1053
mockbus_self.called = True
1056
mockbus = get_mandos_dbus_object(bus=MockBus())
1057
self.assertIsInstance(mockbus, MockBus)
1058
self.assertTrue(mockbus.called)
1060
def test_logs_and_exits_on_dbus_error(self):
1061
class MockBusFailing(object):
1062
def get_object(self, busname, dbus_path):
1063
raise dbus.exceptions.DBusException("Test")
1065
with self.assertLogs(log, logging.CRITICAL):
1066
with self.assertRaises(SystemExit) as e:
1067
bus = get_mandos_dbus_object(bus=MockBusFailing())
1069
if isinstance(e.exception.code, int):
1070
self.assertNotEqual(e.exception.code, 0)
1072
self.assertIsNotNone(e.exception.code)
1075
class Test_get_managed_objects(TestCaseWithAssertLogs):
1076
def test_calls_and_returns_GetManagedObjects(self):
1077
managed_objects = {"/clients/foo": { "Name": "foo"}}
1078
class MockObjectManager(object):
1079
def GetManagedObjects(self):
1080
return managed_objects
1081
retval = get_managed_objects(MockObjectManager())
1082
self.assertDictEqual(managed_objects, retval)
1084
def test_logs_and_exits_on_dbus_error(self):
1085
dbus_logger = logging.getLogger("dbus.proxies")
1087
class MockObjectManagerFailing(object):
1088
def GetManagedObjects(self):
1089
dbus_logger.error("Test")
1090
raise dbus.exceptions.DBusException("Test")
1092
class CountingHandler(logging.Handler):
1094
def emit(self, record):
1097
counting_handler = CountingHandler()
1099
dbus_logger.addHandler(counting_handler)
1102
with self.assertLogs(log, logging.CRITICAL) as watcher:
1103
with self.assertRaises(SystemExit) as e:
1104
get_managed_objects(MockObjectManagerFailing())
1106
dbus_logger.removeFilter(counting_handler)
1108
# Make sure the dbus logger was suppressed
1109
self.assertEqual(counting_handler.count, 0)
1111
# Test that the dbus_logger still works
1112
with self.assertLogs(dbus_logger, logging.ERROR):
1113
dbus_logger.error("Test")
1115
if isinstance(e.exception.code, int):
1116
self.assertNotEqual(e.exception.code, 0)
1118
self.assertIsNotNone(e.exception.code)
1121
class Test_commands_from_options(unittest.TestCase):
973
class Test_command_from_options(unittest.TestCase):
1122
974
def setUp(self):
1123
975
self.parser = argparse.ArgumentParser()
1124
976
add_command_line_options(self.parser)
1126
def test_is_enabled(self):
1127
self.assert_command_from_args(["--is-enabled", "foo"],
1130
977
def assert_command_from_args(self, args, command_cls,
1132
979
"""Assert that parsing ARGS should result in an instance of
1139
986
self.assertIsInstance(command, command_cls)
1140
987
for key, value in cmd_attrs.items():
1141
988
self.assertEqual(getattr(command, key), value)
1143
def test_is_enabled_short(self):
1144
self.assert_command_from_args(["-V", "foo"], IsEnabledCmd)
1146
def test_approve(self):
1147
self.assert_command_from_args(["--approve", "foo"],
1150
def test_approve_short(self):
1151
self.assert_command_from_args(["-A", "foo"], ApproveCmd)
1153
def test_deny(self):
1154
self.assert_command_from_args(["--deny", "foo"], DenyCmd)
1156
def test_deny_short(self):
1157
self.assert_command_from_args(["-D", "foo"], DenyCmd)
1159
def test_remove(self):
1160
self.assert_command_from_args(["--remove", "foo"],
1163
def test_deny_before_remove(self):
1164
options = self.parser.parse_args(["--deny", "--remove",
1166
check_option_syntax(self.parser, options)
1167
commands = commands_from_options(options)
1168
self.assertEqual(len(commands), 2)
1169
self.assertIsInstance(commands[0], DenyCmd)
1170
self.assertIsInstance(commands[1], RemoveCmd)
1172
def test_deny_before_remove_reversed(self):
1173
options = self.parser.parse_args(["--remove", "--deny",
1175
check_option_syntax(self.parser, options)
1176
commands = commands_from_options(options)
1177
self.assertEqual(len(commands), 2)
1178
self.assertIsInstance(commands[0], DenyCmd)
1179
self.assertIsInstance(commands[1], RemoveCmd)
1181
def test_remove_short(self):
1182
self.assert_command_from_args(["-r", "foo"], RemoveCmd)
1184
def test_dump_json(self):
1185
self.assert_command_from_args(["--dump-json"], DumpJSONCmd)
989
def test_print_table(self):
990
self.assert_command_from_args([], PrintTableCmd,
993
def test_print_table_verbose(self):
994
self.assert_command_from_args(["--verbose"], PrintTableCmd,
997
def test_print_table_verbose_short(self):
998
self.assert_command_from_args(["-v"], PrintTableCmd,
1187
1001
def test_enable(self):
1188
1002
self.assert_command_from_args(["--enable", "foo"], EnableCmd)
1212
1026
self.assert_command_from_args(["--stop-checker", "foo"],
1213
1027
StopCheckerCmd)
1215
def test_approve_by_default(self):
1216
self.assert_command_from_args(["--approve-by-default", "foo"],
1217
ApproveByDefaultCmd)
1029
def test_remove(self):
1030
self.assert_command_from_args(["--remove", "foo"],
1219
def test_deny_by_default(self):
1220
self.assert_command_from_args(["--deny-by-default", "foo"],
1033
def test_remove_short(self):
1034
self.assert_command_from_args(["-r", "foo"], RemoveCmd)
1223
1036
def test_checker(self):
1224
1037
self.assert_command_from_args(["--checker", ":", "foo"],
1232
1045
self.assert_command_from_args(["-c", ":", "foo"],
1233
1046
SetCheckerCmd, value_to_set=":")
1235
def test_host(self):
1236
self.assert_command_from_args(["--host", "foo.example.org",
1238
value_to_set="foo.example.org")
1240
def test_host_short(self):
1241
self.assert_command_from_args(["-H", "foo.example.org",
1243
value_to_set="foo.example.org")
1245
def test_secret_devnull(self):
1246
self.assert_command_from_args(["--secret", os.path.devnull,
1247
"foo"], SetSecretCmd,
1250
def test_secret_tempfile(self):
1251
with tempfile.NamedTemporaryFile(mode="r+b") as f:
1252
value = b"secret\0xyzzy\nbar"
1255
self.assert_command_from_args(["--secret", f.name,
1256
"foo"], SetSecretCmd,
1259
def test_secret_devnull_short(self):
1260
self.assert_command_from_args(["-s", os.path.devnull, "foo"],
1261
SetSecretCmd, value_to_set=b"")
1263
def test_secret_tempfile_short(self):
1264
with tempfile.NamedTemporaryFile(mode="r+b") as f:
1265
value = b"secret\0xyzzy\nbar"
1268
self.assert_command_from_args(["-s", f.name, "foo"],
1272
1048
def test_timeout(self):
1273
1049
self.assert_command_from_args(["--timeout", "PT5M", "foo"],
1305
1089
"foo"], SetApprovalDurationCmd,
1306
1090
value_to_set=1000)
1308
def test_print_table(self):
1309
self.assert_command_from_args([], PrintTableCmd,
1312
def test_print_table_verbose(self):
1313
self.assert_command_from_args(["--verbose"], PrintTableCmd,
1316
def test_print_table_verbose_short(self):
1317
self.assert_command_from_args(["-v"], PrintTableCmd,
1092
def test_host(self):
1093
self.assert_command_from_args(["--host", "foo.example.org",
1095
value_to_set="foo.example.org")
1097
def test_host_short(self):
1098
self.assert_command_from_args(["-H", "foo.example.org",
1100
value_to_set="foo.example.org")
1102
def test_secret_devnull(self):
1103
self.assert_command_from_args(["--secret", os.path.devnull,
1104
"foo"], SetSecretCmd,
1107
def test_secret_tempfile(self):
1108
with tempfile.NamedTemporaryFile(mode="r+b") as f:
1109
value = b"secret\0xyzzy\nbar"
1112
self.assert_command_from_args(["--secret", f.name,
1113
"foo"], SetSecretCmd,
1116
def test_secret_devnull_short(self):
1117
self.assert_command_from_args(["-s", os.path.devnull, "foo"],
1118
SetSecretCmd, value_to_set=b"")
1120
def test_secret_tempfile_short(self):
1121
with tempfile.NamedTemporaryFile(mode="r+b") as f:
1122
value = b"secret\0xyzzy\nbar"
1125
self.assert_command_from_args(["-s", f.name, "foo"],
1129
def test_approve(self):
1130
self.assert_command_from_args(["--approve", "foo"],
1133
def test_approve_short(self):
1134
self.assert_command_from_args(["-A", "foo"], ApproveCmd)
1136
def test_deny(self):
1137
self.assert_command_from_args(["--deny", "foo"], DenyCmd)
1139
def test_deny_short(self):
1140
self.assert_command_from_args(["-D", "foo"], DenyCmd)
1142
def test_dump_json(self):
1143
self.assert_command_from_args(["--dump-json"], DumpJSONCmd)
1145
def test_is_enabled(self):
1146
self.assert_command_from_args(["--is-enabled", "foo"],
1149
def test_is_enabled_short(self):
1150
self.assert_command_from_args(["-V", "foo"], IsEnabledCmd)
1152
def test_deny_before_remove(self):
1153
options = self.parser.parse_args(["--deny", "--remove",
1155
check_option_syntax(self.parser, options)
1156
commands = commands_from_options(options)
1157
self.assertEqual(len(commands), 2)
1158
self.assertIsInstance(commands[0], DenyCmd)
1159
self.assertIsInstance(commands[1], RemoveCmd)
1161
def test_deny_before_remove_reversed(self):
1162
options = self.parser.parse_args(["--remove", "--deny",
1164
check_option_syntax(self.parser, options)
1165
commands = commands_from_options(options)
1166
self.assertEqual(len(commands), 2)
1167
self.assertIsInstance(commands[0], DenyCmd)
1168
self.assertIsInstance(commands[1], RemoveCmd)
1321
1171
class TestCmd(unittest.TestCase):
1322
1172
"""Abstract class for tests of command classes"""
1324
1173
def setUp(self):
1325
1174
testcase = self
1326
1175
class MockClient(object):
1528
1371
return super(TestDumpJSONCmd, self).setUp()
1530
1372
def test_normal(self):
1531
output = DumpJSONCmd().output(self.clients.values())
1532
json_data = json.loads(output)
1373
json_data = json.loads(DumpJSONCmd().output(self.clients))
1533
1374
self.assertDictEqual(json_data, self.expected_json)
1535
1375
def test_one_client(self):
1536
output = DumpJSONCmd().output(self.one_client.values())
1537
json_data = json.loads(output)
1376
clients = self.one_client
1377
json_data = json.loads(DumpJSONCmd().output(clients))
1538
1378
expected_json = {"foo": self.expected_json["foo"]}
1539
1379
self.assertDictEqual(json_data, expected_json)
1643
1482
for rows in columns)
1644
1483
for line in range(num_lines))
1645
1484
self.assertEqual(output, expected_output)
1647
1485
def test_one_client(self):
1648
1486
output = PrintTableCmd().output(self.one_client.values())
1649
expected_output = "\n".join((
1650
"Name Enabled Timeout Last Successful Check",
1651
"foo Yes 00:05:00 2019-02-03T00:00:00 ",
1487
expected_output = """
1488
Name Enabled Timeout Last Successful Check
1489
foo Yes 00:05:00 2019-02-03T00:00:00
1653
1491
self.assertEqual(output, expected_output)
1494
class Test_milliseconds_to_string(unittest.TestCase):
1496
self.assertEqual(milliseconds_to_string(93785000),
1498
def test_no_days(self):
1499
self.assertEqual(milliseconds_to_string(7385000), "02:03:05")
1500
def test_all_zero(self):
1501
self.assertEqual(milliseconds_to_string(0), "00:00:00")
1502
def test_no_fractional_seconds(self):
1503
self.assertEqual(milliseconds_to_string(400), "00:00:00")
1504
self.assertEqual(milliseconds_to_string(900), "00:00:00")
1505
self.assertEqual(milliseconds_to_string(1900), "00:00:01")
1508
class Unique(object):
1509
"""Class for objects which exist only to be unique objects, since
1510
unittest.mock.sentinel only exists in Python 3.3"""
1656
1513
class TestPropertyCmd(TestCmd):
1657
1514
"""Abstract class for tests of PropertyCmd classes"""
1658
1515
def runTest(self):
1665
1522
for clientpath in self.clients:
1666
1523
client = self.bus.get_object(dbus_busname, clientpath)
1667
1524
old_value = client.attributes[self.propname]
1668
self.assertNotIsInstance(old_value, self.Unique)
1669
client.attributes[self.propname] = self.Unique()
1525
self.assertNotIsInstance(old_value, Unique)
1526
client.attributes[self.propname] = Unique()
1670
1527
self.run_command(value_to_set, self.clients)
1671
1528
for clientpath in self.clients:
1672
1529
client = self.bus.get_object(dbus_busname, clientpath)
1673
1530
value = client.attributes[self.propname]
1674
self.assertNotIsInstance(value, self.Unique)
1531
self.assertNotIsInstance(value, Unique)
1675
1532
self.assertEqual(value, value_to_get)
1677
class Unique(object):
1678
"""Class for objects which exist only to be unique objects,
1679
since unittest.mock.sentinel only exists in Python 3.3"""
1681
1533
def run_command(self, value, clients):
1682
1534
self.command().run(clients, self.bus)
1685
class TestEnableCmd(TestPropertyCmd):
1687
propname = "Enabled"
1688
values_to_set = [dbus.Boolean(True)]
1691
class TestDisableCmd(TestPropertyCmd):
1692
command = DisableCmd
1693
propname = "Enabled"
1694
values_to_set = [dbus.Boolean(False)]
1537
class TestEnableCmd(TestCmd):
1538
def test_enable(self):
1539
for clientpath in self.clients:
1540
client = self.bus.get_object(dbus_busname, clientpath)
1541
client.attributes["Enabled"] = False
1543
EnableCmd().run(self.clients, self.bus)
1545
for clientpath in self.clients:
1546
client = self.bus.get_object(dbus_busname, clientpath)
1547
self.assertTrue(client.attributes["Enabled"])
1550
class TestDisableCmd(TestCmd):
1551
def test_disable(self):
1552
DisableCmd().run(self.clients, self.bus)
1553
for clientpath in self.clients:
1554
client = self.bus.get_object(dbus_busname, clientpath)
1555
self.assertFalse(client.attributes["Enabled"])
1697
1558
class TestBumpTimeoutCmd(TestPropertyCmd):