1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989
|
/*
This file is part of the KDE project
SPDX-FileCopyrightText: 2004-2006 David Faure <faure@kde.org>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include "jobtest.h"
#include "mockcoredelegateextensions.h"
#include "kio/job.h"
#include "kiotesthelper.h" // createTestFile etc.
#include <kio/chmodjob.h>
#include <kio/copyjob.h>
#include <kio/deletejob.h>
#include <kio/directorysizejob.h>
#include <kio/statjob.h>
#include <kmountpoint.h>
#include <kprotocolinfo.h>
#include <KJobUiDelegate>
#include <KLocalizedString>
#include <QBuffer>
#include <QDebug>
#include <QDir>
#include <QElapsedTimer>
#include <QEventLoop>
#include <QFileInfo>
#include <QHash>
#include <QPointer>
#include <QProcess>
#include <QSignalSpy>
#include <QTemporaryFile>
#include <QTest>
#include <QTimer>
#include <QUrl>
#include <QVariant>
#ifndef Q_OS_WIN
#include <unistd.h> // for readlink
#endif
QTEST_MAIN(JobTest)
// The code comes partly from kdebase/kioslave/trash/testtrash.cpp
static QString otherTmpDir()
{
#ifdef Q_OS_WIN
return QDir::tempPath() + "/jobtest/";
#else
// This one needs to be on another partition, but we can't guarantee that it is
// On CI, it typically isn't...
return QStringLiteral("/tmp/jobtest/");
#endif
}
static bool otherTmpDirIsOnSamePartition() // true on CI because it's a LXC container
{
KMountPoint::Ptr srcMountPoint = KMountPoint::currentMountPoints().findByPath(homeTmpDir());
KMountPoint::Ptr destMountPoint = KMountPoint::currentMountPoints().findByPath(otherTmpDir());
Q_ASSERT(srcMountPoint);
Q_ASSERT(destMountPoint);
return srcMountPoint->mountedFrom() == destMountPoint->mountedFrom();
}
void JobTest::initTestCase()
{
QStandardPaths::setTestModeEnabled(true);
QCoreApplication::instance()->setApplicationName("kio/jobtest"); // testing for #357499
// to make sure io is not too fast
qputenv("KIOSLAVE_FILE_ENABLE_TESTMODE", "1");
s_referenceTimeStamp = QDateTime::currentDateTime().addSecs(-30); // 30 seconds ago
// Start with a clean base dir
cleanupTestCase();
homeTmpDir(); // create it
if (!QFile::exists(otherTmpDir())) {
bool ok = QDir().mkdir(otherTmpDir());
if (!ok) {
qFatal("couldn't create %s", qPrintable(otherTmpDir()));
}
}
/*****
* Set platform xattr related commands.
* Linux commands: setfattr, getfattr
* BSD commands: setextattr, getextattr
* MacOS commands: xattr -w, xattr -p
****/
m_getXattrCmd = QStandardPaths::findExecutable("getfattr");
if (m_getXattrCmd.endsWith("getfattr")) {
m_setXattrCmd = QStandardPaths::findExecutable("setfattr");
m_setXattrFormatArgs = [](const QString &attrName, const QString &value, const QString &fileName) {
return QStringList{QLatin1String("-n"), attrName, QLatin1String("-v"), value, fileName};
};
} else {
// On BSD there is lsextattr to list all xattrs and getextattr to get a value
// for specific xattr. For test purposes lsextattr is more suitable to be used
// as m_getXattrCmd, so search for it instead of getextattr.
m_getXattrCmd = QStandardPaths::findExecutable("lsextattr");
if (m_getXattrCmd.endsWith("lsextattr")) {
m_setXattrCmd = QStandardPaths::findExecutable("setextattr");
m_setXattrFormatArgs = [](const QString &attrName, const QString &value, const QString &fileName) {
return QStringList{QLatin1String("user"), attrName, value, fileName};
};
} else {
m_getXattrCmd = QStandardPaths::findExecutable("xattr");
m_setXattrFormatArgs = [](const QString &attrName, const QString &value, const QString &fileName) {
return QStringList{QLatin1String("-w"), attrName, value, fileName};
};
if (!m_getXattrCmd.endsWith("xattr")) {
qWarning() << "Neither getfattr, getextattr nor xattr was found.";
}
}
}
qRegisterMetaType<KJob *>("KJob*");
qRegisterMetaType<KIO::Job *>("KIO::Job*");
qRegisterMetaType<QDateTime>("QDateTime");
}
void JobTest::cleanupTestCase()
{
QDir(homeTmpDir()).removeRecursively();
QDir(otherTmpDir()).removeRecursively();
}
struct ScopedCleaner {
using Func = std::function<void()>;
explicit ScopedCleaner(Func f)
: m_f(std::move(f))
{
}
~ScopedCleaner()
{
m_f();
}
private:
const Func m_f;
};
void JobTest::enterLoop()
{
QEventLoop eventLoop;
connect(this, &JobTest::exitLoop, &eventLoop, &QEventLoop::quit);
eventLoop.exec(QEventLoop::ExcludeUserInputEvents);
}
void JobTest::storedGet()
{
// qDebug();
const QString filePath = homeTmpDir() + "fileFromHome";
createTestFile(filePath);
QUrl u = QUrl::fromLocalFile(filePath);
m_result = -1;
KIO::StoredTransferJob *job = KIO::storedGet(u, KIO::NoReload, KIO::HideProgressInfo);
QSignalSpy spyPercent(job, &KJob::percentChanged);
QVERIFY(spyPercent.isValid());
job->setUiDelegate(nullptr);
connect(job, &KJob::result, this, &JobTest::slotGetResult);
enterLoop();
QCOMPARE(m_result, 0); // no error
QCOMPARE(m_data, QByteArray("Hello\0world", 11));
QCOMPARE(m_data.size(), 11);
QVERIFY(!spyPercent.isEmpty());
}
void JobTest::slotGetResult(KJob *job)
{
m_result = job->error();
m_data = static_cast<KIO::StoredTransferJob *>(job)->data();
Q_EMIT exitLoop();
}
void JobTest::put()
{
const QString filePath = homeTmpDir() + "fileFromHome";
QUrl u = QUrl::fromLocalFile(filePath);
KIO::TransferJob *job = KIO::put(u, 0600, KIO::Overwrite | KIO::HideProgressInfo);
quint64 secsSinceEpoch = QDateTime::currentSecsSinceEpoch(); // Use second granularity, supported on all filesystems
QDateTime mtime = QDateTime::fromSecsSinceEpoch(secsSinceEpoch - 30); // 30 seconds ago
job->setModificationTime(mtime);
job->setUiDelegate(nullptr);
connect(job, &KJob::result, this, &JobTest::slotResult);
connect(job, &KIO::TransferJob::dataReq, this, &JobTest::slotDataReq);
m_result = -1;
m_dataReqCount = 0;
enterLoop();
QVERIFY(m_result == 0); // no error
QFileInfo fileInfo(filePath);
QVERIFY(fileInfo.exists());
QCOMPARE(fileInfo.size(), 30LL); // "This is a test for KIO::put()\n"
QCOMPARE((int)fileInfo.permissions(), (int)(QFile::ReadOwner | QFile::WriteOwner | QFile::ReadUser | QFile::WriteUser));
QCOMPARE(fileInfo.lastModified(), mtime);
}
void JobTest::slotDataReq(KIO::Job *, QByteArray &data)
{
// Really not the way you'd write a slotDataReq usually :)
switch (m_dataReqCount++) {
case 0:
data = "This is a test for ";
break;
case 1:
data = "KIO::put()\n";
break;
case 2:
data = QByteArray();
break;
}
}
void JobTest::slotResult(KJob *job)
{
m_result = job->error();
Q_EMIT exitLoop();
}
void JobTest::storedPut()
{
const QString filePath = homeTmpDir() + "fileFromHome";
QUrl u = QUrl::fromLocalFile(filePath);
QByteArray putData = "This is the put data";
KIO::TransferJob *job = KIO::storedPut(putData, u, 0600, KIO::Overwrite | KIO::HideProgressInfo);
QSignalSpy spyPercent(job, &KJob::percentChanged);
QVERIFY(spyPercent.isValid());
quint64 secsSinceEpoch = QDateTime::currentSecsSinceEpoch(); // Use second granularity, supported on all filesystems
QDateTime mtime = QDateTime::fromSecsSinceEpoch(secsSinceEpoch - 30); // 30 seconds ago
job->setModificationTime(mtime);
job->setUiDelegate(nullptr);
QVERIFY2(job->exec(), qPrintable(job->errorString()));
QFileInfo fileInfo(filePath);
QVERIFY(fileInfo.exists());
QCOMPARE(fileInfo.size(), (long long)putData.size());
QCOMPARE((int)fileInfo.permissions(), (int)(QFile::ReadOwner | QFile::WriteOwner | QFile::ReadUser | QFile::WriteUser));
QCOMPARE(fileInfo.lastModified(), mtime);
QVERIFY(!spyPercent.isEmpty());
}
void JobTest::storedPutIODevice()
{
const QString filePath = homeTmpDir() + "fileFromHome";
QBuffer putData;
putData.setData("This is the put data");
QVERIFY(putData.open(QIODevice::ReadOnly));
KIO::TransferJob *job = KIO::storedPut(&putData, QUrl::fromLocalFile(filePath), 0600, KIO::Overwrite | KIO::HideProgressInfo);
QSignalSpy spyPercent(job, &KJob::percentChanged);
QVERIFY(spyPercent.isValid());
quint64 secsSinceEpoch = QDateTime::currentSecsSinceEpoch(); // Use second granularity, supported on all filesystems
QDateTime mtime = QDateTime::fromSecsSinceEpoch(secsSinceEpoch - 30); // 30 seconds ago
job->setModificationTime(mtime);
job->setUiDelegate(nullptr);
QVERIFY2(job->exec(), qPrintable(job->errorString()));
QFileInfo fileInfo(filePath);
QVERIFY(fileInfo.exists());
QCOMPARE(fileInfo.size(), (long long)putData.size());
QCOMPARE((int)fileInfo.permissions(), (int)(QFile::ReadOwner | QFile::WriteOwner | QFile::ReadUser | QFile::WriteUser));
QCOMPARE(fileInfo.lastModified(), mtime);
QVERIFY(!spyPercent.isEmpty());
}
void JobTest::storedPutIODeviceFile()
{
// Given a source file and a destination file
const QString src = homeTmpDir() + "fileFromHome";
createTestFile(src);
QVERIFY(QFile::exists(src));
QFile srcFile(src);
QVERIFY(srcFile.open(QIODevice::ReadOnly));
const QString dest = homeTmpDir() + "fileFromHome_copied";
QFile::remove(dest);
const QUrl destUrl = QUrl::fromLocalFile(dest);
// When using storedPut with the QFile as argument
KIO::StoredTransferJob *job = KIO::storedPut(&srcFile, destUrl, 0600, KIO::Overwrite | KIO::HideProgressInfo);
// Then the copy should succeed and the dest file exist
QVERIFY2(job->exec(), qPrintable(job->errorString()));
QVERIFY(QFile::exists(dest));
QCOMPARE(QFileInfo(src).size(), QFileInfo(dest).size());
QFile::remove(dest);
}
void JobTest::storedPutIODeviceTempFile()
{
// Create a temp file in the current dir.
QTemporaryFile tempFile(QStringLiteral("jobtest-tmp"));
QVERIFY(tempFile.open());
// Write something into the file.
QTextStream stream(&tempFile);
stream << QStringLiteral("This is the put data");
stream.flush();
QVERIFY(QFileInfo(tempFile).size() > 0);
const QString dest = homeTmpDir() + QLatin1String("tmpfile-dest");
const QUrl destUrl = QUrl::fromLocalFile(dest);
// QTemporaryFiles are open in ReadWrite mode,
// so we don't need to close and reopen,
// but we need to rewind to the beginning.
tempFile.seek(0);
auto job = KIO::storedPut(&tempFile, destUrl, -1);
QVERIFY2(job->exec(), qPrintable(job->errorString()));
QVERIFY(QFileInfo::exists(dest));
QCOMPARE(QFileInfo(dest).size(), QFileInfo(tempFile).size());
QVERIFY(QFile::remove(dest));
}
void JobTest::storedPutIODeviceFastDevice()
{
const QString filePath = homeTmpDir() + "fileFromHome";
const QUrl u = QUrl::fromLocalFile(filePath);
const QByteArray putDataContents = "This is the put data";
QBuffer putDataBuffer;
QVERIFY(putDataBuffer.open(QIODevice::ReadWrite));
KIO::StoredTransferJob *job = KIO::storedPut(&putDataBuffer, u, 0600, KIO::Overwrite | KIO::HideProgressInfo);
QSignalSpy spyPercent(job, &KJob::percentChanged);
QVERIFY(spyPercent.isValid());
quint64 secsSinceEpoch = QDateTime::currentSecsSinceEpoch(); // Use second granularity, supported on all filesystems
QDateTime mtime = QDateTime::fromSecsSinceEpoch(secsSinceEpoch - 30); // 30 seconds ago
job->setModificationTime(mtime);
job->setTotalSize(putDataContents.size());
job->setUiDelegate(nullptr);
job->setAsyncDataEnabled(true);
// Emit the readChannelFinished even before the job has had time to start
const auto pos = putDataBuffer.pos();
int size = putDataBuffer.write(putDataContents);
putDataBuffer.seek(pos);
Q_EMIT putDataBuffer.readChannelFinished();
QVERIFY2(job->exec(), qPrintable(job->errorString()));
QCOMPARE(size, putDataContents.size());
QCOMPARE(putDataBuffer.bytesAvailable(), 0);
QFileInfo fileInfo(filePath);
QVERIFY(fileInfo.exists());
QCOMPARE(fileInfo.size(), (long long)putDataContents.size());
QCOMPARE((int)fileInfo.permissions(), (int)(QFile::ReadOwner | QFile::WriteOwner | QFile::ReadUser | QFile::WriteUser));
QCOMPARE(fileInfo.lastModified(), mtime);
QVERIFY(!spyPercent.isEmpty());
}
void JobTest::storedPutIODeviceSlowDevice()
{
const QString filePath = homeTmpDir() + "fileFromHome";
const QUrl u = QUrl::fromLocalFile(filePath);
const QByteArray putDataContents = "This is the put data";
QBuffer putDataBuffer;
QVERIFY(putDataBuffer.open(QIODevice::ReadWrite));
KIO::StoredTransferJob *job = KIO::storedPut(&putDataBuffer, u, 0600, KIO::Overwrite | KIO::HideProgressInfo);
QSignalSpy spyPercent(job, &KJob::percentChanged);
QVERIFY(spyPercent.isValid());
quint64 secsSinceEpoch = QDateTime::currentSecsSinceEpoch(); // Use second granularity, supported on all filesystems
QDateTime mtime = QDateTime::fromSecsSinceEpoch(secsSinceEpoch - 30); // 30 seconds ago
job->setModificationTime(mtime);
job->setTotalSize(putDataContents.size());
job->setUiDelegate(nullptr);
job->setAsyncDataEnabled(true);
int size = 0;
const auto writeOnce = [&putDataBuffer, &size, putDataContents]() {
const auto pos = putDataBuffer.pos();
size += putDataBuffer.write(putDataContents);
putDataBuffer.seek(pos);
// qDebug() << "written" << size;
};
QTimer::singleShot(200, this, writeOnce);
QTimer::singleShot(400, this, writeOnce);
// Simulate the transfer is done
QTimer::singleShot(450, this, [&putDataBuffer]() {
Q_EMIT putDataBuffer.readChannelFinished();
});
QVERIFY2(job->exec(), qPrintable(job->errorString()));
QCOMPARE(size, putDataContents.size() * 2);
QCOMPARE(putDataBuffer.bytesAvailable(), 0);
QFileInfo fileInfo(filePath);
QVERIFY(fileInfo.exists());
QCOMPARE(fileInfo.size(), (long long)putDataContents.size() * 2);
QCOMPARE((int)fileInfo.permissions(), (int)(QFile::ReadOwner | QFile::WriteOwner | QFile::ReadUser | QFile::WriteUser));
QCOMPARE(fileInfo.lastModified(), mtime);
QVERIFY(!spyPercent.isEmpty());
}
void JobTest::storedPutIODeviceSlowDeviceBigChunk()
{
const QString filePath = homeTmpDir() + "fileFromHome";
const QUrl u = QUrl::fromLocalFile(filePath);
const QByteArray putDataContents(300000, 'K'); // Make sure the 300000 is bigger than MAX_READ_BUF_SIZE
QBuffer putDataBuffer;
QVERIFY(putDataBuffer.open(QIODevice::ReadWrite));
KIO::StoredTransferJob *job = KIO::storedPut(&putDataBuffer, u, 0600, KIO::Overwrite | KIO::HideProgressInfo);
QSignalSpy spyPercent(job, &KJob::percentChanged);
QVERIFY(spyPercent.isValid());
quint64 secsSinceEpoch = QDateTime::currentSecsSinceEpoch(); // Use second granularity, supported on all filesystems
QDateTime mtime = QDateTime::fromSecsSinceEpoch(secsSinceEpoch - 30); // 30 seconds ago
job->setModificationTime(mtime);
job->setTotalSize(putDataContents.size());
job->setUiDelegate(nullptr);
job->setAsyncDataEnabled(true);
int size = 0;
const auto writeOnce = [&putDataBuffer, &size, putDataContents]() {
const auto pos = putDataBuffer.pos();
size += putDataBuffer.write(putDataContents);
putDataBuffer.seek(pos);
// qDebug() << "written" << size;
};
QTimer::singleShot(200, this, writeOnce);
// Simulate the transfer is done
QTimer::singleShot(450, this, [&putDataBuffer]() {
Q_EMIT putDataBuffer.readChannelFinished();
});
QVERIFY2(job->exec(), qPrintable(job->errorString()));
QCOMPARE(size, putDataContents.size());
QCOMPARE(putDataBuffer.bytesAvailable(), 0);
QFileInfo fileInfo(filePath);
QVERIFY(fileInfo.exists());
QCOMPARE(fileInfo.size(), (long long)putDataContents.size());
QCOMPARE((int)fileInfo.permissions(), (int)(QFile::ReadOwner | QFile::WriteOwner | QFile::ReadUser | QFile::WriteUser));
QCOMPARE(fileInfo.lastModified(), mtime);
QVERIFY(!spyPercent.isEmpty());
}
void JobTest::asyncStoredPutReadyReadAfterFinish()
{
const QString filePath = homeTmpDir() + "fileFromHome";
const QUrl u = QUrl::fromLocalFile(filePath);
QBuffer putDataBuffer;
QVERIFY(putDataBuffer.open(QIODevice::ReadWrite));
KIO::StoredTransferJob *job = KIO::storedPut(&putDataBuffer, u, 0600, KIO::Overwrite | KIO::HideProgressInfo);
job->setAsyncDataEnabled(true);
bool jobFinished = false;
connect(job, &KJob::finished, this, [&jobFinished, &putDataBuffer] {
putDataBuffer.readyRead();
jobFinished = true;
});
QTimer::singleShot(200, this, [job]() {
job->kill();
});
QTRY_VERIFY(jobFinished);
}
static QHash<QString, QString> getSampleXattrs()
{
QHash<QString, QString> attrs;
attrs["user.name with space"] = "value with spaces";
attrs["user.baloo.rating"] = "1";
attrs["user.fnewLine"] = "line1\\nline2";
attrs["user.flistNull"] = "item1\\0item2";
attrs["user.fattr.with.a.lot.of.namespaces"] = "true";
attrs["user.fempty"] = "";
return attrs;
}
bool JobTest::checkXattrFsSupport(const QString &dir)
{
const QString writeTest = dir + "/fsXattrTestFile";
createTestFile(writeTest);
bool ret = setXattr(writeTest);
QFile::remove(writeTest);
return ret;
}
bool JobTest::setXattr(const QString &dest)
{
QProcess xattrWriter;
xattrWriter.setProcessChannelMode(QProcess::MergedChannels);
QHash<QString, QString> attrs = getSampleXattrs();
QHashIterator<QString, QString> i(attrs);
while (i.hasNext()) {
i.next();
QStringList arguments = m_setXattrFormatArgs(i.key(), i.value(), dest);
xattrWriter.start(m_setXattrCmd, arguments);
xattrWriter.waitForStarted();
xattrWriter.waitForFinished(-1);
if (xattrWriter.exitStatus() != QProcess::NormalExit) {
return false;
}
QList<QByteArray> resultdest = xattrWriter.readAllStandardOutput().split('\n');
if (!resultdest[0].isEmpty()) {
QWARN("Error writing user xattr. Xattr copy tests will be disabled.");
qDebug() << resultdest;
return false;
}
}
return true;
}
QList<QByteArray> JobTest::readXattr(const QString &src)
{
QProcess xattrReader;
xattrReader.setProcessChannelMode(QProcess::MergedChannels);
QStringList arguments;
char outputSeparator = '\n';
// Linux
if (m_getXattrCmd.endsWith("getfattr")) {
arguments = QStringList{"-d", src};
}
// BSD
else if (m_getXattrCmd.endsWith("lsextattr")) {
arguments = QStringList{"-q", "user", src};
outputSeparator = '\t';
}
// MacOS
else {
arguments = QStringList{"-l", src};
}
xattrReader.start(m_getXattrCmd, arguments);
xattrReader.waitForFinished();
QList<QByteArray> result = xattrReader.readAllStandardOutput().split(outputSeparator);
if (m_getXattrCmd.endsWith("getfattr")) {
if (result.size() > 1) {
// Line 1 is the file name
result.removeAt(1);
}
} else if (m_getXattrCmd.endsWith("lsextattr")) {
// cut off trailing \n
result.last().chop(1);
// lsextattr does not sort its output
std::sort(result.begin(), result.end());
}
return result;
}
void JobTest::compareXattr(const QString &src, const QString &dest)
{
auto srcAttrs = readXattr(src);
auto dstAttrs = readXattr(dest);
QCOMPARE(dstAttrs, srcAttrs);
}
void JobTest::copyLocalFile(const QString &src, const QString &dest)
{
const QUrl u = QUrl::fromLocalFile(src);
const QUrl d = QUrl::fromLocalFile(dest);
const int perms = 0666;
// copy the file with file_copy
KIO::Job *job = KIO::file_copy(u, d, perms, KIO::HideProgressInfo);
job->setUiDelegate(nullptr);
QVERIFY2(job->exec(), qPrintable(job->errorString()));
QVERIFY(QFile::exists(dest));
QVERIFY(QFile::exists(src)); // still there
QCOMPARE(int(QFileInfo(dest).permissions()), int(0x6666));
compareXattr(src, dest);
{
// check that the timestamp is the same (#24443)
// Note: this only works because of copy() in kio_file.
// The datapump solution ignores mtime, the app has to call FileCopyJob::setModificationTime()
QFileInfo srcInfo(src);
QFileInfo destInfo(dest);
#ifdef Q_OS_WIN
// win32 time may differs in msec part
QCOMPARE(srcInfo.lastModified().toString("dd.MM.yyyy hh:mm"), destInfo.lastModified().toString("dd.MM.yyyy hh:mm"));
#else
QCOMPARE(srcInfo.lastModified(), destInfo.lastModified());
#endif
}
// cleanup and retry with KIO::copy()
QFile::remove(dest);
auto *copyjob = KIO::copy(u, d, KIO::HideProgressInfo);
QSignalSpy spyCopyingDone(copyjob, &KIO::CopyJob::copyingDone);
copyjob->setUiDelegate(nullptr);
copyjob->setUiDelegateExtension(nullptr);
QVERIFY2(copyjob->exec(), qPrintable(copyjob->errorString()));
QVERIFY(QFile::exists(dest));
QVERIFY(QFile::exists(src)); // still there
compareXattr(src, dest);
{
// check that the timestamp is the same (#24443)
QFileInfo srcInfo(src);
QFileInfo destInfo(dest);
#ifdef Q_OS_WIN
// win32 time may differs in msec part
QCOMPARE(srcInfo.lastModified().toString("dd.MM.yyyy hh:mm"), destInfo.lastModified().toString("dd.MM.yyyy hh:mm"));
#else
QCOMPARE(srcInfo.lastModified(), destInfo.lastModified());
#endif
}
QCOMPARE(spyCopyingDone.count(), 1);
QCOMPARE(copyjob->totalAmount(KJob::Files), 1);
QCOMPARE(copyjob->totalAmount(KJob::Directories), 0);
QCOMPARE(copyjob->processedAmount(KJob::Files), 1);
QCOMPARE(copyjob->processedAmount(KJob::Directories), 0);
QCOMPARE(copyjob->percent(), 100);
// cleanup and retry with KIO::copyAs()
QFile::remove(dest);
job = KIO::copyAs(u, d, KIO::HideProgressInfo);
job->setUiDelegate(nullptr);
job->setUiDelegateExtension(nullptr);
QVERIFY2(job->exec(), qPrintable(job->errorString()));
QVERIFY(QFile::exists(dest));
QVERIFY(QFile::exists(src)); // still there
compareXattr(src, dest);
// Do it again, with Overwrite.
job = KIO::copyAs(u, d, KIO::Overwrite | KIO::HideProgressInfo);
job->setUiDelegate(nullptr);
job->setUiDelegateExtension(nullptr);
QVERIFY2(job->exec(), qPrintable(job->errorString()));
QVERIFY(QFile::exists(dest));
QVERIFY(QFile::exists(src)); // still there
compareXattr(src, dest);
// Do it again, without Overwrite (should fail).
job = KIO::copyAs(u, d, KIO::HideProgressInfo);
job->setUiDelegate(nullptr);
job->setUiDelegateExtension(nullptr);
QVERIFY(!job->exec());
// Clean up
QFile::remove(src);
QFile::remove(dest);
}
void JobTest::copyLocalDirectory(const QString &src, const QString &_dest, int flags)
{
QVERIFY(QFileInfo(src).isDir());
QVERIFY(QFileInfo(src + "/testfile").isFile());
QUrl u = QUrl::fromLocalFile(src);
QString dest(_dest);
QUrl d = QUrl::fromLocalFile(dest);
if (flags & AlreadyExists) {
QVERIFY(QFile::exists(dest));
} else {
QVERIFY(!QFile::exists(dest));
}
KIO::Job *job = KIO::copy(u, d, KIO::HideProgressInfo);
job->setUiDelegate(nullptr);
job->setUiDelegateExtension(nullptr);
QVERIFY2(job->exec(), qPrintable(job->errorString()));
QVERIFY(QFile::exists(dest));
QVERIFY(QFileInfo(dest).isDir());
QVERIFY(QFileInfo(dest + "/testfile").isFile());
QVERIFY(QFile::exists(src)); // still there
if (flags & AlreadyExists) {
dest += '/' + u.fileName();
// qDebug() << "Expecting dest=" << dest;
}
// CopyJob::setNextDirAttribute isn't implemented for Windows currently.
#ifndef Q_OS_WIN
{
// Check that the timestamp is the same (#24443)
QFileInfo srcInfo(src);
QFileInfo destInfo(dest);
QCOMPARE(srcInfo.lastModified(), destInfo.lastModified());
}
#endif
QCOMPARE(job->totalAmount(KJob::Files), 2); // testfile and testlink
QCOMPARE(job->totalAmount(KJob::Directories), 1);
QCOMPARE(job->processedAmount(KJob::Files), 2);
QCOMPARE(job->processedAmount(KJob::Directories), 1);
QCOMPARE(job->percent(), 100);
// Do it again, with Overwrite.
// Use copyAs, we don't want a subdir inside d.
job = KIO::copyAs(u, d, KIO::HideProgressInfo | KIO::Overwrite);
job->setUiDelegate(nullptr);
job->setUiDelegateExtension(nullptr);
QVERIFY2(job->exec(), qPrintable(job->errorString()));
QCOMPARE(job->totalAmount(KJob::Files), 2); // testfile and testlink
QCOMPARE(job->totalAmount(KJob::Directories), 1);
QCOMPARE(job->processedAmount(KJob::Files), 2);
QCOMPARE(job->processedAmount(KJob::Directories), 1);
QCOMPARE(job->percent(), 100);
// Do it again, without Overwrite (should fail).
job = KIO::copyAs(u, d, KIO::HideProgressInfo);
job->setUiDelegate(nullptr);
job->setUiDelegateExtension(nullptr);
QVERIFY(!job->exec());
}
#ifndef Q_OS_WIN
static QString linkTarget(const QString &path)
{
// Use readlink on Unix because symLinkTarget turns relative targets into absolute (#352927)
char linkTargetBuffer[4096];
const int n = readlink(QFile::encodeName(path).constData(), linkTargetBuffer, sizeof(linkTargetBuffer) - 1);
if (n != -1) {
linkTargetBuffer[n] = 0;
}
return QFile::decodeName(linkTargetBuffer);
}
static void copyLocalSymlink(const QString &src, const QString &dest, const QString &expectedLinkTarget)
{
QT_STATBUF buf;
QVERIFY(QT_LSTAT(QFile::encodeName(src).constData(), &buf) == 0);
QUrl u = QUrl::fromLocalFile(src);
QUrl d = QUrl::fromLocalFile(dest);
// copy the symlink
KIO::Job *job = KIO::copy(u, d, KIO::HideProgressInfo);
job->setUiDelegate(nullptr);
job->setUiDelegateExtension(nullptr);
QVERIFY2(job->exec(), qPrintable(QString::number(job->error())));
QVERIFY(QT_LSTAT(QFile::encodeName(dest).constData(), &buf) == 0); // dest exists
QCOMPARE(linkTarget(dest), expectedLinkTarget);
// cleanup
QFile::remove(dest);
}
#endif
void JobTest::copyFileToSamePartition()
{
const QString homeDir = homeTmpDir();
const QString filePath = homeDir + "fileFromHome";
const QString dest = homeDir + "fileFromHome_copied";
createTestFile(filePath);
if (checkXattrFsSupport(homeDir)) {
setXattr(filePath);
}
copyLocalFile(filePath, dest);
}
void JobTest::copyDirectoryToSamePartition()
{
// qDebug();
const QString src = homeTmpDir() + "dirFromHome";
const QString dest = homeTmpDir() + "dirFromHome_copied";
createTestDirectory(src);
copyLocalDirectory(src, dest);
}
void JobTest::copyDirectoryToExistingDirectory()
{
// qDebug();
// just the same as copyDirectoryToSamePartition, but this time dest exists.
// So we get a subdir, "dirFromHome_copy/dirFromHome"
const QString src = homeTmpDir() + "dirFromHome";
const QString dest = homeTmpDir() + "dirFromHome_copied";
createTestDirectory(src);
createTestDirectory(dest);
copyLocalDirectory(src, dest, AlreadyExists);
}
void JobTest::copyDirectoryToExistingSymlinkedDirectory()
{
// qDebug();
// just the same as copyDirectoryToSamePartition, but this time dest is a symlink.
// So we get a file in the symlink dir, "dirFromHome_symlink/dirFromHome" and
// "dirFromHome_symOrigin/dirFromHome"
const QString src = homeTmpDir() + "dirFromHome";
const QString origSymlink = homeTmpDir() + "dirFromHome_symOrigin";
const QString targetSymlink = homeTmpDir() + "dirFromHome_symlink";
createTestDirectory(src);
createTestDirectory(origSymlink);
bool ok = KIOPrivate::createSymlink(origSymlink, targetSymlink);
if (!ok) {
qFatal("couldn't create symlink: %s", strerror(errno));
}
QVERIFY(QFileInfo(targetSymlink).isSymLink());
QVERIFY(QFileInfo(targetSymlink).isDir());
KIO::Job *job = KIO::copy(QUrl::fromLocalFile(src), QUrl::fromLocalFile(targetSymlink), KIO::HideProgressInfo);
job->setUiDelegate(nullptr);
job->setUiDelegateExtension(nullptr);
QVERIFY2(job->exec(), qPrintable(job->errorString()));
QVERIFY(QFile::exists(src)); // still there
// file is visible in both places due to symlink
QVERIFY(QFileInfo(origSymlink + "/dirFromHome").isDir());
;
QVERIFY(QFileInfo(targetSymlink + "/dirFromHome").isDir());
QVERIFY(QDir(origSymlink).removeRecursively());
QVERIFY(QFile::remove(targetSymlink));
}
void JobTest::copyFileToOtherPartition()
{
// qDebug();
const QString homeDir = homeTmpDir();
const QString otherHomeDir = otherTmpDir();
const QString filePath = homeDir + "fileFromHome";
const QString dest = otherHomeDir + "fileFromHome_copied";
bool canRead = checkXattrFsSupport(homeDir);
bool canWrite = checkXattrFsSupport(otherHomeDir);
createTestFile(filePath);
if (canRead && canWrite) {
setXattr(filePath);
}
copyLocalFile(filePath, dest);
}
// Same partition doesn't matter as much as copying to the same filesystem type
// to be sure it supports the same permissions
void JobTest::testCopyFilePermissionsToSamePartition()
{
#if defined(Q_OS_UNIX)
const QString homeDir = homeTmpDir();
const QString src = homeDir + "fileFromHome";
const QUrl u = QUrl::fromLocalFile(src);
createTestFile(src);
const QByteArray src_c = QFile::encodeName(src).constData();
QT_STATBUF src_buff;
QCOMPARE(QT_LSTAT(src_c.constData(), &src_buff), 0); // Exists
// Default system permissions for newly created files, umask et al.
const mode_t systemDefaultPerms = src_buff.st_mode;
const QString dest = homeDir + "fileFromHome_copied";
const QUrl d = QUrl::fromLocalFile(dest);
const QByteArray dest_c = QFile::encodeName(dest).constData();
QT_STATBUF dest_buff;
// Copy the file, with -1 permissions (i.e. don't touch dest permissions)
auto copyStat = [&](const mode_t perms) {
KIO::Job *job = KIO::file_copy(u, d, perms, KIO::HideProgressInfo);
job->setUiDelegate(nullptr);
QVERIFY2(job->exec(), qPrintable(job->errorString()));
QCOMPARE(QT_LSTAT(dest_c.constData(), &dest_buff), 0);
};
copyStat(-1);
// dest should have system default permissions
QCOMPARE(dest_buff.st_mode, systemDefaultPerms);
QVERIFY(QFile::remove(dest));
// Change src permissions to non-default
QCOMPARE(::chmod(src_c.constData(), S_IRUSR), 0);
// Copy the file again, permissions -1 (i.e. don't touch dest permissions)
copyStat(-1);
// dest should have system default permissions, not src's ones
QCOMPARE(dest_buff.st_mode, systemDefaultPerms);
QVERIFY(QFile::remove(dest));
// Copy the file again, explicitly setting the permissions to the src ones
copyStat(src_buff.st_mode);
// dest should have same permissions as src
QCOMPARE(dest_buff.st_mode, dest_buff.st_mode);
QVERIFY(QFile::remove(dest));
// Copy the file again, explicitly setting some other permissions
copyStat(S_IWUSR);
// dest should have S_IWUSR
QCOMPARE(dest_buff.st_mode & 0777, S_IWUSR);
// Clean up, the weird permissions used above mess with the next
// unit tests
QVERIFY(QFile::remove(dest));
QVERIFY(QFile::remove(src));
#endif
}
void JobTest::copyDirectoryToOtherPartition()
{
// qDebug();
const QString src = homeTmpDir() + "dirFromHome";
const QString dest = otherTmpDir() + "dirFromHome_copied";
createTestDirectory(src);
copyLocalDirectory(src, dest);
}
void JobTest::copyRelativeSymlinkToSamePartition() // #352927
{
#ifdef Q_OS_WIN
QSKIP("Skipping symlink test on Windows");
#else
const QString filePath = homeTmpDir() + "testlink";
const QString dest = homeTmpDir() + "testlink_copied";
createTestSymlink(filePath, "relative");
copyLocalSymlink(filePath, dest, QStringLiteral("relative"));
QFile::remove(filePath);
#endif
}
void JobTest::copyAbsoluteSymlinkToOtherPartition()
{
#ifdef Q_OS_WIN
QSKIP("Skipping symlink test on Windows");
#else
const QString filePath = homeTmpDir() + "testlink";
const QString dest = otherTmpDir() + "testlink_copied";
createTestSymlink(filePath, QFile::encodeName(homeTmpDir()));
copyLocalSymlink(filePath, dest, homeTmpDir());
QFile::remove(filePath);
#endif
}
void JobTest::copyFolderWithUnaccessibleSubfolder()
{
#ifdef Q_OS_WIN
QSKIP("Skipping unaccessible folder test on Windows, cannot remove all permissions from a folder");
#endif
QTemporaryDir dir(homeTmpDir() + "UnaccessibleSubfolderTest");
QVERIFY(dir.isValid());
const QString src_dir = dir.path() + "srcHome";
const QString dst_dir = dir.path() + "dstHome";
QDir().remove(src_dir);
QDir().remove(dst_dir);
createTestDirectory(src_dir);
createTestDirectory(src_dir + "/folder1");
QString inaccessible = src_dir + "/folder1/inaccessible";
createTestDirectory(inaccessible);
QFile(inaccessible).setPermissions(QFile::Permissions()); // Make it inaccessible
// Copying should throw some warnings, as it cannot access some folders
ScopedCleaner cleaner([&] {
QFile(inaccessible).setPermissions(QFile::Permissions(QFile::ReadOwner | QFile::WriteOwner | QFile::ExeOwner));
qDebug() << "Cleaning up" << src_dir << "and" << dst_dir;
KIO::DeleteJob *deljob1 = KIO::del(QUrl::fromLocalFile(src_dir), KIO::HideProgressInfo);
deljob1->setUiDelegate(nullptr); // no skip dialog, thanks
const bool job1OK = deljob1->exec();
QVERIFY(job1OK);
KIO::DeleteJob *deljob2 = KIO::del(QUrl::fromLocalFile(dst_dir), KIO::HideProgressInfo);
deljob2->setUiDelegate(nullptr); // no skip dialog, thanks
const bool job2OK = deljob2->exec();
QVERIFY(job2OK);
qDebug() << "Result:" << job1OK << job2OK;
});
KIO::CopyJob *job = KIO::copy(QUrl::fromLocalFile(src_dir), QUrl::fromLocalFile(dst_dir), KIO::HideProgressInfo);
QSignalSpy spy(job, &KJob::warning);
job->setUiDelegate(nullptr); // no skip dialog, thanks
QVERIFY2(job->exec(), qPrintable(job->errorString()));
QCOMPARE(job->totalAmount(KJob::Files), 4); // testfile, testlink, folder1/testlink, folder1/testfile
QCOMPARE(job->totalAmount(KJob::Directories), 3); // srcHome, srcHome/folder1, srcHome/folder1/inaccessible
QCOMPARE(job->processedAmount(KJob::Files), 4);
QCOMPARE(job->processedAmount(KJob::Directories), 3);
QCOMPARE(job->percent(), 100);
QCOMPARE(spy.count(), 1); // one warning should be emitted by the copy job
}
void JobTest::copyDataUrl()
{
// GIVEN
const QString dst_dir = homeTmpDir();
QVERIFY(!QFileInfo::exists(dst_dir + "/data"));
// WHEN
KIO::CopyJob *job = KIO::copy(QUrl("data:,Hello%2C%20World!"), QUrl::fromLocalFile(dst_dir), KIO::HideProgressInfo);
QVERIFY2(job->exec(), qPrintable(job->errorString()));
// THEN
const QFileInfo fileInfo(dst_dir + "/data");
QVERIFY(fileInfo.isFile());
QCOMPARE(fileInfo.size(), 13);
QFile::remove(dst_dir + "/data");
}
void JobTest::suspendFileCopy()
{
const QString filePath = homeTmpDir() + "fileFromHome";
const QString dest = homeTmpDir() + "fileFromHome_copied";
createTestFile(filePath);
const QUrl u = QUrl::fromLocalFile(filePath);
const QUrl d = QUrl::fromLocalFile(dest);
KIO::Job *job = KIO::file_copy(u, d, KIO::HideProgressInfo);
QSignalSpy spyResult(job, &KJob::result);
job->setUiDelegate(nullptr);
job->setUiDelegateExtension(nullptr);
QVERIFY(job->suspend());
QVERIFY(!spyResult.wait(300));
QVERIFY(job->resume());
QVERIFY2(job->exec(), qPrintable(job->errorString()));
QVERIFY(QFile::exists(dest));
QFile::remove(dest);
}
void JobTest::suspendCopy()
{
const QString filePath = homeTmpDir() + "fileFromHome";
const QString dest = homeTmpDir() + "fileFromHome_copied";
createTestFile(filePath);
const QUrl u = QUrl::fromLocalFile(filePath);
const QUrl d = QUrl::fromLocalFile(dest);
KIO::Job *job = KIO::copy(u, d, KIO::HideProgressInfo);
QSignalSpy spyResult(job, &KJob::result);
job->setUiDelegate(nullptr);
job->setUiDelegateExtension(nullptr);
QVERIFY(job->suspend());
QVERIFY(!spyResult.wait(300));
QVERIFY(job->resume());
QVERIFY2(job->exec(), qPrintable(job->errorString()));
QVERIFY(QFile::exists(dest));
QFile::remove(dest);
}
void JobTest::moveLocalFile(const QString &src, const QString &dest)
{
QVERIFY(QFile::exists(src));
QUrl u = QUrl::fromLocalFile(src);
QUrl d = QUrl::fromLocalFile(dest);
// move the file with file_move
KIO::Job *job = KIO::file_move(u, d, 0666, KIO::HideProgressInfo);
job->setUiDelegate(nullptr);
QVERIFY2(job->exec(), qPrintable(job->errorString()));
QVERIFY(QFile::exists(dest));
QVERIFY(!QFile::exists(src)); // not there anymore
QCOMPARE(int(QFileInfo(dest).permissions()), int(0x6666));
// move it back with KIO::move()
job = KIO::move(d, u, KIO::HideProgressInfo);
job->setUiDelegate(nullptr);
job->setUiDelegateExtension(nullptr);
QVERIFY2(job->exec(), qPrintable(job->errorString()));
QVERIFY(!QFile::exists(dest));
QVERIFY(QFile::exists(src)); // it's back
}
static void moveLocalSymlink(const QString &src, const QString &dest)
{
QT_STATBUF buf;
QVERIFY(QT_LSTAT(QFile::encodeName(src).constData(), &buf) == 0);
QUrl u = QUrl::fromLocalFile(src);
QUrl d = QUrl::fromLocalFile(dest);
// move the symlink with move, NOT with file_move
KIO::Job *job = KIO::move(u, d, KIO::HideProgressInfo);
job->setUiDelegate(nullptr);
job->setUiDelegateExtension(nullptr);
QVERIFY2(job->exec(), qPrintable(job->errorString()));
QVERIFY(QT_LSTAT(QFile::encodeName(dest).constData(), &buf) == 0);
QVERIFY(!QFile::exists(src)); // not there anymore
// move it back with KIO::move()
job = KIO::move(d, u, KIO::HideProgressInfo);
job->setUiDelegate(nullptr);
job->setUiDelegateExtension(nullptr);
QVERIFY2(job->exec(), qPrintable(job->errorString()));
QVERIFY(QT_LSTAT(QFile::encodeName(dest).constData(), &buf) != 0); // doesn't exist anymore
QVERIFY(QT_LSTAT(QFile::encodeName(src).constData(), &buf) == 0); // it's back
}
void JobTest::moveLocalDirectory(const QString &src, const QString &dest)
{
qDebug() << src << " " << dest;
QVERIFY(QFile::exists(src));
QVERIFY(QFileInfo(src).isDir());
QVERIFY(QFileInfo(src + "/testfile").isFile());
#ifndef Q_OS_WIN
QVERIFY(QFileInfo(src + "/testlink").isSymLink());
#endif
QUrl u = QUrl::fromLocalFile(src);
QUrl d = QUrl::fromLocalFile(dest);
KIO::Job *job = KIO::move(u, d, KIO::HideProgressInfo);
job->setUiDelegate(nullptr);
job->setUiDelegateExtension(nullptr);
QVERIFY2(job->exec(), qPrintable(job->errorString()));
QVERIFY(QFile::exists(dest));
QVERIFY(QFileInfo(dest).isDir());
QVERIFY(QFileInfo(dest + "/testfile").isFile());
QVERIFY(!QFile::exists(src)); // not there anymore
#ifndef Q_OS_WIN
QVERIFY(QFileInfo(dest + "/testlink").isSymLink());
#endif
}
void JobTest::moveFileToSamePartition()
{
qDebug();
const QString filePath = homeTmpDir() + "fileFromHome";
const QString dest = homeTmpDir() + "fileFromHome_moved";
createTestFile(filePath);
moveLocalFile(filePath, dest);
}
void JobTest::moveDirectoryToSamePartition()
{
qDebug();
const QString src = homeTmpDir() + "dirFromHome";
const QString dest = homeTmpDir() + "dirFromHome_moved";
createTestDirectory(src);
moveLocalDirectory(src, dest);
}
void JobTest::moveDirectoryIntoItself()
{
qDebug();
const QString src = homeTmpDir() + "dirFromHome";
const QString dest = src + "/foo";
createTestDirectory(src);
QVERIFY(QFile::exists(src));
QUrl u = QUrl::fromLocalFile(src);
QUrl d = QUrl::fromLocalFile(dest);
KIO::CopyJob *job = KIO::move(u, d);
QVERIFY(!job->exec());
QCOMPARE(job->error(), (int)KIO::ERR_CANNOT_MOVE_INTO_ITSELF);
QCOMPARE(job->errorString(), i18n("A folder cannot be moved into itself"));
QDir(dest).removeRecursively();
}
void JobTest::moveFileToOtherPartition()
{
qDebug();
const QString filePath = homeTmpDir() + "fileFromHome";
const QString dest = otherTmpDir() + "fileFromHome_moved";
createTestFile(filePath);
moveLocalFile(filePath, dest);
}
void JobTest::moveSymlinkToOtherPartition()
{
#ifndef Q_OS_WIN
qDebug();
const QString filePath = homeTmpDir() + "testlink";
const QString dest = otherTmpDir() + "testlink_moved";
createTestSymlink(filePath);
moveLocalSymlink(filePath, dest);
#endif
}
void JobTest::moveDirectoryToOtherPartition()
{
qDebug();
#ifndef Q_OS_WIN
const QString src = homeTmpDir() + "dirFromHome";
const QString dest = otherTmpDir() + "dirFromHome_moved";
createTestDirectory(src);
moveLocalDirectory(src, dest);
#endif
}
struct CleanupInaccessibleSubdir {
explicit CleanupInaccessibleSubdir(const QString &subdir)
: subdir(subdir)
{
}
~CleanupInaccessibleSubdir()
{
QVERIFY(QFile(subdir).setPermissions(QFile::Permissions(QFile::ReadOwner | QFile::WriteOwner | QFile::ExeOwner)));
QVERIFY(QDir(subdir).removeRecursively());
}
private:
const QString subdir;
};
void JobTest::moveFileNoPermissions()
{
#ifdef Q_OS_WIN
QSKIP("Skipping unaccessible folder test on Windows, cannot remove all permissions from a folder");
#endif
// Given a file that cannot be moved (subdir has no permissions)
const QString subdir = homeTmpDir() + "subdir";
QVERIFY(QDir().mkpath(subdir));
const QString src = subdir + "/thefile";
createTestFile(src);
QVERIFY(QFile(subdir).setPermissions(QFile::Permissions())); // Make it inaccessible
CleanupInaccessibleSubdir c(subdir);
// When trying to move it
const QString dest = homeTmpDir() + "dest";
KIO::CopyJob *job = KIO::move(QUrl::fromLocalFile(src), QUrl::fromLocalFile(dest), KIO::HideProgressInfo);
job->setUiDelegate(nullptr);
job->setUiDelegateExtension(nullptr); // no skip dialog, thanks
// The job should fail with "access denied"
QVERIFY(!job->exec());
QCOMPARE(job->error(), (int)KIO::ERR_ACCESS_DENIED);
// Note that, just like mv(1), KIO's behavior depends on whether
// a direct rename(2) was used, or a full copy+del. In the first case
// there is no destination file created, but in the second case the
// destination file remains.
// In this test it's the same partition, so no dest created.
QVERIFY(!QFile::exists(dest));
QCOMPARE(job->totalAmount(KJob::Files), 1);
QCOMPARE(job->totalAmount(KJob::Directories), 0);
QCOMPARE(job->processedAmount(KJob::Files), 0);
QCOMPARE(job->processedAmount(KJob::Directories), 0);
QCOMPARE(job->percent(), 0);
}
void JobTest::moveDirectoryNoPermissions()
{
#ifdef Q_OS_WIN
QSKIP("Skipping unaccessible folder test on Windows, cannot remove all permissions from a folder");
#endif
// Given a dir that cannot be moved (parent dir has no permissions)
const QString subdir = homeTmpDir() + "subdir";
const QString src = subdir + "/thedir";
QVERIFY(QDir().mkpath(src));
QVERIFY(QFileInfo(src).isDir());
QVERIFY(QFile(subdir).setPermissions(QFile::Permissions())); // Make it inaccessible
CleanupInaccessibleSubdir c(subdir);
// When trying to move it
const QString dest = homeTmpDir() + "mdnp";
KIO::CopyJob *job = KIO::move(QUrl::fromLocalFile(src), QUrl::fromLocalFile(dest), KIO::HideProgressInfo);
job->setUiDelegate(nullptr);
job->setUiDelegateExtension(nullptr); // no skip dialog, thanks
// The job should fail with "access denied"
QVERIFY(!job->exec());
QCOMPARE(job->error(), (int)KIO::ERR_ACCESS_DENIED);
QVERIFY(!QFile::exists(dest));
QCOMPARE(job->totalAmount(KJob::Files), 1);
QCOMPARE(job->totalAmount(KJob::Directories), 0);
QCOMPARE(job->processedAmount(KJob::Files), 0);
QCOMPARE(job->processedAmount(KJob::Directories), 0);
QCOMPARE(job->percent(), 0);
}
void JobTest::moveDirectoryToReadonlyFilesystem_data()
{
QTest::addColumn<QList<QUrl>>("sources");
QTest::addColumn<int>("expectedErrorCode");
const QString srcFileHomePath = homeTmpDir() + "srcFileHome";
const QUrl srcFileHome = QUrl::fromLocalFile(srcFileHomePath);
createTestFile(srcFileHomePath);
const QString srcFileOtherPath = otherTmpDir() + "srcFileOther";
const QUrl srcFileOther = QUrl::fromLocalFile(srcFileOtherPath);
createTestFile(srcFileOtherPath);
const QString srcDirHomePath = homeTmpDir() + "srcDirHome";
const QUrl srcDirHome = QUrl::fromLocalFile(srcDirHomePath);
createTestDirectory(srcDirHomePath);
const QString srcDirHome2Path = homeTmpDir() + "srcDirHome2";
const QUrl srcDirHome2 = QUrl::fromLocalFile(srcDirHome2Path);
createTestDirectory(srcDirHome2Path);
const QString srcDirOtherPath = otherTmpDir() + "srcDirOther";
const QUrl srcDirOther = QUrl::fromLocalFile(srcDirOtherPath);
createTestDirectory(srcDirOtherPath);
QTest::newRow("file_same_partition") << QList<QUrl>{srcFileHome} << int(KIO::ERR_WRITE_ACCESS_DENIED);
QTest::newRow("file_other_partition") << QList<QUrl>{srcFileOther} << int(KIO::ERR_WRITE_ACCESS_DENIED);
QTest::newRow("one_dir_same_partition") << QList<QUrl>{srcDirHome} << int(KIO::ERR_WRITE_ACCESS_DENIED);
QTest::newRow("one_dir_other_partition") << QList<QUrl>{srcDirOther} << int(KIO::ERR_WRITE_ACCESS_DENIED);
QTest::newRow("dirs_same_partition") << QList<QUrl>{srcDirHome, srcDirHome2} << int(KIO::ERR_WRITE_ACCESS_DENIED);
QTest::newRow("dirs_both_partitions") << QList<QUrl>{srcDirOther, srcDirHome} << int(KIO::ERR_WRITE_ACCESS_DENIED);
}
void JobTest::moveDirectoryToReadonlyFilesystem()
{
QFETCH(QList<QUrl>, sources);
QFETCH(int, expectedErrorCode);
const QString dst_dir = homeTmpDir() + "readonlyDest";
const QUrl dst = QUrl::fromLocalFile(dst_dir);
QVERIFY2(QDir().mkdir(dst_dir), qPrintable(dst_dir));
QFile(dst_dir).setPermissions(QFile::Permissions(QFile::ReadOwner | QFile::ExeOwner)); // Make it readonly, moving should throw some errors
ScopedCleaner cleaner([&] {
QVERIFY(QFile(dst_dir).setPermissions(QFile::Permissions(QFile::ReadOwner | QFile::WriteOwner | QFile::ExeOwner)));
QVERIFY(QDir(dst_dir).removeRecursively());
});
KIO::CopyJob *job = KIO::move(sources, dst, KIO::HideProgressInfo | KIO::NoPrivilegeExecution);
job->setUiDelegate(nullptr);
QVERIFY(!job->exec());
QCOMPARE(job->error(), expectedErrorCode);
for (const QUrl &srcUrl : std::as_const(sources)) {
QVERIFY(QFileInfo::exists(srcUrl.toLocalFile())); // no moving happened
}
KIO::CopyJob *job2 = KIO::move(sources, dst, KIO::HideProgressInfo);
job2->setUiDelegate(nullptr);
QVERIFY(!job2->exec());
if (job2->error() != KIO::ERR_CANNOT_MKDIR) { // This can happen when moving between partitions, but on CI it's the same partition so allow both
QCOMPARE(job2->error(), expectedErrorCode);
}
for (const QUrl &srcUrl : std::as_const(sources)) {
QVERIFY(QFileInfo::exists(srcUrl.toLocalFile())); // no moving happened
}
}
static QByteArray expectedListRecursiveOutput()
{
return QByteArray(
".,..,"
"dirFromHome,dirFromHome/testfile,"
"dirFromHome/testlink," // exists on Windows too, see createTestDirectory
"dirFromHome_copied,"
"dirFromHome_copied/dirFromHome,dirFromHome_copied/dirFromHome/testfile,"
"dirFromHome_copied/dirFromHome/testlink,"
"dirFromHome_copied/testfile,"
"dirFromHome_copied/testlink,"
#ifndef Q_OS_WIN
"dirFromHome_link,"
#endif
"fileFromHome");
}
void JobTest::listRecursive()
{
// Note: many other tests must have been run before since we rely on the files they created
const QString src = homeTmpDir();
#ifndef Q_OS_WIN
// Add a symlink to a dir, to make sure we don't recurse into those
bool symlinkOk = symlink("dirFromHome", QFile::encodeName(src + "/dirFromHome_link").constData()) == 0;
QVERIFY(symlinkOk);
#endif
m_names.clear();
KIO::ListJob *job = KIO::listRecursive(QUrl::fromLocalFile(src), KIO::HideProgressInfo);
job->setUiDelegate(nullptr);
connect(job, &KIO::ListJob::entries, this, &JobTest::slotEntries);
QVERIFY2(job->exec(), qPrintable(job->errorString()));
m_names.sort();
const QByteArray ref_names = expectedListRecursiveOutput();
const QString joinedNames = m_names.join(QLatin1Char(','));
if (joinedNames.toLatin1() != ref_names) {
qDebug("%s", qPrintable(joinedNames));
qDebug("%s", ref_names.data());
}
QCOMPARE(joinedNames.toLatin1(), ref_names);
}
void JobTest::multipleListRecursive()
{
// Note: listRecursive() must have been run first
const QString src = homeTmpDir();
m_names.clear();
QVector<KIO::ListJob *> jobs;
for (int i = 0; i < 100; ++i) {
KIO::ListJob *job = KIO::listRecursive(QUrl::fromLocalFile(src), KIO::HideProgressInfo);
job->setUiDelegate(nullptr);
if (i == 6) {
connect(job, &KIO::ListJob::entries, this, &JobTest::slotEntries);
}
connect(job, &KJob::result, this, [&jobs, job]() {
jobs.removeOne(job);
});
jobs.push_back(job);
}
QTRY_VERIFY(jobs.isEmpty());
m_names.sort();
const QByteArray ref_names = expectedListRecursiveOutput();
const QString joinedNames = m_names.join(QLatin1Char(','));
if (joinedNames.toLatin1() != ref_names) {
qDebug("%s", qPrintable(joinedNames));
qDebug("%s", ref_names.data());
}
QCOMPARE(joinedNames.toLatin1(), ref_names);
}
void JobTest::listFile()
{
const QString filePath = homeTmpDir() + "fileFromHome";
createTestFile(filePath);
KIO::ListJob *job = KIO::listDir(QUrl::fromLocalFile(filePath), KIO::HideProgressInfo);
job->setUiDelegate(nullptr);
QVERIFY(!job->exec());
QCOMPARE(job->error(), static_cast<int>(KIO::ERR_IS_FILE));
// And list something that doesn't exist
const QString path = homeTmpDir() + "fileFromHomeDoesNotExist";
job = KIO::listDir(QUrl::fromLocalFile(path), KIO::HideProgressInfo);
job->setUiDelegate(nullptr);
QVERIFY(!job->exec());
QCOMPARE(job->error(), static_cast<int>(KIO::ERR_DOES_NOT_EXIST));
}
void JobTest::killJob()
{
const QString src = homeTmpDir();
KIO::ListJob *job = KIO::listDir(QUrl::fromLocalFile(src), KIO::HideProgressInfo);
QVERIFY(job->isAutoDelete());
QPointer<KIO::ListJob> ptr(job);
job->setUiDelegate(nullptr);
qApp->processEvents(); // let the job start, it's no fun otherwise
job->kill();
qApp->sendPostedEvents(nullptr, QEvent::DeferredDelete); // process the deferred delete of the job
QVERIFY(ptr.isNull());
}
void JobTest::killJobBeforeStart()
{
const QString src = homeTmpDir();
KIO::Job *job = KIO::stat(QUrl::fromLocalFile(src), KIO::HideProgressInfo);
QVERIFY(job->isAutoDelete());
QPointer<KIO::Job> ptr(job);
job->setUiDelegate(nullptr);
job->kill();
qApp->sendPostedEvents(nullptr, QEvent::DeferredDelete); // process the deferred delete of the job
QVERIFY(ptr.isNull());
qApp->processEvents(); // does KIO scheduler crash here? nope.
}
void JobTest::deleteJobBeforeStart() // #163171
{
const QString src = homeTmpDir();
KIO::Job *job = KIO::stat(QUrl::fromLocalFile(src), KIO::HideProgressInfo);
QVERIFY(job->isAutoDelete());
job->setUiDelegate(nullptr);
delete job;
qApp->processEvents(); // does KIO scheduler crash here?
}
void JobTest::directorySize()
{
// Note: many other tests must have been run before since we rely on the files they created
const QString src = homeTmpDir();
KIO::DirectorySizeJob *job = KIO::directorySize(QUrl::fromLocalFile(src));
job->setUiDelegate(nullptr);
QVERIFY2(job->exec(), qPrintable(job->errorString()));
qDebug() << "totalSize: " << job->totalSize();
qDebug() << "totalFiles: " << job->totalFiles();
qDebug() << "totalSubdirs: " << job->totalSubdirs();
#ifdef Q_OS_WIN
QCOMPARE(job->totalFiles(), 5ULL); // see expected result in listRecursive() above
QCOMPARE(job->totalSubdirs(), 3ULL); // see expected result in listRecursive() above
QVERIFY(job->totalSize() > 54);
#else
QCOMPARE(job->totalFiles(), 7ULL); // see expected result in listRecursive() above
QCOMPARE(job->totalSubdirs(), 4ULL); // see expected result in listRecursive() above
QVERIFY2(job->totalSize() >= 60,
qPrintable(QString("totalSize was %1").arg(job->totalSize()))); // size of subdir entries is filesystem dependent. E.g. this is 16428 with ext4 but
// only 272 with xfs, and 63 on FreeBSD
#endif
qApp->sendPostedEvents(nullptr, QEvent::DeferredDelete);
}
void JobTest::directorySizeError()
{
KIO::DirectorySizeJob *job = KIO::directorySize(QUrl::fromLocalFile(QStringLiteral("/I/Dont/Exist")));
job->setUiDelegate(nullptr);
QVERIFY(!job->exec());
qApp->sendPostedEvents(nullptr, QEvent::DeferredDelete);
}
void JobTest::slotEntries(KIO::Job *, const KIO::UDSEntryList &lst)
{
for (KIO::UDSEntryList::ConstIterator it = lst.begin(); it != lst.end(); ++it) {
QString displayName = (*it).stringValue(KIO::UDSEntry::UDS_NAME);
// QUrl url = (*it).stringValue( KIO::UDSEntry::UDS_URL );
m_names.append(displayName);
}
}
void JobTest::calculateRemainingSeconds()
{
unsigned int seconds = KIO::calculateRemainingSeconds(2 * 86400 - 60, 0, 1);
QCOMPARE(seconds, static_cast<unsigned int>(2 * 86400 - 60));
QString text = KIO::convertSeconds(seconds);
QCOMPARE(text, i18n("1 day 23:59:00"));
seconds = KIO::calculateRemainingSeconds(520, 20, 10);
QCOMPARE(seconds, static_cast<unsigned int>(50));
text = KIO::convertSeconds(seconds);
QCOMPARE(text, i18n("00:00:50"));
}
void JobTest::getInvalidUrl()
{
QUrl url(QStringLiteral("http://strange<hostname>/"));
QVERIFY(!url.isValid());
KIO::SimpleJob *job = KIO::get(url, KIO::NoReload, KIO::HideProgressInfo);
QVERIFY(job != nullptr);
job->setUiDelegate(nullptr);
QVERIFY(!job->exec()); // it should fail :)
}
void JobTest::slotMimetype(KIO::Job *job, const QString &type)
{
QVERIFY(job != nullptr);
m_mimetype = type;
}
void JobTest::deleteFile()
{
const QString dest = otherTmpDir() + "fileFromHome_copied";
createTestFile(dest);
KIO::Job *job = KIO::del(QUrl::fromLocalFile(dest), KIO::HideProgressInfo);
job->setUiDelegate(nullptr);
QVERIFY2(job->exec(), qPrintable(job->errorString()));
QVERIFY(!QFile::exists(dest));
}
void JobTest::deleteDirectory()
{
const QString dest = otherTmpDir() + "dirFromHome_copied";
if (!QFile::exists(dest)) {
createTestDirectory(dest);
}
// Let's put a few things in there to see if the recursive deletion works correctly
// A hidden file:
createTestFile(dest + "/.hidden");
#ifndef Q_OS_WIN
// A broken symlink:
createTestSymlink(dest + "/broken_symlink");
// A symlink to a dir:
const auto srcFileName = QFile::encodeName(QFileInfo(QFINDTESTDATA("jobtest.cpp")).absolutePath()).constData();
const auto symLinkFileName = QFile::encodeName(dest + "/symlink_to_dir").constData();
if (symlink(srcFileName, symLinkFileName) != 0) {
qFatal("couldn't create symlink: %s", strerror(errno));
}
#endif
KIO::Job *job = KIO::del(QUrl::fromLocalFile(dest), KIO::HideProgressInfo);
job->setUiDelegate(nullptr);
QVERIFY2(job->exec(), qPrintable(job->errorString()));
QVERIFY(!QFile::exists(dest));
}
void JobTest::deleteSymlink(bool using_fast_path)
{
extern KIOCORE_EXPORT bool kio_resolve_local_urls;
kio_resolve_local_urls = !using_fast_path;
#ifndef Q_OS_WIN
const QString src = homeTmpDir() + "dirFromHome";
createTestDirectory(src);
QVERIFY(QFile::exists(src));
const QString dest = homeTmpDir() + "/dirFromHome_link";
if (!QFile::exists(dest)) {
// Add a symlink to a dir, to make sure we don't recurse into those
bool symlinkOk = symlink(QFile::encodeName(src).constData(), QFile::encodeName(dest).constData()) == 0;
QVERIFY(symlinkOk);
QVERIFY(QFile::exists(dest));
}
KIO::Job *job = KIO::del(QUrl::fromLocalFile(dest), KIO::HideProgressInfo);
job->setUiDelegate(nullptr);
QVERIFY2(job->exec(), qPrintable(job->errorString()));
QVERIFY(!QFile::exists(dest));
QVERIFY(QFile::exists(src));
#endif
kio_resolve_local_urls = true;
}
void JobTest::deleteSymlink()
{
#ifndef Q_OS_WIN
deleteSymlink(true);
deleteSymlink(false);
#endif
}
void JobTest::deleteManyDirs(bool using_fast_path)
{
extern KIOCORE_EXPORT bool kio_resolve_local_urls;
kio_resolve_local_urls = !using_fast_path;
const int numDirs = 50;
QList<QUrl> dirs;
for (int i = 0; i < numDirs; ++i) {
const QString dir = homeTmpDir() + "dir" + QString::number(i);
createTestDirectory(dir);
dirs << QUrl::fromLocalFile(dir);
}
QElapsedTimer dt;
dt.start();
KIO::Job *job = KIO::del(dirs, KIO::HideProgressInfo);
job->setUiDelegate(nullptr);
QVERIFY2(job->exec(), qPrintable(job->errorString()));
for (const QUrl &dir : std::as_const(dirs)) {
QVERIFY(!QFile::exists(dir.toLocalFile()));
}
qDebug() << "Deleted" << numDirs << "dirs in" << dt.elapsed() << "milliseconds";
kio_resolve_local_urls = true;
}
void JobTest::deleteManyDirs()
{
deleteManyDirs(true);
deleteManyDirs(false);
}
static QList<QUrl> createManyFiles(const QString &baseDir, int numFiles)
{
QList<QUrl> ret;
ret.reserve(numFiles);
for (int i = 0; i < numFiles; ++i) {
// create empty file
const QString file = baseDir + QString::number(i);
QFile f(file);
bool ok = f.open(QIODevice::WriteOnly);
if (ok) {
f.write("Hello");
ret.append(QUrl::fromLocalFile(file));
}
}
return ret;
}
void JobTest::deleteManyFilesIndependently()
{
QElapsedTimer dt;
dt.start();
const int numFiles = 100; // Use 1000 for performance testing
const QString baseDir = homeTmpDir();
const QList<QUrl> urls = createManyFiles(baseDir, numFiles);
QCOMPARE(urls.count(), numFiles);
for (int i = 0; i < numFiles; ++i) {
// delete each file independently. lots of jobs. this stress-tests kio scheduling.
const QUrl url = urls.at(i);
const QString file = url.toLocalFile();
QVERIFY(QFile::exists(file));
// qDebug() << file;
KIO::Job *job = KIO::del(url, KIO::HideProgressInfo);
job->setUiDelegate(nullptr);
QVERIFY2(job->exec(), qPrintable(job->errorString()));
QVERIFY(!QFile::exists(file));
}
qDebug() << "Deleted" << numFiles << "files in" << dt.elapsed() << "milliseconds";
}
void JobTest::deleteManyFilesTogether(bool using_fast_path)
{
extern KIOCORE_EXPORT bool kio_resolve_local_urls;
kio_resolve_local_urls = !using_fast_path;
QElapsedTimer dt;
dt.start();
const int numFiles = 100; // Use 1000 for performance testing
const QString baseDir = homeTmpDir();
const QList<QUrl> urls = createManyFiles(baseDir, numFiles);
QCOMPARE(urls.count(), numFiles);
// qDebug() << file;
KIO::Job *job = KIO::del(urls, KIO::HideProgressInfo);
job->setUiDelegate(nullptr);
QVERIFY2(job->exec(), qPrintable(job->errorString()));
qDebug() << "Deleted" << numFiles << "files in" << dt.elapsed() << "milliseconds";
kio_resolve_local_urls = true;
}
void JobTest::deleteManyFilesTogether()
{
deleteManyFilesTogether(true);
deleteManyFilesTogether(false);
}
void JobTest::rmdirEmpty()
{
const QString dir = homeTmpDir() + "dir";
QDir().mkdir(dir);
QVERIFY(QFile::exists(dir));
KIO::Job *job = KIO::rmdir(QUrl::fromLocalFile(dir));
QVERIFY2(job->exec(), qPrintable(job->errorString()));
QVERIFY(!QFile::exists(dir));
}
void JobTest::rmdirNotEmpty()
{
const QString dir = homeTmpDir() + "dir";
createTestDirectory(dir);
createTestDirectory(dir + "/subdir");
KIO::Job *job = KIO::rmdir(QUrl::fromLocalFile(dir));
QVERIFY(!job->exec());
QVERIFY(QFile::exists(dir));
}
void JobTest::stat()
{
#if 1
const QString filePath = homeTmpDir() + "fileFromHome";
createTestFile(filePath);
const QUrl url(QUrl::fromLocalFile(filePath));
KIO::StatJob *job = KIO::stat(url, KIO::HideProgressInfo);
QVERIFY(job);
QVERIFY2(job->exec(), qPrintable(job->errorString()));
// TODO set setSide
const KIO::UDSEntry &entry = job->statResult();
// we only get filename, access, type, size, uid, gid, btime, mtime, atime
QVERIFY(entry.contains(KIO::UDSEntry::UDS_NAME));
QVERIFY(entry.contains(KIO::UDSEntry::UDS_ACCESS));
QVERIFY(entry.contains(KIO::UDSEntry::UDS_SIZE));
QVERIFY(entry.contains(KIO::UDSEntry::UDS_FILE_TYPE));
QVERIFY(entry.contains(KIO::UDSEntry::UDS_USER));
QVERIFY(entry.contains(KIO::UDSEntry::UDS_GROUP));
// QVERIFY(entry.contains(KIO::UDSEntry::UDS_CREATION_TIME)); // only true if st_birthtime or statx is used
QVERIFY(entry.contains(KIO::UDSEntry::UDS_MODIFICATION_TIME));
QVERIFY(entry.contains(KIO::UDSEntry::UDS_ACCESS_TIME));
QCOMPARE(entry.count(), 8 + (entry.contains(KIO::UDSEntry::UDS_CREATION_TIME) ? 1 : 0));
QVERIFY(!entry.isDir());
QVERIFY(!entry.isLink());
QCOMPARE(entry.stringValue(KIO::UDSEntry::UDS_NAME), QStringLiteral("fileFromHome"));
// Compare what we get via kio_file and what we get when KFileItem stat()s directly
const KFileItem kioItem(entry, url);
const KFileItem fileItem(url);
QCOMPARE(kioItem.name(), fileItem.name());
QCOMPARE(kioItem.url(), fileItem.url());
QCOMPARE(kioItem.size(), fileItem.size());
QCOMPARE(kioItem.user(), fileItem.user());
QCOMPARE(kioItem.group(), fileItem.group());
QCOMPARE(kioItem.mimetype(), fileItem.mimetype());
QCOMPARE(kioItem.permissions(), fileItem.permissions());
QCOMPARE(kioItem.time(KFileItem::ModificationTime), fileItem.time(KFileItem::ModificationTime));
QCOMPARE(kioItem.time(KFileItem::AccessTime), fileItem.time(KFileItem::AccessTime));
#else
// Testing stat over HTTP
KIO::StatJob *job = KIO::stat(QUrl("http://www.kde.org"), KIO::HideProgressInfo);
QVERIFY(job);
QVERIFY2(job->exec(), qPrintable(job->errorString()));
// TODO set setSide, setDetails
const KIO::UDSEntry &entry = job->statResult();
QVERIFY(!entry.isDir());
QVERIFY(!entry.isLink());
QCOMPARE(entry.stringValue(KIO::UDSEntry::UDS_NAME), QString());
#endif
}
void JobTest::statDetailsBasic()
{
const QString filePath = homeTmpDir() + "fileFromHome";
createTestFile(filePath);
const QUrl url(QUrl::fromLocalFile(filePath));
KIO::StatJob *job = KIO::statDetails(url, KIO::StatJob::StatSide::SourceSide, KIO::StatBasic, KIO::HideProgressInfo);
QVERIFY(job);
QVERIFY2(job->exec(), qPrintable(job->errorString()));
// TODO set setSide
const KIO::UDSEntry &entry = job->statResult();
// we only get filename, access, type, size, (no linkdest)
QVERIFY(entry.contains(KIO::UDSEntry::UDS_NAME));
QVERIFY(entry.contains(KIO::UDSEntry::UDS_ACCESS));
QVERIFY(entry.contains(KIO::UDSEntry::UDS_SIZE));
QVERIFY(entry.contains(KIO::UDSEntry::UDS_FILE_TYPE));
QCOMPARE(entry.count(), 4);
QVERIFY(!entry.isDir());
QVERIFY(!entry.isLink());
QVERIFY(entry.numberValue(KIO::UDSEntry::UDS_ACCESS) > 0);
QCOMPARE(entry.stringValue(KIO::UDSEntry::UDS_NAME), QStringLiteral("fileFromHome"));
// Compare what we get via kio_file and what we get when KFileItem stat()s directly
// for the requested fields
const KFileItem kioItem(entry, url);
const KFileItem fileItem(url);
QCOMPARE(kioItem.name(), fileItem.name());
QCOMPARE(kioItem.url(), fileItem.url());
QCOMPARE(kioItem.size(), fileItem.size());
QCOMPARE(kioItem.user(), "");
QCOMPARE(kioItem.group(), "");
QCOMPARE(kioItem.mimetype(), "application/octet-stream");
QCOMPARE(kioItem.permissions(), 438);
QCOMPARE(kioItem.time(KFileItem::ModificationTime), QDateTime());
QCOMPARE(kioItem.time(KFileItem::AccessTime), QDateTime());
}
void JobTest::statDetailsBasicSetDetails()
{
const QString filePath = homeTmpDir() + "fileFromHome";
createTestFile(filePath);
const QUrl url(QUrl::fromLocalFile(filePath));
KIO::StatJob *job = KIO::stat(url);
job->setDetails(KIO::StatBasic);
QVERIFY(job);
QVERIFY2(job->exec(), qPrintable(job->errorString()));
// TODO set setSide
const KIO::UDSEntry &entry = job->statResult();
// we only get filename, access, type, size, (no linkdest)
QVERIFY(entry.contains(KIO::UDSEntry::UDS_NAME));
QVERIFY(entry.contains(KIO::UDSEntry::UDS_ACCESS));
QVERIFY(entry.contains(KIO::UDSEntry::UDS_SIZE));
QVERIFY(entry.contains(KIO::UDSEntry::UDS_FILE_TYPE));
QCOMPARE(entry.count(), 4);
QVERIFY(!entry.isDir());
QVERIFY(!entry.isLink());
QVERIFY(entry.numberValue(KIO::UDSEntry::UDS_ACCESS) > 0);
QCOMPARE(entry.stringValue(KIO::UDSEntry::UDS_NAME), QStringLiteral("fileFromHome"));
// Compare what we get via kio_file and what we get when KFileItem stat()s directly
// for the requested fields
const KFileItem kioItem(entry, url);
const KFileItem fileItem(url);
QCOMPARE(kioItem.name(), fileItem.name());
QCOMPARE(kioItem.url(), fileItem.url());
QCOMPARE(kioItem.size(), fileItem.size());
QCOMPARE(kioItem.user(), "");
QCOMPARE(kioItem.group(), "");
QCOMPARE(kioItem.mimetype(), "application/octet-stream");
QCOMPARE(kioItem.permissions(), 438);
QCOMPARE(kioItem.time(KFileItem::ModificationTime), QDateTime());
QCOMPARE(kioItem.time(KFileItem::AccessTime), QDateTime());
}
void JobTest::statWithInode()
{
const QString filePath = homeTmpDir() + "fileFromHome";
createTestFile(filePath);
const QUrl url(QUrl::fromLocalFile(filePath));
KIO::StatJob *job = KIO::statDetails(url, KIO::StatJob::SourceSide, KIO::StatInode);
QVERIFY(job);
QVERIFY2(job->exec(), qPrintable(job->errorString()));
const KIO::UDSEntry entry = job->statResult();
QVERIFY(entry.contains(KIO::UDSEntry::UDS_DEVICE_ID));
QVERIFY(entry.contains(KIO::UDSEntry::UDS_INODE));
QCOMPARE(entry.count(), 2);
const QString path = otherTmpDir() + "otherFile";
createTestFile(path);
const QUrl otherUrl(QUrl::fromLocalFile(path));
KIO::StatJob *otherJob = KIO::statDetails(otherUrl, KIO::StatJob::SourceSide, KIO::StatInode);
QVERIFY(otherJob);
QVERIFY2(otherJob->exec(), qPrintable(otherJob->errorString()));
const KIO::UDSEntry otherEntry = otherJob->statResult();
QVERIFY(otherEntry.contains(KIO::UDSEntry::UDS_DEVICE_ID));
QVERIFY(otherEntry.contains(KIO::UDSEntry::UDS_INODE));
QCOMPARE(otherEntry.count(), 2);
const int device = entry.numberValue(KIO::UDSEntry::UDS_DEVICE_ID);
const int otherDevice = otherEntry.numberValue(KIO::UDSEntry::UDS_DEVICE_ID);
// this test doesn't make sense on the CI as it's an LXC container with one partition
if (otherTmpDirIsOnSamePartition()) {
// On the CI where the two tmp dirs are on the only partition available
// in the LXC container, the device ID's would be identical
QCOMPARE(device, otherDevice);
} else {
QVERIFY(device != otherDevice);
}
}
#ifndef Q_OS_WIN
void JobTest::statSymlink()
{
const QString filePath = homeTmpDir() + "fileFromHome";
createTestFile(filePath);
const QString symlink = otherTmpDir() + "link";
QVERIFY(QFile(filePath).link(symlink));
QVERIFY(QFile::exists(symlink));
setTimeStamp(symlink, QDateTime::currentDateTime().addSecs(-20)); // differentiate link time and source file time
const QUrl url(QUrl::fromLocalFile(symlink));
KIO::StatJob *job = KIO::statDetails(url,
KIO::StatJob::StatSide::SourceSide,
KIO::StatBasic | KIO::StatResolveSymlink | KIO::StatUser | KIO::StatTime,
KIO::HideProgressInfo);
QVERIFY(job);
QVERIFY2(job->exec(), qPrintable(job->errorString()));
// TODO set setSide, setDetails
const KIO::UDSEntry &entry = job->statResult();
// we only get filename, access, type, size, linkdest, uid, gid, btime, mtime, atime
QVERIFY(entry.contains(KIO::UDSEntry::UDS_NAME));
QVERIFY(entry.contains(KIO::UDSEntry::UDS_ACCESS));
QVERIFY(entry.contains(KIO::UDSEntry::UDS_SIZE));
QVERIFY(entry.contains(KIO::UDSEntry::UDS_FILE_TYPE));
QVERIFY(entry.contains(KIO::UDSEntry::UDS_LINK_DEST));
QVERIFY(entry.contains(KIO::UDSEntry::UDS_USER));
QVERIFY(entry.contains(KIO::UDSEntry::UDS_GROUP));
// QVERIFY(entry.contains(KIO::UDSEntry::UDS_CREATION_TIME)); // only true if st_birthtime or statx is used
QVERIFY(entry.contains(KIO::UDSEntry::UDS_MODIFICATION_TIME));
QVERIFY(entry.contains(KIO::UDSEntry::UDS_ACCESS_TIME));
QCOMPARE(entry.count(), 9 + (entry.contains(KIO::UDSEntry::UDS_CREATION_TIME) ? 1 : 0));
QVERIFY(!entry.isDir());
QVERIFY(entry.isLink());
QVERIFY(entry.numberValue(KIO::UDSEntry::UDS_ACCESS) > 0);
QCOMPARE(entry.stringValue(KIO::UDSEntry::UDS_NAME), QStringLiteral("link"));
// Compare what we get via kio_file and what we get when KFileItem stat()s directly
const KFileItem kioItem(entry, url);
const KFileItem fileItem(url);
QCOMPARE(kioItem.name(), fileItem.name());
QCOMPARE(kioItem.url(), fileItem.url());
QVERIFY(kioItem.isLink());
QVERIFY(fileItem.isLink());
QCOMPARE(kioItem.linkDest(), fileItem.linkDest());
QCOMPARE(kioItem.size(), fileItem.size());
QCOMPARE(kioItem.user(), fileItem.user());
QCOMPARE(kioItem.group(), fileItem.group());
QCOMPARE(kioItem.mimetype(), fileItem.mimetype());
QCOMPARE(kioItem.permissions(), fileItem.permissions());
QCOMPARE(kioItem.time(KFileItem::ModificationTime), fileItem.time(KFileItem::ModificationTime));
QCOMPARE(kioItem.time(KFileItem::AccessTime), fileItem.time(KFileItem::AccessTime));
}
/* Check that the underlying system, and Qt, support
* millisecond timestamp resolution.
*/
void JobTest::statTimeResolution()
{
const QString filePath = homeTmpDir() + "statFile";
const QDateTime early70sDate = QDateTime::fromMSecsSinceEpoch(107780520123L);
const time_t early70sTime = 107780520; // Seconds for January 6 1973, 12:02
createTestFile(filePath);
QFile dest_file(filePath);
QVERIFY(dest_file.open(QIODevice::ReadOnly));
#if defined(Q_OS_LINUX) || defined(Q_OS_FREEBSD)
// with nano secs precision
struct timespec ut[2];
ut[0].tv_sec = early70sTime;
ut[0].tv_nsec = 123000000L; // 123 ms
ut[1] = ut[0];
// need to do this with the dest file still opened, or this fails
QCOMPARE(::futimens(dest_file.handle(), ut), 0);
#else
struct timeval ut[2];
ut[0].tv_sec = early70sTime;
ut[0].tv_usec = 123000;
ut[1] = ut[0];
QCOMPARE(::futimes(dest_file.handle(), ut), 0);
#endif
dest_file.close();
// Check that the modification time is set with millisecond precision
dest_file.setFileName(filePath);
QDateTime d = dest_file.fileTime(QFileDevice::FileModificationTime);
QCOMPARE(d, early70sDate);
QCOMPARE(d.time().msec(), 123);
#if defined(Q_OS_LINUX) || defined(Q_OS_FREEBSD)
QT_STATBUF buff_dest;
QCOMPARE(QT_STAT(filePath.toLocal8Bit().data(), &buff_dest), 0);
QCOMPARE(buff_dest.st_mtim.tv_sec, early70sTime);
QCOMPARE(buff_dest.st_mtim.tv_nsec, 123000000L);
#endif
QCOMPARE(QFileInfo(filePath).lastModified(), early70sDate);
}
#endif
void JobTest::mostLocalUrl()
{
const QString filePath = homeTmpDir() + "fileFromHome";
createTestFile(filePath);
KIO::StatJob *job = KIO::mostLocalUrl(QUrl::fromLocalFile(filePath), KIO::HideProgressInfo);
QVERIFY(job);
QVERIFY2(job->exec(), qPrintable(job->errorString()));
QCOMPARE(job->mostLocalUrl().toLocalFile(), filePath);
}
void JobTest::mostLocalUrlHttp()
{
// the url is returned as-is, as an http url can't have a mostLocalUrl
const QUrl url("http://www.google.com");
KIO::StatJob *httpStat = KIO::mostLocalUrl(url, KIO::HideProgressInfo);
QVERIFY(httpStat);
QVERIFY2(httpStat->exec(), qPrintable(httpStat->errorString()));
QCOMPARE(httpStat->mostLocalUrl(), url);
}
void JobTest::chmodFile()
{
const QString filePath = homeTmpDir() + "fileForChmod";
createTestFile(filePath);
KFileItem item(QUrl::fromLocalFile(filePath));
const mode_t origPerm = item.permissions();
mode_t newPerm = origPerm ^ S_IWGRP;
QVERIFY(newPerm != origPerm);
KFileItemList items;
items << item;
KIO::Job *job = KIO::chmod(items, newPerm, S_IWGRP /*TODO: QFile::WriteGroup*/, QString(), QString(), false, KIO::HideProgressInfo);
job->setUiDelegate(nullptr);
QVERIFY2(job->exec(), qPrintable(job->errorString()));
KFileItem newItem(QUrl::fromLocalFile(filePath));
QCOMPARE(QString::number(newItem.permissions(), 8), QString::number(newPerm, 8));
QFile::remove(filePath);
}
#ifdef Q_OS_UNIX
void JobTest::chmodSticky()
{
const QString dirPath = homeTmpDir() + "dirForChmodSticky";
QDir().mkpath(dirPath);
KFileItem item(QUrl::fromLocalFile(dirPath));
const mode_t origPerm = item.permissions();
mode_t newPerm = origPerm ^ S_ISVTX;
QVERIFY(newPerm != origPerm);
KFileItemList items({item});
KIO::Job *job = KIO::chmod(items, newPerm, S_ISVTX, QString(), QString(), false, KIO::HideProgressInfo);
job->setUiDelegate(nullptr);
QVERIFY2(job->exec(), qPrintable(job->errorString()));
KFileItem newItem(QUrl::fromLocalFile(dirPath));
QCOMPARE(QString::number(newItem.permissions(), 8), QString::number(newPerm, 8));
QVERIFY(QDir().rmdir(dirPath));
}
#endif
void JobTest::chmodFileError()
{
// chown(root) should fail
const QString filePath = homeTmpDir() + "fileForChmod";
createTestFile(filePath);
KFileItem item(QUrl::fromLocalFile(filePath));
const mode_t origPerm = item.permissions();
mode_t newPerm = origPerm ^ S_IWGRP;
QVERIFY(newPerm != origPerm);
KFileItemList items;
items << item;
KIO::Job *job = KIO::chmod(items, newPerm, S_IWGRP /*TODO: QFile::WriteGroup*/, QStringLiteral("root"), QString(), false, KIO::HideProgressInfo);
// Simulate the user pressing "Skip" in the dialog.
job->setUiDelegate(new KJobUiDelegate);
auto *askUser = new MockAskUserInterface(job->uiDelegate());
QVERIFY2(job->exec(), qPrintable(job->errorString()));
QCOMPARE(askUser->m_askUserSkipCalled, 1);
KFileItem newItem(QUrl::fromLocalFile(filePath));
// We skipped, so the chmod didn't happen.
QCOMPARE(QString::number(newItem.permissions(), 8), QString::number(origPerm, 8));
QFile::remove(filePath);
}
void JobTest::mimeType()
{
#if 1
const QString filePath = homeTmpDir() + "fileFromHome";
createTestFile(filePath);
KIO::MimetypeJob *job = KIO::mimetype(QUrl::fromLocalFile(filePath), KIO::HideProgressInfo);
QVERIFY(job);
#if KIOCORE_BUILD_DEPRECATED_SINCE(5, 78)
QSignalSpy spyMimeType(job, qOverload<KIO::Job *, const QString &>(&KIO::MimetypeJob::mimetype));
#endif
QSignalSpy spyMimeTypeFound(job, &KIO::TransferJob::mimeTypeFound);
QVERIFY2(job->exec(), qPrintable(job->errorString()));
#if KIOCORE_BUILD_DEPRECATED_SINCE(5, 78)
QCOMPARE(spyMimeType.count(), 1);
QCOMPARE(spyMimeType[0][0], QVariant::fromValue(static_cast<KIO::Job *>(job)));
QCOMPARE(spyMimeType[0][1].toString(), QStringLiteral("application/octet-stream"));
#endif
QCOMPARE(spyMimeTypeFound.count(), 1);
QCOMPARE(spyMimeTypeFound[0][0], QVariant::fromValue(static_cast<KIO::Job *>(job)));
QCOMPARE(spyMimeTypeFound[0][1].toString(), QStringLiteral("application/octet-stream"));
#else
// Testing mimetype over HTTP
KIO::MimetypeJob *job = KIO::mimetype(QUrl("http://www.kde.org"), KIO::HideProgressInfo);
QVERIFY(job);
#if KIOCORE_BUILD_DEPRECATED_SINCE(5, 78)
QSignalSpy spyMimeType(job, qOverload<KIO::Job *, const QString &>(&KIO::MimetypeJob::mimetype));
#endif
QSignalSpy spyMimeTypeFound(job, &KIO::TransferJob::mimeTypeFound);
QVERIFY2(job->exec(), qPrintable(job->errorString()));
#if KIOCORE_BUILD_DEPRECATED_SINCE(5, 78)
QCOMPARE(spyMimeType.count(), 1);
QCOMPARE(spyMimeType[0][0], QVariant::fromValue(static_cast<KIO::Job *>(job)));
QCOMPARE(spyMimeType[0][1].toString(), QString("text/html"));
#endif
QCOMPARE(spyMimeTypeFound.count(), 1);
QCOMPARE(spyMimeTypeFound[0][0], QVariant::fromValue(static_cast<KIO::Job *>(job)));
QCOMPARE(spyMimeTypeFound[0][1].toString(), QString("text/html"));
#endif
}
void JobTest::mimeTypeError()
{
// KIO::mimetype() on a file that doesn't exist
const QString filePath = homeTmpDir() + "doesNotExist";
KIO::MimetypeJob *job = KIO::mimetype(QUrl::fromLocalFile(filePath), KIO::HideProgressInfo);
QVERIFY(job);
#if KIOCORE_BUILD_DEPRECATED_SINCE(5, 78)
QSignalSpy spyMimeType(job, qOverload<KIO::Job *, const QString &>(&KIO::MimetypeJob::mimetype));
#endif
QSignalSpy spyMimeTypeFound(job, &KIO::TransferJob::mimeTypeFound);
QSignalSpy spyResult(job, &KJob::result);
QVERIFY(!job->exec());
#if KIOCORE_BUILD_DEPRECATED_SINCE(5, 78)
QCOMPARE(spyMimeType.count(), 0);
#endif
QCOMPARE(spyMimeTypeFound.count(), 0);
QCOMPARE(spyResult.count(), 1);
}
void JobTest::moveFileDestAlreadyExists_data()
{
QTest::addColumn<bool>("autoSkip");
QTest::newRow("autoSkip") << true;
QTest::newRow("manualSkip") << false;
}
void JobTest::moveFileDestAlreadyExists() // #157601
{
QFETCH(bool, autoSkip);
const QString file1 = homeTmpDir() + "fileFromHome";
createTestFile(file1);
const QString file2 = homeTmpDir() + "fileFromHome2";
createTestFile(file2);
const QString file3 = homeTmpDir() + "anotherFile";
createTestFile(file3);
const QString existingDest = otherTmpDir() + "fileFromHome";
createTestFile(existingDest);
const QString existingDest2 = otherTmpDir() + "fileFromHome2";
createTestFile(existingDest2);
ScopedCleaner cleaner([] {
QFile::remove(otherTmpDir() + "anotherFile");
});
const QList<QUrl> urls{QUrl::fromLocalFile(file1), QUrl::fromLocalFile(file2), QUrl::fromLocalFile(file3)};
KIO::CopyJob *job = KIO::move(urls, QUrl::fromLocalFile(otherTmpDir()), KIO::HideProgressInfo);
MockAskUserInterface *askUserHandler = nullptr;
if (autoSkip) {
job->setUiDelegate(nullptr);
job->setAutoSkip(true);
} else {
// Simulate the user pressing "Skip" in the dialog.
job->setUiDelegate(new KJobUiDelegate);
askUserHandler = new MockAskUserInterface(job->uiDelegate());
askUserHandler->m_renameResult = KIO::Result_Skip;
}
QVERIFY2(job->exec(), qPrintable(job->errorString()));
if (askUserHandler) {
QCOMPARE(askUserHandler->m_askUserRenameCalled, 2);
QCOMPARE(askUserHandler->m_askUserSkipCalled, 0);
}
QVERIFY(QFile::exists(file1)); // it was skipped
QVERIFY(QFile::exists(file2)); // it was skipped
QVERIFY(!QFile::exists(file3)); // it was moved
QCOMPARE(job->totalAmount(KJob::Files), 3);
QCOMPARE(job->totalAmount(KJob::Directories), 0);
QCOMPARE(job->processedAmount(KJob::Files), 1);
QCOMPARE(job->processedAmount(KJob::Directories), 0);
QCOMPARE(job->percent(), 100);
}
void JobTest::copyFileDestAlreadyExists_data()
{
QTest::addColumn<bool>("autoSkip");
QTest::newRow("autoSkip") << true;
QTest::newRow("manualSkip") << false;
}
static void simulatePressingSkip(KJob *job)
{
// Simulate the user pressing "Skip" in the dialog.
job->setUiDelegate(new KJobUiDelegate);
auto *askUserHandler = new MockAskUserInterface(job->uiDelegate());
askUserHandler->m_skipResult = KIO::Result_Skip;
}
void JobTest::copyFileDestAlreadyExists() // to test skipping when copying
{
QFETCH(bool, autoSkip);
const QString file1 = homeTmpDir() + "fileFromHome";
createTestFile(file1);
const QString file2 = homeTmpDir() + "anotherFile";
createTestFile(file2);
const QString existingDest = otherTmpDir() + "fileFromHome";
createTestFile(existingDest);
ScopedCleaner cleaner([] {
QFile::remove(otherTmpDir() + "anotherFile");
});
const QList<QUrl> urls{QUrl::fromLocalFile(file1), QUrl::fromLocalFile(file2)};
KIO::CopyJob *job = KIO::copy(urls, QUrl::fromLocalFile(otherTmpDir()), KIO::HideProgressInfo);
if (autoSkip) {
job->setUiDelegate(nullptr);
job->setAutoSkip(true);
} else {
simulatePressingSkip(job);
}
QVERIFY2(job->exec(), qPrintable(job->errorString()));
QVERIFY(QFile::exists(otherTmpDir() + "anotherFile"));
QCOMPARE(job->totalAmount(KJob::Files), 2); // file1, file2
QCOMPARE(job->totalAmount(KJob::Directories), 0);
QCOMPARE(job->processedAmount(KJob::Files), 1);
QCOMPARE(job->processedAmount(KJob::Directories), 0);
QCOMPARE(job->percent(), 100);
}
void JobTest::moveDestAlreadyExistsAutoRename_data()
{
QTest::addColumn<bool>("samePartition");
QTest::addColumn<bool>("moveDirs");
QTest::newRow("files_same_partition") << true << false;
QTest::newRow("files_other_partition") << false << false;
QTest::newRow("dirs_same_partition") << true << true;
QTest::newRow("dirs_other_partition") << false << true;
}
void JobTest::moveDestAlreadyExistsAutoRename()
{
QFETCH(bool, samePartition);
QFETCH(bool, moveDirs);
QString dir;
if (samePartition) {
dir = homeTmpDir() + "dir/";
QVERIFY(QDir(dir).exists() || QDir().mkdir(dir));
} else {
dir = otherTmpDir();
}
moveDestAlreadyExistsAutoRename(dir, moveDirs);
if (samePartition) {
// cleanup
KIO::Job *job = KIO::del(QUrl::fromLocalFile(dir), KIO::HideProgressInfo);
QVERIFY2(job->exec(), qPrintable(job->errorString()));
QVERIFY(!QFile::exists(dir));
}
}
void JobTest::moveDestAlreadyExistsAutoRename(const QString &destDir, bool moveDirs) // #256650
{
const QString prefix = moveDirs ? QStringLiteral("dir ") : QStringLiteral("file ");
const QString file1 = homeTmpDir() + prefix + "(1)";
const QString file2 = homeTmpDir() + prefix + "(2)";
const QString existingDest1 = destDir + prefix + "(1)";
const QString existingDest2 = destDir + prefix + "(2)";
const QStringList sources = QStringList{file1, file2, existingDest1, existingDest2};
for (const QString &source : sources) {
if (moveDirs) {
QVERIFY(QDir().mkdir(source));
createTestFile(source + "/innerfile");
createTestFile(source + "/innerfile2");
} else {
createTestFile(source);
}
}
const QString file3 = destDir + prefix + "(3)";
const QString file4 = destDir + prefix + "(4)";
ScopedCleaner cleaner([&]() {
if (moveDirs) {
QDir().rmdir(file1);
QDir().rmdir(file2);
QDir().rmdir(file3);
QDir().rmdir(file4);
} else {
QFile::remove(file1);
QFile::remove(file2);
QFile::remove(file3);
QFile::remove(file4);
}
});
const QList<QUrl> urls = {QUrl::fromLocalFile(file1), QUrl::fromLocalFile(file2)};
KIO::CopyJob *job = KIO::move(urls, QUrl::fromLocalFile(destDir), KIO::HideProgressInfo);
job->setUiDelegate(nullptr);
job->setUiDelegateExtension(nullptr);
job->setAutoRename(true);
QSignalSpy spyRenamed(job, &KIO::CopyJob::renamed);
// qDebug() << QDir(destDir).entryList();
QVERIFY2(job->exec(), qPrintable(job->errorString()));
// qDebug() << QDir(destDir).entryList();
QVERIFY(!QFile::exists(file1)); // it was moved
QVERIFY(!QFile::exists(file2)); // it was moved
QVERIFY(QFile::exists(existingDest1));
QVERIFY(QFile::exists(existingDest2));
QVERIFY(QFile::exists(file3));
QVERIFY(QFile::exists(file4));
QVERIFY(!spyRenamed.isEmpty());
auto list = spyRenamed.takeFirst();
QCOMPARE(list.at(1).toUrl(), QUrl::fromLocalFile(destDir + prefix + "(1)"));
QCOMPARE(list.at(2).toUrl(), QUrl::fromLocalFile(file3));
bool samePartition = false;
// Normally we'd see renamed(1, 3) and renamed(2, 4)
// But across partitions, direct rename fails, and we end up with a task list of
// 1->3, 2->3 since renaming 1 to 3 didn't happen yet.
// so renamed(2, 3) is emitted, as if the user had chosen that.
// And when that fails, we then get (3, 4)
if (spyRenamed.count() == 1) {
// It was indeed on the same partition
samePartition = true;
list = spyRenamed.takeFirst();
QCOMPARE(list.at(1).toUrl(), QUrl::fromLocalFile(destDir + prefix + "(2)"));
QCOMPARE(list.at(2).toUrl(), QUrl::fromLocalFile(file4));
} else {
// Remove all renamed signals about innerfiles
spyRenamed.erase(std::remove_if(spyRenamed.begin(),
spyRenamed.end(),
[](const QList<QVariant> &spy) {
return spy.at(1).toUrl().path().contains("innerfile");
}),
spyRenamed.end());
list = spyRenamed.takeFirst();
QCOMPARE(list.at(1).toUrl(), QUrl::fromLocalFile(destDir + prefix + "(2)"));
QCOMPARE(list.at(2).toUrl(), QUrl::fromLocalFile(file3));
list = spyRenamed.takeFirst();
QCOMPARE(list.at(1).toUrl(), QUrl::fromLocalFile(file3));
QCOMPARE(list.at(2).toUrl(), QUrl::fromLocalFile(file4));
}
if (samePartition) {
QCOMPARE(job->totalAmount(KJob::Files), 2); // direct-renamed, so counted as files
QCOMPARE(job->totalAmount(KJob::Directories), 0);
QCOMPARE(job->processedAmount(KJob::Files), 2);
QCOMPARE(job->processedAmount(KJob::Directories), 0);
} else {
if (moveDirs) {
QCOMPARE(job->totalAmount(KJob::Directories), 2);
QCOMPARE(job->totalAmount(KJob::Files), 4); // innerfiles
QCOMPARE(job->processedAmount(KJob::Directories), 2);
QCOMPARE(job->processedAmount(KJob::Files), 4);
} else {
QCOMPARE(job->totalAmount(KJob::Files), 2);
QCOMPARE(job->totalAmount(KJob::Directories), 0);
QCOMPARE(job->processedAmount(KJob::Files), 2);
QCOMPARE(job->processedAmount(KJob::Directories), 0);
}
}
QCOMPARE(job->percent(), 100);
}
void JobTest::copyDirectoryAlreadyExistsSkip()
{
// when copying a directory (which contains at least one file) to some location, and then
// copying the same dir to the same location again, and clicking "Skip" there should be no
// segmentation fault, bug 408350
const QString src = homeTmpDir() + "a";
createTestDirectory(src);
const QString dest = homeTmpDir() + "dest";
createTestDirectory(dest);
QUrl u = QUrl::fromLocalFile(src);
QUrl d = QUrl::fromLocalFile(dest);
KIO::Job *job = KIO::copy(u, d, KIO::HideProgressInfo);
job->setUiDelegate(nullptr);
QVERIFY2(job->exec(), qPrintable(job->errorString()));
QVERIFY(QFile::exists(dest + QStringLiteral("/a/testfile")));
job = KIO::copy(u, d, KIO::HideProgressInfo);
simulatePressingSkip(job);
QVERIFY2(job->exec(), qPrintable(job->errorString()));
QVERIFY(QFile::exists(dest + QStringLiteral("/a/testfile")));
QDir(src).removeRecursively();
QDir(dest).removeRecursively();
QCOMPARE(job->totalAmount(KJob::Files), 2); // testfile, testlink
QCOMPARE(job->totalAmount(KJob::Directories), 1);
QCOMPARE(job->processedAmount(KJob::Files), 0);
QCOMPARE(job->processedAmount(KJob::Directories), 1);
QCOMPARE(job->percent(), 0);
}
void JobTest::copyFileAlreadyExistsRename()
{
const QString sourceFile = homeTmpDir() + "file";
const QString dest = homeTmpDir() + "dest/";
const QString alreadyExisting = dest + "file";
const QString renamedFile = dest + "file-renamed";
createTestFile(sourceFile);
createTestFile(alreadyExisting);
QVERIFY(QFile::exists(sourceFile));
QVERIFY(QFile::exists(alreadyExisting));
createTestDirectory(dest);
ScopedCleaner cleaner([&] {
QVERIFY(QFile(sourceFile).remove());
QVERIFY(QDir(dest).removeRecursively());
});
QUrl s = QUrl::fromLocalFile(sourceFile);
QUrl d = QUrl::fromLocalFile(dest);
KIO::CopyJob *job = KIO::copy(s, d, KIO::HideProgressInfo);
// Simulate the user pressing "Rename" in the dialog and choosing another destination.
job->setUiDelegate(new KJobUiDelegate);
auto *askUserHandler = new MockAskUserInterface(job->uiDelegate());
askUserHandler->m_renameResult = KIO::Result_Rename;
askUserHandler->m_newDestUrl = QUrl::fromLocalFile(renamedFile);
QSignalSpy spyRenamed(job, &KIO::CopyJob::renamed);
QVERIFY2(job->exec(), qPrintable(job->errorString()));
QVERIFY(QFile::exists(renamedFile));
QCOMPARE(spyRenamed.count(), 1);
auto list = spyRenamed.takeFirst();
QCOMPARE(list.at(1).toUrl(), QUrl::fromLocalFile(alreadyExisting));
QCOMPARE(list.at(2).toUrl(), QUrl::fromLocalFile(renamedFile));
}
void JobTest::safeOverwrite_data()
{
QTest::addColumn<bool>("destFileExists");
QTest::newRow("dest_file_exists") << true;
QTest::newRow("dest_file_does_not_exist") << false;
}
void JobTest::safeOverwrite()
{
#ifdef Q_OS_WIN
QSKIP("Test skipped on Windows");
#endif
QFETCH(bool, destFileExists);
const QString srcDir = homeTmpDir() + "overwrite";
const QString srcFile = srcDir + "/testfile";
const QString destDir = otherTmpDir() + "overwrite_other";
const QString destFile = destDir + "/testfile";
const QString destPartFile = destFile + ".part";
createTestDirectory(srcDir);
createTestDirectory(destDir);
ScopedCleaner cleaner([&] {
QDir(srcDir).removeRecursively();
QDir(destDir).removeRecursively();
});
const int srcSize = 1000000; // ~1MB
QVERIFY(QFile::resize(srcFile, srcSize));
if (!destFileExists) {
QVERIFY(QFile::remove(destFile));
} else {
QVERIFY(QFile::exists(destFile));
}
QVERIFY(!QFile::exists(destPartFile));
if (otherTmpDirIsOnSamePartition()) {
QSKIP(qPrintable(QStringLiteral("This test requires %1 and %2 to be on different partitions").arg(srcDir, destDir)));
}
KIO::FileCopyJob *job = KIO::file_move(QUrl::fromLocalFile(srcFile), QUrl::fromLocalFile(destFile), -1, KIO::HideProgressInfo | KIO::Overwrite);
job->setUiDelegate(nullptr);
QSignalSpy spyTotalSize(job, &KIO::FileCopyJob::totalSize);
connect(job, &KIO::FileCopyJob::processedSize, this, [&](KJob *job, qulonglong size) {
Q_UNUSED(job);
if (size > 0 && size < srcSize) {
// To avoid overwriting dest, we want the KIO worker to use dest.part
QCOMPARE(QFileInfo::exists(destPartFile), destFileExists);
}
});
QVERIFY2(job->exec(), qPrintable(job->errorString()));
QVERIFY(QFile::exists(destFile));
QVERIFY(!QFile::exists(srcFile));
QVERIFY(!QFile::exists(destPartFile));
QCOMPARE(spyTotalSize.count(), 1);
}
void JobTest::overwriteOlderFiles_data()
{
QTest::addColumn<bool>("destFileOlder");
QTest::addColumn<bool>("moving");
QTest::newRow("dest_file_older_copying") << true << false;
QTest::newRow("dest_file_older_moving") << true << true;
QTest::newRow("dest_file_younger_copying") << false << false;
QTest::newRow("dest_file_younger_moving") << false << true;
}
void JobTest::overwriteOlderFiles()
{
QFETCH(bool, destFileOlder);
QFETCH(bool, moving);
const QString srcDir = homeTmpDir() + "overwrite";
const QString srcFile = srcDir + "/testfile";
const QString srcFile2 = srcDir + "/testfile2";
const QString srcFile3 = srcDir + "/testfile3";
const QString destDir = otherTmpDir() + "overwrite_other";
const QString destFile = destDir + "/testfile";
const QString destFile2 = destDir + "/testfile2";
const QString destFile3 = destDir + "/testfile3";
const QString destPartFile = destFile + ".part";
createTestDirectory(srcDir);
createTestDirectory(destDir);
createTestFile(srcFile2);
createTestFile(srcFile3);
createTestFile(destFile2);
createTestFile(destFile3);
QVERIFY(!QFile::exists(destPartFile));
const int srcSize = 1000; // ~1KB
QVERIFY(QFile::resize(srcFile, srcSize));
QVERIFY(QFile::resize(srcFile2, srcSize));
QVERIFY(QFile::resize(srcFile3, srcSize));
if (destFileOlder) {
setTimeStamp(destFile, QFile(srcFile).fileTime(QFileDevice::FileModificationTime).addSecs(-2));
setTimeStamp(destFile2, QFile(srcFile2).fileTime(QFileDevice::FileModificationTime).addSecs(-2));
QVERIFY(QFile(destFile).fileTime(QFileDevice::FileModificationTime) <= QFile(srcFile).fileTime(QFileDevice::FileModificationTime));
QVERIFY(QFile(destFile2).fileTime(QFileDevice::FileModificationTime) <= QFile(srcFile2).fileTime(QFileDevice::FileModificationTime));
} else {
setTimeStamp(destFile, QFile(srcFile).fileTime(QFileDevice::FileModificationTime).addSecs(2));
setTimeStamp(destFile2, QFile(srcFile2).fileTime(QFileDevice::FileModificationTime).addSecs(2));
QVERIFY(QFile(destFile).fileTime(QFileDevice::FileModificationTime) >= QFile(srcFile).fileTime(QFileDevice::FileModificationTime));
QVERIFY(QFile(destFile2).fileTime(QFileDevice::FileModificationTime) >= QFile(srcFile2).fileTime(QFileDevice::FileModificationTime));
}
// to have an always skipped file
setTimeStamp(destFile3, QFile(srcFile3).fileTime(QFileDevice::FileModificationTime).addSecs(2));
KIO::CopyJob *job;
if (moving) {
job = KIO::move({QUrl::fromLocalFile(srcFile), QUrl::fromLocalFile(srcFile2), QUrl::fromLocalFile(srcFile3)},
QUrl::fromLocalFile(destDir),
KIO::HideProgressInfo);
} else {
job = KIO::copy({QUrl::fromLocalFile(srcFile), QUrl::fromLocalFile(srcFile2), QUrl::fromLocalFile(srcFile3)},
QUrl::fromLocalFile(destDir),
KIO::HideProgressInfo);
}
job->setUiDelegate(new KJobUiDelegate);
auto *askUserHandler = new MockAskUserInterface(job->uiDelegate());
askUserHandler->m_renameResult = KIO::Result_OverwriteWhenOlder;
QVERIFY2(job->exec(), qPrintable(job->errorString()));
QCOMPARE(askUserHandler->m_askUserRenameCalled, 1);
QVERIFY(!QFile::exists(destPartFile));
// QCOMPARE(spyTotalSize.count(), 1);
// skipped file whose dest is always newer
QVERIFY(QFile::exists(srcFile3)); // it was skipped
QCOMPARE(QFile(destFile3).size(), 11);
if (destFileOlder) {
// files were overwritten
QCOMPARE(QFile(destFile).size(), 1000);
QCOMPARE(QFile(destFile2).size(), 1000);
// files were overwritten
QCOMPARE(job->processedAmount(KJob::Files), 2);
QCOMPARE(job->processedAmount(KJob::Directories), 0);
if (moving) {
QVERIFY(!QFile::exists(srcFile)); // it was moved
QVERIFY(!QFile::exists(srcFile2)); // it was moved
} else {
QVERIFY(QFile::exists(srcFile)); // it was copied
QVERIFY(QFile::exists(srcFile2)); // it was copied
QCOMPARE(QFile(destFile).fileTime(QFileDevice::FileModificationTime), QFile(srcFile).fileTime(QFileDevice::FileModificationTime));
QCOMPARE(QFile(destFile2).fileTime(QFileDevice::FileModificationTime), QFile(srcFile2).fileTime(QFileDevice::FileModificationTime));
}
} else {
// files were skipped
QCOMPARE(job->processedAmount(KJob::Files), 0);
QCOMPARE(job->processedAmount(KJob::Directories), 0);
QCOMPARE(QFile(destFile).size(), 11);
QCOMPARE(QFile(destFile2).size(), 11);
QVERIFY(QFile::exists(srcFile));
QVERIFY(QFile::exists(srcFile2));
}
QDir(srcDir).removeRecursively();
QDir(destDir).removeRecursively();
}
void JobTest::moveAndOverwrite()
{
const QString sourceFile = homeTmpDir() + "fileFromHome";
createTestFile(sourceFile);
QString existingDest = otherTmpDir() + "fileFromHome";
createTestFile(existingDest);
KIO::FileCopyJob *job = KIO::file_move(QUrl::fromLocalFile(sourceFile), QUrl::fromLocalFile(existingDest), -1, KIO::HideProgressInfo | KIO::Overwrite);
job->setUiDelegate(nullptr);
QVERIFY2(job->exec(), qPrintable(job->errorString()));
QVERIFY(!QFile::exists(sourceFile)); // it was moved
#ifndef Q_OS_WIN
// Now same thing when the target is a symlink to the source
createTestFile(sourceFile);
createTestSymlink(existingDest, QFile::encodeName(sourceFile));
QVERIFY(QFile::exists(existingDest));
job = KIO::file_move(QUrl::fromLocalFile(sourceFile), QUrl::fromLocalFile(existingDest), -1, KIO::HideProgressInfo | KIO::Overwrite);
job->setUiDelegate(nullptr);
QVERIFY2(job->exec(), qPrintable(job->errorString()));
QVERIFY(!QFile::exists(sourceFile)); // it was moved
// Now same thing when the target is a symlink to another file
createTestFile(sourceFile);
createTestFile(sourceFile + QLatin1Char('2'));
createTestSymlink(existingDest, QFile::encodeName(sourceFile + QLatin1Char('2')));
QVERIFY(QFile::exists(existingDest));
job = KIO::file_move(QUrl::fromLocalFile(sourceFile), QUrl::fromLocalFile(existingDest), -1, KIO::HideProgressInfo | KIO::Overwrite);
job->setUiDelegate(nullptr);
QVERIFY2(job->exec(), qPrintable(job->errorString()));
QVERIFY(!QFile::exists(sourceFile)); // it was moved
// Now same thing when the target is a _broken_ symlink
createTestFile(sourceFile);
createTestSymlink(existingDest);
QVERIFY(!QFile::exists(existingDest)); // it exists, but it's broken...
job = KIO::file_move(QUrl::fromLocalFile(sourceFile), QUrl::fromLocalFile(existingDest), -1, KIO::HideProgressInfo | KIO::Overwrite);
job->setUiDelegate(nullptr);
QVERIFY2(job->exec(), qPrintable(job->errorString()));
QVERIFY(!QFile::exists(sourceFile)); // it was moved
#endif
}
void JobTest::moveOverSymlinkToSelf() // #169547
{
#ifndef Q_OS_WIN
const QString sourceFile = homeTmpDir() + "fileFromHome";
createTestFile(sourceFile);
const QString existingDest = homeTmpDir() + "testlink";
createTestSymlink(existingDest, QFile::encodeName(sourceFile));
QVERIFY(QFile::exists(existingDest));
KIO::CopyJob *job = KIO::move(QUrl::fromLocalFile(sourceFile), QUrl::fromLocalFile(existingDest), KIO::HideProgressInfo);
job->setUiDelegate(nullptr);
QVERIFY(!job->exec());
QCOMPARE(job->error(), (int)KIO::ERR_FILE_ALREADY_EXIST); // and not ERR_IDENTICAL_FILES!
QVERIFY(QFile::exists(sourceFile)); // it not moved
#endif
}
void JobTest::createSymlink()
{
#ifdef Q_OS_WIN
QSKIP("Test skipped on Windows");
#endif
const QString sourceFile = homeTmpDir() + "fileFromHome";
createTestFile(sourceFile);
const QString destDir = homeTmpDir() + "dest";
QVERIFY(QDir().mkpath(destDir));
ScopedCleaner cleaner([&] {
QDir(destDir).removeRecursively();
});
// With KIO::link (high-level)
KIO::CopyJob *job = KIO::link(QUrl::fromLocalFile(sourceFile), QUrl::fromLocalFile(destDir), KIO::HideProgressInfo);
QVERIFY2(job->exec(), qPrintable(job->errorString()));
QVERIFY(QFileInfo::exists(sourceFile));
const QString dest = destDir + "/fileFromHome";
QVERIFY(QFileInfo(dest).isSymLink());
QCOMPARE(QFileInfo(dest).symLinkTarget(), sourceFile);
QFile::remove(dest);
// With KIO::symlink (low-level)
const QString linkPath = destDir + "/link";
KIO::Job *symlinkJob = KIO::symlink(sourceFile, QUrl::fromLocalFile(linkPath), KIO::HideProgressInfo);
QVERIFY2(symlinkJob->exec(), qPrintable(symlinkJob->errorString()));
QVERIFY(QFileInfo::exists(sourceFile));
QVERIFY(QFileInfo(linkPath).isSymLink());
QCOMPARE(QFileInfo(linkPath).symLinkTarget(), sourceFile);
}
void JobTest::createSymlinkTargetDirDoesntExist()
{
#ifdef Q_OS_WIN
QSKIP("Test skipped on Windows");
#endif
const QString sourceFile = homeTmpDir() + "fileFromHome";
createTestFile(sourceFile);
const QString destDir = homeTmpDir() + "dest/does/not/exist";
KIO::CopyJob *job = KIO::link(QUrl::fromLocalFile(sourceFile), QUrl::fromLocalFile(destDir), KIO::HideProgressInfo);
QVERIFY(!job->exec());
QCOMPARE(job->error(), static_cast<int>(KIO::ERR_CANNOT_SYMLINK));
}
void JobTest::createSymlinkAsShouldSucceed()
{
#ifdef Q_OS_WIN
QSKIP("Test skipped on Windows");
#endif
const QString sourceFile = homeTmpDir() + "fileFromHome";
createTestFile(sourceFile);
const QString dest = homeTmpDir() + "testlink";
QFile::remove(dest); // just in case
ScopedCleaner cleaner([&] {
QVERIFY(QFile::remove(dest));
});
KIO::CopyJob *job = KIO::linkAs(QUrl::fromLocalFile(sourceFile), QUrl::fromLocalFile(dest), KIO::HideProgressInfo);
QVERIFY2(job->exec(), qPrintable(job->errorString()));
QVERIFY(QFileInfo::exists(sourceFile));
QVERIFY(QFileInfo(dest).isSymLink());
}
void JobTest::createSymlinkAsShouldFailDirectoryExists()
{
#ifdef Q_OS_WIN
QSKIP("Test skipped on Windows");
#endif
const QString sourceFile = homeTmpDir() + "fileFromHome";
createTestFile(sourceFile);
const QString dest = homeTmpDir() + "dest";
QVERIFY(QDir().mkpath(dest)); // dest exists as a directory
ScopedCleaner cleaner([&] {
QVERIFY(QDir().rmdir(dest));
});
// With KIO::link (high-level)
KIO::CopyJob *job = KIO::linkAs(QUrl::fromLocalFile(sourceFile), QUrl::fromLocalFile(dest), KIO::HideProgressInfo);
QVERIFY(!job->exec());
QCOMPARE(job->error(), (int)KIO::ERR_DIR_ALREADY_EXIST);
QVERIFY(QFileInfo::exists(sourceFile));
QVERIFY(!QFileInfo::exists(dest + "/fileFromHome"));
// With KIO::symlink (low-level)
KIO::Job *symlinkJob = KIO::symlink(sourceFile, QUrl::fromLocalFile(dest), KIO::HideProgressInfo);
QVERIFY(!symlinkJob->exec());
QCOMPARE(symlinkJob->error(), (int)KIO::ERR_DIR_ALREADY_EXIST);
QVERIFY(QFileInfo::exists(sourceFile));
}
void JobTest::createSymlinkAsShouldFailFileExists()
{
#ifdef Q_OS_WIN
QSKIP("Test skipped on Windows");
#endif
const QString sourceFile = homeTmpDir() + "fileFromHome";
createTestFile(sourceFile);
const QString dest = homeTmpDir() + "testlink";
QFile::remove(dest); // just in case
ScopedCleaner cleaner([&] {
QVERIFY(QFile::remove(sourceFile));
QVERIFY(QFile::remove(dest));
});
// First time works
KIO::CopyJob *job = KIO::linkAs(QUrl::fromLocalFile(sourceFile), QUrl::fromLocalFile(dest), KIO::HideProgressInfo);
QVERIFY2(job->exec(), qPrintable(job->errorString()));
QVERIFY(QFileInfo(dest).isSymLink());
// Second time fails (already exists)
job = KIO::linkAs(QUrl::fromLocalFile(sourceFile), QUrl::fromLocalFile(dest), KIO::HideProgressInfo);
QVERIFY(!job->exec());
QCOMPARE(job->error(), (int)KIO::ERR_FILE_ALREADY_EXIST);
// KIO::symlink fails too
KIO::Job *symlinkJob = KIO::symlink(sourceFile, QUrl::fromLocalFile(dest), KIO::HideProgressInfo);
QVERIFY(!symlinkJob->exec());
QCOMPARE(symlinkJob->error(), (int)KIO::ERR_FILE_ALREADY_EXIST);
}
void JobTest::createSymlinkWithOverwriteShouldWork()
{
#ifdef Q_OS_WIN
QSKIP("Test skipped on Windows");
#endif
const QString sourceFile = homeTmpDir() + "fileFromHome";
createTestFile(sourceFile);
const QString dest = homeTmpDir() + "testlink";
QFile::remove(dest); // just in case
ScopedCleaner cleaner([&] {
QVERIFY(QFile::remove(sourceFile));
QVERIFY(QFile::remove(dest));
});
// First time works
KIO::CopyJob *job = KIO::linkAs(QUrl::fromLocalFile(sourceFile), QUrl::fromLocalFile(dest), KIO::HideProgressInfo);
QVERIFY2(job->exec(), qPrintable(job->errorString()));
QVERIFY(QFileInfo(dest).isSymLink());
// Changing the link target, with overwrite, works
job = KIO::linkAs(QUrl::fromLocalFile(sourceFile + QLatin1Char('2')), QUrl::fromLocalFile(dest), KIO::Overwrite | KIO::HideProgressInfo);
QVERIFY2(job->exec(), qPrintable(job->errorString()));
QVERIFY(QFileInfo(dest).isSymLink());
QCOMPARE(QFileInfo(dest).symLinkTarget(), QString(sourceFile + QLatin1Char('2')));
// Changing the link target using KIO::symlink, with overwrite, works
KIO::Job *symlinkJob = KIO::symlink(sourceFile + QLatin1Char('3'), QUrl::fromLocalFile(dest), KIO::Overwrite | KIO::HideProgressInfo);
QVERIFY2(symlinkJob->exec(), qPrintable(symlinkJob->errorString()));
QVERIFY(QFileInfo(dest).isSymLink());
QCOMPARE(QFileInfo(dest).symLinkTarget(), QString(sourceFile + QLatin1Char('3')));
}
void JobTest::createBrokenSymlink()
{
#ifdef Q_OS_WIN
QSKIP("Test skipped on Windows");
#endif
const QString sourceFile = "/does/not/exist";
const QString dest = homeTmpDir() + "testlink";
QFile::remove(dest); // just in case
KIO::CopyJob *job = KIO::linkAs(QUrl::fromLocalFile(sourceFile), QUrl::fromLocalFile(dest), KIO::HideProgressInfo);
QVERIFY2(job->exec(), qPrintable(job->errorString()));
QVERIFY(QFileInfo(dest).isSymLink());
// Second time fails (already exists)
job = KIO::linkAs(QUrl::fromLocalFile(sourceFile), QUrl::fromLocalFile(dest), KIO::HideProgressInfo);
QVERIFY(!job->exec());
QCOMPARE(job->error(), (int)KIO::ERR_FILE_ALREADY_EXIST);
QVERIFY(QFile::remove(dest));
}
#if KIOCORE_BUILD_DEPRECATED_SINCE(5, 84)
void JobTest::multiGet()
{
const int numFiles = 10;
const QString baseDir = homeTmpDir();
const QList<QUrl> urls = createManyFiles(baseDir, numFiles);
QCOMPARE(urls.count(), numFiles);
// qDebug() << file;
KIO::MultiGetJob *job = KIO::multi_get(0, urls.at(0), KIO::MetaData()); // TODO: missing KIO::HideProgressInfo
#if KIOCORE_BUILD_DEPRECATED_SINCE(5, 79)
QSignalSpy spyData(job, &KIO::MultiGetJob::data);
#else
QSignalSpy spyData(job, &KIO::MultiGetJob::dataReceived);
#endif
#if KIOCORE_BUILD_DEPRECATED_SINCE(5, 78)
QSignalSpy spyMimeType(job, &KIO::MultiGetJob::mimetype);
#endif
QSignalSpy spyMimeTypeFound(job, &KIO::MultiGetJob::mimeTypeFound);
#if KIOCORE_BUILD_DEPRECATED_SINCE(5, 79)
QSignalSpy spyResultId(job, qOverload<long>(&KIO::MultiGetJob::result));
#else
QSignalSpy spyResultId(job, &KIO::MultiGetJob::fileTransferred);
#endif
QSignalSpy spyResult(job, &KJob::result);
job->setUiDelegate(nullptr);
for (int i = 1; i < numFiles; ++i) {
const QUrl url = urls.at(i);
job->get(i, url, KIO::MetaData());
}
// connect(job, &KIO::MultiGetJob::result, [=] (long id) { qDebug() << "ID I got" << id;});
// connect(job, &KJob::result, [this](KJob* ) {qDebug() << "END";});
QVERIFY2(job->exec(), qPrintable(job->errorString()));
QCOMPARE(spyResult.count(), 1);
QCOMPARE(spyResultId.count(), numFiles);
#if KIOCORE_BUILD_DEPRECATED_SINCE(5, 78)
QCOMPARE(spyMimeType.count(), numFiles);
#endif
QCOMPARE(spyMimeTypeFound.count(), numFiles);
QCOMPARE(spyData.count(), numFiles * 2);
for (int i = 0; i < numFiles; ++i) {
QCOMPARE(spyResultId.at(i).at(0).toInt(), i);
#if KIOCORE_BUILD_DEPRECATED_SINCE(5, 78)
QCOMPARE(spyMimeType.at(i).at(0).toInt(), i);
QCOMPARE(spyMimeType.at(i).at(1).toString(), QStringLiteral("text/plain"));
#endif
QCOMPARE(spyMimeTypeFound.at(i).at(0).toInt(), i);
QCOMPARE(spyMimeTypeFound.at(i).at(1).toString(), QStringLiteral("text/plain"));
QCOMPARE(spyData.at(i * 2).at(0).toInt(), i);
QCOMPARE(QString(spyData.at(i * 2).at(1).toByteArray()), QStringLiteral("Hello"));
QCOMPARE(spyData.at(i * 2 + 1).at(0).toInt(), i);
QCOMPARE(QString(spyData.at(i * 2 + 1).at(1).toByteArray()), QLatin1String(""));
}
}
#endif // Deprecated since 5.84
void JobTest::cancelCopyAndCleanDest_data()
{
QTest::addColumn<bool>("suspend");
QTest::addColumn<bool>("overwrite");
QTest::newRow("suspend_no_overwrite") << true << false;
QTest::newRow("no_suspend_no_overwrite") << false << false;
#ifndef Q_OS_WIN
QTest::newRow("suspend_with_overwrite") << true << true;
QTest::newRow("no_suspend_with_overwrite") << false << true;
#endif
}
void JobTest::cancelCopyAndCleanDest()
{
QFETCH(bool, suspend);
QFETCH(bool, overwrite);
const QString baseDir = homeTmpDir();
const QString srcTemplate = baseDir + QStringLiteral("testfile_XXXXXX");
const QString destFile = baseDir + QStringLiteral("testfile_copy_slow_") + QString::fromLatin1(QTest::currentDataTag());
QTemporaryFile f(srcTemplate);
if (!f.open()) {
qFatal("Couldn't open %s", qPrintable(f.fileName()));
}
const int sz = 4000000; //~4MB
f.seek(sz - 1);
f.write("0");
f.close();
QCOMPARE(f.size(), sz);
if (overwrite) {
createTestFile(destFile);
}
const QString destToCheck = (overwrite) ? destFile + QStringLiteral(".part") : destFile;
KIO::JobFlag m_overwriteFlag = overwrite ? KIO::Overwrite : KIO::DefaultFlags;
KIO::FileCopyJob *copyJob = KIO::file_copy(QUrl::fromLocalFile(f.fileName()), QUrl::fromLocalFile(destFile), -1, KIO::HideProgressInfo | m_overwriteFlag);
copyJob->setUiDelegate(nullptr);
QSignalSpy spyProcessedSize(copyJob, &KIO::Job::processedSize);
QSignalSpy spyFinished(copyJob, &KIO::Job::finished);
connect(copyJob, &KIO::Job::processedSize, this, [destFile, suspend, destToCheck](KJob *job, qulonglong processedSize) {
if (processedSize > 0) {
QVERIFY2(QFile::exists(destToCheck), qPrintable(destToCheck));
qDebug() << "processedSize=" << processedSize << "file size" << QFileInfo(destToCheck).size();
if (suspend) {
job->suspend();
}
QVERIFY(job->kill());
}
});
QVERIFY(!copyJob->exec());
QCOMPARE(spyProcessedSize.count(), 1);
QCOMPARE(spyFinished.count(), 1);
QCOMPARE(copyJob->error(), KIO::ERR_USER_CANCELED);
// the destination file actual deletion happens after finished() is emitted
// we need to give some time to the KIO worker to finish the file cleaning
QTRY_VERIFY2(!QFile::exists(destToCheck), qPrintable(destToCheck));
}
#include "moc_jobtest.cpp"
|