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