/mandos/trunk

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/trunk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
#!/usr/bin/python3 -bbI
# -*- coding: utf-8; lexical-binding: t -*-
#
# Mandos Control - Control or query the Mandos server
#
# Copyright © 2008-2022 Teddy Hogeborn
# Copyright © 2008-2022 Björn Påhlsson
#
# This file is part of Mandos.
#
# Mandos is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
#     Mandos is distributed in the hope that it will be useful, but
#     WITHOUT ANY WARRANTY; without even the implied warranty of
#     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#     GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Mandos.  If not, see <http://www.gnu.org/licenses/>.
#
# Contact the authors at <mandos@recompile.se>.
#
from __future__ import (division, absolute_import, print_function,
                        unicode_literals)

try:
    from future_builtins import *
except ImportError:
    pass

import sys
import unittest
import argparse
import logging
import os
import locale
import datetime
import re
import collections
import json
import io
import tempfile
import contextlib

if sys.version_info.major == 2:
    __metaclass__ = type
    str = unicode
    input = raw_input


class gi:
    """Dummy gi module, for the tests"""
    class repository:
        class GLib:
            class Error(Exception):
                pass


dbussy = None
ravel = None
dbus_python = None
pydbus = None

try:
    import dbussy
    import ravel
except ImportError:
    try:
        import pydbus
        import gi
    except ImportError:
        import dbus as dbus_python


# Show warnings by default
if not sys.warnoptions:
    import warnings
    warnings.simplefilter("default")

log = logging.getLogger(os.path.basename(sys.argv[0]))
logging.basicConfig(level="INFO",         # Show info level messages
                    format="%(message)s") # Show basic log messages

logging.captureWarnings(True)   # Show warnings via the logging system

if sys.version_info.major == 2:
    import StringIO
    io.StringIO = StringIO.StringIO

locale.setlocale(locale.LC_ALL, "")

version = "1.8.16"


def main():
    parser = argparse.ArgumentParser()
    add_command_line_options(parser)

    options = parser.parse_args()
    check_option_syntax(parser, options)

    clientnames = options.client

    if options.debug:
        logging.getLogger("").setLevel(logging.DEBUG)

    if dbussy is not None and ravel is not None:
        bus = dbussy_adapter.CachingBus(dbussy, ravel)
    elif pydbus is not None:
        bus = pydbus_adapter.CachingBus(pydbus)
    else:
        bus = dbus_python_adapter.CachingBus(dbus_python)

    try:
        all_clients = bus.get_clients_and_properties()
    except dbus.ConnectFailed as e:
        log.critical("Could not connect to Mandos server: %s", e)
        sys.exit(1)
    except dbus.Error as e:
        log.critical(
            "Failed to access Mandos server through D-Bus:\n%s", e)
        sys.exit(1)

    # Compile dict of (clientpath: properties) to process
    if not clientnames:
        clients = all_clients
    else:
        clients = {}
        for name in clientnames:
            for objpath, properties in all_clients.items():
                if properties["Name"] == name:
                    clients[objpath] = properties
                    break
            else:
                log.critical("Client not found on server: %r", name)
                sys.exit(1)

    commands = commands_from_options(options)

    for command in commands:
        command.run(clients, bus)


def add_command_line_options(parser):
    parser.add_argument("--version", action="version",
                        version="%(prog)s {}".format(version),
                        help="show version number and exit")
    parser.add_argument("-a", "--all", action="store_true",
                        help="Select all clients")
    parser.add_argument("-v", "--verbose", action="store_true",
                        help="Print all fields")
    parser.add_argument("-j", "--dump-json", dest="commands",
                        action="append_const", default=[],
                        const=command.DumpJSON(),
                        help="Dump client data in JSON format")
    enable_disable = parser.add_mutually_exclusive_group()
    enable_disable.add_argument("-e", "--enable", dest="commands",
                                action="append_const", default=[],
                                const=command.Enable(),
                                help="Enable client")
    enable_disable.add_argument("-d", "--disable", dest="commands",
                                action="append_const", default=[],
                                const=command.Disable(),
                                help="disable client")
    parser.add_argument("-b", "--bump-timeout", dest="commands",
                        action="append_const", default=[],
                        const=command.BumpTimeout(),
                        help="Bump timeout for client")
    start_stop_checker = parser.add_mutually_exclusive_group()
    start_stop_checker.add_argument("--start-checker",
                                    dest="commands",
                                    action="append_const", default=[],
                                    const=command.StartChecker(),
                                    help="Start checker for client")
    start_stop_checker.add_argument("--stop-checker", dest="commands",
                                    action="append_const", default=[],
                                    const=command.StopChecker(),
                                    help="Stop checker for client")
    parser.add_argument("-V", "--is-enabled", dest="commands",
                        action="append_const", default=[],
                        const=command.IsEnabled(),
                        help="Check if client is enabled")
    parser.add_argument("-r", "--remove", dest="commands",
                        action="append_const", default=[],
                        const=command.Remove(),
                        help="Remove client")
    parser.add_argument("-c", "--checker", dest="commands",
                        action="append", default=[],
                        metavar="COMMAND", type=command.SetChecker,
                        help="Set checker command for client")
    parser.add_argument(
        "-t", "--timeout", dest="commands", action="append",
        default=[], metavar="TIME",
        type=command.SetTimeout.argparse(string_to_delta),
        help="Set timeout for client")
    parser.add_argument(
        "--extended-timeout", dest="commands", action="append",
        default=[], metavar="TIME",
        type=command.SetExtendedTimeout.argparse(string_to_delta),
        help="Set extended timeout for client")
    parser.add_argument(
        "-i", "--interval", dest="commands", action="append",
        default=[], metavar="TIME",
        type=command.SetInterval.argparse(string_to_delta),
        help="Set checker interval for client")
    approve_deny_default = parser.add_mutually_exclusive_group()
    approve_deny_default.add_argument(
        "--approve-by-default", dest="commands",
        action="append_const", default=[],
        const=command.ApproveByDefault(),
        help="Set client to be approved by default")
    approve_deny_default.add_argument(
        "--deny-by-default", dest="commands",
        action="append_const", default=[],
        const=command.DenyByDefault(),
        help="Set client to be denied by default")
    parser.add_argument(
        "--approval-delay", dest="commands", action="append",
        default=[], metavar="TIME",
        type=command.SetApprovalDelay.argparse(string_to_delta),
        help="Set delay before client approve/deny")
    parser.add_argument(
        "--approval-duration", dest="commands", action="append",
        default=[], metavar="TIME",
        type=command.SetApprovalDuration.argparse(string_to_delta),
        help="Set duration of one client approval")
    parser.add_argument("-H", "--host", dest="commands",
                        action="append", default=[], metavar="STRING",
                        type=command.SetHost,
                        help="Set host for client")
    parser.add_argument(
        "-s", "--secret", dest="commands", action="append",
        default=[], metavar="FILENAME",
        type=command.SetSecret.argparse(argparse.FileType(mode="rb")),
        help="Set password blob (file) for client")
    approve_deny = parser.add_mutually_exclusive_group()
    approve_deny.add_argument(
        "-A", "--approve", dest="commands", action="append_const",
        default=[], const=command.Approve(),
        help="Approve any current client request")
    approve_deny.add_argument("-D", "--deny", dest="commands",
                              action="append_const", default=[],
                              const=command.Deny(),
                              help="Deny any current client request")
    parser.add_argument("--debug", action="store_true",
                        help="Debug mode (show D-Bus commands)")
    parser.add_argument("--check", action="store_true",
                        help="Run self-test")
    parser.add_argument("client", nargs="*", help="Client name")


def string_to_delta(interval):
    """Parse a string and return a datetime.timedelta"""

    try:
        return rfc3339_duration_to_delta(interval)
    except ValueError as e:
        log.warning("%s - Parsing as pre-1.6.1 interval instead",
                    " ".join(e.args))
    return parse_pre_1_6_1_interval(interval)


def rfc3339_duration_to_delta(duration):
    """Parse an RFC 3339 "duration" and return a datetime.timedelta

    >>> rfc3339_duration_to_delta("P7D") == datetime.timedelta(7)
    True
    >>> rfc3339_duration_to_delta("PT60S") == datetime.timedelta(0, 60)
    True
    >>> rfc3339_duration_to_delta("PT60M") == datetime.timedelta(hours=1)
    True
    >>> # 60 months
    >>> rfc3339_duration_to_delta("P60M") == datetime.timedelta(1680)
    True
    >>> rfc3339_duration_to_delta("PT24H") == datetime.timedelta(1)
    True
    >>> rfc3339_duration_to_delta("P1W") == datetime.timedelta(7)
    True
    >>> rfc3339_duration_to_delta("PT5M30S") == datetime.timedelta(0, 330)
    True
    >>> rfc3339_duration_to_delta("P1DT3M20S") == datetime.timedelta(1, 200)
    True
    >>> # Can not be empty:
    >>> rfc3339_duration_to_delta("")
    Traceback (most recent call last):
    ...
    ValueError: Invalid RFC 3339 duration: ""
    >>> # Must start with "P":
    >>> rfc3339_duration_to_delta("1D")
    Traceback (most recent call last):
    ...
    ValueError: Invalid RFC 3339 duration: "1D"
    >>> # Must use correct order
    >>> rfc3339_duration_to_delta("PT1S2M")
    Traceback (most recent call last):
    ...
    ValueError: Invalid RFC 3339 duration: "PT1S2M"
    >>> # Time needs time marker
    >>> rfc3339_duration_to_delta("P1H2S")
    Traceback (most recent call last):
    ...
    ValueError: Invalid RFC 3339 duration: "P1H2S"
    >>> # Weeks can not be combined with anything else
    >>> rfc3339_duration_to_delta("P1D2W")
    Traceback (most recent call last):
    ...
    ValueError: Invalid RFC 3339 duration: "P1D2W"
    >>> rfc3339_duration_to_delta("P2W2H")
    Traceback (most recent call last):
    ...
    ValueError: Invalid RFC 3339 duration: "P2W2H"
    """

    # Parsing an RFC 3339 duration with regular expressions is not
    # possible - there would have to be multiple places for the same
    # values, like seconds.  The current code, while more esoteric, is
    # cleaner without depending on a parsing library.  If Python had a
    # built-in library for parsing we would use it, but we'd like to
    # avoid excessive use of external libraries.

    # New type for defining tokens, syntax, and semantics all-in-one
    Token = collections.namedtuple("Token", (
        "regexp",  # To match token; if "value" is not None, must have
                   # a "group" containing digits
        "value",   # datetime.timedelta or None
        "followers"))           # Tokens valid after this token
    # RFC 3339 "duration" tokens, syntax, and semantics; taken from
    # the "duration" ABNF definition in RFC 3339, Appendix A.
    token_end = Token(re.compile(r"$"), None, frozenset())
    token_second = Token(re.compile(r"(\d+)S"),
                         datetime.timedelta(seconds=1),
                         frozenset((token_end, )))
    token_minute = Token(re.compile(r"(\d+)M"),
                         datetime.timedelta(minutes=1),
                         frozenset((token_second, token_end)))
    token_hour = Token(re.compile(r"(\d+)H"),
                       datetime.timedelta(hours=1),
                       frozenset((token_minute, token_end)))
    token_time = Token(re.compile(r"T"),
                       None,
                       frozenset((token_hour, token_minute,
                                  token_second)))
    token_day = Token(re.compile(r"(\d+)D"),
                      datetime.timedelta(days=1),
                      frozenset((token_time, token_end)))
    token_month = Token(re.compile(r"(\d+)M"),
                        datetime.timedelta(weeks=4),
                        frozenset((token_day, token_end)))
    token_year = Token(re.compile(r"(\d+)Y"),
                       datetime.timedelta(weeks=52),
                       frozenset((token_month, token_end)))
    token_week = Token(re.compile(r"(\d+)W"),
                       datetime.timedelta(weeks=1),
                       frozenset((token_end, )))
    token_duration = Token(re.compile(r"P"), None,
                           frozenset((token_year, token_month,
                                      token_day, token_time,
                                      token_week)))
    # Define starting values:
    # Value so far
    value = datetime.timedelta()
    found_token = None
    # Following valid tokens
    followers = frozenset((token_duration, ))
    # String left to parse
    s = duration
    # Loop until end token is found
    while found_token is not token_end:
        # Search for any currently valid tokens
        for token in followers:
            match = token.regexp.match(s)
            if match is not None:
                # Token found
                if token.value is not None:
                    # Value found, parse digits
                    factor = int(match.group(1), 10)
                    # Add to value so far
                    value += factor * token.value
                # Strip token from string
                s = token.regexp.sub("", s, 1)
                # Go to found token
                found_token = token
                # Set valid next tokens
                followers = found_token.followers
                break
        else:
            # No currently valid tokens were found
            raise ValueError("Invalid RFC 3339 duration: \"{}\""
                             .format(duration))
    # End token found
    return value


