/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-06 17:59:11 UTC
  • Revision ID: teddy@recompile.se-20190306175911-bl5eyej4cb9vp9u8
mandos-ctl: Add more tests, starting with the --verbose option

* mandos-ctl (TestOptions): New.

Show diffs side-by-side

added added

removed removed

Lines of Context:
42
42
import json
43
43
import unittest
44
44
import logging
45
 
import io
46
 
import tempfile
47
45
 
48
46
import dbus
49
47
 
477
475
    property = "Host"
478
476
 
479
477
class SetSecretCmd(PropertyCmd, ValueArgumentMixIn):
480
 
    @property
481
 
    def value_to_set(self):
482
 
        return self._vts
483
 
    @value_to_set.setter
484
 
    def value_to_set(self, value):
485
 
        """When setting, read data from supplied file object"""
486
 
        self._vts = value.read()
487
 
        value.close()
488
478
    property = "Secret"
489
479
 
490
480
class SetTimeoutCmd(PropertyCmd, MillisecondsValueArgumentMixIn):
619
609
        commands.append(RemoveCmd())
620
610
 
621
611
    if options.checker is not None:
622
 
        commands.append(SetCheckerCmd(options.checker))
 
612
        commands.append(SetCheckerCmd())
623
613
 
624
614
    if options.timeout is not None:
625
615
        commands.append(SetTimeoutCmd(options.timeout))
629
619
            SetExtendedTimeoutCmd(options.extended_timeout))
630
620
 
631
621
    if options.interval is not None:
632
 
        commands.append(SetIntervalCmd(options.interval))
 
622
        command.append(SetIntervalCmd(options.interval))
633
623
 
634
624
    if options.approved_by_default is not None:
635
625
        if options.approved_by_default:
636
 
            commands.append(ApproveByDefaultCmd())
 
626
            command.append(ApproveByDefaultCmd())
637
627
        else:
638
 
            commands.append(DenyByDefaultCmd())
 
628
            command.append(DenyByDefaultCmd())
639
629
 
640
630
    if options.approval_delay is not None:
641
 
        commands.append(SetApprovalDelayCmd(options.approval_delay))
 
631
        command.append(SetApprovalDelayCmd(options.approval_delay))
642
632
 
643
633
    if options.approval_duration is not None:
