1 // Copyright (c) 2015 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.
8 #include "base/files/file_util.h"
9 #include "base/files/scoped_temp_dir.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/run_loop.h"
12 #include "base/time/time.h"
13 #include "chrome/browser/chromeos/settings/scoped_cros_settings_test_helper.h"
14 #include "chrome/browser/extensions/updater/chromeos_extension_cache_delegate.h"
15 #include "chrome/browser/extensions/updater/extension_cache_impl.h"
16 #include "chrome/browser/extensions/updater/local_extension_cache.h"
17 #include "chromeos/settings/cros_settings_names.h"
18 #include "content/public/test/test_browser_thread_bundle.h"
19 #include "testing/gtest/include/gtest/gtest.h"
21 namespace extensions
{
24 const char kTestExtensionId1
[] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
25 const char kTestExtensionId2
[] = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb";
26 const char kTestExtensionId3
[] = "cccccccccccccccccccccccccccccccc";
28 // 2 megabytes are a little less than 2 100 000 bytes, so the third extension
30 const size_t kMaxCacheSize
= 2 * 1024 * 1024;
31 const size_t kExtensionSize1
= 1000 * 1000;
32 const size_t kExtensionSize2
= 1000 * 1000;
33 const size_t kExtensionSize3
= 100 * 1000;
35 static void CreateFile(const base::FilePath
& file
,
37 const base::Time
& timestamp
) {
38 const std::string
data(size
, 0);
39 EXPECT_EQ(base::WriteFile(file
, data
.data(), data
.size()),
40 static_cast<int>(size
));
41 EXPECT_TRUE(base::TouchFile(file
, timestamp
, timestamp
));
44 static base::FilePath
CreateExtensionFile(const base::FilePath
& dir
,
45 const std::string
& id
,
46 const std::string
& version
,
48 const base::Time
& timestamp
) {
49 const base::FilePath filename
= dir
.Append(
50 LocalExtensionCache::ExtensionFileName(id
, version
, "" /* hash */));
51 CreateFile(filename
, size
, timestamp
);
57 class ExtensionCacheTest
: public testing::Test
{
59 content::TestBrowserThreadBundle thread_bundle_
;
60 chromeos::ScopedCrosSettingsTestHelper settings_helper_
;
63 TEST_F(ExtensionCacheTest
, SizePolicy
) {
64 settings_helper_
.ReplaceProvider(chromeos::kExtensionCacheSize
);
65 settings_helper_
.SetInteger(chromeos::kExtensionCacheSize
, kMaxCacheSize
);
67 // Create and initialize local cache.
68 const base::Time now
= base::Time::Now();
69 base::ScopedTempDir cache_dir
;
70 ASSERT_TRUE(cache_dir
.CreateUniqueTempDir());
71 const base::FilePath cache_path
= cache_dir
.path();
72 CreateFile(cache_path
.Append(LocalExtensionCache::kCacheReadyFlagFileName
), 0,
75 // The extension cache only has enough space for two of the three extensions.
76 const base::FilePath file1
=
77 CreateExtensionFile(cache_path
, kTestExtensionId1
, "1.0", kExtensionSize1
,
78 now
- base::TimeDelta::FromSeconds(1));
79 const base::FilePath file2
=
80 CreateExtensionFile(cache_path
, kTestExtensionId2
, "2.0", kExtensionSize2
,
81 now
- base::TimeDelta::FromSeconds(2));
82 const base::FilePath file3
=
83 CreateExtensionFile(cache_path
, kTestExtensionId3
, "3.0", kExtensionSize3
,
84 now
- base::TimeDelta::FromSeconds(3));
86 ExtensionCacheImpl
cache_impl(
87 make_scoped_ptr(new ChromeOSExtensionCacheDelegate(cache_path
)));
89 scoped_ptr
<base::RunLoop
> run_loop(new base::RunLoop
);
90 cache_impl
.Start(run_loop
->QuitClosure());
93 run_loop
.reset(new base::RunLoop
);
94 cache_impl
.Shutdown(run_loop
->QuitClosure());
97 EXPECT_TRUE(base::PathExists(file1
));
98 EXPECT_TRUE(base::PathExists(file2
));
99 EXPECT_FALSE(base::PathExists(file3
));
102 } // namespace extensions