def parse_pre_1_6_1_interval(interval):
    r"""Parse an interval string as documented by Mandos before 1.6.1,
    and return a datetime.timedelta

    >>> parse_pre_1_6_1_interval("7d") == datetime.timedelta(days=7)
    True
    >>> parse_pre_1_6_1_interval("60s") == datetime.timedelta(0, 60)
    True
    >>> parse_pre_1_6_1_interval("60m") == datetime.timedelta(hours=1)
    True
    >>> parse_pre_1_6_1_interval("24h") == datetime.timedelta(days=1)
    True
    >>> parse_pre_1_6_1_interval("1w") == datetime.timedelta(days=7)
    True
    >>> parse_pre_1_6_1_interval("5m 30s") == datetime.timedelta(0, 330)
    True
    >>> parse_pre_1_6_1_interval("") == datetime.timedelta(0)
    True
    >>> # Ignore unknown characters, allow any order and repetitions
    >>> parse_pre_1_6_1_interval("2dxy7zz11y3m5m") \
    ... == datetime.timedelta(2, 480, 18000)
    True

    """

    value = datetime.timedelta(0)
    regexp = re.compile(r"(\d+)([dsmhw]?)")

    for num, suffix in regexp.findall(interval):
        if suffix == "d":
            value += datetime.timedelta(int(num))
        elif suffix == "s":
            value += datetime.timedelta(0, int(num))
        elif suffix == "m":
            value += datetime.timedelta(0, 0, 0, 0, int(num))
        elif suffix == "h":
            value += datetime.timedelta(0, 0, 0, 0, 0, int(num))
        elif suffix == "w":
            value += datetime.timedelta(0, 0, 0, 0, 0, 0, int(num))
        elif suffix == "":
            value += datetime.timedelta(0, 0, 0, int(num))
    return value


def check_option_syntax(parser, options):
    """Apply additional restrictions on options, not expressible in
argparse"""

    def has_commands(options, commands=None):
        if commands is None:
            commands = (command.Enable,
                        command.Disable,
                        command.BumpTimeout,
                        command.StartChecker,
                        command.StopChecker,
                        command.IsEnabled,
                        command.Remove,
                        command.SetChecker,
                        command.SetTimeout,
                        command.SetExtendedTimeout,
                        command.SetInterval,
                        command.ApproveByDefault,
                        command.DenyByDefault,
                        command.SetApprovalDelay,
                        command.SetApprovalDuration,
                        command.SetHost,
                        command.SetSecret,
                        command.Approve,
                        command.Deny)
        return any(isinstance(cmd, commands)
                   for cmd in options.commands)

    if has_commands(options) and not (options.client or options.all):
        parser.error("Options require clients names or --all.")
    if options.verbose and has_commands(options):
        parser.error("--verbose can only be used alone.")
    if (has_commands(options, (command.DumpJSON,))
        and (options.verbose or len(options.commands) > 1)):
        parser.error("--dump-json can only be used alone.")
    if options.all and not has_commands(options):
        parser.error("--all requires an action.")
    if (has_commands(options, (command.IsEnabled,))
        and len(options.client) > 1):
        parser.error("--is-enabled requires exactly one client")
    if (len(options.commands) > 1
        and has_commands(options, (command.Remove,))
        and not has_commands(options, (command.Deny,))):
        parser.error("--remove can only be combined with --deny")


class dbus:

    class SystemBus:

        object_manager_iface = "org.freedesktop.DBus.ObjectManager"

        def get_managed_objects(self, busname, objectpath):
            return self.call_method("GetManagedObjects", busname,
                                    objectpath,
                                    self.object_manager_iface)

        properties_iface = "org.freedesktop.DBus.Properties"

        def set_property(self, busname, objectpath, interface, key,
                         value):
            self.call_method("Set", busname, objectpath,
                             self.properties_iface, interface, key,
                             value)

        def call_method(self, methodname, busname, objectpath,
                        interface, *args):
            raise NotImplementedError()

    class MandosBus(SystemBus):
        busname_domain = "se.recompile"
        busname = busname_domain + ".Mandos"
        server_path = "/"
        server_interface = busname_domain + ".Mandos"
        client_interface = busname_domain + ".Mandos.Client"
        del busname_domain

        def get_clients_and_properties(self):
            managed_objects = self.get_managed_objects(
                self.busname, self.server_path)
            return {objpath: properties[self.client_interface]
                    for objpath, properties in managed_objects.items()
                    if self.client_interface in properties}

        def set_client_property(self, objectpath, key, value):
            return self.set_property(self.busname, objectpath,
                                     self.client_interface, key,
                                     value)

        def call_client_method(self, objectpath, method, *args):
            return self.call_method(method, self.busname, objectpath,
                                    self.client_interface, *args)

        def call_server_method(self, method, *args):
            return self.call_method(method, self.busname,
                                    self.server_path,
                                    self.server_interface, *args)

    class Error(Exception):
        pass

    class ConnectFailed(Error):
        pass


class dbus_python_adapter:

    class SystemBus(dbus.MandosBus):
        """Use dbus-python"""

        def __init__(self, module=dbus_python):
            self.dbus_python = module
            self.bus = self.dbus_python.SystemBus()

        @contextlib.contextmanager
        def convert_exception(self, exception_class=dbus.Error):
            try:
                yield
            except self.dbus_python.exceptions.DBusException as e:
                # This does what "raise from" would do
                exc = exception_class(*e.args)
                exc.__cause__ = e
                raise exc

        def call_method(self, methodname, busname, objectpath,
                        interface, *args):
            proxy_object = self.get_object(busname, objectpath)
            log.debug("D-Bus: %s:%s:%s.%s(%s)", busname, objectpath,
                      interface, methodname,
                      ", ".join(repr(a) for a in args))
            method = getattr(proxy_object, methodname)
            with self.convert_exception():
                with dbus_python_adapter.SilenceLogger(
                        "dbus.proxies"):
                    value = method(*args, dbus_interface=interface)
            return self.type_filter(value)

        def get_object(self, busname, objectpath):
            log.debug("D-Bus: Connect to: (busname=%r, path=%r)",
                      busname, objectpath)
            with self.convert_exception(dbus.ConnectFailed):
                return self.bus.get_object(busname, objectpath)

        def type_filter(self, value):
            """Convert the most bothersome types to Python types"""
            if isinstance(value, self.dbus_python.Boolean):
                return bool(value)
            if isinstance(value, self.dbus_python.ObjectPath):
                return str(value)
            # Also recurse into dictionaries
            if isinstance(value, self.dbus_python.Dictionary):
                return {self.type_filter(key):
                        self.type_filter(subval)
                        for key, subval in value.items()}
            return value

        def set_client_property(self, objectpath, key, value):
            if key == "Secret":
                if not isinstance(value, bytes):
                    value = value.encode("utf-8")
                value = self.dbus_python.ByteArray(value)
            return self.set_property(self.busname, objectpath,
                                     self.client_interface, key,
                                     value)

    class SilenceLogger:
        "Simple context manager to silence a particular logger"

        def __init__(self, loggername):
            self.logger = logging.getLogger(loggername)

        def __enter__(self):
            self.logger.addFilter(self.nullfilter)

        class NullFilter(logging.Filter):
            def filter(self, record):
                return False

        nullfilter = NullFilter()

        def __exit__(self, exc_type, exc_val, exc_tb):
            self.logger.removeFilter(self.nullfilter)

    class CachingBus(SystemBus):
        """A caching layer for dbus_python_adapter.SystemBus"""

        def __init__(self, *args, **kwargs):
            self.object_cache = {}
            super(dbus_python_adapter.CachingBus,
                  self).__init__(*args, **kwargs)

        def get_object(self, busname, objectpath):
            try:
                return self.object_cache[(busname, objectpath)]
            except KeyError:
                new_object = super(
                    dbus_python_adapter.CachingBus,
                    self).get_object(busname, objectpath)
                self.object_cache[(busname, objectpath)] = new_object
                return new_object


class pydbus_adapter:
    class SystemBus(dbus.MandosBus):
        def __init__(self, module=pydbus):
            self.pydbus = module
            self.bus = self.pydbus.SystemBus()

        @contextlib.contextmanager
        def convert_exception(self, exception_class=dbus.Error):
            try:
                yield
            except gi.repository.GLib.Error as e:
                # This does what "raise from" would do
                exc = exception_class(*e.args)
                exc.__cause__ = e
                raise exc

        def call_method(self, methodname, busname, objectpath,
                        interface, *args):
            proxy_object = self.get(busname, objectpath)
            log.debug("D-Bus: %s:%s:%s.%s(%s)", busname, objectpath,
                      interface, methodname,
                      ", ".join(repr(a) for a in args))
            method = getattr(proxy_object[interface], methodname)
            with self.convert_exception():
                return method(*args)

        def get(self, busname, objectpath):
            log.debug("D-Bus: Connect to: (busname=%r, path=%r)",
                      busname, objectpath)
            with self.convert_exception(dbus.ConnectFailed):
                if sys.version_info.major <= 2:
                    with warnings.catch_warnings():
                        warnings.filterwarnings(
                            "ignore", "", DeprecationWarning,
                            r"^xml\.etree\.ElementTree$")
                        return self.bus.get(busname, objectpath)
                else:
                    return self.bus.get(busname, objectpath)

        def set_property(self, busname, objectpath, interface, key,
                         value):
            proxy_object = self.get(busname, objectpath)
            log.debug("D-Bus: %s:%s:%s.Set(%r, %r, %r)", busname,
                      objectpath, self.properties_iface, interface,
                      key, value)
            setattr(proxy_object[interface], key, value)

    class CachingBus(SystemBus):
        """A caching layer for pydbus_adapter.SystemBus"""

        def __init__(self, *args, **kwargs):
            self.object_cache = {}
            super(pydbus_adapter.CachingBus,
                  self).__init__(*args, **kwargs)

        def get(self, busname, objectpath):
            try:
                return self.object_cache[(busname, objectpath)]
            except KeyError:
                new_object = (super(pydbus_adapter.CachingBus, self)
                              .get(busname, objectpath))
                self.object_cache[(busname, objectpath)] = new_object
                return new_object


