OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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/platform_thread.h" | 7 #include "base/platform_thread.h" |
8 #include "base/ref_counted.h" | 8 #include "base/ref_counted.h" |
9 #include "base/scoped_temp_dir.h" | 9 #include "base/scoped_temp_dir.h" |
10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
80 file_util::Delete(install_dir_, true); | 80 file_util::Delete(install_dir_, true); |
81 file_util::CreateDirectory(install_dir_); | 81 file_util::CreateDirectory(install_dir_); |
82 | 82 |
83 FilePath crx_path = install_dir_.AppendASCII(crx_name); | 83 FilePath crx_path = install_dir_.AppendASCII(crx_name); |
84 ASSERT_TRUE(file_util::CopyFile(original_path, crx_path)) << | 84 ASSERT_TRUE(file_util::CopyFile(original_path, crx_path)) << |
85 "Original path: " << original_path.value() << | 85 "Original path: " << original_path.value() << |
86 ", Crx path: " << crx_path.value(); | 86 ", Crx path: " << crx_path.value(); |
87 | 87 |
88 unpacker_.reset(new ExtensionUnpacker(crx_path)); | 88 unpacker_.reset(new ExtensionUnpacker(crx_path)); |
89 | 89 |
90 // It will delete itself. | 90 |
| 91 // Build a temp area where the extension will be unpacked. |
| 92 ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &temp_dir_)); |
| 93 temp_dir_ = temp_dir_.AppendASCII("sandboxed_extension_unpacker_test_Temp"); |
| 94 file_util::CreateDirectory(temp_dir_); |
| 95 |
91 sandboxed_unpacker_ = | 96 sandboxed_unpacker_ = |
92 new SandboxedExtensionUnpacker(crx_path, NULL, client_); | 97 new SandboxedExtensionUnpacker(crx_path, temp_dir_, NULL, client_); |
93 PrepareUnpackerEnv(); | 98 PrepareUnpackerEnv(); |
94 } | 99 } |
95 | 100 |
96 void PrepareUnpackerEnv() { | 101 void PrepareUnpackerEnv() { |
97 sandboxed_unpacker_->extension_root_ = | 102 sandboxed_unpacker_->extension_root_ = |
98 install_dir_.AppendASCII("TEMP_INSTALL"); | 103 install_dir_.AppendASCII(extension_filenames::kTempExtensionName); |
99 | 104 |
100 sandboxed_unpacker_->temp_dir_.Set(install_dir_); | 105 sandboxed_unpacker_->temp_dir_.Set(install_dir_); |
101 sandboxed_unpacker_->public_key_ = | 106 sandboxed_unpacker_->public_key_ = |
102 "ocnapchkplbmjmpfehjocmjnipfmogkh"; | 107 "ocnapchkplbmjmpfehjocmjnipfmogkh"; |
103 } | 108 } |
104 | 109 |
105 void OnUnpackSucceeded() { | 110 void OnUnpackSucceeded() { |
106 sandboxed_unpacker_->OnUnpackExtensionSucceeded( | 111 sandboxed_unpacker_->OnUnpackExtensionSucceeded( |
107 *unpacker_->parsed_manifest()); | 112 *unpacker_->parsed_manifest()); |
108 } | 113 } |
109 | 114 |
110 FilePath GetInstallPath() { | 115 FilePath GetInstallPath() { |
111 return install_dir_.AppendASCII("TEMP_INSTALL"); | 116 return install_dir_.AppendASCII(extension_filenames::kTempExtensionName); |
| 117 } |
| 118 |
| 119 bool TempFilesRemoved() { |
| 120 // Check that temporary files were cleaned up. |
| 121 file_util::FileEnumerator::FILE_TYPE files_and_dirs = |
| 122 static_cast<file_util::FileEnumerator::FILE_TYPE>( |
| 123 file_util::FileEnumerator::DIRECTORIES | |
| 124 file_util::FileEnumerator::FILES); |
| 125 |
| 126 file_util::FileEnumerator temp_iterator( |
| 127 temp_dir_, |
| 128 true, // recursive |
| 129 files_and_dirs |
| 130 ); |
| 131 int items_not_removed = 0; |
| 132 FilePath item_in_temp; |
| 133 item_in_temp = temp_iterator.Next(); |
| 134 while (!item_in_temp.value().empty()) { |
| 135 items_not_removed++; |
| 136 EXPECT_STREQ(FILE_PATH_LITERAL(""), item_in_temp.value().c_str()) |
| 137 << "File was not removed on success."; |
| 138 item_in_temp = temp_iterator.Next(); |
| 139 } |
| 140 return (items_not_removed == 0); |
112 } | 141 } |
113 | 142 |
114 protected: | 143 protected: |
115 FilePath install_dir_; | 144 FilePath install_dir_; |
| 145 FilePath temp_dir_; |
116 MockSandboxedExtensionUnpackerClient* client_; | 146 MockSandboxedExtensionUnpackerClient* client_; |
117 scoped_ptr<ExtensionUnpacker> unpacker_; | 147 scoped_ptr<ExtensionUnpacker> unpacker_; |
118 scoped_refptr<SandboxedExtensionUnpacker> sandboxed_unpacker_; | 148 scoped_refptr<SandboxedExtensionUnpacker> sandboxed_unpacker_; |
119 }; | 149 }; |
120 | 150 |
121 TEST_F(SandboxedExtensionUnpackerTest, NoCatalogsSuccess) { | 151 TEST_F(SandboxedExtensionUnpackerTest, NoCatalogsSuccess) { |
122 EXPECT_CALL(*client_, OnUnpackSuccess(_, _, _)); | 152 EXPECT_CALL(*client_, OnUnpackSuccess(_, _, _)); |
123 | 153 |
124 SetupUnpacker("no_l10n.crx"); | 154 SetupUnpacker("no_l10n.crx"); |
125 ASSERT_TRUE(unpacker_->Run()); | 155 ASSERT_TRUE(unpacker_->Run()); |
126 ASSERT_TRUE(unpacker_->DumpImagesToFile()); | 156 ASSERT_TRUE(unpacker_->DumpImagesToFile()); |
127 ASSERT_TRUE(unpacker_->DumpMessageCatalogsToFile()); | 157 ASSERT_TRUE(unpacker_->DumpMessageCatalogsToFile()); |
128 | 158 |
129 // Check that there is no _locales folder. | 159 // Check that there is no _locales folder. |
130 FilePath install_path = | 160 FilePath install_path = |
131 GetInstallPath().Append(Extension::kLocaleFolder); | 161 GetInstallPath().Append(Extension::kLocaleFolder); |
132 EXPECT_FALSE(file_util::PathExists(install_path)); | 162 EXPECT_FALSE(file_util::PathExists(install_path)); |
133 | 163 |
134 OnUnpackSucceeded(); | 164 OnUnpackSucceeded(); |
135 | 165 |
136 // Check that there still is no _locales folder. | 166 // Check that there still is no _locales folder. |
137 EXPECT_FALSE(file_util::PathExists(install_path)); | 167 EXPECT_FALSE(file_util::PathExists(install_path)); |
| 168 |
| 169 ASSERT_TRUE(TempFilesRemoved()); |
138 } | 170 } |
139 | 171 |
140 TEST_F(SandboxedExtensionUnpackerTest, WithCatalogsSuccess) { | 172 TEST_F(SandboxedExtensionUnpackerTest, WithCatalogsSuccess) { |
141 EXPECT_CALL(*client_, OnUnpackSuccess(_, _, _)); | 173 EXPECT_CALL(*client_, OnUnpackSuccess(_, _, _)); |
142 | 174 |
143 SetupUnpacker("good_l10n.crx"); | 175 SetupUnpacker("good_l10n.crx"); |
144 ASSERT_TRUE(unpacker_->Run()); | 176 ASSERT_TRUE(unpacker_->Run()); |
145 ASSERT_TRUE(unpacker_->DumpImagesToFile()); | 177 ASSERT_TRUE(unpacker_->DumpImagesToFile()); |
146 ASSERT_TRUE(unpacker_->DumpMessageCatalogsToFile()); | 178 ASSERT_TRUE(unpacker_->DumpMessageCatalogsToFile()); |
147 | 179 |
148 // Check timestamp on _locales/en_US/messages.json. | 180 // Check timestamp on _locales/en_US/messages.json. |
149 FilePath messages_file; | 181 FilePath messages_file; |
150 messages_file = GetInstallPath().Append(Extension::kLocaleFolder) | 182 messages_file = GetInstallPath().Append(Extension::kLocaleFolder) |
151 .AppendASCII("en_US") | 183 .AppendASCII("en_US") |
152 .Append(Extension::kMessagesFilename); | 184 .Append(Extension::kMessagesFilename); |
153 file_util::FileInfo old_info; | 185 file_util::FileInfo old_info; |
154 EXPECT_TRUE(file_util::GetFileInfo(messages_file, &old_info)); | 186 EXPECT_TRUE(file_util::GetFileInfo(messages_file, &old_info)); |
155 | 187 |
156 // unpacker_->Run unpacks the extension. OnUnpackSucceeded overwrites some | 188 // unpacker_->Run unpacks the extension. OnUnpackSucceeded overwrites some |
157 // of the files. To force timestamp on overwriten files to be different we use | 189 // of the files. To force timestamp on overwriten files to be different we use |
158 // Sleep(1s). See comment on file_util::CountFilesCreatedAfter. | 190 // Sleep(1s). See comment on file_util::CountFilesCreatedAfter. |
159 PlatformThread::Sleep(1000); | 191 PlatformThread::Sleep(1000); |
160 OnUnpackSucceeded(); | 192 OnUnpackSucceeded(); |
161 | 193 |
162 // Check that there is newer _locales/en_US/messages.json file. | 194 // Check that there is newer _locales/en_US/messages.json file. |
163 file_util::FileInfo new_info; | 195 file_util::FileInfo new_info; |
164 EXPECT_TRUE(file_util::GetFileInfo(messages_file, &new_info)); | 196 EXPECT_TRUE(file_util::GetFileInfo(messages_file, &new_info)); |
165 | 197 |
166 EXPECT_TRUE(new_info.last_modified > old_info.last_modified); | 198 EXPECT_TRUE(new_info.last_modified > old_info.last_modified); |
| 199 |
| 200 ASSERT_TRUE(TempFilesRemoved()); |
167 } | 201 } |
OLD | NEW |