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
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <list>
#include <map>
#include <set>
#include <vector>
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/command_line.h"
#include "base/compiler_specific.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/message_loop/message_loop.h"
#include "base/run_loop.h"
#include "base/sequenced_task_runner.h"
#include "base/stl_util.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/threading/thread.h"
#include "base/version.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/extensions/crx_installer.h"
#include "chrome/browser/extensions/extension_error_reporter.h"
#include "chrome/browser/extensions/extension_sync_data.h"
#include "chrome/browser/extensions/test_extension_prefs.h"
#include "chrome/browser/extensions/test_extension_service.h"
#include "chrome/browser/extensions/test_extension_system.h"
#include "chrome/browser/extensions/updater/chrome_extension_downloader_factory.h"
#include "chrome/browser/extensions/updater/extension_updater.h"
#include "chrome/browser/google/google_brand.h"
#include "chrome/browser/prefs/pref_service_syncable.h"
#include "chrome/test/base/testing_profile.h"
#include "components/crx_file/id_util.h"
#include "components/update_client/update_query_params.h"
#include "content/public/browser/notification_details.h"
#include "content/public/browser/notification_observer.h"
#include "content/public/browser/notification_registrar.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/notification_source.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "content/public/test/test_utils.h"
#include "extensions/browser/extension_prefs.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/extension_system.h"
#include "extensions/browser/updater/extension_downloader.h"
#include "extensions/browser/updater/extension_downloader_delegate.h"
#include "extensions/browser/updater/manifest_fetch_data.h"
#include "extensions/browser/updater/request_queue_impl.h"
#include "extensions/common/extension.h"
#include "extensions/common/extension_urls.h"
#include "extensions/common/manifest_constants.h"
#include "google_apis/gaia/fake_identity_provider.h"
#include "google_apis/gaia/fake_oauth2_token_service.h"
#include "libxml/globals.h"
#include "net/base/backoff_entry.h"
#include "net/base/escape.h"
#include "net/base/load_flags.h"
#include "net/http/http_request_headers.h"
#include "net/url_request/test_url_fetcher_factory.h"
#include "net/url_request/url_request_status.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/third_party/mozilla/url_parse.h"
#if defined(OS_CHROMEOS)
#include "chrome/browser/chromeos/login/users/scoped_test_user_manager.h"
#include "chrome/browser/chromeos/settings/cros_settings.h"
#include "chrome/browser/chromeos/settings/device_settings_service.h"
#endif
using base::Time;
using base::TimeDelta;
using content::BrowserThread;
using update_client::UpdateQueryParams;
using testing::DoAll;
using testing::Invoke;
using testing::InvokeWithoutArgs;
using testing::Mock;
using testing::Return;
using testing::SetArgPointee;
using testing::_;
namespace extensions {
typedef ExtensionDownloaderDelegate::Error Error;
typedef ExtensionDownloaderDelegate::PingResult PingResult;
namespace {
const net::BackoffEntry::Policy kNoBackoffPolicy = {
// Number of initial errors (in sequence) to ignore before applying
// exponential back-off rules.
1000,
// Initial delay for exponential back-off in ms.
0,
// Factor by which the waiting time will be multiplied.
0,
// Fuzzing percentage. ex: 10% will spread requests randomly
// between 90%-100% of the calculated time.
0,
// Maximum amount of time we are willing to delay our request in ms.
0,
// Time to keep an entry from being discarded even when it
// has no significant state, -1 to never discard.
-1,
// Don't use initial delay unless the last request was an error.
false,
};
const char kEmptyUpdateUrlData[] = "";
const char kAuthUserQueryKey[] = "authuser";
int kExpectedLoadFlags =
net::LOAD_DO_NOT_SEND_COOKIES |
net::LOAD_DO_NOT_SAVE_COOKIES |
net::LOAD_DISABLE_CACHE;
int kExpectedLoadFlagsForDownloadWithCookies = net::LOAD_DISABLE_CACHE;
// Fake authentication constants
const char kFakeAccountId[] = "bobloblaw@lawblog.example.com";
const char kFakeOAuth2Token[] = "ce n'est pas un jeton";
const ManifestFetchData::PingData kNeverPingedData(
ManifestFetchData::kNeverPinged,
ManifestFetchData::kNeverPinged,
true,
0);
class MockExtensionDownloaderDelegate : public ExtensionDownloaderDelegate {
public:
MOCK_METHOD4(OnExtensionDownloadFailed, void(const std::string&,
Error,
const PingResult&,
const std::set<int>&));
MOCK_METHOD7(OnExtensionDownloadFinished, void(const std::string&,
const base::FilePath&,
bool,
const GURL&,
const std::string&,
const PingResult&,
const std::set<int>&));
MOCK_METHOD2(GetPingDataForExtension,
bool(const std::string&, ManifestFetchData::PingData*));
MOCK_METHOD1(GetUpdateUrlData, std::string(const std::string&));
MOCK_METHOD1(IsExtensionPending, bool(const std::string&));
MOCK_METHOD2(GetExtensionExistingVersion,
bool(const std::string&, std::string*));
void Wait() {
scoped_refptr<content::MessageLoopRunner> runner =
new content::MessageLoopRunner;
quit_closure_ = runner->QuitClosure();
runner->Run();
quit_closure_.Reset();
}
void Quit() {
quit_closure_.Run();
}
void DelegateTo(ExtensionDownloaderDelegate* delegate) {
ON_CALL(*this, OnExtensionDownloadFailed(_, _, _, _))
.WillByDefault(Invoke(delegate,
&ExtensionDownloaderDelegate::OnExtensionDownloadFailed));
ON_CALL(*this, OnExtensionDownloadFinished(_, _, _, _, _, _, _))
.WillByDefault(Invoke(delegate,
&ExtensionDownloaderDelegate::OnExtensionDownloadFinished));
ON_CALL(*this, GetPingDataForExtension(_, _))
.WillByDefault(Invoke(delegate,
&ExtensionDownloaderDelegate::GetPingDataForExtension));
ON_CALL(*this, GetUpdateUrlData(_))
.WillByDefault(Invoke(delegate,
&ExtensionDownloaderDelegate::GetUpdateUrlData));
ON_CALL(*this, IsExtensionPending(_))
.WillByDefault(Invoke(delegate,
&ExtensionDownloaderDelegate::IsExtensionPending));
ON_CALL(*this, GetExtensionExistingVersion(_, _))
.WillByDefault(Invoke(delegate,
&ExtensionDownloaderDelegate::GetExtensionExistingVersion));
}
private:
base::Closure quit_closure_;
};
const int kNotificationsObserved[] = {
extensions::NOTIFICATION_EXTENSION_UPDATING_STARTED,
extensions::NOTIFICATION_EXTENSION_UPDATE_FOUND,
};
// A class that observes the notifications sent by the ExtensionUpdater and
// the ExtensionDownloader.
class NotificationsObserver : public content::NotificationObserver {
public:
NotificationsObserver() {
for (size_t i = 0; i < arraysize(kNotificationsObserved); ++i) {
count_[i] = 0;
registrar_.Add(this,
kNotificationsObserved[i],
content::NotificationService::AllSources());
}
}
~NotificationsObserver() override {
for (size_t i = 0; i < arraysize(kNotificationsObserved); ++i) {
registrar_.Remove(this,
kNotificationsObserved[i],
content::NotificationService::AllSources());
}
}
size_t StartedCount() { return count_[0]; }
size_t UpdatedCount() { return count_[1]; }
bool Updated(const std::string& id) {
return updated_.find(id) != updated_.end();
}
void Wait() {
scoped_refptr<content::MessageLoopRunner> runner =
new content::MessageLoopRunner;
quit_closure_ = runner->QuitClosure();
runner->Run();
quit_closure_.Reset();
}
private:
void Observe(int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) override {
if (!quit_closure_.is_null())
quit_closure_.Run();
for (size_t i = 0; i < arraysize(kNotificationsObserved); ++i) {
if (kNotificationsObserved[i] == type) {
count_[i]++;
if (type == extensions::NOTIFICATION_EXTENSION_UPDATE_FOUND) {
updated_.insert(
content::Details<UpdateDetails>(details)->id);
}
return;
}
}
NOTREACHED();
}
content::NotificationRegistrar registrar_;
size_t count_[arraysize(kNotificationsObserved)];
std::set<std::string> updated_;
base::Closure quit_closure_;
DISALLOW_COPY_AND_ASSIGN(NotificationsObserver);
};
// Extracts the integer value of the |authuser| query parameter. Returns 0 if
// the parameter is not set.
int GetAuthUserQueryValue(const GURL& url) {
std::string query_string = url.query();
url::Component query(0, query_string.length());
url::Component key, value;
while (
url::ExtractQueryKeyValue(query_string.c_str(), &query, &key, &value)) {
std::string key_string = query_string.substr(key.begin, key.len);
if (key_string == kAuthUserQueryKey) {
int user_index = 0;
base::StringToInt(query_string.substr(value.begin, value.len),
&user_index);
return user_index;
}
}
return 0;
}
} // namespace
// Base class for further specialized test classes.
class MockService : public TestExtensionService {
public:
explicit MockService(TestExtensionPrefs* prefs)
: prefs_(prefs),
pending_extension_manager_(&profile_),
downloader_delegate_override_(NULL),
enable_metrics_(false) {}
~MockService() override {}
PendingExtensionManager* pending_extension_manager() override {
ADD_FAILURE() << "Subclass should override this if it will "
<< "be accessed by a test.";
return &pending_extension_manager_;
}
Profile* profile() { return &profile_; }
net::URLRequestContextGetter* request_context() {
return profile_.GetRequestContext();
}
ExtensionPrefs* extension_prefs() { return prefs_->prefs(); }
PrefService* pref_service() { return prefs_->pref_service(); }
FakeOAuth2TokenService* fake_token_service() {
return fake_token_service_.get();
}
// Controls whether metrics (enable/disabled state, etc.) are sent in the
// autoupdate ping requests.
void set_enable_metrics(bool enable) { enable_metrics_ = enable; }
// Creates test extensions and inserts them into list. The name and
// version are all based on their index. If |update_url| is non-null, it
// will be used as the update_url for each extension.
// The |id| is used to distinguish extension names and make sure that
// no two extensions share the same name.
void CreateTestExtensions(int id, int count, ExtensionList *list,
const std::string* update_url,
Manifest::Location location) {
for (int i = 1; i <= count; i++) {
base::DictionaryValue manifest;
manifest.SetString(manifest_keys::kVersion,
base::StringPrintf("%d.0.0.0", i));
manifest.SetString(manifest_keys::kName,
base::StringPrintf("Extension %d.%d", id, i));
if (update_url)
manifest.SetString(manifest_keys::kUpdateURL, *update_url);
scoped_refptr<Extension> e =
prefs_->AddExtensionWithManifest(manifest, location);
ASSERT_TRUE(e.get() != NULL);
list->push_back(e);
}
}
ExtensionDownloader::Factory GetDownloaderFactory() {
return base::Bind(&MockService::CreateExtensionDownloader,
base::Unretained(this));
}
ExtensionDownloader::Factory GetAuthenticatedDownloaderFactory() {
return base::Bind(&MockService::CreateExtensionDownloaderWithIdentity,
base::Unretained(this));
}
void OverrideDownloaderDelegate(ExtensionDownloaderDelegate* delegate) {
downloader_delegate_override_ = delegate;
}
protected:
TestExtensionPrefs* const prefs_;
TestingProfile profile_;
PendingExtensionManager pending_extension_manager_;
private:
scoped_ptr<ExtensionDownloader> CreateExtensionDownloader(
ExtensionDownloaderDelegate* delegate) {
scoped_ptr<ExtensionDownloader> downloader =
ChromeExtensionDownloaderFactory::CreateForRequestContext(
request_context(),
downloader_delegate_override_ ? downloader_delegate_override_
: delegate);
if (enable_metrics_)
downloader->set_enable_extra_update_metrics(true);
return downloader.Pass();
}
scoped_ptr<ExtensionDownloader> CreateExtensionDownloaderWithIdentity(
ExtensionDownloaderDelegate* delegate) {
scoped_ptr<FakeIdentityProvider> fake_identity_provider;
fake_token_service_.reset(new FakeOAuth2TokenService());
fake_identity_provider.reset(new FakeIdentityProvider(
fake_token_service_.get()));
fake_identity_provider->LogIn(kFakeAccountId);
fake_token_service_->AddAccount(kFakeAccountId);
scoped_ptr<ExtensionDownloader> downloader(
CreateExtensionDownloader(delegate));
downloader->SetWebstoreIdentityProvider(fake_identity_provider.Pass());
return downloader.Pass();
}
scoped_ptr<FakeOAuth2TokenService> fake_token_service_;
ExtensionDownloaderDelegate* downloader_delegate_override_;
bool enable_metrics_;
DISALLOW_COPY_AND_ASSIGN(MockService);
};
bool ShouldInstallExtensionsOnly(const Extension* extension) {
return extension->GetType() == Manifest::TYPE_EXTENSION;
}
bool ShouldInstallThemesOnly(const Extension* extension) {
return extension->is_theme();
}
bool ShouldAlwaysInstall(const Extension* extension) {
return true;
}
// Loads some pending extension records into a pending extension manager.
void SetupPendingExtensionManagerForTest(
int count,
const GURL& update_url,
PendingExtensionManager* pending_extension_manager) {
for (int i = 1; i <= count; ++i) {
PendingExtensionInfo::ShouldAllowInstallPredicate should_allow_install =
(i % 2 == 0) ? &ShouldInstallThemesOnly : &ShouldInstallExtensionsOnly;
const bool kIsFromSync = true;
const bool kMarkAcknowledged = false;
const bool kRemoteInstall = false;
std::string id =
crx_file::id_util::GenerateId(base::StringPrintf("extension%i", i));
pending_extension_manager->AddForTesting(
PendingExtensionInfo(id,
std::string(),
update_url,
Version(),
should_allow_install,
kIsFromSync,
Manifest::INTERNAL,
Extension::NO_FLAGS,
kMarkAcknowledged,
kRemoteInstall));
}
}
class ServiceForManifestTests : public MockService {
public:
explicit ServiceForManifestTests(TestExtensionPrefs* prefs)
: MockService(prefs),
registry_(ExtensionRegistry::Get(profile())) {
}
~ServiceForManifestTests() override {}
const Extension* GetExtensionById(const std::string& id,
bool include_disabled) const override {
const Extension* result = registry_->enabled_extensions().GetByID(id);
if (result || !include_disabled)
return result;
return registry_->disabled_extensions().GetByID(id);
}
PendingExtensionManager* pending_extension_manager() override {
return &pending_extension_manager_;
}
const Extension* GetPendingExtensionUpdate(
const std::string& id) const override {
return NULL;
}
bool IsExtensionEnabled(const std::string& id) const override {
return !registry_->disabled_extensions().Contains(id);
}
void set_extensions(ExtensionList extensions,
ExtensionList disabled_extensions) {
registry_->ClearAll();
for (ExtensionList::const_iterator it = extensions.begin();
it != extensions.end(); ++it) {
registry_->AddEnabled(*it);
}
for (ExtensionList::const_iterator it = disabled_extensions.begin();
it != disabled_extensions.end(); ++it) {
registry_->AddDisabled(*it);
}
}
private:
ExtensionRegistry* registry_;
};
class ServiceForDownloadTests : public MockService {
public:
explicit ServiceForDownloadTests(TestExtensionPrefs* prefs)
: MockService(prefs) {
}
// Add a fake crx installer to be returned by a call to UpdateExtension()
// with a specific ID. Caller keeps ownership of |crx_installer|.
void AddFakeCrxInstaller(const std::string& id, CrxInstaller* crx_installer) {
fake_crx_installers_[id] = crx_installer;
}
bool UpdateExtension(const std::string& id,
const base::FilePath& extension_path,
bool file_ownership_passed,
CrxInstaller** out_crx_installer) override {
extension_id_ = id;
install_path_ = extension_path;
if (ContainsKey(fake_crx_installers_, id)) {
*out_crx_installer = fake_crx_installers_[id];
return true;
}
return false;
}
PendingExtensionManager* pending_extension_manager() override {
return &pending_extension_manager_;
}
const Extension* GetExtensionById(const std::string& id,
bool) const override {
last_inquired_extension_id_ = id;
return NULL;
}
const std::string& extension_id() const { return extension_id_; }
const base::FilePath& install_path() const { return install_path_; }
private:
// Hold the set of ids that UpdateExtension() should fake success on.
// UpdateExtension(id, ...) will return true iff fake_crx_installers_
// contains key |id|. |out_install_notification_source| will be set
// to Source<CrxInstaller(fake_crx_installers_[i]).
std::map<std::string, CrxInstaller*> fake_crx_installers_;
std::string extension_id_;
base::FilePath install_path_;
GURL download_url_;
// The last extension ID that GetExtensionById was called with.
// Mutable because the method that sets it (GetExtensionById) is const
// in the actual extension service, but must record the last extension
// ID in this test class.
mutable std::string last_inquired_extension_id_;
};
static const int kUpdateFrequencySecs = 15;
// Takes a string with KEY=VALUE parameters separated by '&' in |params| and
// puts the key/value pairs into |result|. For keys with no value, the empty
// string is used. So for "a=1&b=foo&c", result would map "a" to "1", "b" to
// "foo", and "c" to "".
static void ExtractParameters(const std::string& params,
std::map<std::string, std::string>* result) {
std::vector<std::string> pairs;
base::SplitString(params, '&', &pairs);
for (size_t i = 0; i < pairs.size(); i++) {
std::vector<std::string> key_val;
base::SplitString(pairs[i], '=', &key_val);
if (!key_val.empty()) {
std::string key = key_val[0];
EXPECT_TRUE(result->find(key) == result->end());
(*result)[key] = (key_val.size() == 2) ? key_val[1] : std::string();
} else {
NOTREACHED();
}
}
}
// Helper function to extract the ping data param values for each extension in
// a manifest fetch url, returned in a map keyed by extension id.
// E.g. for "x=id%3Dabcdef%26ping%3Ddr%253D1%2526dr%253D1024" we'd return
// {"abcdef": {"dr": set("1", "1024")}}
typedef std::map<std::string, std::set<std::string>> ParamsMap;
static std::map<std::string, ParamsMap> GetPingDataFromURL(
const GURL& manifest_url) {
std::map<std::string, ParamsMap> result;
base::StringPairs toplevel_params;
base::SplitStringIntoKeyValuePairs(
manifest_url.query(), '=', '&', &toplevel_params);
for (const auto& param : toplevel_params) {
if (param.first != "x")
continue;
// We've found "x=<something>", now unescape <something> and look for
// the "id=<id>&ping=<ping_value>" parameters within.
std::string unescaped = net::UnescapeURLComponent(
param.second, net::UnescapeRule::URL_SPECIAL_CHARS);
base::StringPairs extension_params;
base::SplitStringIntoKeyValuePairs(unescaped, '=', '&', &extension_params);
std::multimap<std::string, std::string> param_map;
param_map.insert(extension_params.begin(), extension_params.end());
if (ContainsKey(param_map, "id") && ContainsKey(param_map, "ping")) {
std::string id = param_map.find("id")->second;
result[id] = ParamsMap();
// Pull the key=value pairs out of the ping parameter for this id and
// put into the result.
std::string ping = net::UnescapeURLComponent(
param_map.find("ping")->second, net::UnescapeRule::URL_SPECIAL_CHARS);
base::StringPairs ping_params;
base::SplitStringIntoKeyValuePairs(ping, '=', '&', &ping_params);
for (const auto& ping_param : ping_params) {
if (!ContainsKey(result[id], ping_param.first))
result[id][ping_param.first] = std::set<std::string>();
result[id][ping_param.first].insert(ping_param.second);
}
}
}
return result;
}
static void VerifyQueryAndExtractParameters(
const std::string& query,
std::map<std::string, std::string>* result) {
std::map<std::string, std::string> params;
ExtractParameters(query, ¶ms);
std::string omaha_params = UpdateQueryParams::Get(UpdateQueryParams::CRX);
std::map<std::string, std::string> expected;
ExtractParameters(omaha_params, &expected);
for (std::map<std::string, std::string>::iterator it = expected.begin();
it != expected.end(); ++it) {
EXPECT_EQ(it->second, params[it->first]);
}
EXPECT_EQ(1U, params.count("x"));
std::string decoded = net::UnescapeURLComponent(
params["x"], net::UnescapeRule::URL_SPECIAL_CHARS);
ExtractParameters(decoded, result);
}
// All of our tests that need to use private APIs of ExtensionUpdater live
// inside this class (which is a friend to ExtensionUpdater).
class ExtensionUpdaterTest : public testing::Test {
public:
ExtensionUpdaterTest()
: thread_bundle_(
content::TestBrowserThreadBundle::IO_MAINLOOP) {
}
void SetUp() override {
prefs_.reset(new TestExtensionPrefs(base::MessageLoopProxy::current()));
}
void TearDown() override {
// Some tests create URLRequestContextGetters, whose destruction must run
// on the IO thread. Make sure the IO loop spins before shutdown so that
// those objects are released.
RunUntilIdle();
prefs_.reset();
}
void RunUntilIdle() {
prefs_->pref_service()->CommitPendingWrite();
base::RunLoop().RunUntilIdle();
}
void SimulateTimerFired(ExtensionUpdater* updater) {
EXPECT_TRUE(updater->timer_.IsRunning());
updater->timer_.Stop();
updater->TimerFired();
content::RunAllBlockingPoolTasksUntilIdle();
}
// Adds a Result with the given data to results.
void AddParseResult(const std::string& id,
const std::string& version,
const std::string& url,
UpdateManifest::Results* results) {
UpdateManifest::Result result;
result.extension_id = id;
result.version = version;
result.crx_url = GURL(url);
results->list.push_back(result);
}
void StartUpdateCheck(ExtensionDownloader* downloader,
ManifestFetchData* fetch_data) {
downloader->StartUpdateCheck(scoped_ptr<ManifestFetchData>(fetch_data));
}
size_t ManifestFetchersCount(ExtensionDownloader* downloader) {
return downloader->manifests_queue_.size() +
(downloader->manifest_fetcher_.get() ? 1 : 0);
}
void TestExtensionUpdateCheckRequests(bool pending) {
// Create an extension with an update_url.
ServiceForManifestTests service(prefs_.get());
std::string update_url("http://foo.com/bar");
ExtensionList extensions;
NotificationsObserver observer;
PendingExtensionManager* pending_extension_manager =
service.pending_extension_manager();
if (pending) {
SetupPendingExtensionManagerForTest(1, GURL(update_url),
pending_extension_manager);
} else {
service.CreateTestExtensions(1, 1, &extensions, &update_url,
Manifest::INTERNAL);
service.set_extensions(extensions, ExtensionList());
}
// Set up and start the updater.
net::TestURLFetcherFactory factory;
ExtensionUpdater updater(&service,
service.extension_prefs(),
service.pref_service(),
service.profile(),
60 * 60 * 24,
NULL,
service.GetDownloaderFactory());
updater.Start();
// Tell the update that it's time to do update checks.
EXPECT_EQ(0u, observer.StartedCount());
SimulateTimerFired(&updater);
EXPECT_EQ(1u, observer.StartedCount());
// Get the url our mock fetcher was asked to fetch.
net::TestURLFetcher* fetcher =
factory.GetFetcherByID(ExtensionDownloader::kManifestFetcherId);
const GURL& url = fetcher->GetOriginalURL();
EXPECT_FALSE(url.is_empty());
EXPECT_TRUE(url.is_valid());
EXPECT_TRUE(url.SchemeIs("http"));
EXPECT_EQ("foo.com", url.host());
EXPECT_EQ("/bar", url.path());
// Validate the extension request parameters in the query. It should
// look something like "x=id%3D<id>%26v%3D<version>%26uc".
EXPECT_TRUE(url.has_query());
std::map<std::string, std::string> params;
VerifyQueryAndExtractParameters(url.query(), ¶ms);
if (pending) {
EXPECT_TRUE(pending_extension_manager->IsIdPending(params["id"]));
EXPECT_EQ("0.0.0.0", params["v"]);
} else {
EXPECT_EQ(extensions[0]->id(), params["id"]);
EXPECT_EQ(extensions[0]->VersionString(), params["v"]);
}
EXPECT_EQ("", params["uc"]);
}
void TestUpdateUrlDataEmpty() {
const std::string id = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
const std::string version = "1.0";
// Make sure that an empty update URL data string does not cause a ap=
// option to appear in the x= parameter.
scoped_ptr<ManifestFetchData> fetch_data(
CreateManifestFetchData(GURL("http://localhost/foo")));
fetch_data->AddExtension(
id, version, &kNeverPingedData, std::string(), std::string(), false);
std::map<std::string, std::string> params;
VerifyQueryAndExtractParameters(fetch_data->full_url().query(), ¶ms);
EXPECT_EQ(id, params["id"]);
EXPECT_EQ(version, params["v"]);
EXPECT_EQ(0U, params.count("ap"));
}
void TestUpdateUrlDataSimple() {
const std::string id = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
const std::string version = "1.0";
// Make sure that an update URL data string causes an appropriate ap=
// option to appear in the x= parameter.
scoped_ptr<ManifestFetchData> fetch_data(
CreateManifestFetchData(GURL("http://localhost/foo")));
fetch_data->AddExtension(
id, version, &kNeverPingedData, "bar", std::string(), false);
std::map<std::string, std::string> params;
VerifyQueryAndExtractParameters(fetch_data->full_url().query(), ¶ms);
EXPECT_EQ(id, params["id"]);
EXPECT_EQ(version, params["v"]);
EXPECT_EQ("bar", params["ap"]);
}
void TestUpdateUrlDataCompound() {
const std::string id = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
const std::string version = "1.0";
// Make sure that an update URL data string causes an appropriate ap=
// option to appear in the x= parameter.
scoped_ptr<ManifestFetchData> fetch_data(
CreateManifestFetchData(GURL("http://localhost/foo")));
fetch_data->AddExtension(
id, version, &kNeverPingedData, "a=1&b=2&c", std::string(), false);
std::map<std::string, std::string> params;
VerifyQueryAndExtractParameters(fetch_data->full_url().query(), ¶ms);
EXPECT_EQ(id, params["id"]);
EXPECT_EQ(version, params["v"]);
EXPECT_EQ("a%3D1%26b%3D2%26c", params["ap"]);
}
void TestUpdateUrlDataFromGallery(const std::string& gallery_url) {
net::TestURLFetcherFactory factory;
MockService service(prefs_.get());
MockExtensionDownloaderDelegate delegate;
ExtensionDownloader downloader(&delegate, service.request_context());
ExtensionList extensions;
std::string url(gallery_url);
service.CreateTestExtensions(1, 1, &extensions, &url, Manifest::INTERNAL);
const std::string& id = extensions[0]->id();
EXPECT_CALL(delegate, GetPingDataForExtension(id, _));
downloader.AddExtension(*extensions[0].get(), 0);
downloader.StartAllPending(NULL);
net::TestURLFetcher* fetcher =
factory.GetFetcherByID(ExtensionDownloader::kManifestFetcherId);
ASSERT_TRUE(fetcher);
// Make sure that extensions that update from the gallery ignore any
// update URL data.
const std::string& update_url = fetcher->GetOriginalURL().spec();
std::string::size_type x = update_url.find("x=");
EXPECT_NE(std::string::npos, x);
std::string::size_type ap = update_url.find("ap%3D", x);
EXPECT_EQ(std::string::npos, ap);
}
void TestInstallSource() {
const std::string id = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
const std::string version = "1.0";
const std::string install_source = "instally";
// Make sure that an installsource= appears in the x= parameter.
scoped_ptr<ManifestFetchData> fetch_data(
CreateManifestFetchData(GURL("http://localhost/foo")));
fetch_data->AddExtension(id, version, &kNeverPingedData,
kEmptyUpdateUrlData, install_source, false);
std::map<std::string, std::string> params;
VerifyQueryAndExtractParameters(fetch_data->full_url().query(), ¶ms);
EXPECT_EQ(id, params["id"]);
EXPECT_EQ(version, params["v"]);
EXPECT_EQ(install_source, params["installsource"]);
}
void TestDetermineUpdates() {
TestingProfile profile;
MockExtensionDownloaderDelegate delegate;
ExtensionDownloader downloader(&delegate, profile.GetRequestContext());
// Check passing an empty list of parse results to DetermineUpdates
scoped_ptr<ManifestFetchData> fetch_data(
CreateManifestFetchData(GURL("http://localhost/foo")));
UpdateManifest::Results updates;
std::vector<int> updateable;
downloader.DetermineUpdates(*fetch_data, updates, &updateable);
EXPECT_TRUE(updateable.empty());
// Create two updates - expect that DetermineUpdates will return the first
// one (v1.0 installed, v1.1 available) but not the second one (both
// installed and available at v2.0).
const std::string id1 = crx_file::id_util::GenerateId("1");
const std::string id2 = crx_file::id_util::GenerateId("2");
fetch_data->AddExtension(
id1, "1.0.0.0", &kNeverPingedData, kEmptyUpdateUrlData, std::string(),
false);
AddParseResult(id1, "1.1", "http://localhost/e1_1.1.crx", &updates);
fetch_data->AddExtension(
id2, "2.0.0.0", &kNeverPingedData, kEmptyUpdateUrlData, std::string(),
false);
AddParseResult(id2, "2.0.0.0", "http://localhost/e2_2.0.crx", &updates);
EXPECT_CALL(delegate, IsExtensionPending(_)).WillRepeatedly(Return(false));
EXPECT_CALL(delegate, GetExtensionExistingVersion(id1, _))
.WillOnce(DoAll(SetArgPointee<1>("1.0.0.0"),
Return(true)));
EXPECT_CALL(delegate, GetExtensionExistingVersion(id2, _))
.WillOnce(DoAll(SetArgPointee<1>("2.0.0.0"),
Return(true)));
downloader.DetermineUpdates(*fetch_data, updates, &updateable);
EXPECT_EQ(1u, updateable.size());
EXPECT_EQ(0, updateable[0]);
}
void TestDetermineUpdatesPending() {
// Create a set of test extensions
ServiceForManifestTests service(prefs_.get());
PendingExtensionManager* pending_extension_manager =
service.pending_extension_manager();
SetupPendingExtensionManagerForTest(3, GURL(), pending_extension_manager);
TestingProfile profile;
MockExtensionDownloaderDelegate delegate;
ExtensionDownloader downloader(&delegate, profile.GetRequestContext());
scoped_ptr<ManifestFetchData> fetch_data(
CreateManifestFetchData(GURL("http://localhost/foo")));
UpdateManifest::Results updates;
std::list<std::string> ids_for_update_check;
pending_extension_manager->GetPendingIdsForUpdateCheck(
&ids_for_update_check);
std::list<std::string>::const_iterator it;
for (it = ids_for_update_check.begin();
it != ids_for_update_check.end(); ++it) {
fetch_data->AddExtension(*it,
"1.0.0.0",
&kNeverPingedData,
kEmptyUpdateUrlData,
std::string(),
false);
AddParseResult(*it, "1.1", "http://localhost/e1_1.1.crx", &updates);
}
// The delegate will tell the downloader that all the extensions are
// pending.
EXPECT_CALL(delegate, IsExtensionPending(_)).WillRepeatedly(Return(true));
std::vector<int> updateable;
downloader.DetermineUpdates(*fetch_data, updates, &updateable);
// All the apps should be updateable.
EXPECT_EQ(3u, updateable.size());
for (std::vector<int>::size_type i = 0; i < updateable.size(); ++i) {
EXPECT_EQ(static_cast<int>(i), updateable[i]);
}
}
void TestMultipleManifestDownloading() {
net::TestURLFetcherFactory factory;
factory.set_remove_fetcher_on_delete(true);
net::TestURLFetcher* fetcher = NULL;
MockService service(prefs_.get());
MockExtensionDownloaderDelegate delegate;
ExtensionDownloader downloader(&delegate, service.request_context());
downloader.manifests_queue_.set_backoff_policy(&kNoBackoffPolicy);
GURL kUpdateUrl("http://localhost/manifest1");
scoped_ptr<ManifestFetchData> fetch1(CreateManifestFetchData(kUpdateUrl));
scoped_ptr<ManifestFetchData> fetch2(CreateManifestFetchData(kUpdateUrl));
scoped_ptr<ManifestFetchData> fetch3(CreateManifestFetchData(kUpdateUrl));
scoped_ptr<ManifestFetchData> fetch4(CreateManifestFetchData(kUpdateUrl));
ManifestFetchData::PingData zeroDays(0, 0, true, 0);
fetch1->AddExtension(
"1111", "1.0", &zeroDays, kEmptyUpdateUrlData, std::string(), false);
fetch2->AddExtension(
"2222", "2.0", &zeroDays, kEmptyUpdateUrlData, std::string(), false);
fetch3->AddExtension(
"3333", "3.0", &zeroDays, kEmptyUpdateUrlData, std::string(), false);
fetch4->AddExtension(
"4444", "4.0", &zeroDays, kEmptyUpdateUrlData, std::string(), false);
// This will start the first fetcher and queue the others. The next in queue
// is started as each fetcher receives its response. Note that the fetchers
// don't necessarily run in the order that they are started from here.
GURL fetch1_url = fetch1->full_url();
GURL fetch2_url = fetch2->full_url();
GURL fetch3_url = fetch3->full_url();
GURL fetch4_url = fetch4->full_url();
downloader.StartUpdateCheck(fetch1.Pass());
downloader.StartUpdateCheck(fetch2.Pass());
downloader.StartUpdateCheck(fetch3.Pass());
downloader.StartUpdateCheck(fetch4.Pass());
RunUntilIdle();
for (int i = 0; i < 4; ++i) {
fetcher = factory.GetFetcherByID(ExtensionDownloader::kManifestFetcherId);
ASSERT_TRUE(fetcher);
ASSERT_TRUE(fetcher->delegate());
EXPECT_TRUE(fetcher->GetLoadFlags() == kExpectedLoadFlags);
EXPECT_FALSE(fetcher->GetOriginalURL().is_empty());
if (fetcher->GetOriginalURL() == fetch1_url) {
// The first fetch will fail.
EXPECT_CALL(delegate, OnExtensionDownloadFailed(
"1111", ExtensionDownloaderDelegate::MANIFEST_FETCH_FAILED, _, _));
fetcher->set_url(kUpdateUrl);
fetcher->set_status(net::URLRequestStatus());
fetcher->set_response_code(400);
fetcher->delegate()->OnURLFetchComplete(fetcher);
RunUntilIdle();
Mock::VerifyAndClearExpectations(&delegate);
fetch1_url = GURL();
} else if (fetcher->GetOriginalURL() == fetch2_url) {
// The second fetch gets invalid data.
const std::string kInvalidXml = "invalid xml";
EXPECT_CALL(delegate, OnExtensionDownloadFailed(
"2222", ExtensionDownloaderDelegate::MANIFEST_INVALID, _, _))
.WillOnce(InvokeWithoutArgs(
&delegate,
&MockExtensionDownloaderDelegate::Quit));
fetcher->set_url(kUpdateUrl);
fetcher->set_status(net::URLRequestStatus());
fetcher->set_response_code(200);
fetcher->SetResponseString(kInvalidXml);
fetcher->delegate()->OnURLFetchComplete(fetcher);
delegate.Wait();
Mock::VerifyAndClearExpectations(&delegate);
fetch2_url = GURL();
} else if (fetcher->GetOriginalURL() == fetch3_url) {
// The third fetcher doesn't have an update available.
const std::string kNoUpdate =
"<?xml version='1.0' encoding='UTF-8'?>"
"<gupdate xmlns='http://www.google.com/update2/response'"
" protocol='2.0'>"
" <app appid='3333'>"
" <updatecheck codebase='http://example.com/extension_3.0.0.0.crx'"
" version='3.0.0.0' prodversionmin='3.0.0.0' />"
" </app>"
"</gupdate>";
EXPECT_CALL(delegate, IsExtensionPending("3333"))
.WillOnce(Return(false));
EXPECT_CALL(delegate, GetExtensionExistingVersion("3333", _))
.WillOnce(DoAll(SetArgPointee<1>("3.0.0.0"),
Return(true)));
EXPECT_CALL(delegate, OnExtensionDownloadFailed(
"3333", ExtensionDownloaderDelegate::NO_UPDATE_AVAILABLE, _, _))
.WillOnce(InvokeWithoutArgs(
&delegate,
&MockExtensionDownloaderDelegate::Quit));
fetcher->set_url(kUpdateUrl);
fetcher->set_status(net::URLRequestStatus());
fetcher->set_response_code(200);
fetcher->SetResponseString(kNoUpdate);
fetcher->delegate()->OnURLFetchComplete(fetcher);
delegate.Wait();
Mock::VerifyAndClearExpectations(&delegate);
fetch3_url = GURL();
} else if (fetcher->GetOriginalURL() == fetch4_url) {
// The last fetcher has an update.
NotificationsObserver observer;
const std::string kUpdateAvailable =
"<?xml version='1.0' encoding='UTF-8'?>"
"<gupdate xmlns='http://www.google.com/update2/response'"
" protocol='2.0'>"
" <app appid='4444'>"
" <updatecheck codebase='http://example.com/extension_1.2.3.4.crx'"
" version='4.0.42.0' prodversionmin='4.0.42.0' />"
" </app>"
"</gupdate>";
EXPECT_CALL(delegate, IsExtensionPending("4444"))
.WillOnce(Return(false));
EXPECT_CALL(delegate, GetExtensionExistingVersion("4444", _))
.WillOnce(DoAll(SetArgPointee<1>("4.0.0.0"),
Return(true)));
fetcher->set_url(kUpdateUrl);
fetcher->set_status(net::URLRequestStatus());
fetcher->set_response_code(200);
fetcher->SetResponseString(kUpdateAvailable);
fetcher->delegate()->OnURLFetchComplete(fetcher);
observer.Wait();
Mock::VerifyAndClearExpectations(&delegate);
// Verify that the downloader decided to update this extension.
EXPECT_EQ(1u, observer.UpdatedCount());
EXPECT_TRUE(observer.Updated("4444"));
fetch4_url = GURL();
} else {
ADD_FAILURE() << "Unexpected fetch: " << fetcher->GetOriginalURL();
}
}
fetcher = factory.GetFetcherByID(ExtensionDownloader::kManifestFetcherId);
if (fetcher)
ADD_FAILURE() << "Unexpected fetch: " << fetcher->GetOriginalURL();
}
void TestManifestRetryDownloading() {
net::TestURLFetcherFactory factory;
net::TestURLFetcher* fetcher = NULL;
NotificationsObserver observer;
MockService service(prefs_.get());
MockExtensionDownloaderDelegate delegate;
ExtensionDownloader downloader(&delegate, service.request_context());
downloader.manifests_queue_.set_backoff_policy(&kNoBackoffPolicy);
GURL kUpdateUrl("http://localhost/manifest1");
scoped_ptr<ManifestFetchData> fetch(CreateManifestFetchData(kUpdateUrl));
ManifestFetchData::PingData zeroDays(0, 0, true, 0);
fetch->AddExtension(
"1111", "1.0", &zeroDays, kEmptyUpdateUrlData, std::string(), false);
// This will start the first fetcher.
downloader.StartUpdateCheck(fetch.Pass());
RunUntilIdle();
// ExtensionDownloader should retry kMaxRetries times and then fail.
EXPECT_CALL(delegate, OnExtensionDownloadFailed(
"1111", ExtensionDownloaderDelegate::MANIFEST_FETCH_FAILED, _, _));
for (int i = 0; i <= ExtensionDownloader::kMaxRetries; ++i) {
// All fetches will fail.
fetcher = factory.GetFetcherByID(ExtensionDownloader::kManifestFetcherId);
EXPECT_TRUE(fetcher != NULL && fetcher->delegate() != NULL);
EXPECT_TRUE(fetcher->GetLoadFlags() == kExpectedLoadFlags);
fetcher->set_url(kUpdateUrl);
fetcher->set_status(net::URLRequestStatus());
// Code 5xx causes ExtensionDownloader to retry.
fetcher->set_response_code(500);
fetcher->delegate()->OnURLFetchComplete(fetcher);
RunUntilIdle();
}
Mock::VerifyAndClearExpectations(&delegate);
// For response codes that are not in the 5xx range ExtensionDownloader
// should not retry.
fetch.reset(CreateManifestFetchData(kUpdateUrl));
fetch->AddExtension(
"1111", "1.0", &zeroDays, kEmptyUpdateUrlData, std::string(), false);
// This will start the first fetcher.
downloader.StartUpdateCheck(fetch.Pass());
RunUntilIdle();
EXPECT_CALL(delegate, OnExtensionDownloadFailed(
"1111", ExtensionDownloaderDelegate::MANIFEST_FETCH_FAILED, _, _));
// The first fetch will fail, and require retrying.
fetcher = factory.GetFetcherByID(ExtensionDownloader::kManifestFetcherId);
EXPECT_TRUE(fetcher != NULL && fetcher->delegate() != NULL);
EXPECT_TRUE(fetcher->GetLoadFlags() == kExpectedLoadFlags);
fetcher->set_url(kUpdateUrl);
fetcher->set_status(net::URLRequestStatus());
fetcher->set_response_code(500);
fetcher->delegate()->OnURLFetchComplete(fetcher);
RunUntilIdle();
// The second fetch will fail with response 400 and should not cause
// ExtensionDownloader to retry.
fetcher = factory.GetFetcherByID(ExtensionDownloader::kManifestFetcherId);
EXPECT_TRUE(fetcher != NULL && fetcher->delegate() != NULL);
EXPECT_TRUE(fetcher->GetLoadFlags() == kExpectedLoadFlags);
fetcher->set_url(kUpdateUrl);
fetcher->set_status(net::URLRequestStatus());
fetcher->set_response_code(400);
fetcher->delegate()->OnURLFetchComplete(fetcher);
RunUntilIdle();
Mock::VerifyAndClearExpectations(&delegate);
}
void TestSingleExtensionDownloading(bool pending, bool retry, bool fail) {
net::TestURLFetcherFactory factory;
net::TestURLFetcher* fetcher = NULL;
scoped_ptr<ServiceForDownloadTests> service(
new ServiceForDownloadTests(prefs_.get()));
ExtensionUpdater updater(service.get(),
service->extension_prefs(),
service->pref_service(),
service->profile(),
kUpdateFrequencySecs,
NULL,
service->GetDownloaderFactory());
MockExtensionDownloaderDelegate delegate;
delegate.DelegateTo(&updater);
service->OverrideDownloaderDelegate(&delegate);
updater.Start();
updater.EnsureDownloaderCreated();
updater.downloader_->extensions_queue_.set_backoff_policy(
&kNoBackoffPolicy);
GURL test_url("http://localhost/extension.crx");
std::string id = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
std::string hash;
Version version("0.0.1");
std::set<int> requests;
requests.insert(0);
scoped_ptr<ExtensionDownloader::ExtensionFetch> fetch(
new ExtensionDownloader::ExtensionFetch(
id, test_url, hash, version.GetString(), requests));
updater.downloader_->FetchUpdatedExtension(fetch.Pass());
if (pending) {
const bool kIsFromSync = true;
const bool kMarkAcknowledged = false;
const bool kRemoteInstall = false;
PendingExtensionManager* pending_extension_manager =
service->pending_extension_manager();
pending_extension_manager->AddForTesting(
PendingExtensionInfo(id,
std::string(),
test_url,
version,
&ShouldAlwaysInstall,
kIsFromSync,
Manifest::INTERNAL,
Extension::NO_FLAGS,
kMarkAcknowledged,
kRemoteInstall));
}
// Call back the ExtensionUpdater with a 200 response and some test data
base::FilePath extension_file_path(FILE_PATH_LITERAL("/whatever"));
fetcher = factory.GetFetcherByID(ExtensionDownloader::kExtensionFetcherId);
EXPECT_TRUE(fetcher != NULL && fetcher->delegate() != NULL);
EXPECT_TRUE(fetcher->GetLoadFlags() == kExpectedLoadFlags);
if (retry) {
// Reply with response code 500 to cause ExtensionDownloader to retry
fetcher->set_url(test_url);
fetcher->set_status(net::URLRequestStatus());
fetcher->set_response_code(500);
fetcher->delegate()->OnURLFetchComplete(fetcher);
RunUntilIdle();
fetcher = factory.GetFetcherByID(
ExtensionDownloader::kExtensionFetcherId);
EXPECT_TRUE(fetcher != NULL && fetcher->delegate() != NULL);
EXPECT_TRUE(fetcher->GetLoadFlags() == kExpectedLoadFlags);
}
fetcher->set_url(test_url);
fetcher->set_status(net::URLRequestStatus());
if (fail) {
fetcher->set_response_code(404);
EXPECT_CALL(delegate, OnExtensionDownloadFailed(id, _, _, requests));
} else {
fetcher->set_response_code(200);
fetcher->SetResponseFilePath(extension_file_path);
EXPECT_CALL(delegate, OnExtensionDownloadFinished(
id, _, _, _, version.GetString(), _, requests));
}
fetcher->delegate()->OnURLFetchComplete(fetcher);
RunUntilIdle();
if (fail) {
// Don't expect any extension to have been installed.
EXPECT_TRUE(service->extension_id().empty());
} else {
// Expect that ExtensionUpdater asked the mock extensions service to
// install a file with the test data for the right id.
EXPECT_EQ(id, service->extension_id());
base::FilePath tmpfile_path = service->install_path();
EXPECT_FALSE(tmpfile_path.empty());
EXPECT_EQ(extension_file_path, tmpfile_path);
}
}
// Update a single extension in an environment where the download request
// initially responds with a 403 status. If |identity_provider| is not NULL,
// this will first expect a request which includes an Authorization header
// with an OAuth2 bearer token; otherwise, or if OAuth2 failure is simulated,
// this expects the downloader to fall back onto cookie-based credentials.
void TestProtectedDownload(
const std::string& url_prefix,
bool enable_oauth2,
bool succeed_with_oauth2,
int valid_authuser,
int max_authuser) {
net::TestURLFetcherFactory factory;
net::TestURLFetcher* fetcher = NULL;
scoped_ptr<ServiceForDownloadTests> service(
new ServiceForDownloadTests(prefs_.get()));
const ExtensionDownloader::Factory& downloader_factory =
enable_oauth2 ? service->GetAuthenticatedDownloaderFactory()
: service->GetDownloaderFactory();
ExtensionUpdater updater(
service.get(),
service->extension_prefs(),
service->pref_service(),
service->profile(),
kUpdateFrequencySecs,
NULL,
downloader_factory);
updater.Start();
updater.EnsureDownloaderCreated();
updater.downloader_->extensions_queue_.set_backoff_policy(
&kNoBackoffPolicy);
GURL test_url(base::StringPrintf("%s/extension.crx", url_prefix.c_str()));
std::string id = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
std::string hash;
Version version("0.0.1");
std::set<int> requests;
requests.insert(0);
scoped_ptr<ExtensionDownloader::ExtensionFetch> fetch(
new ExtensionDownloader::ExtensionFetch(
id, test_url, hash, version.GetString(), requests));
updater.downloader_->FetchUpdatedExtension(fetch.Pass());
fetcher = factory.GetFetcherByID(ExtensionDownloader::kExtensionFetcherId);
EXPECT_TRUE(fetcher != NULL && fetcher->delegate() != NULL);
EXPECT_EQ(kExpectedLoadFlags, fetcher->GetLoadFlags());
// Fake a 403 response.
fetcher->set_url(test_url);
fetcher->set_status(net::URLRequestStatus());
fetcher->set_response_code(403);
fetcher->delegate()->OnURLFetchComplete(fetcher);
if (service->fake_token_service()) {
service->fake_token_service()->IssueAllTokensForAccount(
kFakeAccountId, kFakeOAuth2Token, base::Time::Now());
}
RunUntilIdle();
bool using_oauth2 = false;
int expected_load_flags = kExpectedLoadFlags;
// Verify that the fetch has had its credentials properly incremented.
fetcher = factory.GetFetcherByID(ExtensionDownloader::kExtensionFetcherId);
EXPECT_TRUE(fetcher != NULL && fetcher->delegate() != NULL);
net::HttpRequestHeaders fetch_headers;
fetcher->GetExtraRequestHeaders(&fetch_headers);
// If the download URL is not https, no credentials should be provided.
if (!test_url.SchemeIsSecure()) {
// No cookies.
EXPECT_EQ(kExpectedLoadFlags, fetcher->GetLoadFlags());
// No Authorization header.
EXPECT_FALSE(fetch_headers.HasHeader(
net::HttpRequestHeaders::kAuthorization));
expected_load_flags = kExpectedLoadFlags;
} else {
// HTTPS is in use, so credentials are allowed.
if (enable_oauth2 && test_url.DomainIs("google.com")) {
// If an IdentityProvider is present and the URL is a google.com
// URL, the fetcher should be in OAuth2 mode after the intitial
// challenge.
EXPECT_TRUE(fetch_headers.HasHeader(
net::HttpRequestHeaders::kAuthorization));
std::string expected_header_value = base::StringPrintf("Bearer %s",
kFakeOAuth2Token);
std::string actual_header_value;
fetch_headers.GetHeader(net::HttpRequestHeaders::kAuthorization,
&actual_header_value);
EXPECT_EQ(expected_header_value, actual_header_value);
using_oauth2 = true;
} else {
// No IdentityProvider (or no google.com), so expect cookies instead of
// an Authorization header.
EXPECT_FALSE(fetch_headers.HasHeader(
net::HttpRequestHeaders::kAuthorization));
EXPECT_EQ(kExpectedLoadFlagsForDownloadWithCookies,
fetcher->GetLoadFlags());
expected_load_flags = kExpectedLoadFlagsForDownloadWithCookies;
}
}
bool success = false;
if (using_oauth2) {
if (succeed_with_oauth2) {
success = true;
} else {
// Simulate OAuth2 failure and ensure that we fall back on cookies.
fetcher->set_url(test_url);
fetcher->set_status(net::URLRequestStatus());
fetcher->set_response_code(403);
fetcher->delegate()->OnURLFetchComplete(fetcher);
RunUntilIdle();
const ExtensionDownloader::ExtensionFetch& fetch =
*updater.downloader_->extensions_queue_.active_request();
EXPECT_EQ(0, GetAuthUserQueryValue(fetch.url));
EXPECT_EQ(ExtensionDownloader::ExtensionFetch::CREDENTIALS_COOKIES,
fetch.credentials);
fetcher = factory.GetFetcherByID(
ExtensionDownloader::kExtensionFetcherId);
EXPECT_TRUE(fetcher != NULL && fetcher->delegate() != NULL);
fetcher->GetExtraRequestHeaders(&fetch_headers);
EXPECT_FALSE(fetch_headers.HasHeader(
net::HttpRequestHeaders::kAuthorization));
EXPECT_EQ(kExpectedLoadFlagsForDownloadWithCookies,
fetcher->GetLoadFlags());
expected_load_flags = kExpectedLoadFlagsForDownloadWithCookies;
}
}
if (!success) {
// Not yet ready to simulate a successful fetch. At this point we begin
// simulating cookie-based authentication with increasing values of
// authuser (starting from 0.)
int user_index = 0;
for (; user_index <= max_authuser; ++user_index) {
const ExtensionDownloader::ExtensionFetch& fetch =
*updater.downloader_->extensions_queue_.active_request();
EXPECT_EQ(user_index, GetAuthUserQueryValue(fetch.url));
if (user_index == valid_authuser) {
success = true;
break;
}
// Simulate an authorization failure which should elicit an increment
// of the authuser value.
fetcher =
factory.GetFetcherByID(ExtensionDownloader::kExtensionFetcherId);
EXPECT_TRUE(fetcher != NULL && fetcher->delegate() != NULL);
EXPECT_EQ(expected_load_flags, fetcher->GetLoadFlags());
fetcher->set_url(fetch.url);
fetcher->set_status(net::URLRequestStatus());
fetcher->set_response_code(403);
fetcher->delegate()->OnURLFetchComplete(fetcher);
RunUntilIdle();
}
// Simulate exhaustion of all available authusers.
if (!success && user_index > max_authuser) {
const ExtensionDownloader::ExtensionFetch& fetch =
*updater.downloader_->extensions_queue_.active_request();
fetcher =
factory.GetFetcherByID(ExtensionDownloader::kExtensionFetcherId);
EXPECT_TRUE(fetcher != NULL && fetcher->delegate() != NULL);
fetcher->set_url(fetch.url);
fetcher->set_status(net::URLRequestStatus());
fetcher->set_response_code(401);
fetcher->delegate()->OnURLFetchComplete(fetcher);
RunUntilIdle();
}
}
// Simulate successful authorization with a 200 response.
if (success) {
fetcher =
factory.GetFetcherByID(ExtensionDownloader::kExtensionFetcherId);
EXPECT_TRUE(fetcher != NULL && fetcher->delegate() != NULL);
base::FilePath extension_file_path(FILE_PATH_LITERAL("/whatever"));
fetcher->set_url(test_url);
fetcher->set_status(net::URLRequestStatus());
fetcher->set_response_code(200);
fetcher->SetResponseFilePath(extension_file_path);
fetcher->delegate()->OnURLFetchComplete(fetcher);
RunUntilIdle();
// Verify installation would proceed as normal.
EXPECT_EQ(id, service->extension_id());
base::FilePath tmpfile_path = service->install_path();
EXPECT_FALSE(tmpfile_path.empty());
EXPECT_EQ(extension_file_path, tmpfile_path);
}
}
// Two extensions are updated. If |updates_start_running| is true, the
// mock extensions service has UpdateExtension(...) return true, and
// the test is responsible for creating fake CrxInstallers. Otherwise,
// UpdateExtension() returns false, signaling install failures.
void TestMultipleExtensionDownloading(bool updates_start_running) {
net::TestURLFetcherFactory factory;
net::TestURLFetcher* fetcher = NULL;
ServiceForDownloadTests service(prefs_.get());
ExtensionUpdater updater(&service,
service.extension_prefs(),
service.pref_service(),
service.profile(),
kUpdateFrequencySecs,
NULL,
service.GetDownloaderFactory());
updater.Start();
updater.EnsureDownloaderCreated();
updater.downloader_->extensions_queue_.set_backoff_policy(
&kNoBackoffPolicy);
EXPECT_FALSE(updater.crx_install_is_running_);
GURL url1("http://localhost/extension1.crx");
GURL url2("http://localhost/extension2.crx");
std::string id1 = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
std::string id2 = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb";
std::string hash1;
std::string hash2;
std::string version1 = "0.1";
std::string version2 = "0.1";
std::set<int> requests;
requests.insert(0);
// Start two fetches
scoped_ptr<ExtensionDownloader::ExtensionFetch> fetch1(
new ExtensionDownloader::ExtensionFetch(
id1, url1, hash1, version1, requests));
scoped_ptr<ExtensionDownloader::ExtensionFetch> fetch2(
new ExtensionDownloader::ExtensionFetch(
id2, url2, hash2, version2, requests));
updater.downloader_->FetchUpdatedExtension(fetch1.Pass());
updater.downloader_->FetchUpdatedExtension(fetch2.Pass());
// Make the first fetch complete.
base::FilePath extension_file_path(FILE_PATH_LITERAL("/whatever"));
fetcher = factory.GetFetcherByID(ExtensionDownloader::kExtensionFetcherId);
EXPECT_TRUE(fetcher != NULL && fetcher->delegate() != NULL);
EXPECT_TRUE(fetcher->GetLoadFlags() == kExpectedLoadFlags);
// We need some CrxInstallers, and CrxInstallers require a real
// ExtensionService. Create one on the testing profile. Any action
// the CrxInstallers take is on the testing profile's extension
// service, not on our mock |service|. This allows us to fake
// the CrxInstaller actions we want.
TestingProfile profile;
static_cast<TestExtensionSystem*>(ExtensionSystem::Get(&profile))
->CreateExtensionService(base::CommandLine::ForCurrentProcess(),
base::FilePath(), false);
ExtensionService* extension_service =
ExtensionSystem::Get(&profile)->extension_service();
extension_service->set_extensions_enabled(true);
extension_service->set_show_extensions_prompts(false);
scoped_refptr<CrxInstaller> fake_crx1(
CrxInstaller::CreateSilent(extension_service));
scoped_refptr<CrxInstaller> fake_crx2(
CrxInstaller::CreateSilent(extension_service));
if (updates_start_running) {
// Add fake CrxInstaller to be returned by service.UpdateExtension().
service.AddFakeCrxInstaller(id1, fake_crx1.get());
service.AddFakeCrxInstaller(id2, fake_crx2.get());
} else {
// If we don't add fake CRX installers, the mock service fakes a failure
// starting the install.
}
fetcher->set_url(url1);
fetcher->set_status(net::URLRequestStatus());
fetcher->set_response_code(200);
fetcher->SetResponseFilePath(extension_file_path);
fetcher->delegate()->OnURLFetchComplete(fetcher);
RunUntilIdle();
// Expect that the service was asked to do an install with the right data.
base::FilePath tmpfile_path = service.install_path();
EXPECT_FALSE(tmpfile_path.empty());
EXPECT_EQ(id1, service.extension_id());
RunUntilIdle();
// Make sure the second fetch finished and asked the service to do an
// update.
base::FilePath extension_file_path2(FILE_PATH_LITERAL("/whatever2"));
fetcher = factory.GetFetcherByID(ExtensionDownloader::kExtensionFetcherId);
EXPECT_TRUE(fetcher != NULL && fetcher->delegate() != NULL);
EXPECT_TRUE(fetcher->GetLoadFlags() == kExpectedLoadFlags);
fetcher->set_url(url2);
fetcher->set_status(net::URLRequestStatus());
fetcher->set_response_code(200);
fetcher->SetResponseFilePath(extension_file_path2);
fetcher->delegate()->OnURLFetchComplete(fetcher);
RunUntilIdle();
if (updates_start_running) {
EXPECT_TRUE(updater.crx_install_is_running_);
// The second install should not have run, because the first has not
// sent a notification that it finished.
EXPECT_EQ(id1, service.extension_id());
// Fake install notice. This should start the second installation,
// which will be checked below.
fake_crx1->NotifyCrxInstallComplete(false);
EXPECT_TRUE(updater.crx_install_is_running_);
}
EXPECT_EQ(id2, service.extension_id());
EXPECT_FALSE(service.install_path().empty());
// Make sure the correct crx contents were passed for the update call.
EXPECT_EQ(extension_file_path2, service.install_path());
if (updates_start_running) {
EXPECT_TRUE(updater.crx_install_is_running_);
fake_crx2->NotifyCrxInstallComplete(false);
}
EXPECT_FALSE(updater.crx_install_is_running_);
}
void TestGalleryRequestsWithBrand(bool use_organic_brand_code) {
google_brand::BrandForTesting brand_for_testing(
use_organic_brand_code ? "GGLS" : "TEST");
// We want to test a variety of combinations of expected ping conditions for
// rollcall and active pings.
int ping_cases[] = { ManifestFetchData::kNeverPinged, 0, 1, 5 };
for (size_t i = 0; i < arraysize(ping_cases); i++) {
for (size_t j = 0; j < arraysize(ping_cases); j++) {
for (size_t k = 0; k < 2; k++) {
int rollcall_ping_days = ping_cases[i];
int active_ping_days = ping_cases[j];
// Skip cases where rollcall_ping_days == -1, but
// active_ping_days > 0, because rollcall_ping_days == -1 means the
// app was just installed and this is the first update check after
// installation.
if (rollcall_ping_days == ManifestFetchData::kNeverPinged &&
active_ping_days > 0)
continue;
bool active_bit = k > 0;
TestGalleryRequests(rollcall_ping_days, active_ping_days, active_bit,
!use_organic_brand_code);
ASSERT_FALSE(HasFailure()) <<
" rollcall_ping_days=" << ping_cases[i] <<
" active_ping_days=" << ping_cases[j] <<
" active_bit=" << active_bit;
}
}
}
}
// Test requests to both a Google server and a non-google server. This allows
// us to test various combinations of installed (ie roll call) and active
// (ie app launch) ping scenarios. The invariant is that each type of ping
// value should be present at most once per day, and can be calculated based
// on the delta between now and the last ping time (or in the case of active
// pings, that delta plus whether the app has been active).
void TestGalleryRequests(int rollcall_ping_days,
int active_ping_days,
bool active_bit,
bool expect_brand_code) {
net::TestURLFetcherFactory factory;
// Set up 2 mock extensions, one with a google.com update url and one
// without.
prefs_.reset(new TestExtensionPrefs(base::MessageLoopProxy::current()));
ServiceForManifestTests service(prefs_.get());
ExtensionList tmp;
GURL url1("http://clients2.google.com/service/update2/crx");
GURL url2("http://www.somewebsite.com");
service.CreateTestExtensions(1, 1, &tmp, &url1.possibly_invalid_spec(),
Manifest::INTERNAL);
service.CreateTestExtensions(2, 1, &tmp, &url2.possibly_invalid_spec(),
Manifest::INTERNAL);
EXPECT_EQ(2u, tmp.size());
service.set_extensions(tmp, ExtensionList());
ExtensionPrefs* prefs = service.extension_prefs();
const std::string& id = tmp[0]->id();
Time now = Time::Now();
if (rollcall_ping_days == 0) {
prefs->SetLastPingDay(id, now - TimeDelta::FromSeconds(15));
} else if (rollcall_ping_days > 0) {
Time last_ping_day = now -
TimeDelta::FromDays(rollcall_ping_days) -
TimeDelta::FromSeconds(15);
prefs->SetLastPingDay(id, last_ping_day);
}
// Store a value for the last day we sent an active ping.
if (active_ping_days == 0) {
prefs->SetLastActivePingDay(id, now - TimeDelta::FromSeconds(15));
} else if (active_ping_days > 0) {
Time last_active_ping_day = now -
TimeDelta::FromDays(active_ping_days) -
TimeDelta::FromSeconds(15);
prefs->SetLastActivePingDay(id, last_active_ping_day);
}
if (active_bit)
prefs->SetActiveBit(id, true);
ExtensionUpdater updater(&service,
service.extension_prefs(),
service.pref_service(),
service.profile(),
kUpdateFrequencySecs,
NULL,
service.GetDownloaderFactory());
ExtensionUpdater::CheckParams params;
updater.Start();
updater.CheckNow(params);
content::RunAllBlockingPoolTasksUntilIdle();
// Make the updater do manifest fetching, and note the urls it tries to
// fetch.
std::vector<GURL> fetched_urls;
net::TestURLFetcher* fetcher =
factory.GetFetcherByID(ExtensionDownloader::kManifestFetcherId);
EXPECT_TRUE(fetcher != NULL && fetcher->delegate() != NULL);
fetched_urls.push_back(fetcher->GetOriginalURL());
fetcher->set_url(fetched_urls[0]);
fetcher->set_status(net::URLRequestStatus());
fetcher->set_response_code(500);
fetcher->SetResponseString(std::string());
fetcher->delegate()->OnURLFetchComplete(fetcher);
fetcher = factory.GetFetcherByID(ExtensionDownloader::kManifestFetcherId);
fetched_urls.push_back(fetcher->GetOriginalURL());
// The urls could have been fetched in either order, so use the host to
// tell them apart and note the query each used.
GURL url1_fetch_url;
GURL url2_fetch_url;
std::string url1_query;
std::string url2_query;
if (fetched_urls[0].host() == url1.host()) {
url1_fetch_url = fetched_urls[0];
url2_fetch_url = fetched_urls[1];
url1_query = fetched_urls[0].query();
url2_query = fetched_urls[1].query();
} else if (fetched_urls[0].host() == url2.host()) {
url1_fetch_url = fetched_urls[1];
url2_fetch_url = fetched_urls[0];
url1_query = fetched_urls[1].query();
url2_query = fetched_urls[0].query();
} else {
NOTREACHED();
}
std::map<std::string, ParamsMap> url1_ping_data =
GetPingDataFromURL(url1_fetch_url);
ParamsMap url1_params = ParamsMap();
if (!url1_ping_data.empty() && ContainsKey(url1_ping_data, id))
url1_params = url1_ping_data[id];
// First make sure the non-google query had no ping parameter.
EXPECT_TRUE(GetPingDataFromURL(url2_fetch_url).empty());
// Now make sure the google query had the correct ping parameter.
bool did_rollcall = false;
if (rollcall_ping_days != 0) {
ASSERT_TRUE(ContainsKey(url1_params, "r"));
ASSERT_EQ(1u, url1_params["r"].size());
EXPECT_EQ(base::IntToString(rollcall_ping_days),
*url1_params["r"].begin());
did_rollcall = true;
}
if (active_bit && active_ping_days != 0 && did_rollcall) {
ASSERT_TRUE(ContainsKey(url1_params, "a"));
ASSERT_EQ(1u, url1_params["a"].size());
EXPECT_EQ(base::IntToString(active_ping_days),
*url1_params["a"].begin());
}
// Make sure the non-google query has no brand parameter.
const std::string brand_string = "brand%3D";
EXPECT_TRUE(url2_query.find(brand_string) == std::string::npos);
#if defined(GOOGLE_CHROME_BUILD)
// Make sure the google query has a brand parameter, but only if the
// brand is non-organic.
if (expect_brand_code) {
EXPECT_TRUE(url1_query.find(brand_string) != std::string::npos);
} else {
EXPECT_TRUE(url1_query.find(brand_string) == std::string::npos);
}
#else
// Chromium builds never add the brand to the parameter, even for google
// queries.
EXPECT_TRUE(url1_query.find(brand_string) == std::string::npos);
#endif
RunUntilIdle();
}
// This makes sure that the extension updater properly stores the results
// of a <daystart> tag from a manifest fetch in one of two cases: 1) This is
// the first time we fetched the extension, or 2) We sent a ping value of
// >= 1 day for the extension.
void TestHandleManifestResults() {
ServiceForManifestTests service(prefs_.get());
GURL update_url("http://www.google.com/manifest");
ExtensionList tmp;
service.CreateTestExtensions(1, 1, &tmp, &update_url.spec(),
Manifest::INTERNAL);
service.set_extensions(tmp, ExtensionList());
ExtensionUpdater updater(&service,
service.extension_prefs(),
service.pref_service(),
service.profile(),
kUpdateFrequencySecs,
nullptr,
service.GetDownloaderFactory());
updater.Start();
updater.EnsureDownloaderCreated();
scoped_ptr<ManifestFetchData> fetch_data(
CreateManifestFetchData(update_url));
const Extension* extension = tmp[0].get();
fetch_data->AddExtension(extension->id(),
extension->VersionString(),
&kNeverPingedData,
kEmptyUpdateUrlData,
std::string(),
false);
UpdateManifest::Results results;
results.daystart_elapsed_seconds = 750;
updater.downloader_->HandleManifestResults(*fetch_data, &results);
Time last_ping_day =
service.extension_prefs()->LastPingDay(extension->id());
EXPECT_FALSE(last_ping_day.is_null());
int64 seconds_diff = (Time::Now() - last_ping_day).InSeconds();
EXPECT_LT(seconds_diff - results.daystart_elapsed_seconds, 5);
}
// This lets us run a test with some enabled and some disabled
// extensions. The |num_enabled| value specifies how many enabled extensions
// to have, and |disabled| is a vector of DisableReason bitmasks for each
// disabled extension we want.
void TestPingMetrics(int num_enabled,
const std::vector<int>& disabled) {
ServiceForManifestTests service(prefs_.get());
ExtensionList enabled_extensions;
ExtensionList disabled_extensions;
std::string update_url = extension_urls::GetWebstoreUpdateUrl().spec();
if (num_enabled > 0)
service.CreateTestExtensions(
1, num_enabled, &enabled_extensions, &update_url, Manifest::INTERNAL);
if (disabled.size() > 0)
service.CreateTestExtensions(2,
disabled.size(),
&disabled_extensions,
&update_url,
Manifest::INTERNAL);
service.set_extensions(enabled_extensions, disabled_extensions);
ExtensionPrefs* prefs = prefs_->prefs();
for (size_t i = 0; i < disabled.size(); i++) {
int reasons = disabled[i];
const std::string& id = disabled_extensions[i]->id();
// Iterate over the DisableReason values, marking that reason in prefs
// for this id if it is set.
for (int reason = 1; reason < Extension::DISABLE_REASON_LAST;
reason <<= 1) {
if (reasons & reason)
prefs->AddDisableReason(
id, static_cast<Extension::DisableReason>(reason));
}
}
// Create the extension updater, make it issue an update, and capture the
// URL that it tried to fetch.
net::TestURLFetcherFactory factory;
ExtensionUpdater updater(&service,
service.extension_prefs(),
service.pref_service(),
service.profile(),
kUpdateFrequencySecs,
nullptr,
service.GetDownloaderFactory());
updater.Start();
SimulateTimerFired(&updater);
net::TestURLFetcher* fetcher =
factory.GetFetcherByID(ExtensionDownloader::kManifestFetcherId);
ASSERT_NE(nullptr, fetcher);
const GURL& url = fetcher->GetOriginalURL();
EXPECT_FALSE(url.is_empty());
EXPECT_TRUE(url.is_valid());
EXPECT_TRUE(url.has_query());
std::map<std::string, ParamsMap> all_pings = GetPingDataFromURL(url);
// Make sure that all the enabled extensions have "e=1" in their ping
// parameter.
for (const auto& ext : enabled_extensions) {
ASSERT_TRUE(ContainsKey(all_pings, ext->id()));
ParamsMap& ping = all_pings[ext->id()];
EXPECT_FALSE(ContainsKey(ping, "dr"));
ASSERT_TRUE(ContainsKey(ping, "e")) << url;
std::set<std::string> e = ping["e"];
ASSERT_EQ(1u, e.size()) << url;
EXPECT_EQ(std::string("1"), *e.begin()) << url;
EXPECT_FALSE(ContainsKey(ping, "dr"));
}
// Make sure that all the disable extensions have the appropriate
// "dr=<num>" values in their ping parameter if metrics are on, or omit
// it otherwise.
ASSERT_EQ(disabled_extensions.size(), disabled.size());
for (size_t i = 0; i < disabled.size(); i++) {
scoped_refptr<const Extension>& ext = disabled_extensions[i];
int disable_reasons = disabled[i];
ASSERT_TRUE(ContainsKey(all_pings, ext->id())) << url;
ParamsMap& ping = all_pings[ext->id()];
ASSERT_TRUE(ContainsKey(ping, "e")) << url;
std::set<std::string> e = ping["e"];
ASSERT_EQ(1u, e.size()) << url;
EXPECT_EQ(std::string("0"), *e.begin()) << url;
if (disable_reasons == 0) {
EXPECT_FALSE(ContainsKey(ping, "dr"));
} else {
ASSERT_TRUE(ContainsKey(ping, "dr"));
int found_reasons = 0;
for (const auto& reason_string : ping["dr"]) {
int reason = 0;
ASSERT_TRUE(base::StringToInt(reason_string, &reason));
// Make sure it's a power of 2.
ASSERT_TRUE(reason < 2 || !(reason & (reason - 1))) << reason;
found_reasons |= reason;
}
EXPECT_EQ(disable_reasons, found_reasons);
}
}
}
protected:
scoped_ptr<TestExtensionPrefs> prefs_;
ManifestFetchData* CreateManifestFetchData(const GURL& update_url) {
return new ManifestFetchData(update_url, 0, "",
UpdateQueryParams::Get(UpdateQueryParams::CRX),
ManifestFetchData::PING);
}
private:
content::TestBrowserThreadBundle thread_bundle_;
content::InProcessUtilityThreadHelper in_process_utility_thread_helper_;
#if defined OS_CHROMEOS
chromeos::ScopedTestDeviceSettingsService test_device_settings_service_;
chromeos::ScopedTestCrosSettings test_cros_settings_;
chromeos::ScopedTestUserManager test_user_manager_;
#endif
};
// Because we test some private methods of ExtensionUpdater, it's easier for the
// actual test code to live in ExtenionUpdaterTest methods instead of TEST_F
// subclasses where friendship with ExtenionUpdater is not inherited.
TEST_F(ExtensionUpdaterTest, TestExtensionUpdateCheckRequests) {
TestExtensionUpdateCheckRequests(false);
}
TEST_F(ExtensionUpdaterTest, TestExtensionUpdateCheckRequestsPending) {
TestExtensionUpdateCheckRequests(true);
}
TEST_F(ExtensionUpdaterTest, TestUpdateUrlData) {
TestUpdateUrlDataEmpty();
TestUpdateUrlDataSimple();
TestUpdateUrlDataCompound();
TestUpdateUrlDataFromGallery(
extension_urls::GetWebstoreUpdateUrl().spec());
}
TEST_F(ExtensionUpdaterTest, TestInstallSource) {
TestInstallSource();
}
TEST_F(ExtensionUpdaterTest, TestDetermineUpdates) {
TestDetermineUpdates();
}
TEST_F(ExtensionUpdaterTest, TestDetermineUpdatesPending) {
TestDetermineUpdatesPending();
}
TEST_F(ExtensionUpdaterTest, TestMultipleManifestDownloading) {
TestMultipleManifestDownloading();
}
TEST_F(ExtensionUpdaterTest, TestSingleExtensionDownloading) {
TestSingleExtensionDownloading(false, false, false);
}
TEST_F(ExtensionUpdaterTest, TestSingleExtensionDownloadingPending) {
TestSingleExtensionDownloading(true, false, false);
}
TEST_F(ExtensionUpdaterTest, TestSingleExtensionDownloadingWithRetry) {
TestSingleExtensionDownloading(false, true, false);
}
TEST_F(ExtensionUpdaterTest, TestSingleExtensionDownloadingPendingWithRetry) {
TestSingleExtensionDownloading(true, true, false);
}
TEST_F(ExtensionUpdaterTest, TestSingleExtensionDownloadingFailure) {
TestSingleExtensionDownloading(false, false, true);
}
TEST_F(ExtensionUpdaterTest, TestSingleExtensionDownloadingFailureWithRetry) {
TestSingleExtensionDownloading(false, true, true);
}
TEST_F(ExtensionUpdaterTest, TestSingleExtensionDownloadingFailurePending) {
TestSingleExtensionDownloading(true, false, true);
}
TEST_F(ExtensionUpdaterTest, ProtectedDownloadCookieAuth) {
TestProtectedDownload(
"https://chrome.google.com/webstore/download",
false, false, // No OAuth2 support
0, 0);
}
TEST_F(ExtensionUpdaterTest, ProtectedDownloadCookieFailure) {
TestProtectedDownload(
"https://chrome.google.com/webstore/download",
false, false, // No OAuth2 support
0, -1); // max_authuser=-1 simulates no valid authuser value.
}
TEST_F(ExtensionUpdaterTest, ProtectedDownloadWithNonDefaultAuthUser1) {
TestProtectedDownload("https://google.com", false, false, 1, 1);
}
TEST_F(ExtensionUpdaterTest, ProtectedDownloadWithNonDefaultAuthUser2) {
TestProtectedDownload("https://google.com", false, false, 2, 2);
}
TEST_F(ExtensionUpdaterTest, ProtectedDownloadAuthUserExhaustionFailure) {
TestProtectedDownload("https://google.com", false, false, 2, 5);
}
TEST_F(ExtensionUpdaterTest, ProtectedDownloadWithOAuth2Token) {
TestProtectedDownload(
"https://google.com",
true, true,
0, -1);
}
TEST_F(ExtensionUpdaterTest, ProtectedDownloadWithOAuth2Failure) {
TestProtectedDownload(
"https://google.com",
true, false,
0, -1);
}
TEST_F(ExtensionUpdaterTest, ProtectedDownloadNoOAuth2WithNonGoogleDomain) {
TestProtectedDownload(
"https://not-google.com",
true, true,
0, -1);
}
TEST_F(ExtensionUpdaterTest, ProtectedDownloadFailWithoutHTTPS) {
TestProtectedDownload(
"http://google.com",
true, true,
0, 0);
}
TEST_F(ExtensionUpdaterTest, TestMultipleExtensionDownloadingUpdatesFail) {
TestMultipleExtensionDownloading(false);
}
TEST_F(ExtensionUpdaterTest, TestMultipleExtensionDownloadingUpdatesSucceed) {
TestMultipleExtensionDownloading(true);
}
TEST_F(ExtensionUpdaterTest, TestManifestRetryDownloading) {
TestManifestRetryDownloading();
}
TEST_F(ExtensionUpdaterTest, TestGalleryRequestsWithOrganicBrand) {
TestGalleryRequestsWithBrand(true);
}
TEST_F(ExtensionUpdaterTest, TestGalleryRequestsWithNonOrganicBrand) {
TestGalleryRequestsWithBrand(false);
}
TEST_F(ExtensionUpdaterTest, TestHandleManifestResults) {
TestHandleManifestResults();
}
TEST_F(ExtensionUpdaterTest, TestNonAutoUpdateableLocations) {
net::TestURLFetcherFactory factory;
ServiceForManifestTests service(prefs_.get());
ExtensionUpdater updater(&service,
service.extension_prefs(),
service.pref_service(),
service.profile(),
kUpdateFrequencySecs,
NULL,
service.GetDownloaderFactory());
MockExtensionDownloaderDelegate delegate;
service.OverrideDownloaderDelegate(&delegate);
// Non-internal non-external extensions should be rejected.
ExtensionList extensions;
service.CreateTestExtensions(1, 1, &extensions, NULL,
Manifest::INVALID_LOCATION);
service.CreateTestExtensions(2, 1, &extensions, NULL, Manifest::INTERNAL);
ASSERT_EQ(2u, extensions.size());
const std::string& updateable_id = extensions[1]->id();
// These expectations fail if the delegate's methods are invoked for the
// first extension, which has a non-matching id.
EXPECT_CALL(delegate,
GetUpdateUrlData(updateable_id)).WillOnce(Return(""));
EXPECT_CALL(delegate, GetPingDataForExtension(updateable_id, _));
service.set_extensions(extensions, ExtensionList());
ExtensionUpdater::CheckParams params;
updater.Start();
updater.CheckNow(params);
content::RunAllBlockingPoolTasksUntilIdle();
}
TEST_F(ExtensionUpdaterTest, TestUpdatingDisabledExtensions) {
net::TestURLFetcherFactory factory;
ServiceForManifestTests service(prefs_.get());
ExtensionUpdater updater(&service,
service.extension_prefs(),
service.pref_service(),
service.profile(),
kUpdateFrequencySecs,
NULL,
service.GetDownloaderFactory());
MockExtensionDownloaderDelegate delegate;
service.OverrideDownloaderDelegate(&delegate);
// Non-internal non-external extensions should be rejected.
ExtensionList enabled_extensions;
ExtensionList disabled_extensions;
service.CreateTestExtensions(1, 1, &enabled_extensions, NULL,
Manifest::INTERNAL);
service.CreateTestExtensions(2, 1, &disabled_extensions, NULL,
Manifest::INTERNAL);
ASSERT_EQ(1u, enabled_extensions.size());
ASSERT_EQ(1u, disabled_extensions.size());
const std::string& enabled_id = enabled_extensions[0]->id();
const std::string& disabled_id = disabled_extensions[0]->id();
// We expect that both enabled and disabled extensions are auto-updated.
EXPECT_CALL(delegate, GetUpdateUrlData(enabled_id)).WillOnce(Return(""));
EXPECT_CALL(delegate, GetPingDataForExtension(enabled_id, _));
EXPECT_CALL(delegate,
GetUpdateUrlData(disabled_id)).WillOnce(Return(""));
EXPECT_CALL(delegate, GetPingDataForExtension(disabled_id, _));
service.set_extensions(enabled_extensions, disabled_extensions);
ExtensionUpdater::CheckParams params;
updater.Start();
updater.CheckNow(params);
content::RunAllBlockingPoolTasksUntilIdle();
}
TEST_F(ExtensionUpdaterTest, TestManifestFetchesBuilderAddExtension) {
net::TestURLFetcherFactory factory;
MockService service(prefs_.get());
MockExtensionDownloaderDelegate delegate;
scoped_ptr<ExtensionDownloader> downloader(
new ExtensionDownloader(&delegate, service.request_context()));
EXPECT_EQ(0u, ManifestFetchersCount(downloader.get()));
// First, verify that adding valid extensions does invoke the callbacks on
// the delegate.
std::string id = crx_file::id_util::GenerateId("foo");
EXPECT_CALL(delegate, GetPingDataForExtension(id, _)).WillOnce(Return(false));
EXPECT_TRUE(
downloader->AddPendingExtension(id, GURL("http://example.com/update"),
0));
downloader->StartAllPending(NULL);
Mock::VerifyAndClearExpectations(&delegate);
EXPECT_EQ(1u, ManifestFetchersCount(downloader.get()));
// Extensions with invalid update URLs should be rejected.
id = crx_file::id_util::GenerateId("foo2");
EXPECT_FALSE(
downloader->AddPendingExtension(id, GURL("http:google.com:foo"), 0));
downloader->StartAllPending(NULL);
EXPECT_EQ(1u, ManifestFetchersCount(downloader.get()));
// Extensions with empty IDs should be rejected.
EXPECT_FALSE(downloader->AddPendingExtension(std::string(), GURL(), 0));
downloader->StartAllPending(NULL);
EXPECT_EQ(1u, ManifestFetchersCount(downloader.get()));
// TODO(akalin): Test that extensions with empty update URLs
// converted from user scripts are rejected.
// Reset the ExtensionDownloader so that it drops the current fetcher.
downloader.reset(
new ExtensionDownloader(&delegate, service.request_context()));
EXPECT_EQ(0u, ManifestFetchersCount(downloader.get()));
// Extensions with empty update URLs should have a default one
// filled in.
id = crx_file::id_util::GenerateId("foo3");
EXPECT_CALL(delegate, GetPingDataForExtension(id, _)).WillOnce(Return(false));
EXPECT_TRUE(downloader->AddPendingExtension(id, GURL(), 0));
downloader->StartAllPending(NULL);
EXPECT_EQ(1u, ManifestFetchersCount(downloader.get()));
net::TestURLFetcher* fetcher =
factory.GetFetcherByID(ExtensionDownloader::kManifestFetcherId);
ASSERT_TRUE(fetcher);
EXPECT_FALSE(fetcher->GetOriginalURL().is_empty());
}
TEST_F(ExtensionUpdaterTest, TestStartUpdateCheckMemory) {
net::TestURLFetcherFactory factory;
MockService service(prefs_.get());
MockExtensionDownloaderDelegate delegate;
ExtensionDownloader downloader(&delegate, service.request_context());
StartUpdateCheck(&downloader,
CreateManifestFetchData(GURL("http://localhost/foo")));
// This should delete the newly-created ManifestFetchData.
StartUpdateCheck(&downloader,
CreateManifestFetchData(GURL("http://localhost/foo")));
// This should add into |manifests_pending_|.
StartUpdateCheck(&downloader,
CreateManifestFetchData(GURL("http://www.google.com")));
// The dtor of |downloader| should delete the pending fetchers.
}
TEST_F(ExtensionUpdaterTest, TestCheckSoon) {
ServiceForManifestTests service(prefs_.get());
net::TestURLFetcherFactory factory;
ExtensionUpdater updater(&service,
service.extension_prefs(),
service.pref_service(),
service.profile(),
kUpdateFrequencySecs,
NULL,
service.GetDownloaderFactory());
EXPECT_FALSE(updater.WillCheckSoon());
updater.Start();
EXPECT_FALSE(updater.WillCheckSoon());
updater.CheckSoon();
EXPECT_TRUE(updater.WillCheckSoon());
updater.CheckSoon();
EXPECT_TRUE(updater.WillCheckSoon());
RunUntilIdle();
EXPECT_FALSE(updater.WillCheckSoon());
updater.CheckSoon();
EXPECT_TRUE(updater.WillCheckSoon());
updater.Stop();
EXPECT_FALSE(updater.WillCheckSoon());
}
TEST_F(ExtensionUpdaterTest, TestDisabledReasons1) {
std::vector<int> disabled;
disabled.push_back(Extension::DISABLE_USER_ACTION);
disabled.push_back(Extension::DISABLE_PERMISSIONS_INCREASE |
Extension::DISABLE_CORRUPTED);
TestPingMetrics(1, disabled);
}
TEST_F(ExtensionUpdaterTest, TestDisabledReasons2) {
std::vector<int> disabled;
TestPingMetrics(1, disabled);
}
TEST_F(ExtensionUpdaterTest, TestDisabledReasons3) {
std::vector<int> disabled;
disabled.push_back(0);
TestPingMetrics(0, disabled);
}
// TODO(asargent) - (http://crbug.com/12780) add tests for:
// -prodversionmin (shouldn't update if browser version too old)
// -manifests & updates arriving out of order / interleaved
// -malformed update url (empty, file://, has query, has a # fragment, etc.)
// -An extension gets uninstalled while updates are in progress (so it doesn't
// "come back from the dead")
// -An extension gets manually updated to v3 while we're downloading v2 (ie
// you don't get downgraded accidentally)
// -An update manifest mentions multiple updates
} // namespace extensions
|