class dbussy_adapter:
    class SystemBus(dbus.SystemBus):
        """Use DBussy"""

        def __init__(self, dbussy, ravel):
            self.dbussy = dbussy
            self.ravel = ravel
            self.bus = ravel.system_bus()

        @contextlib.contextmanager
        def convert_exception(self, exception_class=dbus.Error):
            try:
                yield
            except self.dbussy.DBusError as e:
                # This does what "raise from" would do
                exc = exception_class(*e.args)
                exc.__cause__ = e
                raise exc

        def call_method(self, methodname, busname, objectpath,
                        interface, *args):
            proxy_object = self.get_object(busname, objectpath)
            log.debug("D-Bus: %s:%s:%s.%s(%s)", busname, objectpath,
                      interface, methodname,
                      ", ".join(repr(a) for a in args))
            iface = proxy_object.get_interface(interface)
            method = getattr(iface, methodname)
            with self.convert_exception(dbus.Error):
                value = method(*args)
            # DBussy returns values either as an empty list or as a
            # list of one element with the return value
            if value:
                return self.type_filter(value[0])

        def get_object(self, busname, objectpath):
            log.debug("D-Bus: Connect to: (busname=%r, path=%r)",
                      busname, objectpath)
            with self.convert_exception(dbus.ConnectFailed):
                return self.bus[busname][objectpath]

        def type_filter(self, value):
            """Convert the most bothersome types to Python types"""
            # A D-Bus Variant value is represented as the Python type
            # Tuple[dbussy.DBUS.Signature, Any]
            if isinstance(value, tuple):
                if (len(value) == 2
                    and isinstance(value[0],
                                   self.dbussy.DBUS.Signature)):
                    return self.type_filter(value[1])
            elif isinstance(value, self.dbussy.DBUS.ObjectPath):
                return str(value)
            # Also recurse into dictionaries
            elif isinstance(value, dict):
                return {self.type_filter(key):
                        self.type_filter(subval)
                        for key, subval in value.items()}
            return value

        def set_property(self, busname, objectpath, interface, key,
                         value):
            proxy_object = self.get_object(busname, objectpath)
            log.debug("D-Bus: %s:%s:%s.Set(%r, %r, %r)", busname,
                      objectpath, self.properties_iface, interface,
                      key, value)
            if key == "Secret":
                # DBussy wants a Byte Array to be a sequence of
                # values, not a byte string
                value = tuple(value)
            setattr(proxy_object.get_interface(interface), key, value)

    class MandosBus(SystemBus, dbus.MandosBus):
        pass

    class CachingBus(MandosBus):
        """A caching layer for dbussy_adapter.MandosBus"""

        def __init__(self, *args, **kwargs):
            self.object_cache = {}
            super(dbussy_adapter.CachingBus, self).__init__(*args,
                                                            **kwargs)

        def get_object(self, busname, objectpath):
            try:
                return self.object_cache[(busname, objectpath)]
            except KeyError:
                new_object = super(
                    dbussy_adapter.CachingBus,
                    self).get_object(busname, objectpath)
                self.object_cache[(busname, objectpath)] = new_object
                return new_object


def commands_from_options(options):

    commands = list(options.commands)

    def find_cmd(cmd, commands):
        i = 0
        for i, c in enumerate(commands):
            if isinstance(c, cmd):
                return i
        return i+1

    # If command.Remove is present, move any instances of command.Deny
    # to occur ahead of command.Remove.
    index_of_remove = find_cmd(command.Remove, commands)
    before_remove = commands[:index_of_remove]
    after_remove = commands[index_of_remove:]
    cleaned_after = []
    for cmd in after_remove:
        if isinstance(cmd, command.Deny):
            before_remove.append(cmd)
        else:
            cleaned_after.append(cmd)
    if cleaned_after != after_remove:
        commands = before_remove + cleaned_after

    # If no command option has been given, show table of clients,
    # optionally verbosely
    if not commands:
        commands.append(command.PrintTable(verbose=options.verbose))

    return commands


class command:
    """A namespace for command classes"""

    class Base:
        """Abstract base class for commands"""

        def run(self, clients, bus=None):
            """Normal commands should implement run_on_one_client(),
but commands which want to operate on all clients at the same time can
override this run() method instead.
"""
            self.bus = bus
            for client, properties in clients.items():
                self.run_on_one_client(client, properties)

    class IsEnabled(Base):
        def run(self, clients, bus=None):
            properties = next(iter(clients.values()))
            if properties["Enabled"]:
                sys.exit(0)
            sys.exit(1)

    class Approve(Base):
        def run_on_one_client(self, client, properties):
            self.bus.call_client_method(client, "Approve", True)

    class Deny(Base):
        def run_on_one_client(self, client, properties):
            self.bus.call_client_method(client, "Approve", False)

    class Remove(Base):
        def run(self, clients, bus):
            for clientpath in frozenset(clients.keys()):
                bus.call_server_method("RemoveClient", clientpath)

    class Output(Base):
        """Abstract class for commands outputting client details"""
        all_keywords = ("Name", "Enabled", "Timeout", "LastCheckedOK",
                        "Created", "Interval", "Host", "KeyID",
                        "Fingerprint", "CheckerRunning",
                        "LastEnabled", "ApprovalPending",
                        "ApprovedByDefault", "LastApprovalRequest",
                        "ApprovalDelay", "ApprovalDuration",
                        "Checker", "ExtendedTimeout", "Expires",
                        "LastCheckerStatus")

    class DumpJSON(Output):
        def run(self, clients, bus=None):
            data = {properties["Name"]:
                    {key: properties[key]
                     for key in self.all_keywords}
                    for properties in clients.values()}
            print(json.dumps(data, indent=4, separators=(",", ": ")))

    class PrintTable(Output):
        def __init__(self, verbose=False):
            self.verbose = verbose

        def run(self, clients, bus=None):
            default_keywords = ("Name", "Enabled", "Timeout",
                                "LastCheckedOK")
            keywords = default_keywords
            if self.verbose:
                keywords = self.all_keywords
            print(self.TableOfClients(clients.values(), keywords))

        class TableOfClients:
            tableheaders = {
                "Name": "Name",
                "Enabled": "Enabled",
                "Timeout": "Timeout",
                "LastCheckedOK": "Last Successful Check",
                "LastApprovalRequest": "Last Approval Request",
                "Created": "Created",
                "Interval": "Interval",
                "Host": "Host",
                "Fingerprint": "Fingerprint",
                "KeyID": "Key ID",
                "CheckerRunning": "Check Is Running",
                "LastEnabled": "Last Enabled",
                "ApprovalPending": "Approval Is Pending",
                "ApprovedByDefault": "Approved By Default",
                "ApprovalDelay": "Approval Delay",
                "ApprovalDuration": "Approval Duration",
                "Checker": "Checker",
                "ExtendedTimeout": "Extended Timeout",
                "Expires": "Expires",
                "LastCheckerStatus": "Last Checker Status",
            }

            def __init__(self, clients, keywords):
                self.clients = clients
                self.keywords = keywords

            def __str__(self):
                return "\n".join(self.rows())

            if sys.version_info.major == 2:
                __unicode__ = __str__

                def __str__(self):
                    return str(self).encode(
                        locale.getpreferredencoding())

            def rows(self):
                format_string = self.row_formatting_string()
                rows = [self.header_line(format_string)]
                rows.extend(self.client_line(client, format_string)
                            for client in self.clients)
                return rows

            def row_formatting_string(self):
                "Format string used to format table rows"
                return " ".join("{{{key}:{width}}}".format(
                    width=max(len(self.tableheaders[key]),
                              *(len(self.string_from_client(client,
                                                            key))
                                for client in self.clients)),
                    key=key)
                                for key in self.keywords)

            def string_from_client(self, client, key):
                return self.valuetostring(client[key], key)

            @classmethod
            def valuetostring(cls, value, keyword):
                if isinstance(value, bool):
                    return "Yes" if value else "No"
                if keyword in ("Timeout", "Interval", "ApprovalDelay",
                               "ApprovalDuration", "ExtendedTimeout"):
                    return cls.milliseconds_to_string(value)
                return str(value)

            def header_line(self, format_string):
                return format_string.format(**self.tableheaders)

            def client_line(self, client, format_string):
                return format_string.format(
                    **{key: self.string_from_client(client, key)
                       for key in self.keywords})

            @staticmethod
            def milliseconds_to_string(ms):
                td = datetime.timedelta(0, 0, 0, ms)
                return ("{days}{hours:02}:{minutes:02}:{seconds:02}"
                        .format(days="{}T".format(td.days)
                                if td.days else "",
                                hours=td.seconds // 3600,
                                minutes=(td.seconds % 3600) // 60,
                                seconds=td.seconds % 60))

    class PropertySetter(Base):
        "Abstract class for Actions for setting one client property"

        def run_on_one_client(self, client, properties=None):
            """Set the Client's D-Bus property"""
            self.bus.set_client_property(client, self.propname,
                                         self.value_to_set)

        @property
        def propname(self):
            raise NotImplementedError()

    class Enable(PropertySetter):
        propname = "Enabled"
        value_to_set = True

    class Disable(PropertySetter):
        propname = "Enabled"
        value_to_set = False

    class BumpTimeout(PropertySetter):
        propname = "LastCheckedOK"
        value_to_set = ""

    class StartChecker(PropertySetter):
        propname = "CheckerRunning"
        value_to_set = True

    class StopChecker(PropertySetter):
        propname = "CheckerRunning"
        value_to_set = False

    class ApproveByDefault(PropertySetter):
        propname = "ApprovedByDefault"
        value_to_set = True

    class DenyByDefault(PropertySetter):
        propname = "ApprovedByDefault"
        value_to_set = False

    class PropertySetterValue(PropertySetter):
        """Abstract class for PropertySetter recieving a value as
constructor argument instead of a class attribute."""

        def __init__(self, value):
            self.value_to_set = value

        @classmethod
        def argparse(cls, argtype):
            def cmdtype(arg):
                return cls(argtype(arg))
            return cmdtype

    class SetChecker(PropertySetterValue):
        propname = "Checker"

    class SetHost(PropertySetterValue):
        propname = "Host"

    class SetSecret(PropertySetterValue):
        propname = "Secret"

        @property
        def value_to_set(self):
            return self._vts

        @value_to_set.setter
        def value_to_set(self, value):
            """When setting, read data from supplied file object"""
            self._vts = value.read()
            value.close()

    class PropertySetterValueMilliseconds(PropertySetterValue):
        """Abstract class for PropertySetterValue taking a value
argument as a datetime.timedelta() but should store it as
milliseconds."""

        @property
        def value_to_set(self):
            return self._vts

        @value_to_set.setter
        def value_to_set(self, value):
            "When setting, convert value from a datetime.timedelta"
            self._vts = int(round(value.total_seconds() * 1000))

    class SetTimeout(PropertySetterValueMilliseconds):
        propname = "Timeout"

    class SetExtendedTimeout(PropertySetterValueMilliseconds):
        propname = "ExtendedTimeout"

    class SetInterval(PropertySetterValueMilliseconds):
        propname = "Interval"

    class SetApprovalDelay(PropertySetterValueMilliseconds):
        propname = "ApprovalDelay"

    class SetApprovalDuration(PropertySetterValueMilliseconds):
        propname = "ApprovalDuration"



class TestCaseWithAssertLogs(unittest.TestCase):
    """unittest.TestCase.assertLogs only exists in Python 3.4"""

    if not hasattr(unittest.TestCase, "assertLogs"):
        @contextlib.contextmanager
        def assertLogs(self, logger, level=logging.INFO):
            capturing_handler = self.CapturingLevelHandler(level)
            old_level = logger.level
            old_propagate = logger.propagate
            logger.addHandler(capturing_handler)
            logger.setLevel(level)
            logger.propagate = False
            try:
                yield capturing_handler.watcher
            finally:
                logger.propagate = old_propagate
                logger.removeHandler(capturing_handler)
                logger.setLevel(old_level)
            self.assertGreater(len(capturing_handler.watcher.records),
                               0)

        class CapturingLevelHandler(logging.Handler):
            def __init__(self, level, *args, **kwargs):
                logging.Handler.__init__(self, *args, **kwargs)
                self.watcher = self.LoggingWatcher([], [])
            def emit(self, record):
                self.watcher.records.append(record)
                self.watcher.output.append(self.format(record))

            LoggingWatcher = collections.namedtuple("LoggingWatcher",
                                                    ("records",
                                                     "output"))


class Unique:
    """Class for objects which exist only to be unique objects, since
unittest.mock.sentinel only exists in Python 3.3"""


class Test_string_to_delta(TestCaseWithAssertLogs):
    # Just test basic RFC 3339 functionality here, the doc string for
    # rfc3339_duration_to_delta() already has more comprehensive
    # tests, which are run by doctest.

    def test_rfc3339_zero_seconds(self):
        self.assertEqual(datetime.timedelta(),
                         string_to_delta("PT0S"))

    def test_rfc3339_zero_days(self):
        self.assertEqual(datetime.timedelta(), string_to_delta("P0D"))

    def test_rfc3339_one_second(self):
        self.assertEqual(datetime.timedelta(0, 1),
                         string_to_delta("PT1S"))

    def test_rfc3339_two_hours(self):
        self.assertEqual(datetime.timedelta(0, 7200),
                         string_to_delta("PT2H"))

    def test_falls_back_to_pre_1_6_1_with_warning(self):
        with self.assertLogs(log, logging.WARNING):
            value = string_to_delta("2h")
        self.assertEqual(datetime.timedelta(0, 7200), value)


