summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/sandboxed_extension_unpacker_unittest.cc
blob: 5e190104e0adc484adac74c65a4c99ddd4d8167c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// Copyright (c) 2011 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/file_util.h"
#include "base/memory/ref_counted.h"
#include "base/message_loop.h"
#include "base/path_service.h"
#include "base/scoped_temp_dir.h"
#include "base/scoped_temp_dir.h"
#include "base/string_util.h"
#include "base/values.h"
#include "chrome/browser/extensions/sandboxed_extension_unpacker.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/extensions/extension.h"
#include "chrome/common/extensions/extension_constants.h"
#include "chrome/common/extensions/extension_unpacker.h"
#include "content/test/test_browser_thread.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/skia/include/core/SkBitmap.h"

namespace errors = extension_manifest_errors;
namespace keys = extension_manifest_keys;

using content::BrowserThread;
using testing::_;
using testing::Invoke;

namespace {

void OnUnpackSuccess(const FilePath& temp_dir,
                     const FilePath& extension_root,
                     const DictionaryValue* original_manifest,
                     const Extension* extension) {
  // Don't delete temp_dir here, we need to do some post op checking.
}

}  // namespace

class MockSandboxedExtensionUnpackerClient
    : public SandboxedExtensionUnpackerClient {
 public:
  virtual ~MockSandboxedExtensionUnpackerClient() {}

  MOCK_METHOD4(OnUnpackSuccess,
               void(const FilePath& temp_dir,
                    const FilePath& extension_root,
                    const DictionaryValue* original_manifest,
                    const Extension* extension));

  MOCK_METHOD1(OnUnpackFailure,
               void(const string16& error));

  void DelegateToFake() {
    ON_CALL(*this, OnUnpackSuccess(_, _, _, _))
        .WillByDefault(Invoke(::OnUnpackSuccess));
  }
};

class SandboxedExtensionUnpackerTest : public testing::Test {
 public:
  virtual void SetUp() {
    ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
    file_thread_.reset(new content::TestBrowserThread(BrowserThread::FILE,
                                                      &loop_));
    // It will delete itself.
    client_ = new MockSandboxedExtensionUnpackerClient;
    client_->DelegateToFake();
  }

  virtual void TearDown() {
    // Need to destruct SandboxedExtensionUnpacker before the message loop since
    // it posts a task to it.
    sandboxed_unpacker_ = NULL;
    loop_.RunAllPending();
  }

  void SetupUnpacker(const std::string& crx_name) {
    FilePath original_path;
    ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &original_path));
    original_path = original_path.AppendASCII("extensions")
        .AppendASCII("unpacker")
        .AppendASCII(crx_name);
    ASSERT_TRUE(file_util::PathExists(original_path)) << original_path.value();

    // Try bots won't let us write into DIR_TEST_DATA, so we have to write the
    // CRX to the temp directory, and create a subdirectory into which to
    // unpack it.
    FilePath crx_path = temp_dir_.path().AppendASCII(crx_name);
    ASSERT_TRUE(file_util::CopyFile(original_path, crx_path)) <<
        "Original path: " << original_path.value() <<
        ", Crx path: " << crx_path.value();

    unpacker_.reset(new ExtensionUnpacker(
        crx_path, Extension::INTERNAL, Extension::NO_FLAGS));

    // Build a temp area where the extension will be unpacked.
    temp_path_ =
        temp_dir_.path().AppendASCII("sandboxed_extension_unpacker_test_Temp");
    ASSERT_TRUE(file_util::CreateDirectory(temp_path_));

    sandboxed_unpacker_ =
        new SandboxedExtensionUnpacker(crx_path, NULL, Extension::INTERNAL,
                                       Extension::NO_FLAGS, client_);

    // Hack since SandboxedExtensionUnpacker gets its background thread id from
    // the Start call, but we don't call it here.
    sandboxed_unpacker_->thread_identifier_ = BrowserThread::FILE;
    EXPECT_TRUE(PrepareUnpackerEnv());
  }

  bool PrepareUnpackerEnv() {
    sandboxed_unpacker_->extension_root_ =
      temp_dir_.path().AppendASCII(extension_filenames::kTempExtensionName);

    if (!sandboxed_unpacker_->temp_dir_.Set(temp_dir_.path()))
      return false;
    sandboxed_unpacker_->public_key_ =
      "ocnapchkplbmjmpfehjocmjnipfmogkh";
    return true;
  }

  void OnUnpackSucceeded() {
    sandboxed_unpacker_->OnUnpackExtensionSucceeded(
        *unpacker_->parsed_manifest());
  }

  FilePath GetInstallPath() {
    return temp_dir_.path().AppendASCII(
        extension_filenames::kTempExtensionName);
  }

  bool TempFilesRemoved() {
    // Check that temporary files were cleaned up.
    file_util::FileEnumerator::FileType files_and_dirs =
      static_cast<file_util::FileEnumerator::FileType>(
        file_util::FileEnumerator::DIRECTORIES |
        file_util::FileEnumerator::FILES);

    file_util::FileEnumerator temp_iterator(
      temp_path_,
      true,  // recursive
      files_and_dirs
    );
    int items_not_removed = 0;
    FilePath item_in_temp;
    item_in_temp = temp_iterator.Next();
    while (!item_in_temp.value().empty()) {
      items_not_removed++;
      EXPECT_STREQ(FILE_PATH_LITERAL(""), item_in_temp.value().c_str())
        << "File was not removed on success.";
      item_in_temp = temp_iterator.Next();
    }
    return (items_not_removed == 0);
  }

 protected:
  ScopedTempDir temp_dir_;
  FilePath temp_path_;
  MockSandboxedExtensionUnpackerClient* client_;
  scoped_ptr<ExtensionUnpacker> unpacker_;
  scoped_refptr<SandboxedExtensionUnpacker> sandboxed_unpacker_;
  MessageLoop loop_;
  scoped_ptr<content::TestBrowserThread> file_thread_;
};

