extension_unpacker_unittest.cc revision ddb351dbec246cf1fab5ec20d2d5520909041de1
1// Copyright (c) 2011 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/string_util.h" 8#include "base/values.h" 9#include "base/memory/scoped_temp_dir.h" 10#include "chrome/common/chrome_paths.h" 11#include "chrome/common/extensions/extension_constants.h" 12#include "chrome/common/extensions/extension_unpacker.h" 13#include "testing/gtest/include/gtest/gtest.h" 14#include "third_party/skia/include/core/SkBitmap.h" 15 16namespace errors = extension_manifest_errors; 17namespace keys = extension_manifest_keys; 18 19class ExtensionUnpackerTest : public testing::Test { 20public: 21 void SetupUnpacker(const std::string& crx_name) { 22 FilePath original_path; 23 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &original_path)); 24 original_path = original_path.AppendASCII("extensions") 25 .AppendASCII("unpacker") 26 .AppendASCII(crx_name); 27 ASSERT_TRUE(file_util::PathExists(original_path)) << original_path.value(); 28 29 // Try bots won't let us write into DIR_TEST_DATA, so we have to create 30 // a temp folder to play in. 31 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 32 33 FilePath crx_path = temp_dir_.path().AppendASCII(crx_name); 34 ASSERT_TRUE(file_util::CopyFile(original_path, crx_path)) << 35 "Original path " << original_path.value() << 36 ", Crx path " << crx_path.value(); 37 38 unpacker_.reset(new ExtensionUnpacker(crx_path)); 39 } 40 41 protected: 42 ScopedTempDir temp_dir_; 43 scoped_ptr<ExtensionUnpacker> unpacker_; 44}; 45 46TEST_F(ExtensionUnpackerTest, EmptyDefaultLocale) { 47 SetupUnpacker("empty_default_locale.crx"); 48 EXPECT_FALSE(unpacker_->Run()); 49 EXPECT_EQ(errors::kInvalidDefaultLocale, unpacker_->error_message()); 50} 51 52TEST_F(ExtensionUnpackerTest, HasDefaultLocaleMissingLocalesFolder) { 53 SetupUnpacker("has_default_missing_locales.crx"); 54 EXPECT_FALSE(unpacker_->Run()); 55 EXPECT_EQ(errors::kLocalesTreeMissing, unpacker_->error_message()); 56} 57 58TEST_F(ExtensionUnpackerTest, InvalidDefaultLocale) { 59 SetupUnpacker("invalid_default_locale.crx"); 60 EXPECT_FALSE(unpacker_->Run()); 61 EXPECT_EQ(errors::kInvalidDefaultLocale, unpacker_->error_message()); 62} 63 64TEST_F(ExtensionUnpackerTest, InvalidMessagesFile) { 65 SetupUnpacker("invalid_messages_file.crx"); 66 EXPECT_FALSE(unpacker_->Run()); 67 EXPECT_TRUE(MatchPattern(unpacker_->error_message(), 68 std::string("*_locales?en_US?messages.json: Line: 2, column: 3," 69 " Dictionary keys must be quoted."))); 70} 71 72TEST_F(ExtensionUnpackerTest, MissingDefaultData) { 73 SetupUnpacker("missing_default_data.crx"); 74 EXPECT_FALSE(unpacker_->Run()); 75 EXPECT_EQ(errors::kLocalesNoDefaultMessages, unpacker_->error_message()); 76} 77 78TEST_F(ExtensionUnpackerTest, MissingDefaultLocaleHasLocalesFolder) { 79 SetupUnpacker("missing_default_has_locales.crx"); 80 EXPECT_FALSE(unpacker_->Run()); 81 EXPECT_EQ(errors::kLocalesNoDefaultLocaleSpecified, 82 unpacker_->error_message()); 83} 84 85TEST_F(ExtensionUnpackerTest, MissingMessagesFile) { 86 SetupUnpacker("missing_messages_file.crx"); 87 EXPECT_FALSE(unpacker_->Run()); 88 EXPECT_TRUE(MatchPattern(unpacker_->error_message(), 89 errors::kLocalesMessagesFileMissing + 90 std::string("*_locales?en_US?messages.json"))); 91} 92 93TEST_F(ExtensionUnpackerTest, NoLocaleData) { 94 SetupUnpacker("no_locale_data.crx"); 95 EXPECT_FALSE(unpacker_->Run()); 96 EXPECT_EQ(errors::kLocalesNoDefaultMessages, unpacker_->error_message()); 97} 98 99TEST_F(ExtensionUnpackerTest, GoodL10n) { 100 SetupUnpacker("good_l10n.crx"); 101 EXPECT_TRUE(unpacker_->Run()); 102 EXPECT_TRUE(unpacker_->error_message().empty()); 103 ASSERT_EQ(2U, unpacker_->parsed_catalogs()->size()); 104} 105 106TEST_F(ExtensionUnpackerTest, NoL10n) { 107 SetupUnpacker("no_l10n.crx"); 108 EXPECT_TRUE(unpacker_->Run()); 109 EXPECT_TRUE(unpacker_->error_message().empty()); 110 EXPECT_EQ(0U, unpacker_->parsed_catalogs()->size()); 111} 112