644
 
        commands.append(
 
634
        command.append(
645
635
            SetApprovalDurationCmd(options.approval_duration))
646
636
 
647
637
    if options.host is not None:
648
 
        commands.append(SetHostCmd(options.host))
 
638
        command.append(SetHostCmd(options.host))
649
639
 
650
640
    if options.secret is not None:
651
 
        commands.append(SetSecretCmd(options.secret))
 
641
        command.append(SetSecretCmd(options.secret))
652
642
 
653
643
    if options.approve:
654
644
        commands.append(ApproveCmd())
1093
1083
class TestSetSecretCmd(TestValueArgumentPropertyCmd):
1094
1084
    command = SetSecretCmd
1095
1085
    property = "Secret"
1096
 
    values_to_set = [open("/dev/null", "rb"),
1097
 
                     io.BytesIO(b"secret\0xyzzy\nbar")]
1098
 
    values_to_get = [b"", b"secret\0xyzzy\nbar"]
 
1086
    values_to_set = [b"", b"secret"]
1099
1087
 
1100
1088
class TestSetTimeoutCmd(TestValueArgumentPropertyCmd):
1101
1089
    command = SetTimeoutCmd
1127
1115
    values_to_set = ["P0D", "PT5M", "PT1S", "PT120S", "P1Y"]
1128
1116
    values_to_get = [0, 300000, 1000, 120000, 31449600000]
1129
1117
 
1130
 
class Test_command_from_options(unittest.TestCase):
 
1118
class TestOptions(unittest.TestCase):
1131
1119
    def setUp(self):
1132
1120
        self.parser = argparse.ArgumentParser()
1133
1121
        add_command_line_options(self.parser)
1134
 
    def assert_command_from_args(self, args, command_cls, **cmd_attrs):
1135
 
        """Assert that parsing ARGS should result in an instance of
1136
 
COMMAND_CLS with (optionally) all supplied attributes (CMD_ATTRS)."""
 
1122
    def commands_from_args(self, args):
1137
1123
        options = self.parser.parse_args(args)
1138
 
        commands = commands_from_options(options)
1139
 
        self.assertEqual(len(commands), 1)
1140
 
        command = commands[0]
1141
 
        self.assertIsInstance(command, command_cls)
1142
 
        for key, value in cmd_attrs.items():
1143
 
            self.assertEqual(getattr(command, key), value)
1144
 
    def test_print_table(self):
1145
 
        self.assert_command_from_args([], PrintTableCmd,
1146
 
                                      verbose=False)
1147
 
 
1148
 
    def test_print_table_verbose(self):
1149
 
        self.assert_command_from_args(["--verbose"], PrintTableCmd,
1150
 
                                      verbose=True)
1151
 
 
1152
 
    def test_enable(self):
1153
 
        self.assert_command_from_args(["--enable", "foo"], EnableCmd)
1154
 
 
1155
 
    def test_disable(self):
1156
 
        self.assert_command_from_args(["--disable", "foo"],
1157
 
                                      DisableCmd)
1158
 
 
1159
 
    def test_bump_timeout(self):
1160
 
        self.assert_command_from_args(["--bump-timeout", "foo"],
1161
 
                                      BumpTimeoutCmd)
1162
 
 
1163
 
    def test_start_checker(self):
1164
 
        self.assert_command_from_args(["--start-checker", "foo"],
1165
 
                                      StartCheckerCmd)
1166
 
 
1167
 
    def test_stop_checker(self):
1168
 
        self.assert_command_from_args(["--stop-checker", "foo"],
1169
 
                                      StopCheckerCmd)
1170
 
 
1171
 
    def test_remove(self):
1172
 
        self.assert_command_from_args(["--remove", "foo"],
1173
 
                                      RemoveCmd)
1174
 
 
1175
 
    def test_checker(self):
1176
 
        self.assert_command_from_args(["--checker", ":", "foo"],
1177
 
                                      SetCheckerCmd, value_to_set=":")
1178
 
 
1179
 
    def test_timeout(self):
1180
 
        self.assert_command_from_args(["--timeout", "PT5M", "foo"],
1181
 
                                      SetTimeoutCmd,
1182
 
                                      value_to_set=300000)
1183
 
 
1184
 
    def test_extended_timeout(self):
1185
 
        self.assert_command_from_args(["--extended-timeout", "PT15M",
1186
 
                                       "foo"],
1187
 
                                      SetExtendedTimeoutCmd,
1188
 
                                      value_to_set=900000)
1189
 
 
1190
 
    def test_interval(self):
1191
 
        self.assert_command_from_args(["--interval", "PT2M", "foo"],
1192
 
                                      SetIntervalCmd,
1193
 
                                      value_to_set=120000)
1194
 
 
1195
 
    def test_approve_by_default(self):
1196
 
        self.assert_command_from_args(["--approve-by-default", "foo"],
1197
 
                                      ApproveByDefaultCmd)
1198
 
 
1199
 
    def test_deny_by_default(self):
1200
 
        self.assert_command_from_args(["--deny-by-default", "foo"],
1201
 
                                      DenyByDefaultCmd)
1202
 
 
1203
 
    def test_approval_delay(self):
1204
 
        self.assert_command_from_args(["--approval-delay", "PT30S",
1205
 
                                       "foo"], SetApprovalDelayCmd,
1206
 
                                      value_to_set=30000)
1207
 
 
1208
 
    def test_approval_duration(self):
1209
 
        self.assert_command_from_args(["--approval-duration", "PT1S",
1210
 
                                       "foo"], SetApprovalDurationCmd,
1211
 
                                      value_to_set=1000)
1212
 
 
1213
 
    def test_host(self):
1214
 
        self.assert_command_from_args(["--host", "foo.example.org",
1215
 
                                       "foo"], SetHostCmd,
1216
 
                                      value_to_set="foo.example.org")
1217
 
 
1218
 
    def test_secret_devnull(self):
1219
 
        self.assert_command_from_args(["--secret", os.path.devnull,
1220
 
                                       "foo"], SetSecretCmd,
1221
 
                                      value_to_set=b"")
1222
 
 
1223
 
    def test_secret_tempfile(self):
1224
 
        with tempfile.NamedTemporaryFile(mode="r+b") as f:
1225
 
            value = b"secret\0xyzzy\nbar"
1226
 
            f.write(value)
1227
 
            f.seek(0)
1228
 
            self.assert_command_from_args(["--secret", f.name,
1229
 
                                           "foo"], SetSecretCmd,
1230
 
                                          value_to_set=value)
1231
 
 
1232
 
    def test_approve(self):
1233
 
        self.assert_command_from_args(["--approve", "foo"],
1234
 
                                      ApproveCmd)
1235
 
 
1236
 
    def test_deny(self):
1237
 
        self.assert_command_from_args(["--deny", "foo"], DenyCmd)
1238
 
 
1239
 
    def test_dump_json(self):
1240
 
        self.assert_command_from_args(["--dump-json"], DumpJSONCmd)
1241
 
 
1242
 
    def test_is_enabled(self):
1243
 
        self.assert_command_from_args(["--is-enabled", "foo"],
1244
 
                                      IsEnabledCmd)
 
1124
        return commands_from_options(options)
 
1125
    def test_default_is_show_table(self):
 
1126
        commands = self.commands_from_args([])
 
1127
        self.assertEqual(len(commands), 1)
 
1128
        command = commands[0]
 
1129
        self.assertIsInstance(command, PrintTableCmd)
 
1130
        self.assertEqual(command.verbose, False)
 
1131
    def test_show_table_verbose(self):
 
1132
        commands = self.commands_from_args(["--verbose"])
 
1133
        self.assertEqual(len(commands), 1)
 
1134
        command = commands[0]
 
1135
        self.assertIsInstance(command, PrintTableCmd)
 
1136
        self.assertEqual(command.verbose, True)
1245
1137
 
1246
1138
 
1247
1139