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.
4
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/utility/extensions/unpacker.h"
13 #include "extensions/common/constants.h"
14 #include "extensions/common/extension.h"
15 #include "extensions/common/manifest_constants.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17 #include "third_party/skia/include/core/SkBitmap.h"
18
19 namespace extensions {
20
21 namespace errors = manifest_errors;
22 namespace keys = manifest_keys;
23
24 class UnpackerTest : public testing::Test {
25 public:
~UnpackerTest()26 virtual ~UnpackerTest() {
27 LOG(WARNING) << "Deleting temp dir: "
28 << temp_dir_.path().LossyDisplayName();
29 LOG(WARNING) << temp_dir_.Delete();
30 }
31
SetupUnpacker(const std::string & crx_name)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();
39
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());
43
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();
48
49 unpacker_.reset(new Unpacker(crx_path,
50 std::string(),
51 Manifest::INTERNAL,
52 Extension::NO_FLAGS));
53 }
54
55 protected:
56 base::ScopedTempDir temp_dir_;
57 scoped_ptr<Unpacker> unpacker_;
58 };
59
TEST_F(UnpackerTest,EmptyDefaultLocale)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());
65 }
66
TEST_F(UnpackerTest,HasDefaultLocaleMissingLocalesFolder)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());
72 }
73
TEST_F(UnpackerTest,InvalidDefaultLocale)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());
79 }
80
TEST_F(UnpackerTest,InvalidMessagesFile)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();
87 }
88
TEST_F(UnpackerTest,MissingDefaultData)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());
94 }
95
TEST_F(UnpackerTest,MissingDefaultLocaleHasLocalesFolder)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());
101 }
102
TEST_F(UnpackerTest,MissingMessagesFile)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")));
109 }
110
TEST_F(UnpackerTest,NoLocaleData)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());
116 }
117
TEST_F(UnpackerTest,GoodL10n)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());
123 }
124
TEST_F(UnpackerTest,NoL10n)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());
130 }
131
TEST_F(UnpackerTest,UnzipDirectoryError)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 << "\"";
144 }
145
TEST_F(UnpackerTest,UnzipError)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());
151 }
152
TEST_F(UnpackerTest,BadPathError)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 << "\"";
162 }
163
164
TEST_F(UnpackerTest,ImageDecodingError)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 << "\"";
174 }
175
176 } // namespace extensions
177