summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/extension_file_util_unittest.cc
blob: 73dfb46e63fb378e87b738a5a8aecd7821775ee5 (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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// Copyright (c) 2009 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 "chrome/browser/extensions/extension_file_util.h"

#include "base/file_util.h"
#include "base/scoped_temp_dir.h"
#include "base/path_service.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/extensions/extension.h"
#include "chrome/common/extensions/extension_constants.h"
#include "chrome/common/json_value_serializer.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace keys = extension_manifest_keys;

TEST(ExtensionFileUtil, MoveDirSafely) {
  // Create a test directory structure with some data in it.
  ScopedTempDir temp;
  ASSERT_TRUE(temp.CreateUniqueTempDir());

  FilePath src_path = temp.path().AppendASCII("src");
  ASSERT_TRUE(file_util::CreateDirectory(src_path));

  std::string data = "foobar";
  ASSERT_TRUE(file_util::WriteFile(src_path.AppendASCII("data"),
                                   data.c_str(), data.length()));

  // Move it to a path that doesn't exist yet.
  FilePath dest_path = temp.path().AppendASCII("dest").AppendASCII("dest");
  ASSERT_TRUE(extension_file_util::MoveDirSafely(src_path, dest_path));

  // The path should get created.
  ASSERT_TRUE(file_util::DirectoryExists(dest_path));

  // The data should match.
  std::string data_out;
  ASSERT_TRUE(file_util::ReadFileToString(dest_path.AppendASCII("data"),
                                          &data_out));
  ASSERT_EQ(data, data_out);

  // The src path should be gone.
  ASSERT_FALSE(file_util::PathExists(src_path));

  // Create some new test data.
  ASSERT_TRUE(file_util::CopyDirectory(dest_path, src_path,
                                       true));  // recursive
  data = "hotdog";
  ASSERT_TRUE(file_util::WriteFile(src_path.AppendASCII("data"),
                                   data.c_str(), data.length()));

  // Test again, overwriting the old path.
  ASSERT_TRUE(extension_file_util::MoveDirSafely(src_path, dest_path));
  ASSERT_TRUE(file_util::DirectoryExists(dest_path));

  data_out.clear();
  ASSERT_TRUE(file_util::ReadFileToString(dest_path.AppendASCII("data"),
                                          &data_out));
  ASSERT_EQ(data, data_out);
  ASSERT_FALSE(file_util::PathExists(src_path));
}

TEST(ExtensionFileUtil, SetCurrentVersion) {
  // Create an empty test directory.
  ScopedTempDir temp;
  ASSERT_TRUE(temp.CreateUniqueTempDir());

  // Set its version.
  std::string error;
  std::string version = "1.0";
  ASSERT_TRUE(extension_file_util::SetCurrentVersion(temp.path(), version,
                                                     &error));

  // Test the result.
  std::string version_out;
  ASSERT_TRUE(file_util::ReadFileToString(
      temp.path().AppendASCII(extension_file_util::kCurrentVersionFileName),
      &version_out));
  ASSERT_EQ(version, version_out);

  // Try again, overwriting the old value.
  version = "2.0";
  version_out.clear();
  ASSERT_TRUE(extension_file_util::SetCurrentVersion(temp.path(), version,
                                                     &error));
  ASSERT_TRUE(file_util::ReadFileToString(
      temp.path().AppendASCII(extension_file_util::kCurrentVersionFileName),
      &version_out));
  ASSERT_EQ(version, version_out);
}

TEST(ExtensionFileUtil, ReadCurrentVersion) {
  // Read the version from a valid extension.
  FilePath extension_path;
  ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &extension_path));
  extension_path = extension_path.AppendASCII("extensions")
      .AppendASCII("good")
      .AppendASCII("Extensions")
      .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj");

  std::string version;
  ASSERT_TRUE(extension_file_util::ReadCurrentVersion(extension_path,
                                                      &version));
  ASSERT_EQ("1.0.0.0", version);

  // Create an invalid extension and read its current version.
  ScopedTempDir temp;
  ASSERT_TRUE(temp.CreateUniqueTempDir());
  ASSERT_FALSE(extension_file_util::ReadCurrentVersion(temp.path(), &version));
}

TEST(ExtensionFileUtil, CompareToInstalledVersion) {
  // Compare to an existing extension.
  FilePath install_directory;
  ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &install_directory));
  install_directory = install_directory.AppendASCII("extensions")
      .AppendASCII("good")
      .AppendASCII("Extensions");

  std::string id = "behllobkkfkfnphdnhnkndlbkcpglgmj";
  std::string version = "1.0.0.0";
  std::string version_out;

  ASSERT_EQ(Extension::UPGRADE, extension_file_util::CompareToInstalledVersion(
    install_directory, id, "1.0.0.1", &version_out));
  ASSERT_EQ(version, version_out);

  version_out.clear();
  ASSERT_EQ(Extension::REINSTALL,
            extension_file_util::CompareToInstalledVersion(
                install_directory, id, "1.0.0.0", &version_out));
  ASSERT_EQ(version, version_out);

  version_out.clear();
  ASSERT_EQ(Extension::REINSTALL,
            extension_file_util::CompareToInstalledVersion(
                install_directory, id, "1.0.0", &version_out));
  ASSERT_EQ(version, version_out);

  version_out.clear();
  ASSERT_EQ(Extension::DOWNGRADE,
            extension_file_util::CompareToInstalledVersion(
                install_directory, id, "0.0.1.0", &version_out));
  ASSERT_EQ(version, version_out);

  // Compare to an extension that is missing its Current Version file.
  ScopedTempDir temp;
  ASSERT_TRUE(temp.CreateUniqueTempDir());
  FilePath src = install_directory.AppendASCII(id).AppendASCII(version);
  FilePath dest = temp.path().AppendASCII(id).AppendASCII(version);
  ASSERT_TRUE(file_util::CreateDirectory(dest.DirName()));
  ASSERT_TRUE(file_util::CopyDirectory(src, dest, true));

  version_out.clear();
  ASSERT_EQ(Extension::NEW_INSTALL,
            extension_file_util::CompareToInstalledVersion(
                temp.path(), id, "0.0.1.0", &version_out));
  ASSERT_EQ("", version_out);

  // Compare to a completely non-existent extension.
  version_out.clear();
  ASSERT_EQ(Extension::NEW_INSTALL,
            extension_file_util::CompareToInstalledVersion(
                temp.path(), "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "0.0.1.0",
                &version_out));
  ASSERT_EQ("", version_out);
}

