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/files/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
19using base::ASCIIToUTF16;
20
21namespace extensions {
22
23namespace errors = manifest_errors;
24namespace keys = manifest_keys;
25
26class UnpackerTest : public testing::Test {
27 public:
28  virtual ~UnpackerTest() {
29    LOG(WARNING) << "Deleting temp dir: "
30                 << temp_dir_.path().LossyDisplayName();
31    LOG(WARNING) << temp_dir_.Delete();
32  }
33
34  void SetupUnpacker(const std::string& crx_name) {
35    base::FilePath original_path;
36    ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &original_path));
37    original_path = original_path.AppendASCII("extensions")
38        .AppendASCII("unpacker")
39        .AppendASCII(crx_name);
40    ASSERT_TRUE(base::PathExists(original_path)) << original_path.value();
41
42    // Try bots won't let us write into DIR_TEST_DATA, so we have to create
43    // a temp folder to play in.
44    ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
45
46    base::FilePath crx_path = temp_dir_.path().AppendASCII(crx_name);
47    ASSERT_TRUE(base::CopyFile(original_path, crx_path)) <<
48        "Original path " << original_path.value() <<
49        ", Crx path " << crx_path.value();
50
51    unpacker_.reset(new Unpacker(crx_path,
52                                 std::string(),
53                                 Manifest::INTERNAL,
54                                 Extension::NO_FLAGS));
55  }
56
57 protected:
58  base::ScopedTempDir temp_dir_;
59  scoped_ptr<Unpacker> unpacker_;
60};
61
62TEST_F(UnpackerTest, EmptyDefaultLocale) {
63  SetupUnpacker("empty_default_locale.crx");
64  EXPECT_FALSE(unpacker_->Run());
65  EXPECT_EQ(ASCIIToUTF16(errors::kInvalidDefaultLocale),
66            unpacker_->error_message());
67}
68
69TEST_F(UnpackerTest, HasDefaultLocaleMissingLocalesFolder) {
70  SetupUnpacker("has_default_missing_locales.crx");
71  EXPECT_FALSE(unpacker_->Run());
72  EXPECT_EQ(ASCIIToUTF16(errors::kLocalesTreeMissing),
73            unpacker_->error_message());
74}
75
76TEST_F(UnpackerTest, InvalidDefaultLocale) {
77  SetupUnpacker("invalid_default_locale.crx");
78  EXPECT_FALSE(unpacker_->Run());
79  EXPECT_EQ(ASCIIToUTF16(errors::kInvalidDefaultLocale),
80            unpacker_->error_message());
81}
82
83TEST_F(UnpackerTest, InvalidMessagesFile) {
84  SetupUnpacker("invalid_messages_file.crx");
85  EXPECT_FALSE(unpacker_->Run());
86  EXPECT_TRUE(MatchPattern(unpacker_->error_message(),
87    ASCIIToUTF16("*_locales?en_US?messages.json: Line: 2, column: 11,"
88        " Syntax error."))) << unpacker_->error_message();
89}
90
91TEST_F(UnpackerTest, MissingDefaultData) {
92  SetupUnpacker("missing_default_data.crx");
93  EXPECT_FALSE(unpacker_->Run());
94  EXPECT_EQ(ASCIIToUTF16(errors::kLocalesNoDefaultMessages),
95            unpacker_->error_message());
96}
97
98TEST_F(UnpackerTest, MissingDefaultLocaleHasLocalesFolder) {
99  SetupUnpacker("missing_default_has_locales.crx");
100  EXPECT_FALSE(unpacker_->Run());
101  EXPECT_EQ(ASCIIToUTF16(errors::kLocalesNoDefaultLocaleSpecified),
102            unpacker_->error_message());
103}
104
105TEST_F(UnpackerTest, MissingMessagesFile) {
106  SetupUnpacker("missing_messages_file.crx");
107  EXPECT_FALSE(unpacker_->Run());
108  EXPECT_TRUE(MatchPattern(unpacker_->error_message(),
109    ASCIIToUTF16(errors::kLocalesMessagesFileMissing) +
110    ASCIIToUTF16("*_locales?en_US?messages.json")));
111}
112
113TEST_F(UnpackerTest, NoLocaleData) {
114  SetupUnpacker("no_locale_data.crx");
115  EXPECT_FALSE(unpacker_->Run());
116  EXPECT_EQ(ASCIIToUTF16(errors::kLocalesNoDefaultMessages),
117            unpacker_->error_message());
118}
119
120TEST_F(UnpackerTest, GoodL10n) {
121  SetupUnpacker("good_l10n.crx");
122  EXPECT_TRUE(unpacker_->Run());
123  EXPECT_TRUE(unpacker_->error_message().empty());
124  ASSERT_EQ(2U, unpacker_->parsed_catalogs()->size());
125}
126
127TEST_F(UnpackerTest, NoL10n) {
128  SetupUnpacker("no_l10n.crx");
129  EXPECT_TRUE(unpacker_->Run());
130  EXPECT_TRUE(unpacker_->error_message().empty());
131  EXPECT_EQ(0U, unpacker_->parsed_catalogs()->size());
132}
133
134TEST_F(UnpackerTest, UnzipDirectoryError) {
135  const char* kExpected = "Could not create directory for unzipping: ";
136  SetupUnpacker("good_package.crx");
137  base::FilePath path =
138      temp_dir_.path().AppendASCII(kTempExtensionName);
139  ASSERT_TRUE(base::WriteFile(path, "foo", 3));
140  EXPECT_FALSE(unpacker_->Run());
141  EXPECT_TRUE(StartsWith(unpacker_->error_message(),
142              ASCIIToUTF16(kExpected),
143              false)) << "Expected prefix: \"" << kExpected
144                      << "\", actual error: \"" << unpacker_->error_message()
145                      << "\"";
146}
147
148TEST_F(UnpackerTest, UnzipError) {
149  const char* kExpected = "Could not unzip extension";
150  SetupUnpacker("bad_zip.crx");
151  EXPECT_FALSE(unpacker_->Run());
152  EXPECT_EQ(ASCIIToUTF16(kExpected), unpacker_->error_message());
153}
154
155TEST_F(UnpackerTest, BadPathError) {
156  const char* kExpected = "Illegal path (absolute or relative with '..'): ";
157  SetupUnpacker("bad_path.crx");
158  EXPECT_FALSE(unpacker_->Run());
159  EXPECT_TRUE(StartsWith(unpacker_->error_message(),
160              ASCIIToUTF16(kExpected),
161              false)) << "Expected prefix: \"" << kExpected
162                      << "\", actual error: \"" << unpacker_->error_message()
163                      << "\"";
164}
165
166
167TEST_F(UnpackerTest, ImageDecodingError) {
168  const char* kExpected = "Could not decode image: ";
169  SetupUnpacker("bad_image.crx");
170  EXPECT_FALSE(unpacker_->Run());
171  EXPECT_TRUE(StartsWith(unpacker_->error_message(),
172              ASCIIToUTF16(kExpected),
173              false)) << "Expected prefix: \"" << kExpected
174                      << "\", actual error: \"" << unpacker_->error_message()
175                      << "\"";
176}
177
178}  // namespace extensions
179