unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
blob 2a4ef740a04d02ed2bba2e97796c7cbcf055c6aa 101220 bytes (raw)
name: test/lisp/net/tramp-tests.el 	 # note: path name is non-authoritative(*)

   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
 
;;; tramp-tests.el --- Tests of remote file access  -*- lexical-binding:t -*-

;; Copyright (C) 2013-2017 Free Software Foundation, Inc.

;; Author: Michael Albinus <michael.albinus@gmx.de>

;; This program is free software: you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
;; published by the Free Software Foundation, either version 3 of the
;; License, or (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;; General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see `http://www.gnu.org/licenses/'.

;;; Commentary:

;; The tests require a recent ert.el from Emacs 24.4.

;; Some of the tests require access to a remote host files.  Since
;; this could be problematic, a mock-up connection method "mock" is
;; used.  Emulating a remote connection, it simply calls "sh -i".
;; Tramp's file name handlers still run, so this test is sufficient
;; except for connection establishing.

;; If you want to test a real Tramp connection, set
;; $REMOTE_TEMPORARY_FILE_DIRECTORY to a suitable value in order to
;; overwrite the default value.  If you want to skip tests accessing a
;; remote host, set this environment variable to "/dev/null" or
;; whatever is appropriate on your system.

;; A whole test run can be performed calling the command `tramp-test-all'.

;;; Code:

(require 'dired)
(require 'ert)
(require 'tramp)
(require 'vc)
(require 'vc-bzr)
(require 'vc-git)
(require 'vc-hg)

(declare-function tramp-find-executable "tramp-sh")
(declare-function tramp-get-remote-path "tramp-sh")
(declare-function tramp-get-remote-stat "tramp-sh")
(declare-function tramp-get-remote-perl "tramp-sh")
(defvar auto-save-file-name-transforms)
(defvar tramp-copy-size-limit)
(defvar tramp-persistency-file-name)
(defvar tramp-remote-process-environment)

;; There is no default value on w32 systems, which could work out of the box.
(defconst tramp-test-temporary-file-directory
  (cond
   ((getenv "REMOTE_TEMPORARY_FILE_DIRECTORY"))
   ((eq system-type 'windows-nt) null-device)
   (t (add-to-list
       'tramp-methods
       '("mock"
	 (tramp-login-program        "sh")
	 (tramp-login-args           (("-i")))
	 (tramp-remote-shell         "/bin/sh")
	 (tramp-remote-shell-args    ("-c"))
	 (tramp-connection-timeout   10)))
      (format "/mock::%s" temporary-file-directory)))
  "Temporary directory for Tramp tests.")

(setq password-cache-expiry nil
      tramp-verbose 0
      tramp-copy-size-limit nil
      tramp-message-show-message nil
      tramp-persistency-file-name nil)

;; This shall happen on hydra only.
(when (getenv "NIX_STORE")
  (add-to-list 'tramp-remote-path 'tramp-own-remote-path))

(defvar tramp--test-expensive-test
  (null
   (string-equal (getenv "SELECTOR") "(quote (not (tag :expensive-test)))"))
  "Whether expensive tests are run.")

(defvar tramp--test-enabled-checked nil
  "Cached result of `tramp--test-enabled'.
If the function did run, the value is a cons cell, the `cdr'
being the result.")

(defun tramp--test-enabled ()
  "Whether remote file access is enabled."
  (unless (consp tramp--test-enabled-checked)
    (setq
     tramp--test-enabled-checked
     (cons
      t (ignore-errors
	  (and
	   (file-remote-p tramp-test-temporary-file-directory)
	   (file-directory-p tramp-test-temporary-file-directory)
	   (file-writable-p tramp-test-temporary-file-directory))))))

  (when (cdr tramp--test-enabled-checked)
    ;; Cleanup connection.
    (ignore-errors
      (tramp-cleanup-connection
       (tramp-dissect-file-name tramp-test-temporary-file-directory)
       nil 'keep-password)))

  ;; Return result.
  (cdr tramp--test-enabled-checked))

(defun tramp--test-make-temp-name (&optional local quoted)
  "Create a temporary file name for test.
If LOCAL is non-nil, a local file is created.
If QUOTED is non-nil, the local part of the file is quoted."
  (funcall
   (if quoted 'tramp-compat-file-name-quote 'identity)
   (expand-file-name
    (make-temp-name "tramp-test")
    (if local temporary-file-directory tramp-test-temporary-file-directory))))

(defmacro tramp--instrument-test-case (verbose &rest body)
  "Run BODY with `tramp-verbose' equal VERBOSE.
Print the the content of the Tramp debug buffer, if BODY does not
eval properly in `should' or `should-not'.  `should-error' is not
handled properly.  BODY shall not contain a timeout."
  (declare (indent 1) (debug (natnump body)))
  `(let ((tramp-verbose ,verbose)
	 (tramp-debug-on-error t)
	 (debug-ignored-errors
	  (cons "^make-symbolic-link not supported$" debug-ignored-errors)))
     (unwind-protect
	 (progn ,@body)
       (when (> tramp-verbose 3)
	 (with-parsed-tramp-file-name tramp-test-temporary-file-directory nil
	   (with-current-buffer (tramp-get-connection-buffer v)
	     (message "%s" (buffer-string)))
	   (with-current-buffer (tramp-get-debug-buffer v)
	     (message "%s" (buffer-string))))))))

(ert-deftest tramp-test00-availability ()
  "Test availability of Tramp functions."
  :expected-result (if (tramp--test-enabled) :passed :failed)
  (message "Remote directory: `%s'" tramp-test-temporary-file-directory)
  (should (ignore-errors
	    (and
	     (file-remote-p tramp-test-temporary-file-directory)
	     (file-directory-p tramp-test-temporary-file-directory)
	     (file-writable-p tramp-test-temporary-file-directory)))))

(ert-deftest tramp-test01-file-name-syntax ()
  "Check remote file name syntax."
  ;; Simple cases.
  (should (tramp-tramp-file-p "/method::"))
  (should (tramp-tramp-file-p "/method:host:"))
  (should (tramp-tramp-file-p "/method:user@:"))
  (should (tramp-tramp-file-p "/method:user@host:"))
  (should (tramp-tramp-file-p "/method:user@email@host:"))

  ;; Using a port.
  (should (tramp-tramp-file-p "/method:host#1234:"))
  (should (tramp-tramp-file-p "/method:user@host#1234:"))

  ;; Using an IPv4 address.
  (should (tramp-tramp-file-p "/method:1.2.3.4:"))
  (should (tramp-tramp-file-p "/method:user@1.2.3.4:"))

  ;; Using an IPv6 address.
  (should (tramp-tramp-file-p "/method:[::1]:"))
  (should (tramp-tramp-file-p "/method:user@[::1]:"))

  ;; Local file name part.
  (should (tramp-tramp-file-p "/method:::"))
  (should (tramp-tramp-file-p "/method::/:"))
  (should (tramp-tramp-file-p "/method::/path/to/file"))
  (should (tramp-tramp-file-p "/method::/:/path/to/file"))
  (should (tramp-tramp-file-p "/method::file"))
  (should (tramp-tramp-file-p "/method::/:file"))

  ;; Multihop.
  (should (tramp-tramp-file-p "/method1:|method2::"))
  (should (tramp-tramp-file-p "/method1:host1|method2:host2:"))
  (should (tramp-tramp-file-p "/method1:user1@host1|method2:user2@host2:"))
  (should (tramp-tramp-file-p
	   "/method1:user1@host1|method2:user2@host2|method3:user3@host3:"))

  ;; No strings.
  (should-not (tramp-tramp-file-p nil))
  (should-not (tramp-tramp-file-p 'symbol))
  ;; Ange-ftp syntax.
  (should-not (tramp-tramp-file-p "/host:"))
  (should-not (tramp-tramp-file-p "/user@host:"))
  (should-not (tramp-tramp-file-p "/1.2.3.4:"))
  (should-not (tramp-tramp-file-p "/[]:"))
  (should-not (tramp-tramp-file-p "/[::1]:"))
  (should-not (tramp-tramp-file-p "/host:/:"))
  (should-not (tramp-tramp-file-p "/host1|host2:"))
  (should-not (tramp-tramp-file-p "/user1@host1|user2@host2:"))
  ;; Quote with "/:" suppresses file name handlers.
  (should-not (tramp-tramp-file-p "/::"))
  (should-not (tramp-tramp-file-p "/:@:"))
  (should-not (tramp-tramp-file-p "/:[]:"))
  ;; Methods shall be at least two characters on MS Windows, except
  ;; the default method.
  (let ((system-type 'windows-nt))
    (should-not (tramp-tramp-file-p "/c:/path/to/file"))
    (should-not (tramp-tramp-file-p "/c::/path/to/file"))
    (should (tramp-tramp-file-p "/-::/path/to/file")))
  (let ((system-type 'gnu/linux))
    (should (tramp-tramp-file-p "/-:h:/path/to/file"))
    (should (tramp-tramp-file-p "/m::/path/to/file"))))

(ert-deftest tramp-test02-file-name-dissect ()
  "Check remote file name components."
  (let ((tramp-default-method "default-method")
	(tramp-default-user "default-user")
	(tramp-default-host "default-host"))
    ;; Expand `tramp-default-user' and `tramp-default-host'.
    (should (string-equal
	     (file-remote-p "/method::")
	     (format "/%s:%s@%s:" "method" "default-user" "default-host")))
    (should (string-equal (file-remote-p "/method::" 'method) "method"))
    (should (string-equal (file-remote-p "/method::" 'user) "default-user"))
    (should (string-equal (file-remote-p "/method::" 'host) "default-host"))
    (should (string-equal (file-remote-p "/method::" 'localname) ""))
    (should (string-equal (file-remote-p "/method::" 'hop) nil))

    ;; Expand `tramp-default-method' and `tramp-default-user'.
    (should (string-equal
	     (file-remote-p "/-:host:")
	     (format "/%s:%s@%s:" "default-method" "default-user" "host")))
    (should (string-equal (file-remote-p "/-:host:" 'method) "default-method"))
    (should (string-equal (file-remote-p "/-:host:" 'user) "default-user"))
    (should (string-equal (file-remote-p "/-:host:" 'host) "host"))
    (should (string-equal (file-remote-p "/-:host:" 'localname) ""))
    (should (string-equal (file-remote-p "/-:host:" 'hop) nil))

    ;; Expand `tramp-default-method' and `tramp-default-host'.
    (should (string-equal
	     (file-remote-p "/-:user@:")
	     (format "/%s:%s@%s:" "default-method" "user" "default-host")))
    (should (string-equal (file-remote-p "/-:user@:" 'method) "default-method"))
    (should (string-equal (file-remote-p "/-:user@:" 'user) "user"))
    (should (string-equal (file-remote-p "/-:user@:" 'host) "default-host"))
    (should (string-equal (file-remote-p "/-:user@:" 'localname) ""))
    (should (string-equal (file-remote-p "/-:user@:" 'hop) nil))

    ;; Expand `tramp-default-method'.
    (should (string-equal
	     (file-remote-p "/-:user@host:")
	     (format "/%s:%s@%s:" "default-method" "user" "host")))
    (should (string-equal
	     (file-remote-p "/-:user@host:" 'method) "default-method"))
    (should (string-equal (file-remote-p "/-:user@host:" 'user) "user"))
    (should (string-equal (file-remote-p "/-:user@host:" 'host) "host"))
    (should (string-equal (file-remote-p "/-:user@host:" 'localname) ""))
    (should (string-equal (file-remote-p "/-:user@host:" 'hop) nil))

    ;; Expand `tramp-default-user'.
    (should (string-equal
	     (file-remote-p "/method:host:")
	     (format "/%s:%s@%s:" "method" "default-user" "host")))
    (should (string-equal (file-remote-p "/method:host:" 'method) "method"))
    (should (string-equal (file-remote-p "/method:host:" 'user) "default-user"))
    (should (string-equal (file-remote-p "/method:host:" 'host) "host"))
    (should (string-equal (file-remote-p "/method:host:" 'localname) ""))
    (should (string-equal (file-remote-p "/method:host:" 'hop) nil))

    ;; Expand `tramp-default-host'.
    (should (string-equal
	     (file-remote-p "/method:user@:")
	     (format "/%s:%s@%s:" "method" "user" "default-host")))
    (should (string-equal (file-remote-p "/method:user@:" 'method) "method"))
    (should (string-equal (file-remote-p "/method:user@:" 'user) "user"))
    (should (string-equal (file-remote-p "/method:user@:" 'host)
			  "default-host"))
    (should (string-equal (file-remote-p "/method:user@:" 'localname) ""))
    (should (string-equal (file-remote-p "/method:user@:" 'hop) nil))

    ;; No expansion.
    (should (string-equal
	     (file-remote-p "/method:user@host:")
	     (format "/%s:%s@%s:" "method" "user" "host")))
    (should (string-equal
	     (file-remote-p "/method:user@host:" 'method) "method"))
    (should (string-equal (file-remote-p "/method:user@host:" 'user) "user"))
    (should (string-equal (file-remote-p "/method:user@host:" 'host) "host"))
    (should (string-equal (file-remote-p "/method:user@host:" 'localname) ""))
    (should (string-equal (file-remote-p "/method:user@host:" 'hop) nil))

    ;; No expansion.
    (should (string-equal
	     (file-remote-p "/method:user@email@host:")
	     (format "/%s:%s@%s:" "method" "user@email" "host")))
    (should (string-equal
	     (file-remote-p "/method:user@email@host:" 'method) "method"))
    (should (string-equal
	     (file-remote-p "/method:user@email@host:" 'user) "user@email"))
    (should (string-equal
	     (file-remote-p "/method:user@email@host:" 'host) "host"))
    (should (string-equal
	     (file-remote-p "/method:user@email@host:" 'localname) ""))
    (should (string-equal
	     (file-remote-p "/method:user@email@host:" 'hop) nil))

    ;; Expand `tramp-default-method' and `tramp-default-user'.
    (should (string-equal
	     (file-remote-p "/-:host#1234:")
	     (format "/%s:%s@%s:" "default-method" "default-user" "host#1234")))
    (should (string-equal
	     (file-remote-p "/-:host#1234:" 'method) "default-method"))
    (should (string-equal (file-remote-p "/-:host#1234:" 'user) "default-user"))
    (should (string-equal (file-remote-p "/-:host#1234:" 'host) "host#1234"))
    (should (string-equal (file-remote-p "/-:host#1234:" 'localname) ""))
    (should (string-equal (file-remote-p "/-:host#1234:" 'hop) nil))

    ;; Expand `tramp-default-method'.
    (should (string-equal
	     (file-remote-p "/-:user@host#1234:")
	     (format "/%s:%s@%s:" "default-method" "user" "host#1234")))
    (should (string-equal
	     (file-remote-p "/-:user@host#1234:" 'method) "default-method"))
    (should (string-equal (file-remote-p "/-:user@host#1234:" 'user) "user"))
    (should (string-equal (file-remote-p "/-:user@host#1234:" 'host) "host#1234"))
    (should (string-equal (file-remote-p "/-:user@host#1234:" 'localname) ""))
    (should (string-equal (file-remote-p "/-:user@host#1234:" 'hop) nil))

    ;; Expand `tramp-default-user'.
    (should (string-equal
	     (file-remote-p "/method:host#1234:")
	     (format "/%s:%s@%s:" "method" "default-user" "host#1234")))
    (should (string-equal
	     (file-remote-p "/method:host#1234:" 'method) "method"))
    (should (string-equal
	     (file-remote-p "/method:host#1234:" 'user) "default-user"))
    (should (string-equal
	     (file-remote-p "/method:host#1234:" 'host) "host#1234"))
    (should (string-equal (file-remote-p "/method:host#1234:" 'localname) ""))
    (should (string-equal (file-remote-p "/method:host#1234:" 'hop) nil))

    ;; No expansion.
    (should (string-equal
	     (file-remote-p "/method:user@host#1234:")
	     (format "/%s:%s@%s:" "method" "user" "host#1234")))
    (should (string-equal
	     (file-remote-p "/method:user@host#1234:" 'method) "method"))
    (should (string-equal
	     (file-remote-p "/method:user@host#1234:" 'user) "user"))
    (should (string-equal
	     (file-remote-p "/method:user@host#1234:" 'host) "host#1234"))
    (should (string-equal
	     (file-remote-p "/method:user@host#1234:" 'localname) ""))
    (should (string-equal
	     (file-remote-p "/method:user@host#1234:" 'hop) nil))

    ;; Expand `tramp-default-method' and `tramp-default-user'.
    (should (string-equal
	     (file-remote-p "/-:1.2.3.4:")
	     (format "/%s:%s@%s:" "default-method" "default-user" "1.2.3.4")))
    (should (string-equal (file-remote-p "/-:1.2.3.4:" 'method) "default-method"))
    (should (string-equal (file-remote-p "/-:1.2.3.4:" 'user) "default-user"))
    (should (string-equal (file-remote-p "/-:1.2.3.4:" 'host) "1.2.3.4"))
    (should (string-equal (file-remote-p "/-:1.2.3.4:" 'localname) ""))
    (should (string-equal (file-remote-p "/-:1.2.3.4:" 'hop) nil))

    ;; Expand `tramp-default-method'.
    (should (string-equal
	     (file-remote-p "/-:user@1.2.3.4:")
	     (format "/%s:%s@%s:" "default-method" "user" "1.2.3.4")))
    (should (string-equal
	     (file-remote-p "/-:user@1.2.3.4:" 'method) "default-method"))
    (should (string-equal (file-remote-p "/-:user@1.2.3.4:" 'user) "user"))
    (should (string-equal (file-remote-p "/-:user@1.2.3.4:" 'host) "1.2.3.4"))
    (should (string-equal (file-remote-p "/-:user@1.2.3.4:" 'localname) ""))
    (should (string-equal (file-remote-p "/-:user@1.2.3.4:" 'hop) nil))

    ;; Expand `tramp-default-user'.
    (should (string-equal
	     (file-remote-p "/method:1.2.3.4:")
	     (format "/%s:%s@%s:" "method" "default-user" "1.2.3.4")))
    (should (string-equal (file-remote-p "/method:1.2.3.4:" 'method) "method"))
    (should (string-equal
	     (file-remote-p "/method:1.2.3.4:" 'user) "default-user"))
    (should (string-equal (file-remote-p "/method:1.2.3.4:" 'host) "1.2.3.4"))
    (should (string-equal (file-remote-p "/method:1.2.3.4:" 'localname) ""))
    (should (string-equal (file-remote-p "/method:1.2.3.4:" 'hop) nil))

    ;; No expansion.
    (should (string-equal
	     (file-remote-p "/method:user@1.2.3.4:")
	     (format "/%s:%s@%s:" "method" "user" "1.2.3.4")))
    (should (string-equal
	     (file-remote-p "/method:user@1.2.3.4:" 'method) "method"))
    (should (string-equal (file-remote-p "/method:user@1.2.3.4:" 'user) "user"))
    (should (string-equal
	     (file-remote-p "/method:user@1.2.3.4:" 'host) "1.2.3.4"))
    (should (string-equal
	     (file-remote-p "/method:user@1.2.3.4:" 'localname) ""))
    (should (string-equal
	     (file-remote-p "/method:user@1.2.3.4:" 'hop) nil))

    ;; Expand `tramp-default-method', `tramp-default-user' and
    ;; `tramp-default-host'.
    (should (string-equal
	     (file-remote-p "/-:[]:")
	     (format
	      "/%s:%s@%s:" "default-method" "default-user" "default-host")))
    (should (string-equal (file-remote-p "/-:[]:" 'method) "default-method"))
    (should (string-equal (file-remote-p "/-:[]:" 'user) "default-user"))
    (should (string-equal (file-remote-p "/-:[]:" 'host) "default-host"))
    (should (string-equal (file-remote-p "/-:[]:" 'localname) ""))
    (should (string-equal (file-remote-p "/-:[]:" 'hop) nil))

    ;; Expand `tramp-default-method' and `tramp-default-user'.
    (let ((tramp-default-host "::1"))
      (should (string-equal
	       (file-remote-p "/-:[]:")
	       (format "/%s:%s@%s:" "default-method" "default-user" "[::1]")))
      (should (string-equal (file-remote-p "/-:[]:" 'method) "default-method"))
      (should (string-equal (file-remote-p "/-:[]:" 'user) "default-user"))
      (should (string-equal (file-remote-p "/-:[]:" 'host) "::1"))
      (should (string-equal (file-remote-p "/-:[]:" 'localname) ""))
      (should (string-equal (file-remote-p "/-:[]:" 'hop) nil)))

    ;; Expand `tramp-default-method' and `tramp-default-user'.
    (should (string-equal
	     (file-remote-p "/-:[::1]:")
	     (format "/%s:%s@%s:" "default-method" "default-user" "[::1]")))
    (should (string-equal (file-remote-p "/-:[::1]:" 'method) "default-method"))
    (should (string-equal (file-remote-p "/-:[::1]:" 'user) "default-user"))
    (should (string-equal (file-remote-p "/-:[::1]:" 'host) "::1"))
    (should (string-equal (file-remote-p "/-:[::1]:" 'localname) ""))
    (should (string-equal (file-remote-p "/-:[::1]:" 'hop) nil))

    ;; Expand `tramp-default-method'.
    (should (string-equal
	     (file-remote-p "/-:user@[::1]:")
	     (format "/%s:%s@%s:" "default-method" "user" "[::1]")))
    (should (string-equal
	     (file-remote-p "/-:user@[::1]:" 'method) "default-method"))
    (should (string-equal (file-remote-p "/-:user@[::1]:" 'user) "user"))
    (should (string-equal (file-remote-p "/-:user@[::1]:" 'host) "::1"))
    (should (string-equal (file-remote-p "/-:user@[::1]:" 'localname) ""))
    (should (string-equal (file-remote-p "/-:user@[::1]:" 'hop) nil))

    ;; Expand `tramp-default-user'.
    (should (string-equal
	     (file-remote-p "/method:[::1]:")
	     (format "/%s:%s@%s:" "method" "default-user" "[::1]")))
    (should (string-equal (file-remote-p "/method:[::1]:" 'method) "method"))
    (should (string-equal
	     (file-remote-p "/method:[::1]:" 'user) "default-user"))
    (should (string-equal (file-remote-p "/method:[::1]:" 'host) "::1"))
    (should (string-equal (file-remote-p "/method:[::1]:" 'localname) ""))
    (should (string-equal (file-remote-p "/method:[::1]:" 'hop) nil))

    ;; No expansion.
    (should (string-equal
	     (file-remote-p "/method:user@[::1]:")
	     (format "/%s:%s@%s:" "method" "user" "[::1]")))
    (should (string-equal
	     (file-remote-p "/method:user@[::1]:" 'method) "method"))
    (should (string-equal (file-remote-p "/method:user@[::1]:" 'user) "user"))
    (should (string-equal (file-remote-p "/method:user@[::1]:" 'host) "::1"))
    (should (string-equal
	     (file-remote-p "/method:user@[::1]:" 'localname) ""))
    (should (string-equal (file-remote-p "/method:user@[::1]:" 'hop) nil))

    ;; Local file name part.
    (should (string-equal (file-remote-p "/-:host:/:" 'localname) "/:"))
    (should (string-equal (file-remote-p "/method:::" 'localname) ":"))
    (should (string-equal (file-remote-p "/method:: " 'localname) " "))
    (should (string-equal (file-remote-p "/method::file" 'localname) "file"))
    (should (string-equal
	     (file-remote-p "/method::/path/to/file" 'localname)
	     "/path/to/file"))

    ;; Multihop.
    (should
     (string-equal
      (file-remote-p "/method1:user1@host1|method2:user2@host2:/path/to/file")
      (format "/%s:%s@%s|%s:%s@%s:"
	      "method1" "user1" "host1" "method2" "user2" "host2")))
    (should
     (string-equal
      (file-remote-p
       "/method1:user1@host1|method2:user2@host2:/path/to/file" 'method)
      "method2"))
    (should
     (string-equal
      (file-remote-p
       "/method1:user1@host1|method2:user2@host2:/path/to/file" 'user)
      "user2"))
    (should
     (string-equal
      (file-remote-p
       "/method1:user1@host1|method2:user2@host2:/path/to/file" 'host)
      "host2"))
    (should
     (string-equal
      (file-remote-p
       "/method1:user1@host1|method2:user2@host2:/path/to/file" 'localname)
      "/path/to/file"))
    (should
     (string-equal
      (file-remote-p
       "/method1:user1@host1|method2:user2@host2:/path/to/file" 'hop)
      (format "%s:%s@%s|"
	      "method1" "user1" "host1")))

    (should
     (string-equal
      (file-remote-p
       (concat
	"/method1:user1@host1"
	"|method2:user2@host2"
	"|method3:user3@host3:/path/to/file"))
      (format "/%s:%s@%s|%s:%s@%s|%s:%s@%s:"
	      "method1" "user1" "host1"
	      "method2" "user2" "host2"
	      "method3" "user3" "host3")))
    (should
     (string-equal
      (file-remote-p
       (concat
	"/method1:user1@host1"
	"|method2:user2@host2"
	"|method3:user3@host3:/path/to/file")
       'method)
      "method3"))
    (should
     (string-equal
      (file-remote-p
       (concat
	"/method1:user1@host1"
	"|method2:user2@host2"
	"|method3:user3@host3:/path/to/file")
       'user)
      "user3"))
    (should
     (string-equal
      (file-remote-p
       (concat
	"/method1:user1@host1"
	"|method2:user2@host2"
	"|method3:user3@host3:/path/to/file")
       'host)
      "host3"))
    (should
     (string-equal
      (file-remote-p
       (concat
	"/method1:user1@host1"
	"|method2:user2@host2"
	"|method3:user3@host3:/path/to/file")
       'localname)
      "/path/to/file"))
    (should
     (string-equal
      (file-remote-p
       (concat
	"/method1:user1@host1"
	"|method2:user2@host2"
	"|method3:user3@host3:/path/to/file")
       'hop)
      (format "%s:%s@%s|%s:%s@%s|"
	      "method1" "user1" "host1" "method2" "user2" "host2")))))

(ert-deftest tramp-test03-file-name-defaults ()
  "Check default values for some methods."
  ;; Default values in tramp-adb.el.
  (should (string-equal (file-remote-p "/adb::" 'host) ""))
  ;; Default values in tramp-ftp.el.
  (should (string-equal (file-remote-p "/-:ftp.host:" 'method) "ftp"))
  (dolist (u '("ftp" "anonymous"))
    (should (string-equal (file-remote-p (format "/-:%s@:" u) 'method) "ftp")))
  ;; Default values in tramp-gvfs.el.
  (when (and (load "tramp-gvfs" 'noerror 'nomessage)
	     (symbol-value 'tramp-gvfs-enabled))
    (should (string-equal (file-remote-p "/synce::" 'user) nil)))
  ;; Default values in tramp-sh.el.
  (dolist (h `("127.0.0.1" "[::1]" "localhost" "localhost6" ,(system-name)))
    (should
     (string-equal (file-remote-p (format "/-:root@%s:" h) 'method) "su")))
  (dolist (m '("su" "sudo" "ksu"))
    (should (string-equal (file-remote-p (format "/%s::" m) 'user) "root")))
  (dolist (m '("rcp" "remcp" "rsh" "telnet" "krlogin" "fcp"))
    (should
     (string-equal (file-remote-p (format "/%s::" m) 'user) (user-login-name))))
  ;; Default values in tramp-smb.el.
  (should (string-equal (file-remote-p "/-:user%domain@host:" 'method) "smb"))
  (should (string-equal (file-remote-p "/smb::" 'user) nil)))

(ert-deftest tramp-test04-substitute-in-file-name ()
  "Check `substitute-in-file-name'."
  (should (string-equal (substitute-in-file-name "/method:host://foo") "/foo"))
  (should
   (string-equal
    (substitute-in-file-name "/method:host:/path//foo") "/method:host:/foo"))
  (should
   (string-equal (substitute-in-file-name "/method:host:/path///foo") "/foo"))
  ;; Quoting local part.
  (should
   (string-equal
    (substitute-in-file-name "/method:host:/://foo") "/method:host:/://foo"))
  (should
   (string-equal
    (substitute-in-file-name "/method:host:/:/path//foo")
    "/method:host:/:/path//foo"))
  (should
   (string-equal
    (substitute-in-file-name "/method:host:/:/path///foo")
    "/method:host:/:/path///foo"))

  (should
   (string-equal
    (substitute-in-file-name "/method:host:/path/~/foo") "/method:host:~/foo"))
  (should
   (string-equal (substitute-in-file-name "/method:host:/path//~/foo") "~/foo"))
  ;; Quoting local part.
  (should
   (string-equal
    (substitute-in-file-name "/method:host:/:/path/~/foo")
    "/method:host:/:/path/~/foo"))
  (should
   (string-equal
    (substitute-in-file-name "/method:host:/:/path//~/foo")
    "/method:host:/:/path//~/foo"))

  (let (process-environment)
    (should
     (string-equal
      (substitute-in-file-name "/method:host:/path/$FOO")
      "/method:host:/path/$FOO"))
    (setenv "FOO" "bla")
    (should
     (string-equal
      (substitute-in-file-name "/method:host:/path/$FOO")
      "/method:host:/path/bla"))
    (should
     (string-equal
      (substitute-in-file-name "/method:host:/path/$$FOO")
      "/method:host:/path/$FOO"))
    ;; Quoting local part.
    (should
     (string-equal
      (substitute-in-file-name "/method:host:/:/path/$FOO")
      "/method:host:/:/path/$FOO"))
    (setenv "FOO" "bla")
    (should
     (string-equal
      (substitute-in-file-name "/method:host:/:/path/$FOO")
      "/method:host:/:/path/$FOO"))
    (should
     (string-equal
      (substitute-in-file-name "/method:host:/:/path/$$FOO")
      "/method:host:/:/path/$$FOO"))))

(ert-deftest tramp-test05-expand-file-name ()
  "Check `expand-file-name'."
  (should
   (string-equal
    (expand-file-name "/method:host:/path/./file") "/method:host:/path/file"))
  (should
   (string-equal
    (expand-file-name "/method:host:/path/../file") "/method:host:/file"))
  ;; Quoting local part.
  (should
   (string-equal
    (expand-file-name "/method:host:/:/path/./file")
    "/method:host:/:/path/file"))
  (should
   (string-equal
    (expand-file-name "/method:host:/:/path/../file") "/method:host:/:/file"))
  (should
   (string-equal
    (expand-file-name "/method:host:/:/~/path/./file")
    "/method:host:/:/~/path/file")))

(ert-deftest tramp-test06-directory-file-name ()
  "Check `directory-file-name'.
This checks also `file-name-as-directory', `file-name-directory',
`file-name-nondirectory' and `unhandled-file-name-directory'."
  (should
   (string-equal
    (directory-file-name "/method:host:/path/to/file")
    "/method:host:/path/to/file"))
  (should
   (string-equal
    (directory-file-name "/method:host:/path/to/file/")
    "/method:host:/path/to/file"))
  (should
   (string-equal
    (file-name-as-directory "/method:host:/path/to/file")
    "/method:host:/path/to/file/"))
  (should
   (string-equal
    (file-name-as-directory "/method:host:/path/to/file/")
    "/method:host:/path/to/file/"))
  (should
   (string-equal
    (file-name-directory "/method:host:/path/to/file")
    "/method:host:/path/to/"))
  (should
   (string-equal
    (file-name-directory "/method:host:/path/to/file/")
    "/method:host:/path/to/file/"))
  (should
   (string-equal (file-name-nondirectory "/method:host:/path/to/file") "file"))
  (should
   (string-equal (file-name-nondirectory "/method:host:/path/to/file/") ""))
  (should-not
   (unhandled-file-name-directory "/method:host:/path/to/file"))

  ;; Bug#10085.
  (when (tramp--test-enabled) ;; Packages like tramp-gvfs.el might be disabled.
    (dolist (n-e '(nil t))
      ;; We must clear `tramp-default-method'.  On hydra, it is "ftp",
      ;; which ruins the tests.
      (let ((non-essential n-e)
	    tramp-default-method)
	(dolist
	    (file
	     `(,(format
		 "/%s::"
		 (file-remote-p tramp-test-temporary-file-directory 'method))
	       ,(format
		  "/-:%s:"
		  (file-remote-p tramp-test-temporary-file-directory 'host))))
	  (should (string-equal (directory-file-name file) file))
	  (should
	   (string-equal
	    (file-name-as-directory file)
	    (if (tramp-completion-mode-p)
		file (concat file "./"))))
	  (should (string-equal (file-name-directory file) file))
	  (should (string-equal (file-name-nondirectory file) "")))))))

(ert-deftest tramp-test07-file-exists-p ()
  "Check `file-exist-p', `write-region' and `delete-file'."
  (skip-unless (tramp--test-enabled))

  (dolist (quoted (if tramp--test-expensive-test '(nil t) '(nil)))
    (let ((tmp-name (tramp--test-make-temp-name nil quoted)))
      (should-not (file-exists-p tmp-name))
      (write-region "foo" nil tmp-name)
      (should (file-exists-p tmp-name))
      (delete-file tmp-name)
      (should-not (file-exists-p tmp-name)))))

(ert-deftest tramp-test08-file-local-copy ()
  "Check `file-local-copy'."
  (skip-unless (tramp--test-enabled))

  (dolist (quoted (if tramp--test-expensive-test '(nil t) '(nil)))
    (let ((tmp-name1 (tramp--test-make-temp-name nil quoted))
	  tmp-name2)
      (unwind-protect
	  (progn
	    (write-region "foo" nil tmp-name1)
	    (should (setq tmp-name2 (file-local-copy tmp-name1)))
	    (with-temp-buffer
	      (insert-file-contents tmp-name2)
	      (should (string-equal (buffer-string) "foo")))
	    ;; Check also that a file transfer with compression works.
	    (let ((default-directory tramp-test-temporary-file-directory)
		  (tramp-copy-size-limit 4)
		  (tramp-inline-compress-start-size 2))
	      (delete-file tmp-name2)
	      (should (setq tmp-name2 (file-local-copy tmp-name1)))))

	;; Cleanup.
	(ignore-errors
	  (delete-file tmp-name1)
	  (delete-file tmp-name2))))))

(ert-deftest tramp-test09-insert-file-contents ()
  "Check `insert-file-contents'."
  (skip-unless (tramp--test-enabled))

  (dolist (quoted (if tramp--test-expensive-test '(nil t) '(nil)))
    (let ((tmp-name (tramp--test-make-temp-name nil quoted)))
      (unwind-protect
	  (progn
	    (write-region "foo" nil tmp-name)
	    (with-temp-buffer
	      (insert-file-contents tmp-name)
	      (should (string-equal (buffer-string) "foo"))
	      (insert-file-contents tmp-name)
	      (should (string-equal (buffer-string) "foofoo"))
	      ;; Insert partly.
	      (insert-file-contents tmp-name nil 1 3)
	      (should (string-equal (buffer-string) "oofoofoo"))
	      ;; Replace.
	      (insert-file-contents tmp-name nil nil nil 'replace)
	      (should (string-equal (buffer-string) "foo"))))

	;; Cleanup.
	(ignore-errors (delete-file tmp-name))))))

(ert-deftest tramp-test10-write-region ()
  "Check `write-region'."
  (skip-unless (tramp--test-enabled))

  (dolist (quoted (if tramp--test-expensive-test '(nil t) '(nil)))
    (let ((tmp-name (tramp--test-make-temp-name nil quoted)))
      (unwind-protect
	  (progn
	    (with-temp-buffer
	      (insert "foo")
	      (write-region nil nil tmp-name))
	    (with-temp-buffer
	      (insert-file-contents tmp-name)
	      (should (string-equal (buffer-string) "foo")))
	    ;; Append.
	    (with-temp-buffer
	      (insert "bla")
	      (write-region nil nil tmp-name 'append))
	    (with-temp-buffer
	      (insert-file-contents tmp-name)
	      (should (string-equal (buffer-string) "foobla")))
	    ;; Write string.
	    (write-region "foo" nil tmp-name)
	    (with-temp-buffer
	      (insert-file-contents tmp-name)
	      (should (string-equal (buffer-string) "foo")))
	    ;; Write partly.
	    (with-temp-buffer
	      (insert "123456789")
	      (write-region 3 5 tmp-name))
	    (with-temp-buffer
	      (insert-file-contents tmp-name)
	      (should (string-equal (buffer-string) "34"))))

	;; Cleanup.
	(ignore-errors (delete-file tmp-name))))))

(ert-deftest tramp-test11-copy-file ()
  "Check `copy-file'."
  (skip-unless (tramp--test-enabled))

  ;; TODO: The quoted case does not work.
  ;;(dolist (quoted (if tramp--test-expensive-test '(nil t) '(nil)))
  (let (quoted)
    (let ((tmp-name1 (tramp--test-make-temp-name nil quoted))
	  (tmp-name2 (tramp--test-make-temp-name nil quoted))
	  (tmp-name3 (tramp--test-make-temp-name nil quoted))
	  (tmp-name4 (tramp--test-make-temp-name 'local quoted))
	  (tmp-name5 (tramp--test-make-temp-name 'local quoted)))

      ;; Copy on remote side.
      (unwind-protect
	  (progn
	    (write-region "foo" nil tmp-name1)
	    (copy-file tmp-name1 tmp-name2)
	    (should (file-exists-p tmp-name2))
	    (with-temp-buffer
	      (insert-file-contents tmp-name2)
	      (should (string-equal (buffer-string) "foo")))
	    (should-error (copy-file tmp-name1 tmp-name2))
	    (copy-file tmp-name1 tmp-name2 'ok)
	    (make-directory tmp-name3)
	    (copy-file tmp-name1 tmp-name3)
	    (should
	     (file-exists-p
	      (expand-file-name (file-name-nondirectory tmp-name1) tmp-name3))))

	;; Cleanup.
	(ignore-errors (delete-file tmp-name1))
	(ignore-errors (delete-file tmp-name2))
	(ignore-errors (delete-directory tmp-name3 'recursive)))

      ;; Copy from remote side to local side.
      (unwind-protect
	  (progn
	    (write-region "foo" nil tmp-name1)
	    (copy-file tmp-name1 tmp-name4)
	    (should (file-exists-p tmp-name4))
	    (with-temp-buffer
	      (insert-file-contents tmp-name4)
	      (should (string-equal (buffer-string) "foo")))
	    (should-error (copy-file tmp-name1 tmp-name4))
	    (copy-file tmp-name1 tmp-name4 'ok)
	    (make-directory tmp-name5)
	    (copy-file tmp-name1 tmp-name5)
	    (should
	     (file-exists-p
	      (expand-file-name (file-name-nondirectory tmp-name1) tmp-name5))))

	;; Cleanup.
	(ignore-errors (delete-file tmp-name1))
	(ignore-errors (delete-file tmp-name4))
	(ignore-errors (delete-directory tmp-name5 'recursive)))

      ;; Copy from local side to remote side.
      (unwind-protect
	  (progn
	    (write-region "foo" nil tmp-name4 nil 'nomessage)
	    (copy-file tmp-name4 tmp-name1)
	    (should (file-exists-p tmp-name1))
	    (with-temp-buffer
	      (insert-file-contents tmp-name1)
	      (should (string-equal (buffer-string) "foo")))
	    (should-error (copy-file tmp-name4 tmp-name1))
	    (copy-file tmp-name4 tmp-name1 'ok)
	    (make-directory tmp-name3)
	    (copy-file tmp-name4 tmp-name3)
	    (should
	     (file-exists-p
	      (expand-file-name (file-name-nondirectory tmp-name4) tmp-name3))))

	;; Cleanup.
	(ignore-errors (delete-file tmp-name1))
	(ignore-errors (delete-file tmp-name4))
	(ignore-errors (delete-directory tmp-name3 'recursive))))))

(ert-deftest tramp-test12-rename-file ()
  "Check `rename-file'."
  (skip-unless (tramp--test-enabled))

  ;; TODO: The quoted case does not work.
  ;;(dolist (quoted (if tramp--test-expensive-test '(nil t) '(nil)))
  (let (quoted)
    (let ((tmp-name1 (tramp--test-make-temp-name nil quoted))
	  (tmp-name2 (tramp--test-make-temp-name nil quoted))
	  (tmp-name3 (tramp--test-make-temp-name nil quoted))
	  (tmp-name4 (tramp--test-make-temp-name 'local quoted))
	  (tmp-name5 (tramp--test-make-temp-name 'local quoted)))

      ;; Rename on remote side.
      (unwind-protect
	  (progn
	    (write-region "foo" nil tmp-name1)
	    (rename-file tmp-name1 tmp-name2)
	    (should-not (file-exists-p tmp-name1))
	    (should (file-exists-p tmp-name2))
	    (with-temp-buffer
	      (insert-file-contents tmp-name2)
	      (should (string-equal (buffer-string) "foo")))
	    (write-region "foo" nil tmp-name1)
	    (should-error (rename-file tmp-name1 tmp-name2))
	    (rename-file tmp-name1 tmp-name2 'ok)
	    (should-not (file-exists-p tmp-name1))
	    (write-region "foo" nil tmp-name1)
	    (make-directory tmp-name3)
	    (rename-file tmp-name1 tmp-name3)
	    (should-not (file-exists-p tmp-name1))
	    (should
	     (file-exists-p
	      (expand-file-name (file-name-nondirectory tmp-name1) tmp-name3))))

	;; Cleanup.
	(ignore-errors (delete-file tmp-name1))
	(ignore-errors (delete-file tmp-name2))
	(ignore-errors (delete-directory tmp-name3 'recursive)))

      ;; Rename from remote side to local side.
      (unwind-protect
	  (progn
	    (write-region "foo" nil tmp-name1)
	    (rename-file tmp-name1 tmp-name4)
	    (should-not (file-exists-p tmp-name1))
	    (should (file-exists-p tmp-name4))
	    (with-temp-buffer
	      (insert-file-contents tmp-name4)
	      (should (string-equal (buffer-string) "foo")))
	    (write-region "foo" nil tmp-name1)
	    (should-error (rename-file tmp-name1 tmp-name4))
	    (rename-file tmp-name1 tmp-name4 'ok)
	    (should-not (file-exists-p tmp-name1))
	    (write-region "foo" nil tmp-name1)
	    (make-directory tmp-name5)
	    (rename-file tmp-name1 tmp-name5)
	    (should-not (file-exists-p tmp-name1))
	    (should
	     (file-exists-p
	      (expand-file-name (file-name-nondirectory tmp-name1) tmp-name5))))

	;; Cleanup.
	(ignore-errors (delete-file tmp-name1))
	(ignore-errors (delete-file tmp-name4))
	(ignore-errors (delete-directory tmp-name5 'recursive)))

      ;; Rename from local side to remote side.
      (unwind-protect
	  (progn
	    (write-region "foo" nil tmp-name4 nil 'nomessage)
	    (rename-file tmp-name4 tmp-name1)
	    (should-not (file-exists-p tmp-name4))
	    (should (file-exists-p tmp-name1))
	    (with-temp-buffer
	      (insert-file-contents tmp-name1)
	      (should (string-equal (buffer-string) "foo")))
	    (write-region "foo" nil tmp-name4 nil 'nomessage)
	    (should-error (rename-file tmp-name4 tmp-name1))
	    (rename-file tmp-name4 tmp-name1 'ok)
	    (should-not (file-exists-p tmp-name4))
	    (write-region "foo" nil tmp-name4 nil 'nomessage)
	    (make-directory tmp-name3)
	    (rename-file tmp-name4 tmp-name3)
	    (should-not (file-exists-p tmp-name4))
	    (should
	     (file-exists-p
	      (expand-file-name (file-name-nondirectory tmp-name4) tmp-name3))))

	;; Cleanup.
	(ignore-errors (delete-file tmp-name1))
	(ignore-errors (delete-file tmp-name4))
	(ignore-errors (delete-directory tmp-name3 'recursive))))))

(ert-deftest tramp-test13-make-directory ()
  "Check `make-directory'.
This tests also `file-directory-p' and `file-accessible-directory-p'."
  (skip-unless (tramp--test-enabled))

  (dolist (quoted (if tramp--test-expensive-test '(nil t) '(nil)))
    (let* ((tmp-name1 (tramp--test-make-temp-name nil quoted))
	   (tmp-name2 (expand-file-name "foo/bar" tmp-name1)))
      (unwind-protect
	  (progn
	    (make-directory tmp-name1)
	    (should (file-directory-p tmp-name1))
	    (should (file-accessible-directory-p tmp-name1))
	    (should-error (make-directory tmp-name2))
	    (make-directory tmp-name2 'parents)
	    (should (file-directory-p tmp-name2))
	    (should (file-accessible-directory-p tmp-name2)))

	;; Cleanup.
	(ignore-errors (delete-directory tmp-name1 'recursive))))))

(ert-deftest tramp-test14-delete-directory ()
  "Check `delete-directory'."
  (skip-unless (tramp--test-enabled))

  (dolist (quoted (if tramp--test-expensive-test '(nil t) '(nil)))
    (let ((tmp-name (tramp--test-make-temp-name nil quoted)))
      ;; Delete empty directory.
      (make-directory tmp-name)
      (should (file-directory-p tmp-name))
      (delete-directory tmp-name)
      (should-not (file-directory-p tmp-name))
      ;; Delete non-empty directory.
      (make-directory tmp-name)
      (should (file-directory-p tmp-name))
      (write-region "foo" nil (expand-file-name "bla" tmp-name))
      (should (file-exists-p (expand-file-name "bla" tmp-name)))
      (should-error (delete-directory tmp-name))
      (delete-directory tmp-name 'recursive)
      (should-not (file-directory-p tmp-name)))))

(ert-deftest tramp-test15-copy-directory ()
  "Check `copy-directory'."
  (skip-unless (tramp--test-enabled))

  (dolist (quoted (if tramp--test-expensive-test '(nil t) '(nil)))
    (let* ((tmp-name1 (tramp--test-make-temp-name nil quoted))
	   (tmp-name2 (tramp--test-make-temp-name nil quoted))
	   (tmp-name3 (expand-file-name
		       (file-name-nondirectory tmp-name1) tmp-name2))
	   (tmp-name4 (expand-file-name "foo" tmp-name1))
	   (tmp-name5 (expand-file-name "foo" tmp-name2))
	   (tmp-name6 (expand-file-name "foo" tmp-name3)))

      ;; Copy complete directory.
      (unwind-protect
	  (progn
	    ;; Copy empty directory.
	    (make-directory tmp-name1)
	    (write-region "foo" nil tmp-name4)
	    (should (file-directory-p tmp-name1))
	    (should (file-exists-p tmp-name4))
	    (copy-directory tmp-name1 tmp-name2)
	    (should (file-directory-p tmp-name2))
	    (should (file-exists-p tmp-name5))
	    ;; Target directory does exist already.
	    (copy-directory tmp-name1 tmp-name2)
	    (should (file-directory-p tmp-name3))
	    (should (file-exists-p tmp-name6)))

	;; Cleanup.
	(ignore-errors
	  (delete-directory tmp-name1 'recursive)
	  (delete-directory tmp-name2 'recursive)))

      ;; Copy directory contents.
      (unwind-protect
	  (progn
	    ;; Copy empty directory.
	    (make-directory tmp-name1)
	    (write-region "foo" nil tmp-name4)
	    (should (file-directory-p tmp-name1))
	    (should (file-exists-p tmp-name4))
	    (copy-directory tmp-name1 tmp-name2 nil 'parents 'contents)
	    (should (file-directory-p tmp-name2))
	    (should (file-exists-p tmp-name5))
	    ;; Target directory does exist already.
	    (delete-file tmp-name5)
	    (should-not (file-exists-p tmp-name5))
	    (copy-directory tmp-name1 tmp-name2 nil 'parents 'contents)
	    (should (file-directory-p tmp-name2))
	    (should (file-exists-p tmp-name5))
	    (should-not (file-directory-p tmp-name3))
	    (should-not (file-exists-p tmp-name6)))

	;; Cleanup.
	(ignore-errors
	  (delete-directory tmp-name1 'recursive)
	  (delete-directory tmp-name2 'recursive))))))

(ert-deftest tramp-test16-directory-files ()
  "Check `directory-files'."
  (skip-unless (tramp--test-enabled))

  (dolist (quoted (if tramp--test-expensive-test '(nil t) '(nil)))
    (let* ((tmp-name1
            (expand-file-name (tramp--test-make-temp-name nil quoted)))
	   (tmp-name2 (expand-file-name "bla" tmp-name1))
	   (tmp-name3 (expand-file-name "foo" tmp-name1)))
      (unwind-protect
	  (progn
	    (make-directory tmp-name1)
	    (write-region "foo" nil tmp-name2)
	    (write-region "bla" nil tmp-name3)
	    (should (file-directory-p tmp-name1))
	    (should (file-exists-p tmp-name2))
	    (should (file-exists-p tmp-name3))
	    (should (equal (directory-files tmp-name1) '("." ".." "bla" "foo")))
	    (should (equal (directory-files tmp-name1 'full)
			   `(,(concat tmp-name1 "/.")
			     ,(concat tmp-name1 "/..")
			     ,tmp-name2 ,tmp-name3)))
	    (should (equal (directory-files
			    tmp-name1 nil directory-files-no-dot-files-regexp)
			   '("bla" "foo")))
	    (should (equal (directory-files
			    tmp-name1 'full directory-files-no-dot-files-regexp)
			   `(,tmp-name2 ,tmp-name3))))

	;; Cleanup.
	(ignore-errors (delete-directory tmp-name1 'recursive))))))

(ert-deftest tramp-test17-insert-directory ()
  "Check `insert-directory'."
  (skip-unless (tramp--test-enabled))

  (dolist (quoted (if tramp--test-expensive-test '(nil t) '(nil)))
    (let* ((tmp-name1
            (expand-file-name (tramp--test-make-temp-name nil quoted)))
	   (tmp-name2 (expand-file-name "foo" tmp-name1))
	   ;; We test for the summary line.  Keyword "total" could be localized.
	   (process-environment
	    (append '("LANG=C" "LANGUAGE=C" "LC_ALL=C") process-environment)))
      (unwind-protect
	  (progn
	    (make-directory tmp-name1)
	    (write-region "foo" nil tmp-name2)
	    (should (file-directory-p tmp-name1))
	    (should (file-exists-p tmp-name2))
	    (with-temp-buffer
	      (insert-directory tmp-name1 nil)
	      (goto-char (point-min))
	      (should (looking-at-p (regexp-quote tmp-name1))))
	    (with-temp-buffer
	      (insert-directory tmp-name1 "-al")
	      (goto-char (point-min))
	      (should
	       (looking-at-p (format "^.+ %s$" (regexp-quote tmp-name1)))))
	    (with-temp-buffer
	      (insert-directory (file-name-as-directory tmp-name1) "-al")
	      (goto-char (point-min))
	      (should
	       (looking-at-p (format "^.+ %s/$" (regexp-quote tmp-name1)))))
	    (with-temp-buffer
	      (insert-directory
	       (file-name-as-directory tmp-name1) "-al" nil 'full-directory-p)
	      (goto-char (point-min))
	      (should
	       (looking-at-p
		(concat
		 ;; There might be a summary line.
		 "\\(total.+[[:digit:]]+\n\\)?"
		 ;; We don't know in which order ".", ".." and "foo" appear.
		 "\\(.+ \\(\\.?\\.\\|foo\\)\n\\)\\{3\\}")))))

	;; Cleanup.
	(ignore-errors (delete-directory tmp-name1 'recursive))))))

(ert-deftest tramp-test18-file-attributes ()
  "Check `file-attributes'.
This tests also `file-readable-p', `file-regular-p' and
`file-ownership-preserved-p'."
  (skip-unless (tramp--test-enabled))

  (dolist (quoted (if tramp--test-expensive-test '(nil t) '(nil)))
    ;; We must use `file-truename' for the temporary directory,
    ;; because it could be located on a symlinked directory.  This
    ;; would let the test fail.
    (let* ((tramp-test-temporary-file-directory
	    (file-truename tramp-test-temporary-file-directory))
	   (tmp-name1 (tramp--test-make-temp-name nil quoted))
	   (tmp-name2 (tramp--test-make-temp-name nil quoted))
	   ;; File name with "//".
	   (tmp-name3
	    (format
	     "%s%s"
	     (file-remote-p tmp-name1)
	     (replace-regexp-in-string
	      "/" "//" (file-remote-p tmp-name1 'localname))))
	   attr)
      (unwind-protect
	  (progn
	    ;; `file-ownership-preserved-p' should return t for
	    ;; non-existing files.  It is implemented only in tramp-sh.el.
	    (when (tramp--test-sh-p)
	      (should (file-ownership-preserved-p tmp-name1 'group)))
	    (write-region "foo" nil tmp-name1)
	    (should (file-exists-p tmp-name1))
	    (should (file-readable-p tmp-name1))
	    (should (file-regular-p tmp-name1))
	    (when (tramp--test-sh-p)
	      (should (file-ownership-preserved-p tmp-name1 'group)))

	    ;; We do not test inodes and device numbers.
	    (setq attr (file-attributes tmp-name1))
	    (should (consp attr))
	    (should (null (car attr)))
	    (should (numberp (nth 1 attr))) ;; Link.
	    (should (numberp (nth 2 attr))) ;; Uid.
	    (should (numberp (nth 3 attr))) ;; Gid.
	    ;; Last access time.
	    (should (stringp (current-time-string (nth 4 attr))))
	    ;; Last modification time.
	    (should (stringp (current-time-string (nth 5 attr))))
	    ;; Last status change time.
	    (should (stringp (current-time-string (nth 6 attr))))
	    (should (numberp (nth 7 attr))) ;; Size.
	    (should (stringp (nth 8 attr))) ;; Modes.

	    (setq attr (file-attributes tmp-name1 'string))
	    (should (stringp (nth 2 attr))) ;; Uid.
	    (should (stringp (nth 3 attr))) ;; Gid.

	    (condition-case err
		(progn
		  (when (tramp--test-sh-p)
		    (should (file-ownership-preserved-p tmp-name2 'group)))
		  (make-symbolic-link tmp-name1 tmp-name2)
		  (should (file-exists-p tmp-name2))
		  (should (file-symlink-p tmp-name2))
		  (when (tramp--test-sh-p)
		    (should (file-ownership-preserved-p tmp-name2 'group)))
		  (setq attr (file-attributes tmp-name2))
		  (should
		   (string-equal
		    (funcall
		     (if quoted 'tramp-compat-file-name-quote 'identity)
		     (car attr))
		    (file-remote-p (file-truename tmp-name1) 'localname)))
		  (delete-file tmp-name2))
	      (file-error
	       (should (string-equal (error-message-string err)
				     "make-symbolic-link not supported"))))

	    ;; Check, that "//" in symlinks are handled properly.
	    (with-temp-buffer
	      (let ((default-directory tramp-test-temporary-file-directory))
		(shell-command
		 (format
		  "ln -s %s %s"
		  (tramp-file-name-localname
		   (tramp-dissect-file-name tmp-name3))
		  (tramp-file-name-localname
		   (tramp-dissect-file-name tmp-name2)))
		 t)))
	    (when (file-symlink-p tmp-name2)
	      (setq attr (file-attributes tmp-name2))
	      (should
	       (string-equal
		(car attr)
		(tramp-file-name-localname
		 (tramp-dissect-file-name tmp-name3))))
	      (delete-file tmp-name2))

	    (when (tramp--test-sh-p)
	      (should (file-ownership-preserved-p tmp-name1 'group)))
	    (delete-file tmp-name1)
	    (make-directory tmp-name1)
	    (should (file-exists-p tmp-name1))
	    (should (file-readable-p tmp-name1))
	    (should-not (file-regular-p tmp-name1))
	    (when (tramp--test-sh-p)
	      (should (file-ownership-preserved-p tmp-name1 'group)))
	    (setq attr (file-attributes tmp-name1))
	    (should (eq (car attr) t)))

	;; Cleanup.
	(ignore-errors (delete-directory tmp-name1))
	(ignore-errors (delete-file tmp-name1))
	(ignore-errors (delete-file tmp-name2))))))

(ert-deftest tramp-test19-directory-files-and-attributes ()
  "Check `directory-files-and-attributes'."
  (skip-unless (tramp--test-enabled))

  (dolist (quoted (if tramp--test-expensive-test '(nil t) '(nil)))
    ;; `directory-files-and-attributes' contains also values for
    ;; "../".  Ensure that this doesn't change during tests, for
    ;; example due to handling temporary files.
    (let* ((tmp-name1 (tramp--test-make-temp-name nil quoted))
	   (tmp-name2 (expand-file-name "bla" tmp-name1))
	   attr)
      (unwind-protect
	  (progn
	    (make-directory tmp-name1)
	    (should (file-directory-p tmp-name1))
	    (make-directory tmp-name2)
	    (should (file-directory-p tmp-name2))
	    (write-region "foo" nil (expand-file-name "foo" tmp-name2))
	    (write-region "bar" nil (expand-file-name "bar" tmp-name2))
	    (write-region "boz" nil (expand-file-name "boz" tmp-name2))
	    (setq attr (directory-files-and-attributes tmp-name2))
	    (should (consp attr))
	    ;; Dumb remote shells without perl(1) or stat(1) are not
	    ;; able to return the date correctly.  They say "don't know".
	    (dolist (elt attr)
	      (unless
		  (equal
		   (nth
		    5 (file-attributes (expand-file-name (car elt) tmp-name2)))
		   '(0 0))
		(should
		 (equal (file-attributes (expand-file-name (car elt) tmp-name2))
			(cdr elt)))))
	    (setq attr (directory-files-and-attributes tmp-name2 'full))
	    (dolist (elt attr)
	      (unless (equal (nth 5 (file-attributes (car elt))) '(0 0))
		(should
		 (equal (file-attributes (car elt)) (cdr elt)))))
	    (setq attr (directory-files-and-attributes tmp-name2 nil "^b"))
	    (should (equal (mapcar 'car attr) '("bar" "boz"))))

	;; Cleanup.
	(ignore-errors (delete-directory tmp-name1 'recursive))))))

(ert-deftest tramp-test20-file-modes ()
  "Check `file-modes'.
This tests also `file-executable-p', `file-writable-p' and `set-file-modes'."
  (skip-unless (tramp--test-enabled))
  (skip-unless (tramp--test-sh-p))

  (dolist (quoted (if tramp--test-expensive-test '(nil t) '(nil)))
    (let ((tmp-name (tramp--test-make-temp-name nil quoted)))
      (unwind-protect
	  (progn
	    (write-region "foo" nil tmp-name)
	    (should (file-exists-p tmp-name))
	    (set-file-modes tmp-name #o777)
	    (should (= (file-modes tmp-name) #o777))
	    (should (file-executable-p tmp-name))
	    (should (file-writable-p tmp-name))
	    (set-file-modes tmp-name #o444)
	    (should (= (file-modes tmp-name) #o444))
	    (should-not (file-executable-p tmp-name))
	    ;; A file is always writable for user "root".
	    (unless (zerop (nth 2 (file-attributes tmp-name)))
	      (should-not (file-writable-p tmp-name))))

	;; Cleanup.
	(ignore-errors (delete-file tmp-name))))))

(ert-deftest tramp-test21-file-links ()
  "Check `file-symlink-p'.
This tests also `make-symbolic-link', `file-truename' and `add-name-to-file'."
  (skip-unless (tramp--test-enabled))

  (dolist (quoted (if tramp--test-expensive-test '(nil t) '(nil)))
    ;; We must use `file-truename' for the temporary directory,
    ;; because it could be located on a symlinked directory.  This
    ;; would let the test fail.
    (let* ((tramp-test-temporary-file-directory
	    (file-truename tramp-test-temporary-file-directory))
	   (tmp-name1 (tramp--test-make-temp-name nil quoted))
	   (tmp-name2 (tramp--test-make-temp-name nil quoted))
	   (tmp-name3 (tramp--test-make-temp-name 'local quoted)))

      ;; Check `make-symbolic-link'.
      (unwind-protect
	  (progn
	    (write-region "foo" nil tmp-name1)
	    (should (file-exists-p tmp-name1))
	    ;; Method "smb" supports `make-symbolic-link' only if the
	    ;; remote host has CIFS capabilities.  tramp-adb.el and
	    ;; tramp-gvfs.el do not support symbolic links at all.
	    (condition-case err
		(make-symbolic-link tmp-name1 tmp-name2)
	      (file-error
	       (skip-unless
		(not (string-equal (error-message-string err)
				   "make-symbolic-link not supported")))))
	    (should (file-symlink-p tmp-name2))
	    (should-error (make-symbolic-link tmp-name1 tmp-name2))
	    (make-symbolic-link tmp-name1 tmp-name2 'ok-if-already-exists)
	    (should (file-symlink-p tmp-name2))
	    ;; `tmp-name3' is a local file name.
	    (should-error (make-symbolic-link tmp-name1 tmp-name3)))

	;; Cleanup.
	(ignore-errors
	  (delete-file tmp-name1)
	  (delete-file tmp-name2)))

      ;; Check `add-name-to-file'.
      (unwind-protect
	  (progn
	    (write-region "foo" nil tmp-name1)
	    (should (file-exists-p tmp-name1))
	    (add-name-to-file tmp-name1 tmp-name2)
	    (should-not (file-symlink-p tmp-name2))
	    (should-error (add-name-to-file tmp-name1 tmp-name2))
	    (add-name-to-file tmp-name1 tmp-name2 'ok-if-already-exists)
	    (should-not (file-symlink-p tmp-name2))
	    ;; `tmp-name3' is a local file name.
	    (should-error (add-name-to-file tmp-name1 tmp-name3)))

	;; Cleanup.
	(ignore-errors
	  (delete-file tmp-name1)
	  (delete-file tmp-name2)))

      ;; Check `file-truename'.
      (unwind-protect
	  (progn
	    (write-region "foo" nil tmp-name1)
	    (should (file-exists-p tmp-name1))
	    (make-symbolic-link tmp-name1 tmp-name2)
	    (should (file-symlink-p tmp-name2))
	    (should-not (string-equal tmp-name2 (file-truename tmp-name2)))
	    (should
	     (string-equal (file-truename tmp-name1) (file-truename tmp-name2)))
	    (should (file-equal-p tmp-name1 tmp-name2)))
	(ignore-errors
	  (delete-file tmp-name1)
	  (delete-file tmp-name2)))

      ;; `file-truename' shall preserve trailing link of directories.
      (unless (file-symlink-p tramp-test-temporary-file-directory)
	(let* ((dir1 (directory-file-name tramp-test-temporary-file-directory))
	       (dir2 (file-name-as-directory dir1)))
	  (should (string-equal (file-truename dir1) (expand-file-name dir1)))
	  (should
	   (string-equal (file-truename dir2) (expand-file-name dir2))))))))

(ert-deftest tramp-test22-file-times ()
  "Check `set-file-times' and `file-newer-than-file-p'."
  (skip-unless (tramp--test-enabled))
  (skip-unless (or (tramp--test-adb-p) (tramp--test-sh-p)))

  (dolist (quoted (if tramp--test-expensive-test '(nil t) '(nil)))
    (let ((tmp-name1 (tramp--test-make-temp-name nil quoted))
	  (tmp-name2 (tramp--test-make-temp-name nil quoted))
	  (tmp-name3 (tramp--test-make-temp-name nil quoted)))
      (unwind-protect
	  (progn
	    (write-region "foo" nil tmp-name1)
	    (should (file-exists-p tmp-name1))
	    (should (consp (nth 5 (file-attributes tmp-name1))))
	    ;; '(0 0) means don't know, and will be replaced by
	    ;; `current-time'.  Therefore, we use '(0 1).  We skip the
	    ;; test, if the remote handler is not able to set the
	    ;; correct time.
	    (skip-unless (set-file-times tmp-name1 '(0 1)))
	    ;; Dumb remote shells without perl(1) or stat(1) are not
	    ;; able to return the date correctly.  They say "don't know".
	    (unless (equal (nth 5 (file-attributes tmp-name1)) '(0 0))
	      (should (equal (nth 5 (file-attributes tmp-name1)) '(0 1)))
	      (write-region "bla" nil tmp-name2)
	      (should (file-exists-p tmp-name2))
	      (should (file-newer-than-file-p tmp-name2 tmp-name1))
	      ;; `tmp-name3' does not exist.
	      (should (file-newer-than-file-p tmp-name2 tmp-name3))
	      (should-not (file-newer-than-file-p tmp-name3 tmp-name1))))

	;; Cleanup.
	(ignore-errors
	  (delete-file tmp-name1)
	  (delete-file tmp-name2))))))

(ert-deftest tramp-test23-visited-file-modtime ()
  "Check `set-visited-file-modtime' and `verify-visited-file-modtime'."
  (skip-unless (tramp--test-enabled))

  (dolist (quoted (if tramp--test-expensive-test '(nil t) '(nil)))
    (let ((tmp-name (tramp--test-make-temp-name nil quoted)))
      (unwind-protect
	  (progn
	    (write-region "foo" nil tmp-name)
	    (should (file-exists-p tmp-name))
	    (with-temp-buffer
	      (insert-file-contents tmp-name)
	      (should (verify-visited-file-modtime))
	      (set-visited-file-modtime '(0 1))
	      (should (verify-visited-file-modtime))
	      (should (equal (visited-file-modtime) '(0 1 0 0)))))

	;; Cleanup.
	(ignore-errors (delete-file tmp-name))))))

(ert-deftest tramp-test24-file-name-completion ()
  "Check `file-name-completion' and `file-name-all-completions'."
  (skip-unless (tramp--test-enabled))

  (dolist (n-e '(nil t))
    (dolist (quoted (if tramp--test-expensive-test '(nil t) '(nil)))
      (let ((non-essential n-e)
	    (tmp-name (tramp--test-make-temp-name nil quoted))
	    (method (file-remote-p tramp-test-temporary-file-directory 'method))
	    (host (file-remote-p tramp-test-temporary-file-directory 'host)))

	(unwind-protect
	    (progn
	      ;; Method and host name in completion mode.  This kind
	      ;; of completion does not work on MS Windows.
	      (when (not (memq system-type '(cygwin windows-nt)))
		(unless (zerop (length method))
		  (should
		   (member
		    (format "%s:" method)
		    (file-name-all-completions (substring method 0 1) "/"))))
		(unless (zerop (length host))
		  (let ((tramp-default-method (or method tramp-default-method)))
		    (should
		     (member
		      (format "-:%s:" host)
		      (file-name-all-completions
		       (format "-:%s" (substring host 0 1)) "/")))))
		(unless (or (zerop (length method)) (zerop (length host)))
		  (should
		   (member
		    (format "%s:%s:" method host)
		    (file-name-all-completions
		     (format "%s:" method) "/")))))

	      ;; Local files.
	      (make-directory tmp-name)
	      (should (file-directory-p tmp-name))
	      (write-region "foo" nil (expand-file-name "foo" tmp-name))
	      (should (file-exists-p (expand-file-name "foo" tmp-name)))
	      (write-region "bar" nil (expand-file-name "bold" tmp-name))
	      (should (file-exists-p (expand-file-name "bold" tmp-name)))
	      (make-directory (expand-file-name "boz" tmp-name))
	      (should (file-directory-p (expand-file-name "boz" tmp-name)))
	      (should (equal (file-name-completion "fo" tmp-name) "foo"))
	      (should (equal (file-name-completion "foo" tmp-name) t))
	      (should (equal (file-name-completion "b" tmp-name) "bo"))
	      (should-not (file-name-completion "a" tmp-name))
	      (should
	       (equal
		(file-name-completion "b" tmp-name 'file-directory-p) "boz/"))
	      (should
	       (equal (file-name-all-completions "fo" tmp-name) '("foo")))
	      (should
	       (equal
		(sort (file-name-all-completions "b" tmp-name) 'string-lessp)
		'("bold" "boz/")))
	      (should-not (file-name-all-completions "a" tmp-name))
	      ;; `completion-regexp-list' restricts the completion to
	      ;; files which match all expressions in this list.
	      (let ((completion-regexp-list
		     `(,directory-files-no-dot-files-regexp "b")))
		(should
		 (equal (file-name-completion "" tmp-name) "bo"))
		(should
		 (equal
		  (sort (file-name-all-completions "" tmp-name) 'string-lessp)
		  '("bold" "boz/"))))
	      ;; `file-name-completion' ignores file names that end in
	      ;; any string in `completion-ignored-extensions'.
	      (let ((completion-ignored-extensions '(".ext")))
		(write-region "foo" nil (expand-file-name "foo.ext" tmp-name))
		(should (file-exists-p (expand-file-name "foo.ext" tmp-name)))
		(should (equal (file-name-completion "fo" tmp-name) "foo"))
		(should (equal (file-name-completion "foo" tmp-name) t))
		(should
		 (equal (file-name-completion "foo." tmp-name) "foo.ext"))
		(should (equal (file-name-completion "foo.ext" tmp-name) t))
		;; `file-name-all-completions' is not affected.
		(should
		 (equal
		  (sort (file-name-all-completions "" tmp-name) 'string-lessp)
		  '("../" "./" "bold" "boz/" "foo" "foo.ext")))))

	  ;; Cleanup.
	  (ignore-errors (delete-directory tmp-name 'recursive)))))))

(ert-deftest tramp-test25-load ()
  "Check `load'."
  (skip-unless (tramp--test-enabled))

  (dolist (quoted (if tramp--test-expensive-test '(nil t) '(nil)))
    (let ((tmp-name (tramp--test-make-temp-name nil quoted)))
      (unwind-protect
	  (progn
	    (load tmp-name 'noerror 'nomessage)
	    (should-not (featurep 'tramp-test-load))
	    (write-region "(provide 'tramp-test-load)" nil tmp-name)
	    ;; `load' in lread.c does not pass `must-suffix'.  Why?
	    ;;(should-error
	    ;; (load tmp-name nil 'nomessage 'nosuffix 'must-suffix))
	    (load tmp-name nil 'nomessage 'nosuffix)
	    (should (featurep 'tramp-test-load)))

	;; Cleanup.
	(ignore-errors
	  (and (featurep 'tramp-test-load) (unload-feature 'tramp-test-load))
	  (delete-file tmp-name))))))

(ert-deftest tramp-test26-process-file ()
  "Check `process-file'."
  :tags '(:expensive-test)
  (skip-unless (tramp--test-enabled))
  (skip-unless (or (tramp--test-adb-p) (tramp--test-sh-p)))

  (dolist (quoted (if tramp--test-expensive-test '(nil t) '(nil)))
    (let* ((tmp-name (tramp--test-make-temp-name nil quoted))
	   (fnnd (file-name-nondirectory tmp-name))
	   (default-directory tramp-test-temporary-file-directory)
	   kill-buffer-query-functions)
      (unwind-protect
	  (progn
	    ;; We cannot use "/bin/true" and "/bin/false"; those paths
	    ;; do not exist on hydra.
	    (should (zerop (process-file "true")))
	    (should-not (zerop (process-file "false")))
	    (should-not (zerop (process-file "binary-does-not-exist")))
	    (with-temp-buffer
	      (write-region "foo" nil tmp-name)
	      (should (file-exists-p tmp-name))
	      (should (zerop (process-file "ls" nil t nil fnnd)))
	      ;; `ls' could produce colorized output.
	      (goto-char (point-min))
	      (while
		  (re-search-forward tramp-display-escape-sequence-regexp nil t)
		(replace-match "" nil nil))
	      (should (string-equal (format "%s\n" fnnd) (buffer-string)))
	      (should-not (get-buffer-window (current-buffer) t))

	      ;; Second run. The output must be appended.
	      (goto-char (point-max))
	      (should (zerop (process-file "ls" nil t t fnnd)))
	      ;; `ls' could produce colorized output.
	      (goto-char (point-min))
	      (while
		  (re-search-forward tramp-display-escape-sequence-regexp nil t)
		(replace-match "" nil nil))
	      (should
	       (string-equal (format "%s\n%s\n" fnnd fnnd) (buffer-string)))
	      ;; A non-nil DISPLAY must not raise the buffer.
	      (should-not (get-buffer-window (current-buffer) t))))

	;; Cleanup.
	(ignore-errors (delete-file tmp-name))))))

(ert-deftest tramp-test27-start-file-process ()
  "Check `start-file-process'."
  :tags '(:expensive-test)
  (skip-unless (tramp--test-enabled))
  (skip-unless (tramp--test-sh-p))

  (dolist (quoted (if tramp--test-expensive-test '(nil t) '(nil)))
    (let ((default-directory tramp-test-temporary-file-directory)
	  (tmp-name (tramp--test-make-temp-name nil quoted))
	  kill-buffer-query-functions proc)
      (unwind-protect
	  (with-temp-buffer
	    (setq proc (start-file-process "test1" (current-buffer) "cat"))
	    (should (processp proc))
	    (should (equal (process-status proc) 'run))
	    (process-send-string proc "foo")
	    (process-send-eof proc)
	    ;; Read output.
	    (with-timeout (10 (ert-fail "`start-file-process' timed out"))
	      (while (< (- (point-max) (point-min)) (length "foo"))
		(accept-process-output proc 0.1)))
	    (should (string-equal (buffer-string) "foo")))

	;; Cleanup.
	(ignore-errors (delete-process proc)))

      (unwind-protect
	  (with-temp-buffer
	    (write-region "foo" nil tmp-name)
	    (should (file-exists-p tmp-name))
	    (setq proc
		  (start-file-process
		   "test2" (current-buffer)
		   "cat" (file-name-nondirectory tmp-name)))
	    (should (processp proc))
	    ;; Read output.
	    (with-timeout (10 (ert-fail "`start-file-process' timed out"))
	      (while (< (- (point-max) (point-min)) (length "foo"))
		(accept-process-output proc 0.1)))
	    (should (string-equal (buffer-string) "foo")))

	;; Cleanup.
	(ignore-errors
	  (delete-process proc)
	  (delete-file tmp-name)))

      (unwind-protect
	  (with-temp-buffer
	    (setq proc (start-file-process "test3" (current-buffer) "cat"))
	    (should (processp proc))
	    (should (equal (process-status proc) 'run))
	    (set-process-filter
	     proc
	     (lambda (p s) (with-current-buffer (process-buffer p) (insert s))))
	    (process-send-string proc "foo")
	    (process-send-eof proc)
	    ;; Read output.
	    (with-timeout (10 (ert-fail "`start-file-process' timed out"))
	      (while (< (- (point-max) (point-min)) (length "foo"))
		(accept-process-output proc 0.1)))
	    (should (string-equal (buffer-string) "foo")))

	;; Cleanup.
	(ignore-errors (delete-process proc))))))

(ert-deftest tramp-test28-shell-command ()
  "Check `shell-command'."
  :tags '(:expensive-test)
  (skip-unless (tramp--test-enabled))
  (skip-unless (tramp--test-sh-p))

  (dolist (quoted (if tramp--test-expensive-test '(nil t) '(nil)))
    (let ((tmp-name (tramp--test-make-temp-name nil quoted))
	  (default-directory tramp-test-temporary-file-directory)
	  kill-buffer-query-functions)
      (unwind-protect
	  (with-temp-buffer
	    (write-region "foo" nil tmp-name)
	    (should (file-exists-p tmp-name))
	    (shell-command
	     (format "ls %s" (file-name-nondirectory tmp-name))
	     (current-buffer))
	    ;; `ls' could produce colorized output.
	    (goto-char (point-min))
	    (while
		(re-search-forward tramp-display-escape-sequence-regexp nil t)
	      (replace-match "" nil nil))
	    (should
	     (string-equal
	      (format "%s\n" (file-name-nondirectory tmp-name))
	      (buffer-string))))

	;; Cleanup.
	(ignore-errors (delete-file tmp-name)))

      (unwind-protect
	  (with-temp-buffer
	    (write-region "foo" nil tmp-name)
	    (should (file-exists-p tmp-name))
	    (async-shell-command
	     (format "ls %s" (file-name-nondirectory tmp-name))
	     (current-buffer))
	    (set-process-sentinel (get-buffer-process (current-buffer)) nil)
	    ;; Read output.
	    (with-timeout (10 (ert-fail "`async-shell-command' timed out"))
	      (while (< (- (point-max) (point-min))
			(1+ (length (file-name-nondirectory tmp-name))))
		(accept-process-output
		 (get-buffer-process (current-buffer)) 0.1)))
	    ;; `ls' could produce colorized output.
	    (goto-char (point-min))
	    (while
		(re-search-forward tramp-display-escape-sequence-regexp nil t)
	      (replace-match "" nil nil))
	    ;; There might be a nasty "Process *Async Shell* finished" message.
	    (goto-char (point-min))
	    (forward-line)
	    (narrow-to-region (point-min) (point))
	    (should
	     (string-equal
	      (format "%s\n" (file-name-nondirectory tmp-name))
	      (buffer-string))))

	;; Cleanup.
	(ignore-errors (delete-file tmp-name)))

      (unwind-protect
	  (with-temp-buffer
	    (write-region "foo" nil tmp-name)
	    (should (file-exists-p tmp-name))
	    (async-shell-command "read line; ls $line" (current-buffer))
	    (set-process-sentinel (get-buffer-process (current-buffer)) nil)
	    (process-send-string
	     (get-buffer-process (current-buffer))
	     (format "%s\n" (file-name-nondirectory tmp-name)))
	    ;; Read output.
	    (with-timeout (10 (ert-fail "`async-shell-command' timed out"))
	      (while (< (- (point-max) (point-min))
			(1+ (length (file-name-nondirectory tmp-name))))
		(accept-process-output
		 (get-buffer-process (current-buffer)) 0.1)))
	    ;; `ls' could produce colorized output.
	    (goto-char (point-min))
	    (while
		(re-search-forward tramp-display-escape-sequence-regexp nil t)
	      (replace-match "" nil nil))
	    ;; There might be a nasty "Process *Async Shell* finished" message.
	    (goto-char (point-min))
	    (forward-line)
	    (narrow-to-region (point-min) (point))
	    (should
	     (string-equal
	      (format "%s\n" (file-name-nondirectory tmp-name))
	      (buffer-string))))

	;; Cleanup.
	(ignore-errors (delete-file tmp-name))))))

(defun tramp--test-shell-command-to-string-asynchronously (command)
  "Like `shell-command-to-string', but for asynchronous processes."
  (with-temp-buffer
    (async-shell-command command (current-buffer))
    ;; Suppress nasty messages.
    (set-process-sentinel (get-buffer-process (current-buffer)) nil)
    (with-timeout (10)
      (while (get-buffer-process (current-buffer))
	(accept-process-output (get-buffer-process (current-buffer)) 0.1)))
    (accept-process-output nil 0.1)
    (buffer-substring-no-properties (point-min) (point-max))))

;; This test is inspired by Bug#23952.
(ert-deftest tramp-test29-environment-variables ()
  "Check that remote processes set / unset environment variables properly."
  :tags '(:expensive-test)
  (skip-unless (tramp--test-enabled))
  (skip-unless (tramp--test-sh-p))

  (dolist (this-shell-command-to-string
	   '(;; Synchronously.
	     shell-command-to-string
	     ;; Asynchronously.
	     tramp--test-shell-command-to-string-asynchronously))

    (let ((default-directory tramp-test-temporary-file-directory)
	  (shell-file-name "/bin/sh")
	  (envvar (concat "VAR_" (upcase (md5 (current-time-string)))))
	  kill-buffer-query-functions)

      (unwind-protect
	  ;; Set a value.
	  (let ((process-environment
		 (cons (concat envvar "=foo") process-environment)))
	    ;; Default value.
	    (should
	     (string-match
	      "foo"
	      (funcall
	       this-shell-command-to-string
	       (format "echo -n ${%s:?bla}" envvar))))))

      (unwind-protect
	  ;; Set the empty value.
	  (let ((process-environment
		 (cons (concat envvar "=") process-environment)))
	    ;; Value is null.
	    (should
	     (string-match
	      "bla"
	      (funcall
	       this-shell-command-to-string
	       (format "echo -n ${%s:?bla}" envvar))))
	    ;; Variable is set.
	    (should
	     (string-match
	      (regexp-quote envvar)
	      (funcall this-shell-command-to-string "set")))))

      ;; We force a reconnect, in order to have a clean environment.
      (tramp-cleanup-connection
       (tramp-dissect-file-name tramp-test-temporary-file-directory)
       'keep-debug 'keep-password)
      (unwind-protect
	  ;; Unset the variable.
	  (let ((tramp-remote-process-environment
		 (cons (concat envvar "=foo")
		       tramp-remote-process-environment)))
	    ;; Set the initial value, we want to unset below.
	    (should
	     (string-match
	      "foo"
	      (funcall
	       this-shell-command-to-string
	       (format "echo -n ${%s:?bla}" envvar))))
	    (let ((process-environment
		   (cons envvar process-environment)))
	      ;; Variable is unset.
	      (should
	       (string-match
		"bla"
		(funcall
		 this-shell-command-to-string
		 (format "echo -n ${%s:?bla}" envvar))))
	      ;; Variable is unset.
	      (should-not
	       (string-match
		(regexp-quote envvar)
		(funcall this-shell-command-to-string "set")))))))))

(ert-deftest tramp-test30-vc-registered ()
  "Check `vc-registered'."
  :tags '(:expensive-test)
  (skip-unless (tramp--test-enabled))
  (skip-unless (tramp--test-sh-p))

  ;; TODO: This test fails.
  (dolist (quoted (if tramp--test-expensive-test '(nil t) '(nil)))
    (let* ((default-directory tramp-test-temporary-file-directory)
	   (tmp-name1 (tramp--test-make-temp-name nil quoted))
	   (tmp-name2 (expand-file-name "foo" tmp-name1))
	   (tramp-remote-process-environment tramp-remote-process-environment)
	   (vc-handled-backends
	    (with-parsed-tramp-file-name tramp-test-temporary-file-directory nil
	      (cond
	       ((tramp-find-executable
		 v vc-git-program (tramp-get-remote-path v))
		'(Git))
	       ((tramp-find-executable
		 v vc-hg-program (tramp-get-remote-path v))
		'(Hg))
	       ((tramp-find-executable
		 v vc-bzr-program (tramp-get-remote-path v))
		(setq tramp-remote-process-environment
		      (cons (format "BZR_HOME=%s"
				    (file-remote-p tmp-name1 'localname))
			    tramp-remote-process-environment))
		;; We must force a reconnect, in order to activate $BZR_HOME.
		(tramp-cleanup-connection
		 (tramp-dissect-file-name tramp-test-temporary-file-directory)
		 nil 'keep-password)
		'(Bzr))
	       (t nil)))))
      (skip-unless vc-handled-backends)
      (message "%s" vc-handled-backends)

      (unwind-protect
	  (progn
	    (make-directory tmp-name1)
	    (write-region "foo" nil tmp-name2)
	    (should (file-directory-p tmp-name1))
	    (should (file-exists-p tmp-name2))
	    (should-not (vc-registered tmp-name1))
	    (should-not (vc-registered tmp-name2))

	    (let ((default-directory tmp-name1))
	      ;; Create empty repository, and register the file.
	      ;; Sometimes, creation of repository fails (bzr!); we
	      ;; skip the test then.
	      (condition-case nil
		  (vc-create-repo (car vc-handled-backends))
		(error (skip-unless nil)))
	      ;; The structure of VC-FILESET is not documented.  Let's
	      ;; hope it won't change.
	      (condition-case nil
		  (vc-register
		   (list (car vc-handled-backends)
			 (list (file-name-nondirectory tmp-name2))))
		;; `vc-register' has changed its arguments in Emacs 25.1.
		(error
		 (vc-register
		  nil (list (car vc-handled-backends)
			    (list (file-name-nondirectory tmp-name2))))))
	      ;; vc-git uses an own process sentinel, Tramp's sentinel
	      ;; for flushing the cache isn't used.
	      (dired-uncache (concat (file-remote-p default-directory) "/"))
	      (should (vc-registered (file-name-nondirectory tmp-name2)))))

	;; Cleanup.
	(ignore-errors (delete-directory tmp-name1 'recursive))))))

(ert-deftest tramp-test31-make-auto-save-file-name ()
  "Check `make-auto-save-file-name'."
  (skip-unless (tramp--test-enabled))

  (dolist (quoted (if tramp--test-expensive-test '(nil t) '(nil)))
    (let ((tmp-name1 (tramp--test-make-temp-name nil quoted))
	  (tmp-name2 (tramp--test-make-temp-name nil quoted)))

      (unwind-protect
	  (progn
	    ;; Use default `auto-save-file-name-transforms' mechanism.
	    (let (tramp-auto-save-directory)
	      (with-temp-buffer
		(setq buffer-file-name tmp-name1)
		(should
		 (string-equal
		  (make-auto-save-file-name)
		  ;; This is taken from original `make-auto-save-file-name'.
		  ;; We call `convert-standard-filename', because on
		  ;; MS Windows the (local) colons must be replaced by
		  ;; exclamation marks.
		  (convert-standard-filename
		   (expand-file-name
		    (format
		     "#%s#"
		     (subst-char-in-string
		      ?/ ?! (replace-regexp-in-string "!" "!!" tmp-name1)))
		    temporary-file-directory))))))

	    ;; No mapping.
	    (let (tramp-auto-save-directory auto-save-file-name-transforms)
	      (with-temp-buffer
		(setq buffer-file-name tmp-name1)
		(should
		 (string-equal
		  (make-auto-save-file-name)
		  (funcall
		   (if quoted 'tramp-compat-file-name-quote 'identity)
		   (expand-file-name
		    (format "#%s#" (file-name-nondirectory tmp-name1))
		    tramp-test-temporary-file-directory))))))

            ;; TODO: The following two cases don't work yet.
            (when nil
	    ;; Use default `tramp-auto-save-directory' mechanism.
	    (let ((tramp-auto-save-directory tmp-name2))
	      (with-temp-buffer
		(setq buffer-file-name tmp-name1)
		(should
		 (string-equal
		  (make-auto-save-file-name)
		  ;; This is taken from Tramp.
		  (expand-file-name
		   (format
		    "#%s#"
		    (tramp-subst-strs-in-string
		     '(("_" . "|")
		       ("/" . "_a")
		       (":" . "_b")
		       ("|" . "__")
		       ("[" . "_l")
		       ("]" . "_r"))
		     (tramp-compat-file-name-unquote tmp-name1)))
		   tmp-name2)))
		(should (file-directory-p tmp-name2))))

	    ;; Relative file names shall work, too.
	    (let ((tramp-auto-save-directory "."))
	      (with-temp-buffer
		(setq buffer-file-name tmp-name1
		      default-directory tmp-name2)
		(should
		 (string-equal
		  (make-auto-save-file-name)
		  ;; This is taken from Tramp.
		  (expand-file-name
		   (format
		    "#%s#"
		    (tramp-subst-strs-in-string
		     '(("_" . "|")
		       ("/" . "_a")
		       (":" . "_b")
		       ("|" . "__")
		       ("[" . "_l")
		       ("]" . "_r"))
		     (tramp-compat-file-name-unquote tmp-name1)))
		   tmp-name2)))
		(should (file-directory-p tmp-name2)))))
            ) ;; TODO

	;; Cleanup.
	(ignore-errors (delete-file tmp-name1))
	(ignore-errors (delete-directory tmp-name2 'recursive))))))

;; The functions were introduced in Emacs 26.1.
(ert-deftest tramp-test32-make-nearby-temp-file ()
  "Check `make-nearby-temp-file' and `temporary-file-directory'."
  (skip-unless (tramp--test-enabled))
  (skip-unless
   (and (fboundp 'make-nearby-temp-file) (fboundp 'temporary-file-directory)))

  ;; `make-nearby-temp-file' and `temporary-file-directory' exists
  ;; since Emacs 26.  We don't want to see compiler warnings for older
  ;; Emacsen."
  (let ((default-directory tramp-test-temporary-file-directory)
	tmp-file)
    ;; The remote host shall know a temporary file directory.
    (should (stringp (with-no-warnings (temporary-file-directory))))
    (should
     (string-equal
      (file-remote-p default-directory)
      (file-remote-p (with-no-warnings (temporary-file-directory)))))

    ;; The temporary file shall be located on the remote host.
    (setq tmp-file (with-no-warnings (make-nearby-temp-file "tramp-test")))
    (should (file-exists-p tmp-file))
    (should (file-regular-p tmp-file))
    (should
     (string-equal
      (file-remote-p default-directory)
      (file-remote-p tmp-file)))
    (delete-file tmp-file)
    (should-not (file-exists-p tmp-file))

    (setq tmp-file (with-no-warnings (make-nearby-temp-file "tramp-test" 'dir)))
    (should (file-exists-p tmp-file))
    (should (file-directory-p tmp-file))
    (delete-directory tmp-file)
    (should-not (file-exists-p tmp-file))))

(defun tramp--test-adb-p ()
  "Check, whether the remote host runs Android.
This requires restrictions of file name syntax."
  (tramp-adb-file-name-p tramp-test-temporary-file-directory))

(defun tramp--test-docker-p ()
  "Check, whether the docker method is used.
This does not support some special file names."
  (string-equal
   "docker" (file-remote-p tramp-test-temporary-file-directory 'method)))

(defun tramp--test-ftp-p ()
  "Check, whether an FTP-like method is used.
This does not support globbing characters in file names (yet)."
  ;; Globbing characters are ??, ?* and ?\[.
  (string-match
   "ftp$" (file-remote-p tramp-test-temporary-file-directory 'method)))

(defun tramp--test-gvfs-p ()
  "Check, whether the remote host runs a GVFS based method.
This requires restrictions of file name syntax."
  (tramp-gvfs-file-name-p tramp-test-temporary-file-directory))

(defun tramp--test-hpux-p ()
  "Check, whether the remote host runs HP-UX.
Several special characters do not work properly there."
  ;; We must refill the cache.  `file-truename' does it.
  (with-parsed-tramp-file-name
      (file-truename tramp-test-temporary-file-directory) nil
    (string-match "^HP-UX" (tramp-get-connection-property v "uname" ""))))

(defun tramp--test-rsync-p ()
  "Check, whether the rsync method is used.
This does not support special file names."
  (string-equal
   "rsync" (file-remote-p tramp-test-temporary-file-directory 'method)))

(defun tramp--test-sh-p ()
  "Check, whether the remote host runs a based method from tramp-sh.el."
  (eq
   (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory)
   'tramp-sh-file-name-handler))

(defun tramp--test-windows-nt-and-batch ()
  "Check, whether the locale host runs MS Windows in batch mode.
This does not support special characters."
  (and (eq system-type 'windows-nt) noninteractive))

(defun tramp--test-windows-nt-and-pscp-psftp-p ()
  "Check, whether the locale host runs MS Windows, and ps{cp,ftp} is used.
This does not support utf8 based file transfer."
  (and (eq system-type 'windows-nt)
       (string-match
	(regexp-opt '("pscp" "psftp"))
	(file-remote-p tramp-test-temporary-file-directory 'method))))

(defun tramp--test-windows-nt-or-smb-p ()
  "Check, whether the locale or remote host runs MS Windows.
This requires restrictions of file name syntax."
  (or (eq system-type 'windows-nt)
      (tramp-smb-file-name-p tramp-test-temporary-file-directory)))

(defun tramp--test-check-files (&rest files)
  "Run a simple but comprehensive test over every file in FILES."
  ;; TODO: The quoted case does not work.
  ;;(dolist (quoted (if tramp--test-expensive-test '(nil t) '(nil)))
  (let (quoted)
    ;; We must use `file-truename' for the temporary directory,
    ;; because it could be located on a symlinked directory.  This
    ;; would let the test fail.
    (let* ((tramp-test-temporary-file-directory
	    (file-truename tramp-test-temporary-file-directory))
	   (tmp-name1 (tramp--test-make-temp-name nil quoted))
	   (tmp-name2 (tramp--test-make-temp-name 'local quoted))
	   (files (delq nil files))
	   (process-environment process-environment))
      (unwind-protect
	  (progn
	    (make-directory tmp-name1)
	    (make-directory tmp-name2)

	    (dolist (elt files)
	      (let* ((file1 (expand-file-name elt tmp-name1))
		     (file2 (expand-file-name elt tmp-name2))
		     (file3 (expand-file-name (concat elt "foo") tmp-name1)))
		(write-region elt nil file1)
		(should (file-exists-p file1))

		;; Check file contents.
		(with-temp-buffer
		  (insert-file-contents file1)
		  (should (string-equal (buffer-string) elt)))

		;; Copy file both directions.
		(copy-file file1 tmp-name2)
		(should (file-exists-p file2))
		(delete-file file1)
		(should-not (file-exists-p file1))
		(copy-file file2 tmp-name1)
		(should (file-exists-p file1))

		;; Method "smb" supports `make-symbolic-link' only if the
		;; remote host has CIFS capabilities.  tramp-adb.el and
		;; tramp-gvfs.el do not support symbolic links at all.
		(condition-case err
		    (progn
		      (make-symbolic-link file1 file3)
		      (should (file-symlink-p file3))
		      (should
		       (string-equal
			(expand-file-name file1) (file-truename file3)))
		      (should
		       (string-equal
			(funcall
			 (if quoted 'tramp-compat-file-name-quote 'identity)
			 (car (file-attributes file3)))
			(file-remote-p (file-truename file1) 'localname)))
		      ;; Check file contents.
		      (with-temp-buffer
			(insert-file-contents file3)
			(should (string-equal (buffer-string) elt)))
		      (delete-file file3))
		  (file-error
		   (should
		    (string-equal (error-message-string err)
				  "make-symbolic-link not supported"))))))

	    ;; Check file names.
	    (should (equal (directory-files
			    tmp-name1 nil directory-files-no-dot-files-regexp)
			   (sort (copy-sequence files) 'string-lessp)))
	    (should (equal (directory-files
			    tmp-name2 nil directory-files-no-dot-files-regexp)
			   (sort (copy-sequence files) 'string-lessp)))

	    ;; `substitute-in-file-name' could return different
	    ;; values.  For `adb', there could be strange file
	    ;; permissions preventing overwriting a file.  We don't
	    ;; care in this testcase.
	    (dolist (elt files)
	      (let ((file1
		     (substitute-in-file-name (expand-file-name elt tmp-name1)))
		    (file2
		     (substitute-in-file-name
		      (expand-file-name elt tmp-name2))))
		(ignore-errors (write-region elt nil file1))
		(should (file-exists-p file1))
		(ignore-errors (write-region elt nil file2 nil 'nomessage))
		(should (file-exists-p file2))))

	    (should (equal (directory-files
			    tmp-name1 nil directory-files-no-dot-files-regexp)
			   (directory-files
			    tmp-name2 nil directory-files-no-dot-files-regexp)))

	    ;; Check directory creation.  We use a subdirectory "foo"
	    ;; in order to avoid conflicts with previous file name tests.
	    (dolist (elt files)
	      (let* ((elt1 (concat elt "foo"))
		     (file1 (expand-file-name (concat "foo/" elt) tmp-name1))
		     (file2 (expand-file-name elt file1))
		     (file3 (expand-file-name elt1 file1)))
		(make-directory file1 'parents)
		(should (file-directory-p file1))
		(write-region elt nil file2)
		(should (file-exists-p file2))
		(should
		 (equal
		  (directory-files
		   file1 nil directory-files-no-dot-files-regexp)
		  `(,elt)))
		(should
		 (equal
		  (caar (directory-files-and-attributes
			 file1 nil directory-files-no-dot-files-regexp))
		  elt))

		;; Check symlink in `directory-files-and-attributes'.
		(condition-case err
		    (progn
		      (make-symbolic-link file2 file3)
		      (should (file-symlink-p file3))
		      (should
		       (string-equal
			(caar (directory-files-and-attributes
			       file1 nil (regexp-quote elt1)))
			elt1))
		      (should
		       (string-equal
			(funcall
			 (if quoted 'tramp-compat-file-name-quote 'identity)
			 (cadr (car (directory-files-and-attributes
				     file1 nil (regexp-quote elt1)))))
			(file-remote-p (file-truename file2) 'localname)))
		      (delete-file file3)
		      (should-not (file-exists-p file3)))
		  (file-error
		   (should (string-equal (error-message-string err)
					 "make-symbolic-link not supported"))))

		(delete-file file2)
		(should-not (file-exists-p file2))
		(delete-directory file1)
		(should-not (file-exists-p file1))))

	    ;; Check, that environment variables are set correctly.
	    (when (and tramp--test-expensive-test (tramp--test-sh-p))
	      (dolist (elt files)
		(let ((envvar (concat "VAR_" (upcase (md5 elt))))
		      (default-directory tramp-test-temporary-file-directory)
		      (process-environment process-environment))
		  (setenv envvar elt)
		  ;; The value of PS1 could confuse Tramp's detection
		  ;; of process output.  So we unset it temporarily.
		  (setenv "PS1")
		  (with-temp-buffer
		    (should (zerop (process-file "env" nil t nil)))
		    (goto-char (point-min))
		    (should
		     (re-search-forward
		      (format
		       "^%s=%s$"
		       (regexp-quote envvar)
		       (regexp-quote (getenv envvar))))))))))

	;; Cleanup.
	(ignore-errors (delete-directory tmp-name1 'recursive))
	(ignore-errors (delete-directory tmp-name2 'recursive))))))

(defun tramp--test-special-characters ()
  "Perform the test in `tramp-test33-special-characters*'."
  ;; Newlines, slashes and backslashes in file names are not
  ;; supported.  So we don't test.  And we don't test the tab
  ;; character on Windows or Cygwin, because the backslash is
  ;; interpreted as a path separator, preventing "\t" from being
  ;; expanded to <TAB>.
  (tramp--test-check-files
   (if (or (tramp--test-gvfs-p) (tramp--test-windows-nt-or-smb-p))
       "foo bar baz"
     (if (or (tramp--test-adb-p)
	     (tramp--test-docker-p)
	     (eq system-type 'cygwin))
	 " foo bar baz "
       " foo\tbar baz\t"))
   "$foo$bar$$baz$"
   "-foo-bar-baz-"
   "%foo%bar%baz%"
   "&foo&bar&baz&"
   (unless (or (tramp--test-ftp-p)
	       (tramp--test-gvfs-p)
	       (tramp--test-windows-nt-or-smb-p))
     "?foo?bar?baz?")
   (unless (or (tramp--test-ftp-p)
	       (tramp--test-gvfs-p)
	       (tramp--test-windows-nt-or-smb-p))
     "*foo*bar*baz*")
   (if (or (tramp--test-gvfs-p) (tramp--test-windows-nt-or-smb-p))
       "'foo'bar'baz'"
     "'foo\"bar'baz\"")
   "#foo~bar#baz~"
   (if (or (tramp--test-gvfs-p) (tramp--test-windows-nt-or-smb-p))
       "!foo!bar!baz!"
     "!foo|bar!baz|")
   (if (or (tramp--test-gvfs-p) (tramp--test-windows-nt-or-smb-p))
       ";foo;bar;baz;"
     ":foo;bar:baz;")
   (unless (or (tramp--test-gvfs-p) (tramp--test-windows-nt-or-smb-p))
     "<foo>bar<baz>")
   "(foo)bar(baz)"
   (unless (or (tramp--test-ftp-p) (tramp--test-gvfs-p)) "[foo]bar[baz]")
   "{foo}bar{baz}"))

;; These tests are inspired by Bug#17238.
(ert-deftest tramp-test33-special-characters ()
  "Check special characters in file names."
  (skip-unless (tramp--test-enabled))
  (skip-unless (not (tramp--test-rsync-p)))
  (skip-unless (not (tramp--test-windows-nt-and-pscp-psftp-p)))

  (tramp--test-special-characters))

(ert-deftest tramp-test33-special-characters-with-stat ()
  "Check special characters in file names.
Use the `stat' command."
  :tags '(:expensive-test)
  (skip-unless (tramp--test-enabled))
  (skip-unless (tramp--test-sh-p))
  (skip-unless (not (tramp--test-rsync-p)))
  (skip-unless (not (tramp--test-windows-nt-and-pscp-psftp-p)))
  (with-parsed-tramp-file-name tramp-test-temporary-file-directory nil
    (skip-unless (tramp-get-remote-stat v)))

  (let ((tramp-connection-properties
	 (append
	  `((,(regexp-quote (file-remote-p tramp-test-temporary-file-directory))
	     "perl" nil))
	  tramp-connection-properties)))
    (tramp--test-special-characters)))

(ert-deftest tramp-test33-special-characters-with-perl ()
  "Check special characters in file names.
Use the `perl' command."
  :tags '(:expensive-test)
  (skip-unless (tramp--test-enabled))
  (skip-unless (tramp--test-sh-p))
  (skip-unless (not (tramp--test-rsync-p)))
  (skip-unless (not (tramp--test-windows-nt-and-pscp-psftp-p)))
  (with-parsed-tramp-file-name tramp-test-temporary-file-directory nil
    (skip-unless (tramp-get-remote-perl v)))

  (let ((tramp-connection-properties
	 (append
	  `((,(regexp-quote (file-remote-p tramp-test-temporary-file-directory))
	     "stat" nil)
	    ;; See `tramp-sh-handle-file-truename'.
	    (,(regexp-quote (file-remote-p tramp-test-temporary-file-directory))
	     "readlink" nil))
	  tramp-connection-properties)))
    (tramp--test-special-characters)))

(ert-deftest tramp-test33-special-characters-with-ls ()
  "Check special characters in file names.
Use the `ls' command."
  :tags '(:expensive-test)
  (skip-unless (tramp--test-enabled))
  (skip-unless (tramp--test-sh-p))
  (skip-unless (not (tramp--test-rsync-p)))
  (skip-unless (not (tramp--test-windows-nt-and-batch)))
  (skip-unless (not (tramp--test-windows-nt-and-pscp-psftp-p)))

  (let ((tramp-connection-properties
	 (append
	  `((,(regexp-quote (file-remote-p tramp-test-temporary-file-directory))
	     "perl" nil)
	    (,(regexp-quote (file-remote-p tramp-test-temporary-file-directory))
	     "stat" nil)
	    ;; See `tramp-sh-handle-file-truename'.
	    (,(regexp-quote (file-remote-p tramp-test-temporary-file-directory))
	     "readlink" nil))
	  tramp-connection-properties)))
    (tramp--test-special-characters)))

(defun tramp--test-utf8 ()
  "Perform the test in `tramp-test34-utf8*'."
  (let* ((utf8 (if (and (eq system-type 'darwin)
			(memq 'utf-8-hfs (coding-system-list)))
		   'utf-8-hfs 'utf-8))
	 (coding-system-for-read utf8)
	 (coding-system-for-write utf8)
	 (file-name-coding-system utf8))
    (tramp--test-check-files
     (unless (tramp--test-hpux-p) "Γυρίστε το Γαλαξία με Ώτο Στοπ")
     (unless (tramp--test-hpux-p)
       "أصبح بوسعك الآن تنزيل نسخة كاملة من موسوعة ويكيبيديا العربية لتصفحها بلا اتصال بالإنترنت")
     "银河系漫游指南系列"
     "Автостопом по гала́ктике")))

(ert-deftest tramp-test34-utf8 ()
  "Check UTF8 encoding in file names and file contents."
  (skip-unless (tramp--test-enabled))
  (skip-unless (not (tramp--test-docker-p)))
  (skip-unless (not (tramp--test-rsync-p)))
  (skip-unless (not (tramp--test-windows-nt-and-batch)))
  (skip-unless (not (tramp--test-windows-nt-and-pscp-psftp-p)))

  (tramp--test-utf8))

(ert-deftest tramp-test34-utf8-with-stat ()
  "Check UTF8 encoding in file names and file contents.
Use the `stat' command."
  :tags '(:expensive-test)
  (skip-unless (tramp--test-enabled))
  (skip-unless (tramp--test-sh-p))
  (skip-unless (not (tramp--test-docker-p)))
  (skip-unless (not (tramp--test-rsync-p)))
  (skip-unless (not (tramp--test-windows-nt-and-batch)))
  (skip-unless (not (tramp--test-windows-nt-and-pscp-psftp-p)))
  (with-parsed-tramp-file-name tramp-test-temporary-file-directory nil
    (skip-unless (tramp-get-remote-stat v)))

  (let ((tramp-connection-properties
	 (append
	  `((,(regexp-quote (file-remote-p tramp-test-temporary-file-directory))
	     "perl" nil))
	  tramp-connection-properties)))
    (tramp--test-utf8)))

(ert-deftest tramp-test34-utf8-with-perl ()
  "Check UTF8 encoding in file names and file contents.
Use the `perl' command."
  :tags '(:expensive-test)
  (skip-unless (tramp--test-enabled))
  (skip-unless (tramp--test-sh-p))
  (skip-unless (not (tramp--test-docker-p)))
  (skip-unless (not (tramp--test-rsync-p)))
  (skip-unless (not (tramp--test-windows-nt-and-batch)))
  (skip-unless (not (tramp--test-windows-nt-and-pscp-psftp-p)))
  (with-parsed-tramp-file-name tramp-test-temporary-file-directory nil
    (skip-unless (tramp-get-remote-perl v)))

  (let ((tramp-connection-properties
	 (append
	  `((,(regexp-quote (file-remote-p tramp-test-temporary-file-directory))
	     "stat" nil)
	    ;; See `tramp-sh-handle-file-truename'.
	    (,(regexp-quote (file-remote-p tramp-test-temporary-file-directory))
	     "readlink" nil))
	  tramp-connection-properties)))
    (tramp--test-utf8)))

(ert-deftest tramp-test34-utf8-with-ls ()
  "Check UTF8 encoding in file names and file contents.
Use the `ls' command."
  :tags '(:expensive-test)
  (skip-unless (tramp--test-enabled))
  (skip-unless (tramp--test-sh-p))
  (skip-unless (not (tramp--test-docker-p)))
  (skip-unless (not (tramp--test-rsync-p)))
  (skip-unless (not (tramp--test-windows-nt-and-batch)))
  (skip-unless (not (tramp--test-windows-nt-and-pscp-psftp-p)))

  (let ((tramp-connection-properties
	 (append
	  `((,(regexp-quote (file-remote-p tramp-test-temporary-file-directory))
	     "perl" nil)
	    (,(regexp-quote (file-remote-p tramp-test-temporary-file-directory))
	     "stat" nil)
	    ;; See `tramp-sh-handle-file-truename'.
	    (,(regexp-quote (file-remote-p tramp-test-temporary-file-directory))
	     "readlink" nil))
	  tramp-connection-properties)))
    (tramp--test-utf8)))

;; This test is inspired by Bug#16928.
(ert-deftest tramp-test35-asynchronous-requests ()
  "Check parallel asynchronous requests.
Such requests could arrive from timers, process filters and
process sentinels.  They shall not disturb each other."
  ;; Mark as failed until bug has been fixed.
  :expected-result :failed
  :tags '(:expensive-test)
  (skip-unless (tramp--test-enabled))
  (skip-unless (tramp--test-sh-p))

  (dolist (quoted (if tramp--test-expensive-test '(nil t) '(nil)))
    ;; Keep instrumentation verbosity 0 until Tramp bug is fixed.
    ;; This has the side effect, that this test fails instead to
    ;; abort.  Good for hydra.
    (tramp--instrument-test-case 0
    (let* ((tmp-name (tramp--test-make-temp-name nil quoted))
	   (default-directory tmp-name)
	   (remote-file-name-inhibit-cache t)
	   timer buffers kill-buffer-query-functions)

      (unwind-protect
	  (progn
	    (make-directory tmp-name)

	    ;; Setup a timer in order to raise an ordinary command
	    ;; again and again.  `vc-registered' is well suited,
	    ;; because there are many checks.
	    (setq
	     timer
	     (run-at-time
	      0 1
	      (lambda ()
		(when buffers
		  (vc-registered
		   (buffer-name (nth (random (length buffers)) buffers)))))))

	    ;; Create temporary buffers.  The number of buffers
	    ;; corresponds to the number of processes; it could be
	    ;; increased in order to make pressure on Tramp.
	    (dotimes (_i 5)
	      (add-to-list 'buffers (generate-new-buffer "*temp*")))

	    ;; Open asynchronous processes.  Set process sentinel.
	    (dolist (buf buffers)
	      (async-shell-command "read line; touch $line; echo $line" buf)
	      (set-process-sentinel
	       (get-buffer-process buf)
	       (lambda (proc _state)
		 (delete-file (buffer-name (process-buffer proc))))))

	    ;; Send a string.  Use a random order of the buffers.  Mix
	    ;; with regular operation.
	    (let ((buffers (copy-sequence buffers))
		  buf)
	      (while buffers
		(setq buf (nth (random (length buffers)) buffers))
		(process-send-string
		 (get-buffer-process buf) (format "'%s'\n" buf))
		(file-attributes (buffer-name buf))
		(setq buffers (delq buf buffers))))

	    ;; Wait until the whole output has been read.
	    (with-timeout ((* 10 (length buffers))
			   (ert-fail "`async-shell-command' timed out"))
	      (let ((buffers (copy-sequence buffers))
		    buf)
		(while buffers
		  (setq buf (nth (random (length buffers)) buffers))
		  (if (ignore-errors
			(memq (process-status (get-buffer-process buf))
			      '(run open)))
		      (accept-process-output (get-buffer-process buf) 0.1)
		    (setq buffers (delq buf buffers))))))

	    ;; Check.
	    (dolist (buf buffers)
	      (with-current-buffer buf
		(should
		 (string-equal (format "'%s'\n" buf) (buffer-string)))))
	    (should-not
	     (directory-files
	      tmp-name nil directory-files-no-dot-files-regexp)))

	;; Cleanup.
	(ignore-errors (cancel-timer timer))
	(ignore-errors (delete-directory tmp-name 'recursive))
	(dolist (buf buffers)
	  (ignore-errors (kill-buffer buf))))))))

(ert-deftest tramp-test36-recursive-load ()
  "Check that Tramp does not fail due to recursive load."
  (skip-unless (tramp--test-enabled))

  (dolist (code
	   (list
	    (format "(expand-file-name %S)" tramp-test-temporary-file-directory)
	    (format
	     "(let ((default-directory %S)) (expand-file-name %S))"
	     tramp-test-temporary-file-directory
	     temporary-file-directory)))
    (should-not
     (string-match
      "Recursive load"
      (shell-command-to-string
       (format
	"%s -batch -Q -L %s --eval %s"
	(expand-file-name invocation-name invocation-directory)
	(mapconcat 'shell-quote-argument load-path " -L ")
	(shell-quote-argument code)))))))

(ert-deftest tramp-test37-unload ()
  "Check that Tramp and its subpackages unload completely.
Since it unloads Tramp, it shall be the last test to run."
  ;; Mark as failed until all symbols are unbound.
  :expected-result (if (featurep 'tramp) :failed :passed)
  :tags '(:expensive-test)
  (when (featurep 'tramp)
    (unload-feature 'tramp 'force)
    ;; No Tramp feature must be left.
    (should-not (featurep 'tramp))
    (should-not (all-completions "tramp" (delq 'tramp-tests features)))
    ;; `file-name-handler-alist' must be clean.
    (should-not (all-completions "tramp" (mapcar 'cdr file-name-handler-alist)))
    ;; There shouldn't be left a bound symbol.  We do not regard our
    ;; test symbols, and the Tramp unload hooks.
    (mapatoms
     (lambda (x)
       (and (or (boundp x) (functionp x))
	    (string-match "^tramp" (symbol-name x))
	    (not (string-match "^tramp--?test" (symbol-name x)))
	    (not (string-match "unload-hook$" (symbol-name x)))
	    (ert-fail (format "`%s' still bound" x)))))
    ;; There shouldn't be left a hook function containing a Tramp
    ;; function.  We do not regard the Tramp unload hooks.
    (mapatoms
     (lambda (x)
       (and (boundp x)
	    (string-match "-hooks?$" (symbol-name x))
	    (not (string-match "unload-hook$" (symbol-name x)))
	    (consp (symbol-value x))
	    (ignore-errors (all-completions "tramp" (symbol-value x)))
	    (ert-fail (format "Hook `%s' still contains Tramp function" x)))))))

;; TODO:

;; * dired-compress-file
;; * dired-uncache
;; * file-acl
;; * file-name-case-insensitive-p
;; * file-selinux-context
;; * find-backup-file-name
;; * set-file-acl
;; * set-file-selinux-context

;; * Work on skipped tests.  Make a comment, when it is impossible.
;; * Fix `tramp-test06-directory-file-name' for `ftp'.
;; * Fix `tramp-test27-start-file-process' on MS Windows (`process-send-eof'?).
;; * Fix Bug#16928.  Set expected error of `tramp-test35-asynchronous-requests'.
;; * Fix `tramp-test37-unload' (Not all symbols are unbound).  Set
;;   expected error.

(defun tramp-test-all (&optional interactive)
  "Run all tests for \\[tramp]."
  (interactive "p")
  (funcall
   (if interactive 'ert-run-tests-interactively 'ert-run-tests-batch) "^tramp"))

(provide 'tramp-tests)
;;; tramp-tests.el ends here

debug log:

solving 2a4ef74 ...
found 2a4ef74 in https://git.savannah.gnu.org/cgit/emacs.git

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).