class Test_check_option_syntax(unittest.TestCase):
    def setUp(self):
        self.parser = argparse.ArgumentParser()
        add_command_line_options(self.parser)

    def test_actions_requires_client_or_all(self):
        for action, value in self.actions.items():
            args = self.actionargs(action, value)
            with self.assertParseError():
                self.parse_args(args)

    # This mostly corresponds to the definition from has_commands() in
    # check_option_syntax()
    actions = {
        "--enable": None,
        "--disable": None,
        "--bump-timeout": None,
        "--start-checker": None,
        "--stop-checker": None,
        "--is-enabled": None,
        "--remove": None,
        "--checker": "x",
        "--timeout": "PT0S",
        "--extended-timeout": "PT0S",
        "--interval": "PT0S",
        "--approve-by-default": None,
        "--deny-by-default": None,
        "--approval-delay": "PT0S",
        "--approval-duration": "PT0S",
        "--host": "hostname",
        "--secret": "/dev/null",
        "--approve": None,
        "--deny": None,
    }

    @staticmethod
    def actionargs(action, value, *args):
        if value is not None:
            return [action, value] + list(args)
        else:
            return [action] + list(args)

    @contextlib.contextmanager
    def assertParseError(self):
        with self.assertRaises(SystemExit) as e:
            with self.redirect_stderr_to_devnull():
                yield
        # Exit code from argparse is guaranteed to be "2".  Reference:
        # https://docs.python.org/3/library
        # /argparse.html#exiting-methods
        self.assertEqual(2, e.exception.code)

    def parse_args(self, args):
        options = self.parser.parse_args(args)
        check_option_syntax(self.parser, options)

    @staticmethod
    @contextlib.contextmanager
    def redirect_stderr_to_devnull():
        old_stderr = sys.stderr
        with contextlib.closing(open(os.devnull, "w")) as null:
            sys.stderr = null
            try:
                yield
            finally:
                sys.stderr = old_stderr

    def check_option_syntax(self, options):
        check_option_syntax(self.parser, options)

    def test_actions_all_conflicts_with_verbose(self):
        for action, value in self.actions.items():
            args = self.actionargs(action, value, "--all",
                                   "--verbose")
            with self.assertParseError():
                self.parse_args(args)

    def test_actions_with_client_conflicts_with_verbose(self):
        for action, value in self.actions.items():
            args = self.actionargs(action, value, "--verbose",
                                   "client")
            with self.assertParseError():
                self.parse_args(args)

    def test_dump_json_conflicts_with_verbose(self):
        args = ["--dump-json", "--verbose"]
        with self.assertParseError():
            self.parse_args(args)

    def test_dump_json_conflicts_with_action(self):
        for action, value in self.actions.items():
            args = self.actionargs(action, value, "--dump-json")
            with self.assertParseError():
                self.parse_args(args)

    def test_all_can_not_be_alone(self):
        args = ["--all"]
        with self.assertParseError():
            self.parse_args(args)

    def test_all_is_ok_with_any_action(self):
        for action, value in self.actions.items():
            args = self.actionargs(action, value, "--all")
            self.parse_args(args)

    def test_any_action_is_ok_with_one_client(self):
        for action, value in self.actions.items():
            args = self.actionargs(action, value, "client")
            self.parse_args(args)

    def test_one_client_with_all_actions_except_is_enabled(self):
        for action, value in self.actions.items():
            if action == "--is-enabled":
                continue
            args = self.actionargs(action, value, "client")
            self.parse_args(args)

    def test_two_clients_with_all_actions_except_is_enabled(self):
        for action, value in self.actions.items():
            if action == "--is-enabled":
                continue
            args = self.actionargs(action, value, "client1",
                                   "client2")
            self.parse_args(args)

    def test_two_clients_are_ok_with_actions_except_is_enabled(self):
        for action, value in self.actions.items():
            if action == "--is-enabled":
                continue
            args = self.actionargs(action, value, "client1",
                                   "client2")
            self.parse_args(args)

    def test_is_enabled_fails_without_client(self):
        args = ["--is-enabled"]
        with self.assertParseError():
            self.parse_args(args)

    def test_is_enabled_fails_with_two_clients(self):
        args = ["--is-enabled", "client1", "client2"]
        with self.assertParseError():
            self.parse_args(args)

    def test_remove_can_only_be_combined_with_action_deny(self):
        for action, value in self.actions.items():
            if action in {"--remove", "--deny"}:
                continue
            args = self.actionargs(action, value, "--all",
                                   "--remove")
            with self.assertParseError():
                self.parse_args(args)


class Test_dbus_exceptions(unittest.TestCase):

    def test_dbus_ConnectFailed_is_Error(self):
        with self.assertRaises(dbus.Error):
            raise dbus.ConnectFailed()


class Test_dbus_MandosBus(unittest.TestCase):

    class MockMandosBus(dbus.MandosBus):
        def __init__(self):
            self._name = "se.recompile.Mandos"
            self._server_path = "/"
            self._server_interface = "se.recompile.Mandos"
            self._client_interface = "se.recompile.Mandos.Client"
            self.calls = []
            self.call_method_return = Unique()

        def call_method(self, methodname, busname, objectpath,
                        interface, *args):
            self.calls.append((methodname, busname, objectpath,
                               interface, args))
            return self.call_method_return

    def setUp(self):
        self.bus = self.MockMandosBus()

    def test_set_client_property(self):
        self.bus.set_client_property("objectpath", "key", "value")
        expected_call = ("Set", self.bus._name, "objectpath",
                         "org.freedesktop.DBus.Properties",
                         (self.bus._client_interface, "key", "value"))
        self.assertIn(expected_call, self.bus.calls)

    def test_call_client_method(self):
        ret = self.bus.call_client_method("objectpath", "methodname")
        self.assertIs(self.bus.call_method_return, ret)
        expected_call = ("methodname", self.bus._name, "objectpath",
                         self.bus._client_interface, ())
        self.assertIn(expected_call, self.bus.calls)

    def test_call_client_method_with_args(self):
        args = (Unique(), Unique())
        ret = self.bus.call_client_method("objectpath", "methodname",
                                          *args)
        self.assertIs(self.bus.call_method_return, ret)
        expected_call = ("methodname", self.bus._name, "objectpath",
                         self.bus._client_interface,
                         (args[0], args[1]))
        self.assertIn(expected_call, self.bus.calls)

    def test_get_clients_and_properties(self):
        managed_objects = {
            "objectpath": {
                self.bus._client_interface: {
                    "key": "value",
                    "bool": True,
                },
                "irrelevant_interface": {
                    "key": "othervalue",
                    "bool": False,
                },
            },
            "other_objectpath": {
                "other_irrelevant_interface": {
                    "key": "value 3",
                    "bool": None,
                },
            },
        }
        expected_clients_and_properties = {
            "objectpath": {
                "key": "value",
                "bool": True,
            }
        }
        self.bus.call_method_return = managed_objects
        ret = self.bus.get_clients_and_properties()
        self.assertDictEqual(expected_clients_and_properties, ret)
        expected_call = ("GetManagedObjects", self.bus._name,
                         self.bus._server_path,
                         "org.freedesktop.DBus.ObjectManager", ())
        self.assertIn(expected_call, self.bus.calls)

    def test_call_server_method(self):
        ret = self.bus.call_server_method("methodname")
        self.assertIs(self.bus.call_method_return, ret)
        expected_call = ("methodname", self.bus._name,
                         self.bus._server_path,
                         self.bus._server_interface, ())
        self.assertIn(expected_call, self.bus.calls)

    def test_call_server_method_with_args(self):
        args = (Unique(), Unique())
        ret = self.bus.call_server_method("methodname", *args)
        self.assertIs(self.bus.call_method_return, ret)
        expected_call = ("methodname", self.bus._name,
                         self.bus._server_path,
                         self.bus._server_interface,
                         (args[0], args[1]))
        self.assertIn(expected_call, self.bus.calls)


