diff options
author | asargent <asargent@chromium.org> | 2015-01-16 13:34:16 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-01-16 21:34:55 +0000 |
commit | 12a9cab7ee804fd898dca62004f750cc5a1ad685 (patch) | |
tree | 6734137f0cc7c889fa7c5f4059d12c43642b30d0 /extensions/utility | |
parent | 79126efc4e18a879657667f39b438b6e58523e58 (diff) | |
download | chromium_src-12a9cab7ee804fd898dca62004f750cc5a1ad685.zip chromium_src-12a9cab7ee804fd898dca62004f750cc5a1ad685.tar.gz chromium_src-12a9cab7ee804fd898dca62004f750cc5a1ad685.tar.bz2 |
Move unpacker_unittests.cc and data files from chrome/ to extensions/
Most of the changes here are just moves, with only a few more notable
changes:
Add "manifest_version : 2" and change from theme to extension
-extensions/test/data/unpacker/bad_zip/manifest.json
-extensions/test/data/unpacker/bad_image/manifest.json
-extensions/test/data/unpacker/good_package/manifest.json
Changed BadPathError test and misc cleanup
-extensions/utility/unpacker_unittest.cc
Support for changes to the above mentioned BadPathError test
-extensions/test/test_extensions_client.h
-extensions/test/test_extensions_client.cc
Entire directory just deleted, not moved
-chrome/test/data/extensions/unpacker/bad_path/
BUG=447014
Review URL: https://codereview.chromium.org/849803004
Cr-Commit-Position: refs/heads/master@{#311954}
Diffstat (limited to 'extensions/utility')
-rw-r--r-- | extensions/utility/unpacker_unittest.cc | 205 |
1 files changed, 205 insertions, 0 deletions
diff --git a/extensions/utility/unpacker_unittest.cc b/extensions/utility/unpacker_unittest.cc new file mode 100644 index 0000000..917f66e --- /dev/null +++ b/extensions/utility/unpacker_unittest.cc @@ -0,0 +1,205 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "base/files/file_util.h" +#include "base/files/scoped_temp_dir.h" +#include "base/path_service.h" +#include "base/strings/string_util.h" +#include "base/strings/utf_string_conversions.h" +#include "base/values.h" +#include "extensions/common/constants.h" +#include "extensions/common/extension.h" +#include "extensions/common/extension_paths.h" +#include "extensions/common/manifest_constants.h" +#include "extensions/test/test_extensions_client.h" +#include "extensions/utility/unpacker.h" +#include "testing/gtest/include/gtest/gtest.h" +#include "third_party/skia/include/core/SkBitmap.h" + +using base::ASCIIToUTF16; + +namespace extensions { + +namespace errors = manifest_errors; +namespace keys = manifest_keys; + +class UnpackerTest : public testing::Test { + public: + ~UnpackerTest() override { + VLOG(1) << "Deleting temp dir: " << temp_dir_.path().LossyDisplayName(); + VLOG(1) << temp_dir_.Delete(); + } + + void SetupUnpacker(const std::string& crx_name) { + base::FilePath original_path; + ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &original_path)); + original_path = original_path.AppendASCII("unpacker").AppendASCII(crx_name); + ASSERT_TRUE(base::PathExists(original_path)) << original_path.value(); + + // Try bots won't let us write into DIR_TEST_DATA, so we have to create + // a temp folder to play in. + ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); + + base::FilePath crx_path = temp_dir_.path().AppendASCII(crx_name); + ASSERT_TRUE(base::CopyFile(original_path, crx_path)) + << "Original path " << original_path.value() << ", Crx path " + << crx_path.value(); + + unpacker_.reset(new Unpacker(crx_path, std::string(), Manifest::INTERNAL, + Extension::NO_FLAGS)); + } + + protected: + base::ScopedTempDir temp_dir_; + scoped_ptr<Unpacker> unpacker_; +}; + +TEST_F(UnpackerTest, EmptyDefaultLocale) { + SetupUnpacker("empty_default_locale.crx"); + EXPECT_FALSE(unpacker_->Run()); + EXPECT_EQ(ASCIIToUTF16(errors::kInvalidDefaultLocale), + unpacker_->error_message()); +} + +TEST_F(UnpackerTest, HasDefaultLocaleMissingLocalesFolder) { + SetupUnpacker("has_default_missing_locales.crx"); + EXPECT_FALSE(unpacker_->Run()); + EXPECT_EQ(ASCIIToUTF16(errors::kLocalesTreeMissing), + unpacker_->error_message()); +} + +TEST_F(UnpackerTest, InvalidDefaultLocale) { + SetupUnpacker("invalid_default_locale.crx"); + EXPECT_FALSE(unpacker_->Run()); + EXPECT_EQ(ASCIIToUTF16(errors::kInvalidDefaultLocale), + unpacker_->error_message()); +} + +TEST_F(UnpackerTest, InvalidMessagesFile) { + SetupUnpacker("invalid_messages_file.crx"); + EXPECT_FALSE(unpacker_->Run()); + EXPECT_TRUE( + MatchPattern(unpacker_->error_message(), + ASCIIToUTF16( + "*_locales?en_US?messages.json: Line: 2, column: 11," + " Syntax error."))) + << unpacker_->error_message(); +} + +TEST_F(UnpackerTest, MissingDefaultData) { + SetupUnpacker("missing_default_data.crx"); + EXPECT_FALSE(unpacker_->Run()); + EXPECT_EQ(ASCIIToUTF16(errors::kLocalesNoDefaultMessages), + unpacker_->error_message()); +} + +TEST_F(UnpackerTest, MissingDefaultLocaleHasLocalesFolder) { + SetupUnpacker("missing_default_has_locales.crx"); + EXPECT_FALSE(unpacker_->Run()); + EXPECT_EQ(ASCIIToUTF16(errors::kLocalesNoDefaultLocaleSpecified), + unpacker_->error_message()); +} + +TEST_F(UnpackerTest, MissingMessagesFile) { + SetupUnpacker("missing_messages_file.crx"); + EXPECT_FALSE(unpacker_->Run()); + EXPECT_TRUE(MatchPattern(unpacker_->error_message(), + ASCIIToUTF16(errors::kLocalesMessagesFileMissing) + + ASCIIToUTF16("*_locales?en_US?messages.json"))); +} + +TEST_F(UnpackerTest, NoLocaleData) { + SetupUnpacker("no_locale_data.crx"); + EXPECT_FALSE(unpacker_->Run()); + EXPECT_EQ(ASCIIToUTF16(errors::kLocalesNoDefaultMessages), + unpacker_->error_message()); +} + +TEST_F(UnpackerTest, GoodL10n) { + SetupUnpacker("good_l10n.crx"); + EXPECT_TRUE(unpacker_->Run()); + EXPECT_TRUE(unpacker_->error_message().empty()); + ASSERT_EQ(2U, unpacker_->parsed_catalogs()->size()); +} + +TEST_F(UnpackerTest, NoL10n) { + SetupUnpacker("no_l10n.crx"); + EXPECT_TRUE(unpacker_->Run()); + EXPECT_TRUE(unpacker_->error_message().empty()); + EXPECT_EQ(0U, unpacker_->parsed_catalogs()->size()); +} + +TEST_F(UnpackerTest, UnzipDirectoryError) { + const char kExpected[] = "Could not create directory for unzipping: "; + SetupUnpacker("good_package.crx"); + base::FilePath path = temp_dir_.path().AppendASCII(kTempExtensionName); + ASSERT_TRUE(base::WriteFile(path, "foo", 3)); + EXPECT_FALSE(unpacker_->Run()); + EXPECT_TRUE( + StartsWith(unpacker_->error_message(), ASCIIToUTF16(kExpected), false)) + << "Expected prefix: \"" << kExpected << "\", actual error: \"" + << unpacker_->error_message() << "\""; +} + +TEST_F(UnpackerTest, UnzipError) { + const char kExpected[] = "Could not unzip extension"; + SetupUnpacker("bad_zip.crx"); + EXPECT_FALSE(unpacker_->Run()); + EXPECT_EQ(ASCIIToUTF16(kExpected), unpacker_->error_message()); +} + +namespace { + +// Inserts an illegal path into the browser images returned by +// TestExtensionsClient for any extension. +class IllegalImagePathInserter + : public TestExtensionsClient::BrowserImagePathsFilter { + public: + IllegalImagePathInserter(TestExtensionsClient* client) : client_(client) { + client_->AddBrowserImagePathsFilter(this); + } + + virtual ~IllegalImagePathInserter() { + client_->RemoveBrowserImagePathsFilter(this); + } + + void Filter(const Extension* extension, + std::set<base::FilePath>* paths) override { + base::FilePath illegal_path = + base::FilePath(base::FilePath::kParentDirectory) + .AppendASCII(kTempExtensionName) + .AppendASCII("product_logo_128.png"); + paths->insert(illegal_path); + } + + private: + TestExtensionsClient* client_; +}; + +} // namespace + +TEST_F(UnpackerTest, BadPathError) { + const char kExpected[] = "Illegal path (absolute or relative with '..'): "; + SetupUnpacker("good_package.crx"); + IllegalImagePathInserter inserter( + static_cast<TestExtensionsClient*>(ExtensionsClient::Get())); + + EXPECT_FALSE(unpacker_->Run()); + EXPECT_TRUE( + StartsWith(unpacker_->error_message(), ASCIIToUTF16(kExpected), false)) + << "Expected prefix: \"" << kExpected << "\", actual error: \"" + << unpacker_->error_message() << "\""; +} + +TEST_F(UnpackerTest, ImageDecodingError) { + const char kExpected[] = "Could not decode image: "; + SetupUnpacker("bad_image.crx"); + EXPECT_FALSE(unpacker_->Run()); + EXPECT_TRUE( + StartsWith(unpacker_->error_message(), ASCIIToUTF16(kExpected), false)) + << "Expected prefix: \"" << kExpected << "\", actual error: \"" + << unpacker_->error_message() << "\""; +} + +} // namespace extensions |