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
|
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/351564777): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif
#include "storage/browser/file_system/obfuscated_file_util.h"
#include <stddef.h>
#include <stdint.h>
#include <array>
#include <limits>
#include <memory>
#include <optional>
#include <set>
#include <string>
#include <utility>
#include <vector>
#include "base/files/file.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/scoped_refptr.h"
#include "base/run_loop.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/single_thread_task_runner.h"
#include "base/task/thread_pool.h"
#include "base/test/bind.h"
#include "base/test/gmock_expected_support.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/task_environment.h"
#include "base/test/test_future.h"
#include "base/threading/thread_restrictions.h"
#include "build/build_config.h"
#include "components/services/filesystem/public/mojom/types.mojom.h"
#include "net/base/features.h"
#include "net/base/io_buffer.h"
#include "storage/browser/file_system/external_mount_points.h"
#include "storage/browser/file_system/file_system_backend.h"
#include "storage/browser/file_system/file_system_context.h"
#include "storage/browser/file_system/file_system_operation_context.h"
#include "storage/browser/file_system/file_system_url.h"
#include "storage/browser/file_system/file_system_usage_cache.h"
#include "storage/browser/file_system/obfuscated_file_util_memory_delegate.h"
#include "storage/browser/file_system/sandbox_directory_database.h"
#include "storage/browser/file_system/sandbox_file_system_backend_delegate.h"
#include "storage/browser/file_system/sandbox_origin_database.h"
#include "storage/browser/quota/quota_manager.h"
#include "storage/browser/quota/quota_manager_proxy.h"
#include "storage/browser/test/async_file_test_helper.h"
#include "storage/browser/test/file_system_test_file_set.h"
#include "storage/browser/test/mock_file_change_observer.h"
#include "storage/browser/test/mock_special_storage_policy.h"
#include "storage/browser/test/sandbox_file_system_test_helper.h"
#include "storage/browser/test/test_file_system_context.h"
#include "storage/common/database/database_identifier.h"
#include "storage/common/file_system/file_system_types.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/storage_key/storage_key.h"
#include "third_party/leveldatabase/leveldb_chrome.h"
#include "url/gurl.h"
using url::Origin;
namespace storage {
namespace {
enum TestMode {
kRegularFirstParty,
kRegularFirstPartyNonDefaultBucket,
kRegularThirdParty,
kRegularThirdPartyNonDefaultBucket,
kIncognitoFirstParty,
kIncognitoFirstPartyNonDefaultBucket,
kIncognitoThirdParty,
kIncognitoThirdPartyNonDefaultBucket
};
bool FileExists(const base::FilePath& path) {
return base::PathExists(path) && !base::DirectoryExists(path);
}
int64_t GetLocalFileSize(const base::FilePath& path) {
return base::GetFileSize(path).value_or(0);
}
// After a move, the dest exists and the source doesn't.
// After a copy, both source and dest exist.
struct CopyMoveTestCaseRecord {
bool is_copy_not_move;
const char source_path[64];
const char dest_path[64];
bool cause_overwrite;
};
const CopyMoveTestCaseRecord kCopyMoveTestCases[] = {
// This is the combinatoric set of:
// rename vs. same-name
// different directory vs. same directory
// overwrite vs. no-overwrite
// copy vs. move
// We can never be called with source and destination paths identical, so
// those cases are omitted.
{true, "dir0/file0", "dir0/file1", false},
{false, "dir0/file0", "dir0/file1", false},
{true, "dir0/file0", "dir0/file1", true},
{false, "dir0/file0", "dir0/file1", true},
{true, "dir0/file0", "dir1/file0", false},
{false, "dir0/file0", "dir1/file0", false},
{true, "dir0/file0", "dir1/file0", true},
{false, "dir0/file0", "dir1/file0", true},
{true, "dir0/file0", "dir1/file1", false},
{false, "dir0/file0", "dir1/file1", false},
{true, "dir0/file0", "dir1/file1", true},
{false, "dir0/file0", "dir1/file1", true},
};
struct OriginEnumerationTestRecord {
std::string origin_url;
bool has_temporary;
bool has_persistent;
};
const OriginEnumerationTestRecord kOriginEnumerationTestRecords[] = {
{"http://example.com/", false, true},
{"http://example1.com/", true, false},
{"https://example1.com/", true, true},
{"file:///", false, true},
{"http://example.com:8000/", false, true},
};
FileSystemURL FileSystemURLAppend(const FileSystemURL& url,
const base::FilePath::StringType& child) {
FileSystemURL new_url = FileSystemURL::CreateForTest(
url.storage_key(), url.mount_type(), url.virtual_path().Append(child));
if (url.bucket().has_value())
new_url.SetBucket(url.bucket().value());
return new_url;
}
FileSystemURL FileSystemURLAppendUTF8(const FileSystemURL& url,
const std::string& child) {
FileSystemURL new_url = FileSystemURL::CreateForTest(
url.storage_key(), url.mount_type(),
url.virtual_path().Append(base::FilePath::FromUTF8Unsafe(child)));
if (url.bucket().has_value())
new_url.SetBucket(url.bucket().value());
return new_url;
}
FileSystemURL FileSystemURLDirName(const FileSystemURL& url) {
FileSystemURL new_url =
FileSystemURL::CreateForTest(url.storage_key(), url.mount_type(),
VirtualPath::DirName(url.virtual_path()));
if (url.bucket().has_value())
new_url.SetBucket(url.bucket().value());
return new_url;
}
std::string GetTypeString(FileSystemType type) {
return SandboxFileSystemBackendDelegate::GetTypeString(type);
}
bool HasFileSystemType(
ObfuscatedFileUtil::AbstractStorageKeyEnumerator* enumerator,
FileSystemType type) {
return enumerator->HasTypeDirectory(GetTypeString(type));
}
} // namespace
// TODO(ericu): The vast majority of this and the other FSFU subclass tests
// could theoretically be shared. It would basically be a FSFU interface
// compliance test, and only the subclass-specific bits that look into the
// implementation would need to be written per-subclass.
class ObfuscatedFileUtilTest : public testing::Test,
public ::testing::WithParamInterface<TestMode> {
public:
ObfuscatedFileUtilTest()
: task_environment_(base::test::TaskEnvironment::MainThreadType::IO),
storage_key_(blink::StorageKey::CreateFromStringForTesting(
"http://www.example.com")),
type_(kFileSystemTypeTemporary),
sandbox_file_system_(storage_key_, type_),
quota_status_(blink::mojom::QuotaStatusCode::kUnknown),
usage_(-1) {
if (is_third_party_context()) {
scoped_feature_list_.InitAndEnableFeature(
net::features::kThirdPartyStoragePartitioning);
// Once we enable third-party storage partitioning, we can create a
// third-party StorageKey and re-assign the StorageKey value in the
// SandboxFileSystem with this value in SetUp for default buckets.
storage_key_ = blink::StorageKey::Create(
storage_key_.origin(), storage_key_.top_level_site(),
blink::mojom::AncestorChainBit::kCrossSite);
}
}
ObfuscatedFileUtilTest(const ObfuscatedFileUtilTest&) = delete;
ObfuscatedFileUtilTest& operator=(const ObfuscatedFileUtilTest&) = delete;
~ObfuscatedFileUtilTest() override = default;
bool is_incognito() {
return GetParam() == TestMode::kIncognitoFirstParty ||
(GetParam() == TestMode::kIncognitoFirstPartyNonDefaultBucket) ||
(GetParam() == TestMode::kIncognitoThirdParty) ||
(GetParam() == TestMode::kIncognitoThirdPartyNonDefaultBucket);
}
bool is_third_party_context() {
return GetParam() == TestMode::kRegularThirdParty ||
(GetParam() == TestMode::kRegularThirdPartyNonDefaultBucket) ||
(GetParam() == TestMode::kIncognitoThirdParty) ||
(GetParam() == TestMode::kIncognitoThirdPartyNonDefaultBucket);
}
bool is_non_default_bucket() {
return GetParam() == TestMode::kRegularFirstPartyNonDefaultBucket ||
(GetParam() == TestMode::kRegularThirdPartyNonDefaultBucket) ||
(GetParam() == TestMode::kIncognitoFirstPartyNonDefaultBucket) ||
(GetParam() == TestMode::kIncognitoThirdPartyNonDefaultBucket);
}
void SetUp() override {
ASSERT_TRUE(data_dir_.CreateUniqueTempDir());
storage_policy_ = base::MakeRefCounted<MockSpecialStoragePolicy>();
quota_manager_task_runner_ =
base::ThreadPool::CreateSingleThreadTaskRunner({base::MayBlock()});
quota_manager_ = base::MakeRefCounted<QuotaManager>(
is_incognito(), data_dir_.GetPath(), quota_manager_task_runner_,
storage_policy_, GetQuotaSettingsFunc());
quota_manager_task_runner_->PostTask(
FROM_HERE, base::BindOnce(
[](const scoped_refptr<QuotaManager>& quota_manager) {
QuotaSettings settings;
settings.per_storage_key_quota = 250 * 1024 * 1024;
settings.pool_size =
settings.per_storage_key_quota * 5;
settings.must_remain_available = 10 * 1024 * 1024;
settings.refresh_interval = base::TimeDelta::Max();
quota_manager->SetQuotaSettings(settings);
},
quota_manager_));
// Every time we create a new sandbox_file_system helper,
// it creates another context, which creates another path manager,
// another sandbox_backend, and another OFU.
// We need to pass in the context to skip all that.
file_system_context_ =
is_incognito() ? CreateIncognitoFileSystemContextForTesting(
base::SingleThreadTaskRunner::GetCurrentDefault(),
base::SingleThreadTaskRunner::GetCurrentDefault(),
quota_manager_->proxy(), data_dir_.GetPath())
: CreateFileSystemContextForTesting(
quota_manager_->proxy(), data_dir_.GetPath());
// Create the default bucket member corresponding to the StorageKey member
// we created.
base::test::TestFuture<QuotaErrorOr<BucketInfo>> default_future;
quota_manager_->proxy()->UpdateOrCreateBucket(
BucketInitParams::ForDefaultBucket(storage_key()),
base::SequencedTaskRunner::GetCurrentDefault(),
default_future.GetCallback());
QuotaErrorOr<BucketInfo> default_bucket = default_future.Take();
CHECK(default_bucket.has_value());
default_bucket_ = default_bucket.value().ToBucketLocator();
// Create a non-default bucket member corresponding to the StorageKey
// member we created.
base::test::TestFuture<QuotaErrorOr<BucketInfo>> custom_future;
BucketInitParams params = BucketInitParams::ForDefaultBucket(storage_key());
params.name = "non-default bucket";
quota_manager_->proxy()->UpdateOrCreateBucket(
params, base::SequencedTaskRunner::GetCurrentDefault(),
custom_future.GetCallback());
QuotaErrorOr<BucketInfo> custom_bucket = custom_future.Take();
CHECK(custom_bucket.has_value());
custom_bucket_ = custom_bucket.value().ToBucketLocator();
// Create an alternate non-default bucket member corresponding to the
// StorageKey member we created.
base::test::TestFuture<QuotaErrorOr<BucketInfo>> alternate_future;
params.name = "alternate non-default bucket";
quota_manager_->proxy()->UpdateOrCreateBucket(
params, base::SequencedTaskRunner::GetCurrentDefault(),
alternate_future.GetCallback());
QuotaErrorOr<BucketInfo> alternate_bucket = alternate_future.Take();
CHECK(alternate_bucket.has_value());
alternate_custom_bucket_ = alternate_bucket.value().ToBucketLocator();
is_non_default_bucket()
? sandbox_file_system_.SetUp(file_system_context_, custom_bucket_)
: sandbox_file_system_.SetUp(file_system_context_, default_bucket_);
change_observers_ = MockFileChangeObserver::CreateList(&change_observer_);
if (is_incognito())
incognito_leveldb_environment_ = leveldb_chrome::NewMemEnv("FileSystem");
}
void TearDown() override {
if (is_incognito())
ASSERT_TRUE(IsDirectoryEmpty(data_dir_.GetPath()));
quota_manager_ = nullptr;
task_environment_.RunUntilIdle();
sandbox_file_system_.TearDown();
}
std::unique_ptr<FileSystemOperationContext> LimitedContext(
int64_t allowed_bytes_growth) {
std::unique_ptr<FileSystemOperationContext> context(
sandbox_file_system_.NewOperationContext());
context->set_allowed_bytes_growth(allowed_bytes_growth);
return context;
}
std::unique_ptr<FileSystemOperationContext> UnlimitedContext() {
return LimitedContext(std::numeric_limits<int64_t>::max());
}
std::unique_ptr<FileSystemOperationContext> NewContext(
SandboxFileSystemTestHelper* file_system) {
change_observer()->ResetCount();
std::unique_ptr<FileSystemOperationContext> context;
if (file_system)
context = file_system->NewOperationContext();
else
context = sandbox_file_system_.NewOperationContext();
// Setting allowed_bytes_growth big enough for all tests.
context->set_allowed_bytes_growth(1024 * 1024);
context->set_change_observers(change_observers());
return context;
}
const ChangeObserverList& change_observers() const {
return change_observers_;
}
MockFileChangeObserver* change_observer() { return &change_observer_; }
// This can only be used after SetUp has run and created file_system_context_
// and obfuscated_file_util_.
// Use this for tests which need to run in multiple StorageKeys; we need a
// test helper per StorageKey.
std::unique_ptr<SandboxFileSystemTestHelper> NewFileSystem(
const blink::StorageKey& storage_key,
FileSystemType type) {
auto file_system =
std::make_unique<SandboxFileSystemTestHelper>(storage_key, type);
file_system->SetUp(file_system_context_);
return file_system;
}
// This can only be used after SetUp has run and created file_system_context_
// and obfuscated_file_util_.
// Use this for tests which need to run in multiple BucketLocators; we need a
// test helper per BucketLocator.
std::unique_ptr<SandboxFileSystemTestHelper> NewFileSystem(
const BucketLocator& bucket_locator,
FileSystemType type) {
auto file_system = std::make_unique<SandboxFileSystemTestHelper>(
bucket_locator.storage_key, type);
file_system->SetUp(file_system_context_, bucket_locator);
return file_system;
}
std::unique_ptr<ObfuscatedFileUtil> CreateObfuscatedFileUtil(
scoped_refptr<SpecialStoragePolicy> storage_policy) {
return ObfuscatedFileUtil::CreateForTesting(
std::move(storage_policy), data_dir_path(),
is_incognito() ? incognito_leveldb_environment_.get() : nullptr,
is_incognito());
}
ObfuscatedFileUtil* ofu() {
return static_cast<ObfuscatedFileUtil*>(sandbox_file_system_.file_util());
}
const base::FilePath& test_directory() const { return data_dir_.GetPath(); }
const blink::StorageKey& storage_key() const { return storage_key_; }
const Origin& origin() const { return storage_key_.origin(); }
FileSystemType type() const { return type_; }
std::string type_string() const { return GetTypeString(type_); }
int64_t ComputeTotalFileSize() {
return sandbox_file_system_.ComputeCurrentStorageKeyUsage() -
sandbox_file_system_.ComputeCurrentDirectoryDatabaseUsage();
}
void GetUsageFromQuotaManager() {
int64_t quota = -1;
quota_status_ = AsyncFileTestHelper::GetUsageAndQuota(
quota_manager_->proxy(), storage_key(), sandbox_file_system_.type(),
&usage_, "a);
EXPECT_EQ(blink::mojom::QuotaStatusCode::kOk, quota_status_);
}
void RevokeUsageCache() {
quota_manager_task_runner_->PostTask(
FROM_HERE, base::BindOnce(
[](const scoped_refptr<QuotaManager>& quota_manager,
SandboxFileSystemTestHelper* sandbox_file_system) {
quota_manager->ResetUsageTracker();
},
quota_manager_, &sandbox_file_system_));
base::FileErrorOr<base::FilePath> path =
sandbox_file_system_.GetUsageCachePath();
if (path.has_value())
usage_cache()->Delete(path.value());
}
int64_t SizeByQuotaUtil() { return sandbox_file_system_.GetCachedUsage(); }
int64_t SizeInUsageFile() {
task_environment_.RunUntilIdle();
int64_t usage = 0;
base::FileErrorOr<base::FilePath> path =
sandbox_file_system_.GetUsageCachePath();
return (path.has_value() && usage_cache()->GetUsage(path.value(), &usage))
? usage
: -1;
}
bool PathExists(const FileSystemURL& url) {
std::unique_ptr<FileSystemOperationContext> context = NewContext(nullptr);
base::File::Info file_info;
base::FilePath platform_path;
base::File::Error error =
ofu()->GetFileInfo(context.get(), url, &file_info, &platform_path);
return error == base::File::FILE_OK;
}
int64_t GetPathSize(const FileSystemURL& url) {
std::unique_ptr<FileSystemOperationContext> context = NewContext(nullptr);
base::File::Info file_info;
base::FilePath platform_path;
EXPECT_EQ(
base::File::FILE_OK,
ofu()->GetFileInfo(context.get(), url, &file_info, &platform_path));
return file_info.size;
}
bool DirectoryExists(const FileSystemURL& url) {
return AsyncFileTestHelper::DirectoryExists(file_system_context(), url);
}
int64_t usage() const { return usage_; }
FileSystemUsageCache* usage_cache() {
return sandbox_file_system_.usage_cache();
}
FileSystemURL CreateURL(const base::FilePath& path) {
FileSystemURL test_url = sandbox_file_system_.CreateURL(path);
if (is_non_default_bucket())
test_url.SetBucket(custom_bucket_);
return test_url;
}
FileSystemURL CreateURLFromUTF8(const std::string& path) {
FileSystemURL test_url = sandbox_file_system_.CreateURLFromUTF8(path);
if (is_non_default_bucket())
test_url.SetBucket(custom_bucket_);
return test_url;
}
int64_t PathCost(const FileSystemURL& url) {
return ObfuscatedFileUtil::ComputeFilePathCost(url.path());
}
void CheckFile(const FileSystemURL& url) {
std::unique_ptr<FileSystemOperationContext> context = NewContext(nullptr);
base::FilePath local_path;
EXPECT_EQ(base::File::FILE_OK,
ofu()->GetLocalFilePath(context.get(), url, &local_path));
EXPECT_EQ(0, GetPathSize(url));
base::File::Info file_info0;
base::FilePath data_path;
EXPECT_EQ(base::File::FILE_OK,
ofu()->GetFileInfo(context.get(), url, &file_info0, &data_path));
EXPECT_EQ(data_path, local_path);
EXPECT_EQ(!is_incognito(), FileExists(data_path));
const char data[] = "test data";
const int length = std::size(data) - 1;
base::File file = ofu()->CreateOrOpen(
context.get(), url, base::File::FLAG_WRITE | base::File::FLAG_OPEN);
if (is_incognito()) {
ASSERT_FALSE(file.IsValid());
auto* memory_delegate =
static_cast<ObfuscatedFileUtilMemoryDelegate*>(ofu()->delegate());
ASSERT_EQ(
length,
memory_delegate->WriteFile(
local_path, 0,
base::MakeRefCounted<net::StringIOBuffer>(data).get(), length));
} else {
ASSERT_TRUE(file.IsValid());
ASSERT_EQ(length, file.Write(0, data, length));
file.Close();
}
base::File::Info file_info1;
if (!is_incognito())
EXPECT_EQ(length, GetLocalFileSize(data_path));
EXPECT_EQ(length, GetPathSize(url));
context = NewContext(nullptr);
EXPECT_EQ(base::File::FILE_OK,
ofu()->GetFileInfo(context.get(), url, &file_info1, &data_path));
EXPECT_EQ(data_path, local_path);
EXPECT_FALSE(file_info0.is_directory);
EXPECT_FALSE(file_info1.is_directory);
EXPECT_FALSE(file_info0.is_symbolic_link);
EXPECT_FALSE(file_info1.is_symbolic_link);
EXPECT_EQ(0, file_info0.size);
EXPECT_EQ(length, file_info1.size);
EXPECT_LE(file_info0.last_modified, file_info1.last_modified);
context = NewContext(nullptr);
EXPECT_EQ(base::File::FILE_OK,
ofu()->Truncate(context.get(), url, length * 2));
EXPECT_EQ(length * 2, GetPathSize(url));
context = NewContext(nullptr);
EXPECT_EQ(base::File::FILE_OK, ofu()->Truncate(context.get(), url, 0));
EXPECT_EQ(0, GetPathSize(url));
}
void ValidateTestDirectory(
const FileSystemURL& root_url,
const std::set<base::FilePath::StringType>& files,
const std::set<base::FilePath::StringType>& directories) {
std::unique_ptr<FileSystemOperationContext> context;
for (const auto& file : files) {
bool created = true;
context = NewContext(nullptr);
ASSERT_EQ(
base::File::FILE_OK,
ofu()->EnsureFileExists(
context.get(), FileSystemURLAppend(root_url, file), &created));
ASSERT_FALSE(created);
}
for (const auto& directory : directories) {
context = NewContext(nullptr);
EXPECT_TRUE(DirectoryExists(FileSystemURLAppend(root_url, directory)));
}
}
class UsageVerifyHelper {
public:
UsageVerifyHelper(std::unique_ptr<FileSystemOperationContext> context,
SandboxFileSystemTestHelper* file_system,
int64_t expected_usage,
ObfuscatedFileUtilTest* test)
: context_(std::move(context)),
sandbox_file_system_(file_system),
expected_usage_(expected_usage),
test_(test) {}
~UsageVerifyHelper() {
test_->task_environment_.RunUntilIdle();
Check();
}
FileSystemOperationContext* context() { return context_.get(); }
private:
void Check() {
ASSERT_EQ(expected_usage_, sandbox_file_system_->GetCachedUsage());
}
std::unique_ptr<FileSystemOperationContext> context_;
raw_ptr<SandboxFileSystemTestHelper> sandbox_file_system_;
int64_t expected_usage_;
const raw_ptr<ObfuscatedFileUtilTest> test_;
};
std::unique_ptr<UsageVerifyHelper> AllowUsageIncrease(
int64_t requested_growth) {
int64_t usage = sandbox_file_system_.GetCachedUsage();
return std::make_unique<UsageVerifyHelper>(LimitedContext(requested_growth),
&sandbox_file_system_,
usage + requested_growth, this);
}
std::unique_ptr<UsageVerifyHelper> DisallowUsageIncrease(
int64_t requested_growth) {
int64_t usage = sandbox_file_system_.GetCachedUsage();
return std::make_unique<UsageVerifyHelper>(
LimitedContext(requested_growth - 1), &sandbox_file_system_, usage,
this);
}
void FillTestDirectory(const FileSystemURL& root_url,
std::set<base::FilePath::StringType>* files,
std::set<base::FilePath::StringType>* directories) {
std::unique_ptr<FileSystemOperationContext> context;
std::vector<filesystem::mojom::DirectoryEntry> entries;
EXPECT_EQ(base::File::FILE_OK,
AsyncFileTestHelper::ReadDirectory(file_system_context(),
root_url, &entries));
EXPECT_EQ(0UL, entries.size());
files->clear();
files->insert(FILE_PATH_LITERAL("first"));
files->insert(FILE_PATH_LITERAL("second"));
files->insert(FILE_PATH_LITERAL("third"));
directories->clear();
directories->insert(FILE_PATH_LITERAL("fourth"));
directories->insert(FILE_PATH_LITERAL("fifth"));
directories->insert(FILE_PATH_LITERAL("sixth"));
for (const auto& file : *files) {
bool created = false;
context = NewContext(nullptr);
ASSERT_EQ(
base::File::FILE_OK,
ofu()->EnsureFileExists(
context.get(), FileSystemURLAppend(root_url, file), &created));
ASSERT_TRUE(created);
}
for (const auto& directory : *directories) {
bool exclusive = true;
bool recursive = false;
context = NewContext(nullptr);
EXPECT_EQ(base::File::FILE_OK,
ofu()->CreateDirectory(context.get(),
FileSystemURLAppend(root_url, directory),
exclusive, recursive));
}
ValidateTestDirectory(root_url, *files, *directories);
}
void TestReadDirectoryHelper(const FileSystemURL& root_url) {
std::set<base::FilePath::StringType> files;
std::set<base::FilePath::StringType> directories;
FillTestDirectory(root_url, &files, &directories);
std::unique_ptr<FileSystemOperationContext> context;
std::vector<filesystem::mojom::DirectoryEntry> entries;
context = NewContext(nullptr);
EXPECT_EQ(base::File::FILE_OK,
AsyncFileTestHelper::ReadDirectory(file_system_context(),
root_url, &entries));
EXPECT_EQ(files.size() + directories.size(), entries.size());
EXPECT_TRUE(change_observer()->HasNoChange());
for (const filesystem::mojom::DirectoryEntry& entry : entries) {
auto iter = files.find(entry.name.value());
if (iter != files.end()) {
EXPECT_EQ(entry.type, filesystem::mojom::FsFileType::REGULAR_FILE);
files.erase(iter);
continue;
}
iter = directories.find(entry.name.value());
EXPECT_FALSE(directories.end() == iter);
EXPECT_EQ(entry.type, filesystem::mojom::FsFileType::DIRECTORY);
directories.erase(iter);
}
}
void TestTouchHelper(const FileSystemURL& url, bool is_file) {
base::Time last_access_time = base::Time::Now();
base::Time last_modified_time = base::Time::Now();
std::unique_ptr<FileSystemOperationContext> context = NewContext(nullptr);
EXPECT_EQ(
base::File::FILE_OK,
ofu()->Touch(context.get(), url, last_access_time, last_modified_time));
// Currently we fire no change notifications for Touch.
EXPECT_TRUE(change_observer()->HasNoChange());
base::FilePath local_path;
base::File::Info file_info;
context = NewContext(nullptr);
EXPECT_EQ(base::File::FILE_OK,
ofu()->GetFileInfo(context.get(), url, &file_info, &local_path));
// We compare as time_t here to lower our resolution, to avoid false
// negatives caused by conversion to the local filesystem's native
// representation and back.
EXPECT_EQ(file_info.last_modified.ToTimeT(), last_modified_time.ToTimeT());
context = NewContext(nullptr);
last_modified_time += base::Hours(1);
last_access_time += base::Hours(14);
EXPECT_EQ(
base::File::FILE_OK,
ofu()->Touch(context.get(), url, last_access_time, last_modified_time));
EXPECT_TRUE(change_observer()->HasNoChange());
context = NewContext(nullptr);
EXPECT_EQ(base::File::FILE_OK,
ofu()->GetFileInfo(context.get(), url, &file_info, &local_path));
EXPECT_EQ(file_info.last_modified.ToTimeT(), last_modified_time.ToTimeT());
if (is_file) // Directories in OFU don't support atime.
EXPECT_EQ(file_info.last_accessed.ToTimeT(), last_access_time.ToTimeT());
}
void TestCopyInForeignFileHelper(bool overwrite) {
base::ScopedTempDir source_dir;
ASSERT_TRUE(source_dir.CreateUniqueTempDir());
base::FilePath root_file_path = source_dir.GetPath();
base::FilePath src_file_path = root_file_path.AppendASCII("file_name");
FileSystemURL dest_url = CreateURLFromUTF8("new file");
int64_t src_file_length = 87;
base::File file(src_file_path,
base::File::FLAG_CREATE | base::File::FLAG_WRITE);
ASSERT_TRUE(file.IsValid());
EXPECT_TRUE(file.created());
ASSERT_TRUE(file.SetLength(src_file_length));
file.Close();
std::unique_ptr<FileSystemOperationContext> context;
if (overwrite) {
context = NewContext(nullptr);
bool created = false;
EXPECT_EQ(base::File::FILE_OK,
ofu()->EnsureFileExists(context.get(), dest_url, &created));
EXPECT_TRUE(created);
// We must have observed one (and only one) create_file_count.
EXPECT_EQ(1, change_observer()->get_and_reset_create_file_count());
EXPECT_TRUE(change_observer()->HasNoChange());
}
const int64_t path_cost =
ObfuscatedFileUtil::ComputeFilePathCost(dest_url.path());
if (!overwrite) {
// Verify that file creation requires sufficient quota for the path.
context = NewContext(nullptr);
context->set_allowed_bytes_growth(path_cost + src_file_length - 1);
EXPECT_EQ(
base::File::FILE_ERROR_NO_SPACE,
ofu()->CopyInForeignFile(context.get(), src_file_path, dest_url));
}
context = NewContext(nullptr);
context->set_allowed_bytes_growth(path_cost + src_file_length);
EXPECT_EQ(base::File::FILE_OK,
ofu()->CopyInForeignFile(context.get(), src_file_path, dest_url));
EXPECT_TRUE(PathExists(dest_url));
EXPECT_FALSE(DirectoryExists(dest_url));
context = NewContext(nullptr);
base::File::Info file_info;
base::FilePath data_path;
EXPECT_EQ(base::File::FILE_OK, ofu()->GetFileInfo(context.get(), dest_url,
&file_info, &data_path));
EXPECT_NE(data_path, src_file_path);
EXPECT_TRUE(PathExists(dest_url));
EXPECT_EQ(src_file_length, GetPathSize(dest_url));
EXPECT_EQ(base::File::FILE_OK, ofu()->DeleteFile(context.get(), dest_url));
}
void ClearTimestamp(const FileSystemURL& url) {
std::unique_ptr<FileSystemOperationContext> context = NewContext(nullptr);
EXPECT_EQ(base::File::FILE_OK,
ofu()->Touch(context.get(), url, base::Time(), base::Time()));
EXPECT_EQ(base::Time(), GetModifiedTime(url));
}
base::Time GetModifiedTime(const FileSystemURL& url) {
std::unique_ptr<FileSystemOperationContext> context = NewContext(nullptr);
base::FilePath data_path;
base::File::Info file_info;
context = NewContext(nullptr);
EXPECT_EQ(base::File::FILE_OK,
ofu()->GetFileInfo(context.get(), url, &file_info, &data_path));
EXPECT_TRUE(change_observer()->HasNoChange());
return file_info.last_modified;
}
void TestDirectoryTimestampHelper(const FileSystemURL& base_dir,
bool copy,
bool overwrite) {
std::unique_ptr<FileSystemOperationContext> context;
const FileSystemURL src_dir_url(
FileSystemURLAppendUTF8(base_dir, "foo_dir"));
const FileSystemURL dest_dir_url(
FileSystemURLAppendUTF8(base_dir, "bar_dir"));
const FileSystemURL src_file_url(
FileSystemURLAppendUTF8(src_dir_url, "hoge"));
const FileSystemURL dest_file_url(
FileSystemURLAppendUTF8(dest_dir_url, "fuga"));
context = NewContext(nullptr);
EXPECT_EQ(base::File::FILE_OK,
ofu()->CreateDirectory(context.get(), src_dir_url, true, true));
context = NewContext(nullptr);
EXPECT_EQ(base::File::FILE_OK,
ofu()->CreateDirectory(context.get(), dest_dir_url, true, true));
bool created = false;
context = NewContext(nullptr);
EXPECT_EQ(base::File::FILE_OK,
ofu()->EnsureFileExists(context.get(), src_file_url, &created));
if (overwrite) {
context = NewContext(nullptr);
EXPECT_EQ(
base::File::FILE_OK,
ofu()->EnsureFileExists(context.get(), dest_file_url, &created));
}
ClearTimestamp(src_dir_url);
ClearTimestamp(dest_dir_url);
context = NewContext(nullptr);
EXPECT_EQ(base::File::FILE_OK,
ofu()->CopyOrMoveFile(context.get(), src_file_url, dest_file_url,
FileSystemOperation::CopyOrMoveOptionSet(),
copy));
if (copy)
EXPECT_EQ(base::Time(), GetModifiedTime(src_dir_url));
else
EXPECT_NE(base::Time(), GetModifiedTime(src_dir_url));
EXPECT_NE(base::Time(), GetModifiedTime(dest_dir_url));
}
void MaybeDropDatabasesAliveCaseTestBody() {
std::unique_ptr<ObfuscatedFileUtil> file_util =
CreateObfuscatedFileUtil(/*storage_policy=*/nullptr);
file_util->InitOriginDatabase(url::Origin(), true /*create*/);
ASSERT_TRUE(file_util->origin_database_ != nullptr);
// Callback to Drop DB is called while ObfuscatedFileUtilTest is
// still alive.
file_util->db_flush_delay_seconds_ = 0;
file_util->MarkUsed();
task_environment_.RunUntilIdle();
ASSERT_TRUE(file_util->origin_database_ == nullptr);
}
void MaybeDropDatabasesAlreadyDeletedCaseTestBody() {
// Run message loop after OFU is already deleted to make sure callback
// doesn't cause a crash for use after free.
{
std::unique_ptr<ObfuscatedFileUtil> file_util =
CreateObfuscatedFileUtil(/*storage_policy=*/nullptr);
file_util->InitOriginDatabase(url::Origin(), true /*create*/);
file_util->db_flush_delay_seconds_ = 0;
file_util->MarkUsed();
}
// At this point the callback is still in the message queue but OFU is gone.
task_environment_.RunUntilIdle();
}
void DestroyDirectoryDatabase_IsolatedTestBody() {
storage_policy_->AddIsolated(origin().GetURL());
FileSystemURL url = FileSystemURL::CreateForTest(
storage_key(), kFileSystemTypePersistent, base::FilePath());
if (is_non_default_bucket())
url.SetBucket(custom_bucket_);
// Create DirectoryDatabase for isolated origin.
SandboxDirectoryDatabase* db =
ofu()->GetDirectoryDatabase(url, true /* create */);
ASSERT_TRUE(db != nullptr);
// Destroy it.
ofu()->DestroyDirectoryDatabaseForBucket(
is_non_default_bucket() ? url.bucket().value() : default_bucket_,
url.type());
ASSERT_TRUE(ofu()->directories_.empty());
}
void GetDirectoryDatabase_IsolatedTestBody() {
storage_policy_->AddIsolated(origin().GetURL());
FileSystemURL url = FileSystemURL::CreateForTest(
blink::StorageKey::CreateFirstParty(origin()),
kFileSystemTypePersistent, base::FilePath());
if (is_non_default_bucket())
url.SetBucket(custom_bucket_);
// Create DirectoryDatabase for isolated origin.
SandboxDirectoryDatabase* db =
ofu()->GetDirectoryDatabase(url, true /* create */);
ASSERT_TRUE(db != nullptr);
ASSERT_EQ(1U, ofu()->directories_.size());
// Remove isolated.
storage_policy_->RemoveIsolated(url.origin().GetURL());
// This should still get the same database.
SandboxDirectoryDatabase* db2 =
ofu()->GetDirectoryDatabase(url, false /* create */);
ASSERT_EQ(db, db2);
}
int64_t ComputeCurrentUsage() {
return sandbox_file_system_.ComputeCurrentStorageKeyUsage() -
sandbox_file_system_.ComputeCurrentDirectoryDatabaseUsage();
}
FileSystemContext* file_system_context() {
return sandbox_file_system_.file_system_context();
}
const base::FilePath& data_dir_path() const { return data_dir_.GetPath(); }
void CheckFileSize(FileSystemURL& url,
base::FilePath& local_path,
int64_t expected_size) {
if (!is_incognito())
EXPECT_EQ(expected_size, GetLocalFileSize(local_path));
EXPECT_EQ(expected_size, GetPathSize(url));
}
protected:
base::test::ScopedFeatureList scoped_feature_list_;
std::unique_ptr<leveldb::Env> incognito_leveldb_environment_;
base::test::TaskEnvironment task_environment_;
base::ScopedTempDir data_dir_;
scoped_refptr<MockSpecialStoragePolicy> storage_policy_;
scoped_refptr<QuotaManager> quota_manager_;
scoped_refptr<base::SingleThreadTaskRunner> quota_manager_task_runner_;
scoped_refptr<FileSystemContext> file_system_context_;
blink::StorageKey storage_key_;
BucketLocator default_bucket_;
BucketLocator custom_bucket_;
BucketLocator alternate_custom_bucket_;
FileSystemType type_;
SandboxFileSystemTestHelper sandbox_file_system_;
blink::mojom::QuotaStatusCode quota_status_;
int64_t usage_;
MockFileChangeObserver change_observer_;
ChangeObserverList change_observers_;
base::WeakPtrFactory<ObfuscatedFileUtilTest> weak_factory_{this};
};
INSTANTIATE_TEST_SUITE_P(
All,
ObfuscatedFileUtilTest,
testing::Values(TestMode::kRegularFirstParty,
TestMode::kRegularFirstPartyNonDefaultBucket,
TestMode::kRegularThirdParty,
TestMode::kRegularThirdPartyNonDefaultBucket,
TestMode::kIncognitoFirstParty,
TestMode::kIncognitoFirstPartyNonDefaultBucket,
TestMode::kIncognitoThirdParty,
TestMode::kIncognitoThirdPartyNonDefaultBucket));
TEST_P(ObfuscatedFileUtilTest, TestCreateAndDeleteFile) {
FileSystemURL url = CreateURLFromUTF8("fake/file");
std::unique_ptr<FileSystemOperationContext> context = NewContext(nullptr);
bool created;
base::File::Error result =
ofu()->EnsureFileExists(context.get(), url, &created);
EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND, result);
context = NewContext(nullptr);
EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND,
ofu()->DeleteFile(context.get(), url));
url = CreateURLFromUTF8("test file");
EXPECT_TRUE(change_observer()->HasNoChange());
// Verify that file creation requires sufficient quota for the path.
context = NewContext(nullptr);
context->set_allowed_bytes_growth(
ObfuscatedFileUtil::ComputeFilePathCost(url.path()) - 1);
result = ofu()->EnsureFileExists(context.get(), url, &created);
ASSERT_EQ(base::File::FILE_ERROR_NO_SPACE, result);
context = NewContext(nullptr);
context->set_allowed_bytes_growth(
ObfuscatedFileUtil::ComputeFilePathCost(url.path()));
created = false;
result = ofu()->EnsureFileExists(context.get(), url, &created);
ASSERT_EQ(base::File::FILE_OK, result);
ASSERT_TRUE(created);
EXPECT_EQ(1, change_observer()->get_and_reset_create_file_count());
CheckFile(url);
context = NewContext(nullptr);
base::FilePath local_path;
EXPECT_EQ(base::File::FILE_OK,
ofu()->GetLocalFilePath(context.get(), url, &local_path));
EXPECT_NE(is_incognito(), base::PathExists(local_path));
EXPECT_TRUE(PathExists(url));
// Verify that deleting a file isn't stopped by zero quota, and that it frees
// up quote from its path.
context = NewContext(nullptr);
context->set_allowed_bytes_growth(0);
EXPECT_EQ(base::File::FILE_OK, ofu()->DeleteFile(context.get(), url));
EXPECT_EQ(1, change_observer()->get_and_reset_remove_file_count());
EXPECT_FALSE(PathExists(url));
EXPECT_EQ(ObfuscatedFileUtil::ComputeFilePathCost(url.path()),
context->allowed_bytes_growth());
context = NewContext(nullptr);
bool exclusive = true;
bool recursive = true;
FileSystemURL root_url = CreateURLFromUTF8("series");
FileSystemURL intermediate_url = FileSystemURLAppendUTF8(root_url, "of");
FileSystemURL directory_url =
FileSystemURLAppendUTF8(intermediate_url, "directories");
url = FileSystemURLAppendUTF8(directory_url, "file name");
EXPECT_EQ(base::File::FILE_OK,
ofu()->CreateDirectory(context.get(), directory_url, exclusive,
recursive));
// The operation created 3 directories recursively.
EXPECT_THAT(
change_observer()->get_changed_urls(),
testing::UnorderedElementsAre(root_url, intermediate_url, directory_url));
EXPECT_EQ(3, change_observer()->get_and_reset_create_directory_count());
context = NewContext(nullptr);
created = false;
result = ofu()->EnsureFileExists(context.get(), url, &created);
ASSERT_TRUE(created);
ASSERT_EQ(base::File::FILE_OK, result);
EXPECT_EQ(1, change_observer()->get_and_reset_create_file_count());
CheckFile(url);
context = NewContext(nullptr);
EXPECT_EQ(base::File::FILE_OK,
ofu()->GetLocalFilePath(context.get(), url, &local_path));
EXPECT_NE(is_incognito(), base::PathExists(local_path));
EXPECT_TRUE(PathExists(url));
context = NewContext(nullptr);
EXPECT_EQ(base::File::FILE_OK, ofu()->DeleteFile(context.get(), url));
EXPECT_EQ(1, change_observer()->get_and_reset_remove_file_count());
EXPECT_FALSE(PathExists(url));
// Make sure we have no unexpected changes.
EXPECT_TRUE(change_observer()->HasNoChange());
}
TEST_P(ObfuscatedFileUtilTest, TestTruncate) {
bool created = false;
FileSystemURL url = CreateURLFromUTF8("file");
std::unique_ptr<FileSystemOperationContext> context = NewContext(nullptr);
EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND,
ofu()->Truncate(context.get(), url, 4));
context = NewContext(nullptr);
ASSERT_EQ(base::File::FILE_OK,
ofu()->EnsureFileExists(context.get(), url, &created));
ASSERT_TRUE(created);
EXPECT_EQ(1, change_observer()->get_and_reset_create_file_count());
context = NewContext(nullptr);
base::FilePath local_path;
EXPECT_EQ(base::File::FILE_OK,
ofu()->GetLocalFilePath(context.get(), url, &local_path));
CheckFileSize(url, local_path, 0);
context = NewContext(nullptr);
EXPECT_EQ(base::File::FILE_OK, ofu()->Truncate(context.get(), url, 10));
EXPECT_EQ(1, change_observer()->get_and_reset_modify_file_count());
CheckFileSize(url, local_path, 10);
context = NewContext(nullptr);
EXPECT_EQ(base::File::FILE_OK, ofu()->Truncate(context.get(), url, 1));
EXPECT_EQ(1, change_observer()->get_and_reset_modify_file_count());
CheckFileSize(url, local_path, 1);
EXPECT_FALSE(DirectoryExists(url));
EXPECT_TRUE(PathExists(url));
// Make sure we have no unexpected changes.
EXPECT_TRUE(change_observer()->HasNoChange());
}
TEST_P(ObfuscatedFileUtilTest, TestQuotaOnTruncation) {
bool created = false;
FileSystemURL url = CreateURLFromUTF8("file");
ASSERT_EQ(base::File::FILE_OK,
ofu()->EnsureFileExists(
AllowUsageIncrease(PathCost(url))->context(), url, &created));
ASSERT_TRUE(created);
ASSERT_EQ(0, ComputeTotalFileSize());
ASSERT_EQ(base::File::FILE_OK,
ofu()->Truncate(AllowUsageIncrease(1020)->context(), url, 1020));
ASSERT_EQ(1020, ComputeTotalFileSize());
ASSERT_EQ(base::File::FILE_OK,
ofu()->Truncate(AllowUsageIncrease(-1020)->context(), url, 0));
ASSERT_EQ(0, ComputeTotalFileSize());
EXPECT_EQ(base::File::FILE_ERROR_NO_SPACE,
ofu()->Truncate(DisallowUsageIncrease(1021)->context(), url, 1021));
ASSERT_EQ(0, ComputeTotalFileSize());
EXPECT_EQ(base::File::FILE_OK,
ofu()->Truncate(AllowUsageIncrease(1020)->context(), url, 1020));
ASSERT_EQ(1020, ComputeTotalFileSize());
EXPECT_EQ(base::File::FILE_OK,
ofu()->Truncate(AllowUsageIncrease(0)->context(), url, 1020));
ASSERT_EQ(1020, ComputeTotalFileSize());
// quota exceeded
{
std::unique_ptr<UsageVerifyHelper> helper = AllowUsageIncrease(-1);
helper->context()->set_allowed_bytes_growth(
helper->context()->allowed_bytes_growth() - 1);
EXPECT_EQ(base::File::FILE_OK,
ofu()->Truncate(helper->context(), url, 1019));
ASSERT_EQ(1019, ComputeTotalFileSize());
}
if (!is_incognito()) {
// Delete backing file to make following truncation fail.
base::FilePath local_path;
ASSERT_EQ(
base::File::FILE_OK,
ofu()->GetLocalFilePath(UnlimitedContext().get(), url, &local_path));
ASSERT_FALSE(local_path.empty());
ASSERT_TRUE(base::DeleteFile(local_path));
EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND,
ofu()->Truncate(LimitedContext(1234).get(), url, 1234));
} else {
std::unique_ptr<FileSystemOperationContext> context = NewContext(nullptr);
EXPECT_EQ(base::File::FILE_OK, ofu()->DeleteFile(context.get(), url));
}
ASSERT_EQ(0, ComputeTotalFileSize());
}
TEST_P(ObfuscatedFileUtilTest, TestEnsureFileExists) {
FileSystemURL url = CreateURLFromUTF8("fake/file");
bool created = false;
std::unique_ptr<FileSystemOperationContext> context = NewContext(nullptr);
EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND,
ofu()->EnsureFileExists(context.get(), url, &created));
EXPECT_TRUE(change_observer()->HasNoChange());
// Verify that file creation requires sufficient quota for the path.
context = NewContext(nullptr);
url = CreateURLFromUTF8("test file");
created = false;
context->set_allowed_bytes_growth(
ObfuscatedFileUtil::ComputeFilePathCost(url.path()) - 1);
ASSERT_EQ(base::File::FILE_ERROR_NO_SPACE,
ofu()->EnsureFileExists(context.get(), url, &created));
ASSERT_FALSE(created);
EXPECT_TRUE(change_observer()->HasNoChange());
context = NewContext(nullptr);
context->set_allowed_bytes_growth(
ObfuscatedFileUtil::ComputeFilePathCost(url.path()));
ASSERT_EQ(base::File::FILE_OK,
ofu()->EnsureFileExists(context.get(), url, &created));
ASSERT_TRUE(created);
EXPECT_EQ(1, change_observer()->get_and_reset_create_file_count());
CheckFile(url);
context = NewContext(nullptr);
ASSERT_EQ(base::File::FILE_OK,
ofu()->EnsureFileExists(context.get(), url, &created));
ASSERT_FALSE(created);
EXPECT_TRUE(change_observer()->HasNoChange());
// Also test in a subdirectory.
url = CreateURLFromUTF8("path/to/file.txt");
context = NewContext(nullptr);
bool exclusive = true;
bool recursive = true;
EXPECT_EQ(base::File::FILE_OK,
ofu()->CreateDirectory(context.get(), FileSystemURLDirName(url),
exclusive, recursive));
// 2 directories: path/ and path/to.
EXPECT_EQ(2, change_observer()->get_and_reset_create_directory_count());
context = NewContext(nullptr);
ASSERT_EQ(base::File::FILE_OK,
ofu()->EnsureFileExists(context.get(), url, &created));
ASSERT_TRUE(created);
EXPECT_FALSE(DirectoryExists(url));
EXPECT_TRUE(PathExists(url));
EXPECT_TRUE(change_observer()->HasNoChange());
}
TEST_P(ObfuscatedFileUtilTest, TestDirectoryOps) {
std::unique_ptr<FileSystemOperationContext> context = NewContext(nullptr);
bool exclusive = false;
bool recursive = false;
FileSystemURL url = CreateURLFromUTF8("foo/bar");
EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND,
ofu()->CreateDirectory(context.get(), url, exclusive, recursive));
context = NewContext(nullptr);
EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND,
ofu()->DeleteDirectory(context.get(), url));
FileSystemURL root = CreateURLFromUTF8(std::string());
EXPECT_FALSE(DirectoryExists(url));
EXPECT_FALSE(PathExists(url));
context = NewContext(nullptr);
EXPECT_TRUE(ofu()->IsDirectoryEmpty(context.get(), root));
context = NewContext(nullptr);
exclusive = false;
recursive = true;
EXPECT_EQ(base::File::FILE_OK,
ofu()->CreateDirectory(context.get(), url, exclusive, recursive));
EXPECT_EQ(2, change_observer()->get_and_reset_create_directory_count());
EXPECT_TRUE(DirectoryExists(url));
EXPECT_TRUE(PathExists(url));
context = NewContext(nullptr);
EXPECT_FALSE(ofu()->IsDirectoryEmpty(context.get(), root));
EXPECT_TRUE(DirectoryExists(FileSystemURLDirName(url)));
context = NewContext(nullptr);
EXPECT_FALSE(
ofu()->IsDirectoryEmpty(context.get(), FileSystemURLDirName(url)));
// Can't remove a non-empty directory.
context = NewContext(nullptr);
EXPECT_EQ(base::File::FILE_ERROR_NOT_EMPTY,
ofu()->DeleteDirectory(context.get(), FileSystemURLDirName(url)));
EXPECT_TRUE(change_observer()->HasNoChange());
base::File::Info file_info;
base::FilePath local_path;
EXPECT_EQ(base::File::FILE_OK,
ofu()->GetFileInfo(context.get(), url, &file_info, &local_path));
EXPECT_TRUE(local_path.empty());
EXPECT_TRUE(file_info.is_directory);
EXPECT_FALSE(file_info.is_symbolic_link);
// Same create again should succeed, since exclusive is false.
context = NewContext(nullptr);
EXPECT_EQ(base::File::FILE_OK,
ofu()->CreateDirectory(context.get(), url, exclusive, recursive));
EXPECT_TRUE(change_observer()->HasNoChange());
exclusive = true;
recursive = true;
context = NewContext(nullptr);
EXPECT_EQ(base::File::FILE_ERROR_EXISTS,
ofu()->CreateDirectory(context.get(), url, exclusive, recursive));
EXPECT_TRUE(change_observer()->HasNoChange());
// Verify that deleting a directory isn't stopped by zero quota, and that it
// frees up quota from its path.
context = NewContext(nullptr);
context->set_allowed_bytes_growth(0);
EXPECT_EQ(base::File::FILE_OK, ofu()->DeleteDirectory(context.get(), url));
EXPECT_EQ(1, change_observer()->get_and_reset_remove_directory_count());
EXPECT_EQ(ObfuscatedFileUtil::ComputeFilePathCost(url.path()),
context->allowed_bytes_growth());
url = CreateURLFromUTF8("foo/bop");
EXPECT_FALSE(DirectoryExists(url));
EXPECT_FALSE(PathExists(url));
context = NewContext(nullptr);
EXPECT_TRUE(ofu()->IsDirectoryEmpty(context.get(), url));
EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND,
ofu()->GetFileInfo(context.get(), url, &file_info, &local_path));
// Verify that file creation requires sufficient quota for the path.
exclusive = true;
recursive = false;
context = NewContext(nullptr);
context->set_allowed_bytes_growth(
ObfuscatedFileUtil::ComputeFilePathCost(url.path()) - 1);
EXPECT_EQ(base::File::FILE_ERROR_NO_SPACE,
ofu()->CreateDirectory(context.get(), url, exclusive, recursive));
EXPECT_TRUE(change_observer()->HasNoChange());
context = NewContext(nullptr);
context->set_allowed_bytes_growth(
ObfuscatedFileUtil::ComputeFilePathCost(url.path()));
EXPECT_EQ(base::File::FILE_OK,
ofu()->CreateDirectory(context.get(), url, exclusive, recursive));
EXPECT_EQ(1, change_observer()->get_and_reset_create_directory_count());
EXPECT_TRUE(DirectoryExists(url));
EXPECT_TRUE(PathExists(url));
exclusive = true;
recursive = false;
context = NewContext(nullptr);
EXPECT_EQ(base::File::FILE_ERROR_EXISTS,
ofu()->CreateDirectory(context.get(), url, exclusive, recursive));
EXPECT_TRUE(change_observer()->HasNoChange());
exclusive = true;
recursive = false;
url = CreateURLFromUTF8("foo");
context = NewContext(nullptr);
EXPECT_EQ(base::File::FILE_ERROR_EXISTS,
ofu()->CreateDirectory(context.get(), url, exclusive, recursive));
EXPECT_TRUE(change_observer()->HasNoChange());
url = CreateURLFromUTF8("blah");
EXPECT_FALSE(DirectoryExists(url));
EXPECT_FALSE(PathExists(url));
exclusive = true;
recursive = false;
context = NewContext(nullptr);
EXPECT_EQ(base::File::FILE_OK,
ofu()->CreateDirectory(context.get(), url, exclusive, recursive));
EXPECT_EQ(1, change_observer()->get_and_reset_create_directory_count());
EXPECT_TRUE(DirectoryExists(url));
EXPECT_TRUE(PathExists(url));
exclusive = true;
recursive = false;
context = NewContext(nullptr);
EXPECT_EQ(base::File::FILE_ERROR_EXISTS,
ofu()->CreateDirectory(context.get(), url, exclusive, recursive));
EXPECT_TRUE(change_observer()->HasNoChange());
}
TEST_P(ObfuscatedFileUtilTest, TestReadDirectory) {
std::unique_ptr<FileSystemOperationContext> context = NewContext(nullptr);
bool exclusive = true;
bool recursive = true;
FileSystemURL url = CreateURLFromUTF8("directory/to/use");
EXPECT_EQ(base::File::FILE_OK,
ofu()->CreateDirectory(context.get(), url, exclusive, recursive));
TestReadDirectoryHelper(url);
}
TEST_P(ObfuscatedFileUtilTest, TestReadRootWithSlash) {
TestReadDirectoryHelper(CreateURLFromUTF8(std::string()));
}
TEST_P(ObfuscatedFileUtilTest, TestReadRootWithEmptyString) {
TestReadDirectoryHelper(CreateURLFromUTF8("/"));
}
TEST_P(ObfuscatedFileUtilTest, TestReadDirectoryOnFile) {
FileSystemURL url = CreateURLFromUTF8("file");
std::unique_ptr<FileSystemOperationContext> context = NewContext(nullptr);
bool created = false;
ASSERT_EQ(base::File::FILE_OK,
ofu()->EnsureFileExists(context.get(), url, &created));
ASSERT_TRUE(created);
std::vector<filesystem::mojom::DirectoryEntry> entries;
EXPECT_EQ(
base::File::FILE_ERROR_NOT_A_DIRECTORY,
AsyncFileTestHelper::ReadDirectory(file_system_context(), url, &entries));
EXPECT_TRUE(ofu()->IsDirectoryEmpty(context.get(), url));
}
// TODO(crbug.com/40511450): Remove this test once last_access_time has
// been removed after PPAPI has been deprecated. Fuchsia does not support touch,
// which breaks this test that relies on it. Since PPAPI is being deprecated,
// this test is excluded from the Fuchsia build.
// See https://crbug.com/1077456 for details.
#if !BUILDFLAG(IS_FUCHSIA)
TEST_P(ObfuscatedFileUtilTest, TestTouch) {
FileSystemURL url = CreateURLFromUTF8("file");
std::unique_ptr<FileSystemOperationContext> context = NewContext(nullptr);
base::Time last_access_time = base::Time::Now();
base::Time last_modified_time = base::Time::Now();
// It's not there yet.
EXPECT_EQ(
base::File::FILE_ERROR_NOT_FOUND,
ofu()->Touch(context.get(), url, last_access_time, last_modified_time));
// OK, now create it.
context = NewContext(nullptr);
bool created = false;
ASSERT_EQ(base::File::FILE_OK,
ofu()->EnsureFileExists(context.get(), url, &created));
ASSERT_TRUE(created);
TestTouchHelper(url, true);
// Now test a directory:
context = NewContext(nullptr);
bool exclusive = true;
bool recursive = false;
url = CreateURLFromUTF8("dir");
ASSERT_EQ(base::File::FILE_OK,
ofu()->CreateDirectory(context.get(), url, exclusive, recursive));
TestTouchHelper(url, false);
}
#endif // !BUILDFLAG(IS_FUCHSIA)
TEST_P(ObfuscatedFileUtilTest, TestPathQuotas) {
FileSystemURL url = CreateURLFromUTF8("fake/file");
std::unique_ptr<FileSystemOperationContext> context = NewContext(nullptr);
url = CreateURLFromUTF8("file name");
context->set_allowed_bytes_growth(5);
bool created = false;
EXPECT_EQ(base::File::FILE_ERROR_NO_SPACE,
ofu()->EnsureFileExists(context.get(), url, &created));
EXPECT_FALSE(created);
context->set_allowed_bytes_growth(1024);
EXPECT_EQ(base::File::FILE_OK,
ofu()->EnsureFileExists(context.get(), url, &created));
EXPECT_TRUE(created);
int64_t path_cost = ObfuscatedFileUtil::ComputeFilePathCost(url.path());
EXPECT_EQ(1024 - path_cost, context->allowed_bytes_growth());
context->set_allowed_bytes_growth(1024);
bool exclusive = true;
bool recursive = true;
url = CreateURLFromUTF8("directory/to/use");
path_cost = 0;
for (const auto& component : url.path().GetComponents()) {
path_cost +=
ObfuscatedFileUtil::ComputeFilePathCost(base::FilePath(component));
}
context = NewContext(nullptr);
context->set_allowed_bytes_growth(1024);
EXPECT_EQ(base::File::FILE_OK,
ofu()->CreateDirectory(context.get(), url, exclusive, recursive));
EXPECT_EQ(1024 - path_cost, context->allowed_bytes_growth());
}
TEST_P(ObfuscatedFileUtilTest, TestCopyOrMoveFileNotFound) {
FileSystemURL source_url = CreateURLFromUTF8("path0.txt");
FileSystemURL dest_url = CreateURLFromUTF8("path1.txt");
std::unique_ptr<FileSystemOperationContext> context = NewContext(nullptr);
bool is_copy_not_move = false;
EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND,
ofu()->CopyOrMoveFile(context.get(), source_url, dest_url,
FileSystemOperation::CopyOrMoveOptionSet(),
is_copy_not_move));
EXPECT_TRUE(change_observer()->HasNoChange());
context = NewContext(nullptr);
is_copy_not_move = true;
EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND,
ofu()->CopyOrMoveFile(context.get(), source_url, dest_url,
FileSystemOperation::CopyOrMoveOptionSet(),
is_copy_not_move));
EXPECT_TRUE(change_observer()->HasNoChange());
source_url = CreateURLFromUTF8("dir/dir/file");
bool exclusive = true;
bool recursive = true;
context = NewContext(nullptr);
ASSERT_EQ(
base::File::FILE_OK,
ofu()->CreateDirectory(context.get(), FileSystemURLDirName(source_url),
exclusive, recursive));
EXPECT_EQ(2, change_observer()->get_and_reset_create_directory_count());
is_copy_not_move = false;
EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND,
ofu()->CopyOrMoveFile(context.get(), source_url, dest_url,
FileSystemOperation::CopyOrMoveOptionSet(),
is_copy_not_move));
EXPECT_TRUE(change_observer()->HasNoChange());
context = NewContext(nullptr);
is_copy_not_move = true;
EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND,
ofu()->CopyOrMoveFile(context.get(), source_url, dest_url,
FileSystemOperation::CopyOrMoveOptionSet(),
is_copy_not_move));
EXPECT_TRUE(change_observer()->HasNoChange());
}
TEST_P(ObfuscatedFileUtilTest, TestCopyOrMoveFileSuccess) {
const int64_t kSourceLength = 5;
const int64_t kDestLength = 50;
size_t count = 0u;
for (const auto& test_case : kCopyMoveTestCases) {
SCOPED_TRACE(testing::Message() << "kCopyMoveTestCase " << count++);
SCOPED_TRACE(testing::Message()
<< "\t is_copy_not_move " << test_case.is_copy_not_move);
SCOPED_TRACE(testing::Message()
<< "\t source_path " << test_case.source_path);
SCOPED_TRACE(testing::Message() << "\t dest_path " << test_case.dest_path);
SCOPED_TRACE(testing::Message()
<< "\t cause_overwrite " << test_case.cause_overwrite);
std::unique_ptr<FileSystemOperationContext> context = NewContext(nullptr);
bool exclusive = false;
bool recursive = true;
FileSystemURL source_url = CreateURLFromUTF8(test_case.source_path);
FileSystemURL dest_url = CreateURLFromUTF8(test_case.dest_path);
context = NewContext(nullptr);
ASSERT_EQ(
base::File::FILE_OK,
ofu()->CreateDirectory(context.get(), FileSystemURLDirName(source_url),
exclusive, recursive));
context = NewContext(nullptr);
ASSERT_EQ(
base::File::FILE_OK,
ofu()->CreateDirectory(context.get(), FileSystemURLDirName(dest_url),
exclusive, recursive));
bool created = false;
context = NewContext(nullptr);
ASSERT_EQ(base::File::FILE_OK,
ofu()->EnsureFileExists(context.get(), source_url, &created));
ASSERT_TRUE(created);
context = NewContext(nullptr);
ASSERT_EQ(base::File::FILE_OK,
ofu()->Truncate(context.get(), source_url, kSourceLength));
if (test_case.cause_overwrite) {
context = NewContext(nullptr);
created = false;
ASSERT_EQ(base::File::FILE_OK,
ofu()->EnsureFileExists(context.get(), dest_url, &created));
ASSERT_TRUE(created);
context = NewContext(nullptr);
ASSERT_EQ(base::File::FILE_OK,
ofu()->Truncate(context.get(), dest_url, kDestLength));
}
context = NewContext(nullptr);
EXPECT_EQ(base::File::FILE_OK,
ofu()->CopyOrMoveFile(context.get(), source_url, dest_url,
FileSystemOperation::CopyOrMoveOptionSet(),
test_case.is_copy_not_move));
if (test_case.is_copy_not_move) {
base::File::Info file_info;
base::FilePath local_path;
context = NewContext(nullptr);
EXPECT_EQ(base::File::FILE_OK,
ofu()->GetFileInfo(context.get(), source_url, &file_info,
&local_path));
EXPECT_EQ(kSourceLength, file_info.size);
EXPECT_EQ(base::File::FILE_OK,
ofu()->DeleteFile(context.get(), source_url));
} else {
base::File::Info file_info;
base::FilePath local_path;
context = NewContext(nullptr);
EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND,
ofu()->GetFileInfo(context.get(), source_url, &file_info,
&local_path));
}
base::File::Info file_info;
base::FilePath local_path;
EXPECT_EQ(base::File::FILE_OK, ofu()->GetFileInfo(context.get(), dest_url,
&file_info, &local_path));
EXPECT_EQ(kSourceLength, file_info.size);
EXPECT_EQ(base::File::FILE_OK, ofu()->DeleteFile(context.get(), dest_url));
}
}
TEST_P(ObfuscatedFileUtilTest, TestCopyPathQuotas) {
FileSystemURL src_url = CreateURLFromUTF8("src path");
FileSystemURL dest_url = CreateURLFromUTF8("destination path");
std::unique_ptr<FileSystemOperationContext> context = NewContext(nullptr);
bool created = false;
ASSERT_EQ(base::File::FILE_OK,
ofu()->EnsureFileExists(context.get(), src_url, &created));
bool is_copy = true;
// Copy, no overwrite.
context->set_allowed_bytes_growth(
ObfuscatedFileUtil::ComputeFilePathCost(dest_url.path()) - 1);
EXPECT_EQ(base::File::FILE_ERROR_NO_SPACE,
ofu()->CopyOrMoveFile(context.get(), src_url, dest_url,
FileSystemOperation::CopyOrMoveOptionSet(),
is_copy));
context = NewContext(nullptr);
context->set_allowed_bytes_growth(
ObfuscatedFileUtil::ComputeFilePathCost(dest_url.path()));
EXPECT_EQ(base::File::FILE_OK,
ofu()->CopyOrMoveFile(context.get(), src_url, dest_url,
FileSystemOperation::CopyOrMoveOptionSet(),
is_copy));
// Copy, with overwrite.
context = NewContext(nullptr);
context->set_allowed_bytes_growth(0);
EXPECT_EQ(base::File::FILE_OK,
ofu()->CopyOrMoveFile(context.get(), src_url, dest_url,
FileSystemOperation::CopyOrMoveOptionSet(),
is_copy));
}
TEST_P(ObfuscatedFileUtilTest, TestMovePathQuotasWithRename) {
FileSystemURL src_url = CreateURLFromUTF8("src path");
FileSystemURL dest_url = CreateURLFromUTF8("destination path");
std::unique_ptr<FileSystemOperationContext> context = NewContext(nullptr);
bool created = false;
ASSERT_EQ(base::File::FILE_OK,
ofu()->EnsureFileExists(context.get(), src_url, &created));
bool is_copy = false;
// Move, rename, no overwrite.
context = NewContext(nullptr);
context->set_allowed_bytes_growth(
ObfuscatedFileUtil::ComputeFilePathCost(dest_url.path()) -
ObfuscatedFileUtil::ComputeFilePathCost(src_url.path()) - 1);
EXPECT_EQ(base::File::FILE_ERROR_NO_SPACE,
ofu()->CopyOrMoveFile(context.get(), src_url, dest_url,
FileSystemOperation::CopyOrMoveOptionSet(),
is_copy));
context = NewContext(nullptr);
context->set_allowed_bytes_growth(
ObfuscatedFileUtil::ComputeFilePathCost(dest_url.path()) -
ObfuscatedFileUtil::ComputeFilePathCost(src_url.path()));
EXPECT_EQ(base::File::FILE_OK,
ofu()->CopyOrMoveFile(context.get(), src_url, dest_url,
FileSystemOperation::CopyOrMoveOptionSet(),
is_copy));
context = NewContext(nullptr);
ASSERT_EQ(base::File::FILE_OK,
ofu()->EnsureFileExists(context.get(), src_url, &created));
// Move, rename, with overwrite.
context = NewContext(nullptr);
context->set_allowed_bytes_growth(0);
EXPECT_EQ(base::File::FILE_OK,
ofu()->CopyOrMoveFile(context.get(), src_url, dest_url,
FileSystemOperation::CopyOrMoveOptionSet(),
is_copy));
}
TEST_P(ObfuscatedFileUtilTest, TestMovePathQuotasWithoutRename) {
FileSystemURL src_url = CreateURLFromUTF8("src path");
std::unique_ptr<FileSystemOperationContext> context = NewContext(nullptr);
bool created = false;
ASSERT_EQ(base::File::FILE_OK,
ofu()->EnsureFileExists(context.get(), src_url, &created));
bool exclusive = true;
bool recursive = false;
FileSystemURL dir_url = CreateURLFromUTF8("directory path");
context = NewContext(nullptr);
ASSERT_EQ(base::File::FILE_OK, ofu()->CreateDirectory(context.get(), dir_url,
exclusive, recursive));
FileSystemURL dest_url = FileSystemURLAppend(dir_url, src_url.path().value());
bool is_copy = false;
int64_t allowed_bytes_growth = -1000; // Over quota, this should still work.
// Move, no rename, no overwrite.
context = NewContext(nullptr);
context->set_allowed_bytes_growth(allowed_bytes_growth);
EXPECT_EQ(base::File::FILE_OK,
ofu()->CopyOrMoveFile(context.get(), src_url, dest_url,
FileSystemOperation::CopyOrMoveOptionSet(),
is_copy));
EXPECT_EQ(allowed_bytes_growth, context->allowed_bytes_growth());
// Move, no rename, with overwrite.
context = NewContext(nullptr);
ASSERT_EQ(base::File::FILE_OK,
ofu()->EnsureFileExists(context.get(), src_url, &created));
context = NewContext(nullptr);
context->set_allowed_bytes_growth(allowed_bytes_growth);
EXPECT_EQ(base::File::FILE_OK,
ofu()->CopyOrMoveFile(context.get(), src_url, dest_url,
FileSystemOperation::CopyOrMoveOptionSet(),
is_copy));
EXPECT_EQ(allowed_bytes_growth +
ObfuscatedFileUtil::ComputeFilePathCost(src_url.path()),
context->allowed_bytes_growth());
}
TEST_P(ObfuscatedFileUtilTest, TestCopyInForeignFile) {
TestCopyInForeignFileHelper(false /* overwrite */);
TestCopyInForeignFileHelper(true /* overwrite */);
}
TEST_P(ObfuscatedFileUtilTest, TestEnumerator) {
std::unique_ptr<FileSystemOperationContext> context = NewContext(nullptr);
FileSystemURL src_url = CreateURLFromUTF8("source dir");
bool exclusive = true;
bool recursive = false;
ASSERT_EQ(base::File::FILE_OK, ofu()->CreateDirectory(context.get(), src_url,
exclusive, recursive));
std::set<base::FilePath::StringType> files;
std::set<base::FilePath::StringType> directories;
FillTestDirectory(src_url, &files, &directories);
FileSystemURL dest_url = CreateURLFromUTF8("destination dir");
EXPECT_FALSE(DirectoryExists(dest_url));
ASSERT_EQ(base::File::FILE_OK, AsyncFileTestHelper::Copy(
file_system_context(), src_url, dest_url));
ValidateTestDirectory(dest_url, files, directories);
EXPECT_TRUE(DirectoryExists(src_url));
EXPECT_TRUE(DirectoryExists(dest_url));
recursive = true;
ASSERT_EQ(
base::File::FILE_OK,
AsyncFileTestHelper::Remove(file_system_context(), dest_url, recursive));
EXPECT_FALSE(DirectoryExists(dest_url));
}
TEST_P(ObfuscatedFileUtilTest, TestStorageKeyEnumerator) {
std::unique_ptr<ObfuscatedFileUtil::AbstractStorageKeyEnumerator> enumerator =
ofu()->CreateStorageKeyEnumerator();
// The test helper starts out with a single filesystem.
EXPECT_TRUE(enumerator.get());
// This test is not relevant for third-party or non-default buckets code paths
// because these paths do not add to the OriginDatabase, the structure that
// populates the enumerator being tested. So in a test environment, this
// enumerator should not have any additional StorageKeys to access via Next().
if (is_third_party_context() || is_non_default_bucket()) {
EXPECT_EQ(std::nullopt, enumerator->Next());
return;
}
EXPECT_EQ(storage_key(), enumerator->Next());
ASSERT_TRUE(type() == kFileSystemTypeTemporary);
EXPECT_TRUE(HasFileSystemType(enumerator.get(), kFileSystemTypeTemporary));
EXPECT_FALSE(HasFileSystemType(enumerator.get(), kFileSystemTypePersistent));
EXPECT_FALSE(enumerator->Next());
EXPECT_FALSE(HasFileSystemType(enumerator.get(), kFileSystemTypeTemporary));
EXPECT_FALSE(HasFileSystemType(enumerator.get(), kFileSystemTypePersistent));
std::set<blink::StorageKey> storage_keys_expected;
storage_keys_expected.insert(storage_key());
size_t count = 0u;
for (const auto& record : kOriginEnumerationTestRecords) {
SCOPED_TRACE(testing::Message()
<< "Validating kOriginEnumerationTestRecords " << count++);
blink::StorageKey storage_key =
blink::StorageKey::CreateFromStringForTesting(record.origin_url);
storage_keys_expected.insert(storage_key);
if (record.has_temporary) {
std::unique_ptr<SandboxFileSystemTestHelper> file_system =
NewFileSystem(storage_key, kFileSystemTypeTemporary);
std::unique_ptr<FileSystemOperationContext> context(
NewContext(file_system.get()));
bool created = false;
ASSERT_EQ(
base::File::FILE_OK,
ofu()->EnsureFileExists(
context.get(), file_system->CreateURLFromUTF8("file"), &created));
EXPECT_TRUE(created);
}
if (record.has_persistent) {
std::unique_ptr<SandboxFileSystemTestHelper> file_system =
NewFileSystem(storage_key, kFileSystemTypePersistent);
std::unique_ptr<FileSystemOperationContext> context(
NewContext(file_system.get()));
bool created = false;
ASSERT_EQ(
base::File::FILE_OK,
ofu()->EnsureFileExists(
context.get(), file_system->CreateURLFromUTF8("file"), &created));
EXPECT_TRUE(created);
}
}
enumerator = ofu()->CreateStorageKeyEnumerator();
EXPECT_TRUE(enumerator.get());
std::set<blink::StorageKey> storage_keys_found;
std::optional<blink::StorageKey> enumerator_storage_key;
while ((enumerator_storage_key = enumerator->Next()).has_value()) {
storage_keys_found.insert(enumerator_storage_key.value());
SCOPED_TRACE(testing::Message()
<< "Handling "
<< enumerator_storage_key->origin().Serialize());
bool found = false;
for (const auto& record : kOriginEnumerationTestRecords) {
if (enumerator_storage_key->origin().GetURL() != record.origin_url)
continue;
found = true;
EXPECT_EQ(record.has_temporary,
HasFileSystemType(enumerator.get(), kFileSystemTypeTemporary));
EXPECT_EQ(record.has_persistent,
HasFileSystemType(enumerator.get(), kFileSystemTypePersistent));
}
// Deal with the default filesystem created by the test helper.
if (!found && enumerator_storage_key == storage_key()) {
ASSERT_TRUE(type() == kFileSystemTypeTemporary);
EXPECT_TRUE(
HasFileSystemType(enumerator.get(), kFileSystemTypeTemporary));
EXPECT_FALSE(
HasFileSystemType(enumerator.get(), kFileSystemTypePersistent));
found = true;
}
EXPECT_TRUE(found);
}
std::set<blink::StorageKey> diff;
std::set_symmetric_difference(
storage_keys_expected.begin(), storage_keys_expected.end(),
storage_keys_found.begin(), storage_keys_found.end(),
inserter(diff, diff.begin()));
EXPECT_TRUE(diff.empty());
}
TEST_P(ObfuscatedFileUtilTest, TestRevokeUsageCache) {
std::unique_ptr<FileSystemOperationContext> context = NewContext(nullptr);
int64_t expected_quota = 0;
size_t count = 0u;
for (const auto& test_case : kRegularFileSystemTestCases) {
SCOPED_TRACE(testing::Message() << "Creating kRegularTestCase " << count++);
base::FilePath file_path(test_case.path);
expected_quota += ObfuscatedFileUtil::ComputeFilePathCost(file_path);
if (test_case.is_directory) {
bool exclusive = true;
bool recursive = false;
ASSERT_EQ(base::File::FILE_OK,
ofu()->CreateDirectory(context.get(), CreateURL(file_path),
exclusive, recursive));
} else {
bool created = false;
ASSERT_EQ(base::File::FILE_OK,
ofu()->EnsureFileExists(context.get(), CreateURL(file_path),
&created));
ASSERT_TRUE(created);
ASSERT_EQ(base::File::FILE_OK,
ofu()->Truncate(context.get(), CreateURL(file_path),
test_case.data_file_size));
expected_quota += test_case.data_file_size;
}
}
// Usually raw size in usage cache and the usage returned by QuotaUtil
// should be same.
EXPECT_EQ(expected_quota, SizeInUsageFile());
EXPECT_EQ(expected_quota, SizeByQuotaUtil());
RevokeUsageCache();
EXPECT_EQ(-1, SizeInUsageFile());
EXPECT_EQ(expected_quota, SizeByQuotaUtil());
// This should reconstruct the cache.
GetUsageFromQuotaManager();
EXPECT_EQ(expected_quota, SizeInUsageFile());
EXPECT_EQ(expected_quota, SizeByQuotaUtil());
EXPECT_EQ(expected_quota, usage());
}
TEST_P(ObfuscatedFileUtilTest, TestInconsistency) {
const FileSystemURL kPath1 = CreateURLFromUTF8("hoge");
const FileSystemURL kPath2 = CreateURLFromUTF8("fuga");
std::unique_ptr<FileSystemOperationContext> context;
base::File::Info file_info;
base::FilePath data_path;
bool created = false;
// Create a non-empty file.
context = NewContext(nullptr);
EXPECT_EQ(base::File::FILE_OK,
ofu()->EnsureFileExists(context.get(), kPath1, &created));
EXPECT_TRUE(created);
context = NewContext(nullptr);
EXPECT_EQ(base::File::FILE_OK, ofu()->Truncate(context.get(), kPath1, 10));
context = NewContext(nullptr);
EXPECT_EQ(base::File::FILE_OK,
ofu()->GetFileInfo(context.get(), kPath1, &file_info, &data_path));
EXPECT_EQ(10, file_info.size);
// Destroy database to make inconsistency between database and filesystem.
ofu()->DestroyDirectoryDatabaseForBucket(
is_non_default_bucket() ? custom_bucket_ : default_bucket_, type());
// Try to get file info of broken file.
EXPECT_FALSE(PathExists(kPath1));
context = NewContext(nullptr);
EXPECT_EQ(base::File::FILE_OK,
ofu()->EnsureFileExists(context.get(), kPath1, &created));
EXPECT_TRUE(created);
context = NewContext(nullptr);
EXPECT_EQ(base::File::FILE_OK,
ofu()->GetFileInfo(context.get(), kPath1, &file_info, &data_path));
EXPECT_EQ(0, file_info.size);
// Make another broken file to `kPath2`.
context = NewContext(nullptr);
EXPECT_EQ(base::File::FILE_OK,
ofu()->EnsureFileExists(context.get(), kPath2, &created));
EXPECT_TRUE(created);
// Destroy again.
ofu()->DestroyDirectoryDatabaseForBucket(
is_non_default_bucket() ? custom_bucket_ : default_bucket_, type());
// Repair broken `kPath1`.
context = NewContext(nullptr);
EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND,
ofu()->Touch(context.get(), kPath1, base::Time::Now(),
base::Time::Now()));
EXPECT_EQ(base::File::FILE_OK,
ofu()->EnsureFileExists(context.get(), kPath1, &created));
EXPECT_TRUE(created);
// Copy from sound `kPath1` to broken `kPath2`.
context = NewContext(nullptr);
EXPECT_EQ(base::File::FILE_OK,
ofu()->CopyOrMoveFile(context.get(), kPath1, kPath2,
FileSystemOperation::CopyOrMoveOptionSet(),
true /* copy */));
ofu()->DestroyDirectoryDatabaseForBucket(
is_non_default_bucket() ? custom_bucket_ : default_bucket_, type());
context = NewContext(nullptr);
created = false;
EXPECT_EQ(base::File::FILE_OK,
ofu()->EnsureFileExists(context.get(), kPath1, &created));
EXPECT_TRUE(created);
EXPECT_EQ(0, GetPathSize(kPath1));
}
TEST_P(ObfuscatedFileUtilTest, TestIncompleteDirectoryReading) {
const auto kPath = std::to_array<FileSystemURL>({
CreateURLFromUTF8("foo"),
CreateURLFromUTF8("bar"),
CreateURLFromUTF8("baz"),
});
const FileSystemURL empty_path = CreateURL(base::FilePath());
std::unique_ptr<FileSystemOperationContext> context;
for (const auto& path : kPath) {
bool created = false;
context = NewContext(nullptr);
EXPECT_EQ(base::File::FILE_OK,
ofu()->EnsureFileExists(context.get(), path, &created));
EXPECT_TRUE(created);
}
std::vector<filesystem::mojom::DirectoryEntry> entries;
EXPECT_EQ(base::File::FILE_OK,
AsyncFileTestHelper::ReadDirectory(file_system_context(),
empty_path, &entries));
EXPECT_EQ(3u, entries.size());
EXPECT_EQ(base::File::FILE_OK, ofu()->DeleteFile(context.get(), kPath[0]));
entries.clear();
EXPECT_EQ(base::File::FILE_OK,
AsyncFileTestHelper::ReadDirectory(file_system_context(),
empty_path, &entries));
EXPECT_EQ(std::size(kPath) - 1, entries.size());
}
TEST_P(ObfuscatedFileUtilTest, TestDirectoryTimestampForCreation) {
std::unique_ptr<FileSystemOperationContext> context = NewContext(nullptr);
const FileSystemURL dir_url = CreateURLFromUTF8("foo_dir");
// Create working directory.
EXPECT_EQ(base::File::FILE_OK,
ofu()->CreateDirectory(context.get(), dir_url, false, false));
// EnsureFileExists, create case.
FileSystemURL url(FileSystemURLAppendUTF8(dir_url, "EnsureFileExists_file"));
bool created = false;
ClearTimestamp(dir_url);
context = NewContext(nullptr);
EXPECT_EQ(base::File::FILE_OK,
ofu()->EnsureFileExists(context.get(), url, &created));
EXPECT_TRUE(created);
EXPECT_NE(base::Time(), GetModifiedTime(dir_url));
// non create case.
created = true;
ClearTimestamp(dir_url);
context = NewContext(nullptr);
EXPECT_EQ(base::File::FILE_OK,
ofu()->EnsureFileExists(context.get(), url, &created));
EXPECT_FALSE(created);
EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
// fail case.
url = FileSystemURLAppendUTF8(dir_url, "EnsureFileExists_dir");
context = NewContext(nullptr);
EXPECT_EQ(base::File::FILE_OK,
ofu()->CreateDirectory(context.get(), url, false, false));
ClearTimestamp(dir_url);
context = NewContext(nullptr);
EXPECT_EQ(base::File::FILE_ERROR_NOT_A_FILE,
ofu()->EnsureFileExists(context.get(), url, &created));
EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
// CreateOrOpen, create case.
url = FileSystemURLAppendUTF8(dir_url, "CreateOrOpen_file");
ClearTimestamp(dir_url);
context = NewContext(nullptr);
created = false;
EXPECT_EQ(base::File::FILE_OK,
ofu()->EnsureFileExists(context.get(), url, &created));
EXPECT_TRUE(created);
EXPECT_NE(base::Time(), GetModifiedTime(dir_url));
// open case.
ClearTimestamp(dir_url);
context = NewContext(nullptr);
EXPECT_EQ(base::File::FILE_OK,
ofu()->EnsureFileExists(context.get(), url, &created));
EXPECT_FALSE(created);
EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
// fail case
ClearTimestamp(dir_url);
context = NewContext(nullptr);
EXPECT_EQ(base::File::FILE_OK,
ofu()->EnsureFileExists(context.get(), url, &created));
EXPECT_FALSE(created);
EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
// CreateDirectory, create case.
// Creating CreateDirectory_dir and CreateDirectory_dir/subdir.
url = FileSystemURLAppendUTF8(dir_url, "CreateDirectory_dir");
FileSystemURL subdir_url(FileSystemURLAppendUTF8(url, "subdir"));
ClearTimestamp(dir_url);
context = NewContext(nullptr);
EXPECT_EQ(base::File::FILE_OK,
ofu()->CreateDirectory(context.get(), subdir_url,
true /* exclusive */, true /* recursive */));
EXPECT_NE(base::Time(), GetModifiedTime(dir_url));
// create subdir case.
// Creating CreateDirectory_dir/subdir2.
subdir_url = FileSystemURLAppendUTF8(url, "subdir2");
ClearTimestamp(dir_url);
ClearTimestamp(url);
context = NewContext(nullptr);
EXPECT_EQ(base::File::FILE_OK,
ofu()->CreateDirectory(context.get(), subdir_url,
true /* exclusive */, true /* recursive */));
EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
EXPECT_NE(base::Time(), GetModifiedTime(url));
// fail case.
url = FileSystemURLAppendUTF8(dir_url, "CreateDirectory_dir");
ClearTimestamp(dir_url);
context = NewContext(nullptr);
EXPECT_EQ(base::File::FILE_ERROR_EXISTS,
ofu()->CreateDirectory(context.get(), url, true /* exclusive */,
true /* recursive */));
EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
// CopyInForeignFile, create case.
url = FileSystemURLAppendUTF8(dir_url, "CopyInForeignFile_file");
base::ScopedTempDir foreign_source_dir;
ASSERT_TRUE(foreign_source_dir.CreateUniqueTempDir());
base::FilePath foreign_src_file_path =
foreign_source_dir.GetPath().AppendASCII("file_name");
EXPECT_EQ(base::File::FILE_OK,
NativeFileUtil::EnsureFileExists(foreign_src_file_path, &created));
EXPECT_TRUE(created);
ClearTimestamp(dir_url);
context = NewContext(nullptr);
EXPECT_EQ(
base::File::FILE_OK,
ofu()->CopyInForeignFile(context.get(), foreign_src_file_path, url));
EXPECT_NE(base::Time(), GetModifiedTime(dir_url));
}
TEST_P(ObfuscatedFileUtilTest, TestDirectoryTimestampForDeletion) {
std::unique_ptr<FileSystemOperationContext> context = NewContext(nullptr);
const FileSystemURL dir_url = CreateURLFromUTF8("foo_dir");
// Create working directory.
EXPECT_EQ(base::File::FILE_OK,
ofu()->CreateDirectory(context.get(), dir_url, false, false));
// DeleteFile, delete case.
FileSystemURL url = FileSystemURLAppendUTF8(dir_url, "DeleteFile_file");
bool created = false;
context = NewContext(nullptr);
EXPECT_EQ(base::File::FILE_OK,
ofu()->EnsureFileExists(context.get(), url, &created));
EXPECT_TRUE(created);
ClearTimestamp(dir_url);
context = NewContext(nullptr);
EXPECT_EQ(base::File::FILE_OK, ofu()->DeleteFile(context.get(), url));
EXPECT_NE(base::Time(), GetModifiedTime(dir_url));
// fail case.
ClearTimestamp(dir_url);
context = NewContext(nullptr);
EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND,
ofu()->DeleteFile(context.get(), url));
EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
// DeleteDirectory, fail case.
url = FileSystemURLAppendUTF8(dir_url, "DeleteDirectory_dir");
FileSystemURL file_path(FileSystemURLAppendUTF8(url, "pakeratta"));
context = NewContext(nullptr);
EXPECT_EQ(base::File::FILE_OK,
ofu()->CreateDirectory(context.get(), url, true, true));
created = false;
context = NewContext(nullptr);
EXPECT_EQ(base::File::FILE_OK,
ofu()->EnsureFileExists(context.get(), file_path, &created));
EXPECT_TRUE(created);
ClearTimestamp(dir_url);
context = NewContext(nullptr);
EXPECT_EQ(base::File::FILE_ERROR_NOT_EMPTY,
ofu()->DeleteDirectory(context.get(), url));
EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
// delete case.
context = NewContext(nullptr);
EXPECT_EQ(base::File::FILE_OK, ofu()->DeleteFile(context.get(), file_path));
ClearTimestamp(dir_url);
context = NewContext(nullptr);
EXPECT_EQ(base::File::FILE_OK, ofu()->DeleteDirectory(context.get(), url));
EXPECT_NE(base::Time(), GetModifiedTime(dir_url));
}
TEST_P(ObfuscatedFileUtilTest, TestDirectoryTimestampForCopyAndMove) {
TestDirectoryTimestampHelper(CreateURLFromUTF8("copy overwrite"), true, true);
TestDirectoryTimestampHelper(CreateURLFromUTF8("copy non-overwrite"), true,
false);
TestDirectoryTimestampHelper(CreateURLFromUTF8("move overwrite"), false,
true);
TestDirectoryTimestampHelper(CreateURLFromUTF8("move non-overwrite"), false,
false);
}
TEST_P(ObfuscatedFileUtilTest, TestFileEnumeratorTimestamp) {
FileSystemURL dir = CreateURLFromUTF8("foo");
FileSystemURL url1 = FileSystemURLAppendUTF8(dir, "bar");
FileSystemURL url2 = FileSystemURLAppendUTF8(dir, "baz");
std::unique_ptr<FileSystemOperationContext> context = NewContext(nullptr);
EXPECT_EQ(base::File::FILE_OK,
ofu()->CreateDirectory(context.get(), dir, false, false));
bool created = false;
context = NewContext(nullptr);
EXPECT_EQ(base::File::FILE_OK,
ofu()->EnsureFileExists(context.get(), url1, &created));
EXPECT_TRUE(created);
context = NewContext(nullptr);
EXPECT_EQ(base::File::FILE_OK,
ofu()->CreateDirectory(context.get(), url2, false, false));
base::FilePath file_path;
context = NewContext(nullptr);
EXPECT_EQ(base::File::FILE_OK,
ofu()->GetLocalFilePath(context.get(), url1, &file_path));
EXPECT_FALSE(file_path.empty());
context = NewContext(nullptr);
EXPECT_EQ(base::File::FILE_OK,
ofu()->Touch(context.get(), url1,
base::Time::Now() + base::Hours(1), base::Time()));
context = NewContext(nullptr);
std::unique_ptr<FileSystemFileUtil::AbstractFileEnumerator> file_enum =
ofu()->CreateFileEnumerator(context.get(), dir, false);
int count = 0;
base::FilePath file_path_each;
while (!(file_path_each = file_enum->Next()).empty()) {
context = NewContext(nullptr);
base::File::Info file_info;
FileSystemURL new_url = FileSystemURL::CreateForTest(
dir.storage_key(), dir.mount_type(), file_path_each);
if (dir.bucket().has_value())
new_url.SetBucket(dir.bucket().value());
EXPECT_EQ(base::File::FILE_OK, ofu()->GetFileInfo(context.get(), new_url,
&file_info, &file_path));
EXPECT_EQ(file_info.is_directory, file_enum->IsDirectory());
EXPECT_EQ(file_info.last_modified, file_enum->LastModifiedTime());
EXPECT_EQ(file_info.size, file_enum->Size());
++count;
}
EXPECT_EQ(2, count);
}
TEST_P(ObfuscatedFileUtilTest, TestQuotaOnCopyFile) {
FileSystemURL from_file(CreateURLFromUTF8("fromfile"));
FileSystemURL obstacle_file(CreateURLFromUTF8("obstaclefile"));
FileSystemURL to_file1(CreateURLFromUTF8("tofile1"));
FileSystemURL to_file2(CreateURLFromUTF8("tofile2"));
bool created;
int64_t expected_total_file_size = 0;
ASSERT_EQ(base::File::FILE_OK,
ofu()->EnsureFileExists(
AllowUsageIncrease(PathCost(from_file))->context(), from_file,
&created));
ASSERT_TRUE(created);
ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
ASSERT_EQ(base::File::FILE_OK,
ofu()->EnsureFileExists(
AllowUsageIncrease(PathCost(obstacle_file))->context(),
obstacle_file, &created));
ASSERT_TRUE(created);
ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
int64_t from_file_size = 1020;
expected_total_file_size += from_file_size;
ASSERT_EQ(base::File::FILE_OK,
ofu()->Truncate(AllowUsageIncrease(from_file_size)->context(),
from_file, from_file_size));
ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
int64_t obstacle_file_size = 1;
expected_total_file_size += obstacle_file_size;
ASSERT_EQ(base::File::FILE_OK,
ofu()->Truncate(AllowUsageIncrease(obstacle_file_size)->context(),
obstacle_file, obstacle_file_size));
ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
int64_t to_file1_size = from_file_size;
expected_total_file_size += to_file1_size;
ASSERT_EQ(
base::File::FILE_OK,
ofu()->CopyOrMoveFile(
AllowUsageIncrease(PathCost(to_file1) + to_file1_size)->context(),
from_file, to_file1, FileSystemOperation::CopyOrMoveOptionSet(),
true /* copy */));
ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
ASSERT_EQ(
base::File::FILE_ERROR_NO_SPACE,
ofu()->CopyOrMoveFile(
DisallowUsageIncrease(PathCost(to_file2) + from_file_size)->context(),
from_file, to_file2, FileSystemOperation::CopyOrMoveOptionSet(),
true /* copy */));
ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
int64_t old_obstacle_file_size = obstacle_file_size;
obstacle_file_size = from_file_size;
expected_total_file_size += obstacle_file_size - old_obstacle_file_size;
ASSERT_EQ(base::File::FILE_OK,
ofu()->CopyOrMoveFile(
AllowUsageIncrease(obstacle_file_size - old_obstacle_file_size)
->context(),
from_file, obstacle_file,
FileSystemOperation::CopyOrMoveOptionSet(), true /* copy */));
ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
int64_t old_from_file_size = from_file_size;
from_file_size = old_from_file_size - 1;
expected_total_file_size += from_file_size - old_from_file_size;
ASSERT_EQ(
base::File::FILE_OK,
ofu()->Truncate(
AllowUsageIncrease(from_file_size - old_from_file_size)->context(),
from_file, from_file_size));
ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
// quota exceeded
{
old_obstacle_file_size = obstacle_file_size;
obstacle_file_size = from_file_size;
expected_total_file_size += obstacle_file_size - old_obstacle_file_size;
std::unique_ptr<UsageVerifyHelper> helper =
AllowUsageIncrease(obstacle_file_size - old_obstacle_file_size);
helper->context()->set_allowed_bytes_growth(
helper->context()->allowed_bytes_growth() - 1);
ASSERT_EQ(base::File::FILE_OK,
ofu()->CopyOrMoveFile(helper->context(), from_file, obstacle_file,
FileSystemOperation::CopyOrMoveOptionSet(),
true /* copy */));
ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
}
}
TEST_P(ObfuscatedFileUtilTest, TestQuotaOnMoveFile) {
FileSystemURL from_file(CreateURLFromUTF8("fromfile"));
FileSystemURL obstacle_file(CreateURLFromUTF8("obstaclefile"));
FileSystemURL to_file(CreateURLFromUTF8("tofile"));
bool created;
int64_t expected_total_file_size = 0;
ASSERT_EQ(base::File::FILE_OK,
ofu()->EnsureFileExists(
AllowUsageIncrease(PathCost(from_file))->context(), from_file,
&created));
ASSERT_TRUE(created);
ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
int64_t from_file_size = 1020;
expected_total_file_size += from_file_size;
ASSERT_EQ(base::File::FILE_OK,
ofu()->Truncate(AllowUsageIncrease(from_file_size)->context(),
from_file, from_file_size));
ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
from_file_size = 0;
ASSERT_EQ(base::File::FILE_OK,
ofu()->CopyOrMoveFile(
AllowUsageIncrease(-PathCost(from_file) + PathCost(to_file))
->context(),
from_file, to_file, FileSystemOperation::CopyOrMoveOptionSet(),
false /* move */));
ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
ASSERT_EQ(base::File::FILE_OK,
ofu()->EnsureFileExists(
AllowUsageIncrease(PathCost(from_file))->context(), from_file,
&created));
ASSERT_TRUE(created);
ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
ASSERT_EQ(base::File::FILE_OK,
ofu()->EnsureFileExists(
AllowUsageIncrease(PathCost(obstacle_file))->context(),
obstacle_file, &created));
ASSERT_TRUE(created);
ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
from_file_size = 1020;
expected_total_file_size += from_file_size;
ASSERT_EQ(base::File::FILE_OK,
ofu()->Truncate(AllowUsageIncrease(from_file_size)->context(),
from_file, from_file_size));
ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
int64_t obstacle_file_size = 1;
expected_total_file_size += obstacle_file_size;
ASSERT_EQ(base::File::FILE_OK,
ofu()->Truncate(AllowUsageIncrease(1)->context(), obstacle_file,
obstacle_file_size));
ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
int64_t old_obstacle_file_size = obstacle_file_size;
obstacle_file_size = from_file_size;
from_file_size = 0;
expected_total_file_size -= old_obstacle_file_size;
ASSERT_EQ(
base::File::FILE_OK,
ofu()->CopyOrMoveFile(
AllowUsageIncrease(-old_obstacle_file_size - PathCost(from_file))
->context(),
from_file, obstacle_file, FileSystemOperation::CopyOrMoveOptionSet(),
false /* move */));
ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
ASSERT_EQ(base::File::FILE_OK,
ofu()->EnsureFileExists(
AllowUsageIncrease(PathCost(from_file))->context(), from_file,
&created));
ASSERT_TRUE(created);
ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
from_file_size = 10;
expected_total_file_size += from_file_size;
ASSERT_EQ(base::File::FILE_OK,
ofu()->Truncate(AllowUsageIncrease(from_file_size)->context(),
from_file, from_file_size));
ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
// quota exceeded even after operation
old_obstacle_file_size = obstacle_file_size;
obstacle_file_size = from_file_size;
from_file_size = 0;
expected_total_file_size -= old_obstacle_file_size;
std::unique_ptr<FileSystemOperationContext> context =
LimitedContext(-old_obstacle_file_size - PathCost(from_file) - 1);
ASSERT_EQ(base::File::FILE_OK,
ofu()->CopyOrMoveFile(context.get(), from_file, obstacle_file,
FileSystemOperation::CopyOrMoveOptionSet(),
false /* move */));
ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
context.reset();
}
TEST_P(ObfuscatedFileUtilTest, TestQuotaOnRemove) {
FileSystemURL dir(CreateURLFromUTF8("dir"));
FileSystemURL file(CreateURLFromUTF8("file"));
FileSystemURL dfile1(CreateURLFromUTF8("dir/dfile1"));
FileSystemURL dfile2(CreateURLFromUTF8("dir/dfile2"));
bool created;
ASSERT_EQ(base::File::FILE_OK,
ofu()->EnsureFileExists(
AllowUsageIncrease(PathCost(file))->context(), file, &created));
ASSERT_TRUE(created);
ASSERT_EQ(0, ComputeTotalFileSize());
ASSERT_EQ(base::File::FILE_OK,
ofu()->CreateDirectory(AllowUsageIncrease(PathCost(dir))->context(),
dir, false, false));
ASSERT_EQ(0, ComputeTotalFileSize());
ASSERT_EQ(
base::File::FILE_OK,
ofu()->EnsureFileExists(AllowUsageIncrease(PathCost(dfile1))->context(),
dfile1, &created));
ASSERT_TRUE(created);
ASSERT_EQ(0, ComputeTotalFileSize());
ASSERT_EQ(
base::File::FILE_OK,
ofu()->EnsureFileExists(AllowUsageIncrease(PathCost(dfile2))->context(),
dfile2, &created));
ASSERT_TRUE(created);
ASSERT_EQ(0, ComputeTotalFileSize());
ASSERT_EQ(base::File::FILE_OK,
ofu()->Truncate(AllowUsageIncrease(340)->context(), file, 340));
ASSERT_EQ(340, ComputeTotalFileSize());
ASSERT_EQ(base::File::FILE_OK,
ofu()->Truncate(AllowUsageIncrease(1020)->context(), dfile1, 1020));
ASSERT_EQ(1360, ComputeTotalFileSize());
ASSERT_EQ(base::File::FILE_OK,
ofu()->Truncate(AllowUsageIncrease(120)->context(), dfile2, 120));
ASSERT_EQ(1480, ComputeTotalFileSize());
ASSERT_EQ(base::File::FILE_OK,
ofu()->DeleteFile(
AllowUsageIncrease(-PathCost(file) - 340)->context(), file));
ASSERT_EQ(1140, ComputeTotalFileSize());
ASSERT_EQ(base::File::FILE_OK,
AsyncFileTestHelper::Remove(file_system_context(), dir,
true /* recursive */));
ASSERT_EQ(0, ComputeTotalFileSize());
}
TEST_P(ObfuscatedFileUtilTest, TestQuotaOnOpen) {
FileSystemURL url(CreateURLFromUTF8("file"));
bool created;
// Creating a file.
ASSERT_EQ(base::File::FILE_OK,
ofu()->EnsureFileExists(
AllowUsageIncrease(PathCost(url))->context(), url, &created));
ASSERT_TRUE(created);
ASSERT_EQ(0, ComputeTotalFileSize());
// Opening it, which shouldn't change the usage.
ASSERT_EQ(
base::File::FILE_OK,
ofu()->EnsureFileExists(AllowUsageIncrease(0)->context(), url, &created));
ASSERT_EQ(0, ComputeTotalFileSize());
const int length = 33;
ASSERT_EQ(
base::File::FILE_OK,
ofu()->Truncate(AllowUsageIncrease(length)->context(), url, length));
ASSERT_EQ(length, ComputeTotalFileSize());
// TODO(crbug.com/41444071): After CreateOrOpen is modified to return
// file error instead of file, the in-memory test can proceed through the next
// steps.
if (is_incognito())
return;
// Opening it with CREATE_ALWAYS flag, which should truncate the file size.
base::File file = ofu()->CreateOrOpen(
AllowUsageIncrease(-length)->context(), url,
base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE);
ASSERT_TRUE(file.IsValid());
ASSERT_EQ(0, ComputeTotalFileSize());
file.Close();
// Extending the file again.
ASSERT_EQ(
base::File::FILE_OK,
ofu()->Truncate(AllowUsageIncrease(length)->context(), url, length));
ASSERT_EQ(length, ComputeTotalFileSize());
// Opening it with TRUNCATED flag, which should truncate the file size.
file = ofu()->CreateOrOpen(
AllowUsageIncrease(-length)->context(), url,
base::File::FLAG_OPEN_TRUNCATED | base::File::FLAG_WRITE);
ASSERT_TRUE(file.IsValid());
ASSERT_EQ(0, ComputeTotalFileSize());
file.Close();
}
TEST_P(ObfuscatedFileUtilTest, MaybeDropDatabasesAliveCase) {
MaybeDropDatabasesAliveCaseTestBody();
}
TEST_P(ObfuscatedFileUtilTest, MaybeDropDatabasesAlreadyDeletedCase) {
MaybeDropDatabasesAlreadyDeletedCaseTestBody();
}
TEST_P(ObfuscatedFileUtilTest, DestroyDirectoryDatabase_Isolated) {
DestroyDirectoryDatabase_IsolatedTestBody();
}
TEST_P(ObfuscatedFileUtilTest, GetDirectoryDatabase_Isolated) {
GetDirectoryDatabase_IsolatedTestBody();
}
TEST_P(ObfuscatedFileUtilTest, OpenPathInNonDirectory) {
FileSystemURL url(CreateURLFromUTF8("file"));
FileSystemURL path_in_file(CreateURLFromUTF8("file/file"));
bool created;
ASSERT_EQ(base::File::FILE_OK,
ofu()->EnsureFileExists(UnlimitedContext().get(), url, &created));
ASSERT_TRUE(created);
ASSERT_EQ(base::File::FILE_ERROR_NOT_A_DIRECTORY,
ofu()->EnsureFileExists(UnlimitedContext().get(), path_in_file,
&created));
ASSERT_EQ(
base::File::FILE_ERROR_NOT_A_DIRECTORY,
ofu()->CreateDirectory(UnlimitedContext().get(), path_in_file,
false /* exclusive */, false /* recursive */));
}
TEST_P(ObfuscatedFileUtilTest, CreateDirectory_NotADirectoryInRecursive) {
FileSystemURL file(CreateURLFromUTF8("file"));
FileSystemURL path_in_file(CreateURLFromUTF8("file/child"));
FileSystemURL path_in_file_in_file(
CreateURLFromUTF8("file/child/grandchild"));
bool created;
ASSERT_EQ(base::File::FILE_OK,
ofu()->EnsureFileExists(UnlimitedContext().get(), file, &created));
ASSERT_TRUE(created);
ASSERT_EQ(
base::File::FILE_ERROR_NOT_A_DIRECTORY,
ofu()->CreateDirectory(UnlimitedContext().get(), path_in_file,
false /* exclusive */, true /* recursive */));
ASSERT_EQ(
base::File::FILE_ERROR_NOT_A_DIRECTORY,
ofu()->CreateDirectory(UnlimitedContext().get(), path_in_file_in_file,
false /* exclusive */, true /* recursive */));
}
TEST_P(ObfuscatedFileUtilTest, DeleteDirectoryForBucketAndType) {
// Create directories.
std::unique_ptr<SandboxFileSystemTestHelper> fs1 =
NewFileSystem(default_bucket_, kFileSystemTypeTemporary);
std::unique_ptr<SandboxFileSystemTestHelper> fs2 =
NewFileSystem(default_bucket_, kFileSystemTypePersistent);
std::unique_ptr<SandboxFileSystemTestHelper> fs3 =
NewFileSystem(custom_bucket_, kFileSystemTypeTemporary);
std::unique_ptr<SandboxFileSystemTestHelper> fs4 =
NewFileSystem(custom_bucket_, kFileSystemTypePersistent);
// Make sure directories for default_bucket_ exist.
ASSERT_TRUE(ofu()
->GetDirectoryForBucketAndType(default_bucket_,
kFileSystemTypeTemporary,
/*create=*/false)
.has_value());
ASSERT_TRUE(ofu()
->GetDirectoryForBucketAndType(default_bucket_,
kFileSystemTypePersistent,
/*create=*/false)
.has_value());
// Make sure directories for custom_bucket_ exist.
ASSERT_TRUE(ofu()
->GetDirectoryForBucketAndType(custom_bucket_,
kFileSystemTypeTemporary,
/*create=*/false)
.has_value());
ASSERT_TRUE(ofu()
->GetDirectoryForBucketAndType(custom_bucket_,
kFileSystemTypePersistent,
/*create=*/false)
.has_value());
// Delete a directory for default_bucket_'s persistent filesystem.
ASSERT_TRUE(ofu()->DeleteDirectoryForBucketAndType(
default_bucket_, kFileSystemTypePersistent));
// The directory for default_bucket_'s temporary filesystem should not be
// removed.
ASSERT_TRUE(ofu()
->GetDirectoryForBucketAndType(default_bucket_,
kFileSystemTypeTemporary,
/*create=*/false)
.has_value());
// The directory for default_bucket_'s persistent filesystem should be
// removed.
ASSERT_THAT(ofu()->GetDirectoryForBucketAndType(default_bucket_,
kFileSystemTypePersistent,
/*create=*/false),
base::test::ErrorIs(base::File::FILE_ERROR_NOT_FOUND));
// The directories for custom_bucket_ should not be removed.
ASSERT_TRUE(ofu()
->GetDirectoryForBucketAndType(custom_bucket_,
kFileSystemTypeTemporary,
/*create=*/false)
.has_value());
ASSERT_TRUE(ofu()
->GetDirectoryForBucketAndType(custom_bucket_,
kFileSystemTypePersistent,
/*create=*/false)
.has_value());
// Deleting directories which don't exist is not an error.
ASSERT_TRUE(ofu()->DeleteDirectoryForBucketAndType(
alternate_custom_bucket_, kFileSystemTypePersistent));
}
TEST_P(ObfuscatedFileUtilTest, DeleteDirectoryForBucketAndType_DeleteAll) {
// Create origin directories.
std::unique_ptr<SandboxFileSystemTestHelper> fs1 =
NewFileSystem(default_bucket_, kFileSystemTypeTemporary);
std::unique_ptr<SandboxFileSystemTestHelper> fs2 =
NewFileSystem(default_bucket_, kFileSystemTypePersistent);
std::unique_ptr<SandboxFileSystemTestHelper> fs3 =
NewFileSystem(custom_bucket_, kFileSystemTypeTemporary);
std::unique_ptr<SandboxFileSystemTestHelper> fs4 =
NewFileSystem(custom_bucket_, kFileSystemTypePersistent);
// Make sure directories for default_bucket_ exist.
ASSERT_TRUE(ofu()
->GetDirectoryForBucketAndType(default_bucket_,
kFileSystemTypeTemporary,
/*create=*/false)
.has_value());
ASSERT_TRUE(ofu()
->GetDirectoryForBucketAndType(default_bucket_,
kFileSystemTypePersistent,
/*create=*/false)
.has_value());
// Make sure directories for custom_bucket_ exist.
ASSERT_TRUE(ofu()
->GetDirectoryForBucketAndType(custom_bucket_,
kFileSystemTypeTemporary,
/*create=*/false)
.has_value());
ASSERT_TRUE(ofu()
->GetDirectoryForBucketAndType(custom_bucket_,
kFileSystemTypePersistent,
/*create=*/false)
.has_value());
// Delete all directories for default_bucket_.
ofu()->DeleteDirectoryForBucketAndType(default_bucket_, std::nullopt);
// The directories for default_bucket_ should be removed.
ASSERT_THAT(ofu()->GetDirectoryForBucketAndType(default_bucket_,
kFileSystemTypeTemporary,
/*create=*/false),
base::test::ErrorIs(base::File::FILE_ERROR_NOT_FOUND));
ASSERT_THAT(ofu()->GetDirectoryForBucketAndType(default_bucket_,
kFileSystemTypePersistent,
/*create=*/false),
base::test::ErrorIs(base::File::FILE_ERROR_NOT_FOUND));
// The directories for custom_bucket_ should not be removed.
ASSERT_TRUE(ofu()
->GetDirectoryForBucketAndType(custom_bucket_,
kFileSystemTypeTemporary,
/*create=*/false)
.has_value());
ASSERT_TRUE(ofu()
->GetDirectoryForBucketAndType(custom_bucket_,
kFileSystemTypePersistent,
/*create=*/false)
.has_value());
}
} // namespace storage
|