class Test_dbus_python_adapter_SystemBus(TestCaseWithAssertLogs):

    def MockDBusPython_func(self, func):
        class mock_dbus_python:
            """mock dbus-python module"""
            class exceptions:
                """Pseudo-namespace"""
                class DBusException(Exception):
                    pass
            class SystemBus:
                @staticmethod
                def get_object(busname, objectpath):
                    DBusObject = collections.namedtuple(
                        "DBusObject", ("methodname", "Set"))
                    def method(*args, **kwargs):
                        self.assertEqual({"dbus_interface":
                                          "interface"},
                                         kwargs)
                        return func(*args)
                    def set_property(interface, key, value,
                                     dbus_interface=None):
                        self.assertEqual(
                            "org.freedesktop.DBus.Properties",
                            dbus_interface)
                        self.assertEqual("Secret", key)
                        return func(interface, key, value,
                                    dbus_interface=dbus_interface)
                    return DBusObject(methodname=method,
                                      Set=set_property)
            class Boolean:
                def __init__(self, value):
                    self.value = bool(value)
                def __bool__(self):
                    return self.value
                if sys.version_info.major == 2:
                    __nonzero__ = __bool__
            class ObjectPath(str):
                pass
            class Dictionary(dict):
                pass
            class ByteArray(bytes):
                pass
        return mock_dbus_python

    def call_method(self, bus, methodname, busname, objectpath,
                    interface, *args):
        with self.assertLogs(log, logging.DEBUG):
            return bus.call_method(methodname, busname, objectpath,
                                   interface, *args)

    def test_call_method_returns(self):
        expected_method_return = Unique()
        method_args = (Unique(), Unique())
        def func(*args):
            self.assertEqual(len(method_args), len(args))
            for marg, arg in zip(method_args, args):
                self.assertIs(marg, arg)
            return expected_method_return
        mock_dbus_python = self.MockDBusPython_func(func)
        bus = dbus_python_adapter.SystemBus(mock_dbus_python)
        ret = self.call_method(bus, "methodname", "busname",
                               "objectpath", "interface",
                               *method_args)
        self.assertIs(ret, expected_method_return)

    def test_call_method_filters_bool_true(self):
        def func():
            return method_return
        mock_dbus_python = self.MockDBusPython_func(func)
        bus = dbus_python_adapter.SystemBus(mock_dbus_python)
        method_return = mock_dbus_python.Boolean(True)
        ret = self.call_method(bus, "methodname", "busname",
                               "objectpath", "interface")
        self.assertTrue(ret)
        self.assertNotIsInstance(ret, mock_dbus_python.Boolean)

    def test_call_method_filters_bool_false(self):
        def func():
            return method_return
        mock_dbus_python = self.MockDBusPython_func(func)
        bus = dbus_python_adapter.SystemBus(mock_dbus_python)
        method_return = mock_dbus_python.Boolean(False)
        ret = self.call_method(bus, "methodname", "busname",
                               "objectpath", "interface")
        self.assertFalse(ret)
        self.assertNotIsInstance(ret, mock_dbus_python.Boolean)

    def test_call_method_filters_objectpath(self):
        def func():
            return method_return
        mock_dbus_python = self.MockDBusPython_func(func)
        bus = dbus_python_adapter.SystemBus(mock_dbus_python)
        method_return = mock_dbus_python.ObjectPath("objectpath")
        ret = self.call_method(bus, "methodname", "busname",
                               "objectpath", "interface")
        self.assertEqual("objectpath", ret)
        self.assertIsNot("objectpath", ret)
        self.assertNotIsInstance(ret, mock_dbus_python.ObjectPath)

    def test_call_method_filters_booleans_in_dict(self):
        def func():
            return method_return
        mock_dbus_python = self.MockDBusPython_func(func)
        bus = dbus_python_adapter.SystemBus(mock_dbus_python)
        method_return = mock_dbus_python.Dictionary(
        {mock_dbus_python.Boolean(True):
         mock_dbus_python.Boolean(False),
         mock_dbus_python.Boolean(False):
         mock_dbus_python.Boolean(True)})
        ret = self.call_method(bus, "methodname", "busname",
                               "objectpath", "interface")
        expected_method_return = {True: False,
                                  False: True}
        self.assertEqual(expected_method_return, ret)
        self.assertNotIsInstance(ret, mock_dbus_python.Dictionary)

    def test_call_method_filters_objectpaths_in_dict(self):
        def func():
            return method_return
        mock_dbus_python = self.MockDBusPython_func(func)
        bus = dbus_python_adapter.SystemBus(mock_dbus_python)
        method_return = mock_dbus_python.Dictionary(
        {mock_dbus_python.ObjectPath("objectpath_key_1"):
         mock_dbus_python.ObjectPath("objectpath_value_1"),
         mock_dbus_python.ObjectPath("objectpath_key_2"):
         mock_dbus_python.ObjectPath("objectpath_value_2")})
        ret = self.call_method(bus, "methodname", "busname",
                               "objectpath", "interface")
        expected_method_return = {str(key): str(value)
                                  for key, value in
                                  method_return.items()}
        self.assertEqual(expected_method_return, ret)
        self.assertIsInstance(ret, dict)
        self.assertNotIsInstance(ret, mock_dbus_python.Dictionary)

    def test_call_method_filters_dict_in_dict(self):
        def func():
            return method_return
        mock_dbus_python = self.MockDBusPython_func(func)
        bus = dbus_python_adapter.SystemBus(mock_dbus_python)
        method_return = mock_dbus_python.Dictionary(
        {"key1": mock_dbus_python.Dictionary({"key11": "value11",
                                              "key12": "value12"}),
         "key2": mock_dbus_python.Dictionary({"key21": "value21",
                                              "key22": "value22"})})
        ret = self.call_method(bus, "methodname", "busname",
                               "objectpath", "interface")
        expected_method_return = {
            "key1": {"key11": "value11",
                     "key12": "value12"},
            "key2": {"key21": "value21",
                     "key22": "value22"},
        }
        self.assertEqual(expected_method_return, ret)
        self.assertIsInstance(ret, dict)
        self.assertNotIsInstance(ret, mock_dbus_python.Dictionary)
        for key, value in ret.items():
            self.assertIsInstance(value, dict)
            self.assertEqual(expected_method_return[key], value)
            self.assertNotIsInstance(value,
                                     mock_dbus_python.Dictionary)

    def test_call_method_filters_dict_three_deep(self):
        def func():
            return method_return
        mock_dbus_python = self.MockDBusPython_func(func)
        bus = dbus_python_adapter.SystemBus(mock_dbus_python)
        method_return = mock_dbus_python.Dictionary(
            {"key1":
             mock_dbus_python.Dictionary(
                 {"key2":
                  mock_dbus_python.Dictionary(
                      {"key3":
                       mock_dbus_python.Boolean(True),
                       }),
                  }),
             })
        ret = self.call_method(bus, "methodname", "busname",
                               "objectpath", "interface")
        expected_method_return = {"key1": {"key2": {"key3": True}}}
        self.assertEqual(expected_method_return, ret)
        self.assertIsInstance(ret, dict)
        self.assertNotIsInstance(ret, mock_dbus_python.Dictionary)
        self.assertIsInstance(ret["key1"], dict)
        self.assertNotIsInstance(ret["key1"],
                                 mock_dbus_python.Dictionary)
        self.assertIsInstance(ret["key1"]["key2"], dict)
        self.assertNotIsInstance(ret["key1"]["key2"],
                                 mock_dbus_python.Dictionary)
        self.assertTrue(ret["key1"]["key2"]["key3"])
        self.assertNotIsInstance(ret["key1"]["key2"]["key3"],
                                 mock_dbus_python.Boolean)

    def test_call_method_handles_exception(self):
        dbus_logger = logging.getLogger("dbus.proxies")

        def func():
            dbus_logger.error("Test")
            raise mock_dbus_python.exceptions.DBusException()

        mock_dbus_python = self.MockDBusPython_func(func)
        bus = dbus_python_adapter.SystemBus(mock_dbus_python)

        class CountingHandler(logging.Handler):
            count = 0
            def emit(self, record):
                self.count += 1

        counting_handler = CountingHandler()

        dbus_logger.addHandler(counting_handler)

        try:
            with self.assertRaises(dbus.Error) as e:
                self.call_method(bus, "methodname", "busname",
                                 "objectpath", "interface")
        finally:
            dbus_logger.removeFilter(counting_handler)

        self.assertNotIsInstance(e.exception, dbus.ConnectFailed)

        # Make sure the dbus logger was suppressed
        self.assertEqual(0, counting_handler.count)

    def test_Set_Secret_sends_bytearray(self):
        ret = [None]
        def func(*args, **kwargs):
            ret[0] = (args, kwargs)
        mock_dbus_python = self.MockDBusPython_func(func)
        bus = dbus_python_adapter.SystemBus(mock_dbus_python)
        bus.set_client_property("objectpath", "Secret", "value")
        expected_call = (("se.recompile.Mandos.Client", "Secret",
                          mock_dbus_python.ByteArray(b"value")),
                         {"dbus_interface":
                          "org.freedesktop.DBus.Properties"})
        self.assertEqual(expected_call, ret[0])
        if sys.version_info.major == 2:
            self.assertIsInstance(ret[0][0][-1],
                                  mock_dbus_python.ByteArray)

    def test_get_object_converts_to_correct_exception(self):
        bus = dbus_python_adapter.SystemBus(
            self.fake_dbus_python_raises_exception_on_connect)
        with self.assertRaises(dbus.ConnectFailed):
            self.call_method(bus, "methodname", "busname",
                             "objectpath", "interface")

    class fake_dbus_python_raises_exception_on_connect:
        """fake dbus-python module"""
        class exceptions:
            """Pseudo-namespace"""
            class DBusException(Exception):
                pass

        @classmethod
        def SystemBus(cls):
            def get_object(busname, objectpath):
                raise cls.exceptions.DBusException()
            Bus = collections.namedtuple("Bus", ["get_object"])
            return Bus(get_object=get_object)


class Test_dbus_python_adapter_CachingBus(unittest.TestCase):
    class mock_dbus_python:
        """mock dbus-python modules"""
        class SystemBus:
            @staticmethod
            def get_object(busname, objectpath):
                return Unique()

    def setUp(self):
        self.bus = dbus_python_adapter.CachingBus(
            self.mock_dbus_python)

    def test_returns_distinct_objectpaths(self):
        obj1 = self.bus.get_object("busname", "objectpath1")
        self.assertIsInstance(obj1, Unique)
        obj2 = self.bus.get_object("busname", "objectpath2")
        self.assertIsInstance(obj2, Unique)
        self.assertIsNot(obj1, obj2)

    def test_returns_distinct_busnames(self):
        obj1 = self.bus.get_object("busname1", "objectpath")
        self.assertIsInstance(obj1, Unique)
        obj2 = self.bus.get_object("busname2", "objectpath")
        self.assertIsInstance(obj2, Unique)
        self.assertIsNot(obj1, obj2)

    def test_returns_distinct_both(self):
        obj1 = self.bus.get_object("busname1", "objectpath")
        self.assertIsInstance(obj1, Unique)
        obj2 = self.bus.get_object("busname2", "objectpath")
        self.assertIsInstance(obj2, Unique)
        self.assertIsNot(obj1, obj2)

    def test_returns_same(self):
        obj1 = self.bus.get_object("busname", "objectpath")
        self.assertIsInstance(obj1, Unique)
        obj2 = self.bus.get_object("busname", "objectpath")
        self.assertIsInstance(obj2, Unique)
        self.assertIs(obj1, obj2)

    def test_returns_same_old(self):
        obj1 = self.bus.get_object("busname1", "objectpath1")
        self.assertIsInstance(obj1, Unique)
        obj2 = self.bus.get_object("busname2", "objectpath2")
        self.assertIsInstance(obj2, Unique)
        obj1b = self.bus.get_object("busname1", "objectpath1")
        self.assertIsInstance(obj1b, Unique)
        self.assertIsNot(obj1, obj2)
        self.assertIsNot(obj2, obj1b)
        self.assertIs(obj1, obj1b)


class Test_pydbus_adapter_SystemBus(TestCaseWithAssertLogs):

    def Stub_pydbus_func(self, func):
        class stub_pydbus:
            """stub pydbus module"""
            class SystemBus:
                @staticmethod
                def get(busname, objectpath):
                    DBusObject = collections.namedtuple(
                        "DBusObject", ("methodname",))
                    return {"interface":
                            DBusObject(methodname=func)}
        return stub_pydbus

    def call_method(self, bus, methodname, busname, objectpath,
                    interface, *args):
        with self.assertLogs(log, logging.DEBUG):
            return bus.call_method(methodname, busname, objectpath,
                                   interface, *args)

    def test_call_method_returns(self):
        expected_method_return = Unique()
        method_args = (Unique(), Unique())
        def func(*args):
            self.assertEqual(len(method_args), len(args))
            for marg, arg in zip(method_args, args):
                self.assertIs(marg, arg)
            return expected_method_return
        stub_pydbus = self.Stub_pydbus_func(func)
        bus = pydbus_adapter.SystemBus(stub_pydbus)
        ret = self.call_method(bus, "methodname", "busname",
                               "objectpath", "interface",
                               *method_args)
        self.assertIs(ret, expected_method_return)

    def test_call_method_handles_exception(self):
        def func():
            raise gi.repository.GLib.Error()

        stub_pydbus = self.Stub_pydbus_func(func)
        bus = pydbus_adapter.SystemBus(stub_pydbus)

        with self.assertRaises(dbus.Error) as e:
            self.call_method(bus, "methodname", "busname",
                             "objectpath", "interface")

        self.assertNotIsInstance(e.exception, dbus.ConnectFailed)

    def test_get_converts_to_correct_exception(self):
        bus = pydbus_adapter.SystemBus(
            self.fake_pydbus_raises_exception_on_connect)
        with self.assertRaises(dbus.ConnectFailed):
            self.call_method(bus, "methodname", "busname",
                             "objectpath", "interface")

    class fake_pydbus_raises_exception_on_connect:
        """fake dbus-python module"""
        @classmethod
        def SystemBus(cls):
            def get(busname, objectpath):
                raise gi.repository.GLib.Error()
            Bus = collections.namedtuple("Bus", ["get"])
            return Bus(get=get)

    def test_set_property_uses_setattr(self):
        class Object:
            pass
        obj = Object()
        class pydbus_spy:
            class SystemBus:
                @staticmethod
                def get(busname, objectpath):
                    return {"interface": obj}
        bus = pydbus_adapter.SystemBus(pydbus_spy)
        value = Unique()
        bus.set_property("busname", "objectpath", "interface", "key",
                         value)
        self.assertIs(value, obj.key)

    def test_get_suppresses_xml_deprecation_warning(self):
        if sys.version_info.major >= 3:
            return
        class stub_pydbus_get:
            class SystemBus:
                @staticmethod
                def get(busname, objectpath):
                    warnings.warn_explicit(
                        "deprecated", DeprecationWarning,
                        "xml.etree.ElementTree", 0)
        bus = pydbus_adapter.SystemBus(stub_pydbus_get)
        with warnings.catch_warnings(record=True) as w:
            warnings.simplefilter("always")
            bus.get("busname", "objectpath")
            self.assertEqual(0, len(w))


class Test_pydbus_adapter_CachingBus(unittest.TestCase):
    class stub_pydbus:
        """stub pydbus module"""
        class SystemBus:
            @staticmethod
            def get(busname, objectpath):
                return Unique()

    def setUp(self):
        self.bus = pydbus_adapter.CachingBus(self.stub_pydbus)

    def test_returns_distinct_objectpaths(self):
        obj1 = self.bus.get("busname", "objectpath1")
        self.assertIsInstance(obj1, Unique)
        obj2 = self.bus.get("busname", "objectpath2")
        self.assertIsInstance(obj2, Unique)
        self.assertIsNot(obj1, obj2)

    def test_returns_distinct_busnames(self):
        obj1 = self.bus.get("busname1", "objectpath")
        self.assertIsInstance(obj1, Unique)
        obj2 = self.bus.get("busname2", "objectpath")
        self.assertIsInstance(obj2, Unique)
        self.assertIsNot(obj1, obj2)

    def test_returns_distinct_both(self):
        obj1 = self.bus.get("busname1", "objectpath")
        self.assertIsInstance(obj1, Unique)
        obj2 = self.bus.get("busname2", "objectpath")
        self.assertIsInstance(obj2, Unique)
        self.assertIsNot(obj1, obj2)

    def test_returns_same(self):
        obj1 = self.bus.get("busname", "objectpath")
        self.assertIsInstance(obj1, Unique)
        obj2 = self.bus.get("busname", "objectpath")
        self.assertIsInstance(obj2, Unique)
        self.assertIs(obj1, obj2)

    def test_returns_same_old(self):
        obj1 = self.bus.get("busname1", "objectpath1")
        self.assertIsInstance(obj1, Unique)
        obj2 = self.bus.get("busname2", "objectpath2")
        self.assertIsInstance(obj2, Unique)
        obj1b = self.bus.get("busname1", "objectpath1")
        self.assertIsInstance(obj1b, Unique)
        self.assertIsNot(obj1, obj2)
        self.assertIsNot(obj2, obj1b)
        self.assertIs(obj1, obj1b)


