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