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
|
// Copyright 2013 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 "chrome/browser/chromeos/extensions/external_cache.h"
#include <map>
#include <set>
#include <string>
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/run_loop.h"
#include "base/test/sequenced_worker_pool_owner.h"
#include "base/values.h"
#include "chrome/browser/extensions/external_provider_impl.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "extensions/common/extension_urls.h"
#include "net/url_request/test_url_fetcher_factory.h"
#include "net/url_request/url_fetcher_impl.h"
#include "net/url_request/url_request_test_util.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
const char kTestExtensionId1[] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
const char kTestExtensionId2[] = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb";
const char kTestExtensionId3[] = "cccccccccccccccccccccccccccccccc";
const char kTestExtensionId4[] = "dddddddddddddddddddddddddddddddd";
const char kNonWebstoreUpdateUrl[] = "https://localhost/service/update2/crx";
} // namespace
namespace chromeos {
class ExternalCacheTest : public testing::Test,
public ExternalCache::Delegate {
public:
ExternalCacheTest()
: thread_bundle_(content::TestBrowserThreadBundle::REAL_IO_THREAD) {
}
virtual ~ExternalCacheTest() {}
scoped_refptr<base::SequencedTaskRunner> background_task_runner() {
return background_task_runner_;
}
net::URLRequestContextGetter* request_context_getter() {
return request_context_getter_.get();
}
const base::DictionaryValue* provided_prefs() {
return prefs_.get();
}
// testing::Test overrides:
virtual void SetUp() override {
request_context_getter_ = new net::TestURLRequestContextGetter(
content::BrowserThread::GetMessageLoopProxyForThread(
content::BrowserThread::IO));
fetcher_factory_.reset(new net::TestURLFetcherFactory());
pool_owner_.reset(
new base::SequencedWorkerPoolOwner(3, "Background Pool"));
background_task_runner_ = pool_owner_->pool()->GetSequencedTaskRunner(
pool_owner_->pool()->GetNamedSequenceToken("background"));
}
virtual void TearDown() override {
pool_owner_->pool()->Shutdown();
base::RunLoop().RunUntilIdle();
}
// ExternalCache::Delegate:
virtual void OnExtensionListsUpdated(
const base::DictionaryValue* prefs) override {
prefs_.reset(prefs->DeepCopy());
}
virtual std::string GetInstalledExtensionVersion(
const std::string& id) override {
std::map<std::string, std::string>::iterator it =
installed_extensions_.find(id);
return it != installed_extensions_.end() ? it->second : std::string();
}
base::FilePath CreateCacheDir(bool initialized) {
EXPECT_TRUE(cache_dir_.CreateUniqueTempDir());
if (initialized)
CreateFlagFile(cache_dir_.path());
return cache_dir_.path();
}
base::FilePath CreateTempDir() {
EXPECT_TRUE(temp_dir_.CreateUniqueTempDir());
return temp_dir_.path();
}
void CreateFlagFile(const base::FilePath& dir) {
CreateFile(dir.Append(
extensions::LocalExtensionCache::kCacheReadyFlagFileName));
}
void CreateExtensionFile(const base::FilePath& dir,
const std::string& id,
const std::string& version) {
CreateFile(GetExtensionFile(dir, id, version));
}
void CreateFile(const base::FilePath& file) {
EXPECT_EQ(base::WriteFile(file, NULL, 0), 0);
}
base::FilePath GetExtensionFile(const base::FilePath& dir,
const std::string& id,
const std::string& version) {
return dir.Append(id + "-" + version + ".crx");
}
base::DictionaryValue* CreateEntryWithUpdateUrl(bool from_webstore) {
base::DictionaryValue* entry = new base::DictionaryValue;
entry->SetString(extensions::ExternalProviderImpl::kExternalUpdateUrl,
from_webstore ? extension_urls::GetWebstoreUpdateUrl().spec()
: kNonWebstoreUpdateUrl);
return entry;
}
void WaitForCompletion() {
// Wait for background task completion that sends replay to UI thread.
pool_owner_->pool()->FlushForTesting();
// Wait for UI thread task completion.
base::RunLoop().RunUntilIdle();
}
void AddInstalledExtension(const std::string& id,
const std::string& version) {
installed_extensions_[id] = version;
}
private:
content::TestBrowserThreadBundle thread_bundle_;
scoped_refptr<net::URLRequestContextGetter> request_context_getter_;
scoped_ptr<net::TestURLFetcherFactory> fetcher_factory_;
scoped_ptr<base::SequencedWorkerPoolOwner> pool_owner_;
scoped_refptr<base::SequencedTaskRunner> background_task_runner_;
base::ScopedTempDir cache_dir_;
base::ScopedTempDir temp_dir_;
scoped_ptr<base::DictionaryValue> prefs_;
std::map<std::string, std::string> installed_extensions_;
DISALLOW_COPY_AND_ASSIGN(ExternalCacheTest);
};
TEST_F(ExternalCacheTest, Basic) {
base::FilePath cache_dir(CreateCacheDir(false));
ExternalCache external_cache(cache_dir, request_context_getter(),
background_task_runner(), this, true, false);
scoped_ptr<base::DictionaryValue> prefs(new base::DictionaryValue);
base::DictionaryValue* dict = CreateEntryWithUpdateUrl(true);
prefs->Set(kTestExtensionId1, dict);
CreateExtensionFile(cache_dir, kTestExtensionId1, "1");
dict = CreateEntryWithUpdateUrl(true);
prefs->Set(kTestExtensionId2, dict);
prefs->Set(kTestExtensionId3, CreateEntryWithUpdateUrl(false));
CreateExtensionFile(cache_dir, kTestExtensionId3, "3");
prefs->Set(kTestExtensionId4, CreateEntryWithUpdateUrl(false));
external_cache.UpdateExtensionsList(prefs.Pass());
WaitForCompletion();
ASSERT_TRUE(provided_prefs());
EXPECT_EQ(provided_prefs()->size(), 2ul);
// File in cache from Webstore.
const base::DictionaryValue* entry1 = NULL;
ASSERT_TRUE(provided_prefs()->GetDictionary(kTestExtensionId1, &entry1));
EXPECT_FALSE(entry1->HasKey(
extensions::ExternalProviderImpl::kExternalUpdateUrl));
EXPECT_TRUE(entry1->HasKey(
extensions::ExternalProviderImpl::kExternalCrx));
EXPECT_TRUE(entry1->HasKey(
extensions::ExternalProviderImpl::kExternalVersion));
bool from_webstore = false;
EXPECT_TRUE(entry1->GetBoolean(
extensions::ExternalProviderImpl::kIsFromWebstore, &from_webstore));
EXPECT_TRUE(from_webstore);
// File in cache not from Webstore.
const base::DictionaryValue* entry3 = NULL;
ASSERT_TRUE(provided_prefs()->GetDictionary(kTestExtensionId3, &entry3));
EXPECT_FALSE(entry3->HasKey(
extensions::ExternalProviderImpl::kExternalUpdateUrl));
EXPECT_TRUE(entry3->HasKey(
extensions::ExternalProviderImpl::kExternalCrx));
EXPECT_TRUE(entry3->HasKey(
extensions::ExternalProviderImpl::kExternalVersion));
EXPECT_FALSE(entry3->HasKey(
extensions::ExternalProviderImpl::kIsFromWebstore));
// Update from Webstore.
base::FilePath temp_dir(CreateTempDir());
base::FilePath temp_file2 = temp_dir.Append("b.crx");
CreateFile(temp_file2);
external_cache.OnExtensionDownloadFinished(kTestExtensionId2,
temp_file2,
true,
GURL(),
"2",
extensions::ExtensionDownloaderDelegate::PingResult(),
std::set<int>());
WaitForCompletion();
EXPECT_EQ(provided_prefs()->size(), 3ul);
const base::DictionaryValue* entry2 = NULL;
ASSERT_TRUE(provided_prefs()->GetDictionary(kTestExtensionId2, &entry2));
EXPECT_FALSE(entry2->HasKey(
extensions::ExternalProviderImpl::kExternalUpdateUrl));
EXPECT_TRUE(entry2->HasKey(
extensions::ExternalProviderImpl::kExternalCrx));
EXPECT_TRUE(entry2->HasKey(
extensions::ExternalProviderImpl::kExternalVersion));
from_webstore = false;
EXPECT_TRUE(entry2->GetBoolean(
extensions::ExternalProviderImpl::kIsFromWebstore, &from_webstore));
EXPECT_TRUE(from_webstore);
EXPECT_TRUE(base::PathExists(
GetExtensionFile(cache_dir, kTestExtensionId2, "2")));
// Update not from Webstore.
base::FilePath temp_file4 = temp_dir.Append("d.crx");
CreateFile(temp_file4);
external_cache.OnExtensionDownloadFinished(kTestExtensionId4,
temp_file4,
true,
GURL(),
"4",
extensions::ExtensionDownloaderDelegate::PingResult(),
std::set<int>());
WaitForCompletion();
EXPECT_EQ(provided_prefs()->size(), 4ul);
const base::DictionaryValue* entry4 = NULL;
ASSERT_TRUE(provided_prefs()->GetDictionary(kTestExtensionId4, &entry4));
EXPECT_FALSE(entry4->HasKey(
extensions::ExternalProviderImpl::kExternalUpdateUrl));
EXPECT_TRUE(entry4->HasKey(
extensions::ExternalProviderImpl::kExternalCrx));
EXPECT_TRUE(entry4->HasKey(
extensions::ExternalProviderImpl::kExternalVersion));
EXPECT_FALSE(entry4->HasKey(
extensions::ExternalProviderImpl::kIsFromWebstore));
EXPECT_TRUE(base::PathExists(
GetExtensionFile(cache_dir, kTestExtensionId4, "4")));
// Damaged file should be removed from disk.
external_cache.OnDamagedFileDetected(
GetExtensionFile(cache_dir, kTestExtensionId2, "2"));
WaitForCompletion();
EXPECT_EQ(provided_prefs()->size(), 3ul);
EXPECT_FALSE(base::PathExists(
GetExtensionFile(cache_dir, kTestExtensionId2, "2")));
// Shutdown with callback OnExtensionListsUpdated that clears prefs.
scoped_ptr<base::DictionaryValue> empty(new base::DictionaryValue);
external_cache.Shutdown(
base::Bind(&ExternalCacheTest::OnExtensionListsUpdated,
base::Unretained(this),
base::Unretained(empty.get())));
WaitForCompletion();
EXPECT_EQ(provided_prefs()->size(), 0ul);
// After Shutdown directory shouldn't be touched.
external_cache.OnDamagedFileDetected(
GetExtensionFile(cache_dir, kTestExtensionId4, "4"));
WaitForCompletion();
EXPECT_TRUE(base::PathExists(
GetExtensionFile(cache_dir, kTestExtensionId4, "4")));
}
TEST_F(ExternalCacheTest, PreserveInstalled) {
base::FilePath cache_dir(CreateCacheDir(false));
ExternalCache external_cache(cache_dir, request_context_getter(),
background_task_runner(), this, true, false);
scoped_ptr<base::DictionaryValue> prefs(new base::DictionaryValue);
prefs->Set(kTestExtensionId1, CreateEntryWithUpdateUrl(true));
prefs->Set(kTestExtensionId2, CreateEntryWithUpdateUrl(true));
AddInstalledExtension(kTestExtensionId1, "1");
external_cache.UpdateExtensionsList(prefs.Pass());
WaitForCompletion();
ASSERT_TRUE(provided_prefs());
EXPECT_EQ(provided_prefs()->size(), 1ul);
// File not in cache but extension installed.
const base::DictionaryValue* entry1 = NULL;
ASSERT_TRUE(provided_prefs()->GetDictionary(kTestExtensionId1, &entry1));
EXPECT_TRUE(entry1->HasKey(
extensions::ExternalProviderImpl::kExternalUpdateUrl));
EXPECT_FALSE(entry1->HasKey(
extensions::ExternalProviderImpl::kExternalCrx));
EXPECT_FALSE(entry1->HasKey(
extensions::ExternalProviderImpl::kExternalVersion));
}
} // namespace chromeos
|