class Test_dbussy_adapter_SystemBus(TestCaseWithAssertLogs):

    class dummy_dbussy:
        class DBUS:
            class ObjectPath(str):
                pass
        class DBusError(Exception):
            pass

    def fake_ravel_func(self, func):
        class fake_ravel:
            @staticmethod
            def system_bus():
                class DBusInterfaceProxy:
                    @staticmethod
                    def methodname(*args):
                        return [func(*args)]
                class DBusObject:
                    @staticmethod
                    def get_interface(interface):
                        if interface == "interface":
                            return DBusInterfaceProxy()
                return {"busname": {"objectpath": DBusObject()}}
        return fake_ravel

    def call_method(self, bus, methodname, busname, objectpath,
                    interface, *args):
        with self.assertLogs(log, logging.DEBUG):
            return bus.call_method(methodname, busname, objectpath,
                                   interface, *args)

    def test_call_method_returns(self):
        expected_method_return = Unique()
        method_args = (Unique(), Unique())
        def func(*args):
            self.assertEqual(len(method_args), len(args))
            for marg, arg in zip(method_args, args):
                self.assertIs(marg, arg)
            return expected_method_return
        fake_ravel = self.fake_ravel_func(func)
        bus = dbussy_adapter.SystemBus(self.dummy_dbussy, fake_ravel)
        ret = self.call_method(bus, "methodname", "busname",
                               "objectpath", "interface",
                               *method_args)
        self.assertIs(ret, expected_method_return)

    def test_call_method_filters_objectpath(self):
        def func():
            return method_return
        fake_ravel = self.fake_ravel_func(func)
        bus = dbussy_adapter.SystemBus(self.dummy_dbussy, fake_ravel)
        method_return = (self.dummy_dbussy.DBUS
                         .ObjectPath("objectpath"))
        ret = self.call_method(bus, "methodname", "busname",
                               "objectpath", "interface")
        self.assertEqual("objectpath", ret)
        self.assertNotIsInstance(ret,
                                 self.dummy_dbussy.DBUS.ObjectPath)

    def test_call_method_filters_objectpaths_in_dict(self):
        ObjectPath = self.dummy_dbussy.DBUS.ObjectPath
        def func():
            return method_return
        fake_ravel = self.fake_ravel_func(func)
        bus = dbussy_adapter.SystemBus(self.dummy_dbussy, fake_ravel)
        method_return = {
            ObjectPath("objectpath_key_1"):
            ObjectPath("objectpath_value_1"),
            ObjectPath("objectpath_key_2"):
            ObjectPath("objectpath_value_2"),
        }
        ret = self.call_method(bus, "methodname", "busname",
                               "objectpath", "interface")
        expected_method_return = {str(key): str(value)
                                  for key, value in
                                  method_return.items()}
        for key, value in ret.items():
            self.assertNotIsInstance(key, ObjectPath)
            self.assertNotIsInstance(value, ObjectPath)
        self.assertEqual(expected_method_return, ret)
        self.assertIsInstance(ret, dict)

    def test_call_method_filters_objectpaths_in_dict_in_dict(self):
        ObjectPath = self.dummy_dbussy.DBUS.ObjectPath
        def func():
            return method_return
        fake_ravel = self.fake_ravel_func(func)
        bus = dbussy_adapter.SystemBus(self.dummy_dbussy, fake_ravel)
        method_return = {
            ObjectPath("key1"): {
                ObjectPath("key11"): ObjectPath("value11"),
                ObjectPath("key12"): ObjectPath("value12"),
            },
            ObjectPath("key2"): {
                ObjectPath("key21"): ObjectPath("value21"),
                ObjectPath("key22"): ObjectPath("value22"),
            },
        }
        ret = self.call_method(bus, "methodname", "busname",
                               "objectpath", "interface")
        expected_method_return = {
            "key1": {"key11": "value11",
                     "key12": "value12"},
            "key2": {"key21": "value21",
                     "key22": "value22"},
        }
        self.assertEqual(expected_method_return, ret)
        for key, value in ret.items():
            self.assertIsInstance(value, dict)
            self.assertEqual(expected_method_return[key], value)
            self.assertNotIsInstance(key, ObjectPath)
            for inner_key, inner_value in value.items():
                self.assertIsInstance(value, dict)
                self.assertEqual(
                    expected_method_return[key][inner_key],
                    inner_value)
                self.assertNotIsInstance(key, ObjectPath)

    def test_call_method_filters_objectpaths_in_dict_three_deep(self):
        ObjectPath = self.dummy_dbussy.DBUS.ObjectPath
        def func():
            return method_return
        fake_ravel = self.fake_ravel_func(func)
        bus = dbussy_adapter.SystemBus(self.dummy_dbussy, fake_ravel)
        method_return = {
            ObjectPath("key1"): {
                ObjectPath("key2"): {
                    ObjectPath("key3"): ObjectPath("value"),
                },
            },
        }
        ret = self.call_method(bus, "methodname", "busname",
                               "objectpath", "interface")
        expected_method_return = {"key1": {"key2": {"key3": "value"}}}
        self.assertEqual(expected_method_return, ret)
        self.assertIsInstance(ret, dict)
        self.assertNotIsInstance(next(iter(ret.keys())), ObjectPath)
        self.assertIsInstance(ret["key1"], dict)
        self.assertNotIsInstance(next(iter(ret["key1"].keys())),
                                 ObjectPath)
        self.assertIsInstance(ret["key1"]["key2"], dict)
        self.assertNotIsInstance(
            next(iter(ret["key1"]["key2"].keys())),
            ObjectPath)
        self.assertEqual("value", ret["key1"]["key2"]["key3"])
        self.assertNotIsInstance(ret["key1"]["key2"]["key3"],
                                 self.dummy_dbussy.DBUS.ObjectPath)

    def test_call_method_handles_exception(self):
        def func():
            raise self.dummy_dbussy.DBusError()

        fake_ravel = self.fake_ravel_func(func)
        bus = dbussy_adapter.SystemBus(self.dummy_dbussy, fake_ravel)

        with self.assertRaises(dbus.Error) as e:
            self.call_method(bus, "methodname", "busname",
                             "objectpath", "interface")

        self.assertNotIsInstance(e.exception, dbus.ConnectFailed)

    def test_get_object_converts_to_correct_exception(self):
        class fake_ravel_raises_exception_on_connect:
            @staticmethod
            def system_bus():
                class Bus:
                    @staticmethod
                    def __getitem__(key):
                        if key == "objectpath":
                            raise self.dummy_dbussy.DBusError()
                        raise Exception(key)
                return {"busname": Bus()}
        def func():
            raise self.dummy_dbussy.DBusError()
        bus = dbussy_adapter.SystemBus(
            self.dummy_dbussy,
            fake_ravel_raises_exception_on_connect)
        with self.assertRaises(dbus.ConnectFailed):
            self.call_method(bus, "methodname", "busname",
                             "objectpath", "interface")


class Test_commands_from_options(unittest.TestCase):

    def setUp(self):
        self.parser = argparse.ArgumentParser()
        add_command_line_options(self.parser)

    def test_is_enabled(self):
        self.assert_command_from_args(["--is-enabled", "client"],
                                      command.IsEnabled)

    def assert_command_from_args(self, args, command_cls, length=1,
                                 clients=None, **cmd_attrs):
        """Assert that parsing ARGS should result in an instance of
COMMAND_CLS with (optionally) all supplied attributes (CMD_ATTRS)."""
        options = self.parser.parse_args(args)
        check_option_syntax(self.parser, options)
        commands = commands_from_options(options)
        self.assertEqual(length, len(commands))
        for command in commands:
            if isinstance(command, command_cls):
                break
        else:
            self.assertIsInstance(command, command_cls)
        if clients is not None:
            self.assertEqual(clients, options.client)
        for key, value in cmd_attrs.items():
            self.assertEqual(value, getattr(command, key))

    def assert_commands_from_args(self, args, commands, clients=None):
        for cmd in commands:
            self.assert_command_from_args(args, cmd,
                                          length=len(commands),
                                          clients=clients)

    def test_is_enabled_short(self):
        self.assert_command_from_args(["-V", "client"],
                                      command.IsEnabled)

    def test_approve(self):
        self.assert_command_from_args(["--approve", "client"],
                                      command.Approve)

    def test_approve_short(self):
        self.assert_command_from_args(["-A", "client"],
                                      command.Approve)

    def test_deny(self):
        self.assert_command_from_args(["--deny", "client"],
                                      command.Deny)

    def test_deny_short(self):
        self.assert_command_from_args(["-D", "client"], command.Deny)

    def test_remove(self):
        self.assert_command_from_args(["--remove", "client"],
                                      command.Remove)

    def test_deny_before_remove(self):
        options = self.parser.parse_args(["--deny", "--remove",
                                          "client"])
        check_option_syntax(self.parser, options)
        commands = commands_from_options(options)
        self.assertEqual(2, len(commands))
        self.assertIsInstance(commands[0], command.Deny)
        self.assertIsInstance(commands[1], command.Remove)

    def test_deny_before_remove_reversed(self):
        options = self.parser.parse_args(["--remove", "--deny",
                                          "--all"])
        check_option_syntax(self.parser, options)
        commands = commands_from_options(options)
        self.assertEqual(2, len(commands))
        self.assertIsInstance(commands[0], command.Deny)
        self.assertIsInstance(commands[1], command.Remove)

    def test_remove_short(self):
        self.assert_command_from_args(["-r", "client"],
                                      command.Remove)

    def test_dump_json(self):
        self.assert_command_from_args(["--dump-json"],
                                      command.DumpJSON)

    def test_enable(self):
        self.assert_command_from_args(["--enable", "client"],
                                      command.Enable)

    def test_enable_short(self):
        self.assert_command_from_args(["-e", "client"],
                                      command.Enable)

    def test_disable(self):
        self.assert_command_from_args(["--disable", "client"],
                                      command.Disable)

    def test_disable_short(self):
        self.assert_command_from_args(["-d", "client"],
                                      command.Disable)

    def test_bump_timeout(self):
        self.assert_command_from_args(["--bump-timeout", "client"],
                                      command.BumpTimeout)

    def test_bump_timeout_short(self):
        self.assert_command_from_args(["-b", "client"],
                                      command.BumpTimeout)

    def test_start_checker(self):
        self.assert_command_from_args(["--start-checker", "client"],
                                      command.StartChecker)

    def test_stop_checker(self):
        self.assert_command_from_args(["--stop-checker", "client"],
                                      command.StopChecker)

    def test_approve_by_default(self):
        self.assert_command_from_args(["--approve-by-default",
                                       "client"],
                                      command.ApproveByDefault)

    def test_deny_by_default(self):
        self.assert_command_from_args(["--deny-by-default", "client"],
                                      command.DenyByDefault)

    def test_checker(self):
        self.assert_command_from_args(["--checker", ":", "client"],
                                      command.SetChecker,
                                      value_to_set=":")

    def test_checker_empty(self):
        self.assert_command_from_args(["--checker", "", "client"],
                                      command.SetChecker,
                                      value_to_set="")

    def test_checker_short(self):
        self.assert_command_from_args(["-c", ":", "client"],
                                      command.SetChecker,
                                      value_to_set=":")

    def test_host(self):
        self.assert_command_from_args(
            ["--host", "client.example.org", "client"],
            command.SetHost, value_to_set="client.example.org")

    def test_host_short(self):
        self.assert_command_from_args(
            ["-H", "client.example.org", "client"], command.SetHost,
            value_to_set="client.example.org")

    def test_secret_devnull(self):
        self.assert_command_from_args(["--secret", os.path.devnull,
                                       "client"], command.SetSecret,
                                      value_to_set=b"")

    def test_secret_tempfile(self):
        with tempfile.NamedTemporaryFile(mode="r+b") as f:
            value = b"secret\0xyzzy\nbar"
            f.write(value)
            f.seek(0)
            self.assert_command_from_args(["--secret", f.name,
                                           "client"],
                                          command.SetSecret,
                                          value_to_set=value)

    def test_secret_devnull_short(self):
        self.assert_command_from_args(["-s", os.path.devnull,
                                       "client"], command.SetSecret,
                                      value_to_set=b"")

    def test_secret_tempfile_short(self):
        with tempfile.NamedTemporaryFile(mode="r+b") as f:
            value = b"secret\0xyzzy\nbar"
            f.write(value)
            f.seek(0)
            self.assert_command_from_args(["-s", f.name, "client"],
                                          command.SetSecret,
                                          value_to_set=value)

    def test_timeout(self):
        self.assert_command_from_args(["--timeout", "PT5M", "client"],
                                      command.SetTimeout,
                                      value_to_set=300000)

    def test_timeout_short(self):
        self.assert_command_from_args(["-t", "PT5M", "client"],
                                      command.SetTimeout,
                                      value_to_set=300000)

    def test_extended_timeout(self):
        self.assert_command_from_args(["--extended-timeout", "PT15M",
                                       "client"],
                                      command.SetExtendedTimeout,
                                      value_to_set=900000)

    def test_interval(self):
        self.assert_command_from_args(["--interval", "PT2M",
                                       "client"], command.SetInterval,
                                      value_to_set=120000)

    def test_interval_short(self):
        self.assert_command_from_args(["-i", "PT2M", "client"],
                                      command.SetInterval,
                                      value_to_set=120000)

    def test_approval_delay(self):
        self.assert_command_from_args(["--approval-delay", "PT30S",
                                       "client"],
                                      command.SetApprovalDelay,
                                      value_to_set=30000)

    def test_approval_duration(self):
        self.assert_command_from_args(["--approval-duration", "PT1S",
                                       "client"],
                                      command.SetApprovalDuration,
                                      value_to_set=1000)

    def test_print_table(self):
        self.assert_command_from_args([], command.PrintTable,
                                      verbose=False)

    def test_print_table_verbose(self):
        self.assert_command_from_args(["--verbose"],
                                      command.PrintTable,
                                      verbose=True)

    def test_print_table_verbose_short(self):
        self.assert_command_from_args(["-v"], command.PrintTable,
                                      verbose=True)


    def test_manual_page_example_1(self):
        self.assert_command_from_args("",
                                      command.PrintTable,
                                      clients=[],
                                      verbose=False)

    def test_manual_page_example_2(self):
        self.assert_command_from_args(
            "--verbose foo1.example.org foo2.example.org".split(),
            command.PrintTable, clients=["foo1.example.org",
                                         "foo2.example.org"],
            verbose=True)

    def test_manual_page_example_3(self):
        self.assert_command_from_args("--enable --all".split(),
                                      command.Enable,
                                      clients=[])

    def test_manual_page_example_4(self):
        self.assert_commands_from_args(
            ("--timeout=PT5M --interval=PT1M foo1.example.org"
             " foo2.example.org").split(),
            [command.SetTimeout, command.SetInterval],
            clients=["foo1.example.org", "foo2.example.org"])

    def test_manual_page_example_5(self):
        self.assert_command_from_args("--approve --all".split(),
                                      command.Approve,
                                      clients=[])