// Creates minimal manifest, with or without default_locale section.
bool CreateMinimalManifest(const std::string& locale,
                           const FilePath& manifest_path) {
  DictionaryValue manifest;

  manifest.SetString(keys::kVersion, "1.0.0.0");
  manifest.SetString(keys::kName, "my extension");
  if (!locale.empty()) {
    manifest.SetString(keys::kDefaultLocale, locale);
  }

  JSONFileValueSerializer serializer(manifest_path);
  return serializer.Serialize(manifest);
}

TEST(ExtensionFileUtil, LoadExtensionWithValidLocales) {
  ScopedTempDir temp;
  ASSERT_TRUE(temp.CreateUniqueTempDir());
  ASSERT_TRUE(CreateMinimalManifest(
      "en_US", temp.path().AppendASCII(Extension::kManifestFilename)));

  FilePath src_path = temp.path().AppendASCII(Extension::kLocaleFolder);
  ASSERT_TRUE(file_util::CreateDirectory(src_path));

  FilePath locale_1 = src_path.AppendASCII("sr");
  ASSERT_TRUE(file_util::CreateDirectory(locale_1));

  std::string data = "foobar";
  ASSERT_TRUE(
      file_util::WriteFile(locale_1.AppendASCII(Extension::kMessagesFilename),
                           data.c_str(), data.length()));

  FilePath locale_2 = src_path.AppendASCII("en_US");
  ASSERT_TRUE(file_util::CreateDirectory(locale_2));

  ASSERT_TRUE(
      file_util::WriteFile(locale_2.AppendASCII(Extension::kMessagesFilename),
                           data.c_str(), data.length()));

  std::string error;
  scoped_ptr<Extension> extension(
      extension_file_util::LoadExtension(temp.path(), false, &error));
  ASSERT_FALSE(extension == NULL);
  EXPECT_EQ(static_cast<unsigned int>(2),
            extension->supported_locales().size());
  EXPECT_EQ("en-US", extension->default_locale());
}

TEST(ExtensionFileUtil, LoadExtensionWithoutLocalesFolder) {
  ScopedTempDir temp;
  ASSERT_TRUE(temp.CreateUniqueTempDir());
  ASSERT_TRUE(CreateMinimalManifest(
      "", temp.path().AppendASCII(Extension::kManifestFilename)));

  std::string error;
  scoped_ptr<Extension> extension(
      extension_file_util::LoadExtension(temp.path(), false, &error));
  ASSERT_FALSE(extension == NULL);
  EXPECT_TRUE(extension->supported_locales().empty());
  EXPECT_TRUE(extension->default_locale().empty());
}

TEST(ExtensionFileUtil, CheckIllegalFilenamesNoUnderscores) {
  ScopedTempDir temp;
  ASSERT_TRUE(temp.CreateUniqueTempDir());

  FilePath src_path = temp.path().AppendASCII("some_dir");
  ASSERT_TRUE(file_util::CreateDirectory(src_path));

  std::string data = "foobar";
  ASSERT_TRUE(file_util::WriteFile(src_path.AppendASCII("some_file.txt"),
                                   data.c_str(), data.length()));
  std::string error;
  EXPECT_TRUE(extension_file_util::CheckForIllegalFilenames(temp.path(),
                                                            &error));
}

TEST(ExtensionFileUtil, CheckIllegalFilenamesOnlyReserved) {
  ScopedTempDir temp;
  ASSERT_TRUE(temp.CreateUniqueTempDir());

  FilePath src_path = temp.path().AppendASCII(Extension::kLocaleFolder);
  ASSERT_TRUE(file_util::CreateDirectory(src_path));

  std::string error;
  EXPECT_TRUE(extension_file_util::CheckForIllegalFilenames(temp.path(),
                                                            &error));
}

TEST(ExtensionFileUtil, CheckIllegalFilenamesReservedAndIllegal) {
  ScopedTempDir temp;
  ASSERT_TRUE(temp.CreateUniqueTempDir());

  FilePath src_path = temp.path().AppendASCII(Extension::kLocaleFolder);
  ASSERT_TRUE(file_util::CreateDirectory(src_path));

  src_path = temp.path().AppendASCII("_some_dir");
  ASSERT_TRUE(file_util::CreateDirectory(src_path));

  std::string error;
  EXPECT_FALSE(extension_file_util::CheckForIllegalFilenames(temp.path(),
                                                             &error));
}

// TODO(aa): More tests as motivation allows. Maybe steal some from
// ExtensionsService? Many of them could probably be tested here without the
// MessageLoop shenanigans.