Allow overlapping sync and async startup requests
[chromium-blink-merge.git] / chrome / utility / extensions / unpacker_unittest.cc
blobb61a42cc86a37340e687569abf1d714aec983d4a
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
3 // found in the LICENSE file.
5 #include "base/file_util.h"
6 #include "base/files/scoped_temp_dir.h"
7 #include "base/path_service.h"
8 #include "base/strings/string_util.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "base/values.h"
11 #include "chrome/common/chrome_paths.h"
12 #include "chrome/common/extensions/extension.h"
13 #include "chrome/common/extensions/extension_manifest_constants.h"
14 #include "chrome/utility/extensions/unpacker.h"
15 #include "extensions/common/constants.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17 #include "third_party/skia/include/core/SkBitmap.h"
19 namespace errors = extension_manifest_errors;
20 namespace keys = extensions::manifest_keys;
22 namespace extensions {
24 class UnpackerTest : public testing::Test {
25 public:
26 virtual ~UnpackerTest() {
27 LOG(WARNING) << "Deleting temp dir: "
28 << temp_dir_.path().LossyDisplayName();
29 LOG(WARNING) << temp_dir_.Delete();
32 void SetupUnpacker(const std::string& crx_name) {
33 base::FilePath original_path;
34 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &original_path));
35 original_path = original_path.AppendASCII("extensions")
36 .AppendASCII("unpacker")
37 .AppendASCII(crx_name);
38 ASSERT_TRUE(base::PathExists(original_path)) << original_path.value();
40 // Try bots won't let us write into DIR_TEST_DATA, so we have to create
41 // a temp folder to play in.
42 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
44 base::FilePath crx_path = temp_dir_.path().AppendASCII(crx_name);
45 ASSERT_TRUE(base::CopyFile(original_path, crx_path)) <<
46 "Original path " << original_path.value() <<
47 ", Crx path " << crx_path.value();
49 unpacker_.reset(new Unpacker(crx_path,
50 std::string(),
51 Manifest::INTERNAL,
52 Extension::NO_FLAGS));
55 protected:
56 base::ScopedTempDir temp_dir_;
57 scoped_ptr<Unpacker> unpacker_;
60 TEST_F(UnpackerTest, EmptyDefaultLocale) {
61 SetupUnpacker("empty_default_locale.crx");
62 EXPECT_FALSE(unpacker_->Run());
63 EXPECT_EQ(ASCIIToUTF16(errors::kInvalidDefaultLocale),
64 unpacker_->error_message());
67 TEST_F(UnpackerTest, HasDefaultLocaleMissingLocalesFolder) {
68 SetupUnpacker("has_default_missing_locales.crx");
69 EXPECT_FALSE(unpacker_->Run());
70 EXPECT_EQ(ASCIIToUTF16(errors::kLocalesTreeMissing),
71 unpacker_->error_message());
74 TEST_F(UnpackerTest, InvalidDefaultLocale) {
75 SetupUnpacker("invalid_default_locale.crx");
76 EXPECT_FALSE(unpacker_->Run());
77 EXPECT_EQ(ASCIIToUTF16(errors::kInvalidDefaultLocale),
78 unpacker_->error_message());
81 TEST_F(UnpackerTest, InvalidMessagesFile) {
82 SetupUnpacker("invalid_messages_file.crx");
83 EXPECT_FALSE(unpacker_->Run());
84 EXPECT_TRUE(MatchPattern(unpacker_->error_message(),
85 ASCIIToUTF16("*_locales?en_US?messages.json: Line: 2, column: 11,"
86 " Syntax error."))) << unpacker_->error_message();
89 TEST_F(UnpackerTest, MissingDefaultData) {
90 SetupUnpacker("missing_default_data.crx");
91 EXPECT_FALSE(unpacker_->Run());
92 EXPECT_EQ(ASCIIToUTF16(errors::kLocalesNoDefaultMessages),
93 unpacker_->error_message());
96 TEST_F(UnpackerTest, MissingDefaultLocaleHasLocalesFolder) {
97 SetupUnpacker("missing_default_has_locales.crx");
98 EXPECT_FALSE(unpacker_->Run());
99 EXPECT_EQ(ASCIIToUTF16(errors::kLocalesNoDefaultLocaleSpecified),
100 unpacker_->error_message());
103 TEST_F(UnpackerTest, MissingMessagesFile) {
104 SetupUnpacker("missing_messages_file.crx");
105 EXPECT_FALSE(unpacker_->Run());
106 EXPECT_TRUE(MatchPattern(unpacker_->error_message(),
107 ASCIIToUTF16(errors::kLocalesMessagesFileMissing) +
108 ASCIIToUTF16("*_locales?en_US?messages.json")));
111 TEST_F(UnpackerTest, NoLocaleData) {
112 SetupUnpacker("no_locale_data.crx");
113 EXPECT_FALSE(unpacker_->Run());
114 EXPECT_EQ(ASCIIToUTF16(errors::kLocalesNoDefaultMessages),
115 unpacker_->error_message());
118 TEST_F(UnpackerTest, GoodL10n) {
119 SetupUnpacker("good_l10n.crx");
120 EXPECT_TRUE(unpacker_->Run());
121 EXPECT_TRUE(unpacker_->error_message().empty());
122 ASSERT_EQ(2U, unpacker_->parsed_catalogs()->size());
125 TEST_F(UnpackerTest, NoL10n) {
126 SetupUnpacker("no_l10n.crx");
127 EXPECT_TRUE(unpacker_->Run());
128 EXPECT_TRUE(unpacker_->error_message().empty());
129 EXPECT_EQ(0U, unpacker_->parsed_catalogs()->size());
132 TEST_F(UnpackerTest, UnzipDirectoryError) {
133 const char* kExpected = "Could not create directory for unzipping: ";
134 SetupUnpacker("good_package.crx");
135 base::FilePath path =
136 temp_dir_.path().AppendASCII(kTempExtensionName);
137 ASSERT_TRUE(file_util::WriteFile(path, "foo", 3));
138 EXPECT_FALSE(unpacker_->Run());
139 EXPECT_TRUE(StartsWith(unpacker_->error_message(),
140 ASCIIToUTF16(kExpected),
141 false)) << "Expected prefix: \"" << kExpected
142 << "\", actual error: \"" << unpacker_->error_message()
143 << "\"";
146 TEST_F(UnpackerTest, UnzipError) {
147 const char* kExpected = "Could not unzip extension";
148 SetupUnpacker("bad_zip.crx");
149 EXPECT_FALSE(unpacker_->Run());
150 EXPECT_EQ(ASCIIToUTF16(kExpected), unpacker_->error_message());
153 TEST_F(UnpackerTest, BadPathError) {
154 const char* kExpected = "Illegal path (absolute or relative with '..'): ";
155 SetupUnpacker("bad_path.crx");
156 EXPECT_FALSE(unpacker_->Run());
157 EXPECT_TRUE(StartsWith(unpacker_->error_message(),
158 ASCIIToUTF16(kExpected),
159 false)) << "Expected prefix: \"" << kExpected
160 << "\", actual error: \"" << unpacker_->error_message()
161 << "\"";
165 TEST_F(UnpackerTest, ImageDecodingError) {
166 const char* kExpected = "Could not decode image: ";
167 SetupUnpacker("bad_image.crx");
168 EXPECT_FALSE(unpacker_->Run());
169 EXPECT_TRUE(StartsWith(unpacker_->error_message(),
170 ASCIIToUTF16(kExpected),
171 false)) << "Expected prefix: \"" << kExpected
172 << "\", actual error: \"" << unpacker_->error_message()
173 << "\"";
176 } // namespace extensions