class TestCommand(unittest.TestCase):
    """Abstract class for tests of command classes"""

    class FakeMandosBus(dbus.MandosBus):
        def __init__(self, testcase):
            self.client_properties = {
                "Name": "foo",
                "KeyID": ("92ed150794387c03ce684574b1139a65"
                          "94a34f895daaaf09fd8ea90a27cddb12"),
                "Secret": b"secret",
                "Host": "foo.example.org",
                "Enabled": True,
                "Timeout": 300000,
                "LastCheckedOK": "2019-02-03T00:00:00",
                "Created": "2019-01-02T00:00:00",
                "Interval": 120000,
                "Fingerprint": ("778827225BA7DE539C5A"
                                "7CFA59CFF7CDBD9A5920"),
                "CheckerRunning": False,
                "LastEnabled": "2019-01-03T00:00:00",
                "ApprovalPending": False,
                "ApprovedByDefault": True,
                "LastApprovalRequest": "",
                "ApprovalDelay": 0,
                "ApprovalDuration": 1000,
                "Checker": "fping -q -- %(host)s",
                "ExtendedTimeout": 900000,
                "Expires": "2019-02-04T00:00:00",
                "LastCheckerStatus": 0,
            }
            self.other_client_properties = {
                "Name": "barbar",
                "KeyID": ("0558568eedd67d622f5c83b35a115f79"
                          "6ab612cff5ad227247e46c2b020f441c"),
                "Secret": b"secretbar",
                "Host": "192.0.2.3",
                "Enabled": True,
                "Timeout": 300000,
                "LastCheckedOK": "2019-02-04T00:00:00",
                "Created": "2019-01-03T00:00:00",
                "Interval": 120000,
                "Fingerprint": ("3E393AEAEFB84C7E89E2"
                                "F547B3A107558FCA3A27"),
                "CheckerRunning": True,
                "LastEnabled": "2019-01-04T00:00:00",
                "ApprovalPending": False,
                "ApprovedByDefault": False,
                "LastApprovalRequest": "2019-01-03T00:00:00",
                "ApprovalDelay": 30000,
                "ApprovalDuration": 93785000,
                "Checker": ":",
                "ExtendedTimeout": 900000,
                "Expires": "2019-02-05T00:00:00",
                "LastCheckerStatus": -2,
            }
            self.clients =  collections.OrderedDict(
                [
                    ("client_objectpath", self.client_properties),
                    ("other_client_objectpath",
                     self.other_client_properties),
                ])
            self.one_client = {"client_objectpath":
                               self.client_properties}
            self.testcase = testcase
            self.calls = []

        def call_method(self, methodname, busname, objectpath,
                        interface, *args):
            self.testcase.assertEqual("se.recompile.Mandos", busname)
            self.calls.append((methodname, busname, objectpath,
                               interface, args))
            if interface == "org.freedesktop.DBus.Properties":
                if methodname == "Set":
                    self.testcase.assertEqual(3, len(args))
                    interface, key, value = args
                    self.testcase.assertEqual(
                        "se.recompile.Mandos.Client", interface)
                    self.clients[objectpath][key] = value
                    return
            elif interface == "se.recompile.Mandos":
                self.testcase.assertEqual("RemoveClient", methodname)
                self.testcase.assertEqual(1, len(args))
                clientpath = args[0]
                del self.clients[clientpath]
                return
            elif interface == "se.recompile.Mandos.Client":
                if methodname == "Approve":
                    self.testcase.assertEqual(1, len(args))
                    return
            raise ValueError()

    def setUp(self):
        self.bus = self.FakeMandosBus(self)


class TestBaseCommands(TestCommand):

    def test_IsEnabled_exits_successfully(self):
        with self.assertRaises(SystemExit) as e:
            command.IsEnabled().run(self.bus.one_client)
        if e.exception.code is not None:
            self.assertEqual(0, e.exception.code)
        else:
            self.assertIsNone(e.exception.code)

    def test_IsEnabled_exits_with_failure(self):
        self.bus.client_properties["Enabled"] = False
        with self.assertRaises(SystemExit) as e:
            command.IsEnabled().run(self.bus.one_client)
        if isinstance(e.exception.code, int):
            self.assertNotEqual(0, e.exception.code)
        else:
            self.assertIsNotNone(e.exception.code)

    def test_Approve(self):
        busname = "se.recompile.Mandos"
        client_interface = "se.recompile.Mandos.Client"
        command.Approve().run(self.bus.clients, self.bus)
        self.assertTrue(self.bus.clients)
        for clientpath in self.bus.clients:
            self.assertIn(("Approve", busname, clientpath,
                           client_interface, (True,)), self.bus.calls)

    def test_Deny(self):
        busname = "se.recompile.Mandos"
        client_interface = "se.recompile.Mandos.Client"
        command.Deny().run(self.bus.clients, self.bus)
        self.assertTrue(self.bus.clients)
        for clientpath in self.bus.clients:
            self.assertIn(("Approve", busname, clientpath,
                           client_interface, (False,)),
                          self.bus.calls)

    def test_Remove(self):
        busname = "se.recompile.Mandos"
        server_path = "/"
        server_interface = "se.recompile.Mandos"
        orig_clients = self.bus.clients.copy()
        command.Remove().run(self.bus.clients, self.bus)
        self.assertFalse(self.bus.clients)
        for clientpath in orig_clients:
            self.assertIn(("RemoveClient", busname,
                           server_path, server_interface,
                           (clientpath,)), self.bus.calls)

    expected_json = {
        "foo": {
            "Name": "foo",
            "KeyID": ("92ed150794387c03ce684574b1139a65"
                      "94a34f895daaaf09fd8ea90a27cddb12"),
            "Host": "foo.example.org",
            "Enabled": True,
            "Timeout": 300000,
            "LastCheckedOK": "2019-02-03T00:00:00",
            "Created": "2019-01-02T00:00:00",
            "Interval": 120000,
            "Fingerprint": ("778827225BA7DE539C5A"
                            "7CFA59CFF7CDBD9A5920"),
            "CheckerRunning": False,
            "LastEnabled": "2019-01-03T00:00:00",
            "ApprovalPending": False,
            "ApprovedByDefault": True,
            "LastApprovalRequest": "",
            "ApprovalDelay": 0,
            "ApprovalDuration": 1000,
            "Checker": "fping -q -- %(host)s",
            "ExtendedTimeout": 900000,
            "Expires": "2019-02-04T00:00:00",
            "LastCheckerStatus": 0,
        },
        "barbar": {
            "Name": "barbar",
            "KeyID": ("0558568eedd67d622f5c83b35a115f79"
                      "6ab612cff5ad227247e46c2b020f441c"),
            "Host": "192.0.2.3",
            "Enabled": True,
            "Timeout": 300000,
            "LastCheckedOK": "2019-02-04T00:00:00",
            "Created": "2019-01-03T00:00:00",
            "Interval": 120000,
            "Fingerprint": ("3E393AEAEFB84C7E89E2"
                            "F547B3A107558FCA3A27"),
            "CheckerRunning": True,
            "LastEnabled": "2019-01-04T00:00:00",
            "ApprovalPending": False,
            "ApprovedByDefault": False,
            "LastApprovalRequest": "2019-01-03T00:00:00",
            "ApprovalDelay": 30000,
            "ApprovalDuration": 93785000,
            "Checker": ":",
            "ExtendedTimeout": 900000,
            "Expires": "2019-02-05T00:00:00",
            "LastCheckerStatus": -2,
        },
    }

    def test_DumpJSON_normal(self):
        with self.capture_stdout_to_buffer() as buffer:
            command.DumpJSON().run(self.bus.clients)
        json_data = json.loads(buffer.getvalue())
        self.assertDictEqual(self.expected_json, json_data)

    @staticmethod
    @contextlib.contextmanager
    def capture_stdout_to_buffer():
        capture_buffer = io.StringIO()
        old_stdout = sys.stdout
        sys.stdout = capture_buffer
        try:
            yield capture_buffer
        finally:
            sys.stdout = old_stdout

    def test_DumpJSON_one_client(self):
        with self.capture_stdout_to_buffer() as buffer:
            command.DumpJSON().run(self.bus.one_client)
        json_data = json.loads(buffer.getvalue())
        expected_json = {"foo": self.expected_json["foo"]}
        self.assertDictEqual(expected_json, json_data)

    def test_PrintTable_normal(self):
        with self.capture_stdout_to_buffer() as buffer:
            command.PrintTable().run(self.bus.clients)
        expected_output = "\n".join((
            "Name   Enabled Timeout  Last Successful Check",
            "foo    Yes     00:05:00 2019-02-03T00:00:00  ",
            "barbar Yes     00:05:00 2019-02-04T00:00:00  ",
        )) + "\n"
        self.assertEqual(expected_output, buffer.getvalue())

    def test_PrintTable_verbose(self):
        with self.capture_stdout_to_buffer() as buffer:
            command.PrintTable(verbose=True).run(self.bus.clients)
        columns = (
            (
                "Name   ",
                "foo    ",
                "barbar ",
            ),(
                "Enabled ",
                "Yes     ",
                "Yes     ",
            ),(
                "Timeout  ",
                "00:05:00 ",
                "00:05:00 ",
            ),(
                "Last Successful Check ",
                "2019-02-03T00:00:00   ",
                "2019-02-04T00:00:00   ",
            ),(
                "Created             ",
                "2019-01-02T00:00:00 ",
                "2019-01-03T00:00:00 ",
            ),(
                "Interval ",
                "00:02:00 ",
                "00:02:00 ",
            ),(
                "Host            ",
                "foo.example.org ",
                "192.0.2.3       ",
            ),(
                ("Key ID                                             "
                 "              "),
                ("92ed150794387c03ce684574b1139a6594a34f895daaaf09fd8"
                 "ea90a27cddb12 "),
                ("0558568eedd67d622f5c83b35a115f796ab612cff5ad227247e"
                 "46c2b020f441c "),
            ),(
                "Fingerprint                              ",
                "778827225BA7DE539C5A7CFA59CFF7CDBD9A5920 ",
                "3E393AEAEFB84C7E89E2F547B3A107558FCA3A27 ",
            ),(
                "Check Is Running ",
                "No               ",
                "Yes              ",
            ),(
                "Last Enabled        ",
                "2019-01-03T00:00:00 ",
                "2019-01-04T00:00:00 ",
            ),(
                "Approval Is Pending ",
                "No                  ",
                "No                  ",
            ),(
                "Approved By Default ",
                "Yes                 ",
                "No                  ",
            ),(
                "Last Approval Request ",
                "                      ",
                "2019-01-03T00:00:00   ",
            ),(
                "Approval Delay ",
                "00:00:00       ",
                "00:00:30       ",
            ),(
                "Approval Duration ",
                "00:00:01          ",
                "1T02:03:05        ",
            ),(
                "Checker              ",
                "fping -q -- %(host)s ",
                ":                    ",
            ),(
                "Extended Timeout ",
                "00:15:00         ",
                "00:15:00         ",
            ),(
                "Expires             ",
                "2019-02-04T00:00:00 ",
                "2019-02-05T00:00:00 ",
            ),(
                "Last Checker Status",
                "0                  ",
                "-2                 ",
            )
        )
        num_lines = max(len(rows) for rows in columns)
        expected_output = ("\n".join("".join(rows[line]
                                             for rows in columns)
                                     for line in range(num_lines))
                           + "\n")
        self.assertEqual(expected_output, buffer.getvalue())

    def test_PrintTable_one_client(self):
        with self.capture_stdout_to_buffer() as buffer:
            command.PrintTable().run(self.bus.one_client)
        expected_output = "\n".join((
            "Name Enabled Timeout  Last Successful Check",
            "foo  Yes     00:05:00 2019-02-03T00:00:00  ",
        )) + "\n"
        self.assertEqual(expected_output, buffer.getvalue())


