Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(288)

Side by Side Diff: chrome/browser/extensions/extension_service_unittest.cc

Issue 7003098: Start refractoring extension permissions into ExtensionPermissionSet. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase + small cleanup Created 9 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/extensions/extension_service_unittest.h" 5 #include "chrome/browser/extensions/extension_service_unittest.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <set> 8 #include <set>
9 #include <vector> 9 #include <vector>
10 10
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
107 std::stable_sort(ret_val.begin(), ret_val.end()); 107 std::stable_sort(ret_val.begin(), ret_val.end());
108 108
109 return ret_val; 109 return ret_val;
110 } 110 }
111 111
112 static void AddPattern(URLPatternSet* extent, const std::string& pattern) { 112 static void AddPattern(URLPatternSet* extent, const std::string& pattern) {
113 int schemes = URLPattern::SCHEME_ALL; 113 int schemes = URLPattern::SCHEME_ALL;
114 extent->AddPattern(URLPattern(schemes, pattern)); 114 extent->AddPattern(URLPattern(schemes, pattern));
115 } 115 }
116 116
117 static void AssertEqualExtents(URLPatternSet* extent1, 117 static void AssertEqualExtents(const URLPatternSet& extent1,
118 URLPatternSet* extent2) { 118 const URLPatternSet& extent2) {
119 URLPatternList patterns1 = extent1->patterns(); 119 URLPatternList patterns1 = extent1.patterns();
120 URLPatternList patterns2 = extent2->patterns(); 120 URLPatternList patterns2 = extent2.patterns();
121 std::set<std::string> strings1; 121 std::set<std::string> strings1;
122 EXPECT_EQ(patterns1.size(), patterns2.size()); 122 EXPECT_EQ(patterns1.size(), patterns2.size());
123 123
124 for (size_t i = 0; i < patterns1.size(); ++i) 124 for (size_t i = 0; i < patterns1.size(); ++i)
125 strings1.insert(patterns1.at(i).GetAsString()); 125 strings1.insert(patterns1.at(i).GetAsString());
126 126
127 std::set<std::string> strings2; 127 std::set<std::string> strings2;
128 for (size_t i = 0; i < patterns2.size(); ++i) 128 for (size_t i = 0; i < patterns2.size(); ++i)
129 strings2.insert(patterns2.at(i).GetAsString()); 129 strings2.insert(patterns2.at(i).GetAsString());
130 130
(...skipping 350 matching lines...) Expand 10 before | Expand all | Expand 10 after
481 481
482 // static 482 // static
483 void ExtensionServiceTestBase::SetUpTestCase() { 483 void ExtensionServiceTestBase::SetUpTestCase() {
484 ExtensionErrorReporter::Init(false); // no noisy errors 484 ExtensionErrorReporter::Init(false); // no noisy errors
485 } 485 }
486 486
487 void ExtensionServiceTestBase::SetUp() { 487 void ExtensionServiceTestBase::SetUp() {
488 ExtensionErrorReporter::GetInstance()->ClearErrors(); 488 ExtensionErrorReporter::GetInstance()->ClearErrors();
489 } 489 }
490 490
491 class MockAutoConfirmExtensionInstallUI : public ExtensionInstallUI {
492 public:
493 explicit MockAutoConfirmExtensionInstallUI(Profile* profile) :
494 ExtensionInstallUI(profile) {}
495
496 // Proceed without confirmation prompt.
497 virtual void ConfirmInstall(Delegate* delegate, const Extension* extension) {
498 delegate->InstallUIProceed();
499 }
500
501 virtual void OnInstallSuccess(const Extension* extension, SkBitmap* icon) {}
502 };
503
491 class ExtensionServiceTest 504 class ExtensionServiceTest
492 : public ExtensionServiceTestBase, public NotificationObserver { 505 : public ExtensionServiceTestBase, public NotificationObserver {
493 public: 506 public:
494 ExtensionServiceTest() : installed_(NULL) { 507 ExtensionServiceTest() : installed_(NULL) {
495 registrar_.Add(this, NotificationType::EXTENSION_LOADED, 508 registrar_.Add(this, NotificationType::EXTENSION_LOADED,
496 NotificationService::AllSources()); 509 NotificationService::AllSources());
497 registrar_.Add(this, NotificationType::EXTENSION_UNLOADED, 510 registrar_.Add(this, NotificationType::EXTENSION_UNLOADED,
498 NotificationService::AllSources()); 511 NotificationService::AllSources());
499 registrar_.Add(this, NotificationType::EXTENSION_INSTALLED, 512 registrar_.Add(this, NotificationType::EXTENSION_INSTALLED,
500 NotificationService::AllSources()); 513 NotificationService::AllSources());
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
562 575
563 ASSERT_TRUE(file_util::Delete(crx_path, false)); 576 ASSERT_TRUE(file_util::Delete(crx_path, false));
564 577
565 scoped_ptr<ExtensionCreator> creator(new ExtensionCreator()); 578 scoped_ptr<ExtensionCreator> creator(new ExtensionCreator());
566 ASSERT_TRUE(creator->Run(dir_path, 579 ASSERT_TRUE(creator->Run(dir_path,
567 crx_path, 580 crx_path,
568 pem_path, 581 pem_path,
569 pem_output_path)); 582 pem_output_path));
570 583
571 ASSERT_TRUE(file_util::PathExists(crx_path)); 584 ASSERT_TRUE(file_util::PathExists(crx_path));
572
573 InstallCrx(crx_path, should_succeed); 585 InstallCrx(crx_path, should_succeed);
574 } 586 }
575 587
576 void PackAndInstallCrx(const FilePath& dir_path, 588 void PackAndInstallCrx(const FilePath& dir_path,
577 bool should_succeed) { 589 bool should_succeed) {
578 PackAndInstallCrx(dir_path, FilePath(), should_succeed); 590 PackAndInstallCrx(dir_path, FilePath(), should_succeed);
579 } 591 }
580 592
581 // Create a CrxInstaller and start installation. To allow the install 593 // Create a CrxInstaller and start installation. To allow the install
582 // to happen, use loop_.RunAllPending();. Most tests will not use this 594 // to happen, use loop_.RunAllPending();. Most tests will not use this
583 // method directly. Instead, use InstallCrx(), which waits for 595 // method directly. Instead, use InstallCrx(), which waits for
584 // the crx to be installed and does extra error checking. 596 // the crx to be installed and does extra error checking.
585 void StartCrxInstall(const FilePath& crx_path) { 597 void StartCrxInstall(const FilePath& crx_path) {
586 ASSERT_TRUE(file_util::PathExists(crx_path)) 598 ASSERT_TRUE(file_util::PathExists(crx_path))
587 << "Path does not exist: "<< crx_path.value().c_str(); 599 << "Path does not exist: "<< crx_path.value().c_str();
588 // no client (silent install) 600 MockAutoConfirmExtensionInstallUI* install_ui =
589 scoped_refptr<CrxInstaller> installer(service_->MakeCrxInstaller(NULL)); 601 new MockAutoConfirmExtensionInstallUI(profile_.get());
602 scoped_refptr<CrxInstaller> installer(
603 service_->MakeCrxInstaller(install_ui));
590 installer->InstallCrx(crx_path); 604 installer->InstallCrx(crx_path);
591 } 605 }
592 606
593 void InstallCrx(const FilePath& path, 607 void InstallCrx(const FilePath& path,
594 bool should_succeed) { 608 bool should_succeed) {
595 StartCrxInstall(path); 609 StartCrxInstall(path);
596 WaitForCrxInstall(path, should_succeed); 610 WaitForCrxInstall(path, should_succeed);
597 } 611 }
598 612
599 void InstallCrxWithLocation(const FilePath& crx_path, 613 void InstallCrxWithLocation(const FilePath& crx_path,
(...skipping 426 matching lines...) Expand 10 before | Expand all | Expand 10 after
1026 EXPECT_TRUE(extension->plugins().empty()); 1040 EXPECT_TRUE(extension->plugins().empty());
1027 EXPECT_EQ(1u, scripts[1].url_patterns().size()); 1041 EXPECT_EQ(1u, scripts[1].url_patterns().size());
1028 EXPECT_EQ("http://*.news.com/*", scripts[1].url_patterns()[0].GetAsString()); 1042 EXPECT_EQ("http://*.news.com/*", scripts[1].url_patterns()[0].GetAsString());
1029 ExtensionResource resource10(extension->id(), 1043 ExtensionResource resource10(extension->id(),
1030 scripts[1].js_scripts()[0].extension_root(), 1044 scripts[1].js_scripts()[0].extension_root(),
1031 scripts[1].js_scripts()[0].relative_path()); 1045 scripts[1].js_scripts()[0].relative_path());
1032 expected_path = 1046 expected_path =
1033 extension->path().AppendASCII("js_files").AppendASCII("script3.js"); 1047 extension->path().AppendASCII("js_files").AppendASCII("script3.js");
1034 ASSERT_TRUE(file_util::AbsolutePath(&expected_path)); 1048 ASSERT_TRUE(file_util::AbsolutePath(&expected_path));
1035 EXPECT_TRUE(resource10.ComparePathWithDefault(expected_path)); 1049 EXPECT_TRUE(resource10.ComparePathWithDefault(expected_path));
1036 const URLPatternList permissions = extension->host_permissions(); 1050 const URLPatternList permissions =
1051 extension->permission_set()->explicit_hosts().patterns();
1037 ASSERT_EQ(2u, permissions.size()); 1052 ASSERT_EQ(2u, permissions.size());
1038 EXPECT_EQ("http://*.google.com/*", permissions[0].GetAsString()); 1053 EXPECT_EQ("http://*.google.com/*", permissions[0].GetAsString());
1039 EXPECT_EQ("https://*.google.com/*", permissions[1].GetAsString()); 1054 EXPECT_EQ("https://*.google.com/*", permissions[1].GetAsString());
1040 1055
1041 EXPECT_EQ(std::string(good1), loaded_[1]->id()); 1056 EXPECT_EQ(std::string(good1), loaded_[1]->id());
1042 EXPECT_EQ(std::string("My extension 2"), loaded_[1]->name()); 1057 EXPECT_EQ(std::string("My extension 2"), loaded_[1]->name());
1043 EXPECT_EQ(std::string(""), loaded_[1]->description()); 1058 EXPECT_EQ(std::string(""), loaded_[1]->description());
1044 EXPECT_EQ(loaded_[1]->GetResourceURL("background.html"), 1059 EXPECT_EQ(loaded_[1]->GetResourceURL("background.html"),
1045 loaded_[1]->background_url()); 1060 loaded_[1]->background_url());
1046 EXPECT_EQ(0u, loaded_[1]->content_scripts().size()); 1061 EXPECT_EQ(0u, loaded_[1]->content_scripts().size());
(...skipping 282 matching lines...) Expand 10 before | Expand all | Expand 10 after
1329 // Install a user script (they get converted automatically to an extension) 1344 // Install a user script (they get converted automatically to an extension)
1330 TEST_F(ExtensionServiceTest, InstallUserScript) { 1345 TEST_F(ExtensionServiceTest, InstallUserScript) {
1331 // The details of script conversion are tested elsewhere, this just tests 1346 // The details of script conversion are tested elsewhere, this just tests
1332 // integration with ExtensionService. 1347 // integration with ExtensionService.
1333 InitializeEmptyExtensionService(); 1348 InitializeEmptyExtensionService();
1334 1349
1335 FilePath path = data_dir_ 1350 FilePath path = data_dir_
1336 .AppendASCII("user_script_basic.user.js"); 1351 .AppendASCII("user_script_basic.user.js");
1337 1352
1338 ASSERT_TRUE(file_util::PathExists(path)); 1353 ASSERT_TRUE(file_util::PathExists(path));
1339 // Pass NULL to install silently. 1354 // Use the MockAutoConfirmExtensionInstallUI to silently install.
1340 scoped_refptr<CrxInstaller> installer(service_->MakeCrxInstaller(NULL)); 1355 MockAutoConfirmExtensionInstallUI* install_ui =
1356 new MockAutoConfirmExtensionInstallUI(profile_.get());
1357 scoped_refptr<CrxInstaller> installer(
1358 service_->MakeCrxInstaller(install_ui));
1341 installer->InstallUserScript( 1359 installer->InstallUserScript(
1342 path, 1360 path,
1343 GURL("http://www.aaronboodman.com/scripts/user_script_basic.user.js")); 1361 GURL("http://www.aaronboodman.com/scripts/user_script_basic.user.js"));
1344 1362
1345 loop_.RunAllPending(); 1363 loop_.RunAllPending();
1346 std::vector<std::string> errors = GetErrors(); 1364 std::vector<std::string> errors = GetErrors();
1347 EXPECT_TRUE(installed_) << "Nothing was installed."; 1365 EXPECT_TRUE(installed_) << "Nothing was installed.";
1348 ASSERT_EQ(1u, loaded_.size()) << "Nothing was loaded."; 1366 ASSERT_EQ(1u, loaded_.size()) << "Nothing was loaded.";
1349 EXPECT_EQ(0u, errors.size()) << "There were errors: " 1367 EXPECT_EQ(0u, errors.size()) << "There were errors: "
1350 << JoinString(errors, ','); 1368 << JoinString(errors, ',');
(...skipping 13 matching lines...) Expand all
1364 .AppendASCII("permissions"); 1382 .AppendASCII("permissions");
1365 1383
1366 FilePath pem_path = path.AppendASCII("unknown.pem"); 1384 FilePath pem_path = path.AppendASCII("unknown.pem");
1367 path = path.AppendASCII("unknown"); 1385 path = path.AppendASCII("unknown");
1368 1386
1369 ASSERT_TRUE(file_util::PathExists(pem_path)); 1387 ASSERT_TRUE(file_util::PathExists(pem_path));
1370 ASSERT_TRUE(file_util::PathExists(path)); 1388 ASSERT_TRUE(file_util::PathExists(path));
1371 1389
1372 ExtensionPrefs* prefs = service_->extension_prefs(); 1390 ExtensionPrefs* prefs = service_->extension_prefs();
1373 1391
1374 std::set<std::string> expected_api_perms; 1392 ExtensionAPIPermissionSet expected_api_perms;
1375 std::set<std::string> known_api_perms;
1376 bool full_access;
1377 URLPatternSet expected_host_perms; 1393 URLPatternSet expected_host_perms;
1378 URLPatternSet known_host_perms;
1379 1394
1380 // Make sure there aren't any granted permissions before the 1395 // Make sure there aren't any granted permissions before the
1381 // extension is installed. 1396 // extension is installed.
1382 EXPECT_FALSE(prefs->GetGrantedPermissions( 1397 scoped_ptr<ExtensionPermissionSet> known_perms(
1383 permissions_crx, &full_access, &known_api_perms, &known_host_perms)); 1398 prefs->GetGrantedPermissions(permissions_crx));
1384 EXPECT_TRUE(known_api_perms.empty()); 1399 EXPECT_FALSE(known_perms.get());
1385 EXPECT_TRUE(known_host_perms.is_empty());
1386 1400
1387 PackAndInstallCrx(path, pem_path, true); 1401 PackAndInstallCrx(path, pem_path, true);
1388 1402
1389 EXPECT_EQ(0u, GetErrors().size()); 1403 EXPECT_EQ(0u, GetErrors().size());
1390 ASSERT_EQ(1u, service_->extensions()->size()); 1404 ASSERT_EQ(1u, service_->extensions()->size());
1391 std::string extension_id = service_->extensions()->at(0)->id(); 1405 std::string extension_id = service_->extensions()->at(0)->id();
1392 EXPECT_EQ(permissions_crx, extension_id); 1406 EXPECT_EQ(permissions_crx, extension_id);
1393 1407
1394
1395 // Verify that the valid API permissions have been recognized. 1408 // Verify that the valid API permissions have been recognized.
1396 expected_api_perms.insert("tabs"); 1409 expected_api_perms.insert(ExtensionAPIPermission::kTab);
1397 1410
1398 AddPattern(&expected_host_perms, "http://*.google.com/*"); 1411 AddPattern(&expected_host_perms, "http://*.google.com/*");
1399 AddPattern(&expected_host_perms, "https://*.google.com/*"); 1412 AddPattern(&expected_host_perms, "https://*.google.com/*");
1400 AddPattern(&expected_host_perms, "http://*.google.com.hk/*"); 1413 AddPattern(&expected_host_perms, "http://*.google.com.hk/*");
1401 AddPattern(&expected_host_perms, "http://www.example.com/*"); 1414 AddPattern(&expected_host_perms, "http://www.example.com/*");
1402 1415
1403 EXPECT_TRUE(prefs->GetGrantedPermissions(extension_id, 1416 known_perms.reset(prefs->GetGrantedPermissions(extension_id));
1404 &full_access, 1417 EXPECT_TRUE(known_perms.get());
1405 &known_api_perms, 1418 EXPECT_FALSE(known_perms->IsEmpty());
1406 &known_host_perms)); 1419 EXPECT_EQ(expected_api_perms, known_perms->apis());
1407 1420 EXPECT_FALSE(known_perms->HasEffectiveFullAccess());
1408 EXPECT_EQ(expected_api_perms, known_api_perms); 1421 AssertEqualExtents(expected_host_perms, known_perms->effective_hosts());
1409 EXPECT_FALSE(full_access);
1410 AssertEqualExtents(&expected_host_perms, &known_host_perms);
1411 } 1422 }
1412 1423
1413 #if !defined(OS_CHROMEOS) 1424 #if !defined(OS_CHROMEOS)
1414 // Tests that the granted permissions full_access bit gets set correctly when 1425 // Tests that the granted permissions full_access bit gets set correctly when
1415 // an extension contains an NPAPI plugin. Don't run this test on Chrome OS 1426 // an extension contains an NPAPI plugin. Don't run this test on Chrome OS
1416 // since they don't support plugins. 1427 // since they don't support plugins.
1417 TEST_F(ExtensionServiceTest, GrantedFullAccessPermissions) { 1428 TEST_F(ExtensionServiceTest, GrantedFullAccessPermissions) {
1418 InitializeEmptyExtensionService(); 1429 InitializeEmptyExtensionService();
1419 1430
1420 FilePath path = data_dir_ 1431 FilePath path = data_dir_
1421 .AppendASCII("good") 1432 .AppendASCII("good")
1422 .AppendASCII("Extensions") 1433 .AppendASCII("Extensions")
1423 .AppendASCII(good1) 1434 .AppendASCII(good1)
1424 .AppendASCII("2"); 1435 .AppendASCII("2");
1425 1436
1426 ASSERT_TRUE(file_util::PathExists(path)); 1437 ASSERT_TRUE(file_util::PathExists(path));
1427
1428 PackAndInstallCrx(path, true); 1438 PackAndInstallCrx(path, true);
1429
1430 EXPECT_EQ(0u, GetErrors().size()); 1439 EXPECT_EQ(0u, GetErrors().size());
1431 EXPECT_EQ(1u, service_->extensions()->size()); 1440 EXPECT_EQ(1u, service_->extensions()->size());
1432 const Extension* extension = service_->extensions()->at(0); 1441 const Extension* extension = service_->extensions()->at(0);
1433 std::string extension_id = extension->id(); 1442 std::string extension_id = extension->id();
1434 ExtensionPrefs* prefs = service_->extension_prefs(); 1443 ExtensionPrefs* prefs = service_->extension_prefs();
1435 1444
1436 bool full_access; 1445 scoped_ptr<ExtensionPermissionSet> permissions(
1437 std::set<std::string> api_permissions; 1446 prefs->GetGrantedPermissions(extension_id));
1438 URLPatternSet host_permissions; 1447 EXPECT_FALSE(permissions->IsEmpty());
1439 EXPECT_TRUE(prefs->GetGrantedPermissions( 1448 EXPECT_TRUE(permissions->HasEffectiveFullAccess());
1440 extension_id, &full_access, &api_permissions, &host_permissions)); 1449 EXPECT_FALSE(permissions->apis().empty());
1450 EXPECT_TRUE(permissions->HasAPIPermission(ExtensionAPIPermission::kPlugin));
1441 1451
1442 EXPECT_TRUE(full_access); 1452 // Full access implies full host access too...
1443 EXPECT_TRUE(api_permissions.empty()); 1453 EXPECT_TRUE(permissions->HasEffectiveAccessToAllHosts());
1444 EXPECT_TRUE(host_permissions.is_empty());
1445 } 1454 }
1446 #endif 1455 #endif
1447 1456
1448 // Tests that the extension is disabled when permissions are missing from 1457 // Tests that the extension is disabled when permissions are missing from
1449 // the extension's granted permissions preferences. (This simulates updating 1458 // the extension's granted permissions preferences. (This simulates updating
1450 // the browser to a version which recognizes more permissions). 1459 // the browser to a version which recognizes more permissions).
1451 TEST_F(ExtensionServiceTest, GrantedAPIAndHostPermissions) { 1460 TEST_F(ExtensionServiceTest, GrantedAPIAndHostPermissions) {
1452 InitializeEmptyExtensionService(); 1461 InitializeEmptyExtensionService();
1453 1462
1454 FilePath path = data_dir_ 1463 FilePath path = data_dir_
1455 .AppendASCII("permissions") 1464 .AppendASCII("permissions")
1456 .AppendASCII("unknown"); 1465 .AppendASCII("unknown");
1457 1466
1458 ASSERT_TRUE(file_util::PathExists(path)); 1467 ASSERT_TRUE(file_util::PathExists(path));
1459 1468
1460 PackAndInstallCrx(path, true); 1469 PackAndInstallCrx(path, true);
1461 1470
1462 EXPECT_EQ(0u, GetErrors().size()); 1471 EXPECT_EQ(0u, GetErrors().size());
1463 EXPECT_EQ(1u, service_->extensions()->size()); 1472 EXPECT_EQ(1u, service_->extensions()->size());
1464 const Extension* extension = service_->extensions()->at(0); 1473 const Extension* extension = service_->extensions()->at(0);
1465 std::string extension_id = extension->id(); 1474 std::string extension_id = extension->id();
1466 1475
1467 ExtensionPrefs* prefs = service_->extension_prefs(); 1476 ExtensionPrefs* prefs = service_->extension_prefs();
1468 1477
1469 std::set<std::string> expected_api_permissions; 1478 ExtensionAPIPermissionSet expected_api_permissions;
1470 URLPatternSet expected_host_permissions; 1479 URLPatternSet expected_host_permissions;
1471 1480
1472 expected_api_permissions.insert("tabs"); 1481 expected_api_permissions.insert(ExtensionAPIPermission::kTab);
1473 AddPattern(&expected_host_permissions, "http://*.google.com/*"); 1482 AddPattern(&expected_host_permissions, "http://*.google.com/*");
1474 AddPattern(&expected_host_permissions, "https://*.google.com/*"); 1483 AddPattern(&expected_host_permissions, "https://*.google.com/*");
1475 AddPattern(&expected_host_permissions, "http://*.google.com.hk/*"); 1484 AddPattern(&expected_host_permissions, "http://*.google.com.hk/*");
1476 AddPattern(&expected_host_permissions, "http://www.example.com/*"); 1485 AddPattern(&expected_host_permissions, "http://www.example.com/*");
1477 1486
1478 std::set<std::string> api_permissions;
1479 std::set<std::string> host_permissions; 1487 std::set<std::string> host_permissions;
1480 1488
1481 // Test that the extension is disabled when an API permission is missing from 1489 // Test that the extension is disabled when an API permission is missing from
1482 // the extension's granted api permissions preference. (This simulates 1490 // the extension's granted api permissions preference. (This simulates
1483 // updating the browser to a version which recognizes a new API permission). 1491 // updating the browser to a version which recognizes a new API permission).
1484 SetPrefStringSet(extension_id, "granted_permissions.api", api_permissions); 1492 SetPref(extension_id, "granted_permissions.api",
1485 1493 new ListValue(), "granted_permissions.api");
1486 service_->ReloadExtensions(); 1494 service_->ReloadExtensions();
1487 1495
1488 EXPECT_EQ(1u, service_->disabled_extensions()->size()); 1496 EXPECT_EQ(1u, service_->disabled_extensions()->size());
1489 extension = service_->disabled_extensions()->at(0); 1497 extension = service_->disabled_extensions()->at(0);
1490 1498
1491 ASSERT_TRUE(prefs->GetExtensionState(extension_id) == Extension::DISABLED); 1499 ASSERT_TRUE(prefs->GetExtensionState(extension_id) == Extension::DISABLED);
1492 ASSERT_TRUE(prefs->DidExtensionEscalatePermissions(extension_id)); 1500 ASSERT_TRUE(prefs->DidExtensionEscalatePermissions(extension_id));
1493 1501
1494 // Now grant and re-enable the extension, making sure the prefs are updated. 1502 // Now grant and re-enable the extension, making sure the prefs are updated.
1495 service_->GrantPermissionsAndEnableExtension(extension); 1503 service_->GrantPermissionsAndEnableExtension(extension);
1496 1504
1497 ASSERT_TRUE(prefs->GetExtensionState(extension_id) == Extension::ENABLED); 1505 ASSERT_TRUE(prefs->GetExtensionState(extension_id) == Extension::ENABLED);
1498 ASSERT_FALSE(prefs->DidExtensionEscalatePermissions(extension_id)); 1506 ASSERT_FALSE(prefs->DidExtensionEscalatePermissions(extension_id));
1499 1507
1500 std::set<std::string> current_api_permissions; 1508 scoped_ptr<ExtensionPermissionSet> current_perms(
1501 URLPatternSet current_host_permissions; 1509 prefs->GetGrantedPermissions(extension_id));
1502 bool current_full_access; 1510 ASSERT_TRUE(current_perms.get());
1503 1511 ASSERT_FALSE(current_perms->IsEmpty());
1504 ASSERT_TRUE(prefs->GetGrantedPermissions(extension_id, 1512 ASSERT_FALSE(current_perms->HasEffectiveFullAccess());
1505 &current_full_access, 1513 ASSERT_EQ(expected_api_permissions, current_perms->apis());
1506 &current_api_permissions, 1514 AssertEqualExtents(expected_host_permissions,
1507 &current_host_permissions)); 1515 current_perms->effective_hosts());
1508
1509 ASSERT_FALSE(current_full_access);
1510 ASSERT_EQ(expected_api_permissions, current_api_permissions);
1511 AssertEqualExtents(&expected_host_permissions, &current_host_permissions);
1512 1516
1513 // Tests that the extension is disabled when a host permission is missing from 1517 // Tests that the extension is disabled when a host permission is missing from
1514 // the extension's granted host permissions preference. (This simulates 1518 // the extension's granted host permissions preference. (This simulates
1515 // updating the browser to a version which recognizes additional host 1519 // updating the browser to a version which recognizes additional host
1516 // permissions). 1520 // permissions).
1517 api_permissions.clear();
1518 host_permissions.clear(); 1521 host_permissions.clear();
1519 current_api_permissions.clear(); 1522 current_perms.reset();
1520 current_host_permissions.ClearPatterns();
1521 1523
1522 api_permissions.insert("tabs");
1523 host_permissions.insert("http://*.google.com/*"); 1524 host_permissions.insert("http://*.google.com/*");
1524 host_permissions.insert("https://*.google.com/*"); 1525 host_permissions.insert("https://*.google.com/*");
1525 host_permissions.insert("http://*.google.com.hk/*"); 1526 host_permissions.insert("http://*.google.com.hk/*");
1526 1527
1527 SetPrefStringSet(extension_id, "granted_permissions.api", api_permissions); 1528 ListValue* api_permissions = new ListValue();
1528 SetPrefStringSet(extension_id, "granted_permissions.host", host_permissions); 1529 api_permissions->Append(
1530 Value::CreateIntegerValue(ExtensionAPIPermission::kTab));
1531 SetPref(extension_id, "granted_permissions.api",
1532 api_permissions, "granted_permissions.api");
1533 SetPrefStringSet(
1534 extension_id, "granted_permissions.scriptable_host", host_permissions);
1529 1535
1530 service_->ReloadExtensions(); 1536 service_->ReloadExtensions();
1531 1537
1532 EXPECT_EQ(1u, service_->disabled_extensions()->size()); 1538 EXPECT_EQ(1u, service_->disabled_extensions()->size());
1533 extension = service_->disabled_extensions()->at(0); 1539 extension = service_->disabled_extensions()->at(0);
1534 1540
1535 ASSERT_TRUE(prefs->GetExtensionState(extension_id) == Extension::DISABLED); 1541 ASSERT_TRUE(prefs->GetExtensionState(extension_id) == Extension::DISABLED);
1536 ASSERT_TRUE(prefs->DidExtensionEscalatePermissions(extension_id)); 1542 ASSERT_TRUE(prefs->DidExtensionEscalatePermissions(extension_id));
1537 1543
1538 // Now grant and re-enable the extension, making sure the prefs are updated. 1544 // Now grant and re-enable the extension, making sure the prefs are updated.
1539 service_->GrantPermissionsAndEnableExtension(extension); 1545 service_->GrantPermissionsAndEnableExtension(extension);
1540 1546
1541 ASSERT_TRUE(prefs->GetExtensionState(extension_id) == Extension::ENABLED); 1547 ASSERT_TRUE(prefs->GetExtensionState(extension_id) == Extension::ENABLED);
1542 ASSERT_FALSE(prefs->DidExtensionEscalatePermissions(extension_id)); 1548 ASSERT_FALSE(prefs->DidExtensionEscalatePermissions(extension_id));
1543 1549
1544 ASSERT_TRUE(prefs->GetGrantedPermissions(extension_id, 1550 current_perms.reset(prefs->GetGrantedPermissions(extension_id));
1545 &current_full_access, 1551 ASSERT_TRUE(current_perms.get());
1546 &current_api_permissions, 1552 ASSERT_FALSE(current_perms->IsEmpty());
1547 &current_host_permissions)); 1553 ASSERT_FALSE(current_perms->HasEffectiveFullAccess());
1548 1554 ASSERT_EQ(expected_api_permissions, current_perms->apis());
1549 ASSERT_FALSE(current_full_access); 1555 AssertEqualExtents(expected_host_permissions,
1550 ASSERT_EQ(expected_api_permissions, current_api_permissions); 1556 current_perms->effective_hosts());
1551 AssertEqualExtents(&expected_host_permissions, &current_host_permissions);
1552
1553 // Tests that the granted permissions preferences are initialized when
1554 // migrating from the old pref schema.
1555 current_api_permissions.clear();
1556 current_host_permissions.ClearPatterns();
1557
1558 ClearPref(extension_id, "granted_permissions");
1559
1560 service_->ReloadExtensions();
1561
1562 EXPECT_EQ(1u, service_->extensions()->size());
1563 extension = service_->extensions()->at(0);
1564
1565 ASSERT_TRUE(prefs->GetExtensionState(extension_id) == Extension::ENABLED);
1566 ASSERT_FALSE(prefs->DidExtensionEscalatePermissions(extension_id));
1567
1568 ASSERT_TRUE(prefs->GetGrantedPermissions(extension_id,
1569 &current_full_access,
1570 &current_api_permissions,
1571 &current_host_permissions));
1572
1573 ASSERT_FALSE(current_full_access);
1574 ASSERT_EQ(expected_api_permissions, current_api_permissions);
1575 AssertEqualExtents(&expected_host_permissions, &current_host_permissions);
1576 } 1557 }
1577 1558
1578 // Test Packaging and installing an extension. 1559 // Test Packaging and installing an extension.
1579 TEST_F(ExtensionServiceTest, PackExtension) { 1560 TEST_F(ExtensionServiceTest, PackExtension) {
1580 InitializeEmptyExtensionService(); 1561 InitializeEmptyExtensionService();
1581 FilePath input_directory = data_dir_ 1562 FilePath input_directory = data_dir_
1582 .AppendASCII("good") 1563 .AppendASCII("good")
1583 .AppendASCII("Extensions") 1564 .AppendASCII("Extensions")
1584 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj") 1565 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj")
1585 .AppendASCII("1.0.0.0"); 1566 .AppendASCII("1.0.0.0");
(...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after
1827 EXPECT_TRUE(service_->extensions()->empty()); 1808 EXPECT_TRUE(service_->extensions()->empty());
1828 1809
1829 int pref_count = 0; 1810 int pref_count = 0;
1830 1811
1831 // Install app1 with unlimited storage. 1812 // Install app1 with unlimited storage.
1832 PackAndInstallCrx(data_dir_.AppendASCII("app1"), true); 1813 PackAndInstallCrx(data_dir_.AppendASCII("app1"), true);
1833 ValidatePrefKeyCount(++pref_count); 1814 ValidatePrefKeyCount(++pref_count);
1834 ASSERT_EQ(1u, service_->extensions()->size()); 1815 ASSERT_EQ(1u, service_->extensions()->size());
1835 const Extension* extension = service_->extensions()->at(0); 1816 const Extension* extension = service_->extensions()->at(0);
1836 const std::string id1 = extension->id(); 1817 const std::string id1 = extension->id();
1837 EXPECT_TRUE(extension->HasApiPermission( 1818 EXPECT_TRUE(extension->HasAPIPermission(
1838 Extension::kUnlimitedStoragePermission)); 1819 ExtensionAPIPermission::kUnlimitedStorage));
1839 EXPECT_TRUE(extension->web_extent().MatchesURL( 1820 EXPECT_TRUE(extension->web_extent().MatchesURL(
1840 extension->GetFullLaunchURL())); 1821 extension->GetFullLaunchURL()));
1841 const GURL origin1(extension->GetFullLaunchURL().GetOrigin()); 1822 const GURL origin1(extension->GetFullLaunchURL().GetOrigin());
1842 EXPECT_TRUE(profile_->GetExtensionSpecialStoragePolicy()-> 1823 EXPECT_TRUE(profile_->GetExtensionSpecialStoragePolicy()->
1843 IsStorageUnlimited(origin1)); 1824 IsStorageUnlimited(origin1));
1844 1825
1845 // Install app2 from the same origin with unlimited storage. 1826 // Install app2 from the same origin with unlimited storage.
1846 PackAndInstallCrx(data_dir_.AppendASCII("app2"), true); 1827 PackAndInstallCrx(data_dir_.AppendASCII("app2"), true);
1847 ValidatePrefKeyCount(++pref_count); 1828 ValidatePrefKeyCount(++pref_count);
1848 ASSERT_EQ(2u, service_->extensions()->size()); 1829 ASSERT_EQ(2u, service_->extensions()->size());
1849 extension = service_->extensions()->at(1); 1830 extension = service_->extensions()->at(1);
1850 const std::string id2 = extension->id(); 1831 const std::string id2 = extension->id();
1851 EXPECT_TRUE(extension->HasApiPermission( 1832 EXPECT_TRUE(extension->HasAPIPermission(
1852 Extension::kUnlimitedStoragePermission)); 1833 ExtensionAPIPermission::kUnlimitedStorage));
1853 EXPECT_TRUE(extension->web_extent().MatchesURL( 1834 EXPECT_TRUE(extension->web_extent().MatchesURL(
1854 extension->GetFullLaunchURL())); 1835 extension->GetFullLaunchURL()));
1855 const GURL origin2(extension->GetFullLaunchURL().GetOrigin()); 1836 const GURL origin2(extension->GetFullLaunchURL().GetOrigin());
1856 EXPECT_EQ(origin1, origin2); 1837 EXPECT_EQ(origin1, origin2);
1857 EXPECT_TRUE(profile_->GetExtensionSpecialStoragePolicy()-> 1838 EXPECT_TRUE(profile_->GetExtensionSpecialStoragePolicy()->
1858 IsStorageUnlimited(origin2)); 1839 IsStorageUnlimited(origin2));
1859 1840
1860 1841
1861 // Uninstall one of them, unlimited storage should still be granted 1842 // Uninstall one of them, unlimited storage should still be granted
1862 // to the origin. 1843 // to the origin.
(...skipping 1937 matching lines...) Expand 10 before | Expand all | Expand 10 after
3800 ASSERT_FALSE(AddPendingSyncInstall()); 3781 ASSERT_FALSE(AddPendingSyncInstall());
3801 3782
3802 // Wait for the external source to install. 3783 // Wait for the external source to install.
3803 WaitForCrxInstall(crx_path_, true); 3784 WaitForCrxInstall(crx_path_, true);
3804 ASSERT_TRUE(IsCrxInstalled()); 3785 ASSERT_TRUE(IsCrxInstalled());
3805 3786
3806 // Now that the extension is installed, sync request should fail 3787 // Now that the extension is installed, sync request should fail
3807 // because the extension is already installed. 3788 // because the extension is already installed.
3808 ASSERT_FALSE(AddPendingSyncInstall()); 3789 ASSERT_FALSE(AddPendingSyncInstall());
3809 } 3790 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698