OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/common/extensions/extension_file_util.h" | 5 #include "chrome/common/extensions/extension_file_util.h" |
6 | 6 |
7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
8 #include "base/files/scoped_temp_dir.h" | 8 #include "base/files/scoped_temp_dir.h" |
9 #include "base/json/json_string_value_serializer.h" | 9 #include "base/json/json_string_value_serializer.h" |
10 #include "base/path_service.h" | 10 #include "base/path_service.h" |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
136 } | 136 } |
137 | 137 |
138 TEST_F(ExtensionFileUtilTest, CheckIllegalFilenamesNoUnderscores) { | 138 TEST_F(ExtensionFileUtilTest, CheckIllegalFilenamesNoUnderscores) { |
139 base::ScopedTempDir temp; | 139 base::ScopedTempDir temp; |
140 ASSERT_TRUE(temp.CreateUniqueTempDir()); | 140 ASSERT_TRUE(temp.CreateUniqueTempDir()); |
141 | 141 |
142 base::FilePath src_path = temp.path().AppendASCII("some_dir"); | 142 base::FilePath src_path = temp.path().AppendASCII("some_dir"); |
143 ASSERT_TRUE(base::CreateDirectory(src_path)); | 143 ASSERT_TRUE(base::CreateDirectory(src_path)); |
144 | 144 |
145 std::string data = "{ \"name\": { \"message\": \"foobar\" } }"; | 145 std::string data = "{ \"name\": { \"message\": \"foobar\" } }"; |
146 ASSERT_TRUE(file_util::WriteFile(src_path.AppendASCII("some_file.txt"), | 146 ASSERT_TRUE(base::WriteFile(src_path.AppendASCII("some_file.txt"), |
147 data.c_str(), data.length())); | 147 data.c_str(), data.length())); |
148 std::string error; | 148 std::string error; |
149 EXPECT_TRUE(extension_file_util::CheckForIllegalFilenames(temp.path(), | 149 EXPECT_TRUE(extension_file_util::CheckForIllegalFilenames(temp.path(), |
150 &error)); | 150 &error)); |
151 } | 151 } |
152 | 152 |
153 TEST_F(ExtensionFileUtilTest, CheckIllegalFilenamesOnlyReserved) { | 153 TEST_F(ExtensionFileUtilTest, CheckIllegalFilenamesOnlyReserved) { |
154 base::ScopedTempDir temp; | 154 base::ScopedTempDir temp; |
155 ASSERT_TRUE(temp.CreateUniqueTempDir()); | 155 ASSERT_TRUE(temp.CreateUniqueTempDir()); |
156 | 156 |
157 const base::FilePath::CharType* folders[] = | 157 const base::FilePath::CharType* folders[] = |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
265 } | 265 } |
266 | 266 |
267 TEST_F(ExtensionFileUtilTest, ValidateThemeUTF8) { | 267 TEST_F(ExtensionFileUtilTest, ValidateThemeUTF8) { |
268 base::ScopedTempDir temp; | 268 base::ScopedTempDir temp; |
269 ASSERT_TRUE(temp.CreateUniqueTempDir()); | 269 ASSERT_TRUE(temp.CreateUniqueTempDir()); |
270 | 270 |
271 // "aeo" with accents. Use http://0xcc.net/jsescape/ to decode them. | 271 // "aeo" with accents. Use http://0xcc.net/jsescape/ to decode them. |
272 std::string non_ascii_file = "\xC3\xA0\xC3\xA8\xC3\xB2.png"; | 272 std::string non_ascii_file = "\xC3\xA0\xC3\xA8\xC3\xB2.png"; |
273 base::FilePath non_ascii_path = | 273 base::FilePath non_ascii_path = |
274 temp.path().Append(base::FilePath::FromUTF8Unsafe(non_ascii_file)); | 274 temp.path().Append(base::FilePath::FromUTF8Unsafe(non_ascii_file)); |
275 file_util::WriteFile(non_ascii_path, "", 0); | 275 base::WriteFile(non_ascii_path, "", 0); |
276 | 276 |
277 std::string kManifest = | 277 std::string kManifest = |
278 base::StringPrintf( | 278 base::StringPrintf( |
279 "{ \"name\": \"Test\", \"version\": \"1.0\", " | 279 "{ \"name\": \"Test\", \"version\": \"1.0\", " |
280 " \"theme\": { \"images\": { \"theme_frame\": \"%s\" } }" | 280 " \"theme\": { \"images\": { \"theme_frame\": \"%s\" } }" |
281 "}", non_ascii_file.c_str()); | 281 "}", non_ascii_file.c_str()); |
282 std::string error; | 282 std::string error; |
283 scoped_refptr<Extension> extension = LoadExtensionManifest( | 283 scoped_refptr<Extension> extension = LoadExtensionManifest( |
284 kManifest, temp.path(), Manifest::UNPACKED, 0, &error); | 284 kManifest, temp.path(), Manifest::UNPACKED, 0, &error); |
285 ASSERT_TRUE(extension.get()) << error; | 285 ASSERT_TRUE(extension.get()) << error; |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
354 "g==\n" | 354 "g==\n" |
355 "-----END PRIVATE KEY-----\n"; | 355 "-----END PRIVATE KEY-----\n"; |
356 | 356 |
357 TEST_F(ExtensionFileUtilTest, FindPrivateKeyFiles) { | 357 TEST_F(ExtensionFileUtilTest, FindPrivateKeyFiles) { |
358 base::ScopedTempDir temp; | 358 base::ScopedTempDir temp; |
359 ASSERT_TRUE(temp.CreateUniqueTempDir()); | 359 ASSERT_TRUE(temp.CreateUniqueTempDir()); |
360 | 360 |
361 base::FilePath src_path = temp.path().AppendASCII("some_dir"); | 361 base::FilePath src_path = temp.path().AppendASCII("some_dir"); |
362 ASSERT_TRUE(base::CreateDirectory(src_path)); | 362 ASSERT_TRUE(base::CreateDirectory(src_path)); |
363 | 363 |
364 ASSERT_TRUE(file_util::WriteFile(src_path.AppendASCII("a_key.pem"), | 364 ASSERT_TRUE(base::WriteFile(src_path.AppendASCII("a_key.pem"), |
365 private_key, arraysize(private_key))); | 365 private_key, arraysize(private_key))); |
366 ASSERT_TRUE(file_util::WriteFile(src_path.AppendASCII("second_key.pem"), | 366 ASSERT_TRUE(base::WriteFile(src_path.AppendASCII("second_key.pem"), |
367 private_key, arraysize(private_key))); | 367 private_key, arraysize(private_key))); |
368 // Shouldn't find a key with a different extension. | 368 // Shouldn't find a key with a different extension. |
369 ASSERT_TRUE(file_util::WriteFile(src_path.AppendASCII("key.diff_ext"), | 369 ASSERT_TRUE(base::WriteFile(src_path.AppendASCII("key.diff_ext"), |
370 private_key, arraysize(private_key))); | 370 private_key, arraysize(private_key))); |
371 // Shouldn't find a key that isn't parsable. | 371 // Shouldn't find a key that isn't parsable. |
372 ASSERT_TRUE(file_util::WriteFile(src_path.AppendASCII("unparsable_key.pem"), | 372 ASSERT_TRUE(base::WriteFile(src_path.AppendASCII("unparsable_key.pem"), |
373 private_key, arraysize(private_key) - 30)); | 373 private_key, arraysize(private_key) - 30)); |
374 std::vector<base::FilePath> private_keys = | 374 std::vector<base::FilePath> private_keys = |
375 extension_file_util::FindPrivateKeyFiles(temp.path()); | 375 extension_file_util::FindPrivateKeyFiles(temp.path()); |
376 EXPECT_EQ(2U, private_keys.size()); | 376 EXPECT_EQ(2U, private_keys.size()); |
377 EXPECT_THAT(private_keys, | 377 EXPECT_THAT(private_keys, |
378 testing::Contains(src_path.AppendASCII("a_key.pem"))); | 378 testing::Contains(src_path.AppendASCII("a_key.pem"))); |
379 EXPECT_THAT(private_keys, | 379 EXPECT_THAT(private_keys, |
380 testing::Contains(src_path.AppendASCII("second_key.pem"))); | 380 testing::Contains(src_path.AppendASCII("second_key.pem"))); |
381 } | 381 } |
382 | 382 |
383 TEST_F(ExtensionFileUtilTest, WarnOnPrivateKey) { | 383 TEST_F(ExtensionFileUtilTest, WarnOnPrivateKey) { |
384 base::ScopedTempDir temp; | 384 base::ScopedTempDir temp; |
385 ASSERT_TRUE(temp.CreateUniqueTempDir()); | 385 ASSERT_TRUE(temp.CreateUniqueTempDir()); |
386 | 386 |
387 base::FilePath ext_path = temp.path().AppendASCII("ext_root"); | 387 base::FilePath ext_path = temp.path().AppendASCII("ext_root"); |
388 ASSERT_TRUE(base::CreateDirectory(ext_path)); | 388 ASSERT_TRUE(base::CreateDirectory(ext_path)); |
389 | 389 |
390 const char manifest[] = | 390 const char manifest[] = |
391 "{\n" | 391 "{\n" |
392 " \"name\": \"Test Extension\",\n" | 392 " \"name\": \"Test Extension\",\n" |
393 " \"version\": \"1.0\",\n" | 393 " \"version\": \"1.0\",\n" |
394 " \"manifest_version\": 2,\n" | 394 " \"manifest_version\": 2,\n" |
395 " \"description\": \"The first extension that I made.\"\n" | 395 " \"description\": \"The first extension that I made.\"\n" |
396 "}\n"; | 396 "}\n"; |
397 ASSERT_TRUE(file_util::WriteFile(ext_path.AppendASCII("manifest.json"), | 397 ASSERT_TRUE(base::WriteFile(ext_path.AppendASCII("manifest.json"), |
398 manifest, strlen(manifest))); | 398 manifest, strlen(manifest))); |
399 ASSERT_TRUE(file_util::WriteFile(ext_path.AppendASCII("a_key.pem"), | 399 ASSERT_TRUE(base::WriteFile(ext_path.AppendASCII("a_key.pem"), |
400 private_key, strlen(private_key))); | 400 private_key, strlen(private_key))); |
401 | 401 |
402 std::string error; | 402 std::string error; |
403 scoped_refptr<Extension> extension(extension_file_util::LoadExtension( | 403 scoped_refptr<Extension> extension(extension_file_util::LoadExtension( |
404 ext_path, "the_id", Manifest::EXTERNAL_PREF, | 404 ext_path, "the_id", Manifest::EXTERNAL_PREF, |
405 Extension::NO_FLAGS, &error)); | 405 Extension::NO_FLAGS, &error)); |
406 ASSERT_TRUE(extension.get()) << error; | 406 ASSERT_TRUE(extension.get()) << error; |
407 ASSERT_EQ(1u, extension->install_warnings().size()); | 407 ASSERT_EQ(1u, extension->install_warnings().size()); |
408 EXPECT_THAT( | 408 EXPECT_THAT( |
409 extension->install_warnings(), | 409 extension->install_warnings(), |
410 testing::ElementsAre( | 410 testing::ElementsAre( |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
460 scoped_refptr<Extension> extension3(extension_file_util::LoadExtension( | 460 scoped_refptr<Extension> extension3(extension_file_util::LoadExtension( |
461 ext_dir, Manifest::UNPACKED, Extension::NO_FLAGS, &error)); | 461 ext_dir, Manifest::UNPACKED, Extension::NO_FLAGS, &error)); |
462 EXPECT_TRUE(extension3.get() == NULL); | 462 EXPECT_TRUE(extension3.get() == NULL); |
463 EXPECT_STREQ("Could not load icon 'icon.png' for page action.", | 463 EXPECT_STREQ("Could not load icon 'icon.png' for page action.", |
464 error.c_str()); | 464 error.c_str()); |
465 } | 465 } |
466 | 466 |
467 // TODO(aa): More tests as motivation allows. Maybe steal some from | 467 // TODO(aa): More tests as motivation allows. Maybe steal some from |
468 // ExtensionService? Many of them could probably be tested here without the | 468 // ExtensionService? Many of them could probably be tested here without the |
469 // MessageLoop shenanigans. | 469 // MessageLoop shenanigans. |
OLD | NEW |