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
|
/**
* (c) 2019 by Mega Limited, Wellsford, New Zealand
*
* This file is part of the MEGA SDK - Client Access Engine.
*
* Applications using the MEGA API must present a valid application key
* and comply with the the rules set forth in the Terms of Service.
*
* The MEGA SDK is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* @copyright Simplified (2-clause) BSD License.
*
* You should have received a copy of the license along with this
* program.
*/
#include "megafs.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <mega/base64.h>
#include <mega/db.h>
#include <mega/db/sqlite.h>
#include <mega/filesystem.h>
#include <mega/json.h>
#include <mega/process.h>
#include <mega/scoped_helpers.h>
#include <mega/utils.h>
#include <algorithm>
#include <vector>
TEST(utils, readLines)
{
static const std::string input =
"\r"
"\n"
" \r"
" a\r\n"
"b\n"
"c\r"
" d \r"
" \n"
"efg\n";
static const std::vector<std::string> expected = {
" a",
"b",
"c",
" d ",
"efg"
};
std::vector<std::string> output;
ASSERT_TRUE(::mega::readLines(input, output));
ASSERT_EQ(output.size(), expected.size());
ASSERT_TRUE(std::equal(expected.begin(), expected.end(), output.begin()));
}
TEST(Filesystem, EscapesControlCharactersIfNecessary)
{
using namespace mega;
FSACCESS_CLASS fsAccess;
// Cloud should never receive unescaped control characters.
// If it does, make sure we escape accordingly.
const string input("\0\r\n", 3);
// Most restrictive escaping policy.
{
string name = input;
fsAccess.escapefsincompatible(&name, FS_UNKNOWN);
ASSERT_EQ(name, "%00%0d%0a");
}
// Least restrictive escaping policy.
{
string name = input;
fsAccess.escapefsincompatible(&name, FS_EXT);
ASSERT_EQ(name, "%00\r\n");
}
}
TEST(Filesystem, EscapesReservedCharacters)
{
using namespace mega;
// All of these characters will be escaped.
string name = "\\/:?\"<>|*"; // not % anymore (for now)
// Generate expected result.
ostringstream osstream;
for (auto character : name)
{
osstream << "%"
<< std::hex
<< std::setfill('0')
<< std::setw(2)
<< +character;
}
// Use most restrictive escaping policy.
FSACCESS_CLASS fsAccess;
fsAccess.escapefsincompatible(&name, FS_UNKNOWN);
// Was the string correctly escaped?
ASSERT_EQ(name, osstream.str());
}
TEST(Filesystem, UnescapesEscapedCharacters)
{
using namespace mega;
FSACCESS_CLASS fsAccess;
// All of these characters will be escaped.
string name = "%\\/:?\"<>|*";
fsAccess.escapefsincompatible(&name, FS_UNKNOWN);
// Everything will be unescaped except for control characters.
fsAccess.unescapefsincompatible(&name);
// Was the string correctly unescaped?
ASSERT_STREQ(name.c_str(), "%\\/:?\"<>|*");
}
TEST(CharacterSet, IterateUtf8)
{
using ::mega::unicodeCodepointIterator;
// Single code-unit.
{
auto it = unicodeCodepointIterator("abc");
EXPECT_FALSE(it.end());
EXPECT_EQ(it.get(), 'a');
EXPECT_EQ(it.get(), 'b');
EXPECT_EQ(it.get(), 'c');
EXPECT_TRUE(it.end());
EXPECT_EQ(it.get(), '\0');
}
// Multiple code-unit.
{
auto it = unicodeCodepointIterator("q\xf0\x90\x80\x80r");
EXPECT_FALSE(it.end());
EXPECT_EQ(it.get(), 'q');
EXPECT_EQ(it.get(), 0x10000);
EXPECT_EQ(it.get(), 'r');
EXPECT_TRUE(it.end());
EXPECT_EQ(it.get(), '\0');
}
}
TEST(CharacterSet, IterateUtf16)
{
using mega::unicodeCodepointIterator;
// Single code-unit.
{
auto it = unicodeCodepointIterator(L"abc");
EXPECT_FALSE(it.end());
EXPECT_EQ(it.get(), L'a');
EXPECT_EQ(it.get(), L'b');
EXPECT_EQ(it.get(), L'c');
EXPECT_TRUE(it.end());
EXPECT_EQ(it.get(), L'\0');
}
// Multiple code-unit.
{
auto it = unicodeCodepointIterator(L"q\xd800\xdc00r");
EXPECT_FALSE(it.end());
EXPECT_EQ(it.get(), L'q');
EXPECT_EQ(it.get(), 0x10000);
EXPECT_EQ(it.get(), L'r');
EXPECT_TRUE(it.end());
EXPECT_EQ(it.get(), L'\0');
}
}
using namespace mega;
using namespace std;
// Disambiguate between Microsoft's FileSystemType.
using ::mega::FileSystemType;
class ComparatorTest
: public ::testing::Test
{
public:
template<typename T, typename U>
int compare(const T& lhs, const U& rhs) const
{
return compareUtf(lhs, true, rhs, true, false);
}
template<typename T, typename U>
int ciCompare(const T& lhs, const U& rhs) const
{
return compareUtf(lhs, true, rhs, true, true);
}
LocalPath fromAbsPath(const string& s)
{
return LocalPath::fromAbsolutePath(s);
}
LocalPath fromRelPath(const string& s)
{
return LocalPath::fromRelativePath(s);
}
}; // ComparatorTest
TEST_F(ComparatorTest, CompareLocalPaths)
{
LocalPath lhs;
LocalPath rhs;
// Case insensitive
{
// Make sure basic characters are uppercased.
lhs = fromRelPath("abc");
rhs = fromRelPath("ABC");
EXPECT_EQ(ciCompare(lhs, rhs), 0);
EXPECT_EQ(ciCompare(rhs, lhs), 0);
// Make sure comparison invariants are not violated.
lhs = fromRelPath("abc");
rhs = fromRelPath("ABCD");
EXPECT_LT(ciCompare(lhs, rhs), 0);
EXPECT_GT(ciCompare(rhs, lhs), 0);
// Make sure escapes are decoded.
lhs = fromRelPath("a%30b");
rhs = fromRelPath("A0B");
EXPECT_EQ(ciCompare(lhs, rhs), 0);
EXPECT_EQ(ciCompare(rhs, lhs), 0);
// Make sure decoded characters are uppercased.
lhs = fromRelPath("%61%62%63");
rhs = fromRelPath("ABC");
EXPECT_EQ(ciCompare(lhs, rhs), 0);
EXPECT_EQ(ciCompare(rhs, lhs), 0);
// Invalid escapes are left as-is.
lhs = fromRelPath("a%qb%");
rhs = fromRelPath("A%qB%");
EXPECT_EQ(ciCompare(lhs, rhs), 0);
EXPECT_EQ(ciCompare(rhs, lhs), 0);
}
// Case sensitive
{
// Basic comparison.
lhs = fromRelPath("abc");
EXPECT_EQ(compare(lhs, lhs), 0);
// Make sure characters are not uppercased.
rhs = fromRelPath("ABC");
EXPECT_NE(compare(lhs, rhs), 0);
EXPECT_NE(compare(rhs, lhs), 0);
// Make sure comparison invariants are not violated.
lhs = fromRelPath("abc");
rhs = fromRelPath("abcd");
EXPECT_LT(compare(lhs, rhs), 0);
EXPECT_GT(compare(rhs, lhs), 0);
// Make sure escapes are decoded.
lhs = fromRelPath("a%30b");
rhs = fromRelPath("a0b");
EXPECT_EQ(compare(lhs, rhs), 0);
EXPECT_EQ(compare(rhs, lhs), 0);
// Invalid escapes are left as-is.
lhs = fromRelPath("a%qb%");
EXPECT_EQ(compare(lhs, lhs), 0);
#ifdef _WIN32
// Non-UNC prefixes should be skipped.
lhs = fromAbsPath("\\\\?\\C:\\");
rhs = fromAbsPath("C:\\");
EXPECT_EQ(compare(lhs, rhs), 0);
EXPECT_EQ(compare(rhs, lhs), 0);
lhs = fromAbsPath("\\\\.\\C:\\");
rhs = fromAbsPath("C:\\");
EXPECT_EQ(compare(lhs, rhs), 0);
EXPECT_EQ(compare(rhs, lhs), 0);
//// Prefixes should only be removed from absolute paths.
//lhs = fromAbsPath("\\\\?\\X");
//rhs = fromAbsPath("X");
//EXPECT_NE(compare(lhs, rhs), 0);
//EXPECT_NE(compare(rhs, lhs), 0);
#endif // _WIN32
}
// Filesystem-specific
{
lhs = fromRelPath("a\7%30b%31c");
rhs = fromRelPath("A%070B1C");
}
}
TEST_F(ComparatorTest, CompareLocalPathAgainstString)
{
LocalPath lhs;
string rhs;
// Case insensitive
{
// Simple comparison.
lhs = fromRelPath("abc");
rhs = "ABC";
EXPECT_EQ(ciCompare(lhs, rhs), 0);
// Invariants.
lhs = fromRelPath("abc");
rhs = "abcd";
EXPECT_LT(ciCompare(lhs, rhs), 0);
lhs = fromRelPath("abcd");
rhs = "abc";
EXPECT_GT(ciCompare(lhs, rhs), 0);
// All local escapes are decoded.
lhs = fromRelPath("a%30b%31c");
rhs = "A0b1C";
EXPECT_EQ(ciCompare(lhs, rhs), 0);
// Escapes are uppercased.
lhs = fromRelPath("%61%62%63");
rhs = "ABC";
EXPECT_EQ(ciCompare(lhs, rhs), 0);
// Invalid escapes are left as-is.
lhs = fromRelPath("a%qb%");
rhs = "A%QB%";
EXPECT_EQ(ciCompare(lhs, rhs), 0);
}
// Case sensitive
{
// Simple comparison.
lhs = fromRelPath("abc");
rhs = "abc";
EXPECT_EQ(compare(lhs, rhs), 0);
// Invariants.
rhs = "abcd";
EXPECT_LT(compare(lhs, rhs), 0);
lhs = fromRelPath("abcd");
rhs = "abc";
EXPECT_GT(compare(lhs, rhs), 0);
// All local escapes are decoded.
lhs = fromRelPath("a%30b%31c");
rhs = "a0b1c";
EXPECT_EQ(compare(lhs, rhs), 0);
// Invalid escapes left as-is.
lhs = fromRelPath("a%qb%r");
rhs = "a%qb%r";
EXPECT_EQ(compare(lhs, rhs), 0);
#ifdef _WIN32
// Non-UNC prefixes should be skipped.
lhs = fromAbsPath("\\\\?\\C:\\");
rhs = "C:\\";
EXPECT_EQ(compare(lhs, rhs), 0);
EXPECT_EQ(compare(rhs, lhs), 0);
lhs = fromAbsPath("\\\\.\\C:\\");
rhs = "C:\\";
EXPECT_EQ(compare(lhs, rhs), 0);
EXPECT_EQ(compare(rhs, lhs), 0);
//// Prefixes should only be removed from absolute paths.
//lhs = fromAbsPath("\\\\?\\X");
//rhs = "X";
//EXPECT_NE(compare(lhs, rhs), 0);
//EXPECT_NE(compare(rhs, lhs), 0);
#endif // _WIN32
}
// Filesystem-specific
{
lhs = fromRelPath("a\7%30b%31c");
rhs = "A%070B1C";
}
}
TEST(Conversion, HexVal)
{
// Decimal [0-9]
for (int i = 0x30; i < 0x3a; ++i)
{
EXPECT_EQ(hexval(i), i - 0x30);
}
// Lowercase hexadecimal [a-f]
for (int i = 0x41; i < 0x47; ++i)
{
EXPECT_EQ(hexval(i), i - 0x37);
}
// Uppercase hexadeimcal [A-F]
for (int i = 0x61; i < 0x67; ++i)
{
EXPECT_EQ(hexval(i), i - 0x57);
}
}
TEST(URLCodec, Escape)
{
string input = "abc123!@#$%^&*()";
string output;
URLCodec::escape(&input, &output);
EXPECT_EQ(output, "abc123%21%40%23%24%25%5e%26%2a%28%29");
string input2 = "EF字幕组 编织记忆 stitchers S02E10.mp4";
string output2;
URLCodec::escape(&input2, &output2);
EXPECT_EQ(output2, "EF%e5%ad%97%e5%b9%95%e7%bb%84%20%e7%bc%96%e7%bb%87%e8%ae%b0%e5%bf%86%20stitchers%20S02E10.mp4");
}
TEST(URLCodec, Unescape)
{
string input = "a%4a%4Bc";
string output;
URLCodec::unescape(&input, &output);
EXPECT_EQ(output, "aJKc");
}
TEST(URLCodec, UnescapeInvalidEscape)
{
string input;
string output;
// First character is invalid.
input = "a%qbc";
URLCodec::unescape(&input, &output);
EXPECT_EQ(output, "a%qbc");
// Second character is invalid.
input = "a%bqc";
URLCodec::unescape(&input, &output);
EXPECT_EQ(output, "a%bqc");
}
TEST(URLCodec, UnescapeShortEscape)
{
string input;
string output;
// No hex digits.
input = "a%";
URLCodec::unescape(&input, &output);
EXPECT_EQ(output, "a%");
// Single hex digit.
input = "a%a";
URLCodec::unescape(&input, &output);
EXPECT_EQ(output, "a%a");
}
TEST(Filesystem, isContainingPathOf)
{
using namespace mega;
#ifdef _WIN32
#define SEP "\\"
#else // _WIN32
#define SEP "/"
#endif // ! _WIN32
LocalPath lhs;
LocalPath rhs;
size_t pos;
// lhs does not contain rhs.
constexpr const size_t sentinel = std::numeric_limits<size_t>::max();
pos = sentinel;
lhs = LocalPath::fromRelativePath("a" SEP "b");
rhs = LocalPath::fromRelativePath("a" SEP "c");
EXPECT_FALSE(lhs.isContainingPathOf(rhs, &pos));
EXPECT_EQ(pos, sentinel);
// lhs does not contain rhs.
// they do, however, share a common prefix.
pos = sentinel;
lhs = LocalPath::fromRelativePath("a");
rhs = LocalPath::fromRelativePath("ab");
EXPECT_FALSE(lhs.isContainingPathOf(rhs, &pos));
EXPECT_EQ(pos, sentinel);
// lhs contains rhs.
// no trailing separator.
pos = sentinel;
lhs = LocalPath::fromRelativePath("a");
rhs = LocalPath::fromRelativePath("a" SEP "b");
EXPECT_TRUE(lhs.isContainingPathOf(rhs, &pos));
EXPECT_EQ(pos, 2u);
// trailing separator.
pos = sentinel;
lhs = LocalPath::fromRelativePath("a" SEP);
rhs = LocalPath::fromRelativePath("a" SEP "b");
EXPECT_TRUE(lhs.isContainingPathOf(rhs, &pos));
EXPECT_EQ(pos, 2u);
// lhs contains itself.
pos = sentinel;
lhs = LocalPath::fromRelativePath("a" SEP "b");
EXPECT_TRUE(lhs.isContainingPathOf(lhs, &pos));
EXPECT_EQ(pos, 3u);
#ifdef _WIN32
// case insensitive.
pos = sentinel;
lhs = LocalPath::fromRelativePath("a" SEP "B");
rhs = LocalPath::fromRelativePath("A" SEP "b");
EXPECT_TRUE(lhs.isContainingPathOf(rhs, &pos));
EXPECT_EQ(pos, 3u);
#endif // _WIN32
#undef SEP
}
class SqliteDBTest
: public ::testing::Test
{
public:
SqliteDBTest()
: Test()
, fsAccess()
, name("test")
, rng()
, rootPath(LocalPath::fromAbsolutePath("."))
{
// Get the current path.
bool result = fsAccess.cwd(rootPath);
if (!result)
assert(result);
// Create temporary DB root path.
rootPath.appendWithSeparator(
LocalPath::fromRelativePath("db"), false);
// Make sure our root path is clear.
fsAccess.emptydirlocal(rootPath);
fsAccess.rmdirlocal(rootPath);
// Create root path.
result = fsAccess.mkdirlocal(rootPath, false, true);
if (!result)
assert(result);
}
~SqliteDBTest()
{
// Remove temporary root path.
fsAccess.emptydirlocal(rootPath);
bool result = fsAccess.rmdirlocal(rootPath);
if (!result)
assert(result);
}
FSACCESS_CLASS fsAccess;
string name;
PrnGen rng;
LocalPath rootPath;
}; // SqliteDBTest
TEST_F(SqliteDBTest, CreateCurrent)
{
SqliteDbAccess dbAccess(rootPath);
// Assume databases are in legacy format until proven otherwise.
EXPECT_EQ(dbAccess.currentDbVersion, DbAccess::LEGACY_DB_VERSION);
// Create a new database.
DbTablePtr dbTable(dbAccess.openTableWithNodes(rng, fsAccess, name, 0, nullptr));
// Was the database created successfully?
ASSERT_TRUE(!!dbTable);
// New databases should not be in the legacy format.
EXPECT_EQ(dbAccess.currentDbVersion, DbAccess::DB_VERSION);
}
TEST_F(SqliteDBTest, OpenCurrent)
{
// Create a dummy database.
{
SqliteDbAccess dbAccess(rootPath);
EXPECT_EQ(dbAccess.currentDbVersion, DbAccess::LEGACY_DB_VERSION);
DbTablePtr dbTable(dbAccess.openTableWithNodes(rng, fsAccess, name, 0, nullptr));
ASSERT_TRUE(!!dbTable);
EXPECT_EQ(dbAccess.currentDbVersion, DbAccess::DB_VERSION);
}
// Open the database.
SqliteDbAccess dbAccess(rootPath);
EXPECT_EQ(dbAccess.currentDbVersion, DbAccess::LEGACY_DB_VERSION);
DbTablePtr dbTable(dbAccess.openTableWithNodes(rng, fsAccess, name, 0, nullptr));
EXPECT_TRUE(!!dbTable);
EXPECT_EQ(dbAccess.currentDbVersion, DbAccess::DB_VERSION);
}
TEST_F(SqliteDBTest, ProbeCurrent)
{
SqliteDbAccess dbAccess(rootPath);
// Create dummy database.
{
auto dbFile =
dbAccess.databasePath(fsAccess,
name,
DbAccess::DB_VERSION);
auto fileAccess = fsAccess.newfileaccess(false);
EXPECT_TRUE(fileAccess->fopen(dbFile, false, true, FSLogging::logOnError));
}
EXPECT_TRUE(dbAccess.probe(fsAccess, name));
}
TEST_F(SqliteDBTest, ProbeLegacy)
{
SqliteDbAccess dbAccess(rootPath);
// Create dummy database.
{
auto dbFile =
dbAccess.databasePath(fsAccess,
name,
DbAccess::LEGACY_DB_VERSION);
auto fileAccess = fsAccess.newfileaccess(false);
EXPECT_TRUE(fileAccess->fopen(dbFile, false, true, FSLogging::logOnError));
}
EXPECT_TRUE(dbAccess.probe(fsAccess, name));
}
TEST_F(SqliteDBTest, ProbeNone)
{
SqliteDbAccess dbAccess(rootPath);
EXPECT_FALSE(dbAccess.probe(fsAccess, name));
}
TEST_F(SqliteDBTest, RootPath)
{
SqliteDbAccess dbAccess(rootPath);
EXPECT_EQ(dbAccess.rootPath(), rootPath);
}
#ifdef WIN32
#define SEP "\\"
#else // WIN32
#define SEP "/"
#endif // ! WIN32
TEST(LocalPath, AppendWithSeparator)
{
LocalPath source;
LocalPath target = LocalPath::fromRelativePath("");
// Doesn't add a separator if the target is empty.
source = LocalPath::fromRelativePath("a");
target.appendWithSeparator(source, false);
EXPECT_EQ(target.toPath(false), "a");
// Doesn't add a separator if the source begins with one.
source = LocalPath::fromRelativePath(SEP "b");
target = LocalPath::fromRelativePath("a");
target.appendWithSeparator(source, true);
EXPECT_EQ(target.toPath(false), "a" SEP "b");
// Doesn't add a separator if the target ends with one.
source = LocalPath::fromRelativePath("b");
target = LocalPath::fromRelativePath("a" SEP);
target.appendWithSeparator(source, true);
EXPECT_EQ(target.toPath(false), "a" SEP "b");
// Adds a separator when:
// - source doesn't begin with one.
// - target doesn't end with one.
target = LocalPath::fromRelativePath("a");
target.appendWithSeparator(source, true);
EXPECT_EQ(target.toPath(false), "a" SEP "b");
}
TEST(LocalPath, PrependWithSeparator)
{
LocalPath source;
LocalPath target;
// No separator if target is empty.
source = LocalPath::fromRelativePath("b");
target.prependWithSeparator(source);
EXPECT_EQ(target.toPath(false), "b");
// No separator if target begins with separator.
target = LocalPath::fromRelativePath(SEP "a");
target.prependWithSeparator(source);
EXPECT_EQ(target.toPath(false), "b" SEP "a");
// No separator if source ends with separator.
source = LocalPath::fromRelativePath("b" SEP);
target = LocalPath::fromRelativePath("a");
target.prependWithSeparator(source);
EXPECT_EQ(target.toPath(false), "b" SEP "a");
}
#undef SEP
TEST(JSONWriter, arg_stringWithEscapes)
{
JSONWriter writer;
writer.arg_stringWithEscapes("ke", "\"\\");
EXPECT_EQ(writer.getstring(), "\"ke\":\"\\\"\\\\\"");
}
TEST(JSONWriter, escape)
{
class Writer
: public JSONWriter
{
public:
using JSONWriter::escape;
};
Writer writer;
string input = "\"\\";
string expected = "\\\"\\\\";
EXPECT_EQ(writer.escape(input.c_str(), input.size()), expected);
}
TEST(JSON, stripWhitespace)
{
auto input = string(" a\rb\n c\r{\"a\":\"q\\r \\\" s\"\n} x y\n z\n");
auto expected = string("abc{\"a\":\"q\\r \\\" s\"}xyz");
auto computed = JSON::stripWhitespace(input);
ASSERT_EQ(computed, expected);
input = "{\"a\":\"bcde";
expected = "{\"a\":\"";
computed = JSON::stripWhitespace(input);
ASSERT_EQ(computed, expected);
}
TEST(Utils, replace_char)
{
ASSERT_EQ(Utils::replace(string(""), '*', '@'), "");
ASSERT_EQ(Utils::replace(string("*"), '*', '@'), "@");
ASSERT_EQ(Utils::replace(string("**"), '*', '@'), "@@");
ASSERT_EQ(Utils::replace(string("*aa"), '*', '@'), "@aa");
ASSERT_EQ(Utils::replace(string("*aa*bb*"), '*', '@'), "@aa@bb@");
ASSERT_EQ(Utils::replace(string("sd*"), '*', '@'), "sd@");
ASSERT_EQ(Utils::replace(string("*aa**bb*"), '*', '@'), "@aa@@bb@");
}
TEST(Utils, replace_string)
{
ASSERT_EQ(Utils::replace(string(""), "*", "@"), "");
ASSERT_EQ(Utils::replace(string("*"), "*", "@"), "@");
ASSERT_EQ(Utils::replace(string("**"), "*", "@"), "@@");
ASSERT_EQ(Utils::replace(string("*aa"), "*", "@"), "@aa");
ASSERT_EQ(Utils::replace(string("*aa*bb*"), "*", "@"), "@aa@bb@");
ASSERT_EQ(Utils::replace(string("sd*"), "*", "@"), "sd@");
ASSERT_EQ(Utils::replace(string("*aa**bb*"), "*", "@"), "@aa@@bb@");
ASSERT_EQ(Utils::replace(string("*aa**bb*"), "*", "@"), "@aa@@bb@");
ASSERT_EQ(Utils::replace(string(""), "", "@"), "");
ASSERT_EQ(Utils::replace(string("abc"), "", "@"), "abc");
}
TEST(Utils, NaturalSortingAscii)
{
// Comparison between symbols
ASSERT_EQ(naturalsorting_compare("!", "!"), 0);
ASSERT_GT(naturalsorting_compare("@", "!"), 0);
ASSERT_LT(naturalsorting_compare("#", "$"), 0);
// Comparison between symbols and numbers
ASSERT_LT(naturalsorting_compare("#", "0"), 0);
ASSERT_LT(naturalsorting_compare("!", "9"), 0);
ASSERT_GT(naturalsorting_compare("9", "#"), 0);
// Comparison between symbols and letters
ASSERT_LT(naturalsorting_compare("&", "a"), 0);
ASSERT_LT(naturalsorting_compare("!", "Z"), 0);
ASSERT_GT(naturalsorting_compare("a", "#"), 0);
// Comparison between numbers and letters
ASSERT_LT(naturalsorting_compare("9", "a"), 0);
ASSERT_GT(naturalsorting_compare("a", "1"), 0);
ASSERT_LT(naturalsorting_compare("1", "A"), 0);
// Comparison between symbols and letters (case sensitive)
ASSERT_GT(naturalsorting_compare("A", "a"), 0);
ASSERT_GT(naturalsorting_compare("B", "a"), 0);
ASSERT_LT(naturalsorting_compare("a", "C"), 0);
// Comparison between strings containing letters and numbers
ASSERT_GT(naturalsorting_compare("a1", "a0"), 0);
ASSERT_LT(naturalsorting_compare("a1", "a2"), 0);
// Comparison between strings containing letters and symbols
ASSERT_LT(naturalsorting_compare("a!", "a#"), 0);
ASSERT_GT(naturalsorting_compare("a#", "a@"), 0);
// Comparison between strings containing letters, numbers and symbols
ASSERT_LT(naturalsorting_compare("1a!", "1a#"), 0);
ASSERT_LT(naturalsorting_compare("!a1", "a1#"), 0);
ASSERT_LT(naturalsorting_compare("!a1", "1#a"), 0);
ASSERT_GT(naturalsorting_compare("a1!", "1a#"), 0);
ASSERT_GT(naturalsorting_compare("a!1", "1a#"), 0);
ASSERT_GT(naturalsorting_compare("2a!", "1a#"), 0);
ASSERT_EQ(naturalsorting_compare("1a&", "1a&"), 0);
// Comparison between strings with different lengths
ASSERT_GT(naturalsorting_compare("abc", "ab"), 0);
ASSERT_LT(naturalsorting_compare("ab", "abc"), 0);
// Comparison between strings containing white spaces
ASSERT_LT(naturalsorting_compare("a ", "a!"), 0);
ASSERT_GT(naturalsorting_compare("a#", "a "), 0);
// Comparison between numbers of different lengths
ASSERT_GT(naturalsorting_compare("10", "2"), 0);
ASSERT_GT(naturalsorting_compare("100", "20"), 0);
// Comparison between numbers containing zeros at the beginning
ASSERT_LT(naturalsorting_compare("0", "00"), 0);
ASSERT_LT(naturalsorting_compare("00", "000"), 0);
ASSERT_LT(naturalsorting_compare("00123", "123"), 0);
ASSERT_LT(naturalsorting_compare("a0", "a00"), 0);
ASSERT_LT(naturalsorting_compare("00123", "124"), 0);
ASSERT_GT(naturalsorting_compare("0124", "00123"), 0);
}
TEST(Utils, NaturalSortingUnicode)
{
std::vector<std::string> names{
"11.txt", "#.txt", "中文.txt", "😼.txt", "१.txt",
"一.txt", "2.txt", "cafe.txt", "file2.txt", "1.txt",
"Cafe.txt", "अमन.txt.txt", "file1.txt", "cáfe.txt", "file11.txt",
"test.txt", "中文1.txt", "☺️.txt", "{}.txt",
};
std::vector<std::string> sortedNames{
"{}.txt", "#.txt", "☺️.txt", "😼.txt", "१.txt", "1.txt", "2.txt",
"11.txt", "cafe.txt", "Cafe.txt", "cáfe.txt", "file1.txt", "file2.txt", "file11.txt",
"test.txt", "अमन.txt.txt", "一.txt", "中文.txt", "中文1.txt",
};
auto comp = [](const std::string& a, const std::string& b)
{
return naturalsorting_compare(a.c_str(), b.c_str()) < 0;
};
std::stable_sort(names.begin(), names.end(), comp);
ASSERT_THAT(names, testing::ElementsAreArray(sortedNames));
}
TEST(RemotePath, nextPathComponent)
{
// Absolute path.
{
RemotePath path("/a/b/");
RemotePath component;
size_t index = 0;
ASSERT_TRUE(path.nextPathComponent(index, component));
ASSERT_EQ(component, "a");
ASSERT_TRUE(path.nextPathComponent(index, component));
ASSERT_EQ(component, "b");
ASSERT_FALSE(path.nextPathComponent(index, component));
ASSERT_TRUE(component.empty());
// Sanity.
path = RemotePath("/");
index = 0;
ASSERT_FALSE(path.nextPathComponent(index, component));
ASSERT_TRUE(component.empty());
}
// Relative path.
{
RemotePath path("a/b/");
RemotePath component;
size_t index = 0;
ASSERT_TRUE(path.nextPathComponent(index, component));
ASSERT_EQ(component, "a");
ASSERT_TRUE(path.nextPathComponent(index, component));
ASSERT_EQ(component, "b");
ASSERT_FALSE(path.nextPathComponent(index, component));
ASSERT_TRUE(component.empty());
// Sanity.
path = RemotePath("");
index = 0;
ASSERT_FALSE(path.nextPathComponent(index, component));
ASSERT_TRUE(component.empty());
}
}
class TooLongNameTest
: public ::testing::Test
{
public:
TooLongNameTest()
: Test()
, mPrefixName(LocalPath::fromRelativePath("d"))
, mPrefixPath()
{
}
LocalPath Append(const LocalPath& prefix, const string& name) const
{
LocalPath path = prefix;
path.appendWithSeparator(
LocalPath::fromRelativeName(name, mFsAccess, FS_UNKNOWN),
false);
return path;
}
LocalPath AppendLongName(const LocalPath& prefix, char character) const
{
// Representative limit.
//
// True limit depends on specific filesystem.
constexpr size_t MAX_COMPONENT_LENGTH = 255;
string name(MAX_COMPONENT_LENGTH + 1, character);
return Append(prefix, name);
}
bool CreateDummyFile(const LocalPath& path)
{
::mega::byte data = 0x21;
auto fileAccess = mFsAccess.newfileaccess(false);
return fileAccess->fopen(path, false, true, FSLogging::logOnError)
&& fileAccess->fwrite(&data, 1, 0);
}
void SetUp() override
{
// Flag should initially be clear.
ASSERT_FALSE(mFsAccess.target_name_too_long);
// Retrieve the current working directory.
ASSERT_TRUE(mFsAccess.cwd(mPrefixPath));
// Compute absolute path to "container" directory.
mPrefixPath.appendWithSeparator(mPrefixName, false);
// Remove container directory.
mFsAccess.emptydirlocal(mPrefixPath);
mFsAccess.rmdirlocal(mPrefixPath);
// Create container directory.
ASSERT_TRUE(mFsAccess.mkdirlocal(mPrefixPath, false, true));
}
void TearDown() override
{
// Destroy container directory.
mFsAccess.emptydirlocal(mPrefixPath);
mFsAccess.rmdirlocal(mPrefixPath);
}
FSACCESS_CLASS mFsAccess;
LocalPath mPrefixName;
LocalPath mPrefixPath;
}; // TooLongNameTest
TEST_F(TooLongNameTest, Copy)
{
// Absolute
{
auto source = Append(mPrefixPath, "s");
auto target = AppendLongName(mPrefixPath, 'u');
ASSERT_TRUE(CreateDummyFile(source));
ASSERT_FALSE(mFsAccess.copylocal(source, target, 0));
ASSERT_TRUE(mFsAccess.target_name_too_long);
// Legitimate "bad path" error should clear the flag.
target = Append(mPrefixPath, "u");
target = Append(target, "v");
ASSERT_FALSE(mFsAccess.copylocal(source, target, 0));
ASSERT_FALSE(mFsAccess.target_name_too_long);
}
}
TEST_F(TooLongNameTest, CreateDirectory)
{
// Absolute
{
auto path = AppendLongName(mPrefixPath, 'x');
ASSERT_FALSE(mFsAccess.mkdirlocal(path, false, true));
ASSERT_TRUE(mFsAccess.target_name_too_long);
// A legitimate "bad path" error should clear the flag.
path = Append(mPrefixPath, "x");
path = Append(path, "y");
ASSERT_FALSE(mFsAccess.mkdirlocal(path, false, true));
ASSERT_FALSE(mFsAccess.target_name_too_long);
}
}
TEST_F(TooLongNameTest, Rename)
{
// Absolute
{
auto source = Append(mPrefixPath, "q");
auto target = AppendLongName(mPrefixPath, 'r');
ASSERT_TRUE(mFsAccess.mkdirlocal(source, false, true));
ASSERT_FALSE(mFsAccess.renamelocal(source, target, false));
ASSERT_TRUE(mFsAccess.target_name_too_long);
// Legitimate "bad path" error should clear the flag.
target = Append(mPrefixPath, "u");
target = Append(target, "v");
ASSERT_FALSE(mFsAccess.renamelocal(source, target, false));
ASSERT_FALSE(mFsAccess.target_name_too_long);
}
}
class ProcessTest
: public ::testing::Test
{
public:
ProcessTest()
: Test()
{
}
};
#ifdef WIN32
string dirCommand = "dir";
string shellCommand = "cmd";
#else
string dirCommand = "ls";
string shellCommand = "sh";
#endif
TEST_F(ProcessTest, Poll)
{
Process p;
string out;
string error;
bool ok = p.run(vector<string>{dirCommand}, unordered_map<string, string>(), [&](const unsigned char* data, size_t len) {out.append((const char*)(data), len); }, [&](const unsigned char* data, size_t len) {error.append((const char*)(data), len); });
ASSERT_TRUE(ok) << "run failed" << endl;
while (p.isAlive()) {
if (!p.poll())
usleep(100000);
}
p.flush();
ASSERT_FALSE(out.empty()) << "no output received";
ASSERT_TRUE(error.empty()) << "error received";
}
TEST_F(ProcessTest, Wait)
{
Process p;
string out;
string error;
bool ok = p.run(vector<string>{dirCommand}, unordered_map<string, string>(), [&](const unsigned char* data, size_t len) {out.append((const char*)(data), len); }, [&](const unsigned char* data, size_t len) {error.append((const char*)(data), len); });
ASSERT_TRUE(ok) << "run failed" << endl;
p.wait();
ASSERT_FALSE(out.empty()) << "no output received";
ASSERT_TRUE(error.empty()) << "error received";
}
TEST_F(ProcessTest, RunError)
{
Process p;
string out;
string error;
bool ok = p.run(vector<string>{"this-command-does-not-exist", "tmp"}, unordered_map<string, string>(), [&](const unsigned char* data, size_t len) {out.append((const char*)(data), len); }, [&](const unsigned char* data, size_t len) {error.append((const char*)(data), len); });
// ok posix
// fails windows
ok = p.wait();
ASSERT_FALSE(ok) << "run ok!" << endl;
}
TEST_F(ProcessTest, WaitNonRedirect)
{
Process p;
bool ok = p.run(vector<string>{dirCommand});
ASSERT_TRUE(ok) << "run failed" << endl;
ok = p.wait();
ASSERT_TRUE(ok) << "program failed" << endl;
}
TEST_F(ProcessTest, ErrorNonRedirect)
{
Process p;
bool ok = p.run(vector<string>{dirCommand, "/file-does-not-exist"});
ASSERT_TRUE(ok) << "run failed" << endl;
ok = p.wait();
ASSERT_FALSE(ok) << "program ok" << endl;
}
class SprintfTest
: public ::testing::Test
{
};
TEST_F(SprintfTest, nulTerminateWhenBufferFull)
{
std::string countToSix("123456");
// g++ detects if we don't use a variable
std::string buf(countToSix.size(), 'x');
// with macro commented out
snprintf(buf.data(), 3, "%s", countToSix.data());
ASSERT_EQ(buf[0], '1');
ASSERT_EQ(buf[1], '2');
ASSERT_EQ(buf[2], '\0');
}
TEST_F(SprintfTest, Multiple)
{
std::string buffer(7, '\x0');
std::string aToH("ABCDEFGH");
std::string countToFour("1234");
snprintf(buffer.data(), buffer.size(), "%s", countToFour.data());
snprintf(&buffer[countToFour.size()], buffer.size() - countToFour.size(), "%s", aToH.data());
ASSERT_EQ(buffer[0], '1');
ASSERT_EQ(buffer[1], '2');
ASSERT_EQ(buffer[2], '3');
ASSERT_EQ(buffer[3], '4');
ASSERT_EQ(buffer[4], 'A');
ASSERT_EQ(buffer[5], 'B');
ASSERT_EQ(buffer[6], '\0');
}
TEST_F(SprintfTest, ResizeAndPrint) {
unsigned int price = 120;
string sprice;
sprice.resize(128);
snprintf(const_cast<char*>(sprice.data()), sprice.length(), "%.2f", price / 100.0);
replace(sprice.begin(), sprice.end(), ',', '.');
// sprince = "1.20\0\0\0\..."
ASSERT_EQ((string)sprice.c_str(), "1.20");
}
TEST(extensionOf, fails_when_extension_contains_invalid_characters)
{
using ::mega::extensionOf;
std::string computed;
// Characters below '.'
ASSERT_FALSE(extensionOf(std::string("a.-"), computed));
ASSERT_TRUE(computed.empty());
// Characters above 'z'.
ASSERT_FALSE(extensionOf(std::string("a.{"), computed));
ASSERT_TRUE(computed.empty());
}
TEST(extensionOf, fails_when_extension_isnt_present)
{
using ::mega::extensionOf;
std::string computed;
// No extension.
ASSERT_FALSE(extensionOf(std::string("a"), computed));
ASSERT_TRUE(computed.empty());
// Empty string.
ASSERT_FALSE(extensionOf(std::string(), computed));
ASSERT_TRUE(computed.empty());
}
TEST(extensionOf, succeeds)
{
using ::mega::extensionOf;
std::string computed;
// Multicharacter extension.
ASSERT_TRUE(extensionOf(std::string("a.BcD"), computed));
ASSERT_EQ(computed, ".bcd");
// Single character extension.
ASSERT_TRUE(extensionOf(std::wstring(L".a"), computed));
ASSERT_EQ(computed, ".a");
// Empty extension.
ASSERT_TRUE(extensionOf(std::string("."), computed));
ASSERT_EQ(computed, ".");
}
TEST(fromHex, fails_when_empty_string)
{
EXPECT_FALSE(fromHex<short>(nullptr, nullptr).second);
EXPECT_FALSE(fromHex<short>("").second);
}
TEST(fromHex, fails_when_invalid_character)
{
EXPECT_FALSE(fromHex<short>('q').second);
EXPECT_FALSE(fromHex<short>('_').second);
}
TEST(fromHex, fails_when_out_of_range)
{
EXPECT_FALSE(fromHex<signed char>("80").second);
EXPECT_FALSE(fromHex<short>("8000").second);
EXPECT_FALSE(fromHex<unsigned char>("100").second);
EXPECT_FALSE(fromHex<unsigned short>("10000").second);
}
TEST(fromHex, succeeds)
{
auto s8 = fromHex<signed char>("7f");
EXPECT_TRUE(s8.second);
EXPECT_EQ(s8.first, 0x7f);
auto s16 = fromHex<short>("7fff");
EXPECT_TRUE(s16.second);
EXPECT_EQ(s16.first, 0x7fff);
auto u8 = fromHex<unsigned char>("ff");
EXPECT_TRUE(u8.second);
EXPECT_EQ(u8.first, 0xff);
auto u16 = fromHex<unsigned short>("ffff");
EXPECT_TRUE(u16.second);
EXPECT_EQ(u16.first, 0xffff);
}
TEST(Split, no_delimiter)
{
auto input = std::string();
auto result = split(input, '.');
// Empty string.
EXPECT_EQ(result.first.first, input.data());
EXPECT_FALSE(result.first.second);
EXPECT_FALSE(result.second.first);
EXPECT_FALSE(result.second.second);
// No delimiter.
input = "abc";
result = split(input, '.');
EXPECT_EQ(result.first.first, input.data());
EXPECT_EQ(result.first.second, input.size());
EXPECT_FALSE(result.second.first);
EXPECT_FALSE(result.second.second);
}
TEST(Split, with_delimiter)
{
auto input = std::string("a.");
auto result = split(input, '.');
// Delimiter only.
EXPECT_EQ(result.first.first, input.data());
EXPECT_EQ(result.first.second, 1u);
EXPECT_EQ(result.second.first, &input[1]);
EXPECT_EQ(result.second.second, 1u);
// Delimiter and tail.
input = "abc.qrs";
result = split(input, '.');
EXPECT_EQ(result.first.first, input.data());
EXPECT_EQ(result.first.second, 3u);
EXPECT_EQ(result.second.first, &input[3]);
EXPECT_EQ(result.second.second, 4u);
}
TEST(EscapeWildCars, UseCases)
{
EXPECT_EQ(escapeWildCards("hello"), "hello");
EXPECT_EQ(escapeWildCards("hel*lo"), "hel\\*lo");
EXPECT_EQ(escapeWildCards("*hello*"), "\\*hello\\*");
EXPECT_EQ(escapeWildCards("\\*hello*"), "\\*hello\\*");
EXPECT_EQ(escapeWildCards("\\*hello\\*"), "\\*hello\\*");
EXPECT_EQ(escapeWildCards("hel\\\\*lo"), "hel\\\\\\*lo");
}
TEST(ScopedHelpers, ScopedDestructor)
{
// So we can test various binding styles.
struct Functor
{
void memberNoArguments() {}
void memberWithArguments(std::string) {}
static void rawNoArguments() {}
static void rawWithArguments(std::string) {}
}; // Functor
// Make sure we can bind raw function pointers.
{
auto x = makeScopedDestructor(&Functor::rawNoArguments);
// This also tests that convertible arguments are allowed.
auto y = makeScopedDestructor(&Functor::rawWithArguments, "Test");
}
// Make sure we can bind member function pointers.
{
auto f = Functor();
auto x = makeScopedDestructor(&Functor::memberNoArguments, &f);
auto y = makeScopedDestructor(&Functor::memberWithArguments, &f, "Test");
}
// Make sure we can bind lambda functions.
auto x = 0;
// Lambda without parameters.
{
auto y = makeScopedDestructor(
[&x]()
{
++x;
});
}
// Make sure the destructor was executed.
EXPECT_EQ(x, 1);
// Lambda with parameters.
{
auto y = makeScopedDestructor(
[&x](int v)
{
x += v;
},
3);
// Make sure convertible arguments are accepted.
auto z = makeScopedDestructor([](std::string) {}, "Test");
}
// Make sure destructor was executed.
EXPECT_EQ(x, 4);
}
TEST(ScopedHelpers, ScopedValue)
{
const std::string originalValue = "before";
std::string value = originalValue;
{
const std::string expectedValue = "After";
// Also tests that conertible arguments are accepted.
auto guard = makeScopedValue(value, expectedValue.c_str());
// Make sure value's value was changed.
ASSERT_EQ(value, expectedValue);
}
// Make sure value's value was restored.
ASSERT_EQ(value, originalValue);
}
TEST(ScopedHelpers, MakePtrFrom)
{
struct Dummy
{
static void destructor(Dummy* dummy)
{
delete dummy;
}
}; // Dummy
// Shared pointer, default deleter.
{
auto x = makeSharedFrom(new Dummy);
// Verify type signature.
static_assert(std::is_same_v<decltype(x), std::shared_ptr<Dummy>>);
}
// Shared pointer, custom deleter.
{
auto x = makeSharedFrom(new Dummy, &Dummy::destructor);
// Verify type signature.
static_assert(std::is_same_v<decltype(x), std::shared_ptr<Dummy>>);
// Verify deleter.
auto d = std::get_deleter<void (*)(Dummy*)>(x);
ASSERT_TRUE(d);
EXPECT_EQ(*d, &Dummy::destructor);
}
// Unique pointer, default deleter.
{
auto x = makeUniqueFrom(new Dummy);
// Verify type signature.
using ComputedType = decltype(x);
using ExpectedType = std::unique_ptr<Dummy, std::default_delete<Dummy>>;
static_assert(std::is_same_v<ComputedType, ExpectedType>);
}
// Unique pointer, custom deleter.
{
auto x = makeUniqueFrom(new Dummy, &Dummy::destructor);
using ComputedType = decltype(x);
using ExpectedType = std::unique_ptr<Dummy, void (*)(Dummy*)>;
static_assert(std::is_same_v<ComputedType, ExpectedType>);
}
}
TEST(LikeCompare, ExactMatch)
{
ASSERT_TRUE(likeCompare("hello", "hello"));
ASSERT_TRUE(likeCompare("he1lo", "he1lo"));
ASSERT_TRUE(likeCompare("hélloé", "hélloé"));
ASSERT_TRUE(likeCompare("你好", "你好"));
ASSERT_FALSE(likeCompare("hello1", "hello"));
ASSERT_FALSE(likeCompare("helo", "he1lo"));
ASSERT_FALSE(likeCompare("héllo", "hélloé"));
ASSERT_FALSE(likeCompare("你好", "你好!"));
}
TEST(LikeCompare, MatchOne)
{
ASSERT_TRUE(likeCompare("hell?", "hello"));
ASSERT_TRUE(likeCompare("héll?é", "hélloé"));
ASSERT_TRUE(likeCompare("你?", "你好"));
ASSERT_FALSE(likeCompare("hello?", "hello"));
ASSERT_FALSE(likeCompare("hel?o", "he1lo"));
ASSERT_FALSE(likeCompare("héll?", "hélloé"));
ASSERT_FALSE(likeCompare("你?", "你好!"));
}
TEST(LikeCompare, MatchAll)
{
ASSERT_TRUE(likeCompare("h*o", "hello"));
ASSERT_TRUE(likeCompare("*é", "hélloé"));
ASSERT_TRUE(likeCompare("*", "你好"));
ASSERT_FALSE(likeCompare("he1*lo", "hello"));
ASSERT_FALSE(likeCompare("*你", "你好!"));
}
TEST(LikeCompare, CaseInsensitiveMatch)
{
ASSERT_TRUE(likeCompare("HELLO", "hello"));
ASSERT_TRUE(likeCompare("HÉllOé", "hélloé"));
}
TEST(LikeCompare, AccentInsensitiveMatch)
{
ASSERT_TRUE(likeCompare("HÉllOé", "HElloe"));
ASSERT_TRUE(likeCompare("façade", "facade"));
ASSERT_TRUE(likeCompare("nghiên", "nghiAªn"));
}
// \\* is \* in c++ string. It is the escaping of the character * in the pattern, which makes it
// match only the single character *.
TEST(LikeCompare, EscapeMatch)
{
ASSERT_TRUE(likeCompare("H\\*Elloe", "H*Elloe"));
ASSERT_TRUE(likeCompare("\\*你*", "*你好!"));
ASSERT_FALSE(likeCompare("H\\*", "H*Elloe"));
ASSERT_FALSE(likeCompare("\\*你", "**你"));
}
TEST(LikeCompare, CombinedMatch)
{
ASSERT_TRUE(likeCompare("HÉ?l*e", "heLloé"));
ASSERT_TRUE(likeCompare("你ç?*", "你c好!"));
ASSERT_FALSE(likeCompare("HÉ?l*e\\*", "heLloé"));
}
TEST(NaturalSorting, Numbers)
{
static const std::vector<std::string> input =
{"123", "0123", "00123", "234", "0234", "00234", "00", "0", "000"}; // input
static const std::vector<std::string> expected =
{"0", "00", "000", "00123", "0123", "123", "00234", "0234", "234"}; // expected
std::vector<std::string> computed = input;
std::sort(computed.begin(), computed.end(), NaturalSortingComparator());
EXPECT_EQ(computed, expected);
}
class CreateIdFromName: public testing::TestWithParam<uint64_t>
{
public:
static constexpr uint64_t compileTimeSeed()
{
uint64_t s = 0;
for (const auto c: __TIME__)
{
s <<= 8;
s |= static_cast<uint64_t>(c);
}
return s;
}
};
TEST_P(CreateIdFromName, ValidateNewImplementation)
{
static constexpr uint64_t seed = CreateIdFromName::compileTimeSeed();
static constexpr string_view validChars{"!#$%&*+0123456789?^_abcdefghijklmnopqrstuvwxyz~"};
static constexpr char n[8]{validChars[seed % validChars.size()],
validChars[seed * 2 % validChars.size()],
validChars[seed * 3 % validChars.size()],
validChars[seed * 4 % validChars.size()],
validChars[seed * 5 % validChars.size()],
validChars[seed * 6 % validChars.size()],
validChars[seed * 7 % validChars.size()],
validChars[seed * 8 % validChars.size()]};
const uint64_t nameSize = GetParam();
switch (nameSize)
{
case 1:
{
static constexpr char name[]{n[0], 0};
static_assert(makeNameid(name) == MAKENAMEID1(n[0]));
ASSERT_EQ(makeNameid(string{name}), MAKENAMEID1(n[0]))
<< "Failed for \"" << name << '"';
static const char* constCharPtr = name;
ASSERT_EQ(makeNameid(constCharPtr), MAKENAMEID1(n[0]))
<< "Failed for \"" << name << '"';
break;
}
case 2:
{
static constexpr char name[]{n[0], n[1], 0};
static_assert(makeNameid(name) == MAKENAMEID2(n[0], n[1]));
ASSERT_EQ(makeNameid(string{name}), MAKENAMEID2(n[0], n[1]))
<< "Failed for \"" << name << '"';
static const char* constCharPtr = name;
ASSERT_EQ(makeNameid(constCharPtr), MAKENAMEID2(n[0], n[1]))
<< "Failed for \"" << name << '"';
break;
}
case 3:
{
static constexpr char name[]{n[0], n[1], n[2], 0};
static_assert(makeNameid(name) == MAKENAMEID3(n[0], n[1], n[2]));
ASSERT_EQ(makeNameid(string{name}), MAKENAMEID3(n[0], n[1], n[2]))
<< "Failed for \"" << name << '"';
static const char* constCharPtr = name;
ASSERT_EQ(makeNameid(constCharPtr), MAKENAMEID3(n[0], n[1], n[2]))
<< "Failed for \"" << name << '"';
break;
}
case 4:
{
static constexpr char name[]{n[0], n[1], n[2], n[3], 0};
static_assert(makeNameid(name) == MAKENAMEID4(n[0], n[1], n[2], n[3]));
ASSERT_EQ(makeNameid(string{name}), MAKENAMEID4(n[0], n[1], n[2], n[3]))
<< "Failed for \"" << name << '"';
static const char* constCharPtr = name;
ASSERT_EQ(makeNameid(constCharPtr), MAKENAMEID4(n[0], n[1], n[2], n[3]))
<< "Failed for \"" << name << '"';
break;
}
case 5:
{
static constexpr char name[]{n[0], n[1], n[2], n[3], n[4], 0};
static_assert(makeNameid(name) == MAKENAMEID5(n[0], n[1], n[2], n[3], n[4]));
ASSERT_EQ(makeNameid(string{name}), MAKENAMEID5(n[0], n[1], n[2], n[3], n[4]))
<< "Failed for \"" << name << '"';
static const char* constCharPtr = name;
ASSERT_EQ(makeNameid(constCharPtr), MAKENAMEID5(n[0], n[1], n[2], n[3], n[4]))
<< "Failed for \"" << name << '"';
break;
}
case 6:
{
static constexpr char name[]{n[0], n[1], n[2], n[3], n[4], n[5], 0};
static_assert(makeNameid(name) == MAKENAMEID6(n[0], n[1], n[2], n[3], n[4], n[5]));
ASSERT_EQ(makeNameid(string{name}), MAKENAMEID6(n[0], n[1], n[2], n[3], n[4], n[5]))
<< "Failed for \"" << name << '"';
static const char* constCharPtr = name;
ASSERT_EQ(makeNameid(constCharPtr), MAKENAMEID6(n[0], n[1], n[2], n[3], n[4], n[5]))
<< "Failed for \"" << name << '"';
break;
}
case 7:
{
static constexpr char name[]{n[0], n[1], n[2], n[3], n[4], n[5], n[6], 0};
static_assert(makeNameid(name) ==
MAKENAMEID7(n[0], n[1], n[2], n[3], n[4], n[5], n[6]));
ASSERT_EQ(makeNameid(string{name}),
MAKENAMEID7(n[0], n[1], n[2], n[3], n[4], n[5], n[6]))
<< "Failed for \"" << name << '"';
static const char* constCharPtr = name;
ASSERT_EQ(makeNameid(constCharPtr),
MAKENAMEID7(n[0], n[1], n[2], n[3], n[4], n[5], n[6]))
<< "Failed for \"" << name << '"';
break;
}
case 8:
{
static constexpr char name[]{n[0], n[1], n[2], n[3], n[4], n[5], n[6], n[7], 0};
static_assert(makeNameid(name) ==
MAKENAMEID8(n[0], n[1], n[2], n[3], n[4], n[5], n[6], n[7]));
ASSERT_EQ(makeNameid(string{name}),
MAKENAMEID8(n[0], n[1], n[2], n[3], n[4], n[5], n[6], n[7]))
<< "Failed for \"" << name << '"';
static const char* constCharPtr = name;
ASSERT_EQ(makeNameid(constCharPtr),
MAKENAMEID8(n[0], n[1], n[2], n[3], n[4], n[5], n[6], n[7]))
<< "Failed for \"" << name << '"';
break;
}
}
}
INSTANTIATE_TEST_SUITE_P(NameidTests, CreateIdFromName, testing::Values(1, 2, 3, 4, 5, 6, 7, 8));
// Test class Range
TEST(RangeTest, ValidRange)
{
// Range from 2 to 5 -> expect iteration over 2, 3, 4
Range r(2, 5);
std::vector<unsigned> values(std::begin(r), std::end(r));
EXPECT_THAT(values, testing::ElementsAre(2, 3, 4));
}
TEST(RangeTest, EmptyRangeWhenStartEqualsToEnd)
{
// Range from 5 to 5 -> empty range
Range r(5, 5);
EXPECT_TRUE(r.empty());
}
TEST(RangeTest, EmptyRangeWhenStartGreaterThanEnd)
{
// Range from 6 to 5 -> empty range
Range r(6, 5);
EXPECT_TRUE(r.empty());
unsigned count = 0;
for ([[maybe_unused]] const auto val: r)
{
++count;
}
EXPECT_EQ(count, 0);
}
TEST(RangeTest, OverloadRangeToZeroStart)
{
// range(5) -> Range(0, 5)
auto r = range(5);
std::vector<unsigned> values;
for (const auto val: r)
{
values.push_back(val);
}
EXPECT_THAT(values, testing::ElementsAre(0, 1, 2, 3, 4));
}
TEST(RangeTest, VerifySingleElementRange)
{
// Range(7, 8) should iterate exactly once
auto r = range(7, 8);
unsigned count = 0;
unsigned valueCollected = 0;
for (const auto val: r)
{
++count;
valueCollected = val;
}
EXPECT_EQ(count, 1);
EXPECT_EQ(valueCollected, 7u);
}
struct FileAccessTest: ::testing::Test
{
FileAccessTest():
Test(),
mFSAccess(),
mName(LocalPath::fromAbsolutePath("file"))
{}
// Called before any test in the fixture is executed.
void SetUp() override
{
// Remove the file if it's present after a previous test run.
ASSERT_TRUE(mFSAccess.unlinklocal(mName) || !mFSAccess.target_exists);
}
// Convenience.
::mega::FSLogging NO_LOGGING = ::mega::FSLogging::noLogging;
// So we can get our hands on a FileAccess instance.
FSACCESS_CLASS mFSAccess;
// The name of our test file.
::mega::LocalPath mName;
}; // FileAccessTest
TEST_F(FileAccessTest, OpenForReadWriteSucceeds)
{
// So we can open a file.
auto fileAccess = mFSAccess.newfileaccess(false);
// Sanity.
ASSERT_TRUE(fileAccess);
// Opening for reading and writing should create a new file if necessary.
EXPECT_TRUE(fileAccess->fopen(mName, true, true, NO_LOGGING));
}
TEST_F(FileAccessTest, OpenEquivalence)
{
auto fileAccess0 = mFSAccess.newfileaccess(false);
auto fileAccess1 = mFSAccess.newfileaccess(false);
// Sanity.
ASSERT_TRUE(fileAccess0);
ASSERT_TRUE(fileAccess1);
// Create a new file.
ASSERT_TRUE(fileAccess0->fopen(mName, true, true, NO_LOGGING));
// Open an existing file.
EXPECT_TRUE(fileAccess1->fopen(mName, true, true, NO_LOGGING));
// Convenience.
auto& lhs = *fileAccess0;
auto& rhs = *fileAccess1;
// Make sure selected state is equivalent.
EXPECT_EQ(lhs.fopenSucceeded, rhs.fopenSucceeded);
EXPECT_EQ(lhs.size, rhs.size);
EXPECT_EQ(lhs.mtime, rhs.mtime);
EXPECT_EQ(lhs.fsid, rhs.fsid);
EXPECT_EQ(lhs.type, rhs.type);
EXPECT_EQ(lhs.mIsSymLink, rhs.mIsSymLink);
}
TEST(IP, is_valid_ipv4_address_fails)
{
ASSERT_FALSE(isValidIPv4Address(""));
ASSERT_FALSE(isValidIPv4Address("1"));
ASSERT_FALSE(isValidIPv4Address("1.2"));
ASSERT_FALSE(isValidIPv4Address("1.2.3"));
ASSERT_FALSE(isValidIPv4Address("::1"));
}
TEST(IP, is_valid_ipv4_address_succeeds)
{
ASSERT_TRUE(isValidIPv4Address("192.168.0.1"));
}
TEST(IP, is_valid_ipv6_address_fails)
{
ASSERT_FALSE(isValidIPv6Address(""));
ASSERT_FALSE(isValidIPv6Address("192.168.0.1"));
ASSERT_FALSE(isValidIPv6Address("0"));
ASSERT_FALSE(isValidIPv6Address("::q"));
}
TEST(IP, is_valid_ipv6_address_succeeds)
{
ASSERT_TRUE(isValidIPv6Address("2001:db8:3333:4444:5555:6666:7777:8888"));
ASSERT_TRUE(isValidIPv6Address("2001:db8::"));
ASSERT_TRUE(isValidIPv6Address("::1234:5678"));
ASSERT_TRUE(isValidIPv6Address("2001:db8:3333:4444:5555:6666:1.2.3.4"));
ASSERT_TRUE(isValidIPv6Address("2001:db8::1234:5678:5.6.7.8"));
ASSERT_TRUE(isValidIPv6Address("::11.22.33.44"));
}
// Using string_vector to avoid parsing problems with GTest.
TEST(DNS, cache_resolved_urls_fails)
{
CurlHttpIO io;
// Not enough IPs for each URI.
EXPECT_LT(io.cacheresolvedurls(string_vector(1), string_vector(1)), 0);
// Not enough URIs for each IP.
EXPECT_LT(io.cacheresolvedurls(string_vector(1), string_vector(4)), 0);
// Multiple URIs and no IPs.
EXPECT_LT(io.cacheresolvedurls(string_vector(2), string_vector()), 0);
// Make sure the cache remains empty.
EXPECT_TRUE(io.getCachedDNSEntries().empty());
}
TEST(DNS, cache_resolved_urls_succeeds)
{
CurlHttpIO io;
// Expected DNS cache entries.
std::map<std::string, DNSEntry> expected = {{"a.com", DNSEntry{"1.2.3.4", "::1"}},
{"b.com", DNSEntry{"2.3.4.5", "::2"}}}; // expected
std::vector<std::string> uris = {"https://a.com", "https://b.com"};
std::vector<std::string> ips = {"1.2.3.4", "::1", "2.3.4.5", "::2"};
// Each URI is associated with a valid IPv4 and IPv6 address.
EXPECT_EQ(io.cacheresolvedurls(uris, ips), 0);
// Make sure the DNS cache was updated as expected.
EXPECT_EQ(expected, io.getCachedDNSEntries());
// Each URI is associated with an invalid IP.
//
// a.com has an invalid IPv4 address.
// b.com has an invalid IPv6 address.
ips[0] = "badV4";
ips[3] = "badV6";
EXPECT_EQ(io.cacheresolvedurls(uris, ips), 2);
// Make sure previously valid IPs were cleared.
expected["a.com"].ipv4.clear();
expected["b.com"].ipv6.clear();
EXPECT_EQ(expected, io.getCachedDNSEntries());
// Each URI isn't associated with any valid IP address.
ips[1] = ips[3];
ips[2] = ips[0];
EXPECT_EQ(io.cacheresolvedurls(uris, ips), 4);
// Make sure URIs remain and that any valid IPs were cleared.
expected["a.com"].ipv6.clear();
expected["b.com"].ipv4.clear();
EXPECT_EQ(expected, io.getCachedDNSEntries());
// A new URI isn't associated with any valid IP addresses.
uris = {"c.com"};
ips = {"badV4", "badV6"};
EXPECT_EQ(io.cacheresolvedurls(uris, ips), 2);
// Make sure no entry was added to the DNS cache.
EXPECT_EQ(expected, io.getCachedDNSEntries());
// Two new URIs only have a single valid IP each.
uris = {"https://d.com", "https://e.com"};
ips = {"4.5.6.7", "badV6", "badV4", "::3"};
EXPECT_EQ(io.cacheresolvedurls(uris, ips), 2);
// Make sure each URI was added to the cache.
expected["d.com"].ipv4 = ips[0];
expected["e.com"].ipv6 = ips[3];
EXPECT_EQ(expected, io.getCachedDNSEntries());
// Invalid URIs are skipped.
uris = {"z"};
ips = {"4.3.2.1", "::4"};
EXPECT_EQ(io.cacheresolvedurls(uris, ips), 0);
// Make sure the bad URI wasn't added to the cache.
EXPECT_EQ(expected, io.getCachedDNSEntries());
}
|