| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/bind.h" | 5 #include "base/bind.h" |
| 6 #include "base/command_line.h" |
| 6 #include "base/files/file_util.h" | 7 #include "base/files/file_util.h" |
| 7 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
| 8 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 9 #include "base/path_service.h" | 10 #include "base/path_service.h" |
| 10 #include "base/run_loop.h" | 11 #include "base/run_loop.h" |
| 11 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
| 12 #include "base/values.h" | 13 #include "base/values.h" |
| 13 #include "chrome/browser/extensions/sandboxed_unpacker.h" | 14 #include "chrome/browser/extensions/sandboxed_unpacker.h" |
| 14 #include "chrome/common/chrome_paths.h" | 15 #include "chrome/common/chrome_paths.h" |
| 15 #include "content/public/test/test_browser_thread_bundle.h" | 16 #include "content/public/test/test_browser_thread_bundle.h" |
| 16 #include "content/public/test/test_utils.h" | 17 #include "content/public/test/test_utils.h" |
| 17 #include "extensions/common/constants.h" | 18 #include "extensions/common/constants.h" |
| 18 #include "extensions/common/extension.h" | 19 #include "extensions/common/extension.h" |
| 20 #include "extensions/common/switches.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
| 20 #include "third_party/skia/include/core/SkBitmap.h" | 22 #include "third_party/skia/include/core/SkBitmap.h" |
| 21 | 23 |
| 22 namespace extensions { | 24 namespace extensions { |
| 23 | 25 |
| 24 class MockSandboxedUnpackerClient : public SandboxedUnpackerClient { | 26 class MockSandboxedUnpackerClient : public SandboxedUnpackerClient { |
| 25 public: | 27 public: |
| 26 | 28 |
| 27 void WaitForUnpack() { | 29 void WaitForUnpack() { |
| 28 scoped_refptr<content::MessageLoopRunner> runner = | 30 scoped_refptr<content::MessageLoopRunner> runner = |
| 29 new content::MessageLoopRunner; | 31 new content::MessageLoopRunner; |
| 30 quit_closure_ = runner->QuitClosure(); | 32 quit_closure_ = runner->QuitClosure(); |
| 31 runner->Run(); | 33 runner->Run(); |
| 32 } | 34 } |
| 33 | 35 |
| 34 base::FilePath temp_dir() const { return temp_dir_; } | 36 base::FilePath temp_dir() const { return temp_dir_; } |
| 37 base::string16 unpack_err() const { return error_; } |
| 35 | 38 |
| 36 private: | 39 private: |
| 37 ~MockSandboxedUnpackerClient() override {} | 40 ~MockSandboxedUnpackerClient() override {} |
| 38 | 41 |
| 39 void OnUnpackSuccess(const base::FilePath& temp_dir, | 42 void OnUnpackSuccess(const base::FilePath& temp_dir, |
| 40 const base::FilePath& extension_root, | 43 const base::FilePath& extension_root, |
| 41 const base::DictionaryValue* original_manifest, | 44 const base::DictionaryValue* original_manifest, |
| 42 const Extension* extension, | 45 const Extension* extension, |
| 43 const SkBitmap& install_icon) override { | 46 const SkBitmap& install_icon) override { |
| 44 temp_dir_ = temp_dir; | 47 temp_dir_ = temp_dir; |
| 45 quit_closure_.Run(); | 48 quit_closure_.Run(); |
| 46 | |
| 47 } | 49 } |
| 48 | 50 |
| 49 void OnUnpackFailure(const base::string16& error) override { | 51 void OnUnpackFailure(const base::string16& error) override { |
| 50 ASSERT_TRUE(false); | 52 error_ = error; |
| 53 quit_closure_.Run(); |
| 51 } | 54 } |
| 52 | 55 |
| 56 base::string16 error_; |
| 53 base::Closure quit_closure_; | 57 base::Closure quit_closure_; |
| 54 base::FilePath temp_dir_; | 58 base::FilePath temp_dir_; |
| 55 }; | 59 }; |
| 56 | 60 |
| 57 class SandboxedUnpackerTest : public testing::Test { | 61 class SandboxedUnpackerTest : public testing::Test { |
| 58 public: | 62 public: |
| 59 void SetUp() override { | 63 void SetUp() override { |
| 60 ASSERT_TRUE(extensions_dir_.CreateUniqueTempDir()); | 64 ASSERT_TRUE(extensions_dir_.CreateUniqueTempDir()); |
| 61 browser_threads_.reset(new content::TestBrowserThreadBundle( | 65 browser_threads_.reset(new content::TestBrowserThreadBundle( |
| 62 content::TestBrowserThreadBundle::IO_MAINLOOP)); | 66 content::TestBrowserThreadBundle::IO_MAINLOOP)); |
| 63 in_process_utility_thread_helper_.reset( | 67 in_process_utility_thread_helper_.reset( |
| 64 new content::InProcessUtilityThreadHelper); | 68 new content::InProcessUtilityThreadHelper); |
| 65 // It will delete itself. | 69 // It will delete itself. |
| 66 client_ = new MockSandboxedUnpackerClient; | 70 client_ = new MockSandboxedUnpackerClient; |
| 67 } | 71 } |
| 68 | 72 |
| 69 void TearDown() override { | 73 void TearDown() override { |
| 70 // Need to destruct SandboxedUnpacker before the message loop since | 74 // Need to destruct SandboxedUnpacker before the message loop since |
| 71 // it posts a task to it. | 75 // it posts a task to it. |
| 72 sandboxed_unpacker_ = NULL; | 76 sandboxed_unpacker_ = NULL; |
| 73 base::RunLoop().RunUntilIdle(); | 77 base::RunLoop().RunUntilIdle(); |
| 74 } | 78 } |
| 75 | 79 |
| 76 void SetupUnpacker(const std::string& crx_name) { | 80 void SetupUnpacker(const std::string& crx_name, |
| 81 const std::string& package_hash) { |
| 77 base::FilePath original_path; | 82 base::FilePath original_path; |
| 78 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &original_path)); | 83 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &original_path)); |
| 79 original_path = original_path.AppendASCII("extensions") | 84 original_path = original_path.AppendASCII("extensions") |
| 80 .AppendASCII("unpacker") | 85 .AppendASCII("unpacker") |
| 81 .AppendASCII(crx_name); | 86 .AppendASCII(crx_name); |
| 82 ASSERT_TRUE(base::PathExists(original_path)) << original_path.value(); | 87 ASSERT_TRUE(base::PathExists(original_path)) << original_path.value(); |
| 83 | 88 |
| 84 sandboxed_unpacker_ = new SandboxedUnpacker( | 89 sandboxed_unpacker_ = new SandboxedUnpacker( |
| 85 original_path, | 90 original_path, package_hash, Manifest::INTERNAL, Extension::NO_FLAGS, |
| 86 Manifest::INTERNAL, | 91 extensions_dir_.path(), base::MessageLoopProxy::current(), client_); |
| 87 Extension::NO_FLAGS, | |
| 88 extensions_dir_.path(), | |
| 89 base::MessageLoopProxy::current(), | |
| 90 client_); | |
| 91 | 92 |
| 92 base::MessageLoopProxy::current()->PostTask( | 93 base::MessageLoopProxy::current()->PostTask( |
| 93 FROM_HERE, | 94 FROM_HERE, |
| 94 base::Bind(&SandboxedUnpacker::Start, sandboxed_unpacker_.get())); | 95 base::Bind(&SandboxedUnpacker::Start, sandboxed_unpacker_.get())); |
| 95 client_->WaitForUnpack(); | 96 client_->WaitForUnpack(); |
| 96 } | 97 } |
| 97 | 98 |
| 98 base::FilePath GetInstallPath() { | 99 base::FilePath GetInstallPath() { |
| 99 return client_->temp_dir().AppendASCII(kTempExtensionName); | 100 return client_->temp_dir().AppendASCII(kTempExtensionName); |
| 100 } | 101 } |
| 101 | 102 |
| 103 base::string16 GetInstallError() { |
| 104 return client_->unpack_err(); |
| 105 } |
| 106 |
| 102 protected: | 107 protected: |
| 103 base::ScopedTempDir extensions_dir_; | 108 base::ScopedTempDir extensions_dir_; |
| 104 MockSandboxedUnpackerClient* client_; | 109 MockSandboxedUnpackerClient* client_; |
| 105 scoped_refptr<SandboxedUnpacker> sandboxed_unpacker_; | 110 scoped_refptr<SandboxedUnpacker> sandboxed_unpacker_; |
| 106 scoped_ptr<content::TestBrowserThreadBundle> browser_threads_; | 111 scoped_ptr<content::TestBrowserThreadBundle> browser_threads_; |
| 107 scoped_ptr<content::InProcessUtilityThreadHelper> | 112 scoped_ptr<content::InProcessUtilityThreadHelper> |
| 108 in_process_utility_thread_helper_; | 113 in_process_utility_thread_helper_; |
| 109 }; | 114 }; |
| 110 | 115 |
| 111 TEST_F(SandboxedUnpackerTest, NoCatalogsSuccess) { | 116 TEST_F(SandboxedUnpackerTest, NoCatalogsSuccess) { |
| 112 SetupUnpacker("no_l10n.crx"); | 117 SetupUnpacker("no_l10n.crx", ""); |
| 113 // Check that there is no _locales folder. | 118 // Check that there is no _locales folder. |
| 114 base::FilePath install_path = | 119 base::FilePath install_path = |
| 115 GetInstallPath().Append(kLocaleFolder); | 120 GetInstallPath().Append(kLocaleFolder); |
| 116 EXPECT_FALSE(base::PathExists(install_path)); | 121 EXPECT_FALSE(base::PathExists(install_path)); |
| 117 } | 122 } |
| 118 | 123 |
| 119 TEST_F(SandboxedUnpackerTest, WithCatalogsSuccess) { | 124 TEST_F(SandboxedUnpackerTest, WithCatalogsSuccess) { |
| 120 SetupUnpacker("good_l10n.crx"); | 125 SetupUnpacker("good_l10n.crx", ""); |
| 121 // Check that there is _locales folder. | 126 // Check that there is _locales folder. |
| 122 base::FilePath install_path = | 127 base::FilePath install_path = |
| 123 GetInstallPath().Append(kLocaleFolder); | 128 GetInstallPath().Append(kLocaleFolder); |
| 124 EXPECT_TRUE(base::PathExists(install_path)); | 129 EXPECT_TRUE(base::PathExists(install_path)); |
| 125 } | 130 } |
| 126 | 131 |
| 132 TEST_F(SandboxedUnpackerTest, FailHashCheck) { |
| 133 base::CommandLine::ForCurrentProcess()->AppendSwitch( |
| 134 extensions::switches::kEnableCrxHashCheck); |
| 135 SetupUnpacker("good_l10n.crx", "badhash"); |
| 136 // Check that there is an error message. |
| 137 EXPECT_NE(base::string16(), GetInstallError()); |
| 138 } |
| 139 |
| 140 TEST_F(SandboxedUnpackerTest, PassHashCheck) { |
| 141 base::CommandLine::ForCurrentProcess()->AppendSwitch( |
| 142 extensions::switches::kEnableCrxHashCheck); |
| 143 SetupUnpacker("good_l10n.crx", |
| 144 "6fa171c726373785aa4fcd2df448c3db0420a95d5044fbee831f089b979c4068"); |
| 145 // Check that there is no error message. |
| 146 EXPECT_EQ(base::string16(), GetInstallError()); |
| 147 } |
| 148 |
| 149 TEST_F(SandboxedUnpackerTest, SkipHashCheck) { |
| 150 SetupUnpacker("good_l10n.crx", "badhash"); |
| 151 // Check that there is no error message. |
| 152 EXPECT_EQ(base::string16(), GetInstallError()); |
| 153 } |
| 154 |
| 127 } // namespace extensions | 155 } // namespace extensions |
| OLD | NEW |