| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/file_util.h" |
| 6 #include "base/path_service.h" |
| 7 #include "base/ref_counted.h" |
| 8 #include "base/scoped_temp_dir.h" |
| 9 #include "base/string_util.h" |
| 10 #include "base/values.h" |
| 11 #include "chrome/browser/extensions/sandboxed_extension_unpacker.h" |
| 12 #include "chrome/common/chrome_paths.h" |
| 13 #include "chrome/common/extensions/extension.h" |
| 14 #include "chrome/common/extensions/extension_constants.h" |
| 15 #include "chrome/common/extensions/extension_unpacker.h" |
| 16 #include "testing/gmock/include/gmock/gmock.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" |
| 18 #include "third_party/skia/include/core/SkBitmap.h" |
| 19 |
| 20 namespace errors = extension_manifest_errors; |
| 21 namespace keys = extension_manifest_keys; |
| 22 |
| 23 using testing::_; |
| 24 using testing::Invoke; |
| 25 |
| 26 void OnUnpackSuccess(const FilePath& temp_dir, |
| 27 const FilePath& extension_root, |
| 28 Extension* extension) { |
| 29 delete extension; |
| 30 // Don't delete temp_dir here, we need to do some post op checking. |
| 31 } |
| 32 |
| 33 class MockSandboxedExtensionUnpackerClient |
| 34 : public SandboxedExtensionUnpackerClient { |
| 35 public: |
| 36 virtual ~MockSandboxedExtensionUnpackerClient() {} |
| 37 |
| 38 MOCK_METHOD3(OnUnpackSuccess, |
| 39 void(const FilePath& temp_dir, |
| 40 const FilePath& extension_root, |
| 41 Extension* extension)); |
| 42 |
| 43 MOCK_METHOD1(OnUnpackFailure, |
| 44 void(const std::string& error)); |
| 45 |
| 46 void DelegateToFake() { |
| 47 ON_CALL(*this, OnUnpackSuccess(_, _, _)) |
| 48 .WillByDefault(Invoke(::OnUnpackSuccess)); |
| 49 } |
| 50 }; |
| 51 |
| 52 class SandboxedExtensionUnpackerTest : public testing::Test { |
| 53 public: |
| 54 virtual void SetUp () { |
| 55 // It will delete itself. |
| 56 client_ = new MockSandboxedExtensionUnpackerClient; |
| 57 client_->DelegateToFake(); |
| 58 } |
| 59 |
| 60 virtual void TearDown() { |
| 61 // Clean up finally. |
| 62 ASSERT_TRUE(file_util::Delete(install_dir_, true)) << |
| 63 install_dir_.value(); |
| 64 } |
| 65 |
| 66 void SetupUnpacker(const std::string& crx_name) { |
| 67 FilePath original_path; |
| 68 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &original_path)); |
| 69 original_path = original_path.AppendASCII("extensions") |
| 70 .AppendASCII("unpacker") |
| 71 .AppendASCII(crx_name); |
| 72 ASSERT_TRUE(file_util::PathExists(original_path)) << original_path.value(); |
| 73 |
| 74 // Try bots won't let us write into DIR_TEST_DATA, so we have to create |
| 75 // a temp folder to play in. |
| 76 ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &install_dir_)); |
| 77 install_dir_ = |
| 78 install_dir_.AppendASCII("sandboxed_extension_unpacker_test"); |
| 79 ASSERT_TRUE(file_util::Delete(install_dir_, true)) << |
| 80 install_dir_.value(); |
| 81 ASSERT_TRUE(file_util::CreateDirectory(install_dir_)) << |
| 82 install_dir_.value(); |
| 83 |
| 84 FilePath crx_path = install_dir_.AppendASCII(crx_name); |
| 85 ASSERT_TRUE(file_util::CopyFile(original_path, crx_path)) << |
| 86 "Original path: " << original_path.value() << |
| 87 ", Crx path: " << crx_path.value(); |
| 88 |
| 89 unpacker_.reset(new ExtensionUnpacker(crx_path)); |
| 90 |
| 91 // It will delete itself. |
| 92 sandboxed_unpacker_ = |
| 93 new SandboxedExtensionUnpacker(crx_path, NULL, client_); |
| 94 PrepareUnpackerEnv(); |
| 95 } |
| 96 |
| 97 void PrepareUnpackerEnv() { |
| 98 sandboxed_unpacker_->extension_root_ = |
| 99 install_dir_.AppendASCII("TEMP_INSTALL"); |
| 100 |
| 101 sandboxed_unpacker_->temp_dir_.Set(install_dir_); |
| 102 sandboxed_unpacker_->public_key_ = |
| 103 "ocnapchkplbmjmpfehjocmjnipfmogkh"; |
| 104 } |
| 105 |
| 106 void OnUnpackSucceeded() { |
| 107 sandboxed_unpacker_->OnUnpackExtensionSucceeded( |
| 108 *unpacker_->parsed_manifest(), |
| 109 *unpacker_->parsed_catalogs()); |
| 110 } |
| 111 |
| 112 FilePath GetInstallPath() { |
| 113 return install_dir_.AppendASCII("TEMP_INSTALL"); |
| 114 } |
| 115 |
| 116 protected: |
| 117 FilePath install_dir_; |
| 118 MockSandboxedExtensionUnpackerClient* client_; |
| 119 scoped_ptr<ExtensionUnpacker> unpacker_; |
| 120 scoped_refptr<SandboxedExtensionUnpacker> sandboxed_unpacker_; |
| 121 }; |
| 122 |
| 123 TEST_F(SandboxedExtensionUnpackerTest, NoCatalogsSuccess) { |
| 124 EXPECT_CALL(*client_, OnUnpackSuccess(_, _, _)); |
| 125 |
| 126 SetupUnpacker("no_l10n.crx"); |
| 127 ASSERT_TRUE(unpacker_->Run()); |
| 128 ASSERT_TRUE(unpacker_->DumpImagesToFile()); |
| 129 |
| 130 // Check that there is no _locales folder. |
| 131 FilePath install_path = |
| 132 GetInstallPath().AppendASCII(Extension::kLocaleFolder); |
| 133 EXPECT_FALSE(file_util::PathExists(install_path)); |
| 134 |
| 135 OnUnpackSucceeded(); |
| 136 |
| 137 // Check that there still is no _locales folder. |
| 138 EXPECT_FALSE(file_util::PathExists(install_path)); |
| 139 } |
| 140 |
| 141 TEST_F(SandboxedExtensionUnpackerTest, WithCatalogsSuccess) { |
| 142 EXPECT_CALL(*client_, OnUnpackSuccess(_, _, _)); |
| 143 |
| 144 SetupUnpacker("good_l10n.crx"); |
| 145 ASSERT_TRUE(unpacker_->Run()); |
| 146 ASSERT_TRUE(unpacker_->DumpImagesToFile()); |
| 147 |
| 148 // Delete _locales/en_US/messages.json. |
| 149 FilePath messages_file; |
| 150 messages_file = GetInstallPath() |
| 151 .AppendASCII(Extension::kLocaleFolder) |
| 152 .AppendASCII("en_US") |
| 153 .AppendASCII(Extension::kMessagesFilename); |
| 154 EXPECT_TRUE(file_util::Delete(messages_file, false)); |
| 155 |
| 156 OnUnpackSucceeded(); |
| 157 |
| 158 // Check that there is _locales/en_US/messages.json file. |
| 159 EXPECT_TRUE(file_util::PathExists(messages_file)); |
| 160 } |
| OLD | NEW |