Home | History | Annotate | Download | only in browser
      1 // Copyright 2013 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "base/json/json_file_value_serializer.h"
      6 #include "base/message_loop/message_loop.h"
      7 #include "base/path_service.h"
      8 #include "chrome/common/chrome_paths.h"
      9 #include "content/public/test/test_browser_thread.h"
     10 #include "extensions/browser/info_map.h"
     11 #include "extensions/common/extension.h"
     12 #include "extensions/common/manifest_constants.h"
     13 #include "extensions/common/permissions/permissions_data.h"
     14 #include "testing/gtest/include/gtest/gtest.h"
     15 
     16 using content::BrowserThread;
     17 
     18 namespace keys = extensions::manifest_keys;
     19 
     20 namespace extensions {
     21 
     22 class InfoMapTest : public testing::Test {
     23  public:
     24   InfoMapTest()
     25       : ui_thread_(BrowserThread::UI, &message_loop_),
     26         io_thread_(BrowserThread::IO, &message_loop_) {}
     27 
     28  private:
     29   base::MessageLoop message_loop_;
     30   content::TestBrowserThread ui_thread_;
     31   content::TestBrowserThread io_thread_;
     32 };
     33 
     34 // Returns a barebones test Extension object with the given name.
     35 static scoped_refptr<Extension> CreateExtension(const std::string& name) {
     36 #if defined(OS_WIN)
     37   base::FilePath path(FILE_PATH_LITERAL("c:\\foo"));
     38 #elif defined(OS_POSIX)
     39   base::FilePath path(FILE_PATH_LITERAL("/foo"));
     40 #endif
     41 
     42   base::DictionaryValue manifest;
     43   manifest.SetString(keys::kVersion, "1.0.0.0");
     44   manifest.SetString(keys::kName, name);
     45 
     46   std::string error;
     47   scoped_refptr<Extension> extension =
     48       Extension::Create(path.AppendASCII(name),
     49                         Manifest::INVALID_LOCATION,
     50                         manifest,
     51                         Extension::NO_FLAGS,
     52                         &error);
     53   EXPECT_TRUE(extension.get()) << error;
     54 
     55   return extension;
     56 }
     57 
     58 static scoped_refptr<Extension> LoadManifest(const std::string& dir,
     59                                              const std::string& test_file) {
     60   base::FilePath path;
     61   PathService::Get(chrome::DIR_TEST_DATA, &path);
     62   path = path.AppendASCII("extensions").AppendASCII(dir).AppendASCII(test_file);
     63 
     64   JSONFileValueSerializer serializer(path);
     65   scoped_ptr<base::Value> result(serializer.Deserialize(NULL, NULL));
     66   if (!result)
     67     return NULL;
     68 
     69   std::string error;
     70   scoped_refptr<Extension> extension =
     71       Extension::Create(path,
     72                         Manifest::INVALID_LOCATION,
     73                         *static_cast<base::DictionaryValue*>(result.get()),
     74                         Extension::NO_FLAGS,
     75                         &error);
     76   EXPECT_TRUE(extension.get()) << error;
     77 
     78   return extension;
     79 }
     80 
     81 // Test that the InfoMap handles refcounting properly.
     82 TEST_F(InfoMapTest, RefCounting) {
     83   scoped_refptr<InfoMap> info_map(new InfoMap());
     84 
     85   // New extensions should have a single reference holding onto them.
     86   scoped_refptr<Extension> extension1(CreateExtension("extension1"));
     87   scoped_refptr<Extension> extension2(CreateExtension("extension2"));
     88   scoped_refptr<Extension> extension3(CreateExtension("extension3"));
     89   EXPECT_TRUE(extension1->HasOneRef());
     90   EXPECT_TRUE(extension2->HasOneRef());
     91   EXPECT_TRUE(extension3->HasOneRef());
     92 
     93   // Add a ref to each extension and give it to the info map.
     94   info_map->AddExtension(extension1.get(), base::Time(), false, false);
     95   info_map->AddExtension(extension2.get(), base::Time(), false, false);
     96   info_map->AddExtension(extension3.get(), base::Time(), false, false);
     97 
     98   // Release extension1, and the info map should have the only ref.
     99   const Extension* weak_extension1 = extension1.get();
    100   extension1 = NULL;
    101   EXPECT_TRUE(weak_extension1->HasOneRef());
    102 
    103   // Remove extension2, and the extension2 object should have the only ref.
    104   info_map->RemoveExtension(
    105       extension2->id(), extensions::UnloadedExtensionInfo::REASON_UNINSTALL);
    106   EXPECT_TRUE(extension2->HasOneRef());
    107 
    108   // Delete the info map, and the extension3 object should have the only ref.
    109   info_map = NULL;
    110   EXPECT_TRUE(extension3->HasOneRef());
    111 }
    112 
    113 // Tests that we can query a few extension properties from the InfoMap.
    114 TEST_F(InfoMapTest, Properties) {
    115   scoped_refptr<InfoMap> info_map(new InfoMap());
    116 
    117   scoped_refptr<Extension> extension1(CreateExtension("extension1"));
    118   scoped_refptr<Extension> extension2(CreateExtension("extension2"));
    119 
    120   info_map->AddExtension(extension1.get(), base::Time(), false, false);
    121   info_map->AddExtension(extension2.get(), base::Time(), false, false);
    122 
    123   EXPECT_EQ(2u, info_map->extensions().size());
    124   EXPECT_EQ(extension1.get(), info_map->extensions().GetByID(extension1->id()));
    125   EXPECT_EQ(extension2.get(), info_map->extensions().GetByID(extension2->id()));
    126 }
    127 
    128 // Tests CheckURLAccessToExtensionPermission given both extension and app URLs.
    129 TEST_F(InfoMapTest, CheckPermissions) {
    130   scoped_refptr<InfoMap> info_map(new InfoMap());
    131 
    132   scoped_refptr<Extension> app(
    133       LoadManifest("manifest_tests", "valid_app.json"));
    134   scoped_refptr<Extension> extension(
    135       LoadManifest("manifest_tests", "tabs_extension.json"));
    136 
    137   GURL app_url("http://www.google.com/mail/foo.html");
    138   ASSERT_TRUE(app->is_app());
    139   ASSERT_TRUE(app->web_extent().MatchesURL(app_url));
    140 
    141   info_map->AddExtension(app.get(), base::Time(), false, false);
    142   info_map->AddExtension(extension.get(), base::Time(), false, false);
    143 
    144   // The app should have the notifications permission, either from a
    145   // chrome-extension URL or from its web extent.
    146   const Extension* match = info_map->extensions().GetExtensionOrAppByURL(
    147       app->GetResourceURL("a.html"));
    148   EXPECT_TRUE(match &&
    149               match->permissions_data()->HasAPIPermission(
    150                   APIPermission::kNotification));
    151   match = info_map->extensions().GetExtensionOrAppByURL(app_url);
    152   EXPECT_TRUE(match &&
    153               match->permissions_data()->HasAPIPermission(
    154                   APIPermission::kNotification));
    155   EXPECT_FALSE(
    156       match &&
    157       match->permissions_data()->HasAPIPermission(APIPermission::kTab));
    158 
    159   // The extension should have the tabs permission.
    160   match = info_map->extensions().GetExtensionOrAppByURL(
    161       extension->GetResourceURL("a.html"));
    162   EXPECT_TRUE(match &&
    163               match->permissions_data()->HasAPIPermission(APIPermission::kTab));
    164   EXPECT_FALSE(match &&
    165                match->permissions_data()->HasAPIPermission(
    166                    APIPermission::kNotification));
    167 
    168   // Random URL should not have any permissions.
    169   GURL evil_url("http://evil.com/a.html");
    170   match = info_map->extensions().GetExtensionOrAppByURL(evil_url);
    171   EXPECT_FALSE(match);
    172 }
    173 
    174 TEST_F(InfoMapTest, TestNotificationsDisabled) {
    175   scoped_refptr<InfoMap> info_map(new InfoMap());
    176   scoped_refptr<Extension> app(LoadManifest("manifest_tests",
    177                                             "valid_app.json"));
    178   info_map->AddExtension(app.get(), base::Time(), false, false);
    179 
    180   EXPECT_FALSE(info_map->AreNotificationsDisabled(app->id()));
    181   info_map->SetNotificationsDisabled(app->id(), true);
    182   EXPECT_TRUE(info_map->AreNotificationsDisabled(app->id()));
    183   info_map->SetNotificationsDisabled(app->id(), false);
    184 }
    185 
    186 }  // namespace extensions
    187