TEST_F(SandboxedExtensionUnpackerTest, NoCatalogsSuccess) {
  EXPECT_CALL(*client_, OnUnpackSuccess(_, _, _, _));
  EXPECT_CALL(*client_, OnUnpackFailure(_)).Times(0);

  SetupUnpacker("no_l10n.crx");
  ASSERT_TRUE(unpacker_->Run());
  ASSERT_TRUE(unpacker_->DumpImagesToFile());
  ASSERT_TRUE(unpacker_->DumpMessageCatalogsToFile());

  // Check that there is no _locales folder.
  FilePath install_path =
    GetInstallPath().Append(Extension::kLocaleFolder);
  EXPECT_FALSE(file_util::PathExists(install_path));

  OnUnpackSucceeded();

  // Check that there still is no _locales folder.
  EXPECT_FALSE(file_util::PathExists(install_path));

  ASSERT_TRUE(TempFilesRemoved());
}

TEST_F(SandboxedExtensionUnpackerTest, WithCatalogsSuccess) {
  EXPECT_CALL(*client_, OnUnpackSuccess(_, _, _, _));
  EXPECT_CALL(*client_, OnUnpackFailure(_)).Times(0);

  SetupUnpacker("good_l10n.crx");
  ASSERT_TRUE(unpacker_->Run());
  ASSERT_TRUE(unpacker_->DumpImagesToFile());
  ASSERT_TRUE(unpacker_->DumpMessageCatalogsToFile());

  // Set timestamp on _locales/en_US/messages.json into the past.
  FilePath messages_file;
  messages_file = GetInstallPath().Append(Extension::kLocaleFolder)
      .AppendASCII("en_US")
      .Append(Extension::kMessagesFilename);
  base::PlatformFileInfo old_info;
  EXPECT_TRUE(file_util::GetFileInfo(messages_file, &old_info));
  base::Time old_time =
      old_info.last_modified - base::TimeDelta::FromSeconds(2);
  EXPECT_TRUE(file_util::SetLastModifiedTime(messages_file, old_time));
  // Refresh old_info, just to be sure.
  EXPECT_TRUE(file_util::GetFileInfo(messages_file, &old_info));

  OnUnpackSucceeded();

  // Check that there is newer _locales/en_US/messages.json file.
  base::PlatformFileInfo new_info;
  EXPECT_TRUE(file_util::GetFileInfo(messages_file, &new_info));

  EXPECT_TRUE(new_info.last_modified > old_info.last_modified);

  ASSERT_TRUE(TempFilesRemoved());
}