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

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

Issue 6297003: Fail gracefully if profile Temp dir can not be accessed. (Closed) Base URL: http://git.chromium.org/git/chromium.git
Patch Set: Address rev comments. Created 9 years, 11 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
« no previous file with comments | « chrome/browser/extensions/sandboxed_extension_unpacker.cc ('k') | chrome/common/chrome_paths.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2009 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 "base/file_util.h" 5 #include "base/file_util.h"
6 #include "base/path_service.h" 6 #include "base/path_service.h"
7 #include "base/ref_counted.h" 7 #include "base/ref_counted.h"
8 #include "base/scoped_temp_dir.h" 8 #include "base/scoped_temp_dir.h"
9 #include "base/string_util.h" 9 #include "base/string_util.h"
10 #include "chrome/browser/extensions/sandboxed_extension_unpacker.h" 10 #include "chrome/browser/extensions/sandboxed_extension_unpacker.h"
11 #include "chrome/common/chrome_paths.h" 11 #include "chrome/common/chrome_paths.h"
12 #include "chrome/common/extensions/extension.h" 12 #include "chrome/common/extensions/extension.h"
13 #include "chrome/common/extensions/extension_constants.h" 13 #include "chrome/common/extensions/extension_constants.h"
14 #include "chrome/common/extensions/extension_unpacker.h" 14 #include "chrome/common/extensions/extension_unpacker.h"
15 #include "testing/gmock/include/gmock/gmock.h" 15 #include "testing/gmock/include/gmock/gmock.h"
16 #include "testing/gtest/include/gtest/gtest.h" 16 #include "testing/gtest/include/gtest/gtest.h"
17 #include "third_party/skia/include/core/SkBitmap.h" 17 #include "third_party/skia/include/core/SkBitmap.h"
18 18
19 namespace errors = extension_manifest_errors; 19 namespace errors = extension_manifest_errors;
20 namespace keys = extension_manifest_keys; 20 namespace keys = extension_manifest_keys;
21 21
22 using testing::_; 22 using testing::_;
23 using testing::Invoke; 23 using testing::Invoke;
24 24
25 namespace {
26
25 void OnUnpackSuccess(const FilePath& temp_dir, 27 void OnUnpackSuccess(const FilePath& temp_dir,
26 const FilePath& extension_root, 28 const FilePath& extension_root,
27 const Extension* extension) { 29 const Extension* extension) {
28 // Don't delete temp_dir here, we need to do some post op checking. 30 // Don't delete temp_dir here, we need to do some post op checking.
29 } 31 }
30 32
33 } // namespace
34
31 class MockSandboxedExtensionUnpackerClient 35 class MockSandboxedExtensionUnpackerClient
32 : public SandboxedExtensionUnpackerClient { 36 : public SandboxedExtensionUnpackerClient {
33 public: 37 public:
34 virtual ~MockSandboxedExtensionUnpackerClient() {} 38 virtual ~MockSandboxedExtensionUnpackerClient() {}
35 39
36 MOCK_METHOD3(OnUnpackSuccess, 40 MOCK_METHOD3(OnUnpackSuccess,
37 void(const FilePath& temp_dir, 41 void(const FilePath& temp_dir,
38 const FilePath& extension_root, 42 const FilePath& extension_root,
39 const Extension* extension)); 43 const Extension* extension));
40 44
(...skipping 30 matching lines...) Expand all
71 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &original_path)); 75 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &original_path));
72 original_path = original_path.AppendASCII("extensions") 76 original_path = original_path.AppendASCII("extensions")
73 .AppendASCII("unpacker") 77 .AppendASCII("unpacker")
74 .AppendASCII(crx_name); 78 .AppendASCII(crx_name);
75 ASSERT_TRUE(file_util::PathExists(original_path)) << original_path.value(); 79 ASSERT_TRUE(file_util::PathExists(original_path)) << original_path.value();
76 80
77 // Try bots won't let us write into DIR_TEST_DATA, so we have to create 81 // Try bots won't let us write into DIR_TEST_DATA, so we have to create
78 // a temp folder to play in. 82 // a temp folder to play in.
79 ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &install_dir_)); 83 ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &install_dir_));
80 install_dir_ = 84 install_dir_ =
81 install_dir_.AppendASCII("sandboxed_extension_unpacker_test"); 85 install_dir_.AppendASCII("sandboxed_extension_unpacker_test");
82 file_util::Delete(install_dir_, true); 86 file_util::Delete(install_dir_, true);
83 file_util::CreateDirectory(install_dir_); 87 file_util::CreateDirectory(install_dir_);
84 88
85 FilePath crx_path = install_dir_.AppendASCII(crx_name); 89 FilePath crx_path = install_dir_.AppendASCII(crx_name);
86 ASSERT_TRUE(file_util::CopyFile(original_path, crx_path)) << 90 ASSERT_TRUE(file_util::CopyFile(original_path, crx_path)) <<
87 "Original path: " << original_path.value() << 91 "Original path: " << original_path.value() <<
88 ", Crx path: " << crx_path.value(); 92 ", Crx path: " << crx_path.value();
89 93
90 unpacker_.reset(new ExtensionUnpacker(crx_path)); 94 unpacker_.reset(new ExtensionUnpacker(crx_path));
91 95
92
93 // Build a temp area where the extension will be unpacked. 96 // Build a temp area where the extension will be unpacked.
94 ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &temp_dir_)); 97 ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &temp_dir_));
95 temp_dir_ = temp_dir_.AppendASCII("sandboxed_extension_unpacker_test_Temp"); 98 temp_dir_ = temp_dir_.AppendASCII("sandboxed_extension_unpacker_test_Temp");
96 file_util::CreateDirectory(temp_dir_); 99 ASSERT_TRUE(file_util::CreateDirectory(temp_dir_));
97 100
98 sandboxed_unpacker_ = 101 sandboxed_unpacker_ =
99 new SandboxedExtensionUnpacker(crx_path, temp_dir_, NULL, client_); 102 new SandboxedExtensionUnpacker(crx_path, NULL, client_);
103
100 // Hack since SandboxedExtensionUnpacker gets its background thread id from 104 // Hack since SandboxedExtensionUnpacker gets its background thread id from
101 // the Start call, but we don't call it here. 105 // the Start call, but we don't call it here.
102 sandboxed_unpacker_->thread_identifier_ = BrowserThread::FILE; 106 sandboxed_unpacker_->thread_identifier_ = BrowserThread::FILE;
103 EXPECT_TRUE(PrepareUnpackerEnv()); 107 EXPECT_TRUE(PrepareUnpackerEnv());
104 } 108 }
105 109
106 bool PrepareUnpackerEnv() { 110 bool PrepareUnpackerEnv() {
107 sandboxed_unpacker_->extension_root_ = 111 sandboxed_unpacker_->extension_root_ =
108 install_dir_.AppendASCII(extension_filenames::kTempExtensionName); 112 install_dir_.AppendASCII(extension_filenames::kTempExtensionName);
109 113
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
152 FilePath temp_dir_; 156 FilePath temp_dir_;
153 MockSandboxedExtensionUnpackerClient* client_; 157 MockSandboxedExtensionUnpackerClient* client_;
154 scoped_ptr<ExtensionUnpacker> unpacker_; 158 scoped_ptr<ExtensionUnpacker> unpacker_;
155 scoped_refptr<SandboxedExtensionUnpacker> sandboxed_unpacker_; 159 scoped_refptr<SandboxedExtensionUnpacker> sandboxed_unpacker_;
156 MessageLoop loop_; 160 MessageLoop loop_;
157 scoped_ptr<BrowserThread> file_thread_; 161 scoped_ptr<BrowserThread> file_thread_;
158 }; 162 };
159 163
160 TEST_F(SandboxedExtensionUnpackerTest, NoCatalogsSuccess) { 164 TEST_F(SandboxedExtensionUnpackerTest, NoCatalogsSuccess) {
161 EXPECT_CALL(*client_, OnUnpackSuccess(_, _, _)); 165 EXPECT_CALL(*client_, OnUnpackSuccess(_, _, _));
166 EXPECT_CALL(*client_, OnUnpackFailure(_)).Times(0);
162 167
163 SetupUnpacker("no_l10n.crx"); 168 SetupUnpacker("no_l10n.crx");
164 ASSERT_TRUE(unpacker_->Run()); 169 ASSERT_TRUE(unpacker_->Run());
165 ASSERT_TRUE(unpacker_->DumpImagesToFile()); 170 ASSERT_TRUE(unpacker_->DumpImagesToFile());
166 ASSERT_TRUE(unpacker_->DumpMessageCatalogsToFile()); 171 ASSERT_TRUE(unpacker_->DumpMessageCatalogsToFile());
167 172
168 // Check that there is no _locales folder. 173 // Check that there is no _locales folder.
169 FilePath install_path = 174 FilePath install_path =
170 GetInstallPath().Append(Extension::kLocaleFolder); 175 GetInstallPath().Append(Extension::kLocaleFolder);
171 EXPECT_FALSE(file_util::PathExists(install_path)); 176 EXPECT_FALSE(file_util::PathExists(install_path));
172 177
173 OnUnpackSucceeded(); 178 OnUnpackSucceeded();
174 179
175 // Check that there still is no _locales folder. 180 // Check that there still is no _locales folder.
176 EXPECT_FALSE(file_util::PathExists(install_path)); 181 EXPECT_FALSE(file_util::PathExists(install_path));
177 182
178 ASSERT_TRUE(TempFilesRemoved()); 183 ASSERT_TRUE(TempFilesRemoved());
179 } 184 }
180 185
181 TEST_F(SandboxedExtensionUnpackerTest, WithCatalogsSuccess) { 186 TEST_F(SandboxedExtensionUnpackerTest, WithCatalogsSuccess) {
182 EXPECT_CALL(*client_, OnUnpackSuccess(_, _, _)); 187 EXPECT_CALL(*client_, OnUnpackSuccess(_, _, _));
188 EXPECT_CALL(*client_, OnUnpackFailure(_)).Times(0);
183 189
184 SetupUnpacker("good_l10n.crx"); 190 SetupUnpacker("good_l10n.crx");
185 ASSERT_TRUE(unpacker_->Run()); 191 ASSERT_TRUE(unpacker_->Run());
186 ASSERT_TRUE(unpacker_->DumpImagesToFile()); 192 ASSERT_TRUE(unpacker_->DumpImagesToFile());
187 ASSERT_TRUE(unpacker_->DumpMessageCatalogsToFile()); 193 ASSERT_TRUE(unpacker_->DumpMessageCatalogsToFile());
188 194
189 // Set timestamp on _locales/en_US/messages.json into the past. 195 // Set timestamp on _locales/en_US/messages.json into the past.
190 FilePath messages_file; 196 FilePath messages_file;
191 messages_file = GetInstallPath().Append(Extension::kLocaleFolder) 197 messages_file = GetInstallPath().Append(Extension::kLocaleFolder)
192 .AppendASCII("en_US") 198 .AppendASCII("en_US")
193 .Append(Extension::kMessagesFilename); 199 .Append(Extension::kMessagesFilename);
194 base::PlatformFileInfo old_info; 200 base::PlatformFileInfo old_info;
195 EXPECT_TRUE(file_util::GetFileInfo(messages_file, &old_info)); 201 EXPECT_TRUE(file_util::GetFileInfo(messages_file, &old_info));
196 base::Time old_time = 202 base::Time old_time =
197 old_info.last_modified - base::TimeDelta::FromSeconds(2); 203 old_info.last_modified - base::TimeDelta::FromSeconds(2);
198 EXPECT_TRUE(file_util::SetLastModifiedTime(messages_file, old_time)); 204 EXPECT_TRUE(file_util::SetLastModifiedTime(messages_file, old_time));
199 // Refresh old_info, just to be sure. 205 // Refresh old_info, just to be sure.
200 EXPECT_TRUE(file_util::GetFileInfo(messages_file, &old_info)); 206 EXPECT_TRUE(file_util::GetFileInfo(messages_file, &old_info));
201 207
202 OnUnpackSucceeded(); 208 OnUnpackSucceeded();
203 209
204 // Check that there is newer _locales/en_US/messages.json file. 210 // Check that there is newer _locales/en_US/messages.json file.
205 base::PlatformFileInfo new_info; 211 base::PlatformFileInfo new_info;
206 EXPECT_TRUE(file_util::GetFileInfo(messages_file, &new_info)); 212 EXPECT_TRUE(file_util::GetFileInfo(messages_file, &new_info));
207 213
208 EXPECT_TRUE(new_info.last_modified > old_info.last_modified); 214 EXPECT_TRUE(new_info.last_modified > old_info.last_modified);
209 215
210 ASSERT_TRUE(TempFilesRemoved()); 216 ASSERT_TRUE(TempFilesRemoved());
211 } 217 }
OLDNEW
« no previous file with comments | « chrome/browser/extensions/sandboxed_extension_unpacker.cc ('k') | chrome/common/chrome_paths.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698