summaryrefslogtreecommitdiff
path: root/test/test-dbus.py
blob: 9af8bfd7577bc520f552b28ac52b06dd1cb9033e (plain)
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
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
#! /usr/bin/python -u
# -*- coding: utf-8 -*-
# vim: set fileencoding=utf-8 :#
#
# Copyright (C) 2009 Intel Corporation
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) version 3.
#
# This library 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301  USA

import random
import unittest
import subprocess
import time
import os
import errno
import signal
import shutil
import copy
import heapq
import string
import difflib
import traceback
import ConfigParser
import io
import inspect
import gzip
import httplib
import socket

import dbus
from dbus.mainloop.glib import DBusGMainLoop
import dbus.service
import gobject
import glib
import sys
import traceback
import re
import atexit
import base64

# introduced in python-gobject 2.16, not available
# on all Linux distros => make it optional
try:
    import glib
    have_glib = True
except ImportError:
    have_glib = False

DBusGMainLoop(set_as_default=True)

debugger = os.environ.get("TEST_DBUS_GDB", None) and "gdb" or ""
# Verbosity must at least include INFO messages, the "ready to run" we
# wait for is not printed otherwise. Default is DEBUG.
level = os.environ.get("TEST_DBUS_QUIET", False) and 2 or 3
server = ("syncevo-dbus-server --no-syslog --stdout --verbosity=%d --dbus-verbosity=%d" % (level, level)).split()

# primarily for XDG files, but also other temporary files
xdg_root = "temp-test-dbus"
configName = "dbus_unittest"

def usingValgrind():
    return 'valgrind' in os.environ.get("TEST_DBUS_PREFIX", "")

def which(program):
    '''find absolute path to program (simple file name, no path) in PATH env variable'''
    def isExe(fpath):
        return os.path.isfile(fpath) and os.access(fpath, os.X_OK)

    for path in os.environ["PATH"].split(os.pathsep):
        exeFile = os.path.join(path, program)
        if isExe(exeFile):
            return os.path.abspath(exeFile)

    return None

def GrepNotifications(dbuslog):
    '''finds all Notify calls and returns their parameters as list of line lists'''
    return re.findall(r'^method call .* dest=.* .*interface=org.freedesktop.Notifications; member=Notify\n((?:^   .*\n)*)',
                      dbuslog,
                      re.MULTILINE)

# See notification-daemon.py for a stand-alone version.
#
# Embedded here to avoid issues with setting up the environment
# in such a way that the stand-alone version can be run
# properly.
class Notifications (dbus.service.Object):
    '''fake org.freedesktop.Notifications implementation,'''
    '''used when there is none already registered on the session bus'''

    @dbus.service.method(dbus_interface='org.freedesktop.Notifications', in_signature='', out_signature='ssss')
    def GetServerInformation(self):
        return ('test-dbus', 'SyncEvolution', '0.1', '1.1')

    @dbus.service.method(dbus_interface='org.freedesktop.Notifications', in_signature='', out_signature='as')
    def GetCapabilities(self):
        return ['actions', 'body', 'body-hyperlinks', 'body-markup', 'icon-static', 'sound']

    @dbus.service.method(dbus_interface='org.freedesktop.Notifications', in_signature='susssasa{sv}i', out_signature='u')
    def Notify(self, app, replaces, icon, summary, body, actions, hints, expire):
        return random.randint(1,100)

# fork before connecting to the D-Bus daemon
child = os.fork()
if child == 0:
    bus = dbus.SessionBus()
    loop = gobject.MainLoop()
    name = dbus.service.BusName("org.freedesktop.Notifications", bus)
    # start dummy notification daemon, if possible;
    # if it fails, ignore (probably already one running)
    notifications = Notifications(bus, "/org/freedesktop/Notifications")
    loop.run()
    sys.exit(0)

# testing continues in parent process
atexit.register(os.kill, child, 9)
bus = dbus.SessionBus()
loop = gobject.MainLoop()

# log to .dbus.log of a test and to stdout, if running a debugger
class Logging(dbus.service.Object):
    def __init__(self):
        dbus.service.Object.__init__(self, bus, '/test/dbus/py')
        self.start = time.time()

    @dbus.service.signal(dbus_interface='t.d.p',
                         signature='s')
    def log2(self, str):
        if debugger or os.environ.get("TEST_DBUS_VERBOSE", False):
            print str
        pass

    def log(self, str):
        now = time.time()
        self.log2(('%.3fs: ' % (now - self.start)) + str)

    def printf(self, format, *args):
        self.log(format % args)
logging = Logging()

class NullLogging:
    def log(self, str):
        pass
    def printf(self, format, *args):
        pass

# Bluez default adapter
bt_adaptor = "/org/bluez/1036/hci0"

# handles D-Bus messages for '/' object in both net.connman and
# org.bluez; must be the same object because D-Bus cannot switch based
# on the bus name (not necessarily included in message)
class RootObject (dbus.service.Object):
    # ConnMan state
    state = "online"
    getPropertiesCalled = False
    waitingForGetProperties = False

    def __init__(self):
        self.bluez_name = dbus.service.BusName('org.bluez', bus)
        self.conn_name = dbus.service.BusName("net.connman", bus)
        dbus.service.Object.__init__(self, bus, '/')

    @dbus.service.method(dbus_interface='org.bluez.Manager', in_signature='', out_signature='o')
    def DefaultAdapter(self):
        return bt_adaptor

    @dbus.service.signal(dbus_interface='org.bluez.Manager', signature='o')
    def DefaultAdapterChanged(self, obj):
        return bt_adaptor

    @dbus.service.method(dbus_interface='net.connman.Manager', in_signature='', out_signature='a{sv}')
    def GetProperties(self):
        # notify TestDBusServerPresence.setUp()?
        if self.waitingForGetProperties:
            loop.quit()
        self.getPropertiesCalled = True
        return { "State" : self.state }

    @dbus.service.signal(dbus_interface='net.connman.Manager', signature='sv')
    def PropertyChanged(self, key, value):
        pass

    def setState(self, state):
        if self.state != state:
            self.state = state
            self.PropertyChanged("State", state)
            # race condition: it happened that method calls
            # reached syncevo-dbus-server before the state change,
            # thus breaking the test
            time.sleep(1)

    def reset(self):
        self.state = "online"
        self.getPropertiesCalled = False
        self.waitingForGetProperties = False

root = RootObject()

def property(key, value):
    """Function decorator which sets an arbitrary property of a test.
    Use like this:
         @property("foo", "bar")
         def testMyTest:
             ...

             print self.getTestProperty("foo", "default")
    """
    def __setProperty(func):
        if not "properties" in dir(func):
            func.properties = {}
        func.properties[key] = value
        return func
    return __setProperty

def timeout(seconds):
    """Function decorator which sets a non-default timeout for a test.
    The default timeout, enforced by DBusTest.runTest(), are 20 seconds.
    Use like this:
        @timeout(60)
        def testMyTest:
            ...
    """
    return property("timeout", seconds)

class Timeout:
    """Implements global time-delayed callbacks."""
    alarms = []
    next_alarm = None
    previous_handler = None
    debugTimeout = False

    @classmethod
    def addTimeout(cls, delay_seconds, callback, use_glib=True):
        """Call function after a certain delay, specified in seconds.
        If possible and use_glib=True, then it will only fire inside
        glib event loop. Otherwise it uses signals. When signals are
        used it is a bit uncertain what kind of Python code can
        be executed. It was observed that trying to append to
        DBusUtil.quit_events before calling loop.quit() caused
        a KeyboardInterrupt"""
        if have_glib and use_glib:
            glib.timeout_add(delay_seconds, callback)
            # TODO: implement removal of glib timeouts
            return None
        else:
            now = time.time()
            if cls.debugTimeout:
                print "addTimeout", now, delay_seconds, callback, use_glib
            timeout = (now + delay_seconds, callback)
            heapq.heappush(cls.alarms, timeout)
            cls.__check_alarms()
            return timeout

    @classmethod
    def removeTimeout(cls, timeout):
        """Remove a timeout returned by a previous addTimeout call.
        None and timeouts which have already fired are acceptable."""
        try:
            cls.alarms.remove(timeout)
        except ValueError:
            pass
        else:
            heapq.heapify(cls.alarms)
            cls.__check_alarms()

    @classmethod
    def __handler(cls, signum, stack):
        """next_alarm has fired, check for expired timeouts and reinstall"""
        if cls.debugTimeout:
            print "fired", time.time()
        cls.next_alarm = None
        cls.__check_alarms()

    @classmethod
    def __check_alarms(cls):
        now = time.time()
        while cls.alarms and cls.alarms[0][0] <= now:
            timeout = heapq.heappop(cls.alarms)
            if cls.debugTimeout:
                print "invoking", timeout
            timeout[1]()

        if cls.alarms:
            if not cls.next_alarm or \
                    cls.next_alarm > cls.alarms[0][0]:
                if cls.previous_handler == None:
                    cls.previous_handler = signal.signal(signal.SIGALRM, cls.__handler)
                cls.next_alarm = cls.alarms[0][0]
                delay = int(cls.next_alarm - now + 0.5)
                if not delay:
                    delay = 1
                if cls.debugTimeout:
                    print "next alarm", cls.next_alarm, delay
                signal.alarm(delay)
        elif cls.next_alarm:
            if cls.debugTimeout:
                print "disarming alarm"
            signal.alarm(0)
            cls.next_alarm = None

# commented out because running it takes time
#class TestTimeout(unittest.TestCase):
class TimeoutTest:
    """unit test for Timeout mechanism"""

    def testOneTimeout(self):
        """testOneTimeout - OneTimeout"""
        self.called = False
        start = time.time()
        def callback():
            self.called = True
        Timeout.addTimeout(2, callback, use_glib=False)
        time.sleep(10)
        end = time.time()
        self.assertTrue(self.called)
        self.assertFalse(end - start < 2)
        self.assertFalse(end - start >= 3)

    def testEmptyTimeout(self):
        """testEmptyTimeout - EmptyTimeout"""
        self.called = False
        start = time.time()
        def callback():
            self.called = True
        Timeout.addTimeout(0, callback, use_glib=False)
        if not self.called:
            time.sleep(10)
        end = time.time()
        self.assertTrue(self.called)
        self.assertFalse(end - start < 0)
        self.assertFalse(end - start >= 1)

    def testTwoTimeouts(self):
        """testTwoTimeouts - TwoTimeouts"""
        self.called = False
        start = time.time()
        def callback():
            self.called = True
        Timeout.addTimeout(2, callback, use_glib=False)
        Timeout.addTimeout(5, callback, use_glib=False)
        time.sleep(10)
        end = time.time()
        self.assertTrue(self.called)
        self.assertFalse(end - start < 2)
        self.assertFalse(end - start >= 3)
        time.sleep(10)
        end = time.time()
        self.assertTrue(self.called)
        self.assertFalse(end - start < 5)
        self.assertFalse(end - start >= 6)

    def testTwoReversedTimeouts(self):
        """testTwoReversedTimeouts - TwoReversedTimeouts"""
        self.called = False
        start = time.time()
        def callback():
            self.called = True
        Timeout.addTimeout(5, callback, use_glib=False)
        Timeout.addTimeout(2, callback, use_glib=False)
        time.sleep(10)
        end = time.time()
        self.assertTrue(self.called)
        self.assertFalse(end - start < 2)
        self.assertFalse(end - start >= 3)
        time.sleep(10)
        end = time.time()
        self.assertTrue(self.called)
        self.assertFalse(end - start < 5)
        self.assertFalse(end - start >= 6)

def TryKill(pid, signal):
    try:
        os.kill(pid, signal)
        return True
    except OSError, ex:
        # might have quit in the meantime, deal with the race
        # condition
        if ex.errno != 3:
            raise ex
    return False

def ShutdownSubprocess(popen, timeout):
    start = time.time()
    if popen.poll() == None:
        # kill process and process group, in case that process has
        # forked children (valgrindcheck.sh + syncevo-dbus-server
        # case or syncevo-dbus-server + local sync)
        TryKill(popen.pid, signal.SIGTERM)
        TryKill(-popen.pid, signal.SIGTERM)
    while popen.poll() == None and start + timeout >= time.time():
        time.sleep(0.01)
    if popen.poll() == None:
        TryKill(popen.pid, signal.SIGKILL)
        TryKill(-popen.pid, signal.SIGKILL)
        while popen.poll() == None and start + timeout + 1 >= time.time():
            time.sleep(0.01)
        return False
    else:
        # there shouldn't be any processes in the process group left now
        # because the parent process has quit normally, but make sure anyway
        TryKill(-popen.pid, signal.SIGKILL)
        return True

class DBusUtil(Timeout):
    """Contains the common run() method for all D-Bus test suites
    and some utility functions."""

    # Use class variables because that way it is ensured that there is
    # only one set of them. Previously instance variables were used,
    # which had the effect that D-Bus signal handlers from test A
    # wrote into variables which weren't the ones used by test B.
    # Unfortunately it is impossible to remove handlers when
    # completing test A.
    events = []
    quit_events = []
    reply = None
    pserver = None
    pmonitor = None
    storedenv = {}
    # Choose a very long timeout for method calls;
    # 'infinite' doesn't seem to be documented for Python.
    dbusTimeout = 100000

    def getTestProperty(self, key, default):
        """retrieve values set with @property()"""
        # Assume that IDs are composed of class name + dot + function name.
        # Directly acccessing self._testMethodName would bypass the public
        # Unittest API.
        testMethodName = self.id().split('.')[-1]
        testMethod = getattr(self, testMethodName)
        properties = getattr(testMethod, "properties", {})
        return properties.get(key, default)

    def runTest(self, result, own_xdg=True, serverArgs=[], own_home=False, defTimeout=20):
        """Starts the D-Bus server and dbus-monitor before the test
        itself. After the test run, the output of these two commands
        are added to the test's failure, if any. Otherwise the output
        is ignored. A non-zero return code of the D-Bus server is
        logged as separate failure.

        The D-Bus server must print at least one line of output
        before the test is allowed to start.
        
        The commands are run with XDG_DATA_HOME, XDG_CONFIG_HOME,
        XDG_CACHE_HOME pointing towards local dirs
        test-dbus/[data|config|cache] which are removed before each
        test."""

        DBusUtil.events = []
        DBusUtil.quit_events = []
        DBusUtil.reply = None
        self.pserverpid = None

        # allow arbitrarily long diffs in Python unittest
        self.maxDiff = None

        self.killChildren(0)
        # collect zombies
        try:
            while os.waitpid(-1, os.WNOHANG)[0]:
                pass
        except OSError, ex:
            if ex.errno != errno.ECHILD:
                raise ex

        """own_xdg is saved in self for we use this flag to check whether
        to copy the reference directory tree."""
        self.own_xdg = own_xdg
        env = copy.deepcopy(os.environ)
        if own_xdg or own_home:
            shutil.rmtree(xdg_root, True)
        if own_xdg:
            env["XDG_DATA_HOME"] = os.path.abspath(os.path.join(xdg_root, "data"))
            env["XDG_CONFIG_HOME"] = os.path.abspath(os.path.join(xdg_root, "config"))
            env["XDG_CACHE_HOME"] = os.path.abspath(os.path.join(xdg_root, "cache"))
        if own_home:
            env["HOME"] = os.path.abspath(xdg_root)

        # populate xdg_root
        snapshot = self.getTestProperty("snapshot", None)
        if snapshot:
            self.setUpFiles(snapshot)

        # set additional environment variables for the test run,
        # as defined by @property("ENV", "foo=bar x=y")
        for assignment in self.getTestProperty("ENV", "").split():
            var, value = assignment.split("=", 1)
            env[var] = value

        # always print all debug output directly (no output redirection),
        # and increase log level
        if self.getTestProperty("debug", True):
            env["SYNCEVOLUTION_DEBUG"] = "1"

        self.storedenv = env

        # Can be set by a test to run additional tests on the content
        # of the D-Bus log. May be set before calling runTest() or
        # in the test method itself.
        # self.runTestDBusCheck = lambda test, log: test.assertNotIn('ERROR', log)
        #
        # And for stdout.
        # self.runTestOutputCheck = None

        # Compress dbus log with gzip. Warning, can lead to truncated logs
        # when gzip doesn't flush everything in time.
        useGZip = os.environ.get("TEST_DBUS_GZIP", False)

        # testAutoSyncFailure (__main__.TestSessionAPIsDummy) => testAutoSyncFailure_TestSessionAPIsDummy
        self.testname = str(self).replace(" ", "_").replace("__main__.", "").replace("(", "").replace(")", "")
        dbuslog = self.testname + ".dbus.log"
        if useGZip:
            dbuslog = dbuslog + ".gz"
        syncevolog = self.testname + ".syncevo.log"

        self.pmonitor = subprocess.Popen(useGZip and ['sh', '-c', 'dbus-monitor | gzip'] or ['dbus-monitor'],
                                         stdout=open(dbuslog, "w"),
                                         stderr=subprocess.STDOUT)

        if debugger:
            print "\n%s: %s\n" % (self.id(), self.shortDescription())
            if env.get("HOME") != os.environ.get("HOME") and \
                    os.path.exists(os.path.join(os.environ.get("HOME"), ".gdbinit")):
                gdbinit = ['-x', os.path.join(os.environ.get("HOME"), ".gdbinit")]
            else:
                gdbinit = []
            DBusUtil.pserver = subprocess.Popen([debugger] + gdbinit + ['--args'] + server,
                                                env=env)

            while not bus.name_has_owner('org.syncevolution'):
                time.sleep(1)
            print "\nfound org.syncevolution on session bus, starting test"
        else:
            if os.environ.get("SYNCEVOLUTION_DEBUG", None):
                logfile = None
            else:
                logfile = open(syncevolog, "w")
            prefix = os.environ.get("TEST_DBUS_PREFIX", "")
            args = []
            if prefix:
                args.append(prefix)
            args.extend(server)
            args.extend(serverArgs)
            if logfile != None:
                logfile.write("env:\n%s\n\nargs:\n%s\n\n" % (env, args))
                logfile.flush()
            size = os.path.getsize(syncevolog)
            DBusUtil.pserver = subprocess.Popen(args,
                                                preexec_fn=lambda: os.setpgid(0, 0),
                                                env=env,
                                                stdout=logfile,
                                                stderr=subprocess.STDOUT)
            if logfile != None:
                while self.isServerRunning():
                    newsize = os.path.getsize(syncevolog)
                    if newsize != size:
                        if "] ready to run\n" in open(syncevolog).read():
                            break
                        size = newsize
                        time.sleep(1)
            else:
                print "test-dbus.py: giving syncevo-dbus-server time to start"
                time.sleep(5)
                print "test-dbus.py: starting test"

        # pserver.pid is not necessarily the pid of syncevo-dbus-server.
        # It might be the child of the pserver process.
        try:
            self.pserverpid = self.serverPid()
            self.running = True
        except:
            result.errors.append((self,
                                  "server startup failed: %s\nchildren: %s" % (traceback.format_exc(), self.getChildren())))
            self.running = False

        numerrors = len(result.errors)
        numfailures = len(result.failures)
        if debugger:
            print "\nrunning\n"

        # Find out what test function we run and look into
        # the function definition to see whether it comes
        # with a non-default timeout, otherwise use a 20 second
        # timeout.
        timeout = self.getTestProperty("timeout", defTimeout)
        timeout_handle = None
        if timeout and not debugger and not os.environ.get("SYNCEVOLUTION_LOCAL_CHILD_DELAY", None):
            logging.printf('killing test in %d seconds' % timeout)
            def timedout():
                error = "%s timed out after %d seconds, current quit events: %s" % (self.id(), timeout, self.quit_events)
                logging.log('%s\n%s' % (error, ''.join(traceback.format_stack())))
                raise Exception(error)
            timeout_handle = self.addTimeout(timeout, timedout, use_glib=False)
        else:
            logging.printf('killing test disabled')
        try:
            if self.running:
                unittest.TestCase.run(self, result)
        except KeyboardInterrupt, ex:
            # somehow this happens when timedout() above raises the exception
            # while inside glib main loop
            result.errors.append((self,
                                  "interrupted by timeout (%d seconds, current quit events: %s) or CTRL-C or Python signal handler problem, exception is: %s" % (timeout, self.quit_events, traceback.format_exc())))
        self.running = False
        self.removeTimeout(timeout_handle)
        if debugger:
            # Print result of this test run.
            for test, trace in result.errors[numerrors:] + result.failures[numfailures:]:
                print trace
            print "\nDone, quit gdb now to proceed.\nSee %s for D-Bus messages.\n" % dbuslog
        hasfailed = numerrors + numfailures != len(result.errors) + len(result.failures)

        if debugger:
            # allow debugger to run as long as it is needed
            DBusUtil.pserver.communicate()

        # Find and kill all sub-processes except for dbus-monitor:
        # first try SIGTERM, then if they don't respond, SIGKILL. The
        # latter is an error. How much time we allow them between
        # SIGTERM and SIGTERM depends on how much work still needs to
        # be done after being asked to quit. valgrind leak checking
        # can take a while.
        unresponsive = self.killChildren(usingValgrind() and 60 or 20)
        if unresponsive:
            error = "/".join(unresponsive) + " had to be killed with SIGKILL"
            print "   ", error
            result.errors.append((self, error))

        if debugger:
            serverout = '<see console>'
        else:
            serverout = open(syncevolog).read()
        if DBusUtil.pserver is not None and DBusUtil.pserver.returncode != -15:
            hasfailed = True
        if hasfailed and not debugger:
            # give D-Bus time to settle down
            time.sleep(1)
        if not ShutdownSubprocess(self.pmonitor, 5):
            print "   dbus-monitor had to be killed with SIGKILL"
            result.errors.append((self,
                                  "dbus-monitor had to be killed with SIGKILL"))

        # If reading the dbus-monitor output, then read it completely.
        # The runTestDBusCheck callbacks expect that and it is confusing
        # when the report printed as part of a test failure is incomplete.
        if debugger:
            monitorout = '<see %s>' % dbuslog
        elif useGZip:
            monitorout = dbuslog + ':\n' + gzip.GzipFile(dbuslog).read()
        else:
            monitorout = dbuslog + ':\n' + open(dbuslog).read()
        report = "\n\nD-Bus traffic:\n%s\n\nserver output:\n%s\n" % \
            (monitorout, serverout)
        runTestDBusCheck = getattr(self, 'runTestDBusCheck', None)
        if runTestDBusCheck:
            try:
                runTestDBusCheck(self, monitorout)
            except:
                # only append report if not part of some other error below
                result.errors.append((self,
                                      "D-Bus log failed check: %s\n%s" % (sys.exc_info()[1], (not hasfailed and report) or "")))
        runTestOutputCheck = getattr(self, 'runTestOutputCheck', None)
        if runTestOutputCheck:
            try:
                runTestOutputCheck(self, serverout)
            except:
                # only append report if not part of some other error below
                result.errors.append((self,
                                      "server stdout failed check: %s\n%s" % (sys.exc_info()[1], (not hasfailed and report) or "")))
        # detect the expected "killed by signal TERM" both when
        # running syncevo-dbus-server directly (negative value) and
        # when valgrindcheck.sh returns the error code 128 + 15 = 143
        if DBusUtil.pserver and \
           DBusUtil.pserver.returncode and \
           DBusUtil.pserver.returncode != 128 + 15 and \
           DBusUtil.pserver.returncode != -15:
            # create a new failure specifically for the server
            result.errors.append((self,
                                  "server terminated with error code %d%s" % (DBusUtil.pserver.returncode, report)))
        elif numerrors != len(result.errors):
            # append report to last error
            result.errors[-1] = (result.errors[-1][0], result.errors[-1][1] + report)
        elif numfailures != len(result.failures):
            # same for failure
            result.failures[-1] = (result.failures[-1][0], result.failures[-1][1] + report)

    def loopIteration(self, message, may_block=True):
        '''Trigger glib, allow it to wait once (optional).'''
        # Calling this is necessary, Python uses it to re-raise a
        # timeout exception that was originally raised inside the C
        # glib code. If None, the caller must do it.
        if message:
            logging.log(message)
        loop.get_context().iteration(may_block)

    def runUntil(self, state, check, until, may_block=False):
        '''Loop until 'check' throws an exception or 'until' returns True.
Use check=lambda: (expr1, expr2, ...) when more than one check is needed.
        '''
        message = 'waiting for ' + state
        if may_block:
            # Allow process to get stuck for a while.
            # Return from the loop every half second to
            # check status.
            logging.log(message)
            t = glib.Timeout(500)
            try:
                t.set_callback(lambda: loop.quit() or True)
                t.attach(loop.get_context())
                while True:
                    check()
                    if until():
                        break
                    loop.run()
            finally:
                t.destroy()
        else:
            progress = True
            while True:
                # Only print it if something changed, not every 0.1 second.
                if progress:
                    logging.log(message)
                check()
                if until():
                    break
                if not progress and not may_block:
                    # Don't block forever if nothing is to be processed.
                    # To keep the load down, sleep for a short period.
                    time.sleep(0.1)
                progress = self.loopIteration(None, may_block=False)
        logging.log('succeeded waiting for ' + state)

    def isServerRunning(self):
        """True while the syncevo-dbus-server executable is still running"""
        return DBusUtil.pserver and DBusUtil.pserver.poll() == None

    def getChildren(self):
        '''Find all children of the current process (ppid = out pid,
        in our process group, or in process group of known
        children). Return mapping from pid to (name, cmdline),
        where cmdline has spaces between parameters.'''

        # Any of the known children might have its own process group.
        # syncevo-dbus-server definitely does (we create it like that);
        # that allows us to find its forked processes.
        pgids = []
        if self.pmonitor:
            pgids.append(self.pmonitor.pid)
        if self.pserverpid:
            pgids.append(self.pserverpid)

        # Maps from pid to name, process ID, process group ID.
        procs = {}
        statre = re.compile(r'^\d+ \((?P<name>.*?)\) \S (?P<ppid>\d+) (?P<pgid>\d+)')
        for process in os.listdir('/proc'):
            try:
                pid = int(process)
                stat = open('/proc/%d/stat' % pid).read()
                m = statre.search(stat)
                if m:
                    procs[pid] = m.groupdict()
                    procs[pid]['cmdline'] = open('/proc/%d/cmdline' % pid, 'r').read().replace('\0', ' ')
                    for i in ('ppid', 'pgid'):
                        procs[pid][i] = int(procs[pid][i])
            except:
                # ignore all errors
                pass
        logging.printf("found processes: %s", procs)
        # Now determine direct or indirect children.
        children = {}
        mypid = os.getpid()
        def isChild(pid):
            if pid == mypid:
                return False
            if not procs.get(pid, None):
                return False
            return procs[pid]['ppid'] == mypid or \
                procs[pid]['pgid'] in pgids or \
                isChild(procs[pid]['ppid'])
        for pid, info in procs.iteritems():
            if isChild(pid):
                children[pid] = (info['name'], info['cmdline'])
        # Exclude dbus-monitor and forked test-dbus.py, they are handled separately.
        if self.pmonitor:
            del children[self.pmonitor.pid]
        del children[child]
        logging.printf("found children: %s", children)
        return children

    def killPending(self, pending):
        '''Ensure that all processes listed with their pid are not running.'''
        while True:
            again = []
            for pid in pending:
                logging.printf("sending SIGKILL to pending proccess %d" % pid)
                if not TryKill(pid, signal.SIGKILL):
                    logging.printf("pending process %d is gone" % pid)
                else:
                    again.append(pid)
            if again:
                time.sleep(0.1)
                pending = again
            else:
                break

    def killChildren(self, delay):
        '''Find all children of the current process and kill them. First send SIGTERM,
        then after a grace period SIGKILL.'''
        children = self.getChildren()
        pending = []
        # First pass with SIGTERM?
        if delay:
            for pid, (name, cmdline) in children.iteritems():
                logging.printf("sending SIGTERM to %d %s = %s", pid, name, cmdline)
                TryKill(pid, signal.SIGTERM)
        start = time.time()
        logging.printf("starting to wait for process termination at %s", time.asctime(time.localtime(start)))
        while delay and start + delay >= time.time():
            # Check if any process has quit, remove from list.
            pids = copy.deepcopy(children)
            def checkKnown(p):
                if (p):
                    if p.pid in pids:
                        del pids[p.pid]
                    if p.poll() != None and p.pid in children:
                        logging.printf("known process %d %s has quit at %s",
                                       p.pid, children[p.pid], time.asctime())
                        del children[p.pid]
            checkKnown(self.pserver)
            for pid, name in pids.iteritems():
                try:
                    res = os.waitpid(pid, os.WNOHANG)
                    if res[0]:
                        # got result, remove process
                        logging.printf("got status for process %d %s at %s",
                                       pid, name, time.asctime())
                        del children[pid]
                except OSError, ex:
                    if ex.errno == errno.ECHILD:
                        # Process might have transferred to init as parent:
                        #
                        #   PID  PPID   TID NLWP CMD                         COMMAND
                        # 20617     1     -    2 [memcheck-amd64-] <defunct> [memcheck-amd64-] <defunct>
                        #     -     - 20617    - -                           -
                        #     -     - 21012    - -                           -
                        #
                        # This was observed for syncevo-dbus-server when running under
                        # valgrind despite apparently having terminated normally
                        # (valgrindcheck.sh sees the exist status, log output confirms
                        # normal shutdown). Not sure why process shutdown did not
                        # terminate all threads.
                        #
                        # Remember to kill the remaining threads with SIGKILL,
                        # as a workaround.
                        logging.printf("process %d %s gone at %s",
                                       pid, name, time.asctime())
                        pending.append(pid)
                        del children[pid]
                    else:
                        raise ex
            if not children:
                # All children quit normally.
                logging.printf("all process gone at %s",
                               time.asctime())
                self.killPending(pending)
                return []
            time.sleep(0.1)
        # Force killing of remaining children. It's still possible
        # that one of them quits before we get around to sending the
        # signal.
        if children:
            logging.printf("starting to kill unresponsive processes at %s: %s", time.asctime(), str(children))
        killed = []
        for pid, name in children.iteritems():
            if TryKill(pid, signal.SIGKILL):
                logging.printf("killed %d %s", pid, name)
                killed.append("%d %s" % (pid, name))
        self.killPending(pending)
        return killed

    def serverExecutableHelper(self, pid):
        self.assertTrue(self.isServerRunning())
        maps = open("/proc/%d/maps" % pid, "r")
        regex = re.compile(r'[0-9a-f]*-[0-9a-f]* r-xp [0-9a-f]* [^ ]* \d* *(.*)\n')
        parentre = re.compile(r'^PPid:\s+(\d+)', re.MULTILINE)
        for line in maps:
            match = regex.match(line)
            if match:
                # must be syncevo-dbus-server
                res = match.group(1)
                if 'syncevo-dbus-server' in res:
                    return (res, pid)
                # not found, try children
                for process in os.listdir('/proc'):
                    try:
                        status = open('/proc/%s/status' % process).read()
                        parent = parentre.search(status)
                        if parent and int(parent.group(1)) == pid:
                            res = self.serverExecutableHelper(int(process))
                            if res:
                                return res
                    except:
                        # ignore all errors
                        pass
        # no result
        return None

    def serverExecutable(self):
        """returns full path of currently running syncevo-dbus-server binary"""
        res = self.serverExecutableHelper(DBusUtil.pserver.pid)
        self.assertTrue(res)
        return res[0]

    def serverPid(self):
        """PID of syncevo-dbus-server, None if not running. Works regardless whether it is
        started directly or with a wrapper script like valgrindcheck.sh."""
        res = self.serverExecutableHelper(DBusUtil.pserver.pid)
        self.assertTrue(res)
        return res[1]

    def setUpServer(self):
        self.server = dbus.Interface(bus.get_object('org.syncevolution',
                                                    '/org/syncevolution/Server'),
                                     'org.syncevolution.Server')

    def createSession(self, config, wait, flags=[]):
        """Return sessionpath and session object for session using 'config'.
        A signal handler calls loop.quit() when this session becomes ready.
        If wait=True, then this call blocks until the session is ready.
        """
        if flags:
            sessionpath = self.server.StartSessionWithFlags(config, flags, timeout=self.dbusTimeout)
        else:
            sessionpath = self.server.StartSession(config, timeout=self.dbusTimeout)

        def session_ready(object, ready):
            if self.running and ready and object == sessionpath:
                DBusUtil.quit_events.append("session " + object + " ready")
                loop.quit()

        signal = bus.add_signal_receiver(session_ready,
                                         'SessionChanged',
                                         'org.syncevolution.Server',
                                         self.server.bus_name,
                                         None,
                                         byte_arrays=True,
                                         utf8_strings=True)
        session = dbus.Interface(bus.get_object(self.server.bus_name,
                                                sessionpath),
                                 'org.syncevolution.Session')
        status, error, sources = session.GetStatus(utf8_strings=True, timeout=self.dbusTimeout)
        if wait and status == "queueing":
            # wait for signal
            loop.run()
            self.assertEqual(DBusUtil.quit_events, ["session " + sessionpath + " ready"])
        elif DBusUtil.quit_events:
            # signal was processed inside D-Bus call?
            self.assertEqual(DBusUtil.quit_events, ["session " + sessionpath + " ready"])
        if wait:
            # signal no longer needed, remove it because otherwise it
            # might record unexpected "session ready" events
            signal.remove()
        DBusUtil.quit_events = []
        return (sessionpath, session)

    def setUpSession(self, config, flags=[]):
        """stores ready session in self.sessionpath and self.session"""
        self.sessionpath, self.session = self.createSession(config, True, flags)

    def progressChanged(self, *args, **keywords):
        '''subclasses override this method to write specified callbacks for ProgressChanged signals
           It is called by progress signal receivers in setUpListeners'''
        pass

    def statusChanged(self, *args, **keywords):
        '''subclasses override this method to write specified callbacks for StatusChanged signals
           It is called by status signal receivers in setUpListeners'''
        pass

    def setUpListeners(self, sessionpath):
        """records progress and status changes in DBusUtil.events and
        quits the main loop when the session is done"""

        def progress(*args, **keywords):
            if self.running:
                DBusUtil.events.append(("progress", args, keywords['path']))
                self.progressChanged(*args, **keywords)

        def status(*args, **keywords):
            if self.running:
                DBusUtil.events.append(("status", args, keywords['path']))
                self.statusChanged(*args, **keywords)
                if args[0] == "done":
                    if sessionpath:
                        DBusUtil.quit_events.append("session " + sessionpath + " done")
                    else:
                        DBusUtil.quit_events.append("session done")
                    loop.quit()

        DBusUtil.events = []
        bus.add_signal_receiver(progress,
                                'ProgressChanged',
                                'org.syncevolution.Session',
                                self.server.bus_name,
                                sessionpath,
                                path_keyword='path',
                                byte_arrays=True,
                                utf8_strings=True)
        bus.add_signal_receiver(status,
                                'StatusChanged',
                                'org.syncevolution.Session',
                                self.server.bus_name,
                                sessionpath,
                                path_keyword='path',
                                byte_arrays=True,
                                utf8_strings=True)

    def setUpConfigListeners(self):
        """records ConfigChanged signal and records it in DBusUtil.events, then quits the loop"""

        def config():
            if self.running:
                DBusUtil.events.append("ConfigChanged")
                DBusUtil.quit_events.append("ConfigChanged")
                loop.quit()

        bus.add_signal_receiver(config,
                                'ConfigChanged',
                                'org.syncevolution.Server',
                                self.server.bus_name,
                                byte_arrays=True, 
                                utf8_strings=True)

    def setUpConnectionListeners(self, conpath):
        """records connection signals (abort and reply), quits when
        getting an abort"""

        def abort():
            if self.running:
                DBusUtil.events.append(("abort",))
                DBusUtil.quit_events.append("connection " + conpath + " aborted")
                loop.quit()

        def reply(*args):
            if self.running:
                DBusUtil.reply = args
                if args[3]:
                    DBusUtil.quit_events.append("connection " + conpath + " got final reply")
                else:
                    DBusUtil.quit_events.append("connection " + conpath + " got reply")
                loop.quit()

        bus.add_signal_receiver(abort,
                                'Abort',
                                'org.syncevolution.Connection',
                                self.server.bus_name,
                                conpath,
                                byte_arrays=True, 
                                utf8_strings=True)
        bus.add_signal_receiver(reply,
                                'Reply',
                                'org.syncevolution.Connection',
                                self.server.bus_name,
                                conpath,
                                byte_arrays=True, 
                                utf8_strings=True)

    def collectEvents(self, until='\nstatus: done'):
        '''Normally collection of events stops when any of the 'quit events' are encounted.
        Cmdline tests are different, they stop when the command line tool returns. At that
        time the quit event may or may not have been seen.
        This method continues collecting events until the 'until' string is in the
        pretty-printed events.'''
        while not until in self.prettyPrintEvents():
            loop.get_context().iteration(True)

    def setUpLocalSyncConfigs(self, childPassword=None, enableCalendar=False, preventSlowSync=None):
        # create file<->file configs
        self.setUpSession("target-config@client")
        addressbook = { "sync": "two-way",
                        "backend": "file",
                        "databaseFormat": "text/vcard",
                        "database": "file://" + xdg_root + "/client" }
        calendar = { "sync": "two-way",
                     "backend": "file",
                     "databaseFormat": "text/calendar",
                     "database": "file://" + xdg_root + "/client-calendar" }
        if childPassword:
            addressbook["databaseUser"] = "foo-user"
            addressbook["databasePassword"] = childPassword
        config = {"" : { "loglevel": "4" } }
        if preventSlowSync != None:
            config = {"" : { "preventSlowSync": preventSlowSync and "1" or "0" }}
        config["source/addressbook"] = addressbook
        if enableCalendar:
            config["source/calendar"] = calendar
        self.session.SetConfig(False, False, config, timeout=self.dbusTimeout)
        self.session.Detach(timeout=self.dbusTimeout)
        self.setUpSession("server")
        config = {"" : { "loglevel": "4",
                         "syncURL": "local://@client",
                         "RetryDuration": self.getTestProperty("resendDuration", "60"),
                         "peerIsClient": "1" },
                  "source/addressbook": { "sync": "two-way",
                                          "uri": "addressbook",
                                          "backend": "file",
                                          "databaseFormat": "text/vcard",
                                          "database": "file://" + xdg_root + "/server" } }
        if enableCalendar:
            config["source/calendar"] = { "sync": "two-way",
                                          # "uri": "calendar", uri = local source name is the default
                                          "backend": "file",
                                          "databaseFormat": "text/calendar",
                                          "database": "file://" + xdg_root + "/server-calendar" }
        self.session.SetConfig(False, False, config, timeout=self.dbusTimeout)

    def setUpFiles(self, snapshot):
        """ Copy reference directory trees from
        test/test-dbus/<snapshot> to own xdg_root (=./test-dbus). To
        be used only in tests which called runTest() with
        own_xdg=True."""
        self.assertTrue(self.own_xdg)
        # Get the absolute path of the current python file.
        scriptpath = os.path.abspath(os.path.expanduser(os.path.expandvars(sys.argv[0])))
        # reference directory 'test-dbus' is in the same directory as the current python file
        sourcedir = os.path.join(os.path.dirname(scriptpath), 'test-dbus', snapshot)
        """ Directories in test/test-dbus are copied to xdg_root, but
        maybe with different names, mappings are:
                  'test/test-dbus/<snapshot>'   './test-dbus'"""
        pairs = { 'sync4j'                    : '.sync4j'  ,
                  'config'                    : 'config'   ,
                  'cache'                     : 'cache'    ,
                  'data'                      : 'data'     ,
                  'templates'                 : 'templates'}
        for src, dest in pairs.items():
            destpath = os.path.join(xdg_root, dest)
            # make sure the dest directory does not exist, which is required by shutil.copytree
            shutil.rmtree(destpath, True)
            sourcepath = os.path.join(sourcedir, src)
            # if source exists and could be accessed, then copy them
            if os.access(sourcepath, os.F_OK):
                shutil.copytree(sourcepath, destpath)

    def prettyPrintEvents(self, events=None):
        '''Format events as lines without full class specifiers, like this:
status: idle, 0, {}
'''
        if events == None:
            events = DBusUtil.events
        lines = []
        def prettyPrintArg(arg):
            if isinstance(arg, type(())):
                res = []
                for i in arg:
                    res.append(prettyPrintArg(i))
                return '(' + ', '.join(res) + ')'
            elif isinstance(arg, type([])):
                res = []
                for i in arg:
                    res.append(prettyPrintArg(i))
                return '[' + ', '.join(res) + ']'
            elif isinstance(arg, type({})):
                res = []
                items = arg.items()
                items.sort()
                for i,e in items:
                    res.append('%s: %s' % (prettyPrintArg(i), prettyPrintArg(e)))
                return '{' + ', '.join(res) + '}'
            else:
                return str(arg)
        def prettyPrintArgs(args):
            res = []
            for arg in args:
                res.append(prettyPrintArg(arg))
            return ', '.join(res)
        for event in events:
            self.assertTrue(len(event) >= 1,
                            'Unexpected number of entries in event: %s' % str(event))
            if len(event) >= 2:
                lines.append('%s: %s' % (event[0], prettyPrintArgs(event[1])))
            else:
                lines.append(event[0])
        return '\n'.join(lines)

    def getSyncStatus(self, config):
        cache = self.own_xdg and os.path.join(xdg_root, 'cache', 'syncevolution') or \
            os.path.join(os.environ['HOME'], '.cache', 'syncevolution')
        entries = [x for x in os.listdir(cache) if x.startswith(config + '-')]
        entries.sort()
        self.assertEqual(1, len(entries))
        config = ConfigParser.ConfigParser()
        content = '[fake]\n' + open(os.path.join(cache, entries[0], 'status.ini')).read()
        config.readfp(io.BytesIO(content))
        status = config.getint('fake', 'status')
        error = None
        if config.has_option('fake', 'error'):
            error = config.get('fake', 'error')
        return (status, error)

    def isSet(self, data):
        return isinstance(data, type([])) or \
            isinstance(data, type(()))

    def assertSyncStatus(self, config, status, error):
        realStatus, realError = self.getSyncStatus(config)
        if self.isSet(status):
            self.assertTrue(realStatus in status)
        else:
            self.assertEqual(status, realStatus)
        if self.isSet(error):
            self.assertTrue(realError in error)
        else:
            self.assertEqual(error, realError)

    def doCheckSync(self, expectedError=0, expectedResult=0, reportOptional=False, numReports=1, checkPercent=True):
        # check recorded events in DBusUtil.events, first filter them
        statuses = []
        progresses = []
        # Dict is used to check status order.  
        statusPairs = {"": 0, "idle": 1, "running" : 2, "aborting" : 3, "done" : 4}
        for item in DBusUtil.events:
            if item[0] == "status":
                statuses.append(item[1])
            elif item[0] == "progress":
                progresses.append(item[1])

        # check statuses
        lastStatus = ""
        lastSources = {}
        lastError = 0
        for status, error, sources in statuses:
            # consecutive entries should not be equal
            self.assertNotEqual((lastStatus, lastError, lastSources), (status, error, sources))
            # no error, unless expected
            if expectedError:
                if error:
                    self.assertEqual(expectedError, error)
            else:
                self.assertEqual(error, 0)
            # keep order: session status must be unchanged or the next status 
            seps = status.split(';')
            lastSeps = lastStatus.split(';')
            self.assertTrue(statusPairs.has_key(seps[0]))
            self.assertTrue(statusPairs[seps[0]] >= statusPairs[lastSeps[0]])
            # check specifiers
            if len(seps) > 1:
                self.assertEqual(seps[1], "waiting")
            for sourcename, value in sources.items():
                # no error
                self.assertEqual(value[2], 0)
                # keep order: source status must also be unchanged or the next status
                if lastSources.has_key(sourcename):
                    lastValue = lastSources[sourcename]
                    self.assertTrue(statusPairs[value[1]] >= statusPairs[lastValue[1]])

            lastStatus = status
            lastSources = sources
            lastError = error

        # check increasing progress percentage
        lastPercent = 0
        for percent, sources in progresses:
            self.assertFalse(percent < lastPercent)
            lastPercent = percent

        status, error, sources = self.session.GetStatus(utf8_strings=True)
        self.assertEqual(status, "done")
        self.assertEqual(error, expectedError)

        # now check that report is sane
        reports = self.session.GetReports(0, 100, utf8_strings=True)
        if reportOptional and len(reports) == 0:
            # no report was written
            return None
        self.assertEqual(len(reports), numReports)
        if expectedResult:
            self.assertEqual(int(reports[0]["status"]), expectedResult)
        else:
            self.assertEqual(int(reports[0]["status"]), 200)
            self.assertNotIn("error", reports[0])
        return reports[0]

    def checkSync(self, *args, **keywords):
        '''augment any assertion in doCheckSync() with text dump of events'''
        events = self.prettyPrintEvents()
        try:
            return self.doCheckSync(*args, **keywords)
        except AssertionError, ex:
            raise self.failureException('Assertion about the following events failed:\n%s\n%s' %
                                        (events, traceback.format_exc()))

    def assertEqualDiff(self, expected, res):
        '''Like assertEqual(), but raises an error which contains a
        diff of the two parameters. Useful when they are long strings
        (will be split at newlines automatically) or lists (compared
        as-is). Very similar to Python's 2.7 unittest, but also works
        for older Python releases and allows comparing strings against lists.'''
        def splitlines(str):
            '''split any object which looks like a string == has splitlines'''
            if 'splitlines' in dir(str):
                return str.splitlines(True)
            else:
                return str
        expected = splitlines(expected)
        res = splitlines(res)
        if expected != res:
            diff = ''.join(difflib.Differ().compare(expected, res))
            self.fail('differences between expected and actual text\n\n' + diff)

    def assertRegexpMatchesCustom(self, text, regex, msg=None):
        if isinstance(regex, str):
            regex = re.compile(regex)
        if not regex.search(text):
            if msg != None:
                self.fail(msg)
            else:
                self.fail('text does not match regex\n\nText:\n%s\n\nRegex:\n%s' % \
                              (text, regex.pattern))


    def assertInCustom(self, needle, haystack, msg=None):
        if not needle in haystack:
            if msg != None:
                self.fail(msg)
            else:
                self.fail("'" + str(needle) + "' not found in '" + str(haystack) + "'")

    def assertNotInCustom(self, needle, haystack, msg=None):
        if needle in haystack:
            if msg != None:
                self.fail(msg)
            else:
                self.fail("'" + str(needle) + "' found in '" + str(haystack) + "'")

    dbusTypeMapping = {
        dbus.Array: list,
        dbus.Boolean: bool,
        dbus.Byte: int,
        dbus.Dictionary: dict,
        dbus.Double: float,
        dbus.Int16: int,
        dbus.Int32: int,
        dbus.Int64: long,
        dbus.ObjectPath: str,
        dbus.Signature: str,
        dbus.String: unicode,
        dbus.Struct: tuple,
        dbus.UInt16: int,
        dbus.UInt32: int,
        dbus.UInt64: long,
        dbus.UTF8String: unicode
        }

    def stripDBus(self, instance, sortLists):
        base =  DBusUtil.dbusTypeMapping.get(type(instance), None)
        if base == dict or isinstance(instance, dict):
            return dict([(self.stripDBus(k, sortLists), self.stripDBus(v, sortLists)) for k, v in instance.iteritems()])
        if base == list or isinstance(instance, list):
            l = [self.stripDBus(v, sortLists) for v in instance]
            if sortLists:
                l.sort()
            return l
        if base == tuple or isinstance(instance, tuple):
            return tuple([self.stripDBus(v, sortLists) for v in instance])
        if base == None:
            return instance
        if base == unicode:
            # try conversion to normal string
            try:
                return str(instance)
            except UnicodeEncodeError:
                pass
        return base(instance)

    def assertEqualCustom(self, a, b, msg=None, sortLists=False):
        '''Reduce custom dbus types to the corresponding Python type, to simplify the output in the error message.'''
        unittest.TestCase.assertEqual(self, self.stripDBus(a, sortLists), self.stripDBus(b, sortLists), msg)

    # always use our own compare
    assertEqual = assertEqualCustom

    # reimplement Python 2.7 assertions only in older Python
    if True or not 'assertRegexpMatches' in dir(unittest.TestCase):
        assertRegexpMatches = assertRegexpMatchesCustom

    if not 'assertIn' in dir(unittest.TestCase):
        assertIn = assertInCustom

    if not 'assertNotIn' in dir(unittest.TestCase):
        assertNotIn = assertNotInCustom

class TestDBusServer(DBusUtil, unittest.TestCase):
    """Tests for the read-only Server API."""

    def setUp(self):
        self.setUpServer()

    def run(self, result):
        self.runTest(result)

    def testCapabilities(self):
        """TestDBusServer.testCapabilities - Server.Capabilities()"""
        capabilities = self.server.GetCapabilities()
        capabilities.sort()
        self.assertEqual(capabilities, ['ConfigChanged', 'DatabaseProperties', 'GetConfigName', 'NamedConfig', 'Notifications', 'SessionAttach', 'SessionFlags', 'Version'])

    def testVersions(self):
        """TestDBusServer.testVersions - Server.GetVersions()"""
        versions = self.server.GetVersions()
        self.assertNotEqual(versions["version"], "")
        self.assertNotEqual(versions["system"], None)
        self.assertNotEqual(versions["backends"], None)

    def testGetConfigsEmpty(self):
        """TestDBusServer.testGetConfigsEmpty - Server.GetConfigsEmpty()"""
        configs = self.server.GetConfigs(False, utf8_strings=True)
        self.assertEqual(configs, [])

    @property("ENV", "DBUS_TEST_BLUETOOTH=none")
    def testGetConfigsTemplates(self):
        """TestDBusServer.testGetConfigsTemplates - Server.GetConfigsTemplates()"""
        configs = self.server.GetConfigs(True, utf8_strings=True)
        configs.sort()
        self.assertEqual(configs, ["Funambol",
                                   "Google_Calendar",
                                   "Google_Contacts",
                                   "Goosync",
                                   "Memotoo",
                                   "Mobical",
                                   "Oracle",
                                   "Ovi",
                                   "ScheduleWorld",
                                   "SyncEvolution",
                                   "Synthesis",
                                   "WebDAV",
                                   "Yahoo",
                                   "eGroupware"])

    def testGetConfigScheduleWorld(self):
        """TestDBusServer.testGetConfigScheduleWorld - Server.GetConfigScheduleWorld()"""
        config1 = self.server.GetConfig("scheduleworld", True, utf8_strings=True)
        config2 = self.server.GetConfig("ScheduleWorld", True, utf8_strings=True)
        self.assertNotEqual(config1[""]["deviceId"], config2[""]["deviceId"])
        config1[""]["deviceId"] = "foo"
        config2[""]["deviceId"] = "foo"
        self.assertEqual(config1, config2)

    def testInvalidConfig(self):
        """TestDBusServer.testInvalidConfig - Server.NoSuchConfig exception"""
        try:
            config1 = self.server.GetConfig("no-such-config", False, utf8_strings=True)
        except dbus.DBusException, ex:
            self.assertEqual(str(ex),
                                 "org.syncevolution.NoSuchConfig: No configuration 'no-such-config' found")
        else:
            self.fail("no exception thrown")

class TestDBusServerStart(DBusUtil, unittest.TestCase):
    def testAutoActivation(self):
        '''TestDBusServerStart.testAutoActivation - check that activating syncevo-dbus-server via D-Bus daemon works'''
        # Create a D-Bus service file for the syncevo-dbus-server that we
        # are testing (= the one in PATH).
        shutil.rmtree(xdg_root, True)
        dirname = os.path.join(xdg_root, "share", "dbus-1", "services")
        os.makedirs(dirname)
        service = open(os.path.join(dirname, "org.syncevolution.service"), "w")
        service.write('''[D-BUS Service]
Name=org.syncevolution
Exec=%s
''' % which('syncevo-dbus-server'))
        service.close()

        # Now run a private D-Bus session in which dbus-send activates
        # that syncevo-dbus-server. Uses a dbus-session.sh from the
        # same dir as test-dbus.py itself.
        env = copy.deepcopy(os.environ)
        env['XDG_DATA_DIRS'] = env.get('XDG_DATA_DIRS', '') + ':' + os.path.abspath(os.path.join(xdg_root, "share"))

        # Avoid running EDS and Akonadi. They are not needed for this test
        # and can cause false failures, for example when the daemons
        # return an unexpected error cause, which then would be returned
        # by dbus-session.sh.
        for key in ('DBUS_SESSION_SH_EDS_BASE', 'DBUS_SESSION_SH_AKONADI'):
            if env.has_key(key):
                del env[key]

        # First run something which just starts the daemon.
        cmd = (os.path.join(os.path.dirname(sys.argv[0]), 'dbus-session.sh'),
               'dbus-send',
               '--print-reply',
               '--dest=org.syncevolution',
               '/',
               'org.freedesktop.DBus.Introspectable.Introspect')
        dbus = subprocess.Popen(cmd,
                                env=env,
                                stdout=subprocess.PIPE,
                                stderr=subprocess.STDOUT)
        (out, err) = dbus.communicate()
        self.assertEqual(0, dbus.returncode,
                         msg='introspection of syncevo-dbus-server failed:\nenv = %s\ncmd = %s:\output:\n%s' %
                         (env, cmd, out))

        # Now try some real command.
        dbus = subprocess.Popen((os.path.join(os.path.dirname(sys.argv[0]), 'dbus-session.sh'),
                                 'dbus-send',
                                 '--print-reply',
                                 '--dest=org.syncevolution',
                                 '/org/syncevolution/Server',
                                 'org.syncevolution.Server.GetVersions'),
                                env=env,
                                stdout=subprocess.PIPE,
                                stderr=subprocess.STDOUT)
        (out, err) = dbus.communicate()
        self.assertEqual(0, dbus.returncode,
                         msg='GetVersions of syncevo-dbus-server failed:\n' + out)


class TestDBusServerTerm(DBusUtil, unittest.TestCase):
    def setUp(self):
        self.setUpServer()

    def run(self, result):
        self.runTest(result, True, ["-d", "10"])

    def testSingleton(self):
        """TestDBusServerTerm.testSingleton - a second instance of syncevo-dbus-server must terminate right away"""
        dbus = subprocess.Popen('syncevo-dbus-server --no-syslog --stdout'.split(),
                                stdout=subprocess.PIPE,
                                stderr=subprocess.STDOUT)
        (out, err) = dbus.communicate()
        if not re.search(r'''\[ERROR.*already running\?\n''', out):
            self.fail('second dbus server did not recognize already running server:\n%s' % out)

    @timeout(100)
    def testNoTerm(self):
        """TestDBusServerTerm.testNoTerm - D-Bus server must stay around during calls"""

        """The server should stay alive because we have dbus call within
        the duration. The loop is to make sure the total time is longer 
        than duration and the dbus server still stays alive for dbus calls.""" 
        for i in range(0, 4):
            time.sleep(4)
            try:
                self.server.GetConfigs(True, utf8_strings=True)
            except dbus.DBusException:
                self.fail("dbus server should work correctly")

    @timeout(100)
    def testTerm(self):
        """TestDBusServerTerm.testTerm - D-Bus server must auto-terminate"""
        #sleep a duration and wait for syncevo-dbus-server termination
        time.sleep(16)
        try:
            self.server.GetConfigs(True, utf8_strings=True)
        except dbus.DBusException:
            pass
        else:
            self.fail("no exception thrown")

    @timeout(100)
    def testTermConnection(self):
        """TestDBusServerTerm.testTermConnection - D-Bus server must terminate after closing connection and not sooner"""
        conpath = self.server.Connect({'description': 'test-dbus.py',
                                       'transport': 'dummy'},
                                      False,
                                      "")
        time.sleep(16)
        try:
            self.server.GetConfigs(True, utf8_strings=True)
        except dbus.DBusException:
            self.fail("dbus server should not terminate")

        connection = dbus.Interface(bus.get_object(self.server.bus_name,
                                                   conpath),
                                    'org.syncevolution.Connection')
        connection.Close(False, "good bye", utf8_strings=True)
        time.sleep(16)
        try:
            self.server.GetConfigs(True, utf8_strings=True)
        except dbus.DBusException:
            pass
        else:
            self.fail("no exception thrown")

    @timeout(100)
    def testTermAttachedClients(self):
        """TestDBusServerTerm.testTermAttachedClients - D-Bus server must not terminate while clients are attached"""

        """Also it tries to test the dbus server's behavior when a client 
        attaches the server many times"""
        self.server.Attach()
        self.server.Attach()
        time.sleep(16)
        try:
            self.server.GetConfigs(True, utf8_strings=True)
        except dbus.DBusException:
            self.fail("dbus server should not terminate")
        self.server.Detach()
        time.sleep(16)

        try:
            self.server.GetConfigs(True, utf8_strings=True)
        except dbus.DBusException:
            self.fail("dbus server should not terminate")

        self.server.Detach()
        time.sleep(16)
        try:
            self.server.GetConfigs(True, utf8_strings=True)
        except dbus.DBusException:
            pass
        else:
            self.fail("no exception thrown")

    @timeout(100)
    def testAutoSyncOn(self):
        """TestDBusServerTerm.testAutoSyncOn - D-Bus server must not terminate while auto syncing is enabled"""
        self.setUpSession("scheduleworld")
        # enable auto syncing with a very long delay to prevent accidentally running it
        config = self.session.GetConfig(True, utf8_strings=True)
        config[""]["autoSync"] = "1"
        config[""]["autoSyncInterval"] = "60m"
        self.session.SetConfig(False, False, config)
        self.session.Detach()

        time.sleep(16)

        try:
            self.server.GetConfigs(True, utf8_strings=True)
        except dbus.DBusException:
            self.fail("dbus server should not terminate")

    @timeout(100)
    def testAutoSyncOff(self):
        """TestDBusServerTerm.testAutoSyncOff - D-Bus server must terminate after auto syncing was disabled"""
        self.setUpSession("scheduleworld")
        # enable auto syncing with a very long delay to prevent accidentally running it
        config = self.session.GetConfig(True, utf8_strings=True)
        config[""]["autoSync"] = "1"
        config[""]["autoSyncInterval"] = "60m"
        self.session.SetConfig(False, False, config)
        self.session.Detach()

        self.setUpSession("scheduleworld")
        config[""]["autoSync"] = "0"
        self.session.SetConfig(True, False, config)
        self.session.Detach()

        time.sleep(16)
        try:
            self.server.GetConfigs(True, utf8_strings=True)
        except dbus.DBusException:
            pass
        else:
            self.fail("no exception thrown")

    @timeout(100)
    def testAutoSyncOff2(self):
        """TestDBusServerTerm.testAutoSyncOff2 - D-Bus server must terminate after auto syncing was disabled after a while"""
        self.setUpSession("scheduleworld")
        # enable auto syncing with a very long delay to prevent accidentally running it
        config = self.session.GetConfig(True, utf8_strings=True)
        config[""]["autoSync"] = "1"
        config[""]["autoSyncInterval"] = "60m"
        self.session.SetConfig(False, False, config)
        self.session.Detach()

        # wait until -d 10 second timeout has triggered in syncevo-dbus-server
        time.sleep(11)

        self.setUpSession("scheduleworld")
        config[""]["autoSync"] = "0"
        self.session.SetConfig(True, False, config)
        self.session.Detach()

        # should shut down after the 10 second idle period
        time.sleep(16)

        try:
            self.server.GetConfigs(True, utf8_strings=True)
        except dbus.DBusException:
            pass
        else:
            self.fail("no exception thrown")

    # configure server with files in test/test-dbus/auto-sync before starting it
    @property("snapshot", "auto-sync")
    @timeout(100)
    def testAutoSyncOff3(self):
        """TestDBusServerTerm.testAutoSyncOff3 - start with auto-syncing on, D-Bus server must terminate after disabling auto syncing"""
        # wait until -d 10 second timeout has triggered in syncevo-dbus-server,
        # should not shut down because of auto sync
        time.sleep(11)
        self.assertTrue(self.isServerRunning())

        self.setUpSession("scheduleworld")
        config = self.session.GetConfig(False)
        config[""]["autoSync"] = "0"
        self.session.SetConfig(False, False, config)
        self.session.Detach()

        # should shut down after the 10 second idle period,
        # may take longer when using valgrind
        if usingValgrind():
            time.sleep(30)
        else:
            time.sleep(16)
        self.assertFalse(self.isServerRunning())


class TestNamedConfig(DBusUtil, unittest.TestCase):
    """Tests for Set/GetNamedConfig"""

    def setUp(self):
        self.setUpServer()

    def run(self, result):
        self.runTest(result)

    def testSetNamedConfigError(self):
        """TestNamedConfig.testSetNamedConfigError - SetNamedConfig() only allowed in 'all-configs' sessions"""
        self.setUpSession("")
        try:
            self.session.SetNamedConfig("foobar", False, False, {})
        except dbus.DBusException, ex:
            self.assertEqual(str(ex),
                             "org.syncevolution.InvalidCall: SetNameConfig() only allowed in 'all-configs' sessions")
        else:
            self.fail("no exception thrown")

    def testSetNamedConfigErrorTemporary(self):
        """TestNamedConfig.testSetNamedConfigErrorTemporary - SetNamedConfig() only implemented for session config"""
        self.setUpSession("foo", [ "all-configs" ])
        try:
            self.session.SetNamedConfig("foobar", False, True, {})
        except dbus.DBusException, ex:
            self.assertEqual(str(ex),
                             "org.syncevolution.InvalidCall: SetNameConfig() with temporary config change only supported for config named when starting the session")
        else:
            self.fail("no exception thrown")
        self.session.Detach()

        self.setUpSession("")
        self.session.SetNamedConfig("", False, True, {})

    def testSetNamedConfig(self):
        """TestNamedConfig.testSetNamedConfig - create two different configs in one session"""
        self.setUpSession("", [ "all-configs" ])

        fooConfig = {"": {"username": "foo", "configName": "foo"}}
        barConfig = {"": {"username": "bar", "configName": "bar"}}

        self.session.SetNamedConfig("foo", False, False, fooConfig)
        self.session.SetNamedConfig("bar", False, False, barConfig)

        self.assertEqual(fooConfig, self.server.GetConfig("foo", False))
        self.assertEqual(barConfig, self.server.GetConfig("bar", False))

        self.assertEqual(fooConfig, self.session.GetNamedConfig("foo", False))
        self.assertEqual(barConfig, self.session.GetNamedConfig("bar", False))

class TestDBusServerPresence(DBusUtil, unittest.TestCase):
    """Tests Presence signal and checkPresence API"""

    # TODO: check auto sync + presence combination

    # Our fake ConnMan implementation must be present on the
    # bus also outside of tests, because syncevo-dbus-server
    # will try to call it before setUp(). The implementation's
    # initialization and tearDown() below ensures that the state
    # is "online" outside of tests.
    conn = root

    def setUp(self):
        self.setUpServer()
        self.cbFailure = None
        # we don't know if the GetProperties() call was already
        # processed; if not, wait for it here
        if not self.conn.getPropertiesCalled:
            self.conn.waitingForGetProperties = True
            loop.run()

    def tearDown(self):
        self.conn.reset()
        self.conf = None

    def presenceCB(self,
                   server, status, transport,
                   expected):
        try:
            state = expected.pop(server, None)
            if not state:
                self.fail("unexpected presence signal for config " + server)
            self.failUnlessEqual(status, state[0])
            if not status:
                self.failUnlessEqual(transport, state[1])
        except Exception, ex:
            # tell test method about the problem
            loop.quit()
            self.cbFailure = ex
            # log exception
            raise ex

        if not expected:
            # got all expected signals
            loop.quit()

    def expect(self, expected):
        '''expected: hash from server config name to state+transport'''
        match = bus.add_signal_receiver(lambda x,y,z:
                                            self.presenceCB(x,y,z, expected), \
                                            'Presence',
                                            'org.syncevolution.Server',
                                            self.server.bus_name,
                                            None,
                                            byte_arrays=True,
                                            utf8_strings=True)
        return match

    @property("ENV", "DBUS_TEST_CONNMAN=session DBUS_TEST_NETWORK_MANAGER=none")
    @timeout(100)
    def testPresenceSignal(self):
        """TestDBusServerPresence.testPresenceSignal - check Server.Presence signal"""

        # creating a config does not trigger a Presence signal
        self.setUpSession("foo")
        self.session.SetConfig(False, False, {"" : {"syncURL": "http://http-only-1"}})
        self.session.Detach()

        # go offline
        match = self.expect({"foo" : ("no transport", "")})
        self.conn.setState("idle")
        loop.run()
        self.failIf(self.cbFailure)
        match.remove()

        # Changing the properties temporarily does change
        # the presence of the config although strictly speaking,
        # the presence of the config on disk hasn't changed.
        # Not sure whether we really want that behavior.
        match = self.expect({"foo" : ("", "obex-bt://temp-bluetooth-peer-changed-from-http")})
        self.setUpSession("foo")
        self.session.SetConfig(True, False, {"" : {"syncURL":
        "obex-bt://temp-bluetooth-peer-changed-from-http"}})
        # A ConnMan state change is needed to trigger the presence signal.
        # Definitely not the behavior that we want :-/
        self.conn.setState("failure")
        loop.run()
        self.failIf(self.cbFailure)
        match.remove()
        # remove temporary config change, back to using HTTP
        # BUG BMC #24648 in syncevo-dbus-server: after discarding the temporary
        # config change it keeps using the obex-bt syncURL.
        # Work around that bug in thus test here temporarily
        # by explicitly restoring the previous URL.
        self.session.SetConfig(True, False, {"" : {"syncURL": "http://http-only-1"}})
        self.session.Detach()

        # create second session
        self.setUpSession("bar")
        self.session.SetConfig(False, False, {"" : {"syncURL":
            "http://http-client-2"}})
        self.session.Detach()

        # go back to online mode
        match = self.expect({"foo" : ("", "http://http-only-1"),
                             "bar" : ("", "http://http-client-2")})
        self.conn.setState("online")
        loop.run()
        self.failIf(self.cbFailure)
        match.remove()

        # and offline
        match = self.expect({"foo" : ("no transport", ""),
                             "bar" : ("no transport", "")})
        self.conn.setState("idle")
        loop.run()
        self.failIf(self.cbFailure)
        match.remove()

    @property("ENV", "DBUS_TEST_CONNMAN=session DBUS_TEST_NETWORK_MANAGER=none")
    @timeout(100)
    def testServerCheckPresence(self):
        """TestDBusServerPresence.testServerCheckPresence - check Server.CheckPresence()"""
        self.setUpSession("foo")
        self.session.SetConfig(False, False, {"" : {"syncURL":
        "http://http-client"}})
        self.session.Detach()
        self.setUpSession("bar")
        self.session.SetConfig(False, False, {"" : {"syncURL":
            "obex-bt://bt-client"}})
        self.session.Detach()
        self.setUpSession("foobar")
        self.session.SetConfig(False, False, {"" : {"syncURL":
            "obex-bt://bt-client-mixed http://http-client-mixed"}})
        self.session.Detach()

        # online initially
        (status, transports) = self.server.CheckPresence ("foo")
        self.assertEqual (status, "")
        self.assertEqual (transports, ["http://http-client"])
        (status, transports) = self.server.CheckPresence ("bar")
        self.assertEqual (status, "")
        self.assertEqual (transports, ["obex-bt://bt-client"])
        (status, transports) = self.server.CheckPresence ("foobar")
        self.assertEqual (status, "")
        self.assertEqual (transports, ["obex-bt://bt-client-mixed",
        "http://http-client-mixed"])

        # go offline; Bluetooth remains on
        self.conn.setState("idle")
        # wait until server has seen the state change
        match = bus.add_signal_receiver(lambda x,y,z: loop.quit(),
                                        'Presence',
                                        'org.syncevolution.Server',
                                        self.server.bus_name,
                                        None,
                                        byte_arrays=True,
                                        utf8_strings=True)
        match.remove()
        (status, transports) = self.server.CheckPresence ("foo")
        self.assertEqual (status, "no transport")
        (status, transports) = self.server.CheckPresence ("bar")
        self.assertEqual (status, "")
        self.assertEqual (transports, ["obex-bt://bt-client"])
        (status, transports) = self.server.CheckPresence ("foobar")
        self.assertEqual (status, "")
        self.assertEqual (transports, ["obex-bt://bt-client-mixed"])

    @property("ENV", "DBUS_TEST_CONNMAN=session DBUS_TEST_NETWORK_MANAGER=none")
    @timeout(100)
    def testSessionCheckPresence(self):
        """TestDBusServerPresence.testSessionCheckPresence - check Session.CheckPresence()"""
        self.setUpSession("foobar")
        self.session.SetConfig(False, False, {"" : {"syncURL":
            "obex-bt://bt-client-mixed http://http-client-mixed"}})
        status = self.session.CheckPresence()
        self.failUnlessEqual (status, "")

        # go offline; Bluetooth remains on
        self.conn.setState("idle")
        # wait until server has seen the state change
        match = bus.add_signal_receiver(lambda x,y,z: loop.quit(),
                                        'Presence',
                                        'org.syncevolution.Server',
                                        self.server.bus_name,
                                        None,
                                        byte_arrays=True,
                                        utf8_strings=True)
        match.remove()

        # config uses Bluetooth, so syncing still possible
        status = self.session.CheckPresence()
        self.failUnlessEqual (status, "")

        # now the same without Bluetooth, while offline
        self.session.Detach()
        self.setUpSession("foo")
        self.session.SetConfig(False, False, {"" : {"syncURL": "http://http-only"}})
        status = self.session.CheckPresence()
        self.assertEqual (status, "no transport")

        # go online
        self.conn.setState("online")
        # wait until server has seen the state change
        match = bus.add_signal_receiver(lambda x,y,z: loop.quit(),
                                        'Presence',
                                        'org.syncevolution.Server',
                                        self.server.bus_name,
                                        None,
                                        byte_arrays=True,
                                        utf8_strings=True)
        match.remove()
        status = self.session.CheckPresence()
        self.failUnlessEqual (status, "")

        # temporary config change shall always affect the
        # Session.CheckPresence() result: go offline,
        # then switch to Bluetooth (still present)
        self.conn.setState("idle")
        match = bus.add_signal_receiver(lambda x,y,z: loop.quit(),
                                        'Presence',
                                        'org.syncevolution.Server',
                                        self.server.bus_name,
                                        None,
                                        byte_arrays=True,
                                        utf8_strings=True)
        match.remove()
        status = self.session.CheckPresence()
        self.failUnlessEqual (status, "no transport")
        self.session.SetConfig(True, False, {"" : {"syncURL": "obex-bt://bt-client-mixed"}})
        status = self.session.CheckPresence()
        self.failUnlessEqual (status, "")

    def run(self, result):
        self.runTest(result, True)

class TestDBusSession(DBusUtil, unittest.TestCase):
    """Tests that work with an active session."""

    def setUp(self):
        self.setUpServer()
        self.setUpSession("")

    def run(self, result):
        self.runTest(result)

    def testCreateSession(self):
        """TestDBusSession.testCreateSession - ask for session"""
        self.assertEqual(self.session.GetFlags(), [])
        self.assertEqual(self.session.GetConfigName(), "@default")

    def testAttachSession(self):
        """TestDBusSession.testAttachSession - attach to running session"""
        self.session.Attach()
        self.session.Detach()
        self.assertEqual(self.session.GetFlags(), [])
        self.assertEqual(self.session.GetConfigName(), "@default")

    @timeout(70)
    def testAttachOldSession(self):
        """TestDBusSession.testAttachOldSession - attach to session which no longer has clients"""
        self.session.Detach()
        time.sleep(5)
        # This used to be impossible with SyncEvolution 1.0 because it
        # removed the session right after the previous client
        # left. SyncEvolution 1.1 makes it possible by keeping
        # sessions around for a minute. However, the session is
        # no longer listed because it really should only be used
        # by clients which heard about it before.
        self.assertEqual(self.server.GetSessions(), [])
        self.session.Attach()
        self.assertEqual(self.session.GetFlags(), [])
        self.assertEqual(self.session.GetConfigName(), "@default")
        time.sleep(60)
        self.assertEqual(self.session.GetFlags(), [])

    @timeout(70)
    def testExpireSession(self):
        """TestDBusSession.testExpireSession - ensure that session stays around for a minute"""
        self.session.Detach()
        time.sleep(5)
        self.assertEqual(self.session.GetFlags(), [])
        self.assertEqual(self.session.GetConfigName(), "@default")
        time.sleep(60)
        try:
            self.session.GetFlags()
        except:
            pass
        else:
            self.fail("Session.GetFlags() should have failed")

    def testCreateSessionWithFlags(self):
        """TestDBusSession.testCreateSessionWithFlags - ask for session with some specific flags and config"""
        self.session.Detach()
        self.sessionpath, self.session = self.createSession("FooBar@no-such-context", True, ["foo", "bar"])
        self.assertEqual(self.session.GetFlags(), ["foo", "bar"])
        self.assertEqual(self.session.GetConfigName(), "foobar@no-such-context")

    def testSecondSession(self):
        """TestDBusSession.testSecondSession - a second session should not run unless the first one stops"""
        sessions = self.server.GetSessions()
        self.assertEqual(sessions, [self.sessionpath])
        sessionpath = self.server.StartSession("")
        sessions = self.server.GetSessions()
        self.assertEqual(sessions, [self.sessionpath, sessionpath])

        def session_ready(object, ready):
            if self.running:
                DBusUtil.quit_events.append("session " + object + (ready and " ready" or " done"))
                loop.quit()

        bus.add_signal_receiver(session_ready,
                                'SessionChanged',
                                'org.syncevolution.Server',
                                self.server.bus_name,
                                None,
                                byte_arrays=True,
                                utf8_strings=True)

        def status(*args):
            if self.running:
                DBusUtil.events.append(("status", args))
                if args[0] == "idle":
                    DBusUtil.quit_events.append("session " + sessionpath + " idle")
                    loop.quit()

        bus.add_signal_receiver(status,
                                'StatusChanged',
                                'org.syncevolution.Session',
                                self.server.bus_name,
                                sessionpath,
                                byte_arrays=True, 
                                utf8_strings=True)

        session = dbus.Interface(bus.get_object(self.server.bus_name,
                                                sessionpath),
                                 'org.syncevolution.Session')
        status, error, sources = session.GetStatus(utf8_strings=True)
        self.assertEqual(status, "queueing")
        # use hash so that we can write into it in callback()
        callback_called = {}
        def callback():
            callback_called[1] = "callback()"
            self.session.Detach()
        try:
            t1 = self.addTimeout(2, callback)
            # session 1 done
            loop.run()
            self.assertTrue(callback_called)
            # session 2 ready and idle
            loop.run()
            loop.run()
            expected = ["session " + self.sessionpath + " done",
                        "session " + sessionpath + " idle",
                        "session " + sessionpath + " ready"]
            expected.sort()
            DBusUtil.quit_events.sort()
            self.assertEqual(DBusUtil.quit_events, expected)
            status, error, sources = session.GetStatus(utf8_strings=True)
            self.assertEqual(status, "idle")
        finally:
            self.removeTimeout(t1)

class TestSessionAPIsEmptyName(DBusUtil, unittest.TestCase):
    """Test session APIs that work with an empty server name. Thus, all of session APIs which
       need this kind of checking are put in this class. """

    def setUp(self):
        self.setUpServer()
        self.setUpSession("")

    def run(self, result):
        self.runTest(result)

    def testGetConfigEmptyName(self):
        """TestSessionAPIsEmptyName.testGetConfigEmptyName - reading empty default config"""
        config = self.session.GetConfig(False, utf8_strings=True)

    def testGetTemplateEmptyName(self):
        """TestSessionAPIsEmptyName.testGetTemplateEmptyName - trigger error by getting template for empty server name"""
        try:
            config = self.session.GetConfig(True, utf8_strings=True)
        except dbus.DBusException, ex:
            self.assertEqual(str(ex),
                                 "org.syncevolution.NoSuchConfig: No template '' found")
        else:
            self.fail("no exception thrown")

    def testCheckSourceEmptyName(self):
        """TestSessionAPIsEmptyName.testCheckSourceEmptyName - Test the error is reported when the server name is empty for CheckSource"""
        try:
            self.session.CheckSource("", utf8_strings=True)
        except dbus.DBusException, ex:
            self.assertEqual(str(ex),
                                 "org.syncevolution.NoSuchSource: '' has no '' source")
        else:
            self.fail("no exception thrown")

    def testGetDatabasesEmptyName(self):
        """TestSessionAPIsEmptyName.testGetDatabasesEmptyName - Test the error is reported when the server name is empty for GetDatabases"""
        try:
            self.session.GetDatabases("", utf8_strings=True)
        except dbus.DBusException, ex:
            self.assertEqual(str(ex),
                                 "org.syncevolution.NoSuchSource: '' has no '' source")
        else:
            self.fail("no exception thrown")

    def testGetReportsEmptyName(self):
        """TestSessionAPIsEmptyName.testGetReportsEmptyName - Test reports from all peers are returned in order when the peer name is empty for GetReports"""
        self.setUpFiles('reports')
        reports = self.session.GetReports(0, 0xFFFFFFFF, utf8_strings=True)
        self.assertEqual(len(reports), 7)
        refPeers = ["dummy-test", "dummy", "dummy-test", "dummy-test",
                    "dummy-test", "dummy_test", "dummy-test"]
        for i in range(0, len(refPeers)):
            self.assertEqual(reports[i]["peer"], refPeers[i])

    def testGetReportsContext(self):
        """TestSessionAPIsEmptyName.testGetReportsContext - Test reports from a context are returned when the peer name is empty for GetReports"""
        self.setUpFiles('reports')
        self.session.Detach()
        self.setUpSession("@context")
        reports = self.session.GetReports(0, 0xFFFFFFFF, utf8_strings=True)
        self.assertEqual(len(reports), 1)
        self.assertTrue(reports[0]["dir"].endswith("dummy_+test@context-2010-01-20-10-10"))


class TestSessionAPIsDummy(DBusUtil, unittest.TestCase):
    """Tests that work for GetConfig/SetConfig/CheckSource/GetDatabases/GetReports in Session.
       This class is only working in a dummy config. Thus it can't do sync correctly. The purpose
       is to test some cleanup cases and expected errors. Also, some unit tests for some APIs 
       depend on a clean configuration so they are included here. For those unit tests depending
       on sync, another class is used """

    def setUp(self):
        self.setUpServer()
        # use 'dummy-test' as the server name
        self.setUpSession("dummy-test")
        # default config
        self.config = { 
                         "" : { "syncURL" : "http://impossible-syncurl-just-for-testing-to-avoid-conflict",
                                "username" : "unknown",
                                # the password request tests depend on not having a real password here
                                "password" : "-",
                                "deviceId" : "foo",
                                "RetryInterval" : "10",
                                "RetryDuration" : "20",
                                "ConsumerReady" : "1",
                                "configName" : "dummy-test"
                              },
                         "source/addressbook" : { "sync" : "slow",
                                                  "backend" : "file",
                                                  "database" : "file://" + xdg_root + "/addressbook",
                                                  "databaseFormat" : "text/vcard",
                                                  "uri" : "card"
                                                },
                         "source/calendar"    : { "sync" : "disabled",
                                                  "backend" : "file",
                                                  "database" : "file://" + xdg_root + "/calendar",
                                                  "databaseFormat" : "text/calendar",
                                                  "uri" : "cal"
                                                },
                         "source/todo"        : { "sync" : "disabled",
                                                  "backend" : "file",
                                                  "database" : "file://" + xdg_root + "/todo",
                                                  "databaseFormat" : "text/calendar",
                                                  "uri" : "task"
                                                },
                         "source/memo"        : { "sync" : "disabled",
                                                  "backend" : "file",
                                                  "database" : "file://" + xdg_root + "/memo",
                                                  "databaseFormat" : "text/calendar",
                                                  "uri" : "text"
                                                }
                       }
        # update config
        self.updateConfig = { 
                               "" : { "username" : "doe"},
                               "source/addressbook" : { "sync" : "slow"}
                            }
        self.sources = ['addressbook', 'calendar', 'todo', 'memo']

        # set by SessionReady signal handlers in some tests
        self.auto_sync_session_path = None

    def run(self, result):
        self.runTest(result)

    def clearAllConfig(self):
        """ clear a server config. All should be removed. Used internally. """
        emptyConfig = {}
        self.session.SetConfig(False, False, emptyConfig, utf8_strings=True)

    def setupConfig(self):
        """ create a server with full config. Used internally. """
        self.session.SetConfig(False, False, self.config, utf8_strings=True)

    def testTemporaryConfig(self):
        """TestSessionAPIsDummy.testTemporaryConfig - various temporary config changes"""
        ref = { "": { "loglevel": "2", "configName": "dummy-test" } }
        config = copy.deepcopy(ref)
        self.session.SetConfig(False, False, config, utf8_strings=True)
        # reset
        self.session.SetConfig(False, True, {}, utf8_strings=True)
        self.assertEqual(config, self.session.GetConfig(False, utf8_strings=True))
        # add sync prop
        self.session.SetConfig(True, True, { "": { "loglevel": "100" } }, utf8_strings=True)
        config[""]["loglevel"] = "100"
        self.assertEqual(config, self.session.GetConfig(False, utf8_strings=True))
        # add source
        self.session.SetConfig(True, True, { "source/foobar": { "sync": "two-way" } }, utf8_strings=True)
        config["source/foobar"] = { "sync": "two-way" }
        self.session.SetConfig(True, True, { "": { "loglevel": "100" } }, utf8_strings=True)
        # add source prop
        self.session.SetConfig(True, True, { "source/foobar": { "database": "xyz" } }, utf8_strings=True)
        config["source/foobar"]["database"] = "xyz"
        self.assertEqual(config, self.session.GetConfig(False, utf8_strings=True))
        # reset temporary settings
        self.session.SetConfig(False, True, { }, utf8_strings=True)
        config = copy.deepcopy(ref)
        self.assertEqual(config, self.session.GetConfig(False, utf8_strings=True))

    def testCreateGetConfig(self):
        """TestSessionAPIsDummy.testCreateGetConfig -  test the config is created successfully. """
        self.setUpConfigListeners()
        self.config[""]["username"] = "creategetconfig"
        self.config[""]["password"] = "112233445566778"
        self.setupConfig()
        """ get config and compare """
        config = self.session.GetConfig(False, utf8_strings=True)
        self.assertEqual(config, self.config)
        # terminate session and check whether a "config changed" signal
        # was sent as required
        self.session.Detach()
        loop.run()
        self.assertEqual(DBusUtil.events, ["ConfigChanged"])

    def testUpdateConfig(self):
        """TestSessionAPIsDummy.testUpdateConfig -  test the config is permenantly updated correctly. """
        self.setupConfig()
        """ update the given config """
        self.session.SetConfig(True, False, self.updateConfig, utf8_strings=True)
        config = self.session.GetConfig(False, utf8_strings=True)
        self.assertEqual(config[""]["username"], "doe")
        self.assertEqual(config["source/addressbook"]["sync"], "slow")

    def testUpdateConfigTemp(self):
        """TestSessionAPIsDummy.testUpdateConfigTemp -  test the config is just temporary updated but no effect in storage. """
        self.setupConfig()
        """ set config temporary """
        self.session.SetConfig(True, True, self.updateConfig, utf8_strings=True)
        self.session.Detach()
        """ creat a new session to lose the temporary configs """
        self.setUpSession("dummy-test")
        config = self.session.GetConfig(False, utf8_strings=True)
        """ no change of any properties """
        self.assertEqual(config, self.config)

    def testGetConfigUpdateConfigTemp(self):
        """TestSessionAPIsDummy.testGetConfigUpdateConfigTemp -  test the config is temporary updated and in effect for GetConfig in the current session. """
        self.setupConfig()
        """ set config temporary """
        self.session.SetConfig(True, True, self.updateConfig, utf8_strings=True)
        """ GetConfig is affected """
        config = self.session.GetConfig(False, utf8_strings=True)
        """ no change of any properties """
        self.assertEqual(config[""]["username"], "doe")
        self.assertEqual(config["source/addressbook"]["sync"], "slow")

    def testGetConfigWithTempConfig(self):
        """TestSessionAPIsDummy.testGetConfigWithTempConfig -  test the config is gotten for a new temporary config. """
        """ The given config doesn't exist on disk and it's set temporarily. Then GetConfig should
            return the configs temporarily set. """
        self.session.SetConfig(True, True, self.config, utf8_strings=True)
        config = self.session.GetConfig(False, utf8_strings=True)
        self.assertEqual(config, self.config)

    def testUpdateConfigError(self):
        """TestSessionAPIsDummy.testUpdateConfigError -  test the right error is reported when an invalid property value is set """
        self.setupConfig()
        config = { 
                     "source/addressbook" : { "sync" : "invalid-value"}
                  }
        try:
            self.session.SetConfig(True, False, config, utf8_strings=True)
        except dbus.DBusException, ex:
            self.assertEqual(str(ex),
                                 "org.syncevolution.InvalidCall: invalid value 'invalid-value' for "
                                 "property 'sync': 'not one of the valid values (two-way, slow, "
                                 "refresh-from-local, refresh-from-remote = refresh, one-way-from-local, "
                                 "one-way-from-remote = one-way, refresh-from-client = refresh-client, "
                                 "refresh-from-server = refresh-server, one-way-from-client = one-way-client, "
                                 "one-way-from-server = one-way-server, "
                                 "local-cache-slow, local-cache-incremental = local-cache, "
                                 "disabled = none)'")
        else:
            self.fail("no exception thrown")

    def testUpdateNoConfig(self):
        """TestSessionAPIsDummy.testUpdateNoConfig -  test the right error is reported when updating properties for a non-existing server """
        try:
            self.session.SetConfig(True, False, self.updateConfig, utf8_strings=True)
        except dbus.DBusException, ex:
            self.assertEqual(str(ex),
                                 "org.syncevolution.NoSuchConfig: The configuration 'dummy-test' doesn't exist")
        else:
            self.fail("no exception thrown")

    def testUnknownConfigContent(self):
        """TestSessionAPIsDummy.testUnknownConfigContent - config with unkown must be rejected"""
        self.setupConfig()

        try:
            config1 = copy.deepcopy(self.config)
            config1[""]["no-such-sync-property"] = "foo"
            self.session.SetConfig(False, False, config1, utf8_strings=True)
        except dbus.DBusException, ex:
            self.assertEqual(str(ex),
                                 "org.syncevolution.InvalidCall: unknown property 'no-such-sync-property'")
        else:
            self.fail("no exception thrown")

        try:
            config1 = copy.deepcopy(self.config)
            config1["source/addressbook"]["no-such-source-property"] = "foo"
            self.session.SetConfig(False, False, config1, utf8_strings=True)
        except dbus.DBusException, ex:
            self.assertEqual(str(ex),
                                 "org.syncevolution.InvalidCall: unknown property 'no-such-source-property'")
        else:
            self.fail("no exception thrown")

        try:
            config1 = copy.deepcopy(self.config)
            config1["no-such-key"] = { "foo": "bar" }
            self.session.SetConfig(False, False, config1, utf8_strings=True)
        except dbus.DBusException, ex:
            self.assertEqual(str(ex),
                                 "org.syncevolution.InvalidCall: invalid config entry 'no-such-key'")
        else:
            self.fail("no exception thrown")

    def testClearAllConfig(self):
        """TestSessionAPIsDummy.testClearAllConfig -  test all configs of a server are cleared correctly. """
        """ first set up config and then clear all configs and also check a non-existing config """
        self.setupConfig()
        self.clearAllConfig()
        try:
            config = self.session.GetConfig(False, utf8_strings=True)
        except dbus.DBusException, ex:
            self.assertEqual(str(ex),
                                "org.syncevolution.NoSuchConfig: No configuration 'dummy-test' found")
        else:
            self.fail("no exception thrown")

    def testCheckSourceNoConfig(self):
        """TestSessionAPIsDummy.testCheckSourceNoConfig -  test the right error is reported when the server doesn't exist """
        try:
            self.session.CheckSource("", utf8_strings=True)
        except dbus.DBusException, ex:
            self.assertEqual(str(ex),
                                 "org.syncevolution.NoSuchSource: 'dummy-test' has no '' source")
        else:
            self.fail("no exception thrown")

    def testCheckSourceNoSourceName(self):
        """TestSessionAPIsDummy.testCheckSourceNoSourceName -  test the right error is reported when the source doesn't exist """
        self.setupConfig()
        try:
            self.session.CheckSource("dummy", utf8_strings=True)
        except dbus.DBusException, ex:
            self.assertEqual(str(ex),
                                 "org.syncevolution.NoSuchSource: 'dummy-test' "
                                 "has no 'dummy' source")
        else:
            self.fail("no exception thrown")

    def testCheckSourceInvalidDatabase(self):
        """TestSessionAPIsDummy.testCheckSourceInvalidEvolutionSource -  test the right error is reported when the evolutionsource is invalid """
        self.setupConfig()
        config = { "source/memo" : { "database" : "impossible-source"} }
        self.session.SetConfig(True, False, config, utf8_strings=True)
        try:
            self.session.CheckSource("memo", utf8_strings=True)
        except dbus.DBusException, ex:
            self.assertEqual(str(ex),
                                 "org.syncevolution.SourceUnusable: The source 'memo' is not usable")
        else:
            self.fail("no exception thrown")

    def testCheckSourceInvalidBackend(self):
        """TestSessionAPIsDummy.testCheckSourceInvalidBackend -  test the right error is reported when the type is invalid """
        self.setupConfig()
        config = { "source/memo" : { "backend" : "no-such-backend"} }
        try:
            self.session.SetConfig(True, False, config, utf8_strings=True)
        except dbus.DBusException, ex:
            expected = "org.syncevolution.InvalidCall: invalid value 'no-such-backend' for property 'backend': "
            self.assertEqual(str(ex)[0:len(expected)], expected)
        else:
            self.fail("no exception thrown")

    def testCheckSourceNoBackend(self):
        """TestSessionAPIsDummy.testCheckSourceNoBackend -  test the right error is reported when the source is unusable"""
        self.setupConfig()
        config = { "source/memo" : { "backend" : "file",
                                     "databaseFormat" : "text/calendar",
                                     "database" : "file:///no/such/path" } }
        self.session.SetConfig(True, False, config, utf8_strings=True)
        try:
            self.session.CheckSource("memo", utf8_strings=True)
        except dbus.DBusException, ex:
            self.assertEqual(str(ex),
                                 "org.syncevolution.SourceUnusable: The source 'memo' is not usable")
        else:
            self.fail("no exception thrown")

    def testCheckSource(self):
        """TestSessionAPIsDummy.testCheckSource - testCheckSource - test all sources are okay"""
        self.setupConfig()
        try:
            for source in self.sources:
                self.session.CheckSource(source, utf8_strings=True)
        except dbus.DBusException, ex:
            self.fail(ex)

    def testCheckSourceUpdateConfigTemp(self):
        """TestSessionAPIsDummy.testCheckSourceUpdateConfigTemp -  test the config is temporary updated and in effect for GetDatabases in the current session. """
        self.setupConfig()
        tempConfig = {"source/temp" : { "backend" : "calendar"}}
        self.session.SetConfig(True, True, tempConfig, utf8_strings=True)
        databases2 = self.session.CheckSource("temp", utf8_strings=True)

    def testGetDatabasesNoConfig(self):
        """TestSessionAPIsDummy.testGetDatabasesNoConfig -  test the right error is reported when the server doesn't exist """
        # make sure the config doesn't exist """
        try:
            self.session.GetDatabases("", utf8_strings=True)
        except dbus.DBusException, ex:
            self.assertEqual(str(ex),
                                 "org.syncevolution.NoSuchSource: 'dummy-test' has no '' source")
        else:
            self.fail("no exception thrown")

    def testGetDatabasesEmpty(self):
        """TestSessionAPIsDummy.testGetDatabasesEmpty -  test the right error is reported for non-existing source"""
        self.setupConfig()
        try:
            databases = self.session.GetDatabases("never_use_this_source_name", utf8_strings=True)
        except dbus.DBusException, ex:
            self.assertEqual(str(ex),
                                 "org.syncevolution.NoSuchSource: 'dummy-test' has no 'never_use_this_source_name' source")
        else:
            self.fail("no exception thrown")

    def testGetDatabases(self):
        """TestSessionAPIsDummy.testGetDatabases -  test the right way to get databases """
        self.setupConfig()

        # don't know actual databases, so compare results of two different times
        sources = ['addressbook', 'calendar', 'todo', 'memo']
        databases1 = []
        for source in sources:
            databases1.append(self.session.GetDatabases(source, utf8_strings=True))
        # reverse the list of sources and get databases again
        sources.reverse()
        databases2 = []
        for source in sources:
            databases2.append(self.session.GetDatabases(source, utf8_strings=True))
        # sort two arrays
        databases1.sort()
        databases2.sort()
        self.assertEqual(databases1, databases2)

    def testGetDatabasesUpdateConfigTemp(self):
        """TestSessionAPIsDummy.testGetDatabasesUpdateConfigTemp -  test the config is temporary updated and in effect for GetDatabases in the current session. """
        self.setupConfig()
        # file backend: reports a short help text instead of a real database list
        databases1 = self.session.GetDatabases("calendar", utf8_strings=True)
        # databaseFormat is required for file backend, otherwise it
        # cannot be instantiated and even simple operations as reading
        # the (in this case fixed) list of databases fail
        tempConfig = {"source/temp" : { "backend" : "file", "databaseFormat" : "text/calendar" }}
        self.session.SetConfig(True, True, tempConfig, utf8_strings=True)
        databases2 = self.session.GetDatabases("temp", utf8_strings=True)
        self.assertEqual(databases2, databases1)

    def testGetReportsNoConfig(self):
        """TestSessionAPIsDummy.testGetReportsNoConfig -  Test nothing is gotten when the given server doesn't exist. Also covers boundaries """
        reports = self.session.GetReports(0, 0, utf8_strings=True)
        self.assertEqual(reports, [])
        reports = self.session.GetReports(0, 1, utf8_strings=True)
        self.assertEqual(reports, [])
        reports = self.session.GetReports(0, 0xFFFFFFFF, utf8_strings=True)
        self.assertEqual(reports, [])
        reports = self.session.GetReports(0xFFFFFFFF, 0xFFFFFFFF, utf8_strings=True)
        self.assertEqual(reports, [])

    def testGetReportsNoReports(self):
        """TestSessionAPIsDummy.testGetReportsNoReports -  Test when the given server has no reports. Also covers boundaries """
        self.setupConfig()
        reports = self.session.GetReports(0, 0, utf8_strings=True)
        self.assertEqual(reports, [])
        reports = self.session.GetReports(0, 1, utf8_strings=True)
        self.assertEqual(reports, [])
        reports = self.session.GetReports(0, 0xFFFFFFFF, utf8_strings=True)
        self.assertEqual(reports, [])
        reports = self.session.GetReports(0xFFFFFFFF, 0xFFFFFFFF, utf8_strings=True)
        self.assertEqual(reports, [])

    def testGetReportsByRef(self):
        """TestSessionAPIsDummy.testGetReportsByRef -  Test the reports are gotten correctly from reference files. Also covers boundaries """
        """ This could be extractly compared since the reference files are known """
        self.setUpFiles('reports')
        report0 = { "peer" : "dummy-test",
                    "start" : "1258520955",
                    "end" : "1258520964",
                    "status" : "200",
                    "source-addressbook-mode" : "slow",
                    "source-addressbook-first" : "true",
                    "source-addressbook-resume" : "false",
                    "source-addressbook-status" : "0",
                    "source-addressbook-backup-before" : "0",
                    "source-addressbook-backup-after" : "0",
                    "source-addressbook-stat-local-any-sent" : "9168",
                    "source-addressbook-stat-remote-added-total" : "71",
                    "source-addressbook-stat-remote-updated-total" : "100",
                    "source-addressbook-stat-local-updated-total" : "632",
                    "source-addressbook-stat-remote-any-reject" : "100",
                    "source-addressbook-stat-remote-any-conflict_duplicated" : "5293487",
                    "source-addressbook-stat-remote-any-conflict_client_won" : "33",
                    "source-addressbook-stat-local-any-received" : "2",
                    "source-addressbook-stat-local-removed-total" : "4",
                    "source-addressbook-stat-remote-any-conflict_server_won" : "38",
                    "source-addressbook-stat-local-any-reject" : "77",
                    "source-addressbook-stat-local-added-total" : "84",
                    "source-addressbook-stat-remote-removed-total" : "66",
                    "source-calendar-mode" : "slow",
                    "source-calendar-first" : "true",
                    "source-calendar-resume" : "false",
                    "source-calendar-status" : "0",
                    "source-calendar-backup-before" : "17",
                    "source-calendar-backup-after" : "17",
                    "source-calendar-stat-local-any-sent" : "8619",
                    "source-calendar-stat-remote-added-total": "17",
                    "source-calendar-stat-remote-updated-total" : "10",
                    "source-calendar-stat-local-updated-total" : "6",
                    "source-calendar-stat-remote-any-reject" : "1",
                    "source-calendar-stat-remote-any-conflict_duplicated" : "5",
                    "source-calendar-stat-remote-any-conflict_client_won" : "3",
                    "source-calendar-stat-local-any-received" : "24",
                    "source-calendar-stat-local-removed-total" : "54",
                    "source-calendar-stat-remote-any-conflict_server_won" : "38",
                    "source-calendar-stat-local-any-reject" : "7",
                    "source-calendar-stat-local-added-total" : "42",
                    "source-calendar-stat-remote-removed-total" : "6",
                    "source-memo-mode" : "slow",
                    "source-memo-first" : "true",
                    "source-memo-resume" : "false",
                    "source-memo-status" : "0",
                    "source-memo-backup-before" : "3",
                    "source-memo-backup-after" : "4",
                    "source-memo-stat-local-any-sent" : "8123",
                    "source-memo-stat-remote-added-total" : "15",
                    "source-memo-stat-remote-updated-total" : "6",
                    "source-memo-stat-local-updated-total" : "8",
                    "source-memo-stat-remote-any-reject" : "16",
                    "source-memo-stat-remote-any-conflict_duplicated" : "27",
                    "source-memo-stat-remote-any-conflict_client_won" : "2",
                    "source-memo-stat-local-any-received" : "3",
                    "source-memo-stat-local-removed-total" : "4",
                    "source-memo-stat-remote-any-conflict_server_won" : "8",
                    "source-memo-stat-local-any-reject" : "40",
                    "source-memo-stat-local-added-total" : "34",
                    "source-memo-stat-remote-removed-total" : "5",
                    "source-todo-mode" : "slow",
                    "source-todo-first" : "true",
                    "source-todo-resume" : "false",
                    "source-todo-status" : "0",
                    "source-todo-backup-before" : "2",
                    "source-todo-backup-after" : "2",
                    "source-todo-stat-local-any-sent" : "619",
                    "source-todo-stat-remote-added-total" : "71",
                    "source-todo-stat-remote-updated-total" : "1",
                    "source-todo-stat-local-updated-total" : "9",
                    "source-todo-stat-remote-any-reject" : "10",
                    "source-todo-stat-remote-any-conflict_duplicated" : "15",
                    "source-todo-stat-remote-any-conflict_client_won" : "7",
                    "source-todo-stat-local-any-received" : "2",
                    "source-todo-stat-local-removed-total" : "4",
                    "source-todo-stat-remote-any-conflict_server_won" : "8",
                    "source-todo-stat-local-any-reject" : "3",
                    "source-todo-stat-local-added-total" : "24",
                    "source-todo-stat-remote-removed-total" : "80" }
        reports = self.session.GetReports(0, 0, utf8_strings=True)
        self.assertEqual(reports, [])
        # get only one report
        reports = self.session.GetReports(0, 1, utf8_strings=True)
        self.assertTrue(len(reports) == 1)
        del reports[0]["dir"]

        self.assertEqual(reports[0], report0)
        """ the number of reference sessions is totally 5. Check the returned count
        when parameter is bigger than 5 """
        reports = self.session.GetReports(0, 0xFFFFFFFF, utf8_strings=True)
        self.assertTrue(len(reports) == 5)
        # start from 2, this could check integer overflow
        reports2 = self.session.GetReports(2, 0xFFFFFFFF, utf8_strings=True)
        self.assertTrue(len(reports2) == 3)
        # the first element of reports2 should be the same as the third element of reports
        self.assertEqual(reports[2], reports2[0])
        # indexed from 5, nothing could be gotten
        reports = self.session.GetReports(5, 0xFFFFFFFF, utf8_strings=True)
        self.assertEqual(reports, [])

    def testRestoreByRef(self):
        """TestSessionAPIsDummy.testRestoreByRef - restore data before or after a given session"""
        self.setUpFiles('restore')
        self.setupConfig()
        self.setUpListeners(self.sessionpath)
        reports = self.session.GetReports(0, 1, utf8_strings=True)
        dir = reports[0]["dir"]
        sessionpath, session = self.createSession("dummy-test", False)
        #TODO: check restore result, how?
        #restore data before this session
        self.session.Restore(dir, True, [], utf8_strings=True)
        loop.run()
        self.session.Detach()

        # check recorded events in DBusUtil.events, first filter them
        statuses = []
        progresses = []
        for item in DBusUtil.events:
            if item[0] == "status":
                statuses.append(item[1])
            elif item[0] == "progress":
                progresses.append(item[1])

        lastStatus = ""
        lastSources = {}
        statusPairs = {"": 0, "idle": 1, "running" : 2, "done" : 3}
        for status, error, sources in statuses:
            self.assertFalse(status == lastStatus and lastSources == sources)
            # no error
            self.assertEqual(error, 0)
            for sourcename, value in sources.items():
                # no error
                self.assertEqual(value[2], 0)
                # keep order: source status must also be unchanged or the next status
                if lastSources.has_key(sourcename):
                    lastValue = lastSources[sourcename]
                    self.assertTrue(statusPairs[value[1]] >= statusPairs[lastValue[1]])

            lastStatus = status
            lastSources = sources

        # check increasing progress percentage
        lastPercent = 0
        for percent, sources in progresses:
            self.assertFalse(percent < lastPercent)
            lastPercent = percent

        session.SetConfig(False, False, self.config, utf8_strings=True)
        #restore data after this session
        session.Restore(dir, False, ["addressbook", "calendar"], utf8_strings=True)
        loop.run()

    def testSecondRestore(self):
        """TestSessionAPIsDummy.testSecondRestore - right error thrown when session is not active?"""
        self.setUpFiles('restore')
        self.setupConfig()
        self.setUpListeners(self.sessionpath)
        reports = self.session.GetReports(0, 1, utf8_strings=True)
        dir = reports[0]["dir"]
        sessionpath, session = self.createSession("dummy-test", False)
        try:
            session.Restore(dir, False, [], utf8_strings=True)
        except dbus.DBusException, ex:
            self.assertEqual(str(ex),
                    "org.syncevolution.InvalidCall: session is not active, call not allowed at this time")
        else:
            self.fail("no exception thrown")

        self.session.Detach()
        session.SetConfig(False, False, self.config, utf8_strings=True)
        session.Restore(dir, False, [], utf8_strings=True)
        loop.run()

    @timeout(300)
    def testInteractivePassword(self):
        """TestSessionAPIsDummy.testInteractivePassword -  test the info request is correctly working for password """
        self.setupConfig()
        self.setUpListeners(self.sessionpath)
        self.lastState = "unknown"
        # define callback for InfoRequest signals and send corresponds response
        # to dbus server
        def infoRequest(id, session, state, handler, type, params):
            if state == "request":
                self.assertEqual(self.lastState, "unknown")
                self.lastState = "request"
                self.server.InfoResponse(id, "working", {}, utf8_strings=True)
            elif state == "waiting":
                self.assertEqual(self.lastState, "request")
                self.lastState = "waiting"
                self.server.InfoResponse(id, "response", {"password" : "123456"}, utf8_strings=True)
            elif state == "done":
                self.assertEqual(self.lastState, "waiting")
                self.lastState = "done"
            else:
                self.fail("state should not be '" + state + "'")

        signal = bus.add_signal_receiver(infoRequest,
                                         'InfoRequest',
                                         'org.syncevolution.Server',
                                         self.server.bus_name,
                                         None,
                                         byte_arrays=True,
                                         utf8_strings=True)

        # dbus server will be blocked by gnome-keyring-ask dialog, so we kill it, and then 
        # it can't get the password from gnome keyring and send info request for password
        def callback():
            kill = subprocess.Popen('killall -9 gnome-keyring-ask'.split(),
                                    stdout=open('/dev/null', 'w'),
                                    stderr=subprocess.STDOUT)
            kill.wait()
            # Kill again soon.
            return True

        timeout_handler = Timeout.addTimeout(1, callback)

        # try to sync and invoke password request
        self.session.Sync("", {})
        loop.run()
        Timeout.removeTimeout(timeout_handler)
        self.assertEqual(self.lastState, "done")

    def doAutoSyncNetworkFailure(self):
        """TestSessionAPIsDummy.testAutoSyncNetworkFailure - test that auto-sync is triggered, fails due to (temporary?!) network error here"""
        self.setupConfig()
        # enable auto-sync
        config = copy.deepcopy(self.config)
        # Note that writing this config will modify the host's keyring!
        # Use a syncURL that is unlikely to conflict with the host
        # or any other D-Bus test.
        config[""]["syncURL"] = "http://no-such-domain.foobar"
        config[""]["autoSync"] = "1"
        config[""]["autoSyncDelay"] = "1"
        config[""]["autoSyncInterval"] = "10s"
        config[""]["password"] = "foobar"
        self.session.SetConfig(True, False, config, utf8_strings=True)

        def session_ready(object, ready):
            if self.running and object != self.sessionpath and \
                (self.auto_sync_session_path == None and ready or \
                 self.auto_sync_session_path == object):
                self.auto_sync_session_path = object
                DBusUtil.quit_events.append("session " + object + (ready and " ready" or " done"))
                loop.quit()

        signal = bus.add_signal_receiver(session_ready,
                                         'SessionChanged',
                                         'org.syncevolution.Server',
                                         self.server.bus_name,
                                         None,
                                         byte_arrays=True,
                                         utf8_strings=True)

        # shut down current session, will allow auto-sync
        self.session.Detach()

        # wait for start and end of auto-sync session
        loop.run()
        start1 = time.time()
        loop.run()
        self.assertEqual(DBusUtil.quit_events, ["session " + self.auto_sync_session_path + " ready",
                                                "session " + self.auto_sync_session_path + " done"])
        DBusUtil.quit_events = []
        # session must be around for a while after terminating, to allow
        # reading information about it by clients who didn't start it
        # and thus wouldn't know what the session was about otherwise
        session = dbus.Interface(bus.get_object(self.server.bus_name,
                                                self.auto_sync_session_path),
                                 'org.syncevolution.Session')
        reports = session.GetReports(0, 100, utf8_strings=True)
        self.assertEqual(len(reports), 1)
        self.assertEqual(reports[0]["status"], "20043")
        name = session.GetConfigName()
        self.assertEqual(name, "dummy-test")
        flags = session.GetFlags()
        self.assertEqual(flags, [])
        first_auto = self.auto_sync_session_path
        self.auto_sync_session_path = None

        # check that interval between auto-sync sessions is right
        loop.run()
        start2 = time.time()
        loop.run()
        self.assertEqual(DBusUtil.quit_events, ["session " + self.auto_sync_session_path + " ready",
                                                "session " + self.auto_sync_session_path + " done"])
        self.assertNotEqual(first_auto, self.auto_sync_session_path)
        delta = start2 - start1
        # avoid timing checks when running under valgrind
        if not usingValgrind():
            self.assertTrue(delta < 13)
            self.assertTrue(delta > 7)

        # check that org.freedesktop.Notifications.Notify was not called
        # (network errors are considered temporary, can't tell in this case
        # that the name lookup error is permanent)
        def checkDBusLog(self, content):
            notifications = GrepNotifications(content)
            self.assertEqual(notifications, [])

        # done as part of post-processing in runTest()
        self.runTestDBusCheck = checkDBusLog

    @timeout(60)
    @property("ENV", "LC_ALL=en_US.UTF-8 LANGUAGE=en_US")
    def testAutoSyncNetworkFailure(self):
        """TestSessionAPIsDummy.testAutoSyncNetworkFailure - test that auto-sync is triggered, fails due to (temporary?!) network error here"""
        self.doAutoSyncNetworkFailure()

    @timeout(60)
    @property("ENV", "DBUS_TEST_CONNMAN=session DBUS_TEST_NETWORK_MANAGER=session "
                     "LC_ALL=en_US.UTF-8 LANGUAGE=en_US")
    def testAutoSyncNoNetworkManager(self):
        """TestSessionAPIsDummy.testAutoSyncNoNetworkManager - test that auto-sync is triggered despite having neither NetworkManager nor Connman, fails due to (temporary?!) network error here"""
        self.doAutoSyncNetworkFailure()

    @timeout(60)
    def doAutoSyncLocalConfigError(self, notifyLevel,
                                   notification=
                                   '   string "SyncEvolution"\n'
                                   '   uint32 0\n'
                                   '   string ""\n'
                                   '   string "Sync problem."\n'
                                   '   string "Sorry, there\'s a problem with your sync that you need to attend to."\n'
                                   '   array [\n'
                                   '      string "view"\n'
                                   '      string "View"\n'
                                   '      string "default"\n'
                                   '      string "Dismiss"\n'
                                   '   ]\n'
                                   '   array [\n'
                                   '   ]\n'
                                   '   int32 -1\n'):
        self.setupConfig()
        # enable auto-sync
        config = copy.deepcopy(self.config)
        config[""]["syncURL"] = "local://@foobar" # will fail
        config[""]["autoSync"] = "1"
        config[""]["autoSyncDelay"] = "0"
        config[""]["autoSyncInterval"] = "10s"
        config[""]["password"] = "foobar"
        if notifyLevel != 3:
            config[""]["notifyLevel"] = str(notifyLevel)
        self.session.SetConfig(True, False, config, utf8_strings=True)

        def session_ready(object, ready):
            if self.running and object != self.sessionpath and \
                (self.auto_sync_session_path == None and ready or \
                 self.auto_sync_session_path == object):
                self.auto_sync_session_path = object
                DBusUtil.quit_events.append("session " + object + (ready and " ready" or " done"))
                loop.quit()

        signal = bus.add_signal_receiver(session_ready,
                                         'SessionChanged',
                                         'org.syncevolution.Server',
                                         self.server.bus_name,
                                         None,
                                         byte_arrays=True,
                                         utf8_strings=True)

        # shut down current session, will allow auto-sync
        self.session.Detach()

        # wait for start and end of auto-sync session
        loop.run()
        loop.run()
        self.assertEqual(DBusUtil.quit_events, ["session " + self.auto_sync_session_path + " ready",
                                                "session " + self.auto_sync_session_path + " done"])
        session = dbus.Interface(bus.get_object(self.server.bus_name,
                                                self.auto_sync_session_path),
                                 'org.syncevolution.Session')
        reports = session.GetReports(0, 100, utf8_strings=True)
        self.assertEqual(len(reports), 1)
        self.assertEqual(reports[0]["status"], "10500")
        name = session.GetConfigName()
        self.assertEqual(name, "dummy-test")
        flags = session.GetFlags()
        self.assertEqual(flags, [])

        # check that org.freedesktop.Notifications.Notify was called
        # once to report the failed attempt to start the sync
        def checkDBusLog(self, content):
            notifications = GrepNotifications(content)
            self.assertEqual(notifications,
                             notifyLevel >= 1 and [notification]
                             or [])

        # check that no other session is started at the time of
        # the next regular auto sync session
        def testDone():
            DBusUtil.quit_events.append("test done")
            loop.quit()
            return False
        def any_session_ready(object, ready):
            if self.running:
                DBusUtil.quit_events.append("session " + object + (ready and " ready" or " done"))
                loop.quit()
        signal = bus.add_signal_receiver(any_session_ready,
                                         'SessionChanged',
                                         'org.syncevolution.Server',
                                         self.server.bus_name,
                                         None,
                                         byte_arrays=True,
                                         utf8_strings=True)

        try:
            timeout = glib.timeout_add(15 * 1000, testDone)
            loop.run()
        finally:
            glib.source_remove(timeout)
        self.assertEqual(DBusUtil.quit_events, ["session " + self.auto_sync_session_path + " ready",
                                                "session " + self.auto_sync_session_path + " done",
                                                "test done"])

        # done as part of post-processing in runTest()
        self.runTestDBusCheck = checkDBusLog

    @timeout(60)
    @property("ENV", "LC_ALL=en_US.UTF-8 LANGUAGE=en_US")
    def testAutoSyncLocalConfigError(self):
        """TestSessionAPIsDummy.testAutoSyncLocalConfigError - test that auto-sync is triggered for local sync, fails due to permanent config error here"""
        self.doAutoSyncLocalConfigError(3)

    @timeout(60)
    @property("ENV", "LC_ALL=de_DE.UTF-8 LANGUAGE=de_DE:de")
    def testAutoSyncLocalConfigErrorGerman(self):
        """TestSessionAPIsDummy.testAutoSyncLocalConfigErrorGerman - test that auto-sync is triggered for local sync, fails due to permanent config error here"""
        self.doAutoSyncLocalConfigError(3,
                                        '   string "SyncEvolution"\n'
                                        '   uint32 0\n'
                                        '   string ""\n'
                                        '   string "Sync-Problem"\n'
                                        '   string "Ein Problem mit der Synchronisation bedarf deiner Aufmerksamkeit."\n'
                                        '   array [\n'
                                        '      string "view"\n'
                                        '      string "Anzeigen"\n'
                                        '      string "default"\n'
                                        '      string "Ignorieren"\n'
                                        '   ]\n'
                                        '   array [\n'
                                        '   ]\n'
                                        '   int32 -1\n')

    @timeout(60)
    @property("ENV", "LC_ALL=en_US.UTF-8 LANGUAGE=en_US")
    def testAutoSyncLocalConfigErrorEssential(self):
        """TestSessionAPIsDummy.testAutoSyncLocalConfigErrorEssential - test that auto-sync is triggered for local sync, fails due to permanent config error here, with only the essential error notification"""
        self.doAutoSyncLocalConfigError(1)

    @timeout(60)
    @property("ENV", "LC_ALL=en_US.UTF-8 LANGUAGE=en_US")
    def testAutoSyncLocalConfigErrorQuiet(self):
        """TestSessionAPIsDummy.testAutoSyncLocalConfigErrorQuiet - test that auto-sync is triggered for local sync, fails due to permanent config error here, with no notification"""
        self.doAutoSyncLocalConfigError(0)

    def doAutoSyncLocalSuccess(self, notifyLevel, repeat=False):
        # create @foobar config
        self.session.Detach()
        self.setUpSession("target-config@foobar")
        config = copy.deepcopy(self.config)
        config[""]["remoteDeviceId"] = "foo"
        config[""]["deviceId"] = "bar"
        del config[""]["password"]
        for i in ("addressbook", "calendar", "todo", "memo"):
            source = config["source/" + i]
            source["database"] = source["database"] + ".server"
        self.session.SetConfig(False, False, config, utf8_strings=True)
        self.session.Detach()

        # create dummy-test@default auto-sync config
        self.setUpSession("dummy-test")
        config = copy.deepcopy(self.config)
        config[""]["syncURL"] = "local://@foobar"
        config[""]["PeerIsClient"] = "1"
        config[""]["autoSync"] = "1"
        config[""]["autoSyncDelay"] = "0"
        if notifyLevel != 3:
            config[""]["notifyLevel"] = str(notifyLevel)
        del config[""]["password"]
        # must be small enough (otherwise test runs a long time)
        # but not too small (otherwise the next sync already starts
        # before we can check the result and kill the daemon)
        config[""]["autoSyncInterval"] = usingValgrind() and "60s" or "10s"
        config["source/addressbook"]["uri"] = "addressbook"
        self.session.SetConfig(False, False, config, utf8_strings=True)

        def session_ready(object, ready):
            if self.running and object != self.sessionpath and \
                (self.auto_sync_session_path == None and ready or \
                 self.auto_sync_session_path == object):
                self.auto_sync_session_path = object
                DBusUtil.quit_events.append("session " + object + (ready and " ready" or " done"))
                loop.quit()

        signal = bus.add_signal_receiver(session_ready,
                                         'SessionChanged',
                                         'org.syncevolution.Server',
                                         self.server.bus_name,
                                         None,
                                         byte_arrays=True,
                                         utf8_strings=True)

        # shut down current session, will allow auto-sync
        self.session.Detach()

        # wait for start and end of auto-sync session
        def run(operation, numSyncs):
            logging.log(operation)
            loop.run()
            loop.run()
            self.assertEqual(DBusUtil.quit_events, ["session " + self.auto_sync_session_path + " ready",
                                                    "session " + self.auto_sync_session_path + " done"])
            session = dbus.Interface(bus.get_object(self.server.bus_name,
                                                    self.auto_sync_session_path),
                                     'org.syncevolution.Session')
            reports = session.GetReports(0, 100, utf8_strings=True)
            self.assertEqual(len(reports), numSyncs)
            self.assertEqual(reports[0]["status"], "200")
            name = session.GetConfigName()
            self.assertEqual(name, "dummy-test")
            flags = session.GetFlags()
            self.assertEqual(flags, [])
            DBusUtil.quit_events = []
            self.auto_sync_session_path = None

        run('waiting for first auto sync', 1)

        # check that org.freedesktop.Notifications.Notify was called
        # when starting and completing the sync
        def checkDBusLog(self, content):
            notifications = GrepNotifications(content)
            self.assertEqual(notifications,
                             notifyLevel >= 3 and
                             ['   string "SyncEvolution"\n'
                              '   uint32 0\n'
                              '   string ""\n'
                              '   string "dummy-test is syncing"\n'
                              '   string "We have just started to sync your computer with the dummy-test sync service."\n'
                              '   array [\n'
                              '      string "view"\n'
                              '      string "View"\n'
                              '      string "default"\n'
                              '      string "Dismiss"\n'
                              '   ]\n'
                              '   array [\n'
                              '   ]\n'
                              '   int32 -1\n',

                              '   string "SyncEvolution"\n'
                              '   uint32 0\n'
                              '   string ""\n'
                              '   string "dummy-test sync complete"\n'
                              '   string "We have just finished syncing your computer with the dummy-test sync service."\n'
                              '   array [\n'
                              '      string "view"\n'
                              '      string "View"\n'
                              '      string "default"\n'
                              '      string "Dismiss"\n'
                              '   ]\n'
                              '   array [\n'
                              '   ]\n'
                              '   int32 -1\n']
                             or [])

        if repeat:
            for i in range(1,3):
                run('waiting for auto sync #%d' % i, i + 1)
        else:
            # done as part of post-processing in runTest()
            self.runTestDBusCheck = checkDBusLog

    @timeout(120)
    @property("ENV", "LC_ALL=en_US.UTF-8 LANGUAGE=en_US")
    def testAutoSyncLocalSuccess(self):
        """TestSessionAPIsDummy.testAutoSyncLocalSuccess - test that auto-sync is done successfully for local sync between file backends, with notifications"""
        self.doAutoSyncLocalSuccess(3)

    @timeout(120)
    @property("ENV", "LC_ALL=en_US.UTF-8 LANGUAGE=en_US")
    def testAutoSyncLocalSuccessQuiet(self):
        """TestSessionAPIsDummy.testAutoSyncLocalSuccessQuiet - test that auto-sync is done successfully for local sync between file backends, without notifications"""
        self.doAutoSyncLocalSuccess(1)

    @timeout(240)
    @property("ENV", "LC_ALL=en_US.UTF-8 LANGUAGE=en_US")
    def testAutoSyncLocalMultiple(self):
        """TestSessionAPIsDummy.testAutoSyncLocalMultiple - test that auto-sync is done successfully for local sync between file backends, several times"""
        self.doAutoSyncLocalSuccess(1, repeat=True)

class TestSessionAPIsReal(DBusUtil, unittest.TestCase):
    """ This class is used to test those unit tests of session APIs, depending on doing sync.
        Thus we need a real server configuration to confirm sync could be run successfully.
        Typically we need make sure that at least one sync has been done before testing our
        desired unit tests. Note that it also covers session.Sync API itself """
    """ All unit tests in this class have a dependency on a real sync
    config named 'dbus_unittest'. That config must have preventSlowSync=0,
    maxLogDirs=1, username, password set such that syncing succeeds
    for at least one source. It does not matter which data is synchronized.
    For example, the following config will work:
    syncevolution --configure --template <server of your choice> \
                  username=<your username> \
                  password=<your password> \
                  preventSlowSync=0 \
                  maxLogDirs=1 \
                  backend=file \
                  database=file:///tmp/test_dbus_data \
                  databaseFormat=text/vcard \
                  dbus_unittest@test-dbus addressbook
                  """

    def setUp(self):
        self.setUpServer()
        self.setUpSession(configName)
        self.operation = "" 

    def run(self, result):
        self.runTest(result, own_xdg=False)

    def setupConfig(self):
        """ Apply for user settings. Used internally. """
        configProps = { }
        # check whether 'dbus_unittest' is configured.
        try:
            configProps = self.session.GetConfig(False, utf8_strings=True)
        except dbus.DBusException, ex:
            self.fail(str(ex) + 
                      ". To test this case, please first set up a correct config named 'dbus_unittest'.")

    def doSync(self):
        self.setupConfig()
        self.setUpListeners(self.sessionpath)
        self.session.Sync("slow", {})
        loop.run()
        self.assertEqual(DBusUtil.quit_events, ["session " + self.sessionpath + " done"])

    def progressChanged(self, *args, **keywords):
        '''abort or suspend once session has progressed far enough'''
        percentage = args[0]
        # make sure sync is really running
        if percentage > 20:
            if self.operation == "abort":
                self.session.Abort()
                self.operation = "aborted"
            if self.operation == "suspend":
                self.session.Suspend()
                self.operation = "suspended"

    @timeout(300)
    def testSync(self):
        """TestSessionAPIsReal.testSync - run a real sync with default server and test status list and progress number"""
        """ check events list is correct for StatusChanged and ProgressChanged """
        # do sync
        self.doSync()
        self.checkSync()
    
    @timeout(300)
    def testSyncStatusAbort(self):
        """TestSessionAPIsReal.testSyncStatusAbort -  test status is set correctly when the session is aborted """
        self.operation = "abort"
        self.doSync()
        hasAbortingStatus = False
        for item in DBusUtil.events:
            if item[0] == "status" and item[1][0] == "aborting":
                hasAbortingStatus = True
                break
        self.assertEqual(hasAbortingStatus, True)

    @timeout(300)
    def testSyncStatusSuspend(self):
        """TestSessionAPIsReal.testSyncStatusSuspend -  test status is set correctly when the session is suspended """
        self.operation = "suspend"
        self.doSync()
        hasSuspendingStatus = False
        for item in DBusUtil.events:
            if item[0] == "status" and "suspending" in item[1][0] :
                hasSuspendingStatus = True
                break
        self.assertEqual(hasSuspendingStatus, True)

    @timeout(300)
    def testSyncSecondSession(self):
        """TestSessionAPIsReal.testSyncSecondSession - ask for a second session that becomes ready after a real sync"""
        sessionpath2, session2 = self.createSession("", False)
        status, error, sources = session2.GetStatus(utf8_strings=True)
        self.assertEqual(status, "queueing")
        self.testSync()
        # now wait for second session becoming ready
        loop.run()
        status, error, sources = session2.GetStatus(utf8_strings=True)
        self.assertEqual(status, "idle")
        self.assertEqual(DBusUtil.quit_events, ["session " + self.sessionpath + " done",
                                                    "session " + sessionpath2 + " ready"])
        session2.Detach()

class TestDBusSyncError(DBusUtil, unittest.TestCase):
    def setUp(self):
        self.setUpServer()
        self.setUpSession(configName)

    def run(self, result):
        self.runTest(result, own_xdg=True)

    def testSyncNoConfig(self):
        """TestDBusSyncError.testSyncNoConfig - Executes a real sync with no corresponding config."""
        self.setUpListeners(self.sessionpath)
        self.session.Sync("", {})
        loop.run()
        # TODO: check recorded events in DBusUtil.events
        status, error, sources = self.session.GetStatus(utf8_strings=True)
        self.assertEqual(status, "done")
        self.assertEqual(error, 10500)

class TestConnection(DBusUtil, unittest.TestCase):
    """Tests Server.Connect(). Tests depend on getting one Abort signal to terminate."""

    """a real message sent to our own server, DevInf stripped, username/password foo/bar"""
    message1 = '''<?xml version="1.0" encoding="UTF-8"?><SyncML xmlns='SYNCML:SYNCML1.2'><SyncHdr><VerDTD>1.2</VerDTD><VerProto>SyncML/1.2</VerProto><SessionID>255</SessionID><MsgID>1</MsgID><Target><LocURI>http://127.0.0.1:9000/syncevolution</LocURI></Target><Source><LocURI>sc-api-nat</LocURI><LocName>test</LocName></Source><Cred><Meta><Format xmlns='syncml:metinf'>b64</Format><Type xmlns='syncml:metinf'>syncml:auth-md5</Type></Meta><Data>kHzMn3RWFGWSKeBpXicppQ==</Data></Cred><Meta><MaxMsgSize xmlns='syncml:metinf'>20000</MaxMsgSize><MaxObjSize xmlns='syncml:metinf'>4000000</MaxObjSize></Meta></SyncHdr><SyncBody><Alert><CmdID>1</CmdID><Data>200</Data><Item><Target><LocURI>addressbook</LocURI></Target><Source><LocURI>./addressbook</LocURI></Source><Meta><Anchor xmlns='syncml:metinf'><Last>20091105T092757Z</Last><Next>20091105T092831Z</Next></Anchor><MaxObjSize xmlns='syncml:metinf'>4000000</MaxObjSize></Meta></Item></Alert><Final/></SyncBody></SyncML>'''

    """a real WBXML message, expected to trigger an authentication failure"""
    message1WBXML = base64.b64decode('AqQBagBtbHEDMS4yAAFyA1N5bmNNTC8xLjIAAWUDMjExAAFbAzEAAW5XA2h0dHA6Ly9teS5mdW5hbWJvbC5jb20vc3luYwABAWdXA3NjLWFwaS1uYXQAAVYDcGF0cmljay5vaGx5AAEBTloAAUcDYjY0AAFTA3N5bmNtbDphdXRoLW1kNQABAQAATwNyT0dFbGR1Y2FjNE5mc3dZSm5lR2lnPT0AAQFaAAFMAzE1MDAwMAABVQM0MDAwMDAwAAEBAQAAa19LAzEAAVoAAVMDYXBwbGljYXRpb24vdm5kLnN5bmNtbC1kZXZpbmYrd2J4bWwAAQEAAFRnVwMuL2RldmluZjEyAAEBT8OMewKkA2oASmUDMS4yAAFRA1BhdHJpY2sgT2hseQABVQNTeW5jRXZvbHV0aW9uAAFWA1N5bnRoZXNpcyBBRwABTwMxLjIuOTkrMjAxMjAzMjcrU0UrMjI1MGVhMCt1bmNsZWFuAAFeAzMuNC4wLjM1AAFQA3Vua25vd24AAUkDc2MtYXBpLW5hdAABSwN3b3Jrc3RhdGlvbgABKCkqR10DLi9lZHNfZXZlbnQAAUwDZWRzX2V2ZW50AAFSAzY0AAFaRgN0ZXh0L2NhbGVuZGFyAAFkAzIuMAABAWJGA3RleHQvY2FsZW5kYXIAAWQDMi4wAAEBRUYDdGV4dC9jYWxlbmRhcgABZAMyLjAAAWtYA0JFR0lOAAFjA1ZDQUxFTkRBUgABYwNWVElNRVpPTkUAAWMDU1RBTkRBUkQAAWMDREFZTElHSFQAAWMDVlRPRE8AAWMDVkFMQVJNAAFjA1ZFVkVOVAABYwNWQUxBUk0AAQFrWANFTkQAAWMDVkNBTEVOREFSAAFjA1ZUSU1FWk9ORQABYwNTVEFOREFSRAABYwNEQVlMSUdIVAABYwNWVE9ETwABYwNWQUxBUk0AAWMDVkVWRU5UAAFjA1ZBTEFSTQABAWtYA1ZFUlNJT04AAWMDMi4wAAFtAzEAAQFrWANQUk9ESUQAAW0DMQABAWtYA1RaSUQAAQFrWANEVFNUQVJUAAEBa1gDUlJVTEUAAQFrWANUWk9GRlNFVEZST00AAQFrWANUWk9GRlNFVFRPAAEBa1gDVFpOQU1FAAEBa1gDTEFTVC1NT0RJRklFRAABbQMxAAEBa1gDRFRTVEFNUAABbQMxAAEBa1gDQ1JFQVRFRAABbQMxAAEBa1gDVUlEAAFtAzEAAQFrWANTRVFVRU5DRQABbQMxAAEBa1gDR0VPAAFtAzEAAQFrWANDQVRFR09SSUVTAAEBa1gDQ0xBU1MAAW0DMQABAWtYA1NVTU1BUlkAAW0DMQABAWtYA0RFU0NSSVBUSU9OAAFtAzEAAQFrWANMT0NBVElPTgABbQMxAAEBa1gDVVJMAAFtAzEAAQFrWANDT01QTEVURUQAAW0DMQABbFcDVFpJRAABAWxXA1ZBTFVFAAEBAWtYA0RVRQABbQMxAAFsVwNUWklEAAEBbFcDVkFMVUUAAQEBa1gDUFJJT1JJVFkAAW0DMQABAWtYA1NUQVRVUwABbQMxAAEBa1gDUEVSQ0VOVC1DT01QTEVURQABbQMxAAEBa1gDUkVMQVRFRC1UTwABbQMxAAFsVwNSRUxUWVBFAAFjA1BBUkVOVAABAQFrWANUUklHR0VSAAFtAzEAAWxXA1ZBTFVFAAEBbFcDUkVMQVRFRAABYwNTVEFSVAABYwNFTkQAAQEBa1gDQUNUSU9OAAFtAzEAAQFrWANSRVBFQVQAAW0DMQABAWtYA1gtRVZPTFVUSU9OLUFMQVJNLVVJRAABbQMxAAEBa1gDVFoAAW0DMQABAWtYA1RSQU5TUAABbQMxAAEBa1gDUkVDVVJSRU5DRS1JRAABbQMxAAFsVwNUWklEAAEBbFcDVkFMVUUAAQEBa1gDRVhEQVRFAAEBa1gDWC1TWU5DRVZPTFVUSU9OLUVYREFURS1ERVRBQ0hFRAABbFcDVFpJRAABAQFrWANEVEVORAABbQMxAAFsVwNUWklEAAEBbFcDVkFMVUUAAQEBa1gDRFVSQVRJT04AAW0DMQABAWtYA0FUVEVOREVFAAFsVwNDTgABAWxXA1BBUlRTVEFUAAFjA05FRURTLUFDVElPTgABYwNBQ0NFUFRFRAABYwNERUNMSU5FRAABYwNURU5UQVRJVkUAAWMDREVMRUdBVEVEAAEBbFcDUk9MRQABYwNDSEFJUgABYwNSRVEtUEFSVElDSVBBTlQAAWMDT1BULVBBUlRJQ0lQQU5UAAFjA05PTi1QQVJUSUNJUEFOVAABAWxXA1JTVlAAAWMDVFJVRQABYwNGQUxTRQABAWxXA0xBTkdVQUdFAAEBbFcDQ1VUWVBFAAFjA0lORElWSURVQUwAAWMDR1JPVVAAAWMDUkVTT1VSQ0UAAWMDUk9PTQABYwNVTktOT1dOAAEBAWtYA09SR0FOSVpFUgABbQMxAAFsVwNDTgABAQEBX2ADMQABYAMyAAFgAzMAAWADNAABYAM1AAFgAzYAAWADNwABYAM1NDQwMDEAAQEBAQEBAVNLAzIAAVoAAVMDYXBwbGljYXRpb24vdm5kLnN5bmNtbC1kZXZpbmYrd2J4bWwAAQEAAFRuVwMuL2RldmluZjEyAAEBAQFGSwMzAAFPAzIwMAABVG5XA2V2ZW50AAEBZ1cDLi9lZHNfZXZlbnQAAQFaAAFFSgMyMDEyMDMyOVQxMTAxMjZaAAFPAzIwMTIwMzI5VDExMDE1MloAAQFVAzQwMDAwMDAAAQEBAQAAEgEB')

    def setUp(self):
        self.setUpServer()
        self.setUpListeners(None)
        # default config
        self.config = { 
                         "" : { "remoteDeviceId" : "sc-api-nat",
                                "password" : "test",
                                "username" : "test",
                                "PeerIsClient" : "1",
                                "RetryInterval" : "1",
                                "RetryDuration" : "10"
                              },
                         "source/addressbook" : { "sync" : "slow",
                                                  "backend" : "file",
                                                  "database" : "file://" + xdg_root + "/addressbook",
                                                  "databaseFormat" : "text/vcard",
                                                  "uri" : "card"
                                                },
                         "source/calendar"    : { "sync" : "disabled",
                                                  "backend" : "file",
                                                  "database" : "file://" + xdg_root + "/calendar",
                                                  "databaseFormat" : "text/calendar",
                                                  "uri" : "cal"
                                                },
                         "source/todo"        : { "sync" : "disabled",
                                                  "backend" : "file",
                                                  "database" : "file://" + xdg_root + "/todo",
                                                  "databaseFormat" : "text/calendar",
                                                  "uri" : "task"
                                                },
                         "source/memo"        : { "sync" : "disabled",
                                                  "backend" : "file",
                                                  "database" : "file://" + xdg_root + "/memo",
                                                  "databaseFormat" : "text/calendar",
                                                  "uri" : "text"
                                                }
                       }

    def setupConfig(self, name="dummy-test", deviceId="sc-api-nat"):
        self.setUpSession(name)
        self.config[""]["remoteDeviceId"] = deviceId
        self.session.SetConfig(False, False, self.config, utf8_strings=True)
        self.session.Detach()
        # SyncEvolution <= 1.2.2 delayed the "Session.StatusChanged"
        # "done" signal. The correct behavior is to send that
        # important change right away.
        loop.run()
        self.assertEqual(DBusUtil.quit_events, ["session done"])
        DBusUtil.quit_events = []

    def run(self, result):
        self.runTest(result, own_xdg=True)

    def getConnection(self, must_authenticate=False):
        conpath = self.server.Connect({'description': 'test-dbus.py',
                                       'transport': 'dummy'},
                                      must_authenticate,
                                      "")
        self.setUpConnectionListeners(conpath)
        connection = dbus.Interface(bus.get_object(self.server.bus_name,
                                                   conpath),
                                    'org.syncevolution.Connection')
        return (conpath, connection)

    def testConnect(self):
        """TestConnection.testConnect - get connection and close it"""
        conpath, connection = self.getConnection()
        connection.Close(False, 'good bye')
        loop.run()
        self.assertEqual(DBusUtil.events, [('abort',)])

    def testInvalidConnect(self):
        """TestConnection.testInvalidConnect - get connection, send invalid initial message"""
        self.setupConfig()
        conpath, connection = self.getConnection()
        try:
            connection.Process('1234', 'invalid message type')
        except dbus.DBusException, ex:
            self.assertEqual(str(ex),
                                 "org.syncevolution.Exception: message type 'invalid message type' not supported for starting a sync")
        else:
            self.fail("no exception thrown")
        loop.run()
        # 'idle' status doesn't be checked
        self.assertIn(('abort',), DBusUtil.events)

    @timeout(60)
    def testStartSync(self):
        """TestConnection.testStartSync - send a valid initial SyncML message"""
        self.setupConfig()
        conpath, connection = self.getConnection()
        connection.Process(TestConnection.message1, 'application/vnd.syncml+xml')
        loop.run()
        self.assertEqual(DBusUtil.quit_events, ["connection " + conpath + " got reply"])
        DBusUtil.quit_events = []
        # TODO: check events
        self.assertNotEqual(DBusUtil.reply, None)
        self.assertEqual(DBusUtil.reply[1], 'application/vnd.syncml+xml')
        # credentials should have been accepted because must_authenticate=False
        # in Connect(); 508 = "refresh required" is normal
        self.assertIn('<Status><CmdID>2</CmdID><MsgRef>1</MsgRef><CmdRef>1</CmdRef><Cmd>Alert</Cmd><TargetRef>addressbook</TargetRef><SourceRef>./addressbook</SourceRef><Data>508</Data>', DBusUtil.reply[0])
        self.assertNotIn('<Chal>', DBusUtil.reply[0])
        self.assertEqual(DBusUtil.reply[3], False)
        self.assertNotEqual(DBusUtil.reply[4], '')
        connection.Close(False, 'good bye')
        loop.run()
        loop.run()
        self.assertEqual(DBusUtil.quit_events, ["connection " + conpath + " aborted",
                                                    "session done"])
        # start another session for the server (ensures that the previous one is done),
        # then check the server side report
        DBusUtil.quit_events = []
        self.setUpSession("dummy-test")
        sessions = self.session.GetReports(0, 100)
        self.assertEqual(len(sessions), 1)
        # transport failure, only addressbook active and later aborted
        self.assertEqual(sessions[0]["status"], "20043")
        # Exact error string doesn't matter much, it is mostly internal.
        if sessions[0]["error"] != "D-Bus peer has disconnected": # old error
            self.assertEqual(sessions[0]["error"], "transport problem: send() on connection which is not ready")
        self.assertEqual(sessions[0]["source-addressbook-status"], "20017")
        # The other three sources are disabled and should not be listed in the
        # report. Used to be listed with status 0 in the past, which would also
        # be acceptable, but here we use the strict check for "not present" to
        # ensure that the current behavior is preserved.
        self.assertNotIn("source-calendar-status", sessions[0])
        self.assertNotIn("source-todo-status", sessions[0])
        self.assertNotIn("source-memo-status", sessions[0])

    @timeout(60)
    def testCredentialsWrong(self):
        """TestConnection.testCredentialsWrong - send invalid credentials"""
        self.setupConfig()
        conpath, connection = self.getConnection(must_authenticate=True)
        connection.Process(TestConnection.message1, 'application/vnd.syncml+xml')
        loop.run()
        self.assertEqual(DBusUtil.quit_events, ["connection " + conpath + " got reply"])
        DBusUtil.quit_events = []
        # TODO: check events
        self.assertNotEqual(DBusUtil.reply, None)
        self.assertEqual(DBusUtil.reply[1], 'application/vnd.syncml+xml')
        # credentials should have been rejected because of wrong Nonce
        self.assertIn('<Chal>', DBusUtil.reply[0])
        self.assertEqual(DBusUtil.reply[3], False)
        self.assertNotEqual(DBusUtil.reply[4], '')
        # When the login fails, the server also ends the session and the connection;
        # no more Connection.close() possible (could fail, depending on the timing).
        loop.run()
        loop.run()
        DBusUtil.quit_events.sort()
        self.assertEqual(DBusUtil.quit_events, ["connection " + conpath + " aborted",
                                                    "session done"])

    @timeout(60)
    def testCredentialsWrongWBXML(self):
        """TestConnection.testCredentialsWrongWBXML - send invalid credentials using WBXML"""
        self.setupConfig()
        conpath, connection = self.getConnection(must_authenticate=True)
        connection.Process(TestConnection.message1WBXML, 'application/vnd.syncml+wbxml')
        loop.run()
        self.assertEqual(DBusUtil.quit_events, ["connection " + conpath + " got reply"])
        DBusUtil.quit_events = []
        # TODO: check events
        self.assertNotEqual(DBusUtil.reply, None)
        self.assertEqual(DBusUtil.reply[1], 'application/vnd.syncml+wbxml')
        # Credentials should have been rejected because of wrong Nonce.
        # Impossible to check with WBXML...
        # self.assertTrue('<Chal>' in DBusUtil.reply[0])
        self.assertEqual(DBusUtil.reply[3], False)
        self.assertNotEqual(DBusUtil.reply[4], '')
        loop.run()
        loop.run()
        DBusUtil.quit_events.sort()
        self.assertEqual(DBusUtil.quit_events, ["connection " + conpath + " aborted",
                                                "session done"])

    @timeout(60)
    def testCredentialsRight(self):
        """TestConnection.testCredentialsRight - send correct credentials"""
        self.setupConfig()
        conpath, connection = self.getConnection(must_authenticate=True)
        plain_auth = TestConnection.message1.replace("<Type xmlns='syncml:metinf'>syncml:auth-md5</Type></Meta><Data>kHzMn3RWFGWSKeBpXicppQ==</Data>",
                                                     "<Type xmlns='syncml:metinf'>syncml:auth-basic</Type></Meta><Data>dGVzdDp0ZXN0</Data>")
        connection.Process(plain_auth, 'application/vnd.syncml+xml')
        loop.run()
        self.assertEqual(DBusUtil.quit_events, ["connection " + conpath + " got reply"])
        DBusUtil.quit_events = []
        self.assertNotEqual(DBusUtil.reply, None)
        self.assertEqual(DBusUtil.reply[1], 'application/vnd.syncml+xml')
        # credentials should have been accepted because with basic auth,
        # credentials can be replayed; 508 = "refresh required" is normal
        self.assertIn('<Status><CmdID>2</CmdID><MsgRef>1</MsgRef><CmdRef>1</CmdRef><Cmd>Alert</Cmd><TargetRef>addressbook</TargetRef><SourceRef>./addressbook</SourceRef><Data>508</Data>', DBusUtil.reply[0])
        self.assertEqual(DBusUtil.reply[3], False)
        self.assertNotEqual(DBusUtil.reply[4], '')
        connection.Close(False, 'good bye')
        loop.run()
        loop.run()
        self.assertEqual(DBusUtil.quit_events, ["connection " + conpath + " aborted",
                                                    "session done"])

    @timeout(60)
    def testStartSyncTwice(self):
        """TestConnection.testStartSyncTwice - send the same SyncML message twice, starting two sessions"""
        self.setupConfig()
        conpath, connection = self.getConnection()
        connection.Process(TestConnection.message1, 'application/vnd.syncml+xml')
        loop.run()
        # TODO: check events
        self.assertEqual(DBusUtil.quit_events, ["connection " + conpath + " got reply"])
        self.assertNotEqual(DBusUtil.reply, None)
        self.assertEqual(DBusUtil.reply[1], 'application/vnd.syncml+xml')
        self.assertEqual(DBusUtil.reply[3], False)
        self.assertNotEqual(DBusUtil.reply[4], '')
        DBusUtil.reply = None
        DBusUtil.quit_events = []

        # Now start another session with the same client *without*
        # closing the first one. The server should detect this
        # and forcefully close the first one.
        conpath2, connection2 = self.getConnection()
        connection2.Process(TestConnection.message1, 'application/vnd.syncml+xml')

        # reasons for leaving the loop, in random order:
        # - abort of first connection
        # - first session done
        # - reply for second one
        loop.run()
        loop.run()
        loop.run()
        DBusUtil.quit_events.sort()
        expected = [ "connection " + conpath + " aborted",
                     "session done",
                     "connection " + conpath2 + " got reply" ]
        expected.sort()
        self.assertEqual(DBusUtil.quit_events, expected)
        self.assertNotEqual(DBusUtil.reply, None)
        self.assertEqual(DBusUtil.reply[1], 'application/vnd.syncml+xml')
        self.assertEqual(DBusUtil.reply[3], False)
        self.assertNotEqual(DBusUtil.reply[4], '')
        DBusUtil.quit_events = []

        # now quit for good
        connection2.Close(False, 'good bye')
        loop.run()
        loop.run()
        self.assertEqual(DBusUtil.quit_events, ["connection " + conpath2 + " aborted",
                                                    "session done"])

    @timeout(60)
    def testKillInactive(self):
        """TestConnection.testKillInactive - block server with client A, then let client B connect twice"""
        #set up 2 configs
        self.setupConfig()
        self.setupConfig("dummy", "sc-pim-ppc")
        conpath, connection = self.getConnection()
        connection.Process(TestConnection.message1, 'application/vnd.syncml+xml')
        loop.run()
        # TODO: check events
        self.assertEqual(DBusUtil.quit_events, ["connection " + conpath + " got reply"])
        self.assertNotEqual(DBusUtil.reply, None)
        self.assertEqual(DBusUtil.reply[1], 'application/vnd.syncml+xml')
        self.assertEqual(DBusUtil.reply[3], False)
        self.assertNotEqual(DBusUtil.reply[4], '')
        DBusUtil.reply = None
        DBusUtil.quit_events = []

        # Now start two more sessions with the second client *without*
        # closing the first one. The server should remove only the
        # first connection of client B.
        message1_clientB = TestConnection.message1.replace("sc-api-nat", "sc-pim-ppc")
        conpath2, connection2 = self.getConnection()
        connection2.Process(message1_clientB, 'application/vnd.syncml+xml')
        conpath3, connection3 = self.getConnection()
        connection3.Process(message1_clientB, 'application/vnd.syncml+xml')
        # Queueing session for connection2 done now.
        loop.run()
        loop.run()
        self.assertEqual(DBusUtil.quit_events, ["connection " + conpath2 + " aborted",
                                                "session done"])
        DBusUtil.quit_events = []

        # now quit for good
        connection3.Close(False, 'good bye client B')
        loop.run()
        loop.run()
        self.assertEqual(DBusUtil.quit_events, ["connection " + conpath3 + " aborted",
                                                "session done"])
        DBusUtil.quit_events = []
        connection.Close(False, 'good bye client A')
        loop.run()
        loop.run()
        self.assertEqual(DBusUtil.quit_events, ["connection " + conpath + " aborted",
                                                    "session done"])

    @timeout(60)
    def testTimeoutSync(self):
        """TestConnection.testTimeoutSync - start a sync, then wait for server to detect that we stopped replying"""

        # The server-side configuration for sc-api-nat must contain a retryDuration=10
        # because this test itself will time out with a failure after 60 seconds.
        self.setupConfig()
        conpath, connection = self.getConnection()
        connection.Process(TestConnection.message1, 'application/vnd.syncml+xml')
        loop.run()
        self.assertEqual(DBusUtil.quit_events, ["connection " + conpath + " got reply"])
        DBusUtil.quit_events = []
        # TODO: check events
        self.assertNotEqual(DBusUtil.reply, None)
        self.assertEqual(DBusUtil.reply[1], 'application/vnd.syncml+xml')
        # wait for connection reset and "session done" due to timeout
        loop.run()
        loop.run()
        self.assertEqual(DBusUtil.quit_events, ["connection " + conpath + " aborted",
                                                    "session done"])

class TestMultipleConfigs(unittest.TestCase, DBusUtil):
    """ sharing of properties between configs

    Creates and tests the configs 'foo', 'bar', 'foo@other_context',
    '@default' and checks that 'defaultPeer' (global), 'syncURL' (per
    peer), 'database' (per source), 'uri' (per source and peer)
    are shared correctly.

    Runs with a the server ready, without session."""

    def setUp(self):
        self.setUpServer()

    def run(self, result):
        self.runTest(result)

    def setupEmpty(self):
        """Creates empty configs 'foo', 'bar', 'foo@other_context'.
        Updating non-existant configs is an error. Use this
        function before trying to update one of these configs."""
        self.setUpSession("foo")
        self.session.SetConfig(False, False, {"" : {}})
        self.session.Detach()
        self.setUpSession("bar")
        self.session.SetConfig(False, False, {"": {}})
        self.session.Detach()
        self.setUpSession("foo@other_CONTEXT")
        self.session.SetConfig(False, False, {"": {}})
        self.session.Detach()

    def setupConfigs(self):
        """Creates polulated configs 'foo', 'bar', 'foo@other_context'."""
        self.setupEmpty()

        # update normal view on "foo"
        self.setUpSession("foo")
        self.session.SetConfig(True, False,
                               { "" : { "defaultPeer" : "foobar_peer",
                                        "deviceId" : "shared-device-identifier",
                                        "syncURL": "http://scheduleworld" },
                                 "source/calendar" : { "uri" : "cal3" },
                                 "source/addressbook" : { "database": "Personal",
                                                          "sync" : "two-way",
                                                          "uri": "card3" } },
                               utf8_strings=True)
        self.session.Detach()

        # "bar" shares properties with "foo"
        self.setUpSession("bar")
        config = self.session.GetConfig(False, utf8_strings=True)
        self.assertEqual(config[""]["defaultPeer"], "foobar_peer")
        self.assertEqual(config[""]["deviceId"], "shared-device-identifier")
        self.assertEqual(config["source/addressbook"]["database"], "Personal")
        self.session.SetConfig(True, False,
                               { "" : { "syncURL": "http://funambol" },
                                 "source/calendar" : { "uri" : "cal" },
                                 "source/addressbook" : { "database": "Work",
                                                          "sync" : "refresh-from-client",
                                                          "uri": "card" } },
                               utf8_strings=True)
        self.session.Detach()

    def testSharing(self):
        """TestMultipleConfigs.testSharing - set up configs and tests reading them"""
        self.setupConfigs()

        # check how view "foo" has been modified
        self.setUpSession("Foo@deFAULT")
        config = self.session.GetConfig(False, utf8_strings=True)
        self.assertEqual(config[""]["defaultPeer"], "foobar_peer")
        self.assertEqual(config[""]["syncURL"], "http://scheduleworld")
        self.assertEqual(config["source/addressbook"]["database"], "Work")
        self.assertEqual(config["source/addressbook"]["uri"], "card3")
        self.session.Detach()

        # different ways of addressing this context
        self.setUpSession("")
        config = self.session.GetConfig(False, utf8_strings=True)
        self.assertEqual(config[""]["defaultPeer"], "foobar_peer")
        self.assertIn("source/addressbook", config)
        self.assertNotIn("uri", config["source/addressbook"])
        self.session.Detach()

        self.setUpSession("@DEFAULT")
        config = self.session.GetConfig(False, utf8_strings=True)
        self.assertEqual(config[""]["defaultPeer"], "foobar_peer")
        self.assertEqual(config[""]["deviceId"], "shared-device-identifier")
        self.assertIn("source/addressbook", config)
        self.assertNotIn("uri", config["source/addressbook"])
        self.session.Detach()

        # different context
        self.setUpSession("@other_context")
        config = self.session.GetConfig(False, utf8_strings=True)
        self.assertEqual(config[""]["defaultPeer"], "foobar_peer")
        self.assertNotIn("source/addressbook", config)
        self.session.Detach()

    def testSharedTemplate(self):
        """TestMultipleConfigs.testSharedTemplate - templates must contain shared properties"""
        self.setupConfigs()

        config = self.server.GetConfig("scheduleworld", True, utf8_strings=True)
        self.assertEqual(config[""]["defaultPeer"], "foobar_peer")
        self.assertEqual(config[""]["deviceId"], "shared-device-identifier")
        self.assertEqual(config["source/addressbook"]["database"], "Work")

    def testSharedProperties(self):
        """TestMultipleConfigs.testSharedProperties - 'type' consists of per-peer and shared properties"""
        self.setupConfigs()

        # writing for peer modifies properties in "foo" and context
        self.setUpSession("Foo@deFAULT")
        config = self.session.GetConfig(False, utf8_strings=True)
        config["source/addressbook"]["syncFormat"] = "text/vcard"
        config["source/addressbook"]["backend"] = "file"
        config["source/addressbook"]["databaseFormat"] = "text/x-vcard"
        self.session.SetConfig(True, False,
                               config,
                               utf8_strings=True)
        config = self.server.GetConfig("Foo", False, utf8_strings=True)
        self.assertEqual(config["source/addressbook"]["syncFormat"], "text/vcard")
        config = self.server.GetConfig("@default", False, utf8_strings=True)
        self.assertEqual(config["source/addressbook"]["backend"], "file")
        self.assertEqual(config["source/addressbook"]["databaseFormat"], "text/x-vcard")
        self.session.Detach()

    def testSharedPropertyOther(self):
        """TestMultipleConfigs.testSharedPropertyOther - shared backend properties must be preserved when adding peers"""
        # writing peer modifies properties in "foo" and creates context "@other"
        self.setUpSession("Foo@other")
        config = self.server.GetConfig("ScheduleWorld@other", True, utf8_strings=True)
        config["source/addressbook"]["backend"] = "file"
        config["source/addressbook"]["databaseFormat"] = "text/x-vcard"
        self.session.SetConfig(False, False,
                               config,
                               utf8_strings=True)
        config = self.server.GetConfig("Foo", False, utf8_strings=True)
        self.assertEqual(config["source/addressbook"]["backend"], "file")
        config = self.server.GetConfig("@other", False, utf8_strings=True)
        self.assertEqual(config["source/addressbook"]["databaseFormat"], "text/x-vcard")
        self.session.Detach()

        # adding second client must preserve backend value
        self.setUpSession("bar@other")
        config = self.server.GetConfig("Funambol@other", True, utf8_strings=True)
        self.assertEqual(config["source/addressbook"]["backend"], "file")
        self.session.SetConfig(False, False,
                               config,
                               utf8_strings=True)
        config = self.server.GetConfig("bar", False, utf8_strings=True)
        self.assertEqual(config["source/addressbook"]["backend"], "file")
        self.assertEqual(config["source/addressbook"].get("syncFormat"), None)
        config = self.server.GetConfig("@other", False, utf8_strings=True)
        self.assertEqual(config["source/addressbook"]["databaseFormat"], "text/x-vcard")

    def testOtherContext(self):
        """TestMultipleConfigs.testOtherContext - write into independent context"""
        self.setupConfigs()

        # write independent "foo@other_context" config
        self.setUpSession("foo@other_context")
        config = self.session.GetConfig(False, utf8_strings=True)
        config[""]["syncURL"] = "http://scheduleworld2"
        config["source/addressbook"] = { "database": "Play",
                                         "uri": "card30" }
        self.session.SetConfig(True, False,
                               config,
                               utf8_strings=True)
        config = self.session.GetConfig(False, utf8_strings=True)
        self.assertEqual(config[""]["defaultPeer"], "foobar_peer")
        self.assertEqual(config[""]["syncURL"], "http://scheduleworld2")
        self.assertEqual(config["source/addressbook"]["database"], "Play")
        self.assertEqual(config["source/addressbook"]["uri"], "card30")
        self.session.Detach()

        # "foo" modified?
        self.setUpSession("foo")
        config = self.session.GetConfig(False, utf8_strings=True)
        self.assertEqual(config[""]["defaultPeer"], "foobar_peer")
        self.assertEqual(config[""]["syncURL"], "http://scheduleworld")
        self.assertEqual(config["source/addressbook"]["database"], "Work")
        self.assertEqual(config["source/addressbook"]["uri"], "card3")
        self.session.Detach()

    def testSourceRemovalLocal(self):
        """TestMultipleConfigs.testSourceRemovalLocal - remove 'addressbook' source in 'foo'"""
        self.setupConfigs()
        self.setUpSession("foo")
        config = self.session.GetConfig(False, utf8_strings=True)
        del config["source/addressbook"]
        self.session.SetConfig(False, False, config, utf8_strings=True)
        self.session.Detach()

        # "addressbook" still exists in "foo" but only with default values
        config = self.server.GetConfig("foo", False, utf8_strings=True)
        self.assertNotIn("uri", config["source/addressbook"])
        self.assertNotIn("sync", config["source/addressbook"])

        # "addressbook" unchanged in "bar"
        config = self.server.GetConfig("bar", False, utf8_strings=True)
        self.assertEqual(config["source/addressbook"]["uri"], "card")
        self.assertEqual(config["source/addressbook"]["sync"], "refresh-from-client")

    def testSourceRemovalGlobal(self):
        """TestMultipleConfigs.testSourceRemovalGlobal - remove "addressbook" everywhere"""
        self.setupConfigs()
        self.setUpSession("")
        config = self.session.GetConfig(False, utf8_strings=True)
        del config["source/addressbook"]
        self.session.SetConfig(False, False, config, utf8_strings=True)
        self.session.Detach()

        # "addressbook" gone in "foo" and "bar"
        config = self.server.GetConfig("foo", False, utf8_strings=True)
        self.assertNotIn("source/addressbook", config)
        config = self.server.GetConfig("bar", False, utf8_strings=True)
        self.assertNotIn("source/addressbook", config)

    def testRemovePeer(self):
        """TestMultipleConfigs.testRemovePeer - check listing of peers while removing 'bar'"""
        self.setupConfigs()
        self.testOtherContext()
        self.setUpSession("bar")
        peers = self.session.GetConfigs(False, utf8_strings=True)
        self.assertEqual(peers,
                             [ "bar", "foo", "foo@other_context" ])
        peers2 = self.server.GetConfigs(False, utf8_strings=True)
        self.assertEqual(peers, peers2)
        # remove "bar"
        self.session.SetConfig(False, False, {}, utf8_strings=True)
        peers = self.server.GetConfigs(False, utf8_strings=True)
        self.assertEqual(peers,
                             [ "foo", "foo@other_context" ])
        self.session.Detach()

        # other configs should not have been affected
        config = self.server.GetConfig("foo", False, utf8_strings=True)
        self.assertEqual(config[""]["defaultPeer"], "foobar_peer")
        self.assertEqual(config[""]["syncURL"], "http://scheduleworld")
        self.assertEqual(config["source/calendar"]["uri"], "cal3")
        config = self.server.GetConfig("foo@other_context", False, utf8_strings=True)
        self.assertEqual(config[""]["defaultPeer"], "foobar_peer")
        self.assertEqual(config[""]["syncURL"], "http://scheduleworld2")
        self.assertEqual(config["source/addressbook"]["database"], "Play")
        self.assertEqual(config["source/addressbook"]["uri"], "card30")

    def testRemoveContext(self):
        """TestMultipleConfigs.testRemoveContext - remove complete config"""
        self.setupConfigs()
        self.setUpSession("")
        self.session.SetConfig(False, False, {}, utf8_strings=True)
        config = self.session.GetConfig(False, utf8_strings=True)
        self.assertEqual(config[""]["defaultPeer"], "foobar_peer")
        peers = self.server.GetConfigs(False, utf8_strings=True)
        self.assertEqual(peers, ['foo@other_context'])
        self.session.Detach()

    def testTemplates(self):
        """TestMultipleConfigs.testTemplates - templates reuse common properties"""
        self.setupConfigs()

        # deviceID must be shared and thus be reused in templates
        self.setUpSession("")
        config = self.session.GetConfig(False, utf8_strings=True)
        config[""]["DEVICEID"] = "shared-device-identifier"
        self.session.SetConfig(True, False, config, utf8_strings=True)
        config = self.server.GetConfig("", False, utf8_strings=True)
        self.assertEqual(config[""]["deviceId"], "shared-device-identifier")

        # get template for default context
        config = self.server.GetConfig("scheduleworld", True, utf8_strings=True)
        self.assertEqual(config[""]["defaultPeer"], "foobar_peer")
        self.assertEqual(config[""]["deviceId"], "shared-device-identifier")

        # now for @other_context - different device ID!
        config = self.server.GetConfig("scheduleworld@other_context", True, utf8_strings=True)
        self.assertEqual(config[""]["defaultPeer"], "foobar_peer")
        self.assertNotEqual(config[""]["deviceId"], "shared-device-identifier")

class TestLocalSync(unittest.TestCase, DBusUtil):
    """Tests involving local sync."""

    def run(self, result):
        self.runTest(result)

    def setUp(self):
        self.setUpServer()

    def setUpConfigs(self, childPassword=None):
        self.setUpLocalSyncConfigs(childPassword)

    @timeout(100)
    def testSync(self):
        """TestLocalSync.testSync - run a simple slow sync between local dirs"""
        self.setUpConfigs()
        os.makedirs(xdg_root + "/server")
        output = open(xdg_root + "/server/0", "w")
        output.write('''BEGIN:VCARD
VERSION:3.0
FN:John Doe
N:Doe;John
END:VCARD''')
        output.close()
        self.setUpListeners(self.sessionpath)
        self.session.Sync("slow", {})
        loop.run()
        self.assertEqual(DBusUtil.quit_events, ["session " + self.sessionpath + " done"])
        self.checkSync()
        input = open(xdg_root + "/server/0", "r")
        self.assertIn("FN:John Doe", input.read())

    def setUpInfoRequest(self, response={"password" : "123456"}):
        self.lastState = "unknown"
        def infoRequest(id, session, state, handler, type, params):
            if state == "request":
                self.assertEqual(self.lastState, "unknown")
                self.lastState = "request"
                if response != None:
                    self.server.InfoResponse(id, "working", {}, utf8_strings=True)
            elif state == "waiting":
                self.assertEqual(self.lastState, "request")
                self.lastState = "waiting"
                if response != None:
                    self.server.InfoResponse(id, "response", response, utf8_strings=True)
            elif state == "done":
                self.assertEqual(self.lastState, "waiting")
                self.lastState = "done"
            else:
                self.fail("state should not be '" + state + "'")

        signal = bus.add_signal_receiver(infoRequest,
                                         'InfoRequest',
                                         'org.syncevolution.Server',
                                         self.server.bus_name,
                                         None,
                                         byte_arrays=True,
                                         utf8_strings=True)
        return signal

    @timeout(100)
    def testPasswordRequest(self):
        """TestLocalSync.testPasswordRequest - check that password request child->parent->us works"""
        self.setUpConfigs(childPassword="-")
        self.setUpListeners(self.sessionpath)
        signal = self.setUpInfoRequest()
        try:
            self.session.Sync("slow", {})
            loop.run()
        finally:
            signal.remove()

        self.assertEqual(DBusUtil.quit_events, ["session " + self.sessionpath + " done"])
        self.assertEqual(self.lastState, "done")
        self.checkSync()
        self.assertSyncStatus('server', 200, None)

    @timeout(100)
    def testPasswordRequestAbort(self):
        """TestLocalSync.testPasswordRequestAbort - let user cancel password request"""
        self.setUpConfigs(childPassword="-")
        self.setUpListeners(self.sessionpath)
        signal = self.setUpInfoRequest(response={})
        try:
            self.session.Sync("slow", {})
            loop.run()
        finally:
            signal.remove()

        self.assertEqual(DBusUtil.quit_events, ["session " + self.sessionpath + " done"])
        self.assertEqual(self.lastState, "done")
        self.checkSync(expectedError=20017, expectedResult=20017)
        self.assertSyncStatus('server', 20017, "error code from SyncEvolution aborted on behalf of user (local, status 20017): User did not provide the 'addressbook backend' password.")

    @timeout(200)
    def testPasswordRequestTimeout(self):
        """TestLocalSync.testPasswordRequestTimeout - let password request time out"""
        self.setUpConfigs(childPassword="-")
        self.setUpListeners(self.sessionpath)
        signal = self.setUpInfoRequest(response=None)
        try:
            # Will time out roughly after 120 seconds.
            start = time.time()
            self.session.Sync("slow", {})
            loop.run()
        finally:
            signal.remove()

        self.assertEqual(DBusUtil.quit_events, ["session " + self.sessionpath + " done"])
        self.assertEqual(self.lastState, "request")
        self.checkSync(expectedError=22003, expectedResult=22003)
        self.assertSyncStatus('server', 22003, "error code from SyncEvolution password request timed out (local, status 22003): Could not get the 'addressbook backend' password from user.")
        end = time.time()
        self.assertTrue(abs(120 + (usingValgrind() and 20 or 0) -
                            (end - start)) <
                        (usingValgrind() and 60 or 20))


    # Killing the syncevo-dbus-helper before it even starts (SYNCEVOLUTION_LOCAL_CHILD_DELAY=5)
    # is possible, but leads to ugly valgrind warnings about "possibly lost" memory because
    # SIGTERM really kills the process right away. Better wait until syncing really has started
    # in the helper (SYNCEVOLUTION_SYNC_DELAY). The test is more realistic that way, too.
    # The delay itself can be aborted, which is why this test will complete fairly quickly
    # despite that delay.
    @property("ENV", "SYNCEVOLUTION_SYNC_DELAY=1000")
    @timeout(100)
    def testConcurrency(self):
        """TestLocalSync.testConcurrency - D-Bus server must remain responsive while sync runs"""
        self.setUpConfigs()
        self.setUpListeners(self.sessionpath)
        self.session.Sync("slow", {})

        self.aborted = False
        def output(path, level, text, procname):
            if self.running and not self.aborted and text == 'ready to sync':
                logging.printf('aborting sync')
                self.session.Abort()
                self.aborted = True

        receiver = bus.add_signal_receiver(output,
                                           'LogOutput',
                                           'org.syncevolution.Server',
                                           self.server.bus_name,
                                           byte_arrays=True,
                                           utf8_strings=True)
        try:
            loop.run()
        finally:
            receiver.remove()
        self.assertTrue(self.aborted)
        self.assertEqual(DBusUtil.quit_events, ["session " + self.sessionpath + " done"])
        report = self.checkSync(20017, 20017, reportOptional=True) # aborted, with or without report
        if report:
            self.assertNotIn("error", report) # ... but without error message
            self.assertEqual(report["source-addressbook-status"], "0") # unknown status for source (aborted early)

    @timeout(200)
    @property("ENV", "LC_ALL=en_US.UTF-8 LANGUAGE=en_US")
    def testParentFailure(self):
        """TestLocalSync.testParentFailure - check that server and local sync helper detect when D-Bus helper dies"""
        self.setUpConfigs(childPassword="-")
        self.setUpListeners(self.sessionpath)
        self.lastState = "unknown"
        def infoRequest(id, session, state, handler, type, params):
            if state == "request":
                self.assertEqual(self.lastState, "unknown")
                self.lastState = "request"
                # kill syncevo-dbus-helper
                for pid, (name, cmdline) in self.getChildren().iteritems():
                    if 'syncevo-dbus-helper' in cmdline:
                        logging.printf('killing syncevo-dbus-helper with pid %d', pid)
                        os.kill(pid, signal.SIGKILL)
                        break
        dbusSignal = bus.add_signal_receiver(infoRequest,
                                             'InfoRequest',
                                             'org.syncevolution.Server',
                                             self.server.bus_name,
                                             None,
                                             byte_arrays=True,
                                             utf8_strings=True)

        try:
            self.session.Sync("slow", {})
            loop.run()
            self.assertEqual(DBusUtil.quit_events, ["session " + self.sessionpath + " done"])
            self.checkSync(expectedError=10500, expectedResult=22002)
        finally:
            dbusSignal.remove()

        # kill syncevo-dbus-server (and not the wrapper, because that would
        # also kill the process group)
        pid = self.serverPid()
        logging.printf('killing syncevo-dbus-server with pid %d', pid)
        os.kill(pid, signal.SIGKILL)

        # Give syncevo-local-sync some time to shut down.
        time.sleep(usingValgrind() and 60 or 10)

        # Remove syncevo-dbus-server zombie process(es).
        DBusUtil.pserver.wait()
        DBusUtil.pserver = None
        try:
            while True:
                res = os.waitpid(-1, os.WNOHANG)
                if res[0]:
                    logging.printf('got status %d for pid %d', res[1], res[0])
                else:
                    break
        except OSError, ex:
            if ex.errno != errno.ECHILD:
                raise ex

        # Now no processes should be left in the process group
        # of the syncevo-dbus-server.
        self.assertEqual({}, self.getChildren())

        # check status reports
        self.assertSyncStatus('server', 22002, 'synchronization process died prematurely')
        # Local sync helper cannot tell for sure what happened.
        # A generic error is okay. If the ForkExecChild::m_onQuit signal is
        # triggered first, it'll report the "aborted" error.
        status, error = self.getSyncStatus('target_+config@client')
        self.assertTrue(status in (10500, 20017))
        self.assertTrue(error in ('retrieving password failed: The connection is closed',
                                  'sync parent quit unexpectedly'))

    @timeout(200)
    @property("ENV", "LC_ALL=en_US.UTF-8 LANGUAGE=en_US")
    def testChildFailure(self):
        """TestLocalSync.testChildFailure - check that server and D-Bus sync helper detect when local sync helper dies"""
        self.setUpConfigs(childPassword="-")
        self.setUpListeners(self.sessionpath)
        self.lastState = "unknown"
        def infoRequest(id, session, state, handler, type, params):
            if state == "request":
                self.assertEqual(self.lastState, "unknown")
                self.lastState = "request"
                # kill syncevo-dbus-helper
                for pid, (name, cmdline) in self.getChildren().iteritems():
                    if 'syncevo-local-sync' in cmdline:
                        logging.printf('killing syncevo-local-sync with pid %d', pid)
                        os.kill(pid, signal.SIGKILL)
                        break
        dbusSignal = bus.add_signal_receiver(infoRequest,
                                             'InfoRequest',
                                             'org.syncevolution.Server',
                                             self.server.bus_name,
                                             None,
                                             byte_arrays=True,
                                             utf8_strings=True)

        try:
            self.session.Sync("slow", {})
            loop.run()
            self.assertEqual(DBusUtil.quit_events, ["session " + self.sessionpath + " done"])
            # TODO: this shouldn't be 20043 'external transport failure'
            self.checkSync(expectedError=20043, expectedResult=20043)
        finally:
            dbusSignal.remove()

        # kill syncevo-dbus-server (and not the wrapper, because that would
        # also kill the process group)
        pid = self.serverPid()
        logging.printf('killing syncevo-dbus-server with pid %d', pid)
        os.kill(pid, signal.SIGKILL)

        # Give syncevo-local-sync some time to shut down.
        time.sleep(usingValgrind() and 60 or 10)

        # Remove syncevo-dbus-server zombie process(es).
        DBusUtil.pserver.wait()
        DBusUtil.pserver = None
        try:
            while True:
                res = os.waitpid(-1, os.WNOHANG)
                if res[0]:
                    logging.printf('got status %d for pid %d', res[1], res[0])
                else:
                    break
        except OSError, ex:
            if ex.errno != errno.ECHILD:
                raise ex

        # Now no processes should be left in the process group
        # of the syncevo-dbus-server.
        self.assertEqual({}, self.getChildren())

        # check status reports
        status, error = self.getSyncStatus('server')
        self.assertSyncStatus('server', 20043, ('child process quit because of signal 9',
                                                'sending message to child failed: The connection is closed'))
        self.assertSyncStatus('target_+config@client', 22002, 'synchronization process died prematurely')

    @timeout(200)
    def testServerFailure(self):
        """TestLocalSync.testServerFailure - check that D-Bus helper detects when server dies"""
        self.setUpConfigs(childPassword="-")
        self.setUpListeners(self.sessionpath)
        self.lastState = "unknown"
        pid = self.serverPid()
        serverPid = DBusUtil.pserver.pid
        def infoRequest(id, session, state, handler, type, params):
            if state == "request":
                self.assertEqual(self.lastState, "unknown")
                self.lastState = "request"
                # kill syncevo-dbus-server
                if pid != serverPid:
                    logging.printf('killing syncevo-dbus-server wrapper with pid %d', serverPid)
                    os.kill(serverPid, signal.SIGKILL)
                logging.printf('killing syncevo-dbus-server with pid %d', pid)
                os.kill(pid, signal.SIGKILL)
                loop.quit()
        dbusSignal = bus.add_signal_receiver(infoRequest,
                                             'InfoRequest',
                                             'org.syncevolution.Server',
                                             self.server.bus_name,
                                             None,
                                             byte_arrays=True,
                                             utf8_strings=True)

        try:
            self.session.Sync("slow", {})
            loop.run()
        finally:
            dbusSignal.remove()

        # Give syncevo-dbus-helper and syncevo-local-sync some time to shut down.
        time.sleep(usingValgrind() and 60 or 10)

        # Remove syncevo-dbus-server zombie process(es).
        DBusUtil.pserver.wait()
        DBusUtil.pserver = None
        try:
            while True:
                res = os.waitpid(-1, os.WNOHANG)
                if res[0]:
                    logging.printf('got status %d for pid %d', res[1], res[0])
                else:
                    break
        except OSError, ex:
            if ex.errno != errno.ECHILD:
                raise ex

        # Now no processes should be left in the process group
        # of the syncevo-dbus-server.
        self.assertEqual({}, self.getChildren())

        # Sync should have failed with an explanation that it was
        # because of the password.
        self.assertSyncStatus('server', 22003, "error code from SyncEvolution password request timed out (local, status 22003): Could not get the 'addressbook backend' password from user.")

    @timeout(200)
    @property("ENV", "SYNCEVOLUTION_LOCAL_CHILD_DELAY=10") # allow killing syncevo-dbus-server in middle of sync
    def testNoParent(self):
        """TestLocalSync.testNoParent - check that sync helper can continue without parent"""
        self.setUpConfigs()
        self.setUpListeners(self.sessionpath)
        pid = self.serverPid()
        serverPid = DBusUtil.pserver.pid
        def killServer(*args, **keywords):
            if pid != serverPid:
                logging.printf('killing syncevo-dbus-server wrapper with pid %d', serverPid)
                os.kill(serverPid, signal.SIGKILL)
            logging.printf('killing syncevo-dbus-server with pid %d', pid)
            os.kill(pid, signal.SIGKILL)
            DBusUtil.pserver.wait()
            DBusUtil.pserver = None
            loop.quit()
        self.progressChanged = killServer

        self.session.Sync("slow", {})
        loop.run()

        # Should have killed server.
        self.assertFalse(DBusUtil.pserver)

        # Give syncevo-dbus-helper and syncevo-local-sync some time to shut down.
        time.sleep(usingValgrind() and 60 or 20)
        logging.log('sync should be done now')

        # Remove syncevo-dbus-server zombie process(es).
        try:
            while True:
                res = os.waitpid(-1, os.WNOHANG)
                if res[0]:
                    logging.printf('got status %d for pid %d', res[1], res[0])
                else:
                    break
        except OSError, ex:
            if ex.errno != errno.ECHILD:
                raise ex

        # Now no processes should be left in the process group
        # of the syncevo-dbus-server.
        self.assertEqual({}, self.getChildren())

        # Sync should have succeeded.
        self.assertSyncStatus('server', 200, None)

class TestLocalCache(unittest.TestCase, DBusUtil):
    """Tests involving local sync and the local-cache mode."""

    serverDB = os.path.join(xdg_root, "server")
    clientDB = os.path.join(xdg_root, "client")
    johnVCard = '''BEGIN:VCARD
VERSION:3.0
FN:John Doe
N:Doe;John
ORG:Test Inc.
END:VCARD'''
    johnComplexVCard = '''BEGIN:VCARD
VERSION:3.0
URL:http://john.doe.com
TITLE:Senior Tester
ORG:Test Inc.;Testing;test#1
ROLE:professional test case
X-EVOLUTION-MANAGER:John Doe Senior
X-EVOLUTION-ASSISTANT:John Doe Junior
NICKNAME:user1
BDAY:2006-01-08
X-FOOBAR-EXTENSION;X-FOOBAR-PARAMETER=foobar:has to be stored internally by engine and preserved in testExtensions test\; never sent to a peer
X-TEST;PARAMETER1=nonquoted;PARAMETER2="quoted because of spaces":Content with\nMultiple\nText lines\nand national chars: äöü
X-EVOLUTION-ANNIVERSARY:2006-01-09
X-EVOLUTION-SPOUSE:Joan Doe
NOTE:This is a test case which uses almost all Evolution fields.
FN:John Doe
N:Doe;John;;;
X-EVOLUTION-FILE-AS:Doe\, John
CATEGORIES:TEST
X-EVOLUTION-BLOG-URL:web log
CALURI:calender
FBURL:free/busy
X-EVOLUTION-VIDEO-URL:chat
X-MOZILLA-HTML:TRUE
ADR;TYPE=WORK:Test Box #2;;Test Drive 2;Test Town;Upper Test County;12346;O
 ld Testovia
LABEL;TYPE=WORK:Test Drive 2\nTest Town\, Upper Test County\n12346\nTest Bo
 x #2\nOld Testovia
ADR;TYPE=HOME:Test Box #1;;Test Drive 1;Test Village;Lower Test County;1234
 5;Testovia
LABEL;TYPE=HOME:Test Drive 1\nTest Village\, Lower Test County\n12345\nTest
  Box #1\nTestovia
ADR:Test Box #3;;Test Drive 3;Test Megacity;Test County;12347;New Testonia
LABEL;TYPE=OTHER:Test Drive 3\nTest Megacity\, Test County\n12347\nTest Box
  #3\nNew Testonia
UID:pas-id-43C0ED3900000001
EMAIL;TYPE=WORK;X-EVOLUTION-UI-SLOT=1:john.doe@work.com
EMAIL;TYPE=HOME;X-EVOLUTION-UI-SLOT=2:john.doe@home.priv
EMAIL;TYPE=OTHER;X-EVOLUTION-UI-SLOT=3:john.doe@other.world
EMAIL;TYPE=OTHER;X-EVOLUTION-UI-SLOT=4:john.doe@yet.another.world
TEL;TYPE=work;TYPE=Voice;X-EVOLUTION-UI-SLOT=1:business 1
TEL;TYPE=homE;TYPE=VOICE;X-EVOLUTION-UI-SLOT=2:home 2
TEL;TYPE=CELL;X-EVOLUTION-UI-SLOT=3:mobile 3
TEL;TYPE=WORK;TYPE=FAX;X-EVOLUTION-UI-SLOT=4:businessfax 4
TEL;TYPE=HOME;TYPE=FAX;X-EVOLUTION-UI-SLOT=5:homefax 5
TEL;TYPE=PAGER;X-EVOLUTION-UI-SLOT=6:pager 6
TEL;TYPE=CAR;X-EVOLUTION-UI-SLOT=7:car 7
TEL;TYPE=PREF;X-EVOLUTION-UI-SLOT=8:primary 8
X-AIM;X-EVOLUTION-UI-SLOT=1:AIM JOHN
X-YAHOO;X-EVOLUTION-UI-SLOT=2:YAHOO JDOE
X-ICQ;X-EVOLUTION-UI-SLOT=3:ICQ JD
X-GROUPWISE;X-EVOLUTION-UI-SLOT=4:GROUPWISE DOE
X-GADUGADU:GADUGADU DOE
X-JABBER:JABBER DOE
X-MSN:MSN DOE
X-SKYPE:SKYPE DOE
X-SIP:SIP DOE
PHOTO;ENCODING=b;TYPE=JPEG:/9j/4AAQSkZJRgABAQEASABIAAD/4QAWRXhpZgAATU0AKgAA
 AAgAAAAAAAD//gAXQ3JlYXRlZCB3aXRoIFRoZSBHSU1Q/9sAQwAFAwQEBAMFBAQEBQUFBgcM
 CAcHBwcPCwsJDBEPEhIRDxERExYcFxMUGhURERghGBodHR8fHxMXIiQiHiQcHh8e/9sAQwEF
 BQUHBgcOCAgOHhQRFB4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4e
 Hh4eHh4eHh4e/8AAEQgAFwAkAwEiAAIRAQMRAf/EABkAAQADAQEAAAAAAAAAAAAAAAAGBwgE
 Bf/EADIQAAECBQMCAwQLAAAAAAAAAAECBAADBQYRBxIhEzEUFSIIFjNBGCRHUVZ3lqXD0+P/
 xAAUAQEAAAAAAAAAAAAAAAAAAAAA/8QAFBEBAAAAAAAAAAAAAAAAAAAAAP/aAAwDAQACEQMR
 AD8AuX6UehP45/aXv9MTPTLVKxNSvMPcqu+a+XdLxf1SfJ6fU37PioTnOxfbOMc/KIZ7U/2V
 fmTR/wCaKlu6+blu/Ui72zxWtUmmUOrTaWwkWDT09FPR4K587OVrUfVsIwElPPPAbAjxr2um
 hWXbDu5rmfeApLPZ4hx0lzNm9aUJ9KAVHKlJHAPf7ozPLqWt9y6Z0EPGmoLNjTq48a1iaybJ
 YV52yEtCms5KJmAT61JXtJyUdyQTEc1WlMql7N1/oZ6jagVZVFfUyZPpFy5lvWcxU7Z03BUk
 GZLWJqVhPYLkIIPBEBtSEUyNAsjI1q1m/VP+UICwL/sqlXp7v+aOHsnyGttq218MtKd8+Ru2
 JXuScoO45Awe2CIi96aKW1cVyubkYVy6rTqz0J8a5t2qqZl0UjAMwYKScfPAJ+cIQHHP0Dth
 VFaMWt0XwxetnM50Ks2rsxL6ZMnJlJmb5hBBBEiVxjA28dznqo+hdksbQuS3Hs6tVtNzdM1Z
 /VH5nO3Bl/CJmYHKDynjv3zCEB5rLQNo0bIbydWNWxKljbLQLoWkISOAkBKAABCEID//2Q==
END:VCARD
'''
    joanVCard = '''BEGIN:VCARD
VERSION:3.0
FN:Joan Doe
N:Doe;Joan
ORG:Test Inc.
END:VCARD'''
    vcardFormat = '''BEGIN:VCARD
VERSION:3.0
FN:John_%(index)02d Doe
N:Doe;John_%(index)02d
ORG:Test Inc.
END:VCARD'''
    itemName = "test-dbus.vcf"
    itemNameFormat = "test-dbus-%d.vcf"

    def run(self, result):
        self.runTest(result)

    def setUp(self):
        self.setUpServer()

    def setUpConfigs(self, childPassword=None):
        self.setUpLocalSyncConfigs(childPassword, preventSlowSync=False)

    def checkInSync(self, numReports=2):
        '''verify that client and server do not need to transmit anything in an incremental sync'''
        self.sessionpath, self.session = self.createSession("server", True)
        self.setUpListeners(self.sessionpath)
        self.session.Sync("local-cache-incremental", {})
        loop.run()
        self.assertEqual(DBusUtil.quit_events, ["session " + self.sessionpath + " done"])
        report = self.checkSync(numReports=numReports)
        self.assertEqual("local-cache-incremental", report.get('source-addressbook-mode'))
        self.assertEqual("0", report.get('source-addressbook-stat-remote-added-total', "0"))
        self.assertEqual("0", report.get('source-addressbook-stat-remote-updated-total', "0"))
        self.assertEqual("0", report.get('source-addressbook-stat-remote-removed-total', "0"))
        self.assertEqual("0", report.get('source-addressbook-stat-local-added-total', "0"))
        self.assertEqual("0", report.get('source-addressbook-stat-local-updated-total', "0"))
        self.assertEqual("0", report.get('source-addressbook-stat-local-removed-total', "0"))
        self.session.Detach()
        DBusUtil.quit_events = []
        self.sessionpath, self.session = self.createSession("target-config@client", True)
        reports = self.session.GetReports(0, 100, utf8_strings=True)
        self.assertEqual(numReports, len(reports))
        report = reports[0]
        self.assertEqual("two-way", report.get('source-addressbook-mode'))
        self.assertEqual("0", report.get('source-addressbook-stat-remote-added-total', "0"))
        self.assertEqual("0", report.get('source-addressbook-stat-remote-updated-total', "0"))
        self.assertEqual("0", report.get('source-addressbook-stat-remote-removed-total', "0"))
        self.assertEqual("0", report.get('source-addressbook-stat-local-added-total', "0"))
        self.assertEqual("0", report.get('source-addressbook-stat-local-updated-total', "0"))
        self.assertEqual("0", report.get('source-addressbook-stat-local-removed-total', "0"))
        self.session.Detach()

    @timeout(100)
    def testItemRemoval(self):
        """TestLocalCache.testItemRemoval - ensure that extra item on server gets removed"""
        self.setUpConfigs()
        os.makedirs(self.serverDB)
        output = open(os.path.join(self.serverDB, self.itemName), "w")
        output.write(self.johnVCard)
        output.close()
        self.setUpListeners(self.sessionpath)
        # ask for incremental caching, expecting it do be done in slow mode
        self.session.Sync("local-cache-incremental", {})
        loop.run()
        self.assertEqual(DBusUtil.quit_events, ["session " + self.sessionpath + " done"])
        # check sync from server perspective
        report = self.checkSync()
        self.assertEqual("local-cache-slow", report.get('source-addressbook-mode'))
        self.assertEqual("0", report.get('source-addressbook-stat-remote-added-total', "0"))
        self.assertEqual("0", report.get('source-addressbook-stat-remote-updated-total', "0"))
        self.assertEqual("0", report.get('source-addressbook-stat-remote-removed-total', "0"))
        self.assertEqual("0", report.get('source-addressbook-stat-local-added-total', "0"))
        self.assertEqual("0", report.get('source-addressbook-stat-local-updated-total', "0"))
        self.assertEqual("1", report.get('source-addressbook-stat-local-removed-total', "0"))
        self.assertEqual(0, len(os.listdir(self.serverDB)))
        self.assertEqual(0, len(os.listdir(self.clientDB)))
        # check client report
        self.session.Detach()
        DBusUtil.quit_events = []
        self.sessionpath, self.session = self.createSession("target-config@client", True)
        reports = self.session.GetReports(0, 100, utf8_strings=True)
        self.assertEqual(1, len(reports))
        report = reports[0]
        self.assertEqual("slow", report.get('source-addressbook-mode'))
        self.assertEqual("0", report.get('source-addressbook-stat-remote-added-total', "0"))
        self.assertEqual("0", report.get('source-addressbook-stat-remote-updated-total', "0"))
        self.assertEqual("0", report.get('source-addressbook-stat-remote-removed-total', "0"))
        self.assertEqual("0", report.get('source-addressbook-stat-local-added-total', "0"))
        self.assertEqual("0", report.get('source-addressbook-stat-local-updated-total', "0"))
        self.assertEqual("0", report.get('source-addressbook-stat-local-removed-total', "0"))
        self.session.Detach()

        self.checkInSync()

    def doItemChange(self, numAdditional=0, syncFirst=False, change="Add"):
        self.setUpConfigs()
        entries = []
        os.makedirs(self.clientDB)
        os.makedirs(self.serverDB)

        numReports = 1
        added = 0
        updated = 0
        deleted = 0

        # Create additional items in client and server, before initial
        # sync. Creating items on the server later would violate the
        # rule that items on the server are only written during a
        # sync.
        for i in range(0, numAdditional):
            filename = self.itemNameFormat % i
            data = self.vcardFormat % { 'index': i }
            entries.append(filename)
            output = open(os.path.join(self.clientDB, filename), "w")
            output.write(data)
            output.close()
            output = open(os.path.join(self.serverDB, filename), "w")
            output.write(data)
            output.close()

        if syncFirst:
            # get client and server into sync with empty databases
            self.setUpListeners(self.sessionpath)
            self.session.Sync("local-cache-incremental", {})
            loop.run()
            self.assertEqual(DBusUtil.quit_events, ["session " + self.sessionpath + " done"])
            report = self.checkSync(numReports=numReports)
            self.assertEqual("local-cache-slow", report.get('source-addressbook-mode'))
            self.assertEqual("0", report.get('source-addressbook-stat-remote-added-total', "0"))
            self.assertEqual("0", report.get('source-addressbook-stat-remote-updated-total', "0"))
            self.assertEqual("0", report.get('source-addressbook-stat-remote-removed-total', "0"))
            self.assertEqual("0", report.get('source-addressbook-stat-local-added-total', "0"))
            self.assertEqual("0", report.get('source-addressbook-stat-local-updated-total', "0"))
            self.assertEqual("0", report.get('source-addressbook-stat-local-removed-total', "0"))
            self.session.Detach()
            DBusUtil.quit_events = []
            self.sessionpath, self.session = self.createSession("server", True)
            numReports = numReports + 1

        # create named contact on client
        entries.append(self.itemName)
        output = open(os.path.join(self.clientDB, self.itemName), "w")
        output.write(self.johnVCard)
        output.close()

        # ask for incremental caching, expecting it do be done in slow mode
        # or incremental, depending on whether both sides were in sync
        self.setUpListeners(self.sessionpath)
        self.session.Sync("local-cache-incremental", {})
        loop.run()
        self.assertEqual(DBusUtil.quit_events, ["session " + self.sessionpath + " done"])

        # check sync from server perspective
        report = self.checkSync(numReports=numReports)
        self.assertEqual(syncFirst and "local-cache-incremental" or "local-cache-slow",
                         report.get('source-addressbook-mode'))
        self.assertEqual("0", report.get('source-addressbook-stat-remote-added-total', "0"))
        self.assertEqual("0", report.get('source-addressbook-stat-remote-updated-total', "0"))
        self.assertEqual("0", report.get('source-addressbook-stat-remote-removed-total', "0"))
        self.assertEqual("1", report.get('source-addressbook-stat-local-added-total', "0"))
        self.assertEqual("0", report.get('source-addressbook-stat-local-updated-total', "0"))
        self.assertEqual("0", report.get('source-addressbook-stat-local-removed-total', "0"))
        self.assertEqual(1 + numAdditional, len(os.listdir(self.serverDB)))
        clientDBEntries = os.listdir(self.clientDB)
        clientDBEntries.sort()
        entries.sort()
        self.assertEqual(entries, clientDBEntries)

        # check client report
        self.session.Detach()
        DBusUtil.quit_events = []
        self.sessionpath, self.session = self.createSession("target-config@client", True)
        reports = self.session.GetReports(0, 100, utf8_strings=True)
        self.assertEqual(numReports, len(reports))
        report = reports[0]
        self.assertEqual(syncFirst and "two-way" or "slow", report.get('source-addressbook-mode'))
        self.assertEqual(str(1 + (not syncFirst and numAdditional or 0)), report.get('source-addressbook-stat-remote-added-total', "0"))
        self.assertEqual("0", report.get('source-addressbook-stat-remote-updated-total', "0"))
        self.assertEqual("0", report.get('source-addressbook-stat-remote-removed-total', "0"))
        self.assertEqual("0", report.get('source-addressbook-stat-local-added-total', "0"))
        self.assertEqual("0", report.get('source-addressbook-stat-local-updated-total', "0"))
        self.assertEqual("0", report.get('source-addressbook-stat-local-removed-total', "0"))
        self.session.Detach()

        numReports = numReports + 1

        if change == "Add" or change == "Add+Slow":
            # client and server are now in sync
            self.checkInSync(numReports=numReports)
        elif change == "Update":
            # update item to something completely, using an incremental sync
            serverContent = os.listdir(self.serverDB)
            output = open(os.path.join(self.clientDB, self.itemName), "w")
            output.write(self.joanVCard)
            output.close()
            self.sessionpath, self.session = self.createSession("server", True)
            self.setUpListeners(self.sessionpath)
            self.session.Sync("local-cache-incremental", {})
            loop.run()
            self.assertEqual(DBusUtil.quit_events, ["session " + self.sessionpath + " done"])
            report = self.checkSync(numReports=numReports)
            self.assertEqual("local-cache-incremental", report.get('source-addressbook-mode'))
            self.assertEqual("0", report.get('source-addressbook-stat-remote-added-total', "0"))
            self.assertEqual("0", report.get('source-addressbook-stat-remote-updated-total', "0"))
            self.assertEqual("0", report.get('source-addressbook-stat-remote-removed-total', "0"))
            self.assertEqual("0", report.get('source-addressbook-stat-local-added-total', "0"))
            self.assertEqual("1", report.get('source-addressbook-stat-local-updated-total', "0"))
            self.assertEqual("0", report.get('source-addressbook-stat-local-removed-total', "0"))
            self.session.Detach()
            self.assertEqual(serverContent, os.listdir(self.serverDB))
            clientDBEntries = os.listdir(self.clientDB)
            clientDBEntries.sort()
            self.assertEqual(entries, clientDBEntries)
            DBusUtil.quit_events = []
            self.sessionpath, self.session = self.createSession("target-config@client", True)
            reports = self.session.GetReports(0, 100, utf8_strings=True)
            self.assertEqual(numReports, len(reports))
            report = reports[0]
            self.assertEqual("two-way", report.get('source-addressbook-mode'))
            self.assertEqual("0", report.get('source-addressbook-stat-remote-added-total', "0"))
            self.assertEqual("1", report.get('source-addressbook-stat-remote-updated-total', "0"))
            self.assertEqual("0", report.get('source-addressbook-stat-remote-removed-total', "0"))
            self.assertEqual("0", report.get('source-addressbook-stat-local-added-total', "0"))
            self.assertEqual("0", report.get('source-addressbook-stat-local-updated-total', "0"))
            self.assertEqual("0", report.get('source-addressbook-stat-local-removed-total', "0"))
            self.session.Detach()
        elif change == "Delete":
            # remove item, using an incremental sync
            os.unlink(os.path.join(self.clientDB, self.itemName))
            self.sessionpath, self.session = self.createSession("server", True)
            self.setUpListeners(self.sessionpath)
            self.session.Sync("local-cache-incremental", {})
            loop.run()
            self.assertEqual(DBusUtil.quit_events, ["session " + self.sessionpath + " done"])
            report = self.checkSync(numReports=numReports)
            self.assertEqual("local-cache-incremental", report.get('source-addressbook-mode'))
            self.assertEqual("0", report.get('source-addressbook-stat-remote-added-total', "0"))
            self.assertEqual("0", report.get('source-addressbook-stat-remote-updated-total', "0"))
            self.assertEqual("0", report.get('source-addressbook-stat-remote-removed-total', "0"))
            self.assertEqual("0", report.get('source-addressbook-stat-local-added-total', "0"))
            self.assertEqual("0", report.get('source-addressbook-stat-local-updated-total', "0"))
            self.assertEqual("1", report.get('source-addressbook-stat-local-removed-total', "0"))
            self.session.Detach()
            self.assertEqual(numAdditional, len(os.listdir(self.serverDB)))
            clientDBEntries = os.listdir(self.clientDB)
            clientDBEntries.sort()
            entries.remove(self.itemName)
            self.assertEqual(entries, clientDBEntries)
            DBusUtil.quit_events = []
            self.sessionpath, self.session = self.createSession("target-config@client", True)
            reports = self.session.GetReports(0, 100, utf8_strings=True)
            self.assertEqual(numReports, len(reports))
            report = reports[0]
            self.assertEqual("two-way", report.get('source-addressbook-mode'))
            self.assertEqual("0", report.get('source-addressbook-stat-remote-added-total', "0"))
            self.assertEqual("0", report.get('source-addressbook-stat-remote-updated-total', "0"))
            self.assertEqual("1", report.get('source-addressbook-stat-remote-removed-total', "0"))
            self.assertEqual("0", report.get('source-addressbook-stat-local-added-total', "0"))
            self.assertEqual("0", report.get('source-addressbook-stat-local-updated-total', "0"))
            self.assertEqual("0", report.get('source-addressbook-stat-local-removed-total', "0"))
            self.session.Detach()

        numReports = numReports + 1

        if change == "Add+Slow":
            # explicitly request a slow sync
            DBusUtil.quit_events = []
            self.sessionpath, self.session = self.createSession("server", True)
            self.setUpListeners(self.sessionpath)
            self.session.Sync("local-cache-slow", {})
            loop.run()
            self.assertEqual(DBusUtil.quit_events, ["session " + self.sessionpath + " done"])
            report = self.checkSync(numReports=numReports)
            self.assertEqual("local-cache-slow", report.get('source-addressbook-mode'))
            self.assertEqual("0", report.get('source-addressbook-stat-remote-added-total', "0"))
            self.assertEqual("0", report.get('source-addressbook-stat-remote-updated-total', "0"))
            self.assertEqual("0", report.get('source-addressbook-stat-remote-removed-total', "0"))
            self.assertEqual("0", report.get('source-addressbook-stat-local-added-total', "0"))
            self.assertEqual("0", report.get('source-addressbook-stat-local-updated-total', "0"))
            self.assertEqual("0", report.get('source-addressbook-stat-local-removed-total', "0"))
            self.session.Detach()
            numReports = numReports + 1


    @timeout(100)
    def testItemAdd(self):
        """TestLocalCache.testItemAdd - ensure that new item from client gets added in initial slow sync"""
        self.doItemChange()

    @timeout(200)
    def testItemAdd100(self):
        """TestLocalCache.testItemAdd100 - ensure that new item from client gets added in initial slow sync while leaving 100 items unchanged"""
        self.doItemChange(numAdditional=100)

    @timeout(100)
    def testSyncMode(self):
        """TestLocalCache.testSyncMode - ensure that requesting specific caching sync works"""
        self.doItemChange(change="Add+Slow")

    @timeout(100)
    def testItemAddIncremental(self):
        """TestLocalCache.testItemAddIncremental - ensure that new item from client gets added in incremental sync"""
        self.doItemChange(syncFirst=True)

    @timeout(200)
    def testItemAdd100Incremental(self):
        """TestLocalCache.testItemAdd100Incremental - ensure that new item from client gets added in incremental while leaving 100 items unchanged"""
        self.doItemChange(numAdditional=100, syncFirst=True)

    @timeout(100)
    def testItemUpdate(self):
        """TestLocalCache.testItemUpdate - ensure that an item can be updated incrementally"""
        self.doItemChange(change="Update")

    @timeout(200)
    def testItemUpdate100(self):
        """TestLocalCache.testItemUpdate100 - ensure that an item can be updated incrementally while leaving 100 items unchanged"""
        self.doItemChange(change="Update", numAdditional=100)

    @timeout(100)
    def testItemDelete(self):
        """TestLocalCache.testItemUpdate - ensure that an item can be deleted incrementally"""
        self.doItemChange(change="Delete")

    @timeout(200)
    def testItemDelete100(self):
        """TestLocalCache.testItemUpdate100 - ensure that an item can be deleted incrementally while leaving 100 items unchanged"""
        self.doItemChange(change="Delete", numAdditional=100)

    def doPropertyRemoval(self, step=0, numAdditional=0):
        """ensure that obsolete items of an item get removed, either during initial slow sync, second slow sync or incremental sync"""
        self.setUpConfigs()
        entries = []
        os.makedirs(self.clientDB)
        os.makedirs(self.serverDB)
        output = open(os.path.join(self.serverDB, self.itemName), "w")
        output.write(self.johnComplexVCard)
        output.close()
        entries.append(self.itemName)
        output = open(os.path.join(self.clientDB, self.itemName), "w")
        if step == 0:
            # Client has simple version of John,
            # slow sync applies update.
            data = self.johnVCard
        else:
            # Client has same data as on server,
            # slow sync changes nothing.
            data = self.johnComplexVCard
        output.write(data)
        output.close()

        # create additional items in client and server
        for i in range(0, numAdditional):
            filename = self.itemNameFormat % i
            data = self.vcardFormat % { 'index': i }
            entries.append(filename)
            output = open(os.path.join(self.clientDB, filename), "w")
            output.write(data)
            output.close()
            output = open(os.path.join(self.serverDB, filename), "w")
            output.write(data)
            output.close()

        self.setUpListeners(self.sessionpath)
        self.session.Sync("local-cache-incremental", {})
        loop.run()
        self.assertEqual(DBusUtil.quit_events, ["session " + self.sessionpath + " done"])
        # check sync from server perspective
        report = self.checkSync()
        self.assertEqual("local-cache-slow", report.get('source-addressbook-mode'))
        self.assertEqual("0", report.get('source-addressbook-stat-remote-added-total', "0"))
        self.assertEqual("0", report.get('source-addressbook-stat-remote-updated-total', "0"))
        self.assertEqual("0", report.get('source-addressbook-stat-remote-removed-total', "0"))
        self.assertEqual("0", report.get('source-addressbook-stat-local-added-total', "0"))
        self.assertEqual((step == 0) and "1" or "0", report.get('source-addressbook-stat-local-updated-total', "0"))
        self.assertEqual("0", report.get('source-addressbook-stat-local-removed-total', "0"))
        self.assertEqual(1 + numAdditional, len(os.listdir(self.serverDB)))
        clientDBEntries = os.listdir(self.clientDB)
        clientDBEntries.sort()
        entries.sort()
        self.assertEqual(entries, clientDBEntries)
        # check client report
        self.session.Detach()
        DBusUtil.quit_events = []
        self.sessionpath, self.session = self.createSession("target-config@client", True)
        reports = self.session.GetReports(0, 100, utf8_strings=True)
        self.assertEqual(1, len(reports))
        report = reports[0]
        self.assertEqual("slow", report.get('source-addressbook-mode'))
        self.assertEqual(str(1 + numAdditional), report.get('source-addressbook-stat-remote-added-total', "0"))
        self.assertEqual("0", report.get('source-addressbook-stat-remote-updated-total', "0"))
        self.assertEqual("0", report.get('source-addressbook-stat-remote-removed-total', "0"))
        self.assertEqual("0", report.get('source-addressbook-stat-local-added-total', "0"))
        self.assertEqual("0", report.get('source-addressbook-stat-local-updated-total', "0"))
        self.assertEqual("0", report.get('source-addressbook-stat-local-removed-total', "0"))
        self.session.Detach()

        if step == 0:
            # Work done already, just check that in sync.
            self.checkInSync()
        else:
            # update item to simple version, using an incremental sync
            # or another slow sync
            if step == 1:
                # force slow sync by removing client-side meta data
                shutil.rmtree(os.path.join(xdg_root, 'config', 'syncevolution', 'default', 'peers', 'server', '.@client', '.synthesis'))
            serverContent = os.listdir(self.serverDB)
            output = open(os.path.join(self.clientDB, self.itemName), "w")
            output.write(self.johnVCard)
            output.close()
            self.sessionpath, self.session = self.createSession("server", True)
            self.setUpListeners(self.sessionpath)
            self.session.Sync("local-cache-incremental", {})
            loop.run()
            self.assertEqual(DBusUtil.quit_events, ["session " + self.sessionpath + " done"])
            report = self.checkSync(numReports=2)
            self.assertEqual(step == 1 and "local-cache-slow" or "local-cache-incremental", report.get('source-addressbook-mode'))
            self.assertEqual("0", report.get('source-addressbook-stat-remote-added-total', "0"))
            self.assertEqual("0", report.get('source-addressbook-stat-remote-updated-total', "0"))
            self.assertEqual("0", report.get('source-addressbook-stat-remote-removed-total', "0"))
            self.assertEqual("0", report.get('source-addressbook-stat-local-added-total', "0"))
            self.assertEqual("1", report.get('source-addressbook-stat-local-updated-total', "0"))
            self.assertEqual("0", report.get('source-addressbook-stat-local-removed-total', "0"))
            self.session.Detach()
            self.assertEqual(serverContent, os.listdir(self.serverDB))
            clientDBEntries = os.listdir(self.clientDB)
            clientDBEntries.sort()
            self.assertEqual(entries, clientDBEntries)
            DBusUtil.quit_events = []
            self.sessionpath, self.session = self.createSession("target-config@client", True)
            reports = self.session.GetReports(0, 100, utf8_strings=True)
            self.assertEqual(2, len(reports))
            report = reports[0]
            self.assertEqual(step == 1 and "slow" or "two-way", report.get('source-addressbook-mode'))
            self.assertEqual(step == 1 and str(1 + numAdditional) or "0", report.get('source-addressbook-stat-remote-added-total', "0"))
            self.assertEqual(step == 1 and "0" or "1", report.get('source-addressbook-stat-remote-updated-total', "0"))
            self.assertEqual("0", report.get('source-addressbook-stat-remote-removed-total', "0"))
            self.assertEqual("0", report.get('source-addressbook-stat-local-added-total', "0"))
            self.assertEqual("0", report.get('source-addressbook-stat-local-updated-total', "0"))
            self.assertEqual("0", report.get('source-addressbook-stat-local-removed-total', "0"))
            self.session.Detach()

        if step == 1:
            # second sync was a slow sync, now we should be in sync
            self.checkInSync(numReports=3)

        # Server item should be the simple one now, as in the client.
        sub = subprocess.Popen(['synccompare', self.clientDB, self.serverDB],
                               stdout=subprocess.PIPE,
                               stderr=subprocess.STDOUT)
        stdout, stderr = sub.communicate()
        self.assertEqual(0, sub.returncode,
                         msg=stdout)

    @timeout(100)
    def testPropertyRemovalSlow(self):
        """TestLocalCache.testPropertyRemovalSlow - ensure that obsolete item properties are removed during slow sync"""
        self.doPropertyRemoval()

    @timeout(200)
    def testPropertyRemovalSlow100(self):
        """TestLocalCache.testPropertyRemovalSlow100 - ensure that obsolete item properties are removed during slow sync while leaving 100 items unchanged"""
        self.doPropertyRemoval(numAdditional=100)

    @timeout(100)
    def testPropertyRemovalSecondSlow(self):
        """TestLocalCache.testPropertyRemovalSecondSlow - ensure that obsolete item properties are removed during non-initial slow sync"""
        self.doPropertyRemoval(step=1)

    @timeout(200)
    def testPropertyRemovalSecondSlow100(self):
        """TestLocalCache.testPropertyRemovalSecondSlow100 - ensure that obsolete item properties are removed during non-initial slow sync while leaving 100 items unchanged"""
        self.doPropertyRemoval(step=1, numAdditional=100)

    @timeout(100)
    def testPropertyRemovalIncremental(self):
        """TestLocalCache.testPropertyRemovalIncremental - ensure that obsolete item properties are removed during incremental sync"""
        self.doPropertyRemoval(step=2)

    @timeout(200)
    def testPropertyRemovalIncremental100(self):
        """TestLocalCache.testPropertyRemoval - ensure that obsolete item properties are removed during incremental sync while leaving 100 items unchanged"""
        self.doPropertyRemoval(step=2, numAdditional=100)


class TestFileNotify(unittest.TestCase, DBusUtil):
    """syncevo-dbus-server must stop if one of its files mapped into
    memory (executable, libraries) change. Furthermore it must restart
    if automatic syncs are enabled. This class simulates such file changes
    by starting the server, identifying the location of the main executable,
    and renaming it back and forth."""

    def setUp(self):
        self.setUpServer()
        self.serverexe = self.serverExecutable()

    def tearDown(self):
        if os.path.isfile(self.serverexe + ".bak"):
            os.rename(self.serverexe + ".bak", self.serverexe)

    def run(self, result):
        self.runTest(result)

    def modifyServerFile(self):
        """rename server executable to trigger shutdown"""
        os.rename(self.serverexe, self.serverexe + ".bak")
        os.rename(self.serverexe + ".bak", self.serverexe)        

    @timeout(100)
    def testShutdown(self):
        """TestFileNotify.testShutdown - update server binary for 30 seconds, check that it shuts down at most 15 seconds after last mod"""
        self.assertTrue(self.isServerRunning())
        i = 0
        # Server must not shut down immediately, more changes might follow.
        # Simulate that.
        while i < 6:
            self.modifyServerFile()
            time.sleep(5)
            i = i + 1
        self.assertTrue(self.isServerRunning())
        if usingValgrind():
            # valgrind shutdown takes time
            time.sleep(20)
        else:
            time.sleep(10)
        self.assertFalse(self.isServerRunning())

    @timeout(100)
    def testSession(self):
        """TestFileNotify.testSession - create session, shut down directly after closing it"""
        self.assertTrue(self.isServerRunning())
        self.setUpSession("")
        self.modifyServerFile()
        time.sleep(15)
        self.assertTrue(self.isServerRunning())
        self.session.Detach()
        # should shut down almost immediately,
        # except when using valgrind
        if usingValgrind():
            time.sleep(20)
        else:
            time.sleep(1)
        self.assertFalse(self.isServerRunning())

    @timeout(100)
    def testSession2(self):
        """TestFileNotify.testSession2 - create session, shut down after quiescence period after closing it"""
        self.assertTrue(self.isServerRunning())
        self.setUpSession("")
        self.modifyServerFile()
        self.assertTrue(self.isServerRunning())
        self.session.Detach()
        time.sleep(8)
        self.assertTrue(self.isServerRunning())
        if usingValgrind():
            time.sleep(20)
        else:
            time.sleep(4)
        self.assertFalse(self.isServerRunning())

    @timeout(100)
    def testSession3(self):
        """TestFileNotify.testSession3 - shut down after quiescence period without activating a pending session request"""
        self.assertTrue(self.isServerRunning())
        self.modifyServerFile()
        self.assertTrue(self.isServerRunning())
        time.sleep(8)
        self.assertTrue(self.isServerRunning())
        self.session = None
        try:
            # this should not succeed: the server should rejects the
            # session request because it is shutting down
            self.setUpSession("")
        except dbus.DBusException, ex:
            self.assertEqual(str(ex),
                             "org.syncevolution.Exception: server shutting down")
        else:
            self.fail("no exception thrown")

    @timeout(100)
    def testRestart(self):
        """TestFileNotify.testRestart - set up auto sync, then check that server restarts"""
        self.assertTrue(self.isServerRunning())
        self.setUpSession("memotoo")
        config = self.session.GetConfig(True, utf8_strings=True)
        config[""]["autoSync"] = "1"
        self.session.SetConfig(False, False, config)
        self.assertTrue(self.isServerRunning())
        self.session.Detach()
        self.modifyServerFile()
        bus_name = self.server.bus_name
        # give server time to restart
        if usingValgrind():
            time.sleep(40)
        else:
            time.sleep(15)
        self.setUpServer()
        self.assertNotEqual(bus_name, self.server.bus_name)
        # serverExecutable() will fail if the service wasn't properly
        # with execve() because then the old process is dead.
        self.assertEqual(self.serverexe, self.serverExecutable())

bt_mac         = "D4:5D:42:73:E4:6C"
bt_fingerprint = "Nokia 5230"
bt_name        = "My Nokia 5230"
bt_template    = "Bluetooth_%s_1" % (bt_mac)
bt_device      = "%s/dev_%s" % (bt_adaptor, string.replace(bt_mac, ':', '_'))

class BluezAdapter (dbus.service.Object):
    def __init__(self):
        self.SUPPORTS_MULTIPLE_OBJECT_PATHS = True
        bus_name = dbus.service.BusName('org.bluez', bus)
        dbus.service.Object.__init__(self, bus_name, bt_adaptor)

    @dbus.service.signal(dbus_interface='org.bluez.Adapter', signature='o')
    def DeviceCreated(self, obj):
        return bt_adaptor

    @dbus.service.signal(dbus_interface='org.bluez.Adapter', signature='o')
    def DeviceRemoved(self, obj):
        return bt_device

    @dbus.service.method(dbus_interface='org.bluez.Adapter', in_signature='', out_signature='ao')
    def ListDevices(self):
        return [bt_device]

class BluezDevice (dbus.service.Object):
    def __init__(self):
        self.SUPPORTS_MULTIPLE_OBJECT_PATHS = True
        bus_name = dbus.service.BusName('org.bluez', bus)
        dbus.service.Object.__init__(self, bus_name, bt_device)

    @dbus.service.method(dbus_interface='org.bluez.Device', in_signature='', out_signature='a{sv}')
    def GetProperties(self):
        return {"Name": bt_name,
                "Address": bt_mac,
                "UUIDs": ['00000002-0000-1000-8000-0002ee000002',
                          '00001000-0000-1000-8000-00805f9b34fb',
                          '00001101-0000-1000-8000-00805f9b34fb',
                          '00001103-0000-1000-8000-00805f9b34fb',
                          '00001105-0000-1000-8000-00805f9b34fb',
                          '00001106-0000-1000-8000-00805f9b34fb',
                          '0000110a-0000-1000-8000-00805f9b34fb',
                          '0000110c-0000-1000-8000-00805f9b34fb',
                          '0000110e-0000-1000-8000-00805f9b34fb',
                          '00001112-0000-1000-8000-00805f9b34fb',
                          '0000111b-0000-1000-8000-00805f9b34fb',
                          '0000111f-0000-1000-8000-00805f9b34fb',
                          '0000112d-0000-1000-8000-00805f9b34fb',
                          '0000112f-0000-1000-8000-00805f9b34fb',
                          '00001200-0000-1000-8000-00805f9b34fb',
                          '00005005-0000-1000-8000-0002ee000001',
                          '00005557-0000-1000-8000-0002ee000001',
                          '00005601-0000-1000-8000-0002ee000001']}

    @dbus.service.method(dbus_interface='org.bluez.Device', in_signature='s', out_signature='a{us}')
    def DiscoverServices(self, ignore):
        # This should be the last method to call. So, we need to quit the loop to exit.
        loop.quit()
        return { 65569L: '<?xml version="1.0" encoding="UTF-8" ?><record><attribute id="0x0000"><uint32 value="0x00010021" /></attribute><attribute id="0x0001"><sequence><uuid value="0x1200" /></sequence></attribute><attribute id="0x0005"><sequence><uuid value="0x1002" /></sequence></attribute><attribute id="0x0006"><sequence><uint16 value="0x454e" /><uint16 value="0x006a" /><uint16 value="0x0100" /></sequence></attribute><attribute id="0x0100"><text value="PnP Information" /></attribute><attribute id="0x0200"><uint16 value="0x0102" /></attribute><attribute id="0x0201"><uint16 value="0x0001" /></attribute><attribute id="0x0202"><uint16 value="0x00e7" /></attribute><attribute id="0x0203"><uint16 value="0x0000" /></attribute><attribute id="0x0204"><boolean value="true" /></attribute><attribute id="0x0205"><uint16 value="0x0001" /></attribute></record>'}

    @dbus.service.signal(dbus_interface='org.bluez.Device', signature='sv')
    def PropertyChanged(self, key, value):
        if(key == "Name"):
            bt_name = value

    def emitSignal(self):
        """ Change the device name. """
        self.PropertyChanged("Name", [string.replace(bt_name, "My", "Changed")])
        return

class TestBluetooth(unittest.TestCase, DBusUtil):
    """Tests that Bluetooth works properly."""

    def setUp(self):
        self.adp_conn = BluezAdapter()
        self.dev_conn = BluezDevice()
        loop.run()
        self.setUpServer()

    def tearDown(self):
        self.adp_conn.remove_from_connection()
        self.dev_conn.remove_from_connection()

    def run(self, result):
        self.runTest(result)

    @property("ENV", "DBUS_TEST_BLUETOOTH=session")
    @timeout(100)
    def testBluetoothTemplates(self):
        """TestBluetooth.testBluetoothTemplates - check for the bluetooth device's template"""
        configs = self.server.GetConfigs(True, utf8_strings=True)
        config = next((config for config in configs if config == bt_template), None)
        self.failUnless(config)

    @property("ENV", "DBUS_TEST_BLUETOOTH=session")
    @timeout(100)
    def testBluetoothNames(self):
        """TestBluetooth.testBluetoothNames - check that fingerPrint/peerName/deviceName/hardwareName are set correctly"""
        # This needs to be called before we can fetch the single config.
        configs = self.server.GetConfigs(True, utf8_strings=True)
        config  = self.server.GetConfig(bt_template, True, utf8_strings=True)
        # user-configurable name
        self.failUnlessEqual(config['']["deviceName"], bt_name)
        # must not be set
        self.assertNotIn("peerName", config[''])
        # all of the possible strings in the template, must include the hardware name of this example device
        self.failIf(string.find(config['']["fingerPrint"], bt_fingerprint) < 0)
        # real hardware information
        self.failUnlessEqual(config['']["hardwareName"], bt_fingerprint)

def createFiles(root, content, append = False):
    '''create directory hierarchy, overwriting previous content'''
    if not append:
        shutil.rmtree(root, True)

    entries = content.split("\n")
    outname = ''
    outfile = None
    for entry in entries:
        if not entry:
            continue
        parts = entry.split(":", 1)
        newname = parts[0]
        line = parts[1]
        if newname != outname:
            fullpath = root + "/" + newname
            try:
                os.makedirs(fullpath[0:fullpath.rindex("/")])
            except:
                pass
            mode = "w"
            if append:
                mode = "a"
            outfile = open(fullpath, mode)
            outname = newname
        outfile.write(line + "\n")
    outfile.close()

isPropRegEx = re.compile(r'^([a-zA-Z]+) = ')
def isPropAssignment (line):
    '''true if "<word> = "'''
    m = isPropRegEx.search(line)
    if not m:
        return False
    # exclude some false positives
    if m.group(1) in ('KCalExtended', 'mkcal', 'QtContacts'):
        return False
    return True

def scanFiles(root, peer = '', onlyProps = True, directory = ''):
    '''turn directory hierarchy into string
    root      - root path in file system
    peer      - if non-empty, then ignore all <root>/peers/<foo>
                directories where <foo> != peer
    onlyProps - ignore lines which are comments
    directory - a subdirectory of root (used for recursion)'''
    newroot = root + '/' + directory
    out = ''

    for entry in sorted(os.listdir(newroot)):
        fullEntry = newroot + "/" + entry
        if os.path.isdir(fullEntry):
            if not (newroot.endswith("/peers") and peer and entry != peer):
                if directory:
                    newdir = directory + '/' + entry
                else:
                    newdir = entry
                out += scanFiles(root, peer, onlyProps, newdir)
        else:
            infile = open (fullEntry)
            for line in infile:
                line = line.rstrip("\r\n")
                if (line):
                    takeIt = False
                    if (line.startswith("# ")):
                        takeIt = isPropAssignment(line[2:])
                    else:
                        takeIt = True
                    if (not onlyProps or takeIt):
                        if (directory):
                            out += directory + "/"
                        out += entry + ':' + line + "\n"
    return out

def sortConfig(config):
    '''sort lines by file, preserving order inside each line'''
    lines = config.splitlines()
    linenr = -1

    unsorted = []
    for line in lines:
        linenr += 1
        if not line:
            continue
        parts = line.split(":", 1)
        element = parts[0], linenr, parts[1]
        unsorted.append(element)

    # stable sort because of line number
    # probably it would be stable without it
    # but better be safe than sorry
    lines = sorted(unsorted)
    unsorted = []
    newconfig = ""
    for line in lines:
        newconfig += line[0] + ":" + line[2] + "\n"

    return newconfig

def lastLine(string):
    return string.splitlines(True)[-1]

def stripOutput(string):
    # strip debug output, if it was enabled via env var
    if os.environ.get('SYNCEVOLUTION_DEBUG', None) != None:
        string = re.sub(r'\[DEBUG *\S*?\].*?\n', '', string)
    # remove time
    r = re.compile(r'^\[(\w+)\s+\d\d:\d\d:\d\d\]', re.MULTILINE)
    string = r.sub(r'[\1]', string)
    return string

def injectValues(config):
    res = config

    if False:
        # username/password not set in templates, only in configs
        # created via the command line - not anymore, but if it ever
        # comes back, here's the place for it
        res = res.replace("# username = ",
                          "username = your SyncML server account name",
                          1)
        res = res.replace("# password = ",
                          "password = your SyncML server password",
                          1)

    return res

def filterConfig(config):
    '''remove pure comment lines from buffer, also empty lines, also
    defaultPeer/keyring (because reference properties do not include global
    props)'''
    config_lines = config.splitlines()
    out = ''

    for line in config_lines:
        if line and \
                "defaultPeer =" not in line and \
                "keyring =" not in line and \
                (line.startswith("# ") == False or \
                     isPropAssignment(line[2:])):
            out += line + "\n"

    return out

def internalToIni(config):
    '''convert the internal config dump to .ini style (--print-config)'''
    config_lines = config.splitlines()
    ini = ''
    section = ''

    for line in config_lines:
        if not line:
            continue
        colon = line.find(':')
        prefix = line[0:colon]
        # internal values are not part of the --print-config output
        if ".internal.ini" in prefix or "= internal value" in line:
            continue

        # --print-config also does not duplicate the "type" property
        # remove the shared property
        if ":type" in line and line.startswith("sources/"):
            continue

        # sources/<name>/config.ini or spds/sources/<name>/config.ini
        endslash = prefix.rfind("/")
        if endslash > 1:
            slash = prefix.rfind("/", 0, endslash)
            if slash != -1:
                newsource = prefix[slash + 1:endslash]
                if newsource != section:
                    sources = prefix.find("/sources/")
                    if sources != -1 and newsource != "syncml":
                        ini += "[" + newsource + "]\n"
                        section = newsource

        assignment = line[colon + 1:]
        # substitute aliases with generic values
        assignment = assignment.replace("= syncml:auth-md5", "= md5", 1)
        assignment = assignment.replace("= syncml:auth-basic", "= basic", 1)

        ini += assignment + "\n"

    return ini

# result of removeComments(self.removeRandomUUID(filterConfig())) for
# Google Calendar template/config
googlecaldav = '''syncURL = https://www.google.com/calendar/dav/%u/user/?SyncEvolution=Google
printChanges = 0
dumpData = 0
deviceId = fixed-devid
IconURI = image://themedimage/icons/services/google-calendar
ConsumerReady = 1
peerType = WebDAV
[calendar]
sync = two-way
backend = CalDAV
'''

# result of removeComments(self.removeRandomUUID(filterConfig())) for
# Yahoo Calendar + Contacts
yahoo = '''printChanges = 0
dumpData = 0
deviceId = fixed-devid
IconURI = image://themedimage/icons/services/yahoo
ConsumerReady = 1
peerType = WebDAV
[addressbook]
sync = disabled
backend = CardDAV
[calendar]
sync = two-way
backend = CalDAV
'''

def removeComments(config):
    lines = config.splitlines()

    out = ''
    for line in lines:
        if line and not line.startswith("#"):
            out += line + "\n"

    return out

def filterIndented(config):
    '''remove lines indented with spaces'''
    lines = config.splitlines()

    out = ''
    first = True
    for line in lines:
        if not line.startswith(" "):
            if first:
                first = False
            else:
                out += "\n"
            out += line

    return out

def filterFiles(config):
    '''remove comment lines from scanFiles() output'''
    out = ''
    lines = config.splitlines()

    for line in lines:
        if line.find(":#") == -1:
            out += line + "\n"
    # strip last newline
#    if out:
#        return out[:-1]
    return out

class CmdlineUtil(DBusUtil):
    """Helper methods for running syncevolution command line tool."""

    def statusChanged(self, *args, **keywords):
        '''remember the command line session'''
        if args[0] == 'idle' and self.session == None:
            self.session = dbus.Interface(bus.get_object(self.server.bus_name,
                                                         keywords['path']),
                                          'org.syncevolution.Session')

    def runCmdline(self, args, env=None, expectSuccess=True, preserveOutputOrder=False,
                   sessionFlags=['no-sync'],
                   testInstance=None):
        '''Run the 'syncevolution' command line (from PATH) with the
        given arguments (list or tuple of strings). Uses environment
        used to run syncevo-dbus-server unless one is set
        explicitly. Unless told otherwise, the result of the command
        is checked for success. Usually stdout and stderr are captured
        separately, in which case relative order of messages from
        different streams cannot be tested. When that is relevant, set
        preserveOutputOrder=True and look only at the stdout.

        Returns tuple with stdout, stderr and result code. DBusUtil.events
        contains the status and progress events seen while the command line
        ran. self.session is the proxy for that session.'''
        s = self.startCmdline(args, env, preserveOutputOrder, testInstance)
        return self.finishCmdline(s, expectSuccess, sessionFlags)

    def startCmdline(self, args, env=None, preserveOutputOrder=False, testInstance=None):
        # Watch all future events, ignore old ones.
        while loop.get_context().iteration(False):
            pass
        DBusUtil.events = []
        self.session = None
        a = [ 'syncevolution' ]
        a.extend(args)
        if testInstance == None:
            testInstance = self
        # Explicitly pass an environment. Otherwise subprocess.Popen()
        # from Python 2.6 uses the environment passed to a previous
        # call to subprocess.Popen() (which will fail if the previous
        # test ran with an environment which had SYNCEVOLUTION_DEBUG
        # set).
        if env == None:
            cmdline_env = testInstance.storedenv
        else:
            cmdline_env = env
        if not 'cmdlineCounter' in dir(testInstance):
            testInstance.cmdlineCounter = 0
        testInstance.cmdlineCounter = testInstance.cmdlineCounter + 1
        if preserveOutputOrder:
            stdoutName = testInstance.testname + (".cmdline.%d.outerr.log" % testInstance.cmdlineCounter)
            stderrName = None
            stdout = open(stdoutName, "w")
            stderr = subprocess.STDOUT
        else:
            stdoutName = testInstance.testname + (".cmdline.%d.out.log" % testInstance.cmdlineCounter)
            stdout = open(stdoutName, "w")
            stderrName = testInstance.testname + (".cmdline.%d.err.log" % testInstance.cmdlineCounter)
            stderr = open(stderrName, "w")

        s = subprocess.Popen(a, stdout=stdout, stderr=stderr,
                             env=cmdline_env)
        s.stdoutName = stdoutName
        s.stderrName = stderrName
        return s

    def finishCmdline(self, s, expectSuccess=True, sessionFlags=['no-sync']):
        try:
            s.wait()
        except Exception, ex:
            message = str(ex)
            message = message + (s.stderrName and "\nStdout:\n" or "\nStdout + Stderr:\n")
            message = message + open(s.stdoutName).read()
            if s.stderrName:
                message = message + "\nStderr:\n"
                message = message + open(s.stderrName).read()
            raise Exception(message)
        s.wait()
        out = open(s.stdoutName).read()
        if s.stderrName:
            err = open(s.stderrName).read()
        else:
            err = None
        doFail = False
        if expectSuccess and s.returncode != 0:
            result = 'syncevolution command failed.'
            doFail = True
        elif not expectSuccess and s.returncode == 0:
            result = 'syncevolution was expected to fail, but it succeeded.'
            doFail = True
        if doFail:
            result += '\nOutput:\n%s' % out
            if s.stderrName != None:
                result += '\nSeparate stderr:\n%s' % err
            self.fail(result)

        # Collect D-Bus events, check session.
        while loop.get_context().iteration(False):
            pass
        if sessionFlags != None:
            self.assertTrue(self.session)
            self.assertEqual(self.session.GetFlags(timeout=self.dbusTimeout), sessionFlags)

        return (out, err, s.returncode)

    def sourceCheckOutput(self, sources=['addressbook', 'calendar', 'memo', 'todo']):
        '''returns the output produced by --configure when checking sources'''
        if not (isinstance(sources, type([])) or isinstance(sources, type(()))):
            sources = (sources,)
        return ''.join(['[INFO] %s: looking for databases...\n[INFO] %s: okay\n' % (i, i) for i in sources])

    def assertNoErrors(self, err):
        '''check that error output is empty'''
        self.assertEqualDiff('', err)


class TestCmdline(CmdlineUtil, unittest.TestCase):
    """Tests cmdline by Session::Execute()."""

    def setUp(self):
        self.setUpServer()
        self.setUpListeners(None)
        # All tests run with their own XDG root hierarchy.
        # Here are the config files.
        self.configdir = xdg_root + "/config/syncevolution"

    def run(self, result):
        # Runtime varies a lot when using valgrind, because
        # of the need to check an additional process. Allow
        # a lot more time when running under valgrind.
        self.runTest(result, own_xdg=True, own_home=True,
                     defTimeout=usingValgrind() and 600 or 20)

    cachedSSLServerCertificates = None
    def getSSLServerCertificates(self):
        '''Default SSLServerCertificates path as compiled into the SyncEvolution
        binaries. Determined once by asking for a template.'''
        if TestCmdline.cachedSSLServerCertificates == None:
            out, err, code = self.runCmdline(['--template', 'default',
                                              '--print-config'])
            self.assertNoErrors(err)
            m = re.search(r'^# SSLServerCertificates = (.*)\n', out, re.MULTILINE)
            self.assertTrue(m)
            TestCmdline.cachedSSLServerCertificates = m.group(1)
        return TestCmdline.cachedSSLServerCertificates

    cachedVersions = {}
    def getVersion(self, versionName):
        '''Parses the SyncConfig.h file to get a version number
        described by versionName.'''
        if not versionName in TestCmdline.cachedVersions:
            # Get the absolute path of the current python file.
            scriptpath = os.path.abspath(os.path.expanduser(os.path.expandvars(sys.argv[0])))
            # Get the path to SyncConfig.h
            header = os.path.join(os.path.dirname(scriptpath), '..', 'src', 'syncevo', 'SyncConfig.h')
            # do not care about escaping the versionName variable.
            # this function is only used from some version getters and
            # those are passing some simple strings with no regexp
            # metacharacters.
            prog = re.compile("^\s*static\s+const\s+int\s+{0}\s*=\s*(\d+);".format(versionName))
            found = False
            # will throw IOError if opening header fails
            for line in open(header):
                m = prog.match(line)
                if m:
                    TestCmdline.cachedVersions[versionName] = m.group(1)
                    found = True
                    break
            if not found:
                self.fail(versionname + " not found in SyncConfig.h")
        return TestCmdline.cachedVersions[versionName]

    def getRootMinVersion(self):
        return self.getVersion('CONFIG_ROOT_MIN_VERSION')

    def getRootCurVersion(self):
        return self.getVersion('CONFIG_ROOT_CUR_VERSION')

    def getContextMinVersion(self):
        return self.getVersion('CONFIG_CONTEXT_MIN_VERSION')

    def getContextCurVersion(self):
        return self.getVersion('CONFIG_CONTEXT_CUR_VERSION')

    def getPeerMinVersion(self):
        return self.getVersion('CONFIG_PEER_MIN_VERSION')

    def getPeerCurVersion(self):
        return self.getVersion('CONFIG_PEER_CUR_VERSION')

    def ScheduleWorldConfig(self,
                            peerMinVersion = None,
                            peerCurVersion = None,
                            contextMinVersion = None,
                            contextCurVersion = None):
        '''properties sorted by the order in which they are defined in
        the sync and sync source property registry'''
        if peerMinVersion == None:
            peerMinVersion = self.getPeerMinVersion()
        if peerCurVersion == None:
            peerCurVersion = self.getPeerCurVersion()
        if contextMinVersion == None:
            contextMinVersion = self.getContextMinVersion()
        if contextCurVersion == None:
            contextCurVersion = self.getContextCurVersion()

        return '''peers/scheduleworld/.internal.ini:peerMinVersion = {0}
peers/scheduleworld/.internal.ini:peerCurVersion = {1}
peers/scheduleworld/.internal.ini:# HashCode = 0
peers/scheduleworld/.internal.ini:# ConfigDate = 
peers/scheduleworld/.internal.ini:# lastNonce = 
peers/scheduleworld/.internal.ini:# deviceData = 
peers/scheduleworld/.internal.ini:# webDAVCredentialsOkay = 0
peers/scheduleworld/config.ini:syncURL = http://sync.scheduleworld.com/funambol/ds
peers/scheduleworld/config.ini:# username = 
peers/scheduleworld/config.ini:# password = 
.internal.ini:contextMinVersion = {2}
.internal.ini:contextCurVersion = {3}
config.ini:# logdir = 
peers/scheduleworld/config.ini:# loglevel = 0
peers/scheduleworld/config.ini:# notifyLevel = 3
peers/scheduleworld/config.ini:# printChanges = 1
peers/scheduleworld/config.ini:# dumpData = 1
config.ini:# maxlogdirs = 10
peers/scheduleworld/config.ini:# autoSync = 0
peers/scheduleworld/config.ini:# autoSyncInterval = 30M
peers/scheduleworld/config.ini:# autoSyncDelay = 5M
peers/scheduleworld/config.ini:# preventSlowSync = 1
peers/scheduleworld/config.ini:# useProxy = 0
peers/scheduleworld/config.ini:# proxyHost = 
peers/scheduleworld/config.ini:# proxyUsername = 
peers/scheduleworld/config.ini:# proxyPassword = 
peers/scheduleworld/config.ini:# clientAuthType = md5
peers/scheduleworld/config.ini:# RetryDuration = 5M
peers/scheduleworld/config.ini:# RetryInterval = 2M
peers/scheduleworld/config.ini:# remoteIdentifier = 
peers/scheduleworld/config.ini:# PeerIsClient = 0
peers/scheduleworld/config.ini:# SyncMLVersion = 
peers/scheduleworld/config.ini:PeerName = ScheduleWorld
config.ini:deviceId = fixed-devid
peers/scheduleworld/config.ini:# remoteDeviceId = 
peers/scheduleworld/config.ini:# enableWBXML = 1
peers/scheduleworld/config.ini:# enableRefreshSync = 0
peers/scheduleworld/config.ini:# maxMsgSize = 150000
peers/scheduleworld/config.ini:# maxObjSize = 4000000
peers/scheduleworld/config.ini:# SSLServerCertificates = {4}
peers/scheduleworld/config.ini:# SSLVerifyServer = 1
peers/scheduleworld/config.ini:# SSLVerifyHost = 1
peers/scheduleworld/config.ini:WebURL = http://www.scheduleworld.com
peers/scheduleworld/config.ini:IconURI = image://themedimage/icons/services/scheduleworld
peers/scheduleworld/config.ini:# ConsumerReady = 0
peers/scheduleworld/config.ini:# peerType = 
peers/scheduleworld/sources/addressbook/.internal.ini:# adminData = 
peers/scheduleworld/sources/addressbook/.internal.ini:# synthesisID = 0
peers/scheduleworld/sources/addressbook/config.ini:sync = two-way
peers/scheduleworld/sources/addressbook/config.ini:uri = card3
sources/addressbook/config.ini:backend = addressbook
peers/scheduleworld/sources/addressbook/config.ini:syncFormat = text/vcard
peers/scheduleworld/sources/addressbook/config.ini:# forceSyncFormat = 0
sources/addressbook/config.ini:# database = 
sources/addressbook/config.ini:# databaseFormat = 
sources/addressbook/config.ini:# databaseUser = 
sources/addressbook/config.ini:# databasePassword = 
peers/scheduleworld/sources/calendar/.internal.ini:# adminData = 
peers/scheduleworld/sources/calendar/.internal.ini:# synthesisID = 0
peers/scheduleworld/sources/calendar/config.ini:sync = two-way
peers/scheduleworld/sources/calendar/config.ini:uri = cal2
sources/calendar/config.ini:backend = calendar
peers/scheduleworld/sources/calendar/config.ini:# syncFormat = 
peers/scheduleworld/sources/calendar/config.ini:# forceSyncFormat = 0
sources/calendar/config.ini:# database = 
sources/calendar/config.ini:# databaseFormat = 
sources/calendar/config.ini:# databaseUser = 
sources/calendar/config.ini:# databasePassword = 
peers/scheduleworld/sources/memo/.internal.ini:# adminData = 
peers/scheduleworld/sources/memo/.internal.ini:# synthesisID = 0
peers/scheduleworld/sources/memo/config.ini:sync = two-way
peers/scheduleworld/sources/memo/config.ini:uri = note
sources/memo/config.ini:backend = memo
peers/scheduleworld/sources/memo/config.ini:# syncFormat = 
peers/scheduleworld/sources/memo/config.ini:# forceSyncFormat = 0
sources/memo/config.ini:# database = 
sources/memo/config.ini:# databaseFormat = 
sources/memo/config.ini:# databaseUser = 
sources/memo/config.ini:# databasePassword = 
peers/scheduleworld/sources/todo/.internal.ini:# adminData = 
peers/scheduleworld/sources/todo/.internal.ini:# synthesisID = 0
peers/scheduleworld/sources/todo/config.ini:sync = two-way
peers/scheduleworld/sources/todo/config.ini:uri = task2
sources/todo/config.ini:backend = todo
peers/scheduleworld/sources/todo/config.ini:# syncFormat = 
peers/scheduleworld/sources/todo/config.ini:# forceSyncFormat = 0
sources/todo/config.ini:# database = 
sources/todo/config.ini:# databaseFormat = 
sources/todo/config.ini:# databaseUser = 
sources/todo/config.ini:# databasePassword = '''.format(
           peerMinVersion, peerCurVersion,
           contextMinVersion, contextCurVersion,
           self.getSSLServerCertificates())

    def DefaultConfig(self):
        config = self.ScheduleWorldConfig()
        config = config.replace("syncURL = http://sync.scheduleworld.com/funambol/ds",
                                "syncURL = http://yourserver:port",
                                1)
        config = config.replace("http://www.scheduleworld.com",
                                "http://www.syncevolution.org",
                                1)
        config = config.replace("ScheduleWorld",
                                "SyncEvolution")
        config = config.replace("scheduleworld",
                                "syncevolution")
        config = config.replace("PeerName = SyncEvolution",
                                "# PeerName = ",
                                1)
        config = config.replace("# ConsumerReady = 0",
                                "ConsumerReady = 1",
                                1)
        config = config.replace("uri = card3",
                                "uri = addressbook",
                                1)
        config = config.replace("uri = cal2",
                                "uri = calendar",
                                1)
        config = config.replace("uri = task2",
                                "uri = todo",
                                1)
        config = config.replace("uri = note",
                                "uri = memo",
                                1)
        config = config.replace("syncFormat = text/vcard",
                                "# syncFormat = ",
                                1)
        return config

    def FunambolConfig(self):
        config = self.ScheduleWorldConfig()
        config = config.replace("/scheduleworld/",
                                "/funambol/")
        config = config.replace("PeerName = ScheduleWorld",
                                "PeerName = Funambol")
        config = config.replace("syncURL = http://sync.scheduleworld.com/funambol/ds",
                                "syncURL = http://my.funambol.com/sync",
                                1)
        config = config.replace("WebURL = http://www.scheduleworld.com",
                                "WebURL = http://my.funambol.com",
                                1)
        config = config.replace("IconURI = image://themedimage/icons/services/scheduleworld",
                                "IconURI = image://themedimage/icons/services/funambol",
                                1)
        config = config.replace("# ConsumerReady = 0",
                                "ConsumerReady = 1",
                                1)
        config = config.replace("# enableWBXML = 1",
                                "enableWBXML = 0",
                                1)
        config = config.replace("# enableRefreshSync = 0",
                                "enableRefreshSync = 1",
                                1)
        config = config.replace("# RetryInterval = 2M",
                                "RetryInterval = 0",
                                1)
        config = config.replace("addressbook/config.ini:uri = card3",
                                "addressbook/config.ini:uri = card",
                                1)
        config = config.replace("addressbook/config.ini:syncFormat = text/vcard",
                                "addressbook/config.ini:# syncFormat = ")
        config = config.replace("calendar/config.ini:uri = cal2",
                                "calendar/config.ini:uri = event",
                                1)
        config = config.replace("calendar/config.ini:# syncFormat = ",
                                "calendar/config.ini:syncFormat = text/calendar")
        config = config.replace("calendar/config.ini:# forceSyncFormat = 0",
                                "calendar/config.ini:forceSyncFormat = 1")
        config = config.replace("todo/config.ini:uri = task2",
                                "todo/config.ini:uri = task",
                                1)
        config = config.replace("todo/config.ini:# syncFormat = ",
                                "todo/config.ini:syncFormat = text/calendar")
        config = config.replace("todo/config.ini:# forceSyncFormat = 0",
                                "todo/config.ini:forceSyncFormat = 1")
        return config

    def SynthesisConfig(self):
        config = self.ScheduleWorldConfig()
        config = config.replace("/scheduleworld/",
                                "/synthesis/")
        config = config.replace("PeerName = ScheduleWorld",
                                "PeerName = Synthesis")
        config = config.replace("syncURL = http://sync.scheduleworld.com/funambol/ds",
                                "syncURL = http://www.synthesis.ch/sync",
                                1)
        config = config.replace("WebURL = http://www.scheduleworld.com",
                                "WebURL = http://www.synthesis.ch",
                                1)
        config = config.replace("IconURI = image://themedimage/icons/services/scheduleworld",
                                "IconURI = image://themedimage/icons/services/synthesis",
                                1)
        config = config.replace("addressbook/config.ini:uri = card3",
                                "addressbook/config.ini:uri = contacts",
                                1)
        config = config.replace("addressbook/config.ini:syncFormat = text/vcard",
                                "addressbook/config.ini:# syncFormat = ")
        config = config.replace("calendar/config.ini:uri = cal2",
                                "calendar/config.ini:uri = events",
                                1)
        config = config.replace("calendar/config.ini:sync = two-way",
                                "calendar/config.ini:sync = disabled",
                                1)
        config = config.replace("memo/config.ini:uri = note",
                                "memo/config.ini:uri = notes",
                                1)
        config = config.replace("todo/config.ini:uri = task2",
                                "todo/config.ini:uri = tasks",
                                1)
        config = config.replace("todo/config.ini:sync = two-way",
                                "todo/config.ini:sync = disabled",
                                1)
        return config

    def OldScheduleWorldConfig(self):
        return '''spds/syncml/config.txt:syncURL = http://sync.scheduleworld.com/funambol/ds
spds/syncml/config.txt:# username = 
spds/syncml/config.txt:# password = 
spds/syncml/config.txt:# logdir = 
spds/syncml/config.txt:# loglevel = 0
spds/syncml/config.txt:# notifyLevel = 3
spds/syncml/config.txt:# printChanges = 1
spds/syncml/config.txt:# dumpData = 1
spds/syncml/config.txt:# maxlogdirs = 10
spds/syncml/config.txt:# autoSync = 0
spds/syncml/config.txt:# autoSyncInterval = 30M
spds/syncml/config.txt:# autoSyncDelay = 5M
spds/syncml/config.txt:# preventSlowSync = 1
spds/syncml/config.txt:# useProxy = 0
spds/syncml/config.txt:# proxyHost = 
spds/syncml/config.txt:# proxyUsername = 
spds/syncml/config.txt:# proxyPassword = 
spds/syncml/config.txt:# clientAuthType = md5
spds/syncml/config.txt:# RetryDuration = 5M
spds/syncml/config.txt:# RetryInterval = 2M
spds/syncml/config.txt:# remoteIdentifier = 
spds/syncml/config.txt:# PeerIsClient = 0
spds/syncml/config.txt:# SyncMLVersion = 
spds/syncml/config.txt:PeerName = ScheduleWorld
spds/syncml/config.txt:deviceId = fixed-devid
spds/syncml/config.txt:# remoteDeviceId = 
spds/syncml/config.txt:# enableWBXML = 1
spds/syncml/config.txt:# enableRefreshSync = 0
spds/syncml/config.txt:# maxMsgSize = 150000
spds/syncml/config.txt:# maxObjSize = 4000000
spds/syncml/config.txt:# SSLServerCertificates = {0}
spds/syncml/config.txt:# SSLVerifyServer = 1
spds/syncml/config.txt:# SSLVerifyHost = 1
spds/syncml/config.txt:WebURL = http://www.scheduleworld.com
spds/syncml/config.txt:IconURI = image://themedimage/icons/services/scheduleworld
spds/syncml/config.txt:# ConsumerReady = 0
spds/sources/addressbook/config.txt:sync = two-way
spds/sources/addressbook/config.txt:type = addressbook:text/vcard
spds/sources/addressbook/config.txt:evolutionsource = xyz
spds/sources/addressbook/config.txt:uri = card3
spds/sources/addressbook/config.txt:evolutionuser = foo
spds/sources/addressbook/config.txt:evolutionpassword = bar
spds/sources/calendar/config.txt:sync = two-way
spds/sources/calendar/config.txt:type = calendar
spds/sources/calendar/config.txt:# database = 
spds/sources/calendar/config.txt:uri = cal2
spds/sources/calendar/config.txt:# evolutionuser = 
spds/sources/calendar/config.txt:# evolutionpassword = 
spds/sources/memo/config.txt:sync = two-way
spds/sources/memo/config.txt:type = memo
spds/sources/memo/config.txt:# database = 
spds/sources/memo/config.txt:uri = note
spds/sources/memo/config.txt:# evolutionuser = 
spds/sources/memo/config.txt:# evolutionpassword = 
spds/sources/todo/config.txt:sync = two-way
spds/sources/todo/config.txt:type = todo
spds/sources/todo/config.txt:# database = 
spds/sources/todo/config.txt:uri = task2
spds/sources/todo/config.txt:# evolutionuser = 
spds/sources/todo/config.txt:# evolutionpassword = 
'''.format(self.getSSLServerCertificates())

    def replaceLineInConfig(self, config, begin, to):
        index = config.find(begin)
        self.assertNotEqual(index, -1)
        newline = config.find("\n", index + len(begin))
        self.assertNotEqual(newline, -1)
        return config[:index] + to + config[newline:]

    def removeRandomUUID(self, config):
        return self.replaceLineInConfig(config,
                                        "deviceId = syncevolution-",
                                        "deviceId = fixed-devid")

    def expectUsageError(self, out, err, specific_error):
        '''verify a short usage info was produced and specific error
        message was printed'''
        self.assertTrue(out.startswith("List and manipulate databases:\n"))
        self.assertEqual(out.find("\nOptions:\n"), -1)
        self.assertTrue(out.endswith("Remove item(s):\n" \
                                     "  syncevolution --delete-items [--] <config> <source> (<luid> ... | '*')\n\n"))
        self.assertEqualDiff(specific_error, stripOutput(err))

    @property('debug', False)
    def testFramework(self):
        """TestCmdline.testFramework - tests whether utility functions work"""
        content = "baz:line\n" \
                  "caz/subdir:booh\n" \
                  "caz/subdir2/sub:# comment\n" \
                  "caz/subdir2/sub:# foo = bar\n" \
                  "caz/subdir2/sub:# empty = \n" \
                  "caz/subdir2/sub:# another comment\n" \
                  "foo:bar1\n" \
                  "foo:\n" \
                  "foo: \n" \
                  "foo:bar2\n"

        filtered = "baz:line\n" \
                   "caz/subdir:booh\n" \
                   "caz/subdir2/sub:# foo = bar\n" \
                   "caz/subdir2/sub:# empty = \n" \
                   "foo:bar1\n" \
                   "foo: \n" \
                   "foo:bar2\n"

        createFiles(self.configdir, content)
        res = scanFiles(self.configdir)
        self.assertEqualDiff(filtered, res)
        randomUUID = "deviceId = syncevolution-blabla\n"
        fixedUUID = "deviceId = fixed-devid\n"
        res = self.removeRandomUUID(randomUUID)
        self.assertEqual(fixedUUID, res)

        unsorted = "f:g\n" \
                   "f:j\n" \
                   "a:b\n" \
                   "f:a\n" \
                   "a/b:a\n"
        expected = "a:b\n" \
                   "a/b:a\n" \
                   "f:g\n" \
                   "f:j\n" \
                   "f:a\n"
        res = sortConfig(unsorted)
        self.assertEqualDiff(expected, res)

        # test DBusUtil.assertEqualDiff()
        try:
            self.assertEqualDiff('foo\nbar\n', 'foo\nxxx\nbar\n')
        except AssertionError, ex:
            expected = '''differences between expected and actual text

  foo
+ xxx
  bar
'''
            self.assertTrue(str(ex).endswith(expected), 'actual exception differs\n' + str(ex))
        else:
            self.fail('''DBusUtil.assertEqualDiff() did not detect diff''')

        self.assertEqualDiff('foo\nbar', [ 'foo\n', 'bar' ])
        self.assertEqualDiff([ 'foo\n', 'bar' ], 'foo\nbar')
        self.assertEqualDiff([ 'foo\n', 'bar' ], [ 'foo\n', 'bar' ])

        # test our own regex match
        self.assertRegexpMatchesCustom('foo\nbar\nend', 'bar')
        self.assertRegexpMatchesCustom('foo\nbar\nend', 'b.r')
        self.assertRegexpMatchesCustom('foo\nbar\nend', re.compile('^b.r$', re.MULTILINE))
        try:
            self.assertRegexpMatchesCustom('foo\nbar\nend', 'xxx')
        except AssertionError, ex:
            expected = '''text does not match regex\n\nText:\nfoo\nbar\nend\n\nRegex:\nxxx'''
            self.assertTrue(str(ex).endswith(expected), 'actual exception differs\n' + str(ex))
        else:
            self.fail('''DBusUtil.assertRegexpMatchesCustom() did not fail''')
        self.assertRegexpMatches('foo\nbar\nend', 'bar')

        haystack = {'in': None}

        self.assertInCustom('in', 'inside')
        self.assertNotInCustom('in', 'outside')
        self.assertInCustom('in', haystack)
        self.assertNotIn('out', haystack)

        try:
            self.assertInCustom('in', 'outside')
        except AssertionError, ex:
            expected = "'in' not found in 'outside'"
            self.assertEqual(expected, str(ex))
        else:
            self.fail('''DBusUtil.assertInCustom() did not fail''')

        try:
            self.assertInCustom('out', haystack)
        except AssertionError, ex:
            expected = "'out' not found in '{'in': None}'"
            self.assertEqual(expected, str(ex))
        else:
            self.fail('''DBusUtil.assertInCustom() did not fail''')

        try:
            self.assertNotInCustom('in', 'inside')
        except AssertionError, ex:
            expected = "'in' found in 'inside'"
            self.assertEqual(expected, str(ex))
        else:
            self.fail('''DBusUtil.assertNotInCustom() did not fail''')

        try:
            self.assertNotInCustom('in', haystack)
        except AssertionError, ex:
            expected = "'in' found in '{'in': None}'"
            self.assertEqual(expected, str(ex))
        else:
            self.fail('''DBusUtil.assertNotInCustom() did not fail''')

        lines = "a\nb\nc\n"
        lastline = "c\n"
        res = lastLine(lines)
        self.assertEqualDiff(lastline, res)

        message = "[ERROR 12:34:56] msg\n"
        stripped = "[ERROR] msg\n"
        res = stripOutput(message)
        self.assertEqualDiff(stripped, res)

        # Run command without talking to server, separate streams.
        out, err, code = self.runCmdline(['--foo-bar'],
                                         sessionFlags=None,
                                         expectSuccess=False)
        self.assertEqualDiff('[ERROR] --foo-bar: unknown parameter\n', stripOutput(err))
        self.assertRegexpMatches(out, '^List and manipulate databases:\n')
        self.assertEqual(1, code)

        # Run command without talking to server, joined streams.
        out, err, code = self.runCmdline(['--foo-bar'],
                                         sessionFlags=None,
                                         expectSuccess=False,
                                         preserveOutputOrder=True)
        self.assertEqual(err, None)
        self.assertRegexpMatches(stripOutput(out), r'^List and manipulate databases:\n(.*\n)*\[ERROR\] --foo-bar: unknown parameter\n$')
        self.assertEqual(1, code)

        peerMin = self.getPeerMinVersion()
        peerCur = self.getPeerCurVersion()
        contextMin = self.getContextMinVersion()
        contextCur = self.getContextCurVersion()
        rootMin = self.getRootMinVersion()
        rootCur = self.getRootCurVersion()

        self.assertRegexpMatches(peerMin, r'^\d+$', 'Peer min version is not a number.')
        self.assertRegexpMatches(peerCur, r'^\d+$', 'Peer cur version is not a number.')
        self.assertRegexpMatches(contextMin, r'^\d+$', 'Context min version is not a number.')
        self.assertRegexpMatches(contextCur, r'^\d+$', 'Context cur version is not a number.')
        self.assertRegexpMatches(rootMin, r'^\d+$', 'Peer min version is not a number.')
        self.assertRegexpMatches(rootCur, r'^\d+$', 'Root cur version is not a number.')

    def assertSilent(self, out, err, ignore=None):
        if err != None and \
                os.environ.get('SYNCEVOLUTION_DEBUG', None) == None:
            if ignore:
                err = err.replace(ignore, "", 1)
            self.assertNoErrors(err)
        self.assertEqualDiff('', out)

    def doSetupScheduleWorld(self, shared):
        root = self.configdir + "/default"
        peer = ""

        if shared:
            peer = root + "/peers/scheduleworld"
        else:
            peer = root

        shutil.rmtree(peer, True)
        out, err, code = self.runCmdline(['--configure',
                                          '--sync-property', 'proxyHost = proxy',
                                          'scheduleworld', 'addressbook'])
        self.assertSilent(out, err, ignore=self.sourceCheckOutput('addressbook'))
        res = sortConfig(scanFiles(root))
        res = self.removeRandomUUID(res)
        expected = self.ScheduleWorldConfig()
        expected = sortConfig(expected)
        expected = expected.replace("# proxyHost = ",
                                    "proxyHost = proxy",
                                    1)
        expected = expected.replace("sync = two-way",
                                    "sync = disabled")
        expected = expected.replace("addressbook/config.ini:sync = disabled",
                                    "addressbook/config.ini:sync = two-way",
                                    1)
        self.assertEqualDiff(expected, res)

        shutil.rmtree(peer, True)
        out, err, code = self.runCmdline(['--configure',
                                          '--sync-property', 'deviceId = fixed-devid',
                                          'scheduleworld'])
        self.assertSilent(out, err, ignore=self.sourceCheckOutput())
        res = sortConfig(scanFiles(root))
        expected = self.ScheduleWorldConfig()
        expected = sortConfig(expected)
        self.assertEqualDiff(expected, res)

    @property('debug', False)
    def testSetupScheduleWorld(self):
        """TestCmdline.testSetupScheduleWorld - configure ScheduleWorld"""
        self.doSetupScheduleWorld(False)

    @property("debug", False)
    def testSetupDefault(self):
        """TestCmdline.testSetupDefault - configure Default"""
        root = self.configdir + '/default'
        out, err, code = self.runCmdline(['--configure',
                                          '--template', 'default',
                                          '--sync-property', 'deviceId = fixed-devid',
                                          'some-other-server'])
        self.assertSilent(out, err, ignore=self.sourceCheckOutput())
        res = scanFiles(root, 'some-other-server')
        expected = sortConfig(self.DefaultConfig()).replace('/syncevolution/', '/some-other-server/')
        self.assertEqualDiff(expected, res)

    @property("debug", False)
    def testSetupRenamed(self):
        """TestCmdline.testSetupRenamed - configure ScheduleWorld with other name"""
        root = self.configdir + "/default"
        args = ["--configure",
                "--template",
                "scheduleworld",
                "--sync-property",
                "deviceId = fixed-devid",
                "scheduleworld2"]
        out, err, code = self.runCmdline(["--configure",
                                          "--template", "scheduleworld",
                                          "--sync-property", "deviceId = fixed-devid",
                                          "scheduleworld2"])
        self.assertSilent(out, err, ignore=self.sourceCheckOutput())
        res = scanFiles(root, "scheduleworld2")
        expected = sortConfig(self.ScheduleWorldConfig()).replace("/scheduleworld/", "/scheduleworld2/")
        self.assertEqualDiff(expected, res)

    def doSetupFunambol(self, shared):
        root = self.configdir + "/default"
        peer = ""
        args = ["--configure"]

        if shared:
            peer = root + "/peers/funambol"
        else:
            peer = root
            args.extend(["--sync-property", "deviceId = fixed-devid"])
        shutil.rmtree(peer, True)

        args.append("FunamBOL")
        out, err, code = self.runCmdline(args)
        self.assertSilent(out, err, ignore=self.sourceCheckOutput())
        res = scanFiles(root, "funambol")
        expected = sortConfig(self.FunambolConfig())
        self.assertEqualDiff(expected, res)

    @property("debug", False)
    def testSetupFunambol(self):
        """TestCmdline.testSetupFunambol - configure Funambol"""
        self.doSetupFunambol(False)

    def doSetupSynthesis(self, shared):
        root = self.configdir + "/default"
        peer = ""
        args = ["--configure"]

        if shared:
            peer = root + "/peers/synthesis"
        else:
            peer = root
            args.extend(["--sync-property", "deviceId = fixed-devid"])
        shutil.rmtree(peer, True)

        args.append("synthesis")
        out, err, code = self.runCmdline(args)
        self.assertSilent(out, err, ignore=self.sourceCheckOutput())
        res = scanFiles(root, "synthesis")
        expected = sortConfig(self.SynthesisConfig())
        self.assertEqualDiff(expected, res)

    @property("debug", False)
    def testSetupSynthesis(self):
        """TestCmdline.testSetupSynthesis - configure Synthesis"""
        self.doSetupSynthesis(False)

    @property("debug", False)
    def testTemplate(self):
        """TestCmdline.testTemplate - check --template parameter"""
        out, err, code = self.runCmdline(['--template'],
                                         sessionFlags=None,
                                         expectSuccess = False)
        self.expectUsageError(out, err,
                              "[ERROR] missing parameter for '--template'\n")

        expected = "Available configuration templates (servers):\n" \
                   "   template name = template description\n" \
                   "   eGroupware = http://www.egroupware.org\n" \
                   "   Funambol = http://my.funambol.com\n" \
                   "   Google_Calendar = event sync via CalDAV, use for the 'target-config@google-calendar' config\n" \
                   "   Google_Contacts = contact sync via SyncML, see http://www.google.com/support/mobile/bin/topic.py?topic=22181\n" \
                   "   Goosync = http://www.goosync.com/\n" \
                   "   Memotoo = http://www.memotoo.com\n" \
                   "   Mobical = https://www.everdroid.com\n" \
                   "   Oracle = http://www.oracle.com/technology/products/beehive/index.html\n" \
                   "   Ovi = http://www.ovi.com\n" \
                   "   ScheduleWorld = server no longer in operation\n" \
                   "   SyncEvolution = http://www.syncevolution.org\n" \
                   "   Synthesis = http://www.synthesis.ch\n" \
                   "   WebDAV = contact and event sync using WebDAV, use for the 'target-config@<server>' config\n" \
                   "   Yahoo = contact and event sync using WebDAV, use for the 'target-config@yahoo' config\n"
        out, err, code = self.runCmdline(["--template", "? "])
        self.assertEqualDiff(expected, out)
        self.assertNoErrors(err)

    @property("ENV", "SYNCEVOLUTION_TEMPLATE_DIR=" + xdg_root + "/templates")
    @property("debug", False)
    def testMatchTemplate(self):
        """TestCmdline.testMatchTemplate - test template matching"""
        env = copy.deepcopy(self.storedenv)
        env["XDG_CONFIG_HOME"] = "/dev/null"
        self.setUpFiles('templates')
        out, err, code = self.runCmdline(["--template", "?nokia 7210c"], env)
        expected = "Available configuration templates (clients):\n" \
                   "   template name = template description    matching score in percent (100% = exact match)\n" \
                   "   Nokia_7210c = Template for Nokia S40 series Phone    100%\n" \
                   "   SyncEvolution_Client = SyncEvolution server side template    40%\n"
        self.assertEqualDiff(expected, out)
        self.assertNoErrors(err)

        out, err, code = self.runCmdline(["--template", "?nokia"], env)
        self.assertEqualDiff(expected, out)
        self.assertNoErrors(err)

        out, err, code = self.runCmdline(["--template", "?7210c"], env)
        expected = "Available configuration templates (clients):\n" \
                   "   template name = template description    matching score in percent (100% = exact match)\n" \
                   "   Nokia_7210c = Template for Nokia S40 series Phone    60%\n" \
                   "   SyncEvolution_Client = SyncEvolution server side template    20%\n"
        self.assertEqualDiff(expected, out)
        self.assertNoErrors(err)

        out, err, code = self.runCmdline(["--template", "?syncevolution client"], env)
        expected = "Available configuration templates (clients):\n" \
                   "   template name = template description    matching score in percent (100% = exact match)\n" \
                   "   SyncEvolution_Client = SyncEvolution server side template    100%\n" \
                   "   Nokia_7210c = Template for Nokia S40 series Phone    40%\n"
        self.assertEqualDiff(expected, out)
        self.assertNoErrors(err)

    @property("debug", False)
    def testPrintServers(self):
        """TestCmdline.testPrintServers - print correct servers"""

        self.doSetupScheduleWorld(False)
        self.doSetupSynthesis(True)
        self.doSetupFunambol(True)

        out, err, code = self.runCmdline(["--print-servers"])
        expected = "Configured servers:\n" \
                   "   funambol = " + os.path.abspath(self.configdir) + "/default/peers/funambol\n" \
                   "   scheduleworld = " + os.path.abspath(self.configdir) + "/default/peers/scheduleworld\n" \
                   "   synthesis = " + os.path.abspath(self.configdir) + "/default/peers/synthesis\n"
        self.assertEqualDiff(expected, out)
        self.assertNoErrors(err)

    @property("debug", False)
    def testPrintConfig(self):
        """TestCmdline.testPrintConfig - print various configurations"""
        self.doSetupFunambol(False)

        out, err, code = self.runCmdline(["--print-config"],
                                         expectSuccess = False)
        self.expectUsageError(out, err,
                              "[ERROR] --print-config requires either a --template or a server name.\n")

        out, err, code = self.runCmdline(["--print-config", "foo"],
                                         expectSuccess = False)
        self.assertEqualDiff('', out)
        self.assertEqualDiff("[ERROR] Server 'foo' has not been configured yet.\n",
                             stripOutput(err))

        out, err, code = self.runCmdline(["--print-config", "--template", "foo"],
                                         expectSuccess = False)
        self.assertEqualDiff("", out)
        self.assertEqualDiff("[ERROR] No configuration template for 'foo' available.\n",
                             stripOutput(err))

        out, err, code = self.runCmdline(["--print-config", "--template", "scheduleworld"])
        self.assertEqualDiff("", err)
        # deviceId must be the one from Funambol
        self.assertIn("deviceId = fixed-devid", out)
        filtered = injectValues(filterConfig(out))
        self.assertEqualDiff(filterConfig(internalToIni(self.ScheduleWorldConfig())),
                             filtered)
        # there should have been comments
        self.assertTrue(len(out) > len(filtered))

        out, err, code = self.runCmdline(["--print-config", "--template", "scheduleworld@nosuchcontext"])
        self.assertEqualDiff("", err)
        # deviceId must *not* be the one from Funambol because of the new context
        self.assertNotIn("deviceId = fixed-devid", out)

        out, err, code = self.runCmdline(["--print-config", "--template", "Default"])
        self.assertEqualDiff("", err)
        actual = injectValues(filterConfig(out))
        self.assertIn("deviceId = fixed-devid", actual)
        self.assertEqualDiff(filterConfig(internalToIni(self.DefaultConfig())),
                             actual)

        out, err, code = self.runCmdline(["--print-config", "funambol"])
        self.assertEqualDiff("", err)
        self.assertEqualDiff(filterConfig(internalToIni(self.FunambolConfig())),
                             injectValues(filterConfig(out)))

        # override context and template properties
        out, err, code = self.runCmdline(["--print-config", "--template", "scheduleworld",
                                          "syncURL=foo",
                                          "database=Personal",
                                          "--source-property", "sync=disabled"])
        self.assertNoErrors(err)
        expected = filterConfig(internalToIni(self.ScheduleWorldConfig()))
        expected = expected.replace("syncURL = http://sync.scheduleworld.com/funambol/ds",
                                    "syncURL = foo",
                                    1)
        expected = expected.replace("# database = ",
                                    "database = Personal")
        expected = expected.replace("sync = two-way",
                                    "sync = disabled")
        actual = injectValues(filterConfig(out))
        self.assertIn("deviceId = fixed-devid", actual)
        self.assertEqualDiff(expected, actual)

        # override context and template properties, using legacy
        # property name
        out, err, code = self.runCmdline(["--print-config", "--template", "scheduleworld",
                                          "--sync-property", "syncURL=foo",
                                          "--source-property", "evolutionsource=Personal",
                                          "--source-property", "sync=disabled"])
        self.assertNoErrors(err)
        expected = filterConfig(internalToIni(self.ScheduleWorldConfig()))
        expected = expected.replace("syncURL = http://sync.scheduleworld.com/funambol/ds",
                                    "syncURL = foo",
                                    1)
        expected = expected.replace("# database = ",
                                    "database = Personal")
        expected = expected.replace("sync = two-way",
                                    "sync = disabled")
        actual = injectValues(filterConfig(out))
        self.assertIn("deviceId = fixed-devid", actual)
        self.assertEqualDiff(expected,
                             actual)

        out, err, code = self.runCmdline(["--print-config", "--quiet",
                                          "--template", "scheduleworld",
                                          "funambol"])
        self.assertEqualDiff("", err)
        self.assertIn("deviceId = fixed-devid", out)
        self.assertEqualDiff(internalToIni(self.ScheduleWorldConfig()),
                             injectValues(filterConfig(out)))

        # change shared source properties, then check template again
        out, err, code = self.runCmdline(["--configure",
                                          "--source-property", "database=Personal",
                                          "funambol"])
        self.assertEqualDiff("", err)

        out, err, code = self.runCmdline(["--print-config", "--quiet",
                                          "--template", "scheduleworld",
                                          "funambol"])
        self.assertEqualDiff("", err)
        # from modified Funambol config
        expected = filterConfig(internalToIni(self.ScheduleWorldConfig())).replace("# database = ",
                                                                                   "database = Personal")
        actual = injectValues(filterConfig(out))
        self.assertIn("deviceId = fixed-devid", actual)
        self.assertEqualDiff(expected, actual)

        # print config => must not use settings from default context
        out, err, code = self.runCmdline(["--print-config", "--template", "scheduleworld@nosuchcontext"])
        self.assertEqualDiff("", err)
        expected = filterConfig(internalToIni(self.ScheduleWorldConfig()))
        actual = injectValues(filterConfig(out))
        # source settings *not* from modified Funambol config
        self.assertNotIn("deviceId = fixed-devid", actual)
        actual = self.removeRandomUUID(actual)
        self.assertEqualDiff(expected, actual)

        # create config => again, must not use settings from default context
        out, err, code = self.runCmdline(["--configure", "--template", "scheduleworld", "other@other"])
        self.assertSilent(out, err, ignore=self.sourceCheckOutput())

        out, err, code = self.runCmdline(["--print-config", "other@other"])
        self.assertEqualDiff("", err)
        # source settings *not* from modified Funambol config
        expected = filterConfig(internalToIni(self.ScheduleWorldConfig()))
        actual = injectValues(filterConfig(out))
        self.assertNotIn("deviceId = fixed-devid", actual)
        actual = self.removeRandomUUID(actual)
        self.assertEqualDiff(expected, actual)

    def doPrintFileTemplates(self):
        '''Compare only the properties which are really set'''

        # note that "backend" will be taken from the @default context
        # if one exists, so run this before setting up Funambol below
        out, err, code = self.runCmdline(["--print-config", "--template", "google calendar"])
        self.assertNoErrors(err)
        self.assertEqualDiff(googlecaldav,
                             removeComments(self.removeRandomUUID(filterConfig(out))))

        out, err, code = self.runCmdline(["--print-config", "--template", "yahoo"])
        self.assertNoErrors(err)
        self.assertEqualDiff(yahoo,
                             removeComments(self.removeRandomUUID(filterConfig(out))))

        self.doSetupFunambol(False)

        out, err, code = self.runCmdline(["--print-config", "--template", "scheduleworld"])
        self.assertNoErrors(err)
        # deviceId must be the one from Funambol
        self.assertIn("deviceId = fixed-devid", out)
        filtered = injectValues(filterConfig(out))
        self.assertEqualDiff(filterConfig(internalToIni(self.ScheduleWorldConfig())),
                             filtered)
        # there should have been comments
        self.assertTrue(len(out) > len(filtered))

        out, err, code = self.runCmdline(["--print-config", "funambol"])
        self.assertNoErrors(err)
        self.assertEqualDiff(filterConfig(internalToIni(self.FunambolConfig())),
                             injectValues(filterConfig(out)))

    # Use local copy of templates in build dir (no need to install);
    # this assumes that test-dbus.py is run in the build "src"
    # directory.
    @property("ENV", "SYNCEVOLUTION_TEMPLATE_DIR=./templates")
    @property("debug", False)
    def testPrintFileTemplates(self):
        """TestCmdline.testPrintFileTemplates - print file templates"""
        self.doPrintFileTemplates()

    # Disable reading default templates, rely on finding the user
    # ones (set up below via symlink).
    @property("ENV", "SYNCEVOLUTION_TEMPLATE_DIR=/dev/null")
    @property("debug", False)
    def testPrintFileTemplatesConfig(self):
        """TestCmdline.testPrintFileTemplatesConfig - print file templates from user home directory"""
        # The user's "home directory" or more precisely, his
        # XDG_CONFIG_HOME, is set to xdg_root + "config". Make sure
        # that there is a "syncevolution-templates" with valid
        # templates in that "config" directory, because "templates"
        # will not be found otherwise (SYNCEVOLUTION_TEMPLATE_DIR
        # doesn't point to it).
        os.makedirs(xdg_root + "/config")
        # Use same "./templates" as in testPrintFileTemplates().
        os.symlink("../../templates", xdg_root + "/config/syncevolution-templates")
        self.doPrintFileTemplates()

    @property("debug", False)
    def testAddSource(self):
        '''TestCmdline.testAddSource - add a source'''
        self.doSetupScheduleWorld(False)
        out, err, code = self.runCmdline(["--configure",
                                          "--source-property", "uri = dummy",
                                          "scheduleworld",
                                          "xyz"])
        res = scanFiles(self.configdir + "/default")
        expected = sortConfig(self.ScheduleWorldConfig() + """
peers/scheduleworld/sources/xyz/.internal.ini:# adminData = 
peers/scheduleworld/sources/xyz/.internal.ini:# synthesisID = 0
peers/scheduleworld/sources/xyz/config.ini:# sync = disabled
peers/scheduleworld/sources/xyz/config.ini:uri = dummy
peers/scheduleworld/sources/xyz/config.ini:# syncFormat = 
peers/scheduleworld/sources/xyz/config.ini:# forceSyncFormat = 0
sources/xyz/config.ini:# backend = select backend
sources/xyz/config.ini:# database = 
sources/xyz/config.ini:# databaseFormat = 
sources/xyz/config.ini:# databaseUser = 
sources/xyz/config.ini:# databasePassword = """)
        self.assertEqualDiff(expected, res)

    @property("debug", False)
    def testEmptyDatabaseProp(self):
        '''TestCmdline.testEmptyDatabaseProp - add a source with explicit empty database property'''
        self.doSetupScheduleWorld(False)
        out, err, code = self.runCmdline(["--configure",
                                          "database=",
                                          "scheduleworld",
                                          "xyz"])
        res = scanFiles(self.configdir + "/default")
        expected = sortConfig(self.ScheduleWorldConfig() + """
peers/scheduleworld/sources/xyz/.internal.ini:# adminData = 
peers/scheduleworld/sources/xyz/.internal.ini:# synthesisID = 0
peers/scheduleworld/sources/xyz/config.ini:# sync = disabled
peers/scheduleworld/sources/xyz/config.ini:# uri = 
peers/scheduleworld/sources/xyz/config.ini:# syncFormat = 
peers/scheduleworld/sources/xyz/config.ini:# forceSyncFormat = 0
sources/xyz/config.ini:# backend = select backend
sources/xyz/config.ini:database = 
sources/xyz/config.ini:# databaseFormat = 
sources/xyz/config.ini:# databaseUser = 
sources/xyz/config.ini:# databasePassword = """)
        self.assertEqualDiff(expected, res)

    @property("debug", False)
    def testSync(self):
        """TestCmdline.testSync - check sync with various options"""
        out, err, code = self.runCmdline(["--sync"],
                                         sessionFlags=None,
                                         expectSuccess = False)
        self.expectUsageError(out, err,
                              "[ERROR] missing parameter for '--sync'\n")

        out, err, code = self.runCmdline(["--sync", "foo"],
                                         sessionFlags=None,
                                         expectSuccess = False)
        self.assertEqualDiff('', out)
        self.assertEqualDiff("[ERROR] '--sync foo': not one of the valid values (two-way, slow, refresh-from-local, refresh-from-remote = refresh, one-way-from-local, one-way-from-remote = one-way, refresh-from-client = refresh-client, refresh-from-server = refresh-server, one-way-from-client = one-way-client, one-way-from-server = one-way-server, local-cache-slow, local-cache-incremental = local-cache, disabled = none)\n",
                             stripOutput(err))

        out, err, code = self.runCmdline(["--sync", " ?"],
                                         sessionFlags=None)
        self.assertEqualDiff("""--sync
   Requests a certain synchronization mode when initiating a sync:
   
     two-way
       only send/receive changes since last sync
     slow
       exchange all items
     refresh-from-remote
       discard all local items and replace with
       the items on the peer
     refresh-from-local
       discard all items on the peer and replace
       with the local items
     one-way-from-remote
       transmit changes from peer
     one-way-from-local
       transmit local changes
     local-cache-slow (server only)
       mirror remote data locally, transferring all data
     local-cache-incremental (server only)
       mirror remote data locally, transferring only changes;
       falls back to local-cache-slow automatically if necessary
     disabled (or none)
       synchronization disabled
   
   refresh/one-way-from-server/client are also supported. Their use is
   discouraged because the direction of the data transfer depends
   on the role of the local side (can be server or client), which is
   not always obvious.
   
   When accepting a sync session in a SyncML server (HTTP server), only
   sources with sync != disabled are made available to the client,
   which chooses the final sync mode based on its own configuration.
   When accepting a sync session in a SyncML client (local sync with
   the server contacting SyncEvolution on a device), the sync mode
   specified in the client is typically overriden by the server.
""",
                             out)
        self.assertEqualDiff("", err)

        out, err, code = self.runCmdline(["--sync", "refresh-from-server"],
                                         sessionFlags=None,
                                         expectSuccess = False)
        self.expectUsageError(out, err,
                              "[ERROR] No configuration name specified.\n")

        out, err, code = self.runCmdline(["--source-property", "sync=refresh"],
                                         sessionFlags=None,
                                         expectSuccess = False)
        self.expectUsageError(out, err,
                              "[ERROR] No configuration name specified.\n")

        out, err, code = self.runCmdline(["--source-property", "xyz=1"],
                                         sessionFlags=None,
                                         expectSuccess = False)
        self.assertEqualDiff('', out)
        self.assertEqualDiff("[ERROR] '--source-property xyz=1': no such property\n",
                             stripOutput(err))

        out, err, code = self.runCmdline(["xyz=1"],
                                         sessionFlags=None,
                                         expectSuccess = False)
        self.expectUsageError(out, err,
                              "[ERROR] unrecognized property in 'xyz=1'\n")

        out, err, code = self.runCmdline(["=1"],
                                         sessionFlags=None,
                                         expectSuccess = False)
        self.expectUsageError(out, err,
                              "[ERROR] a property name must be given in '=1'\n")

    # TODO: scan output from "backend=?" to determine whether CalDAV/CardDAV are enabled
    def isWebDAVEnabled(self):
        '''Checks config.h for existence of '#define ENABLE_DAV' or
        '/* #undef ENABLE_DAV*/'. It assumes that the test is being
        run in $(top_builddir)/src, which is the same assumption like
        some tests already have.'''
        configfile = open('../config.h', 'r')

        for line in configfile:
            if line == '#define ENABLE_DAV 1\n':
                return True
            if line == '/* #undef ENABLE_DAV */\n':
                return False
        self.fail('Could not find out whether DAV is enabled or not.')

    @property("debug", False)
    def testWebDAV(self):
        """TestCmdline.testWebDAV - configure and print WebDAV configs"""

        # configure Yahoo under a different name, with explicit
        # template selection
        out, err, code = self.runCmdline(["--configure",
                                          "--template", "yahoo",
                                          "target-config@my-yahoo"])
        # TODO: why does it check 'addressbook'? It's disabled in the template.
        self.assertSilent(out, err, ignore=self.sourceCheckOutput(['addressbook', 'calendar']))

        out, err, code = self.runCmdline(["--print-config", "target-config@my-yahoo"])
        self.assertNoErrors(err)
        davenabled = self.isWebDAVEnabled()
        if davenabled:
            self.assertEqualDiff(yahoo,
                                 removeComments(self.removeRandomUUID(filterConfig(out))))
        else:
            self.assertEqualDiff(yahoo.replace("sync = two-way", "sync = disabled"),
                                 removeComments(self.removeRandomUUID(filterConfig(out))))

        # configure Google Calendar with template derived from config name
        out, err, code = self.runCmdline(["--configure",
                                          "target-config@google-calendar"])
        self.assertSilent(out, err, ignore=self.sourceCheckOutput('calendar'))

        out, err, code = self.runCmdline(["--print-config", "target-config@google-calendar"])
        self.assertNoErrors(err)
        if davenabled:
            self.assertEqualDiff(googlecaldav,
                                 removeComments(self.removeRandomUUID(filterConfig(out))))
        else:
            self.assertEqualDiff(googlecaldav.replace("sync = two-way", "sync = disabled"),
                                 removeComments(self.removeRandomUUID(filterConfig(out))))

        # test "template not found" error cases
        out, err, code = self.runCmdline(["--configure",
                                          "--template", "yahooxyz",
                                          "target-config@my-yahoo-xyz"],
                                         expectSuccess = False)
        error = """[ERROR] No configuration template for 'yahooxyz' available.
[INFO] 
[INFO] Available configuration templates (clients and servers):
"""
        self.assertEqualDiff('', out)
        self.assertTrue(err.startswith(error))
        self.assertTrue(err.endswith("\n"))
        self.assertFalse(err.endswith("\n\n"))

        out, err, code = self.runCmdline(["--configure",
                                          "target-config@foobar"],
                                         expectSuccess = False)
        error = """[ERROR] No configuration template for 'foobar' available.
[INFO] Use '--template none' and/or specify relevant properties on the command line to create a configuration without a template. Need values for: syncURL
[INFO] 
[INFO] Available configuration templates (clients and servers):
"""

        self.assertEqualDiff('', out)
        self.assertTrue(err.startswith(error))
        self.assertTrue(err.endswith("\n"))
        self.assertFalse(err.endswith("\n\n"))

    def printConfig(self, server):
        out, err, code = self.runCmdline(["--print-config", server])
        self.assertNoErrors(err)
        return out

    def doConfigure(self, config, prefix):
        out, err, code = self.runCmdline(["--configure",
                                          "--source-property", "sync = disabled",
                                          "scheduleworld"])
        self.assertSilent(out, err, ignore=self.sourceCheckOutput())
        expected = filterConfig(internalToIni(config)).replace("sync = two-way",
                                                               "sync = disabled")
        self.assertEqualDiff(expected,
                             filterConfig(self.printConfig("scheduleworld")))

        out, err, code = self.runCmdline(["--configure",
                                          "--source-property", "sync = one-way-from-server",
                                          "scheduleworld",
                                          "addressbook"])
        self.assertSilent(out, err)
        expected = config.replace("sync = two-way",
                                  "sync = disabled")
        expected = expected.replace(prefix + "sync = disabled",
                                    prefix + "sync = one-way-from-server",
                                    1)
        expected = filterConfig(internalToIni(expected))
        self.assertEqualDiff(expected,
                             filterConfig(self.printConfig("scheduleworld")))

        out, err, code = self.runCmdline(["--configure",
                                          "--sync", "two-way",
                                          "-z", "database=source",
                                          # note priority of suffix: most specific wins
                                          "--sync-property", "maxlogdirs@scheduleworld@default=20",
                                          "--sync-property", "maxlogdirs@default=10",
                                          "--sync-property", "maxlogdirs=5",
                                          "-y", "LOGDIR@default=logdir",
                                          "scheduleworld"])
        self.assertSilent(out, err)
        expected = expected.replace("sync = one-way-from-server",
                                    "sync = two-way")
        expected = expected.replace("sync = disabled",
                                    "sync = two-way")
        expected = expected.replace("# database = ",
                                    "database = source")
        expected = expected.replace("database = xyz",
                                    "database = source")
        expected = expected.replace("# maxlogdirs = 10",
                                    "maxlogdirs = 20")
        expected = expected.replace("# logdir = ",
                                    "logdir = logdir")
        self.assertEqualDiff(expected,
                             filterConfig(self.printConfig("scheduleworld")))
        return expected

    @property("debug", False)
    def testConfigure(self):
        """TestCmdline.testConfigure - run configures"""
        self.doSetupScheduleWorld(False)

        expected = self.doConfigure(self.ScheduleWorldConfig(), "sources/addressbook/config.ini:")

        # using "type" for peer is mapped to updating "backend",
        # "databaseFormat", "syncFormat", "forceSyncFormat"
        out, err, code = self.runCmdline(["--configure",
                                          "--source-property", "addressbook/type=file:text/vcard:3.0",
                                          "scheduleworld"])
        self.assertSilent(out, err)
        expected = expected.replace("backend = addressbook",
                                    "backend = file",
                                    1)
        expected = expected.replace("# databaseFormat = ",
                                    "databaseFormat = text/vcard",
                                    1)
        self.assertEqualDiff(expected,
                             filterConfig(self.printConfig("scheduleworld")))
        shared = filterConfig(self.printConfig("@default"))
        self.assertIn("backend = file", shared)
        self.assertIn("databaseFormat = text/vcard", shared)

        # force sync format
        out, err, code = self.runCmdline(["--configure",
                                          "--source-property", "addressbook/type=file:text/vcard:3.0!",
                                          "scheduleworld"])
        self.assertSilent(out, err)
        expected = expected.replace('# forceSyncFormat = 0',
                                    'forceSyncFormat = 1',
                                    1)
        self.assertEqualDiff(expected,
                             filterConfig(self.printConfig("scheduleworld")))

        # unset format
        out, err, code = self.runCmdline(["--configure",
                                          "--source-property", "addressbook/type=file",
                                          "scheduleworld"])
        self.assertSilent(out, err)
        expected = expected.replace('databaseFormat = text/vcard',
                                    '# databaseFormat = ',
                                    1)
        expected = expected.replace('syncFormat = text/vcard',
                                    '# syncFormat = ',
                                    1)
        expected = expected.replace('forceSyncFormat = 1',
                                    '# forceSyncFormat = 0',
                                    1)
        self.assertEqualDiff(expected,
                             filterConfig(self.printConfig("scheduleworld")))

        # updating type for context must not affect peer
        out, err, code = self.runCmdline(["--configure",
                                          "--source-property", "type=file:text/x-vcard:2.1",
                                          "@default", "addressbook"])
        self.assertSilent(out, err)
        expected = expected.replace("# databaseFormat = ",
                                    "databaseFormat = text/x-vcard",
                                    1)
        self.assertEqualDiff(expected,
                             filterConfig(self.printConfig("scheduleworld")))
        shared = filterConfig(self.printConfig("@default"))
        self.assertIn("backend = file", shared)
        self.assertIn("databaseFormat = text/x-vcard", shared)

        syncproperties = """syncURL (no default, unshared, required)

username (no default, unshared)

password (no default, unshared)

logdir (no default, shared)

loglevel (0, unshared)

notifyLevel (3, unshared)

printChanges (TRUE, unshared)

dumpData (TRUE, unshared)

maxlogdirs (10, shared)

autoSync (0, unshared)

autoSyncInterval (30M, unshared)

autoSyncDelay (5M, unshared)

preventSlowSync (TRUE, unshared)

useProxy (FALSE, unshared)

proxyHost (no default, unshared)

proxyUsername (no default, unshared)

proxyPassword (no default, unshared)

clientAuthType (md5, unshared)

RetryDuration (5M, unshared)

RetryInterval (2M, unshared)

remoteIdentifier (no default, unshared)

PeerIsClient (FALSE, unshared)

SyncMLVersion (no default, unshared)

PeerName (no default, unshared)

deviceId (no default, shared)

remoteDeviceId (no default, unshared)

enableWBXML (TRUE, unshared)

enableRefreshSync (FALSE, unshared)

maxMsgSize (150000, unshared), maxObjSize (4000000, unshared)

SSLServerCertificates ({0}, unshared)

SSLVerifyServer (TRUE, unshared)

SSLVerifyHost (TRUE, unshared)

WebURL (no default, unshared)

IconURI (no default, unshared)

ConsumerReady (FALSE, unshared)

peerType (no default, unshared)

defaultPeer (no default, global)

keyring (yes, global)
""".format(self.getSSLServerCertificates())

        sourceproperties = """sync (disabled, unshared, required)

uri (no default, unshared)

backend (select backend, shared)

syncFormat (no default, unshared)

forceSyncFormat (FALSE, unshared)

database = evolutionsource (no default, shared)

databaseFormat (no default, shared)

databaseUser = evolutionuser (no default, shared), databasePassword = evolutionpassword (no default, shared)
"""

        # The WORKAROUND lines remove trailing newline from expected
        # output.  This should be fixed in Cmdline, I guess. For now I
        # adapted the test to actual output to check if there are
        # other errors.

        out, err, code = self.runCmdline(["--sync-property", "?"],
                                         sessionFlags=None)
        self.assertNoErrors(err)
        # WORKAROUND-----------------------vvvvv
        self.assertEqualDiff(syncproperties[:-1],
                             filterIndented(out))

        out, err, code = self.runCmdline(["--source-property", "?"],
                                         sessionFlags=None)
        self.assertNoErrors(err)
        # WORKAROUND-------------------------vvvvv
        self.assertEqualDiff(sourceproperties[:-1],
                             filterIndented(out))

        out, err, code = self.runCmdline(["--source-property", "?",
                                          "--sync-property", "?"],
                                         sessionFlags=None)
        self.assertNoErrors(err)
        # WORKAROUND------------------------------------------vvvvv
        self.assertEqualDiff(sourceproperties + syncproperties[:-1],
                             filterIndented(out))

        out, err, code = self.runCmdline(["--sync-property", "?",
                                          "--source-property", "?"],
                                         sessionFlags=None)
        self.assertNoErrors(err)
        # WORKAROUND------------------------------------------vvvvv
        self.assertEqualDiff(syncproperties + sourceproperties[:-1],
                             filterIndented(out))

        out, err, code = self.runCmdline(["--source-property", "sync=?"],
                                         sessionFlags=None)
        self.assertNoErrors(err)
        # WORKAROUND---------------------------------------vvvvv
        self.assertEqualDiff("'--source-property sync=?'\n"[:-1],
                             filterIndented(out))

        out, err, code = self.runCmdline(["sync=?"],
                                         sessionFlags=None)
        self.assertNoErrors(err)
        # WORKAROUND---------------------vvvvv
        self.assertEqualDiff("'sync=?'\n"[:-1],
                             filterIndented(out))

        out, err, code = self.runCmdline(["syncURL=?"],
                                         sessionFlags=None)
        self.assertNoErrors(err)
        # WORKAROUND------------------------vvvvv
        self.assertEqualDiff("'syncURL=?'\n"[:-1],
                             filterIndented(out))

    # test semantic of config creation (instead of updating) with and
    # without templates. See BMC # 14805
    @property("debug", False)
    def testConfigureTemplates(self):
        """TestCmdline.testConfigureTemplates - make configuration with user provided informations"""
        shutil.rmtree(self.configdir, True)

        # catch possible typos like "sheduleworld"
        out, err, code = self.runCmdline(["--configure", "foo"],
                                         expectSuccess = False)
        error = """[ERROR] No configuration template for 'foo@default' available.
[INFO] Use '--template none' and/or specify relevant properties on the command line to create a configuration without a template. Need values for: syncURL
[INFO] 
[INFO] Available configuration templates (clients and servers):
"""
        err = stripOutput(err)
        self.assertTrue(err.startswith(error))
        self.assertTrue(err.endswith("\n"))
        self.assertFalse(err.endswith("\n\n"))
        self.assertEqualDiff('', out)

        shutil.rmtree(self.configdir, True)
        # catch possible typos like "sheduleworld" when enough
        # properties are specified to continue without a template
        out, err, code = self.runCmdline(["--configure", "syncURL=http://foo.com", "--template", "foo", "bar"],
                                         expectSuccess = False)
        error = """[ERROR] No configuration template for 'foo' available.
[INFO] All relevant properties seem to be set, omit the --template parameter to proceed.
[INFO] 
[INFO] Available configuration templates (clients and servers):
"""
        err = stripOutput(err)
        self.assertTrue(err.startswith(error))
        self.assertTrue(err.endswith("\n"))
        self.assertFalse(err.endswith("\n\n"))
        self.assertEqualDiff('', out)

        fooconfig = """syncevolution/.internal.ini:rootMinVersion = {0}
syncevolution/.internal.ini:rootCurVersion = {1}
syncevolution/default/.internal.ini:contextMinVersion = {2}
syncevolution/default/.internal.ini:contextCurVersion = {3}
syncevolution/default/config.ini:deviceId = fixed-devid
syncevolution/default/peers/foo/.internal.ini:peerMinVersion = {4}
syncevolution/default/peers/foo/.internal.ini:peerCurVersion = {5}
""".format(self.getRootMinVersion(),
           self.getRootCurVersion(),
           self.getContextMinVersion(),
           self.getContextCurVersion(),
           self.getPeerMinVersion(),
           self.getPeerCurVersion())
        syncurl = "syncevolution/default/peers/foo/config.ini:syncURL = local://@bar\n"
        configsource = """syncevolution/default/peers/foo/sources/eds_event/config.ini:sync = two-way
syncevolution/default/sources/eds_event/config.ini:backend = calendar
"""
        xdg_config = xdg_root + "/config"
        shutil.rmtree(self.configdir, True)
        # allow users to proceed if they wish: should result in no
        # sources configured
        out, err, code = self.runCmdline(["--configure", "--template", "none", "foo"])
        self.assertSilent(out, err)
        res = filterFiles(self.removeRandomUUID(scanFiles(xdg_config)))
        self.assertEqualDiff(fooconfig, res)

        shutil.rmtree(self.configdir, True)
        # allow user to proceed if they wish: should result in no
        # sources configured even if general source properties are
        # specified
        out, err, code = self.runCmdline(["--configure", "--template", "none", "backend=calendar", "foo"])
        self.assertSilent(out, err)
        res = filterFiles(self.removeRandomUUID(scanFiles(xdg_config)))
        self.assertEqualDiff(fooconfig, res)

        shutil.rmtree(self.configdir, True)
        # allow user to proceed if they wish: should result in no
        # sources configured, even if specific source properties are
        # specified
        out, err, code = self.runCmdline(["--configure", "--template", "none", "eds_event/backend=calendar", "foo"])
        self.assertSilent(out, err)
        res = filterFiles(self.removeRandomUUID(scanFiles(xdg_config)))
        self.assertEqualDiff(fooconfig, res)

        shutil.rmtree(self.configdir, True)
        # allow user to proceed if they wish and possible: here
        # eds_event is not usable
        out, err, code = self.runCmdline(["--configure", "--template", "none", "foo", "eds_event"],
                                         expectSuccess = False)
        self.assertEqualDiff('', out)
        err = stripOutput(err)
        self.assertEqualDiff(self.sourceCheckOutput('eds_event').replace('okay', 'no backend available') +
                             '[ERROR] error code from SyncEvolution fatal error (local, status 10500): eds_event: no backend available\n', err)

        shutil.rmtree(self.configdir, True)
        # allow user to proceed if they wish and possible: here
        # eds_event is not configurable
        out, err, code = self.runCmdline(["--configure", "syncURL=local://@bar", "foo", "eds_event"],
                                         expectSuccess = False)
        self.assertEqualDiff('', out)
        err = stripOutput(err)
        self.assertEqualDiff('[ERROR] error code from SyncEvolution fatal error (local, status 10500): no such source(s): eds_event\n', err)

        shutil.rmtree(self.configdir, True)
        # allow user to proceed if they wish and possible: here
        # eds_event is not configurable (wrong context)
        out, err, code = self.runCmdline(["--configure", "syncURL=local://@bar", "eds_event/backend@xyz=calendar", "foo", "eds_event"],
                                         expectSuccess = False)
        self.assertEqualDiff('', out)
        err = stripOutput(err)
        self.assertEqualDiff('[ERROR] error code from SyncEvolution fatal error (local, status 10500): no such source(s): eds_event\n', err)

        shutil.rmtree(self.configdir, True)
        # allow user to proceed if they wish: configure exactly the
        # specified sources
        out, err, code = self.runCmdline(["--configure", "--template", "none", "backend=calendar", "foo", "eds_event"])
        self.assertSilent(out, err, ignore=self.sourceCheckOutput('eds_event'))
        res = filterFiles(self.removeRandomUUID(scanFiles(xdg_config)))
        self.assertEqualDiff(fooconfig + configsource, res)

        shutil.rmtree(self.configdir, True)
        # allow user to proceed if they provide enough information:
        # should result in no sources configured
        out, err, code = self.runCmdline(["--configure", "syncURL=local://@bar", "foo"])
        self.assertSilent(out, err)
        res = filterFiles(self.removeRandomUUID(scanFiles(xdg_config)))
        self.assertEqualDiff(fooconfig + syncurl, res)

        shutil.rmtree(self.configdir, True)
        # allow user to proceed if they provide enough information:
        # source created because listed and usable
        out, err, code = self.runCmdline(["--configure", "syncURL=local://@bar", "backend=calendar", "foo", "eds_event"])
        self.assertSilent(out, err, ignore=self.sourceCheckOutput('eds_event'))
        res = filterFiles(self.removeRandomUUID(scanFiles(xdg_config)))
        self.assertEqualDiff(fooconfig + syncurl + configsource, res)

        shutil.rmtree(self.configdir, True)
        # allow user to proceed if they provide enough information:
        # source created because listed and usable
        out, err, code = self.runCmdline(["--configure", "syncURL=local://@bar", "eds_event/backend@default=calendar", "foo", "eds_event"])
        self.assertSilent(out, err, ignore=self.sourceCheckOutput('eds_event'))
        res = filterFiles(self.removeRandomUUID(scanFiles(xdg_config)))
        self.assertEqualDiff(fooconfig + syncurl + configsource, res)

    @property("debug", False)
    def testConfigureSource(self):
        '''TestCmdline.testConfigureSource - configure some sources'''
        # create from scratch with only addressbook configured
        out, err, code = self.runCmdline(["--configure",
                                          "--source-property", "database = file://tmp/test",
                                          "--source-property", "type = file:text/x-vcard",
                                          "@foobar",
                                          "addressbook"])
        self.assertSilent(out, err, ignore=self.sourceCheckOutput('addressbook'))

        root = self.configdir + "/foobar"
        res = self.removeRandomUUID(scanFiles(root))
        expected = '''.internal.ini:contextMinVersion = {0}
.internal.ini:contextCurVersion = {1}
config.ini:# logdir = 
config.ini:# maxlogdirs = 10
config.ini:deviceId = fixed-devid
sources/addressbook/config.ini:backend = file
sources/addressbook/config.ini:database = file://tmp/test
sources/addressbook/config.ini:databaseFormat = text/x-vcard
sources/addressbook/config.ini:# databaseUser = 
sources/addressbook/config.ini:# databasePassword = 
'''.format(self.getContextMinVersion(),
           self.getContextCurVersion())
        self.assertEqualDiff(expected, res)

        # add calendar
        out, err, code = self.runCmdline(["--configure",
                                          "--source-property", "database@foobar = file://tmp/test2",
                                          "--source-property", "backend = calendar",
                                          "@foobar",
                                          "calendar"])
        self.assertSilent(out, err)

        res = self.removeRandomUUID(scanFiles(root))
        expected += '''sources/calendar/config.ini:backend = calendar
sources/calendar/config.ini:database = file://tmp/test2
sources/calendar/config.ini:# databaseFormat = 
sources/calendar/config.ini:# databaseUser = 
sources/calendar/config.ini:# databasePassword = 
'''
        self.assertEqualDiff(expected, res)

        # add ScheduleWorld peer: must reuse existing backend settings
        out, err, code = self.runCmdline(["--configure",
                                          "scheduleworld@foobar"])
        self.assertSilent(out, err, ignore=self.sourceCheckOutput())

        res = self.removeRandomUUID(scanFiles(root))
        expected = self.ScheduleWorldConfig()
        expected = expected.replace("addressbook/config.ini:backend = addressbook",
                                    "addressbook/config.ini:backend = file")
        expected = expected.replace("addressbook/config.ini:# database = ",
                                    "addressbook/config.ini:database = file://tmp/test")
        expected = expected.replace("addressbook/config.ini:# databaseFormat = ",
                                    "addressbook/config.ini:databaseFormat = text/x-vcard")
        expected = expected.replace("calendar/config.ini:# database = ",
                                    "calendar/config.ini:database = file://tmp/test2")
        expected = sortConfig(expected)
        self.assertEqualDiff(expected, res)

        # disable all sources except for addressbook
        out, err, code = self.runCmdline(["--configure",
                                          "--source-property", "addressbook/sync=two-way",
                                          "--source-property", "sync=none",
                                          "scheduleworld@foobar"])
        self.assertSilent(out, err)

        res = self.removeRandomUUID(scanFiles(root))
        expected = expected.replace("sync = two-way",
                                    "sync = disabled")
        expected = expected.replace("sync = disabled",
                                    "sync = two-way",
                                    1)
        self.assertEqualDiff(expected, res)

        # override type in template while creating from scratch
        out, err, code = self.runCmdline(["--configure",
                                          "--template", "SyncEvolution",
                                          "--source-property", "addressbook/type=file:text/vcard:3.0",
                                          "--source-property", "calendar/type=file:text/calendar:2.0",
                                          "syncevo@syncevo"])
        self.assertSilent(out, err, ignore=self.sourceCheckOutput())

        syncevoroot = self.configdir + "/syncevo"
        res = scanFiles(syncevoroot + "/sources/addressbook")
        self.assertIn("backend = file\n", res)
        self.assertIn("databaseFormat = text/vcard\n", res)

        res = scanFiles(syncevoroot + "/sources/calendar")
        self.assertIn("backend = file\n", res)
        self.assertIn("databaseFormat = text/calendar\n", res)

    @property("debug", False)
    def testPrintDatabases(self):
        '''TestCmdline.testPrintDatabases - print some databases'''
        # full output
        out, err, code = self.runCmdline(["--print-databases"])
        # ignore errors about Akonadi not running
        err = re.sub(r'''\[ERROR\] KDE.*\n\[ERROR\] (listing|accessing) databases failed\n\[ERROR\] Akonadi is not running. It can be started with 'akonadictl start'\.\n''', "", err)
        self.assertNoErrors(err)
        # exact output varies, do not test

        haveEDS = False
        out, err, code = self.runCmdline(["--print-databases", "backend=evolution-contacts"])
        if "not one of the valid values" in err:
            # not enabled, only this error message expected
            self.assertEqualDiff('', out)
        else:
            # enabled, no error, one entry
            haveEDS = True
            self.assertNoErrors(err)
            self.assertTrue(out.startswith("evolution-contacts:\n"))
            entries = 0
            lines = out.splitlines()
            for line in lines:
                if line and not line.startswith(" "):
                    entries += 1
            self.assertEqual(1, entries)
        if haveEDS:
            # limit output to one specific backend, chosen via config
            out, err, code = self.runCmdline(["--configure", "backend=evolution-contacts", "@foo-config", "bar-source"])
            self.assertSilent(out, err, ignore=self.sourceCheckOutput('bar-source'))
            out, err, code = self.runCmdline(["--print-databases", "@foo-config", "bar-source"])
            self.assertNoErrors(err)
            self.assertTrue(out.startswith("@foo-config/bar-source:\n"))
            entries = 0
            lines = out.splitlines()
            for line in lines:
                if line and not line.startswith(" "):
                    entries += 1
            self.assertEqual(1, entries)

    @property("debug", False)
    def testMigrate(self):
        '''TestCmdline.testMigrate - migrate from old configuration'''
        oldroot = xdg_root + "/.sync4j/evolution/scheduleworld"
        newroot = self.configdir + "/default"
        oldconfig = self.OldScheduleWorldConfig()

        # migrate old config
        createFiles(oldroot, oldconfig)
        createdoldconfig = scanFiles(oldroot)
        out, err, code = self.runCmdline(["--migrate",
                                          "scheduleworld"])
        self.assertSilent(out, err)

        migratedconfig = scanFiles(newroot)
        expected = sortConfig(self.ScheduleWorldConfig())
        # migrating SyncEvolution < 1.2 configs sets ConsumerReady, to
        # keep config visible in the updated sync-ui
        expected = expected.replace("# ConsumerReady = 0",
                                    "ConsumerReady = 1")
        expected = expected.replace("# database = ",
                                    "database = xyz",
                                    1)
        expected = expected.replace("# databaseUser = ",
                                    "databaseUser = foo",
                                    1)
        # syncevo-dbus-server always uses keyring and doesn't
        # return plain-text password.
        expected = expected.replace("# databasePassword = ",
                                    "databasePassword = -",
                                    1)
        # migrating "type" sets forceSyncFormat (if non-standard) and
        # databaseFormat (if format was part of type, as for
        # addressbook
        expected = expected.replace("# databaseFormat = ",
                                    "databaseFormat = text/vcard",
                                    1)
        self.assertEqualDiff(expected, migratedconfig)
        renamedconfig = scanFiles(oldroot + ".old")
        self.assertEqualDiff(createdoldconfig, renamedconfig)

        # rewrite existing config with obsolete properties => these
        # properties should get removed
        #
        # There is one limitation: shared nodes are not
        # rewritten. This is acceptable.
        createFiles(newroot + "/peers/scheduleworld",
                    '''config.ini:# obsolete comment
config.ini:obsoleteprop = foo
''',
                    True)
        createdconfig = scanFiles(newroot, "scheduleworld")
        out, err, code = self.runCmdline(["--migrate",
                                          "scheduleworld"])
        self.assertSilent(out, err)

        migratedconfig = scanFiles(newroot, "scheduleworld")
        expected = sortConfig(self.ScheduleWorldConfig())
        expected = expected.replace("# ConsumerReady = 0", "ConsumerReady = 1")
        expected = expected.replace("# database = ", "database = xyz", 1)
        expected = expected.replace("# databaseUser = ", "databaseUser = foo", 1)
        # uses keyring
        expected = expected.replace("# databasePassword = ", "databasePassword = -", 1)
        expected = expected.replace("# databaseFormat = ", "databaseFormat = text/vcard", 1)
        self.assertEqualDiff(expected, migratedconfig)
        renamedconfig = scanFiles(newroot, "scheduleworld.old.1")
        createdconfig = createdconfig.replace("ConsumerReady = 1", "ConsumerReady = 0", 1)
        createdconfig = createdconfig.replace("/scheduleworld/", "/scheduleworld.old.1/")
        self.assertEqualDiff(createdconfig, renamedconfig)

        # migrate old config with changes and .synthesis directory, a
        # second time
        createFiles(oldroot, oldconfig)
        createFiles(oldroot,
                    '''.synthesis/dummy-file.bfi:dummy = foobar
spds/sources/addressbook/changes/config.txt:foo = bar
spds/sources/addressbook/changes/config.txt:foo2 = bar2
''',
                    True)
        createdconfig = scanFiles(oldroot)
        shutil.rmtree(newroot, True)
        out, err, code = self.runCmdline(["--migrate",
                                          "scheduleworld"])
        self.assertSilent(out, err)

        migratedconfig = scanFiles(newroot)
        expected = sortConfig(self.ScheduleWorldConfig())
        expected = expected.replace("# ConsumerReady = 0",
                                    "ConsumerReady = 1")
        expected = expected.replace("# database = ",
                                    "database = xyz",
                                    1)
        expected = expected.replace("# databaseUser = ",
                                    "databaseUser = foo",
                                    1)
        # uses keyring
        expected = expected.replace("# databasePassword = ",
                                    "databasePassword = -",
                                    1)
        expected = expected.replace("# databaseFormat = ",
                                    "databaseFormat = text/vcard",
                                    1)
        expected = expected.replace("peers/scheduleworld/sources/addressbook/config.ini",
                                    '''peers/scheduleworld/sources/addressbook/.other.ini:foo = bar
peers/scheduleworld/sources/addressbook/.other.ini:foo2 = bar2
peers/scheduleworld/sources/addressbook/config.ini''',
                                    1)
        expected = expected.replace("peers/scheduleworld/config.ini",
                                    '''peers/scheduleworld/.synthesis/dummy-file.bfi:dummy = foobar
peers/scheduleworld/config.ini''',
                                    1)
        self.assertEqualDiff(expected, migratedconfig)
        renamedconfig = scanFiles(oldroot + ".old.1")
        createdconfig = createdconfig.replace("ConsumerReady = 1",
                                              "ConsumerReady = 0",
                                              1)
        self.assertEqualDiff(createdconfig, renamedconfig)

        otherroot = self.configdir + "/other"
        shutil.rmtree(otherroot, True)

        # migrate old config into non-default context
        createFiles(oldroot, oldconfig)
        createdconfig = scanFiles(oldroot)

        out, err, code = self.runCmdline(["--migrate",
                                          "scheduleworld@other"])
        self.assertSilent(out, err)

        migratedconfig = scanFiles(otherroot)
        expected = sortConfig(self.ScheduleWorldConfig())
        expected = expected.replace("# ConsumerReady = 0",
                                    "ConsumerReady = 1")
        expected = expected.replace("# database = ",
                                    "database = xyz",
                                    1)
        expected = expected.replace("# databaseUser = ",
                                    "databaseUser = foo",
                                    1)
        # uses keyring
        expected = expected.replace("# databasePassword = ",
                                    "databasePassword = -",
                                    1)
        expected = expected.replace("# databaseFormat = ",
                                    "databaseFormat = text/vcard",
                                    1)
        self.assertEqualDiff(expected, migratedconfig)
        renamedconfig = scanFiles(oldroot + ".old")
        self.assertEqualDiff(createdconfig, renamedconfig)

        # migrate the migrated config again inside the "other"
        # context, with no "default" context which might interfere
        # with the tests

        # ConsumerReady was set as part of previous migration, must be
        # removed during migration to hide the migrated config from
        # average users.
        shutil.rmtree(newroot, True)
        out, err, code = self.runCmdline(["--migrate",
                                          "scheduleworld@other"])
        self.assertSilent(out, err)

        migratedconfig = scanFiles(otherroot, "scheduleworld")
        expected = sortConfig(self.ScheduleWorldConfig())
        expected = expected.replace("# ConsumerReady = 0",
                                    "ConsumerReady = 1")
        expected = expected.replace("# database = ",
                                    "database = xyz",
                                    1)
        expected = expected.replace("# databaseUser = ",
                                    "databaseUser = foo",
                                    1)
        # uses keyring
        expected = expected.replace("# databasePassword = ",
                                    "databasePassword = -",
                                    1)
        expected = expected.replace("# databaseFormat = ",
                                    "databaseFormat = text/vcard",
                                    1)
        self.assertEqualDiff(expected, migratedconfig)
        renamedconfig = scanFiles(otherroot, "scheduleworld.old.3")
        expected = expected.replace("/scheduleworld/",
                                    "/scheduleworld.old.3/")
        expected = expected.replace("ConsumerReady = 1",
                                    "ConsumerReady = 0")
        self.assertEqualDiff(expected, renamedconfig)

        # migrate once more, this time without the explicit context in
        # the config name => must not change the context, need second
        # .old dir
        out, err, code = self.runCmdline(["--migrate",
                                          "scheduleworld"])
        self.assertSilent(out, err)
        migratedconfig = scanFiles(otherroot, "scheduleworld")
        expected = expected.replace("/scheduleworld.old.3/",
                                    "/scheduleworld/")
        expected = expected.replace("ConsumerReady = 0",
                                    "ConsumerReady = 1")
        self.assertEqualDiff(expected, migratedconfig)
        renamedconfig = scanFiles(otherroot, "scheduleworld.old.4")
        expected = expected.replace("/scheduleworld/",
                                    "/scheduleworld.old.4/")
        expected = expected.replace("ConsumerReady = 1",
                                    "ConsumerReady = 0")
        self.assertEqualDiff(expected, renamedconfig)

        # remove ConsumerReady: must remain unset when migrating
        # hidden Syncevolution >= 1.2 configs
        out, err, code = self.runCmdline(["--configure",
                                          "--sync-property", "ConsumerReady=0",
                                          "scheduleworld"])
        self.assertSilent(out, err)

        # migrate once more => keep ConsumerReady unset
        out, err, code = self.runCmdline(["--migrate",
                                          "scheduleworld"])
        self.assertSilent(out, err)

        migratedconfig = scanFiles(otherroot, "scheduleworld")
        expected = expected.replace("/scheduleworld.old.4/",
                                    "/scheduleworld/")
        self.assertEqualDiff(expected, migratedconfig)
        renamedconfig = scanFiles(otherroot, "scheduleworld.old.5")
        expected = expected.replace("/scheduleworld/",
                                    "/scheduleworld.old.5/")
        self.assertEqualDiff(expected, renamedconfig)

    @property("debug", False)
    def testMigrateSyncFormat(self):
        '''TestCmdline.testMigrateSyncFormat - migrate from old configuration with forced format'''
        oldroot = xdg_root + "/.sync4j/evolution/scheduleworld"
        newroot = self.configdir + "/default"
        oldconfig = self.OldScheduleWorldConfig()
        oldconfig = oldconfig.replace('type = addressbook:text/vcard',
                                      'type = addressbook:text/vcard!',
                                      1)

        # migrate old config
        createFiles(oldroot, oldconfig)
        createdoldconfig = scanFiles(oldroot)
        out, err, code = self.runCmdline(["--migrate",
                                          "scheduleworld"])
        self.assertSilent(out, err)

        migratedconfig = scanFiles(newroot)
        expected = sortConfig(self.ScheduleWorldConfig())
        # migrating SyncEvolution < 1.2 configs sets ConsumerReady, to
        # keep config visible in the updated sync-ui
        expected = expected.replace("# ConsumerReady = 0",
                                    "ConsumerReady = 1")
        expected = expected.replace("# database = ",
                                    "database = xyz",
                                    1)
        expected = expected.replace("# databaseUser = ",
                                    "databaseUser = foo",
                                    1)
        # syncevo-dbus-server always uses keyring and doesn't
        # return plain-text password.
        expected = expected.replace("# databasePassword = ",
                                    "databasePassword = -",
                                    1)
        # migrating "type" sets forceSyncFormat (if non-standard) and
        # databaseFormat (if format was part of type, as for
        # addressbook
        expected = expected.replace("# databaseFormat = ",
                                    "databaseFormat = text/vcard",
                                    1)
        expected = expected.replace("# forceSyncFormat = 0",
                                    "forceSyncFormat = 1",
                                    1)
        self.assertEqualDiff(expected, migratedconfig)
        renamedconfig = scanFiles(oldroot + ".old")
        self.assertEqualDiff(createdoldconfig, renamedconfig)

    @property("debug", False)
    def testMigrateContext(self):
        '''TestCmdline.testMigrateContext - migrate context containing a peer'''
        # Must also migrate peer. Covers special case of inconsistent
        # "type".

        root = self.configdir + "/default"
        oldconfig = '''config.ini:logDir = none
peers/scheduleworld/config.ini:syncURL = http://sync.scheduleworld.com/funambol/ds
peers/scheduleworld/config.ini:# username = 
peers/scheduleworld/config.ini:# password = 

peers/scheduleworld/sources/addressbook/config.ini:sync = two-way
peers/scheduleworld/sources/addressbook/config.ini:uri = card3
peers/scheduleworld/sources/addressbook/config.ini:type = addressbook:text/vcard
sources/addressbook/config.ini:type = calendar

peers/funambol/config.ini:syncURL = http://sync.funambol.com/funambol/ds
peers/funambol/config.ini:# username = 
peers/funambol/config.ini:# password = 

peers/funambol/sources/calendar/config.ini:sync = refresh-from-server
peers/funambol/sources/calendar/config.ini:uri = cal
peers/funambol/sources/calendar/config.ini:type = calendar
peers/funambol/sources/addressbook/config.ini:# sync = disabled
peers/funambol/sources/addressbook/config.ini:type = file
sources/calendar/config.ini:type = memos

peers/memotoo/config.ini:syncURL = http://sync.memotoo.com/memotoo/ds
peers/memotoo/config.ini:# username = 
peers/memotoo/config.ini:# password = 

peers/memotoo/sources/memo/config.ini:sync = refresh-from-client
peers/memotoo/sources/memo/config.ini:uri = cal
peers/memotoo/sources/memo/config.ini:type = memo:text/plain
sources/memo/config.ini:type = todo
'''

        createFiles(root, oldconfig)
        out, err, code = self.runCmdline(["--migrate",
                                          "memo/backend=file", # override memo "backend" during migration
                                          "@default"])
        self.assertSilent(out, err)

        migratedconfig = scanFiles(root)

        expectedlines = ["peers/scheduleworld/",
                         "sources/addressbook/config.ini:backend = addressbook",
                         "sources/addressbook/config.ini:databaseFormat = text/vcard",
                         "peers/scheduleworld/sources/addressbook/config.ini:syncFormat = text/vcard",
                         "peers/scheduleworld/sources/addressbook/config.ini:sync = two-way",
                         "peers/scheduleworld/sources/calendar/config.ini:# sync = disabled",
                         "peers/scheduleworld/sources/memo/config.ini:# sync = disabled",
                         "sources/calendar/config.ini:backend = calendar",
                         "sources/calendar/config.ini:# databaseFormat = ",
                         "peers/funambol/sources/calendar/config.ini:# syncFormat = ",
                         "peers/funambol/sources/addressbook/config.ini:# sync = disabled",
                         "peers/funambol/sources/calendar/config.ini:sync = refresh-from-server",
                         "peers/funambol/sources/memo/config.ini:# sync = disabled",
                         "sources/memo/config.ini:backend = file",
                         "sources/memo/config.ini:databaseFormat = text/plain",
                         "peers/memotoo/sources/memo/config.ini:syncFormat = text/plain",
                         "peers/memotoo/sources/addressbook/config.ini:# sync = disabled",
                         "peers/memotoo/sources/calendar/config.ini:# sync = disabled",
                         "peers/memotoo/sources/memo/config.ini:sync = refresh-from-client"]

        for expectedline in expectedlines:
            self.assertIn(expectedline, migratedconfig)

    @property("debug", False)
    def testMigrateAutoSync(self):
        '''TestCmdline.testMigrateAutoSync - migrate old configuration files'''
        oldroot = xdg_root + "/.sync4j/evolution/scheduleworld"
        newroot = self.configdir + "/default"
        oldconfig = "spds/syncml/config.txt:autoSync = 1\n" + self.OldScheduleWorldConfig()

        # migrate old config
        createFiles(oldroot, oldconfig)
        createdconfig = scanFiles(oldroot)
        out, err, code = self.runCmdline(["--migrate",
                                          "scheduleworld"])
        self.assertSilent(out, err)

        migratedconfig = scanFiles(newroot)
        expected = self.ScheduleWorldConfig()
        expected = expected.replace("# autoSync = 0",
                                    "autoSync = 1",
                                    1)
        expected = sortConfig(expected)

        # migrating Syncevolution < 1.2 configs sets ConsumerReady, to
        # keep config visible in the updated sync-ui
        expected = expected.replace("# ConsumerReady = 0",
                                    "ConsumerReady = 1")
        expected = expected.replace("# database = ",
                                    "database = xyz",
                                    1)
        expected = expected.replace("# databaseUser = ",
                                    "databaseUser = foo",
                                    1)
        # uses keyring
        expected = expected.replace("# databasePassword = ",
                                    "databasePassword = -",
                                    1)
        # migrating "type" sets forceSyncFormat (if non-standard) and
        # databaseFormat (if format was part of type, as for
        # addressbook)
        expected = expected.replace("# databaseFormat = ",
                                    "databaseFormat = text/vcard", 1)
        self.assertEqualDiff(expected, migratedconfig)
        renamedconfig = scanFiles(oldroot + ".old")
        # autoSync must have been unset
        createdconfig = createdconfig.replace(":autoSync = 1",
                                              ":autoSync = 0",
                                              1)
        self.assertEqualDiff(createdconfig, renamedconfig)

        # rewrite existing config with autoSync set
        createdconfig = scanFiles(newroot, "scheduleworld")

        out, err, code = self.runCmdline(["--migrate",
                                          "scheduleworld"])
        self.assertSilent(out, err)

        migratedconfig = scanFiles(newroot, "scheduleworld")
        expected = self.ScheduleWorldConfig()
        expected = expected.replace("# autoSync = 0",
                                    "autoSync = 1",
                                    1)
        expected = sortConfig(expected)
        expected = expected.replace("# ConsumerReady = 0",
                                    "ConsumerReady = 1")
        expected = expected.replace("# database = ",
                                    "database = xyz",
                                    1)
        expected = expected.replace("# databaseUser = ",
                                    "databaseUser = foo",
                                    1)
        # uses keyring
        expected = expected.replace("# databasePassword = ",
                                    "databasePassword = -",
                                    1)
        expected = expected.replace("# databaseFormat = ",
                                    "databaseFormat = text/vcard",
                                    1)
        self.assertEqualDiff(expected, migratedconfig)
        renamedconfig = scanFiles(newroot, "scheduleworld.old.1")
        # autoSync must have been unset
        createdconfig = createdconfig.replace(":autoSync = 1",
                                              ":autoSync = 0",
                                              1)
        # the scheduleworld config was consumer ready, the migrated
        # one isn't
        createdconfig = createdconfig.replace("ConsumerReady = 1",
                                              "ConsumerReady = 0")
        createdconfig = createdconfig.replace("/scheduleworld/",
                                              "/scheduleworld.old.1/")
        self.assertEqualDiff(createdconfig, renamedconfig)

    @property("debug", False)
    def testItemOperations(self):
        '''TestCmdline.testItemOperations - add and print items'''
        # "foo" not configured
        out, err, code = self.runCmdline(["--print-items",
                                          "foo",
                                          "bar"],
                                         expectSuccess = False)
        # Information about supported modules is optional, depends on compilation of
        # SyncEvolution.
        self.assertRegexpMatches(err, r'''\[ERROR\] error code from SyncEvolution error parsing config file \(local, status 20010\): bar: backend not supported (by any of the backend modules \((\w+\.so, )+\w+\.so\) )?or not correctly configured \(backend=select backend databaseFormat= syncFormat=\)\n\[ERROR\] configuration 'foo' does not exist\n\[ERROR\] source 'bar' does not exist\n\[ERROR\] backend property not set\n''')
        self.assertEqualDiff('', out)

        # "foo" not configured, no source named
        out, err, code  = self.runCmdline(["--print-items",
                                           "foo"],
                                          expectSuccess = False)
        self.assertRegexpMatches(err, r'''\[ERROR\] error code from SyncEvolution error parsing config file \(local, status 20010\): backend not supported (by any of the backend modules \((\w+\.so, )+\w+\.so\) )?or not correctly configured \(backend=select backend databaseFormat= syncFormat=\)\n\[ERROR\] configuration 'foo' does not exist\n\[ERROR\] no source selected\n\[ERROR\] backend property not set\n''')
        self.assertEqualDiff('', out)

        # nothing known about source
        out, err, code = self.runCmdline(["--print-items"],
                                         expectSuccess = False)
        self.assertRegexpMatches(err, r'''\[ERROR\] error code from SyncEvolution error parsing config file \(local, status 20010\): backend not supported (by any of the backend modules \((\w+\.so, )+\w+\.so\) )?or not correctly configured \(backend=select backend databaseFormat= syncFormat=\)\n\[ERROR\] no source selected\n\[ERROR\] backend property not set\n''')
        self.assertEqualDiff('', out)

        # now create "foo"
        out, err, code = self.runCmdline(["--configure",
                                          "--template", "default",
                                          "foo"])
        self.assertSilent(out, err, ignore=self.sourceCheckOutput())

        # "foo" now configured, still no source
        out, err, code  = self.runCmdline(["--print-items",
                                           "foo"],
                                          expectSuccess = False)
        self.assertRegexpMatches(err, r'''\[ERROR\] error code from SyncEvolution error parsing config file \(local, status 20010\): backend not supported (by any of the backend modules \((\w+\.so, )+\w+\.so\) )?or not correctly configured \(backend=select backend databaseFormat= syncFormat=\)\n\[ERROR\] no source selected\n\[ERROR\] backend property not set\n''')
        self.assertEqualDiff('', out)

        # "foo" configured, but "bar" is not
        out, err, code = self.runCmdline(["--print-items",
                                          "foo",
                                          "bar"],
                                         expectSuccess = False)
        self.assertRegexpMatches(err, r'''\[ERROR\] error code from SyncEvolution error parsing config file \(local, status 20010\): bar: backend not supported (by any of the backend modules \((\w+\.so, )+\w+\.so\) )?or not correctly configured \(backend=select backend databaseFormat= syncFormat=\)\n\[ERROR\] source 'bar' does not exist\n\[ERROR\] backend property not set\n''')
        self.assertEqualDiff('', out)

        # add "bar" source, using file backend
        out, err, code = self.runCmdline(["--configure",
                                          "backend=file",
                                          "database=file://" + xdg_root + "/addressbook",
                                          "databaseFormat=text/vcard",
                                          "foo",
                                          "bar"])
        self.assertSilent(out, err)

        # no items yet
        out, err, code = self.runCmdline(["--print-items",
                                          "foo",
                                          "bar"])
        self.assertSilent(out, err)

        john = """BEGIN:VCARD
VERSION:3.0
FN:John Doe
N:Doe;John;;;
END:VCARD
"""
        joan = """BEGIN:VCARD
VERSION:3.0
FN:Joan Doe
N:Doe;Joan;;;
END:VCARD
"""

        # create one file
        file1 = "1:" + john
        file2 = "2:" + joan
        file1 = file1.replace("\n", "\n1:")
        file1 = file1[:-2]
        file2 = file2.replace("\n", "\n2:")
        file2 = file2[:-2]
        createFiles(xdg_root + "/addressbook", file1 + file2)

        out, err, code = self.runCmdline(["--print-items",
                                          "foo",
                                          "bar"])
        self.assertNoErrors(err)
        self.assertEqualDiff("1\n2\n", out)

        # alternatively just specify enough parameters without the
        # "foo" "bar" config part
        out, err, code = self.runCmdline(["--print-items",
                                          "backend=file",
                                          "database=file://" + xdg_root + "/addressbook",
                                          "databaseFormat=text/vcard"])
        self.assertNoErrors(err)
        self.assertEqualDiff("1\n2\n", out)

        # export all
        out, err, code = self.runCmdline(["--export", "-",
                                          "backend=file",
                                          "database=file://" + xdg_root + "/addressbook",
                                          "databaseFormat=text/vcard"])
        self.assertNoErrors(err)
        self.assertEqualDiff(john + "\n" + joan, out)

        # export all via config
        out, err, code = self.runCmdline(["--export", "-",
                                          "foo", "bar"])
        self.assertNoErrors(err)
        self.assertEqualDiff(john + "\n" + joan, out)

        # export all into file
        exportfile = xdg_root + "/export.vcf"
        out, err, code = self.runCmdline(["--export", exportfile,
                                          "foo", "bar"])
        self.assertNoErrors(err)
        self.assertEqualDiff(john + "\n" + joan, open(exportfile).read())

        # export one
        out, err, code = self.runCmdline(["--export", "-",
                                          "backend=file",
                                          "database=file://" + xdg_root + "/addressbook",
                                          "databaseFormat=text/vcard",
                                          "--luids", "1"])
        self.assertNoErrors(err)
        self.assertEqualDiff(john, out)

        # export one via config
        out, err, code = self.runCmdline(["--export", "-",
                                          "foo", "bar", "1"])
        self.assertNoErrors(err)
        self.assertEqualDiff(john, out)

        # export one into file
        exportfile = xdg_root + "/export.vcf"
        out, err, code = self.runCmdline(["--export", exportfile,
                                          "foo", "bar", "1"])
        self.assertNoErrors(err)
        self.assertEqualDiff(john, open(exportfile).read())

        # Copied from C++ test:
        # TODO: check configuration of just the source as @foo bar
        # without peer

        # check error message for missing config name
        out, err, code = self.runCmdline([],
                                         expectSuccess = False)
        self.expectUsageError(out, err,
                              "[ERROR] No configuration name specified.\n")

        # check error message for missing config name, version II
        out, err, code = self.runCmdline(["--run"],
                                         expectSuccess = False)
        self.expectUsageError(out, err,
                              "[ERROR] No configuration name specified.\n")

    def stripSyncTime(self, out):
        '''remove varying time from sync session output'''
        p = re.compile(r'^\| +start .*?, duration \d:\d\dmin +\|$',
                       re.MULTILINE)
        return p.sub('| start xxx, duration a:bcmin |', out)

    @property("debug", False)
    @timeout(200)
    def testSyncOutputErrors(self):
        """TestCmdline.testSyncOutputErrors - check error messages for sync operations"""
        self.setUpLocalSyncConfigs()
        self.session.Detach()

        # error message for --dry-run
        out, err, code = self.runCmdline(["--dry-run", "server"],
                                         expectSuccess=False)
        self.expectUsageError(out, err, '[ERROR] --dry-run not supported for running a synchronization\n')

        # ambiguous command line
        out, err, code = self.runCmdline(["sync=two-way", "server"],
                                         expectSuccess=False)
        self.expectUsageError(out, err, "[ERROR] Properties specified, but neither '--configure' nor '--run' - what did you want?\n")

        # ambiguous command line, empty source property
        out, err, code = self.runCmdline(["database=", "server"],
                                         expectSuccess=False)
        self.expectUsageError(out, err, "[ERROR] Properties specified, but neither '--configure' nor '--run' - what did you want?\n")

        # ambiguous command line, empty sync property
        out, err, code = self.runCmdline(["logdir=", "server"],
                                         expectSuccess=False)
        self.expectUsageError(out, err, "[ERROR] Properties specified, but neither '--configure' nor '--run' - what did you want?\n")

        # empty global property allowed
        out, err, code = self.runCmdline(["keyring=", "server"],
                                         sessionFlags=[],
                                         expectSuccess=True)


    @property("debug", False)
    @property("ENV", "SYNCEVOLUTION_SYNC_DELAY=60 LC_ALL=en_US.UTF-8 LANGUAGE=en_US")
    @timeout(200)
    def testSyncFailure1(self):
        """TestCmdline.testSyncFailure1 - check that cmdline notices when sync fails prematurely before it even starts"""
        self.setUpLocalSyncConfigs()
        self.session.Detach()

        # We must kill the process before the sync starts, to achieve
        # the expected output and sync result. The
        # SYNCEVOLUTION_LOCAL_CHILD_DELAY2 should give us that chance.
        self.killed = False
        def output(path, level, text, procname):
            if self.running and not self.killed and procname == '' and text == 'ready to sync':
                # kill syncevo-local-sync
                for pid, (name, cmdline) in self.getChildren().iteritems():
                    if 'syncevo-dbus-helper' in cmdline:
                        logging.printf('killing syncevo-dbus-helper with pid %d', pid)
                        os.kill(pid, signal.SIGKILL)
                        loop.quit()
                        self.killed = True
                        break

        receiver = bus.add_signal_receiver(output,
                                           'LogOutput',
                                           'org.syncevolution.Server',
                                           self.server.bus_name,
                                           byte_arrays=True,
                                           utf8_strings=True)
        try:
            s = self.startCmdline(["--sync", "slow", "server"], preserveOutputOrder=True)
            loop.run()
        finally:
            receiver.remove()
        self.assertTrue(self.killed)

        out, err, code = self.finishCmdline(s, expectSuccess=False, sessionFlags=[])
        self.assertEqual(err, None)
        self.assertEqual(1, code)
        out = self.stripSyncTime(out)
        # The error message is not particularly informative, but the error should
        # not occur, so let it be... Also, the "connection is closed" error only
        # occurs occasionally.
        self.assertNotEqual(out, '')
        self.assertTrue('''[ERROR] The connection is closed
''' in out or
                        '''[ERROR syncevo-dbus-server] child process quit because of signal 9
''' in out)

    @property("debug", False)
    @property("ENV", "SYNCEVOLUTION_LOCAL_CHILD_DELAY2=60 LC_ALL=en_US.UTF-8 LANGUAGE=en_US")
    @timeout(200)
    def testSyncFailure2(self):
        """TestCmdline.testSyncFailure2 - check that cmdline notices when sync fails prematurely in the middle"""
        self.setUpLocalSyncConfigs()
        self.session.Detach()

        # We must kill the process before the sync starts, to achieve
        # the expected output and sync result. The
        # SYNCEVOLUTION_LOCAL_CHILD_DELAY2 should give us that chance.
        self.killed = False
        def output(path, level, text, procname):
            if self.running and not self.killed and procname == '@client':
                # kill syncevo-local-sync
                for pid, (name, cmdline) in self.getChildren().iteritems():
                    if 'syncevo-local-sync' in cmdline:
                        logging.printf('killing syncevo-local-sync with pid %d', pid)
                        os.kill(pid, signal.SIGKILL)
                        loop.quit()
                        self.killed = True
                        break

        receiver = bus.add_signal_receiver(output,
                                           'LogOutput',
                                           'org.syncevolution.Server',
                                           self.server.bus_name,
                                           byte_arrays=True, 
                                           utf8_strings=True)
        try:
            s = self.startCmdline(["--sync", "slow", "server"], preserveOutputOrder=True)
            loop.run()
        finally:
            receiver.remove()
        self.assertTrue(self.killed)

        out, err, code = self.finishCmdline(s, expectSuccess=False, sessionFlags=[])
        self.assertEqual(err, None)
        self.assertEqual(1, code)
        out = self.stripSyncTime(out)
        out = re.sub(r'giving up after \d+ retries and \d+:\d+min',
                     'giving up after x retries and y:zzmin',
                     out)
        out = re.sub(r'Synchronization failed, see .*temp-test-dbus/cache/syncevolution/server.*/syncevolution-log.html for details.',
                     'Synchronization failed, see syncevolution-log.html for details.',
                     out)
        # Two possible outcomes:
        # 1. death of child is noticed first.
        # 2. loss of D-Bus connection is noticed first.
        # Also, the "connection is closed" error only
        # occurs occasionally.
        if out.startswith('[ERROR] child process quit because of signal 9'):
            out = out.replace('''[ERROR] sending message to child failed: The connection is closed
''', '')
            self.assertEqualDiff(out, '''[ERROR] child process quit because of signal 9
[ERROR] local transport failed: child process quit because of signal 9
[INFO] Transport giving up after x retries and y:zzmin
[ERROR] transport problem: transport failed, retry period exceeded
[INFO] creating complete data backup after sync (enabled with dumpData and needed for printChanges)

Synchronization failed, see syncevolution-log.html for details.

Changes applied during synchronization:
+---------------|-----------------------|-----------------------|-CON-+
|               |       @default        |        @client        | FLI |
|        Source | NEW | MOD | DEL | ERR | NEW | MOD | DEL | ERR | CTS |
+---------------+-----+-----+-----+-----+-----+-----+-----+-----+-----+
|   addressbook |  0  |  0  |  0  |  0  |  0  |  0  |  0  |  0  |  0  |
+---------------+-----+-----+-----+-----+-----+-----+-----+-----+-----+
| start xxx, duration a:bcmin |
|          external transport failure (local, status 20043)           |
+---------------+-----+-----+-----+-----+-----+-----+-----+-----+-----+
First ERROR encountered: child process quit because of signal 9

''')
        else:
            self.assertEqualDiff(out, '''[ERROR] sending message to child failed: The connection is closed
[INFO] Transport giving up after x retries and y:zzmin
[ERROR] transport problem: transport failed, retry period exceeded
[INFO] creating complete data backup after sync (enabled with dumpData and needed for printChanges)

Synchronization failed, see syncevolution-log.html for details.

Changes applied during synchronization:
+---------------|-----------------------|-----------------------|-CON-+
|               |       @default        |        @client        | FLI |
|        Source | NEW | MOD | DEL | ERR | NEW | MOD | DEL | ERR | CTS |
+---------------+-----+-----+-----+-----+-----+-----+-----+-----+-----+
|   addressbook |  0  |  0  |  0  |  0  |  0  |  0  |  0  |  0  |  0  |
+---------------+-----+-----+-----+-----+-----+-----+-----+-----+-----+
| start xxx, duration a:bcmin |
|          external transport failure (local, status 20043)           |
+---------------+-----+-----+-----+-----+-----+-----+-----+-----+-----+
First ERROR encountered: sending message to child failed: The connection is closed

''')

    @property("debug", False)
    @property("ENV", "SYNCEVOLUTION_SYNC_DELAY=60")
    @timeout(200)
    def testSyncFailure3(self):
        """TestCmdline.testSyncFailure3 - check that cmdline notices when server dies"""
        self.setUpLocalSyncConfigs()
        self.session.Detach()

        # We must kill the process before the sync starts, to achieve
        # the expected output and sync result. The
        # SYNCEVOLUTION_LOCAL_CHILD_DELAY2 should give us that chance.
        self.killed = False
        pid = self.serverPid()
        def output(path, level, text, procname):
            if self.running and not self.killed and text == 'ready to sync':
                # kill syncevo-dbus-server (and not the wrapper, because that would
                # also kill the process group)
                logging.printf('killing syncevo-dbus-server with pid %d', pid)
                os.kill(pid, signal.SIGKILL)
                self.killed = True
                loop.quit()

        receiver = bus.add_signal_receiver(output,
                                           'LogOutput',
                                           'org.syncevolution.Server',
                                           self.server.bus_name,
                                           byte_arrays=True,
                                           utf8_strings=True)
        try:
            s = self.startCmdline(["--sync", "slow", "server"], preserveOutputOrder=True)
            loop.run()
        finally:
            receiver.remove()
        self.assertTrue(self.killed)

        # Remove syncevo-dbus-server zombie process(es).
        DBusUtil.pserver.wait()
        DBusUtil.pserver = None

        out, err, code = self.finishCmdline(s, expectSuccess=False, sessionFlags=None)
        self.assertEqual(err, None)
        self.assertEqual(1, code)
        out = self.stripSyncTime(out)
        self.assertEqualDiff(out, '''[ERROR] Background sync daemon has gone.
''')

    @property("debug", False)
    @timeout(200)
    def testSyncOutput(self):
        """TestCmdline.testSyncOutput - run syncs between local dirs and check output"""
        self.setUpLocalSyncConfigs()
        self.session.Detach()
        os.makedirs(xdg_root + "/server")
        item = xdg_root + "/server/0"
        output = open(item, "w")
        output.write('''BEGIN:VCARD
VERSION:3.0
FN:John Doe
N:Doe;John
END:VCARD''')
        output.close()

        out, err, code = self.runCmdline(["--sync", "slow", "server"],
                                         sessionFlags=[],
                                         preserveOutputOrder=True)
        self.assertEqual(err, None)
        self.assertEqual(0, code)
        out = self.stripSyncTime(out)
        self.assertEqualDiff('''[INFO @client] target side of local sync ready
[INFO @client] @client/addressbook: starting first time sync, two-way (peer is server)
[INFO @client] creating complete data backup of source addressbook before sync (enabled with dumpData and needed for printChanges)
@client data changes to be applied during synchronization:
*** @client/addressbook ***
Comparison was impossible.

[INFO] @default/addressbook: starting first time sync, two-way (peer is client)
[INFO] creating complete data backup of source addressbook before sync (enabled with dumpData and needed for printChanges)
@default data changes to be applied during synchronization:
*** @default/addressbook ***
Comparison was impossible.

[INFO] @default/addressbook: started
[INFO] @default/addressbook: sent 1
[INFO @client] @client/addressbook: started
[INFO @client] @client/addressbook: received 1/1
[INFO @client] @client/addressbook: added 1, updated 0, removed 0
[INFO] @default/addressbook: first time sync done successfully
[INFO @client] @client/addressbook: first time sync done successfully
[INFO @client] creating complete data backup after sync (enabled with dumpData and needed for printChanges)

Synchronization successful.

Changes applied during synchronization (@client):
+---------------|-----------------------|-----------------------|-CON-+
|               |        @client        |       @default        | FLI |
|        Source | NEW | MOD | DEL | ERR | NEW | MOD | DEL | ERR | CTS |
+---------------+-----+-----+-----+-----+-----+-----+-----+-----+-----+
|   addressbook |  1  |  0  |  0  |  0  |  0  |  0  |  0  |  0  |  0  |
|   slow, 0 KB sent by client, 0 KB received                          |
|   item(s) in database backup: 0 before sync, 1 after it             |
+---------------+-----+-----+-----+-----+-----+-----+-----+-----+-----+
| start xxx, duration a:bcmin |
|               synchronization completed successfully                |
+---------------+-----+-----+-----+-----+-----+-----+-----+-----+-----+

Data modified @client during synchronization:
*** @client/addressbook ***
                           before sync | after sync
                   removed during sync <
                                       > added during sync
-------------------------------------------------------------------------------
                                       > BEGIN:VCARD                           
                                       > N:Doe;John                            
                                       > FN:John Doe                           
                                       > VERSION:3.0                           
                                       > END:VCARD                             
-------------------------------------------------------------------------------

[INFO] creating complete data backup after sync (enabled with dumpData and needed for printChanges)

Synchronization successful.

Changes applied during synchronization:
+---------------|-----------------------|-----------------------|-CON-+
|               |       @default        |        @client        | FLI |
|        Source | NEW | MOD | DEL | ERR | NEW | MOD | DEL | ERR | CTS |
+---------------+-----+-----+-----+-----+-----+-----+-----+-----+-----+
|   addressbook |  0  |  0  |  0  |  0  |  1  |  0  |  0  |  0  |  0  |
|   slow, 0 KB sent by client, 0 KB received                          |
|   item(s) in database backup: 1 before sync, 1 after it             |
+---------------+-----+-----+-----+-----+-----+-----+-----+-----+-----+
| start xxx, duration a:bcmin |
|               synchronization completed successfully                |
+---------------+-----+-----+-----+-----+-----+-----+-----+-----+-----+

Data modified @default during synchronization:
*** @default/addressbook ***
no changes

''', out)

        # Only 'addressbook' ever active. When done (= progress 100%),
        # a lot of information seems to be missing (= -1) or dubious
        # (1 out of 0 items sent?!). This information comes straight
        # from libsynthesis; use it as it is for now.
        self.collectEvents()
        self.assertRegexpMatches(self.prettyPrintEvents(),
                                 r'''status: idle, .*
(.*\n)+status: running;waiting, 0, \{addressbook: \(slow, running, 0\)\}
(.*\n)*progress: 100, \{addressbook: \(sending, -1, -1, 1, 0, -1, -1\)\}
(.*\n)*status: done, .*''')
        self.checkSync(numReports=1)

        # check result (should be unchanged)
        input = open(item, "r")
        self.assertIn("FN:John Doe", input.read())

        # no changes
        out, err, code = self.runCmdline(["server"],
                                         sessionFlags=[],
                                         preserveOutputOrder=True)
        self.assertEqual(err, None)
        self.assertEqual(0, code)
        out = self.stripSyncTime(out)
        self.assertEqualDiff('''[INFO @client] target side of local sync ready
[INFO @client] @client/addressbook: starting normal sync, two-way (peer is server)
[INFO @client] creating complete data backup of source addressbook before sync (enabled with dumpData and needed for printChanges)
@client data changes to be applied during synchronization:
*** @client/addressbook ***
no changes

[INFO] @default/addressbook: starting normal sync, two-way (peer is client)
[INFO] creating complete data backup of source addressbook before sync (enabled with dumpData and needed for printChanges)
@default data changes to be applied during synchronization:
*** @default/addressbook ***
no changes

[INFO] @default/addressbook: started
[INFO @client] @client/addressbook: started
[INFO] @default/addressbook: normal sync done successfully
[INFO @client] @client/addressbook: normal sync done successfully
[INFO @client] creating complete data backup after sync (enabled with dumpData and needed for printChanges)

Synchronization successful.

Changes applied during synchronization (@client):
+---------------|-----------------------|-----------------------|-CON-+
|               |        @client        |       @default        | FLI |
|        Source | NEW | MOD | DEL | ERR | NEW | MOD | DEL | ERR | CTS |
+---------------+-----+-----+-----+-----+-----+-----+-----+-----+-----+
|   addressbook |  0  |  0  |  0  |  0  |  0  |  0  |  0  |  0  |  0  |
|   two-way, 0 KB sent by client, 0 KB received                       |
|   item(s) in database backup: 1 before sync, 1 after it             |
+---------------+-----+-----+-----+-----+-----+-----+-----+-----+-----+
| start xxx, duration a:bcmin |
|               synchronization completed successfully                |
+---------------+-----+-----+-----+-----+-----+-----+-----+-----+-----+

Data modified @client during synchronization:
*** @client/addressbook ***
no changes

[INFO] creating complete data backup after sync (enabled with dumpData and needed for printChanges)

Synchronization successful.

Changes applied during synchronization:
+---------------|-----------------------|-----------------------|-CON-+
|               |       @default        |        @client        | FLI |
|        Source | NEW | MOD | DEL | ERR | NEW | MOD | DEL | ERR | CTS |
+---------------+-----+-----+-----+-----+-----+-----+-----+-----+-----+
|   addressbook |  0  |  0  |  0  |  0  |  0  |  0  |  0  |  0  |  0  |
|   two-way, 0 KB sent by client, 0 KB received                       |
|   item(s) in database backup: 1 before sync, 1 after it             |
+---------------+-----+-----+-----+-----+-----+-----+-----+-----+-----+
| start xxx, duration a:bcmin |
|               synchronization completed successfully                |
+---------------+-----+-----+-----+-----+-----+-----+-----+-----+-----+

Data modified @default during synchronization:
*** @default/addressbook ***
no changes

''', out)
        self.collectEvents()
        self.assertRegexpMatches(self.prettyPrintEvents(),
                                 r'''status: idle, .*
(.*\n)+status: running;waiting, 0, \{addressbook: \(two-way, running, 0\)\}
(.*\n)*progress: 100, \{addressbook: \(, -1, -1, -1, -1, -1, -1\)\}
(.*\n)*status: done, .*''')
        self.checkSync(numReports=2)

        # update contact
        output = open(item, "w")
        output.write('''BEGIN:VCARD
VERSION:3.0
FN:Joan Doe
N:Doe;Joan
END:VCARD''')
        output.close()
        out, err, code = self.runCmdline(["server"],
                                         sessionFlags=[],
                                         preserveOutputOrder=True)
        self.assertEqual(err, None)
        self.assertEqual(0, code)
        out = self.stripSyncTime(out)
        self.assertEqualDiff('''[INFO @client] target side of local sync ready
[INFO @client] @client/addressbook: starting normal sync, two-way (peer is server)
[INFO @client] creating complete data backup of source addressbook before sync (enabled with dumpData and needed for printChanges)
@client data changes to be applied during synchronization:
*** @client/addressbook ***
no changes

[INFO] @default/addressbook: starting normal sync, two-way (peer is client)
[INFO] creating complete data backup of source addressbook before sync (enabled with dumpData and needed for printChanges)
@default data changes to be applied during synchronization:
*** @default/addressbook ***
                       after last sync | current data
               removed since last sync <
                                       > added since last sync
-------------------------------------------------------------------------------
BEGIN:VCARD                              BEGIN:VCARD                           
N:Doe;John                             | N:Doe;Joan                            
FN:John Doe                            | FN:Joan Doe                           
VERSION:3.0                              VERSION:3.0                           
END:VCARD                                END:VCARD                             
-------------------------------------------------------------------------------

[INFO] @default/addressbook: started
[INFO] @default/addressbook: sent 1
[INFO @client] @client/addressbook: started
[INFO @client] @client/addressbook: received 1/1
[INFO @client] @client/addressbook: added 0, updated 1, removed 0
[INFO] @default/addressbook: normal sync done successfully
[INFO @client] @client/addressbook: normal sync done successfully
[INFO @client] creating complete data backup after sync (enabled with dumpData and needed for printChanges)

Synchronization successful.

Changes applied during synchronization (@client):
+---------------|-----------------------|-----------------------|-CON-+
|               |        @client        |       @default        | FLI |
|        Source | NEW | MOD | DEL | ERR | NEW | MOD | DEL | ERR | CTS |
+---------------+-----+-----+-----+-----+-----+-----+-----+-----+-----+
|   addressbook |  0  |  1  |  0  |  0  |  0  |  0  |  0  |  0  |  0  |
|   two-way, 0 KB sent by client, 0 KB received                       |
|   item(s) in database backup: 1 before sync, 1 after it             |
+---------------+-----+-----+-----+-----+-----+-----+-----+-----+-----+
| start xxx, duration a:bcmin |
|               synchronization completed successfully                |
+---------------+-----+-----+-----+-----+-----+-----+-----+-----+-----+

Data modified @client during synchronization:
*** @client/addressbook ***
                           before sync | after sync
                   removed during sync <
                                       > added during sync
-------------------------------------------------------------------------------
BEGIN:VCARD                              BEGIN:VCARD                           
N:Doe;John                             | N:Doe;Joan                            
FN:John Doe                            | FN:Joan Doe                           
VERSION:3.0                              VERSION:3.0                           
END:VCARD                                END:VCARD                             
-------------------------------------------------------------------------------

[INFO] creating complete data backup after sync (enabled with dumpData and needed for printChanges)

Synchronization successful.

Changes applied during synchronization:
+---------------|-----------------------|-----------------------|-CON-+
|               |       @default        |        @client        | FLI |
|        Source | NEW | MOD | DEL | ERR | NEW | MOD | DEL | ERR | CTS |
+---------------+-----+-----+-----+-----+-----+-----+-----+-----+-----+
|   addressbook |  0  |  0  |  0  |  0  |  0  |  1  |  0  |  0  |  0  |
|   two-way, 0 KB sent by client, 0 KB received                       |
|   item(s) in database backup: 1 before sync, 1 after it             |
+---------------+-----+-----+-----+-----+-----+-----+-----+-----+-----+
| start xxx, duration a:bcmin |
|               synchronization completed successfully                |
+---------------+-----+-----+-----+-----+-----+-----+-----+-----+-----+

Data modified @default during synchronization:
*** @default/addressbook ***
no changes

''', out)
        self.collectEvents()
        self.assertRegexpMatches(self.prettyPrintEvents(),
                                 r'''status: idle, .*
(.*\n)+status: running;waiting, 0, \{addressbook: \(two-way, running, 0\)\}
(.*\n)*progress: 100, \{addressbook: \(sending, -1, -1, 1, 0, -1, -1\)\}
(.*\n)*status: done, .*''')
        self.checkSync(numReports=3)

        # now remove contact
        os.unlink(item)
        out, err, code = self.runCmdline(["server"],
                                         sessionFlags=[],
                                         preserveOutputOrder=True)
        self.assertEqual(err, None)
        self.assertEqual(0, code)
        out = self.stripSyncTime(out)
        self.assertEqualDiff('''[INFO @client] target side of local sync ready
[INFO @client] @client/addressbook: starting normal sync, two-way (peer is server)
[INFO @client] creating complete data backup of source addressbook before sync (enabled with dumpData and needed for printChanges)
@client data changes to be applied during synchronization:
*** @client/addressbook ***
no changes

[INFO] @default/addressbook: starting normal sync, two-way (peer is client)
[INFO] creating complete data backup of source addressbook before sync (enabled with dumpData and needed for printChanges)
@default data changes to be applied during synchronization:
*** @default/addressbook ***
                       after last sync | current data
               removed since last sync <
                                       > added since last sync
-------------------------------------------------------------------------------
BEGIN:VCARD                            <
N:Doe;Joan                             <
FN:Joan Doe                            <
VERSION:3.0                            <
END:VCARD                              <
-------------------------------------------------------------------------------

[INFO] @default/addressbook: started
[INFO] @default/addressbook: sent 1
[INFO @client] @client/addressbook: started
[INFO @client] @client/addressbook: received 1/1
[INFO @client] @client/addressbook: added 0, updated 0, removed 1
[INFO] @default/addressbook: normal sync done successfully
[INFO @client] @client/addressbook: normal sync done successfully
[INFO @client] creating complete data backup after sync (enabled with dumpData and needed for printChanges)

Synchronization successful.

Changes applied during synchronization (@client):
+---------------|-----------------------|-----------------------|-CON-+
|               |        @client        |       @default        | FLI |
|        Source | NEW | MOD | DEL | ERR | NEW | MOD | DEL | ERR | CTS |
+---------------+-----+-----+-----+-----+-----+-----+-----+-----+-----+
|   addressbook |  0  |  0  |  1  |  0  |  0  |  0  |  0  |  0  |  0  |
|   two-way, 0 KB sent by client, 0 KB received                       |
|   item(s) in database backup: 1 before sync, 0 after it             |
+---------------+-----+-----+-----+-----+-----+-----+-----+-----+-----+
| start xxx, duration a:bcmin |
|               synchronization completed successfully                |
+---------------+-----+-----+-----+-----+-----+-----+-----+-----+-----+

Data modified @client during synchronization:
*** @client/addressbook ***
                           before sync | after sync
                   removed during sync <
                                       > added during sync
-------------------------------------------------------------------------------
BEGIN:VCARD                            <
N:Doe;Joan                             <
FN:Joan Doe                            <
VERSION:3.0                            <
END:VCARD                              <
-------------------------------------------------------------------------------

[INFO] creating complete data backup after sync (enabled with dumpData and needed for printChanges)

Synchronization successful.

Changes applied during synchronization:
+---------------|-----------------------|-----------------------|-CON-+
|               |       @default        |        @client        | FLI |
|        Source | NEW | MOD | DEL | ERR | NEW | MOD | DEL | ERR | CTS |
+---------------+-----+-----+-----+-----+-----+-----+-----+-----+-----+
|   addressbook |  0  |  0  |  0  |  0  |  0  |  0  |  1  |  0  |  0  |
|   two-way, 0 KB sent by client, 0 KB received                       |
|   item(s) in database backup: 0 before sync, 0 after it             |
+---------------+-----+-----+-----+-----+-----+-----+-----+-----+-----+
| start xxx, duration a:bcmin |
|               synchronization completed successfully                |
+---------------+-----+-----+-----+-----+-----+-----+-----+-----+-----+

Data modified @default during synchronization:
*** @default/addressbook ***
no changes

''', out)
        self.collectEvents()
        self.assertRegexpMatches(self.prettyPrintEvents(),
                                 r'''status: idle, .*
(.*\n)+status: running;waiting, 0, \{addressbook: \(two-way, running, 0\)\}
(.*\n)*progress: 100, \{addressbook: \(sending, -1, -1, 1, 0, -1, -1\)\}
(.*\n)*status: done, .*''')
        self.checkSync(numReports=4)

    @property("debug", False)
    @timeout(200)
    def testSyncOutput2(self):
        """TestCmdline.testSyncOutput2 - run syncs between local dirs and check output, with two sources"""
        self.setUpLocalSyncConfigs(enableCalendar=True)
        self.session.Detach()
        os.makedirs(xdg_root + "/server")
        item = xdg_root + "/server/0"
        output = open(item, "w")
        output.write('''BEGIN:VCARD
VERSION:3.0
FN:John Doe
N:Doe;John
END:VCARD''')
        output.close()

        out, err, code = self.runCmdline(["--sync", "slow", "server"],
                                         sessionFlags=[],
                                         preserveOutputOrder=True)
        self.assertEqual(err, None)
        self.assertEqual(0, code)
        out = self.stripSyncTime(out)
        self.assertEqualDiff('''[INFO @client] target side of local sync ready
[INFO @client] @client/addressbook: starting first time sync, two-way (peer is server)
[INFO @client] @client/calendar: starting first time sync, two-way (peer is server)
[INFO @client] creating complete data backup of source addressbook before sync (enabled with dumpData and needed for printChanges)
@client data changes to be applied during synchronization:
*** @client/addressbook ***
Comparison was impossible.

[INFO @client] creating complete data backup of source calendar before sync (enabled with dumpData and needed for printChanges)
*** @client/calendar ***
Comparison was impossible.

[INFO] @default/addressbook: starting first time sync, two-way (peer is client)
[INFO] @default/calendar: starting first time sync, two-way (peer is client)
[INFO] creating complete data backup of source addressbook before sync (enabled with dumpData and needed for printChanges)
@default data changes to be applied during synchronization:
*** @default/addressbook ***
Comparison was impossible.

[INFO] @default/addressbook: started
[INFO] creating complete data backup of source calendar before sync (enabled with dumpData and needed for printChanges)
*** @default/calendar ***
Comparison was impossible.

[INFO] @default/calendar: started
[INFO] @default/addressbook: sent 1
[INFO @client] @client/addressbook: started
[INFO @client] @client/addressbook: received 1/1
[INFO @client] @client/addressbook: added 1, updated 0, removed 0
[INFO @client] @client/calendar: started
[INFO] @default/addressbook: first time sync done successfully
[INFO] @default/calendar: first time sync done successfully
[INFO @client] @client/addressbook: first time sync done successfully
[INFO @client] @client/calendar: first time sync done successfully
[INFO @client] creating complete data backup after sync (enabled with dumpData and needed for printChanges)

Synchronization successful.

Changes applied during synchronization (@client):
+---------------|-----------------------|-----------------------|-CON-+
|               |        @client        |       @default        | FLI |
|        Source | NEW | MOD | DEL | ERR | NEW | MOD | DEL | ERR | CTS |
+---------------+-----+-----+-----+-----+-----+-----+-----+-----+-----+
|   addressbook |  1  |  0  |  0  |  0  |  0  |  0  |  0  |  0  |  0  |
|   slow, 0 KB sent by client, 0 KB received                          |
|   item(s) in database backup: 0 before sync, 1 after it             |
+---------------+-----+-----+-----+-----+-----+-----+-----+-----+-----+
|      calendar |  0  |  0  |  0  |  0  |  0  |  0  |  0  |  0  |  0  |
|      slow, 0 KB sent by client, 0 KB received                       |
|      item(s) in database backup: 0 before sync, 0 after it          |
+---------------+-----+-----+-----+-----+-----+-----+-----+-----+-----+
| start xxx, duration a:bcmin |
|               synchronization completed successfully                |
+---------------+-----+-----+-----+-----+-----+-----+-----+-----+-----+

Data modified @client during synchronization:
*** @client/addressbook ***
                           before sync | after sync
                   removed during sync <
                                       > added during sync
-------------------------------------------------------------------------------
                                       > BEGIN:VCARD                           
                                       > N:Doe;John                            
                                       > FN:John Doe                           
                                       > VERSION:3.0                           
                                       > END:VCARD                             
-------------------------------------------------------------------------------
*** @client/calendar ***
no changes

[INFO] creating complete data backup after sync (enabled with dumpData and needed for printChanges)

Synchronization successful.

Changes applied during synchronization:
+---------------|-----------------------|-----------------------|-CON-+
|               |       @default        |        @client        | FLI |
|        Source | NEW | MOD | DEL | ERR | NEW | MOD | DEL | ERR | CTS |
+---------------+-----+-----+-----+-----+-----+-----+-----+-----+-----+
|   addressbook |  0  |  0  |  0  |  0  |  1  |  0  |  0  |  0  |  0  |
|   slow, 0 KB sent by client, 0 KB received                          |
|   item(s) in database backup: 1 before sync, 1 after it             |
+---------------+-----+-----+-----+-----+-----+-----+-----+-----+-----+
|      calendar |  0  |  0  |  0  |  0  |  0  |  0  |  0  |  0  |  0  |
|      slow, 0 KB sent by client, 0 KB received                       |
|      item(s) in database backup: 0 before sync, 0 after it          |
+---------------+-----+-----+-----+-----+-----+-----+-----+-----+-----+
| start xxx, duration a:bcmin |
|               synchronization completed successfully                |
+---------------+-----+-----+-----+-----+-----+-----+-----+-----+-----+

Data modified @default during synchronization:
*** @default/addressbook ***
no changes
*** @default/calendar ***
no changes

''', out)
        self.collectEvents()
        self.assertRegexpMatches(self.prettyPrintEvents(),
                                 r'''status: idle, .*
(.*\n)+status: running;waiting, 0, \{addressbook: \(slow, running, 0\), calendar: \(slow, running, 0\)\}
(.*\n)*progress: 100, \{addressbook: \(sending, -1, -1, 1, 0, -1, -1\), calendar: \(, -1, -1, -1, -1, -1, -1\)\}
(.*\n)*status: done, .*''')
        self.checkSync(numReports=1)

        # check result (should be unchanged)
        input = open(item, "r")
        self.assertIn("FN:John Doe", input.read())

        # no changes
        out, err, code = self.runCmdline(["server"],
                                         sessionFlags=[],
                                         preserveOutputOrder=True)
        self.assertEqual(err, None)
        self.assertEqual(0, code)
        out = self.stripSyncTime(out)
        self.assertEqualDiff('''[INFO @client] target side of local sync ready
[INFO @client] @client/addressbook: starting normal sync, two-way (peer is server)
[INFO @client] @client/calendar: starting normal sync, two-way (peer is server)
[INFO @client] creating complete data backup of source addressbook before sync (enabled with dumpData and needed for printChanges)
@client data changes to be applied during synchronization:
*** @client/addressbook ***
no changes

[INFO @client] creating complete data backup of source calendar before sync (enabled with dumpData and needed for printChanges)
*** @client/calendar ***
no changes

[INFO] @default/addressbook: starting normal sync, two-way (peer is client)
[INFO] @default/calendar: starting normal sync, two-way (peer is client)
[INFO] creating complete data backup of source addressbook before sync (enabled with dumpData and needed for printChanges)
@default data changes to be applied during synchronization:
*** @default/addressbook ***
no changes

[INFO] @default/addressbook: started
[INFO] creating complete data backup of source calendar before sync (enabled with dumpData and needed for printChanges)
*** @default/calendar ***
no changes

[INFO] @default/calendar: started
[INFO @client] @client/addressbook: started
[INFO @client] @client/calendar: started
[INFO] @default/addressbook: normal sync done successfully
[INFO] @default/calendar: normal sync done successfully
[INFO @client] @client/addressbook: normal sync done successfully
[INFO @client] @client/calendar: normal sync done successfully
[INFO @client] creating complete data backup after sync (enabled with dumpData and needed for printChanges)

Synchronization successful.

Changes applied during synchronization (@client):
+---------------|-----------------------|-----------------------|-CON-+
|               |        @client        |       @default        | FLI |
|        Source | NEW | MOD | DEL | ERR | NEW | MOD | DEL | ERR | CTS |
+---------------+-----+-----+-----+-----+-----+-----+-----+-----+-----+
|   addressbook |  0  |  0  |  0  |  0  |  0  |  0  |  0  |  0  |  0  |
|   two-way, 0 KB sent by client, 0 KB received                       |
|   item(s) in database backup: 1 before sync, 1 after it             |
+---------------+-----+-----+-----+-----+-----+-----+-----+-----+-----+
|      calendar |  0  |  0  |  0  |  0  |  0  |  0  |  0  |  0  |  0  |
|      two-way, 0 KB sent by client, 0 KB received                    |
|      item(s) in database backup: 0 before sync, 0 after it          |
+---------------+-----+-----+-----+-----+-----+-----+-----+-----+-----+
| start xxx, duration a:bcmin |
|               synchronization completed successfully                |
+---------------+-----+-----+-----+-----+-----+-----+-----+-----+-----+

Data modified @client during synchronization:
*** @client/addressbook ***
no changes
*** @client/calendar ***
no changes

[INFO] creating complete data backup after sync (enabled with dumpData and needed for printChanges)

Synchronization successful.

Changes applied during synchronization:
+---------------|-----------------------|-----------------------|-CON-+
|               |       @default        |        @client        | FLI |
|        Source | NEW | MOD | DEL | ERR | NEW | MOD | DEL | ERR | CTS |
+---------------+-----+-----+-----+-----+-----+-----+-----+-----+-----+
|   addressbook |  0  |  0  |  0  |  0  |  0  |  0  |  0  |  0  |  0  |
|   two-way, 0 KB sent by client, 0 KB received                       |
|   item(s) in database backup: 1 before sync, 1 after it             |
+---------------+-----+-----+-----+-----+-----+-----+-----+-----+-----+
|      calendar |  0  |  0  |  0  |  0  |  0  |  0  |  0  |  0  |  0  |
|      two-way, 0 KB sent by client, 0 KB received                    |
|      item(s) in database backup: 0 before sync, 0 after it          |
+---------------+-----+-----+-----+-----+-----+-----+-----+-----+-----+
| start xxx, duration a:bcmin |
|               synchronization completed successfully                |
+---------------+-----+-----+-----+-----+-----+-----+-----+-----+-----+

Data modified @default during synchronization:
*** @default/addressbook ***
no changes
*** @default/calendar ***
no changes

''', out)
        self.collectEvents()
        self.assertRegexpMatches(self.prettyPrintEvents(),
                                 r'''status: idle, .*
(.*\n)+status: running;waiting, 0, \{addressbook: \(two-way, running, 0\), calendar: \(two-way, running, 0\)\}
(.*\n)*progress: 100, \{addressbook: \(, -1, -1, -1, -1, -1, -1\), calendar: \(, -1, -1, -1, -1, -1, -1\)\}
(.*\n)*status: done, .*''')
        self.checkSync(numReports=2)

        # update contact
        output = open(item, "w")
        output.write('''BEGIN:VCARD
VERSION:3.0
FN:Joan Doe
N:Doe;Joan
END:VCARD''')
        output.close()
        out, err, code = self.runCmdline(["server"],
                                         sessionFlags=[],
                                         preserveOutputOrder=True)
        self.assertEqual(err, None)
        self.assertEqual(0, code)
        out = self.stripSyncTime(out)
        self.assertEqualDiff('''[INFO @client] target side of local sync ready
[INFO @client] @client/addressbook: starting normal sync, two-way (peer is server)
[INFO @client] @client/calendar: starting normal sync, two-way (peer is server)
[INFO @client] creating complete data backup of source addressbook before sync (enabled with dumpData and needed for printChanges)
@client data changes to be applied during synchronization:
*** @client/addressbook ***
no changes

[INFO @client] creating complete data backup of source calendar before sync (enabled with dumpData and needed for printChanges)
*** @client/calendar ***
no changes

[INFO] @default/addressbook: starting normal sync, two-way (peer is client)
[INFO] @default/calendar: starting normal sync, two-way (peer is client)
[INFO] creating complete data backup of source addressbook before sync (enabled with dumpData and needed for printChanges)
@default data changes to be applied during synchronization:
*** @default/addressbook ***
                       after last sync | current data
               removed since last sync <
                                       > added since last sync
-------------------------------------------------------------------------------
BEGIN:VCARD                              BEGIN:VCARD                           
N:Doe;John                             | N:Doe;Joan                            
FN:John Doe                            | FN:Joan Doe                           
VERSION:3.0                              VERSION:3.0                           
END:VCARD                                END:VCARD                             
-------------------------------------------------------------------------------

[INFO] @default/addressbook: started
[INFO] creating complete data backup of source calendar before sync (enabled with dumpData and needed for printChanges)
*** @default/calendar ***
no changes

[INFO] @default/calendar: started
[INFO] @default/addressbook: sent 1
[INFO @client] @client/addressbook: started
[INFO @client] @client/addressbook: received 1/1
[INFO @client] @client/addressbook: added 0, updated 1, removed 0
[INFO @client] @client/calendar: started
[INFO] @default/addressbook: normal sync done successfully
[INFO] @default/calendar: normal sync done successfully
[INFO @client] @client/addressbook: normal sync done successfully
[INFO @client] @client/calendar: normal sync done successfully
[INFO @client] creating complete data backup after sync (enabled with dumpData and needed for printChanges)

Synchronization successful.

Changes applied during synchronization (@client):
+---------------|-----------------------|-----------------------|-CON-+
|               |        @client        |       @default        | FLI |
|        Source | NEW | MOD | DEL | ERR | NEW | MOD | DEL | ERR | CTS |
+---------------+-----+-----+-----+-----+-----+-----+-----+-----+-----+
|   addressbook |  0  |  1  |  0  |  0  |  0  |  0  |  0  |  0  |  0  |
|   two-way, 0 KB sent by client, 0 KB received                       |
|   item(s) in database backup: 1 before sync, 1 after it             |
+---------------+-----+-----+-----+-----+-----+-----+-----+-----+-----+
|      calendar |  0  |  0  |  0  |  0  |  0  |  0  |  0  |  0  |  0  |
|      two-way, 0 KB sent by client, 0 KB received                    |
|      item(s) in database backup: 0 before sync, 0 after it          |
+---------------+-----+-----+-----+-----+-----+-----+-----+-----+-----+
| start xxx, duration a:bcmin |
|               synchronization completed successfully                |
+---------------+-----+-----+-----+-----+-----+-----+-----+-----+-----+

Data modified @client during synchronization:
*** @client/addressbook ***
                           before sync | after sync
                   removed during sync <
                                       > added during sync
-------------------------------------------------------------------------------
BEGIN:VCARD                              BEGIN:VCARD                           
N:Doe;John                             | N:Doe;Joan                            
FN:John Doe                            | FN:Joan Doe                           
VERSION:3.0                              VERSION:3.0                           
END:VCARD                                END:VCARD                             
-------------------------------------------------------------------------------
*** @client/calendar ***
no changes

[INFO] creating complete data backup after sync (enabled with dumpData and needed for printChanges)

Synchronization successful.

Changes applied during synchronization:
+---------------|-----------------------|-----------------------|-CON-+
|               |       @default        |        @client        | FLI |
|        Source | NEW | MOD | DEL | ERR | NEW | MOD | DEL | ERR | CTS |
+---------------+-----+-----+-----+-----+-----+-----+-----+-----+-----+
|   addressbook |  0  |  0  |  0  |  0  |  0  |  1  |  0  |  0  |  0  |
|   two-way, 0 KB sent by client, 0 KB received                       |
|   item(s) in database backup: 1 before sync, 1 after it             |
+---------------+-----+-----+-----+-----+-----+-----+-----+-----+-----+
|      calendar |  0  |  0  |  0  |  0  |  0  |  0  |  0  |  0  |  0  |
|      two-way, 0 KB sent by client, 0 KB received                    |
|      item(s) in database backup: 0 before sync, 0 after it          |
+---------------+-----+-----+-----+-----+-----+-----+-----+-----+-----+
| start xxx, duration a:bcmin |
|               synchronization completed successfully                |
+---------------+-----+-----+-----+-----+-----+-----+-----+-----+-----+

Data modified @default during synchronization:
*** @default/addressbook ***
no changes
*** @default/calendar ***
no changes

''', out)
        self.collectEvents()
        self.assertRegexpMatches(self.prettyPrintEvents(),
                                 r'''status: idle, .*
(.*\n)+status: running;waiting, 0, \{addressbook: \(two-way, running, 0\), calendar: \(two-way, running, 0\)\}
(.*\n)*progress: 100, \{addressbook: \(sending, -1, -1, 1, 0, -1, -1\), calendar: \(, -1, -1, -1, -1, -1, -1\)\}
(.*\n)*status: done, .*''')
        self.checkSync(numReports=3)

        # now remove contact
        os.unlink(item)
        out, err, code = self.runCmdline(["server"],
                                         sessionFlags=[],
                                         preserveOutputOrder=True)
        self.assertEqual(err, None)
        self.assertEqual(0, code)
        out = self.stripSyncTime(out)
        self.assertEqualDiff('''[INFO @client] target side of local sync ready
[INFO @client] @client/addressbook: starting normal sync, two-way (peer is server)
[INFO @client] @client/calendar: starting normal sync, two-way (peer is server)
[INFO @client] creating complete data backup of source addressbook before sync (enabled with dumpData and needed for printChanges)
@client data changes to be applied during synchronization:
*** @client/addressbook ***
no changes

[INFO @client] creating complete data backup of source calendar before sync (enabled with dumpData and needed for printChanges)
*** @client/calendar ***
no changes

[INFO] @default/addressbook: starting normal sync, two-way (peer is client)
[INFO] @default/calendar: starting normal sync, two-way (peer is client)
[INFO] creating complete data backup of source addressbook before sync (enabled with dumpData and needed for printChanges)
@default data changes to be applied during synchronization:
*** @default/addressbook ***
                       after last sync | current data
               removed since last sync <
                                       > added since last sync
-------------------------------------------------------------------------------
BEGIN:VCARD                            <
N:Doe;Joan                             <
FN:Joan Doe                            <
VERSION:3.0                            <
END:VCARD                              <
-------------------------------------------------------------------------------

[INFO] @default/addressbook: started
[INFO] creating complete data backup of source calendar before sync (enabled with dumpData and needed for printChanges)
*** @default/calendar ***
no changes

[INFO] @default/calendar: started
[INFO] @default/addressbook: sent 1
[INFO @client] @client/addressbook: started
[INFO @client] @client/addressbook: received 1/1
[INFO @client] @client/addressbook: added 0, updated 0, removed 1
[INFO @client] @client/calendar: started
[INFO] @default/addressbook: normal sync done successfully
[INFO] @default/calendar: normal sync done successfully
[INFO @client] @client/addressbook: normal sync done successfully
[INFO @client] @client/calendar: normal sync done successfully
[INFO @client] creating complete data backup after sync (enabled with dumpData and needed for printChanges)

Synchronization successful.

Changes applied during synchronization (@client):
+---------------|-----------------------|-----------------------|-CON-+
|               |        @client        |       @default        | FLI |
|        Source | NEW | MOD | DEL | ERR | NEW | MOD | DEL | ERR | CTS |
+---------------+-----+-----+-----+-----+-----+-----+-----+-----+-----+
|   addressbook |  0  |  0  |  1  |  0  |  0  |  0  |  0  |  0  |  0  |
|   two-way, 0 KB sent by client, 0 KB received                       |
|   item(s) in database backup: 1 before sync, 0 after it             |
+---------------+-----+-----+-----+-----+-----+-----+-----+-----+-----+
|      calendar |  0  |  0  |  0  |  0  |  0  |  0  |  0  |  0  |  0  |
|      two-way, 0 KB sent by client, 0 KB received                    |
|      item(s) in database backup: 0 before sync, 0 after it          |
+---------------+-----+-----+-----+-----+-----+-----+-----+-----+-----+
| start xxx, duration a:bcmin |
|               synchronization completed successfully                |
+---------------+-----+-----+-----+-----+-----+-----+-----+-----+-----+

Data modified @client during synchronization:
*** @client/addressbook ***
                           before sync | after sync
                   removed during sync <
                                       > added during sync
-------------------------------------------------------------------------------
BEGIN:VCARD                            <
N:Doe;Joan                             <
FN:Joan Doe                            <
VERSION:3.0                            <
END:VCARD                              <
-------------------------------------------------------------------------------
*** @client/calendar ***
no changes

[INFO] creating complete data backup after sync (enabled with dumpData and needed for printChanges)

Synchronization successful.

Changes applied during synchronization:
+---------------|-----------------------|-----------------------|-CON-+
|               |       @default        |        @client        | FLI |
|        Source | NEW | MOD | DEL | ERR | NEW | MOD | DEL | ERR | CTS |
+---------------+-----+-----+-----+-----+-----+-----+-----+-----+-----+
|   addressbook |  0  |  0  |  0  |  0  |  0  |  0  |  1  |  0  |  0  |
|   two-way, 0 KB sent by client, 0 KB received                       |
|   item(s) in database backup: 0 before sync, 0 after it             |
+---------------+-----+-----+-----+-----+-----+-----+-----+-----+-----+
|      calendar |  0  |  0  |  0  |  0  |  0  |  0  |  0  |  0  |  0  |
|      two-way, 0 KB sent by client, 0 KB received                    |
|      item(s) in database backup: 0 before sync, 0 after it          |
+---------------+-----+-----+-----+-----+-----+-----+-----+-----+-----+
| start xxx, duration a:bcmin |
|               synchronization completed successfully                |
+---------------+-----+-----+-----+-----+-----+-----+-----+-----+-----+

Data modified @default during synchronization:
*** @default/addressbook ***
no changes
*** @default/calendar ***
no changes

''', out)
        self.collectEvents()
        self.assertRegexpMatches(self.prettyPrintEvents(),
                                 r'''status: idle, .*
(.*\n)+status: running;waiting, 0, \{addressbook: \(two-way, running, 0\), calendar: \(two-way, running, 0\)\}
(.*\n)*progress: 100, \{addressbook: \(sending, -1, -1, 1, 0, -1, -1\), calendar: \(, -1, -1, -1, -1, -1, -1\)\}
(.*\n)*status: done, .*''')
        self.checkSync(numReports=4)

        # only 'addressbook' active
        out, err, code = self.runCmdline(["server", "addressbook"],
                                         sessionFlags=[],
                                         preserveOutputOrder=True)
        self.assertEqual(err, None)
        self.assertEqual(0, code)
        out = self.stripSyncTime(out)
        self.assertEqualDiff('''[INFO] @default/calendar: inactive
[INFO @client] target side of local sync ready
[INFO @client] @client/calendar: inactive
[INFO @client] @client/addressbook: starting normal sync, two-way (peer is server)
[INFO @client] creating complete data backup of source addressbook before sync (enabled with dumpData and needed for printChanges)
@client data changes to be applied during synchronization:
*** @client/addressbook ***
no changes

[INFO] @default/addressbook: starting normal sync, two-way (peer is client)
[INFO] creating complete data backup of source addressbook before sync (enabled with dumpData and needed for printChanges)
@default data changes to be applied during synchronization:
*** @default/addressbook ***
no changes

[INFO] @default/addressbook: started
[INFO @client] @client/addressbook: started
[INFO] @default/addressbook: normal sync done successfully
[INFO @client] @client/addressbook: normal sync done successfully
[INFO @client] creating complete data backup after sync (enabled with dumpData and needed for printChanges)

Synchronization successful.

Changes applied during synchronization (@client):
+---------------|-----------------------|-----------------------|-CON-+
|               |        @client        |       @default        | FLI |
|        Source | NEW | MOD | DEL | ERR | NEW | MOD | DEL | ERR | CTS |
+---------------+-----+-----+-----+-----+-----+-----+-----+-----+-----+
|   addressbook |  0  |  0  |  0  |  0  |  0  |  0  |  0  |  0  |  0  |
|   two-way, 0 KB sent by client, 0 KB received                       |
|   item(s) in database backup: 0 before sync, 0 after it             |
+---------------+-----+-----+-----+-----+-----+-----+-----+-----+-----+
| start xxx, duration a:bcmin |
|               synchronization completed successfully                |
+---------------+-----+-----+-----+-----+-----+-----+-----+-----+-----+

Data modified @client during synchronization:
*** @client/addressbook ***
no changes

[INFO] creating complete data backup after sync (enabled with dumpData and needed for printChanges)

Synchronization successful.

Changes applied during synchronization:
+---------------|-----------------------|-----------------------|-CON-+
|               |       @default        |        @client        | FLI |
|        Source | NEW | MOD | DEL | ERR | NEW | MOD | DEL | ERR | CTS |
+---------------+-----+-----+-----+-----+-----+-----+-----+-----+-----+
|   addressbook |  0  |  0  |  0  |  0  |  0  |  0  |  0  |  0  |  0  |
|   two-way, 0 KB sent by client, 0 KB received                       |
|   item(s) in database backup: 0 before sync, 0 after it             |
+---------------+-----+-----+-----+-----+-----+-----+-----+-----+-----+
| start xxx, duration a:bcmin |
|               synchronization completed successfully                |
+---------------+-----+-----+-----+-----+-----+-----+-----+-----+-----+

Data modified @default during synchronization:
*** @default/addressbook ***
no changes

''', out)
        self.collectEvents()
        self.assertRegexpMatches(self.prettyPrintEvents(),
                                 r'''status: idle, .*
(.*\n)+status: running;waiting, 0, \{addressbook: \(two-way, running, 0\), calendar: \(none, idle, 0\)\}
(.*\n)*progress: 100, \{addressbook: \(, -1, -1, -1, -1, -1, -1\), calendar: \(, -1, -1, -1, -1, -1, -1\)\}
(.*\n)*status: done, .*''')
        self.checkSync(numReports=5)


class TestHTTP(CmdlineUtil, unittest.TestCase):
    """Test syncevo-http-server."""

    def setUp(self):
        self.setUpServer()
        self.setUpListeners(None)
        # All tests run with their own XDG root hierarchy.
        # Here are the config files.
        self.configdir = xdg_root + "/config/syncevolution"

        # Check whether we have multithreading support.
        out, err, code = self.runCmdline(["--sync-property", "SyncMLVersion=?", "--daemon=no"],
                                         sessionFlags=None,
                                         preserveOutputOrder=False)
        self.assertEqual(err, '')
        self.assertIn('REQUESTMAXTIME=', out)
        self.assertEqual(0, code)
        self.haveMultithreadedSyncEvo = 'is thread-safe' in out

    def run(self, result):
        # Runtime varies a lot when using valgrind, because
        # of the need to check an additional process. Allow
        # a lot more time when running under valgrind.
        self.runTest(result, own_xdg=True, own_home=True,
                     defTimeout=usingValgrind() and 600 or 20)

    def runHTTPServer(self):
        '''Pick port dynamically by running syncevo-http-server and checking whether it can listen on the port.'''
        for port in range(9000, 10000):
            httpserver = subprocess.Popen(['syncevo-http-server', '-q', 'http://127.0.0.1:%d' % port],
                                          stderr=subprocess.PIPE)
            while True:
                if httpserver.poll() != None:
                    error = httpserver.stderr.read()
                    self.assertIn('twisted.internet.error.CannotListenError', error)
                    break
                http = httplib.HTTPConnection('127.0.0.1', port)
                try:
                    http.connect()
                    return port
                except socket.error, ex:
                    self.assertEqual(errno.ECONNREFUSED, ex.errno)
                    time.sleep(0.5)

    def setUpConfigs(self,
                     port,
                     slowServer = False,
                     dumpData = True,
                     requestMaxTime = None,
                     retryDuration = None,
                     retryInterval = None,
                     username = "johndoe",
                     password = "foo",
                     clientID = "fake-device"):
        self.setUpSession("", flags = [ "all-configs" ])
        config = {"" : { "loglevel": "4",
                         "syncURL": "http://127.0.0.1:%d" % port,
                         "deviceID": clientID,
                         "useProxy": "0",
                         "username": username,
                         "password": password,
                         "RetryDuration": "1m", # abort syncing after 1 minute of no server response
                         },
                  "source/addressbook-client" : { "sync": "two-way",
                                                  "backend": "file",
                                                  "databaseFormat": "text/vcard",
                                                  "uri": slowServer and "addressbook-slow-server" or "addressbook-server",
                                                  "database": "file://" + xdg_root + "/client",
                                                  },
                  }
        if retryDuration != None:
            config[""]["RetryDuration"] = retryDuration
        if retryInterval != None:
            config[""]["RetryInterval"] = retryInterval
        self.session.SetNamedConfig("client", False, False, config, timeout=self.dbusTimeout)
        config = {"" : { "loglevel": "4",
                         "peerIsClient": "1",
                         "deviceId": clientID, # Has to be repeated because it is shared.
                         "remoteDeviceId": clientID,
                         "username": username,
                         "password": password,
                         "printChanges": "0",
                         "dumpData": dumpData and "1" or "0",
                         },
                  "source/addressbook-server": { "sync": "two-way",
                                                 "uri": "addressbook",
                                                 "backend": "file",
                                                 "databaseFormat": "text/vcard",
                                                 "database": "file://" + xdg_root + "/server" },
                  "source/addressbook-slow-server": { "sync": "two-way",
                                                      "uri": "addressbook",
                                                      "backend": "file",
                                                      "databaseFormat": "text/vcard",
                                                      "database": "file://" + xdg_root + "/server" },
                  }
        if requestMaxTime != None:
            config[""]["SyncMLVersion"] = "requestmaxtime=%d" % requestMaxTime
        self.session.SetNamedConfig("server", False, False, config, timeout=self.dbusTimeout)
        self.session.Detach()

    @timeout(200)
    def testHTTP(self):
        """TestHTTP.testHTTP - run simple HTTP-based sync"""
        port = self.runHTTPServer()
        self.setUpConfigs(port)
        out, err, code = self.runCmdline(["--sync", "slow", "--daemon=no", "client"],
                                         sessionFlags=None,
                                         preserveOutputOrder=True)
        self.assertEqual(err, None)
        self.assertEqual(0, code)

    @timeout(200)
    @property("ENV", "SYNCEVOLUTION_FILE_SOURCE_DELAY_OPEN_addressbook-slow-server=60")
    def testTimeoutOpen(self):
        """TestHTTP.testTimeoutOpen - slow down server in open, let client abort"""
        port = self.runHTTPServer()
        self.setUpConfigs(port, slowServer=True, retryDuration="10s", retryInterval="5m")
        out, err, code = self.runCmdline(["--sync", "slow", "--daemon=no", "client"],
                                         sessionFlags=None,
                                         expectSuccess=False,
                                         preserveOutputOrder=False)
        self.assertNotIn('resend previous message', err)
        self.assertIn('transport problem: timeout, retry period exceeded', err)
        self.assertEqual(1, code)

    @timeout(200)
    @property("ENV", "SYNCEVOLUTION_FILE_SOURCE_DELAY_LISTALL_addressbook-slow-server=60")
    def testTimeoutListAll(self):
        """TestHTTP.testTimeoutListAll - slow down server in listAll, let client abort"""
        port = self.runHTTPServer()
        self.setUpConfigs(port, slowServer=True, retryDuration="10s", retryInterval="5m")
        out, err, code = self.runCmdline(["--sync", "slow", "--daemon=no", "client"],
                                         sessionFlags=None,
                                         expectSuccess=False,
                                         preserveOutputOrder=False)
        self.assertNotIn('resend previous message', err)
        self.assertIn('transport problem: timeout, retry period exceeded', err)
        self.assertEqual(1, code)

    @timeout(200)
    @property("ENV", "SYNCEVOLUTION_FILE_SOURCE_DELAY_OPEN_addressbook-slow-server=140")
    def testTimeoutSlow(self):
        """TestHTTP.testTimeoutSlow - slow down server in open with disabled keep-alive messages, let client abort"""
        port = self.runHTTPServer()
        self.setUpConfigs(port, slowServer=True, retryDuration="130s", retryInterval="5m",
                          requestMaxTime=0)
        out, err, code = self.runCmdline(["--sync", "slow", "--daemon=no", "client"],
                                         sessionFlags=None,
                                         expectSuccess=False,
                                         preserveOutputOrder=False)
        self.assertNotIn('resend previous message', err)
        self.assertIn('transport problem: timeout, retry period exceeded', err)
        self.assertEqual(1, code)

    @timeout(200)
    @property("ENV", "SYNCEVOLUTION_FILE_SOURCE_DELAY_OPEN_addressbook-slow-server=140")
    def testThreadedOpen(self):
        """TestHTTP.testThreadedOpen - slow down server in open, let server send keep-alive messages at default rate"""
        if not self.haveMultithreadedSyncEvo:
            return
        port = self.runHTTPServer()
        self.setUpConfigs(port, slowServer=True, retryDuration="130", retryInterval="5m")
        out, err, code = self.runCmdline(["--sync", "slow", "--daemon=no", "client"],
                                         sessionFlags=None,
                                         expectSuccess=True,
                                         preserveOutputOrder=True)
        self.assertEqual(err, None)
        self.assertEqual(0, code)

    @timeout(200)
    @property("ENV", "SYNCEVOLUTION_FILE_SOURCE_DELAY_LISTALL_addressbook-slow-server=140")
    def testThreadedListAll(self):
        """TestHTTP.testThreadedListAll - slow down server in listAll, let server send keep-alive messages at default rate"""
        if not self.haveMultithreadedSyncEvo:
            return
        port = self.runHTTPServer()
        self.setUpConfigs(port, slowServer=True, retryDuration="130s", retryInterval="5m",
                          dumpData=False) # listAll() gets called too often otherwise, which slows down the test too much.
        out, err, code = self.runCmdline(["--sync", "slow", "--daemon=no", "client"],
                                         sessionFlags=None,
                                         expectSuccess=True,
                                         preserveOutputOrder=True)
        self.assertEqual(err, None)
        self.assertEqual(0, code)

    @timeout(200)
    @property("ENV", "SYNCEVOLUTION_FILE_SOURCE_DELAY_OPEN_addressbook-slow-server=40")
    def testThreadedOpenQuick(self):
        """TestHTTP.testThreadedOpenQuick - slow down server in open, let server send keep-alive messages at faster rate"""
        port = self.runHTTPServer()
        self.setUpConfigs(port, slowServer=True, retryDuration="30s", retryInterval="5m",
                          requestMaxTime=10)
        out, err, code = self.runCmdline(["--sync", "slow", "--daemon=no", "client"],
                                         sessionFlags=None,
                                         expectSuccess=True,
                                         preserveOutputOrder=True)
        self.assertEqual(err, None)
        self.assertEqual(0, code)

    @timeout(200)
    @property("ENV", "SYNCEVOLUTION_FILE_SOURCE_DELAY_LISTALL_addressbook-slow-server=40")
    def testThreadedListAllQuick(self):
        """TestHTTP.testThreadedListAllQuick - slow down server in listAll, let server send keep-alive messages at faster rate"""
        port = self.runHTTPServer()
        self.setUpConfigs(port, slowServer=True, retryDuration="30s", retryInterval="5m",
                          requestMaxTime=10)
        out, err, code = self.runCmdline(["--sync", "slow", "--daemon=no", "client"],
                                         sessionFlags=None,
                                         expectSuccess=True,
                                         preserveOutputOrder=True)
        self.assertEqual(err, None)
        self.assertEqual(0, code)


if __name__ == '__main__':
    unittest.main()