class TestPropertySetterCmd(TestCommand):
    """Abstract class for tests of command.PropertySetter classes"""

    def runTest(self):
        if not hasattr(self, "command"):
            return              # Abstract TestCase class

        if hasattr(self, "values_to_set"):
            cmd_args = [(value,) for value in self.values_to_set]
            values_to_get = getattr(self, "values_to_get",
                                    self.values_to_set)
        else:
            cmd_args = [() for x in range(len(self.values_to_get))]
            values_to_get = self.values_to_get
        self.assertTrue(values_to_get)
        for value_to_get, cmd_arg in zip(values_to_get, cmd_args):
            for clientpath in self.bus.clients:
                self.bus.clients[clientpath][self.propname] = (
                    Unique())
            self.command(*cmd_arg).run(self.bus.clients, self.bus)
            self.assertTrue(self.bus.clients)
            for clientpath in self.bus.clients:
                value = (self.bus.clients[clientpath]
                         [self.propname])
                self.assertNotIsInstance(value, Unique)
                self.assertEqual(value_to_get, value)


class TestEnableCmd(TestPropertySetterCmd):
    command = command.Enable
    propname = "Enabled"
    values_to_get = [True]


class TestDisableCmd(TestPropertySetterCmd):
    command = command.Disable
    propname = "Enabled"
    values_to_get = [False]


class TestBumpTimeoutCmd(TestPropertySetterCmd):
    command = command.BumpTimeout
    propname = "LastCheckedOK"
    values_to_get = [""]


class TestStartCheckerCmd(TestPropertySetterCmd):
    command = command.StartChecker
    propname = "CheckerRunning"
    values_to_get = [True]


class TestStopCheckerCmd(TestPropertySetterCmd):
    command = command.StopChecker
    propname = "CheckerRunning"
    values_to_get = [False]


class TestApproveByDefaultCmd(TestPropertySetterCmd):
    command = command.ApproveByDefault
    propname = "ApprovedByDefault"
    values_to_get = [True]


class TestDenyByDefaultCmd(TestPropertySetterCmd):
    command = command.DenyByDefault
    propname = "ApprovedByDefault"
    values_to_get = [False]


class TestSetCheckerCmd(TestPropertySetterCmd):
    command = command.SetChecker
    propname = "Checker"
    values_to_set = ["", ":", "fping -q -- %s"]


class TestSetHostCmd(TestPropertySetterCmd):
    command = command.SetHost
    propname = "Host"
    values_to_set = ["192.0.2.3", "client.example.org"]


class TestSetSecretCmd(TestPropertySetterCmd):
    command = command.SetSecret
    propname = "Secret"
    def __init__(self, *args, **kwargs):
        self.values_to_set = [io.BytesIO(b""),
                              io.BytesIO(b"secret\0xyzzy\nbar")]
        self.values_to_get = [f.getvalue() for f in
                              self.values_to_set]
        super(TestSetSecretCmd, self).__init__(*args, **kwargs)


class TestSetTimeoutCmd(TestPropertySetterCmd):
    command = command.SetTimeout
    propname = "Timeout"
    values_to_set = [datetime.timedelta(),
                     datetime.timedelta(minutes=5),
                     datetime.timedelta(seconds=1),
                     datetime.timedelta(weeks=1),
                     datetime.timedelta(weeks=52)]
    values_to_get = [dt.total_seconds()*1000 for dt in values_to_set]


class TestSetExtendedTimeoutCmd(TestPropertySetterCmd):
    command = command.SetExtendedTimeout
    propname = "ExtendedTimeout"
    values_to_set = [datetime.timedelta(),
                     datetime.timedelta(minutes=5),
                     datetime.timedelta(seconds=1),
                     datetime.timedelta(weeks=1),
                     datetime.timedelta(weeks=52)]
    values_to_get = [dt.total_seconds()*1000 for dt in values_to_set]


class TestSetIntervalCmd(TestPropertySetterCmd):
    command = command.SetInterval
    propname = "Interval"
    values_to_set = [datetime.timedelta(),
                     datetime.timedelta(minutes=5),
                     datetime.timedelta(seconds=1),
                     datetime.timedelta(weeks=1),
                     datetime.timedelta(weeks=52)]
    values_to_get = [dt.total_seconds()*1000 for dt in values_to_set]


class TestSetApprovalDelayCmd(TestPropertySetterCmd):
    command = command.SetApprovalDelay
    propname = "ApprovalDelay"
    values_to_set = [datetime.timedelta(),
                     datetime.timedelta(minutes=5),
                     datetime.timedelta(seconds=1),
                     datetime.timedelta(weeks=1),
                     datetime.timedelta(weeks=52)]
    values_to_get = [dt.total_seconds()*1000 for dt in values_to_set]


class TestSetApprovalDurationCmd(TestPropertySetterCmd):
    command = command.SetApprovalDuration
    propname = "ApprovalDuration"
    values_to_set = [datetime.timedelta(),
                     datetime.timedelta(minutes=5),
                     datetime.timedelta(seconds=1),
                     datetime.timedelta(weeks=1),
                     datetime.timedelta(weeks=52)]
    values_to_get = [dt.total_seconds()*1000 for dt in values_to_set]



def parse_test_args():
    # type: () -> argparse.Namespace
    parser = argparse.ArgumentParser(add_help=False)
    parser.add_argument("--check", action="store_true")
    parser.add_argument("--prefix", )
    args, unknown_args = parser.parse_known_args()
    if args.check:
        # Remove test options from sys.argv
        sys.argv[1:] = unknown_args
    return args

# Add all tests from doctest strings
def load_tests(loader, tests, none):
    import doctest
    tests.addTests(doctest.DocTestSuite())
    return tests

if __name__ == "__main__":
    options = parse_test_args()
    try:
        if options.check:
            extra_test_prefix = options.prefix
            if extra_test_prefix is not None:
                if not (unittest.main(argv=[""], exit=False)
                        .result.wasSuccessful()):
                    sys.exit(1)
                class ExtraTestLoader(unittest.TestLoader):
                    testMethodPrefix = extra_test_prefix
                # Call using ./scriptname --check [--verbose]
                unittest.main(argv=[""], testLoader=ExtraTestLoader())
            else:
                unittest.main(argv=[""])
        else:
            main()
    finally:
        logging.shutdown()

# Local Variables:
# run-tests:
# (lambda (&optional extra)
#   (if (not (funcall run-tests-in-test-buffer default-directory
# 	      extra))
# 	(funcall show-test-buffer-in-test-window)
#     (funcall remove-test-window)
#     (if extra (message "Extra tests run successfully!"))))
# run-tests-in-test-buffer:
# (lambda (dir &optional extra)
#   (with-current-buffer (get-buffer-create "*Test*")
#     (setq buffer-read-only nil
# 	    default-directory dir)
#     (erase-buffer)
#     (compilation-mode))
#   (let ((process-result
# 	   (let ((inhibit-read-only t))
# 	     (process-file-shell-command
# 	      (funcall get-command-line extra) nil "*Test*"))))
#     (and (numberp process-result)
# 	   (= process-result 0))))
# get-command-line:
# (lambda (&optional extra)
#   (let ((quoted-script
# 	   (shell-quote-argument (funcall get-script-name))))
#     (format
#      (concat "%s --check" (if extra " --prefix=atest" ""))
#      quoted-script)))
# get-script-name:
# (lambda ()
#   (if (fboundp 'file-local-name)
# 	(file-local-name (buffer-file-name))
#     (or (file-remote-p (buffer-file-name) 'localname)
# 	  (buffer-file-name))))
# remove-test-window:
# (lambda ()
#   (let ((test-window (get-buffer-window "*Test*")))
#     (if test-window (delete-window test-window))))
# show-test-buffer-in-test-window:
# (lambda ()
#   (when (not (get-buffer-window-list "*Test*"))
#     (setq next-error-last-buffer (get-buffer "*Test*"))
#     (let* ((side (if (>= (window-width) 146) 'right 'bottom))
# 	     (display-buffer-overriding-action
# 	      `((display-buffer-in-side-window) (side . ,side)
# 		(window-height . fit-window-to-buffer)
# 		(window-width . fit-window-to-buffer))))
# 	(display-buffer "*Test*"))))
# eval:
# (progn
#   (let* ((run-extra-tests (lambda () (interactive)
# 			      (funcall run-tests t)))
# 	   (inner-keymap `(keymap (116 . ,run-extra-tests))) ; t
# 	   (outer-keymap `(keymap (3 . ,inner-keymap))))     ; C-c
#     (setq minor-mode-overriding-map-alist
# 	    (cons `(run-tests . ,outer-keymap)
# 		  minor-mode-overriding-map-alist)))
#   (add-hook 'after-save-hook run-tests 90 t))
# End: