2
# -*- after-save-hook: (lambda () (let ((command (if (fboundp 'file-local-name) (file-local-name (buffer-file-name)) (or (file-remote-p (buffer-file-name) 'localname) (buffer-file-name))))) (if (= (progn (if (get-buffer "*Test*") (kill-buffer "*Test*")) (process-file-shell-command (format "%s --check" (shell-quote-argument command)) nil "*Test*")) 0) (let ((w (get-buffer-window "*Test*"))) (if w (delete-window w))) (progn (with-current-buffer "*Test*" (compilation-mode)) (display-buffer "*Test*" '(display-buffer-in-side-window)))))); coding: utf-8 -*-
2
# -*- mode: python; coding: utf-8; after-save-hook: (lambda () (let ((command (if (and (boundp 'tramp-file-name-structure) (string-match (car tramp-file-name-structure) (buffer-file-name))) (tramp-file-name-localname (tramp-dissect-file-name (buffer-file-name))) (buffer-file-name)))) (if (= (shell-command (format "%s --check" (shell-quote-argument command)) "*Test*") 0) (let ((w (get-buffer-window "*Test*"))) (if w (delete-window w)) (kill-buffer "*Test*")) (display-buffer "*Test*")))); -*-
4
4
# Mandos Monitor - Control and monitor the Mandos server
250
250
def rfc3339_duration_to_delta(duration):
251
251
"""Parse an RFC 3339 "duration" and return a datetime.timedelta
253
>>> rfc3339_duration_to_delta("P7D") == datetime.timedelta(7)
255
>>> rfc3339_duration_to_delta("PT60S") == datetime.timedelta(0, 60)
257
>>> rfc3339_duration_to_delta("PT60M") == datetime.timedelta(hours=1)
260
>>> rfc3339_duration_to_delta("P60M") == datetime.timedelta(1680)
262
>>> rfc3339_duration_to_delta("PT24H") == datetime.timedelta(1)
264
>>> rfc3339_duration_to_delta("P1W") == datetime.timedelta(7)
266
>>> rfc3339_duration_to_delta("PT5M30S") == datetime.timedelta(0, 330)
268
>>> rfc3339_duration_to_delta("P1DT3M20S") == datetime.timedelta(1, 200)
253
>>> rfc3339_duration_to_delta("P7D")
254
datetime.timedelta(7)
255
>>> rfc3339_duration_to_delta("PT60S")
256
datetime.timedelta(0, 60)
257
>>> rfc3339_duration_to_delta("PT60M")
258
datetime.timedelta(0, 3600)
259
>>> rfc3339_duration_to_delta("P60M")
260
datetime.timedelta(1680)
261
>>> rfc3339_duration_to_delta("PT24H")
262
datetime.timedelta(1)
263
>>> rfc3339_duration_to_delta("P1W")
264
datetime.timedelta(7)
265
>>> rfc3339_duration_to_delta("PT5M30S")
266
datetime.timedelta(0, 330)
267
>>> rfc3339_duration_to_delta("P1DT3M20S")
268
datetime.timedelta(1, 200)
270
269
>>> # Can not be empty:
271
270
>>> rfc3339_duration_to_delta("")
272
271
Traceback (most recent call last):
382
381
"""Parse an interval string as documented by Mandos before 1.6.1,
383
382
and return a datetime.timedelta
385
>>> parse_pre_1_6_1_interval('7d') == datetime.timedelta(days=7)
387
>>> parse_pre_1_6_1_interval('60s') == datetime.timedelta(0, 60)
389
>>> parse_pre_1_6_1_interval('60m') == datetime.timedelta(hours=1)
391
>>> parse_pre_1_6_1_interval('24h') == datetime.timedelta(days=1)
393
>>> parse_pre_1_6_1_interval('1w') == datetime.timedelta(days=7)
395
>>> parse_pre_1_6_1_interval('5m 30s') == datetime.timedelta(0, 330)
397
>>> parse_pre_1_6_1_interval('') == datetime.timedelta(0)
384
>>> parse_pre_1_6_1_interval('7d')
385
datetime.timedelta(7)
386
>>> parse_pre_1_6_1_interval('60s')
387
datetime.timedelta(0, 60)
388
>>> parse_pre_1_6_1_interval('60m')
389
datetime.timedelta(0, 3600)
390
>>> parse_pre_1_6_1_interval('24h')
391
datetime.timedelta(1)
392
>>> parse_pre_1_6_1_interval('1w')
393
datetime.timedelta(7)
394
>>> parse_pre_1_6_1_interval('5m 30s')
395
datetime.timedelta(0, 330)
396
>>> parse_pre_1_6_1_interval('')
397
datetime.timedelta(0)
399
398
>>> # Ignore unknown characters, allow any order and repetitions
400
>>> parse_pre_1_6_1_interval('2dxy7zz11y3m5m') == datetime.timedelta(2, 480, 18000)
399
>>> parse_pre_1_6_1_interval('2dxy7zz11y3m5m')
400
datetime.timedelta(2, 480, 18000)
1781
1780
self.assert_command_from_args(["--is-enabled", "client"],
1782
1781
command.IsEnabled)
1784
def assert_command_from_args(self, args, command_cls, length=1,
1785
clients=None, **cmd_attrs):
1783
def assert_command_from_args(self, args, command_cls,
1786
1785
"""Assert that parsing ARGS should result in an instance of
1787
1786
COMMAND_CLS with (optionally) all supplied attributes (CMD_ATTRS)."""
1788
1787
options = self.parser.parse_args(args)
1789
1788
check_option_syntax(self.parser, options)
1790
1789
commands = commands_from_options(options)
1791
self.assertEqual(length, len(commands))
1792
for command in commands:
1793
if isinstance(command, command_cls):
1796
self.assertIsInstance(command, command_cls)
1797
if clients is not None:
1798
self.assertEqual(clients, options.client)
1790
self.assertEqual(1, len(commands))
1791
command = commands[0]
1792
self.assertIsInstance(command, command_cls)
1799
1793
for key, value in cmd_attrs.items():
1800
1794
self.assertEqual(value, getattr(command, key))
1802
def assert_commands_from_args(self, args, commands, clients=None):
1803
for cmd in commands:
1804
self.assert_command_from_args(args, cmd,
1805
length=len(commands),
1808
1796
def test_is_enabled_short(self):
1809
1797
self.assert_command_from_args(["-V", "client"],
1810
1798
command.IsEnabled)
2004
def test_manual_page_example_1(self):
2005
self.assert_command_from_args("--verbose".split(),
2010
def test_manual_page_example_2(self):
2011
self.assert_command_from_args(
2012
"--verbose foo1.example.org foo2.example.org".split(),
2013
command.PrintTable, clients=["foo1.example.org",
2014
"foo2.example.org"],
2017
def test_manual_page_example_3(self):
2018
self.assert_command_from_args("--enable --all".split(),
2022
def test_manual_page_example_4(self):
2023
self.assert_commands_from_args(
2024
("--timeout=PT5M --interval=PT1M foo1.example.org"
2025
" foo2.example.org").split(),
2026
[command.SetTimeout, command.SetInterval],
2027
clients=["foo1.example.org", "foo2.example.org"])
2029
def test_manual_page_example_5(self):
2030
self.assert_command_from_args("--approve --all".split(),
2035
1992
class TestCommand(unittest.TestCase):
2036
1993
"""Abstract class for tests of command classes"""