Home | History | Annotate | Download | only in extensions
      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
      3 // found in the LICENSE file.
      4 
      5 #include "base/values.h"
      6 #include "chrome/browser/extensions/extension_special_storage_policy.h"
      7 #include "chrome/common/extensions/extension.h"
      8 #include "chrome/common/extensions/extension_constants.h"
      9 #include "testing/gtest/include/gtest/gtest.h"
     10 
     11 namespace keys = extension_manifest_keys;
     12 
     13 class ExtensionSpecialStoragePolicyTest : public testing::Test {
     14  protected:
     15   scoped_refptr<Extension> CreateProtectedApp() {
     16 #if defined(OS_WIN)
     17     FilePath path(FILE_PATH_LITERAL("c:\\foo"));
     18 #elif defined(OS_POSIX)
     19     FilePath path(FILE_PATH_LITERAL("/foo"));
     20 #endif
     21     DictionaryValue manifest;
     22     manifest.SetString(keys::kName, "Protected");
     23     manifest.SetString(keys::kVersion, "1");
     24     manifest.SetString(keys::kLaunchWebURL, "http://explicit/protected/start");
     25     ListValue* list = new ListValue();
     26     list->Append(Value::CreateStringValue("http://explicit/protected"));
     27     list->Append(Value::CreateStringValue("*://*.wildcards/protected"));
     28     manifest.Set(keys::kWebURLs, list);
     29     std::string error;
     30     scoped_refptr<Extension> protected_app = Extension::Create(
     31         path, Extension::INVALID, manifest, Extension::STRICT_ERROR_CHECKS,
     32         &error);
     33     EXPECT_TRUE(protected_app.get()) << error;
     34     return protected_app;
     35   }
     36 
     37   scoped_refptr<Extension> CreateUnlimitedApp() {
     38 #if defined(OS_WIN)
     39     FilePath path(FILE_PATH_LITERAL("c:\\bar"));
     40 #elif defined(OS_POSIX)
     41     FilePath path(FILE_PATH_LITERAL("/bar"));
     42 #endif
     43     DictionaryValue manifest;
     44     manifest.SetString(keys::kName, "Unlimited");
     45     manifest.SetString(keys::kVersion, "1");
     46     manifest.SetString(keys::kLaunchWebURL, "http://explicit/unlimited/start");
     47     ListValue* list = new ListValue();
     48     list->Append(Value::CreateStringValue("unlimitedStorage"));
     49     manifest.Set(keys::kPermissions, list);
     50     list = new ListValue();
     51     list->Append(Value::CreateStringValue("http://explicit/unlimited"));
     52     list->Append(Value::CreateStringValue("*://*.wildcards/unlimited"));
     53     manifest.Set(keys::kWebURLs, list);
     54     std::string error;
     55     scoped_refptr<Extension> unlimited_app = Extension::Create(
     56         path, Extension::INVALID, manifest, Extension::STRICT_ERROR_CHECKS,
     57         &error);
     58     EXPECT_TRUE(unlimited_app.get()) << error;
     59     return unlimited_app;
     60   }
     61 
     62   scoped_refptr<Extension> CreateComponentApp() {
     63 #if defined(OS_WIN)
     64     FilePath path(FILE_PATH_LITERAL("c:\\component"));
     65 #elif defined(OS_POSIX)
     66     FilePath path(FILE_PATH_LITERAL("/component"));
     67 #endif
     68     DictionaryValue manifest;
     69     manifest.SetString(keys::kName, "Component");
     70     manifest.SetString(keys::kVersion, "1");
     71     manifest.SetString(keys::kPublicKey,
     72         "MIGdMA0GCSqGSIb3DQEBAQUAA4GLADCBhwKBgQDOuXEIuoK1kAkBe0SKiJn/N9oNn3oU" \
     73         "xGa4dwj40MnJqPn+w0aR2vuyocm0R4Drp67aYwtLjOVPF4CICRq6ICP6eU07gGwQxGdZ" \
     74         "7HJASXV8hm0tab5I70oJmRLfFJyVAMCeWlFaOGq05v2i6EbifZM0qO5xALKNGQt+yjXi" \
     75         "5INM5wIBIw==");
     76     ListValue* list = new ListValue();
     77     list->Append(Value::CreateStringValue("unlimitedStorage"));
     78     list->Append(Value::CreateStringValue("fileSystem"));
     79     list->Append(Value::CreateStringValue("fileBrowserPrivate"));
     80     manifest.Set(keys::kPermissions, list);
     81     std::string error;
     82     scoped_refptr<Extension> component_app = Extension::Create(
     83         path, Extension::COMPONENT, manifest, Extension::STRICT_ERROR_CHECKS,
     84         &error);
     85     EXPECT_TRUE(component_app.get()) << error;
     86     return component_app;
     87   }
     88 
     89   scoped_refptr<Extension> CreateHandlerApp() {
     90 #if defined(OS_WIN)
     91     FilePath path(FILE_PATH_LITERAL("c:\\handler"));
     92 #elif defined(OS_POSIX)
     93     FilePath path(FILE_PATH_LITERAL("/handler"));
     94 #endif
     95     DictionaryValue manifest;
     96     manifest.SetString(keys::kName, "Handler");
     97     manifest.SetString(keys::kVersion, "1");
     98     manifest.SetString(keys::kPublicKey,
     99         "MIGdMA0GCSqGSIb3DQEBAQUAA4GLADCBhwKBgQChptAQ0n4R56N03nWQ1ogR7DVRBjGo" \
    100         "80Vw6G9KLjzZv44D8rq5Q5IkeQrtKgWyZfXevlsCe3LaLo18rcz8iZx6lK2xhLdUR+OR" \
    101         "jsjuBfdEL5a5cWeRTSxf75AcqndQsmpwMBdrMTCZ8jQNusUI+XlrihLNNJuI5TM4vNIN" \
    102         "I5bYFQIBIw==");
    103     ListValue* list = new ListValue();
    104     list->Append(Value::CreateStringValue("unlimitedStorage"));
    105     list->Append(Value::CreateStringValue("fileSystem"));
    106     manifest.Set(keys::kPermissions, list);
    107     std::string error;
    108     scoped_refptr<Extension> handler_app = Extension::Create(
    109         path, Extension::INVALID, manifest, Extension::STRICT_ERROR_CHECKS,
    110         &error);
    111     EXPECT_TRUE(handler_app.get()) << error;
    112     return handler_app;
    113   }
    114 };
    115 
    116 TEST_F(ExtensionSpecialStoragePolicyTest, EmptyPolicy) {
    117   const GURL kHttpUrl("http://foo");
    118   const GURL kExtensionUrl("chrome-extension://bar");
    119 
    120   scoped_refptr<ExtensionSpecialStoragePolicy> policy(
    121       new ExtensionSpecialStoragePolicy);
    122 
    123   ASSERT_FALSE(policy->IsStorageUnlimited(kHttpUrl));
    124   ASSERT_FALSE(policy->IsStorageUnlimited(kHttpUrl));  // test cached result
    125   ASSERT_FALSE(policy->IsStorageUnlimited(kExtensionUrl));
    126   ASSERT_FALSE(policy->IsStorageProtected(kHttpUrl));
    127 
    128   // This one is just based on the scheme.
    129   ASSERT_TRUE(policy->IsStorageProtected(kExtensionUrl));
    130 }
    131 
    132 
    133 TEST_F(ExtensionSpecialStoragePolicyTest, AppWithProtectedStorage) {
    134   scoped_refptr<Extension> extension(CreateProtectedApp());
    135   scoped_refptr<ExtensionSpecialStoragePolicy> policy(
    136       new ExtensionSpecialStoragePolicy);
    137   policy->GrantRightsForExtension(extension);
    138   EXPECT_FALSE(policy->IsStorageUnlimited(extension->url()));
    139   EXPECT_FALSE(policy->IsStorageUnlimited(GURL("http://explicit/")));
    140   EXPECT_TRUE(policy->IsStorageProtected(GURL("http://explicit/")));
    141   EXPECT_TRUE(policy->IsStorageProtected(GURL("http://explicit:6000/")));
    142   EXPECT_TRUE(policy->IsStorageProtected(GURL("http://foo.wildcards/")));
    143   EXPECT_TRUE(policy->IsStorageProtected(GURL("https://bar.wildcards/")));
    144   EXPECT_FALSE(policy->IsStorageProtected(GURL("http://not_listed/")));
    145 
    146   policy->RevokeRightsForExtension(extension);
    147   EXPECT_FALSE(policy->IsStorageProtected(GURL("http://explicit/")));
    148   EXPECT_FALSE(policy->IsStorageProtected(GURL("http://foo.wildcards/")));
    149   EXPECT_FALSE(policy->IsStorageProtected(GURL("https://bar.wildcards/")));
    150 }
    151 
    152 TEST_F(ExtensionSpecialStoragePolicyTest, AppWithUnlimitedStorage) {
    153   scoped_refptr<Extension> extension(CreateUnlimitedApp());
    154   scoped_refptr<ExtensionSpecialStoragePolicy> policy(
    155       new ExtensionSpecialStoragePolicy);
    156   policy->GrantRightsForExtension(extension);
    157   EXPECT_TRUE(policy->IsStorageProtected(GURL("http://explicit/")));
    158   EXPECT_TRUE(policy->IsStorageProtected(GURL("http://explicit:6000/")));
    159   EXPECT_TRUE(policy->IsStorageProtected(GURL("https://foo.wildcards/")));
    160   EXPECT_TRUE(policy->IsStorageProtected(GURL("https://foo.wildcards/")));
    161   EXPECT_TRUE(policy->IsStorageProtected(GURL("http://bar.wildcards/")));
    162   EXPECT_FALSE(policy->IsStorageProtected(GURL("http://not_listed/")));
    163   EXPECT_TRUE(policy->IsStorageUnlimited(extension->url()));
    164   EXPECT_TRUE(policy->IsStorageUnlimited(GURL("http://explicit/")));
    165   EXPECT_TRUE(policy->IsStorageUnlimited(GURL("http://explicit:6000/")));
    166   EXPECT_TRUE(policy->IsStorageUnlimited(GURL("https://foo.wildcards/")));
    167   EXPECT_TRUE(policy->IsStorageUnlimited(GURL("https://bar.wildcards/")));
    168   EXPECT_FALSE(policy->IsStorageUnlimited(GURL("http://not_listed/")));
    169 
    170   policy->RevokeRightsForExtension(extension);
    171   EXPECT_FALSE(policy->IsStorageProtected(GURL("http://explicit/")));
    172   EXPECT_FALSE(policy->IsStorageProtected(GURL("https://foo.wildcards/")));
    173   EXPECT_FALSE(policy->IsStorageProtected(GURL("https://foo.wildcards/")));
    174   EXPECT_FALSE(policy->IsStorageProtected(GURL("http://bar.wildcards/")));
    175   EXPECT_FALSE(policy->IsStorageUnlimited(GURL("http://explicit/")));
    176   EXPECT_FALSE(policy->IsStorageUnlimited(GURL("https://foo.wildcards/")));
    177   EXPECT_FALSE(policy->IsStorageUnlimited(GURL("https://bar.wildcards/")));
    178 }
    179 
    180 TEST_F(ExtensionSpecialStoragePolicyTest, OverlappingApps) {
    181   scoped_refptr<Extension> protected_app(CreateProtectedApp());
    182   scoped_refptr<Extension> unlimited_app(CreateUnlimitedApp());
    183   scoped_refptr<ExtensionSpecialStoragePolicy> policy(
    184       new ExtensionSpecialStoragePolicy);
    185   policy->GrantRightsForExtension(protected_app);
    186   policy->GrantRightsForExtension(unlimited_app);
    187 
    188   EXPECT_TRUE(policy->IsStorageProtected(GURL("http://explicit/")));
    189   EXPECT_TRUE(policy->IsStorageProtected(GURL("http://explicit:6000/")));
    190   EXPECT_TRUE(policy->IsStorageProtected(GURL("https://foo.wildcards/")));
    191   EXPECT_TRUE(policy->IsStorageProtected(GURL("https://foo.wildcards/")));
    192   EXPECT_TRUE(policy->IsStorageProtected(GURL("http://bar.wildcards/")));
    193   EXPECT_FALSE(policy->IsStorageProtected(GURL("http://not_listed/")));
    194   EXPECT_TRUE(policy->IsStorageUnlimited(GURL("http://explicit/")));
    195   EXPECT_TRUE(policy->IsStorageUnlimited(GURL("http://explicit:6000/")));
    196   EXPECT_TRUE(policy->IsStorageUnlimited(GURL("https://foo.wildcards/")));
    197   EXPECT_TRUE(policy->IsStorageUnlimited(GURL("https://bar.wildcards/")));
    198   EXPECT_FALSE(policy->IsStorageUnlimited(GURL("http://not_listed/")));
    199 
    200   policy->RevokeRightsForExtension(unlimited_app);
    201   EXPECT_FALSE(policy->IsStorageUnlimited(GURL("http://explicit/")));
    202   EXPECT_FALSE(policy->IsStorageUnlimited(GURL("https://foo.wildcards/")));
    203   EXPECT_FALSE(policy->IsStorageUnlimited(GURL("https://bar.wildcards/")));
    204   EXPECT_TRUE(policy->IsStorageProtected(GURL("http://explicit/")));
    205   EXPECT_TRUE(policy->IsStorageProtected(GURL("http://foo.wildcards/")));
    206   EXPECT_TRUE(policy->IsStorageProtected(GURL("https://bar.wildcards/")));
    207 
    208   policy->RevokeRightsForExtension(protected_app);
    209   EXPECT_FALSE(policy->IsStorageProtected(GURL("http://explicit/")));
    210   EXPECT_FALSE(policy->IsStorageProtected(GURL("http://foo.wildcards/")));
    211   EXPECT_FALSE(policy->IsStorageProtected(GURL("https://bar.wildcards/")));
    212 }
    213