/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 22:56:24 UTC
  • Revision ID: teddy@recompile.se-20190306225624-834ex7y9sos3vzg4
mandos-ctl: Add test for --checker ""

* mandos-ctl (Test_command_from_options.test_checker_empty): 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
45
47
 
46
48
import dbus
47
49
 
475
477
    property = "Host"
476
478
 
477
479
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()
478
488
    property = "Secret"
479
489
 
480
490
class SetTimeoutCmd(PropertyCmd, MillisecondsValueArgumentMixIn):
609
619
        commands.append(RemoveCmd())
610
620
 
611
621
    if options.checker is not None:
612
 
        commands.append(SetCheckerCmd())
 
622
        commands.append(SetCheckerCmd(options.checker))
613
623
 
614
624
    if options.timeout is not None:
615
625
        commands.append(SetTimeoutCmd(options.timeout))
619
629
            SetExtendedTimeoutCmd(options.extended_timeout))
620
630
 
621
631
    if options.interval is not None:
622
 
        command.append(SetIntervalCmd(options.interval))
 
632
        commands.append(SetIntervalCmd(options.interval))
623
633
 
624
634
    if options.approved_by_default is not None:
625
635
        if options.approved_by_default:
626
 
            command.append(ApproveByDefaultCmd())
 
636
            commands.append(ApproveByDefaultCmd())
627
637
        else:
628
 
            command.append(DenyByDefaultCmd())
 
638
            commands.append(DenyByDefaultCmd())
629
639
 
630
640
    if options.approval_delay is not None:
631
 
        command.append(SetApprovalDelayCmd(options.approval_delay))
 
641
        commands.append(SetApprovalDelayCmd(options.approval_delay))
632
642
 
633
643
    if options.approval_duration is not None:
634
 
        command.append(
 
644
        commands.append(
635
645
            SetApprovalDurationCmd(options.approval_duration))
636
646
 
637
647
    if options.host is not None:
638
 
        command.append(SetHostCmd(options.host))
 
648
        commands.append(SetHostCmd(options.host))
639
649
 
640
650
    if options.secret is not None:
641
 
        command.append(SetSecretCmd(options.secret))
 
651
        commands.append(SetSecretCmd(options.secret))
642
652
 
643
653
    if options.approve:
644
654
        commands.append(ApproveCmd())
1083
1093
class TestSetSecretCmd(TestValueArgumentPropertyCmd):
1084
1094
    command = SetSecretCmd
1085
1095
    property = "Secret"
1086
 
    values_to_set = [b"", b"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"]
1087
1099
 
1088
1100
class TestSetTimeoutCmd(TestValueArgumentPropertyCmd):
1089
1101
    command = SetTimeoutCmd
1115
1127
    values_to_set = ["P0D", "PT5M", "PT1S", "PT120S", "P1Y"]
1116
1128
    values_to_get = [0, 300000, 1000, 120000, 31449600000]
1117
1129
 
1118
 
class TestOptions(unittest.TestCase):
 
1130
class Test_command_from_options(unittest.TestCase):
1119
1131
    def setUp(self):
1120
1132
        self.parser = argparse.ArgumentParser()
1121
1133
        add_command_line_options(self.parser)
1122
 
    def commands_from_args(self, args):
1123
 
        self.options = self.parser.parse_args(args)
1124
 
        return commands_from_options(self.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)
 
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)."""
 
1137
        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
 
1137
1152
    def test_enable(self):
1138
 
        commands = self.commands_from_args(["--enable", "foo"])
1139
 
        self.assertEqual(len(commands), 1)
1140
 
        command = commands[0]
1141
 
        self.assertIsInstance(command, EnableCmd)
1142
 
        self.assertEqual(self.options.client, ["foo"])
 
1153
        self.assert_command_from_args(["--enable", "foo"], EnableCmd)
 
1154
 
1143
1155
    def test_disable(self):
1144
 
        commands = self.commands_from_args(["--disable", "foo"])
1145
 
        self.assertEqual(len(commands), 1)
1146
 
        command = commands[0]
1147
 
        self.assertIsInstance(command, DisableCmd)
1148
 
        self.assertEqual(self.options.client, ["foo"])
 
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_checker_empty(self):
 
1180
        self.assert_command_from_args(["--checker", "", "foo"],
 
1181
                                      SetCheckerCmd, value_to_set="")
 
1182
 
 
1183
    def test_timeout(self):
 
1184
        self.assert_command_from_args(["--timeout", "PT5M", "foo"],
 
1185
                                      SetTimeoutCmd,
 
1186
                                      value_to_set=300000)
 
1187
 
 
1188
    def test_extended_timeout(self):
 
1189
        self.assert_command_from_args(["--extended-timeout", "PT15M",
 
1190
                                       "foo"],
 
1191
                                      SetExtendedTimeoutCmd,
 
1192
                                      value_to_set=900000)
 
1193
 
 
1194
    def test_interval(self):
 
1195
        self.assert_command_from_args(["--interval", "PT2M", "foo"],
 
1196
                                      SetIntervalCmd,
 
1197
                                      value_to_set=120000)
 
1198
 
 
1199
    def test_approve_by_default(self):
 
1200
        self.assert_command_from_args(["--approve-by-default", "foo"],
 
1201
                                      ApproveByDefaultCmd)
 
1202
 
 
1203
    def test_deny_by_default(self):
 
1204
        self.assert_command_from_args(["--deny-by-default", "foo"],
 
1205
                                      DenyByDefaultCmd)
 
1206
 
 
1207
    def test_approval_delay(self):
 
1208
        self.assert_command_from_args(["--approval-delay", "PT30S",
 
1209
                                       "foo"], SetApprovalDelayCmd,
 
1210
                                      value_to_set=30000)
 
1211
 
 
1212
    def test_approval_duration(self):
 
1213
        self.assert_command_from_args(["--approval-duration", "PT1S",
 
1214
                                       "foo"], SetApprovalDurationCmd,
 
1215
                                      value_to_set=1000)
 
1216
 
 
1217
    def test_host(self):
 
1218
        self.assert_command_from_args(["--host", "foo.example.org",
 
1219
                                       "foo"], SetHostCmd,
 
1220
                                      value_to_set="foo.example.org")
 
1221
 
 
1222
    def test_secret_devnull(self):
 
1223
        self.assert_command_from_args(["--secret", os.path.devnull,
 
1224
                                       "foo"], SetSecretCmd,
 
1225
                                      value_to_set=b"")
 
1226
 
 
1227
    def test_secret_tempfile(self):
 
1228
        with tempfile.NamedTemporaryFile(mode="r+b") as f:
 
1229
            value = b"secret\0xyzzy\nbar"
 
1230
            f.write(value)
 
1231
            f.seek(0)
 
1232
            self.assert_command_from_args(["--secret", f.name,
 
1233
                                           "foo"], SetSecretCmd,
 
1234
                                          value_to_set=value)
 
1235
 
 
1236
    def test_approve(self):
 
1237
        self.assert_command_from_args(["--approve", "foo"],
 
1238
                                      ApproveCmd)
 
1239
 
 
1240
    def test_deny(self):
 
1241
        self.assert_command_from_args(["--deny", "foo"], DenyCmd)
 
1242
 
 
1243
    def test_dump_json(self):
 
1244
        self.assert_command_from_args(["--dump-json"], DumpJSONCmd)
 
1245
 
 
1246
    def test_is_enabled(self):
 
1247
        self.assert_command_from_args(["--is-enabled", "foo"],
 
1248
                                      IsEnabledCmd)
1149
1249
 
1150
1250
 
1151
1251