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 18 matching lines...) Expand all Loading... |
29 }; | 29 }; |
30 | 30 |
31 TEST_F(ExtensionFileUtilTest, InstallUninstallGarbageCollect) { | 31 TEST_F(ExtensionFileUtilTest, InstallUninstallGarbageCollect) { |
32 base::ScopedTempDir temp; | 32 base::ScopedTempDir temp; |
33 ASSERT_TRUE(temp.CreateUniqueTempDir()); | 33 ASSERT_TRUE(temp.CreateUniqueTempDir()); |
34 | 34 |
35 // Create a source extension. | 35 // Create a source extension. |
36 std::string extension_id("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"); | 36 std::string extension_id("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"); |
37 std::string version("1.0"); | 37 std::string version("1.0"); |
38 base::FilePath src = temp.path().AppendASCII(extension_id); | 38 base::FilePath src = temp.path().AppendASCII(extension_id); |
39 ASSERT_TRUE(file_util::CreateDirectory(src)); | 39 ASSERT_TRUE(base::CreateDirectory(src)); |
40 | 40 |
41 // Create a extensions tree. | 41 // Create a extensions tree. |
42 base::FilePath all_extensions = temp.path().AppendASCII("extensions"); | 42 base::FilePath all_extensions = temp.path().AppendASCII("extensions"); |
43 ASSERT_TRUE(file_util::CreateDirectory(all_extensions)); | 43 ASSERT_TRUE(base::CreateDirectory(all_extensions)); |
44 | 44 |
45 // Install in empty directory. Should create parent directories as needed. | 45 // Install in empty directory. Should create parent directories as needed. |
46 base::FilePath version_1 = extension_file_util::InstallExtension( | 46 base::FilePath version_1 = extension_file_util::InstallExtension( |
47 src, | 47 src, |
48 extension_id, | 48 extension_id, |
49 version, | 49 version, |
50 all_extensions); | 50 all_extensions); |
51 ASSERT_EQ(version_1.value(), | 51 ASSERT_EQ(version_1.value(), |
52 all_extensions.AppendASCII(extension_id).AppendASCII("1.0_0") | 52 all_extensions.AppendASCII(extension_id).AppendASCII("1.0_0") |
53 .value()); | 53 .value()); |
54 ASSERT_TRUE(base::DirectoryExists(version_1)); | 54 ASSERT_TRUE(base::DirectoryExists(version_1)); |
55 | 55 |
56 // Should have moved the source. | 56 // Should have moved the source. |
57 ASSERT_FALSE(base::DirectoryExists(src)); | 57 ASSERT_FALSE(base::DirectoryExists(src)); |
58 | 58 |
59 // Install again. Should create a new one with different name. | 59 // Install again. Should create a new one with different name. |
60 ASSERT_TRUE(file_util::CreateDirectory(src)); | 60 ASSERT_TRUE(base::CreateDirectory(src)); |
61 base::FilePath version_2 = extension_file_util::InstallExtension( | 61 base::FilePath version_2 = extension_file_util::InstallExtension( |
62 src, | 62 src, |
63 extension_id, | 63 extension_id, |
64 version, | 64 version, |
65 all_extensions); | 65 all_extensions); |
66 ASSERT_EQ(version_2.value(), | 66 ASSERT_EQ(version_2.value(), |
67 all_extensions.AppendASCII(extension_id).AppendASCII("1.0_1") | 67 all_extensions.AppendASCII(extension_id).AppendASCII("1.0_1") |
68 .value()); | 68 .value()); |
69 ASSERT_TRUE(base::DirectoryExists(version_2)); | 69 ASSERT_TRUE(base::DirectoryExists(version_2)); |
70 | 70 |
71 // Should have moved the source. | 71 // Should have moved the source. |
72 ASSERT_FALSE(base::DirectoryExists(src)); | 72 ASSERT_FALSE(base::DirectoryExists(src)); |
73 | 73 |
74 // Install yet again. Should create a new one with a different name. | 74 // Install yet again. Should create a new one with a different name. |
75 ASSERT_TRUE(file_util::CreateDirectory(src)); | 75 ASSERT_TRUE(base::CreateDirectory(src)); |
76 base::FilePath version_3 = extension_file_util::InstallExtension( | 76 base::FilePath version_3 = extension_file_util::InstallExtension( |
77 src, | 77 src, |
78 extension_id, | 78 extension_id, |
79 version, | 79 version, |
80 all_extensions); | 80 all_extensions); |
81 ASSERT_EQ(version_3.value(), | 81 ASSERT_EQ(version_3.value(), |
82 all_extensions.AppendASCII(extension_id).AppendASCII("1.0_2") | 82 all_extensions.AppendASCII(extension_id).AppendASCII("1.0_2") |
83 .value()); | 83 .value()); |
84 ASSERT_TRUE(base::DirectoryExists(version_3)); | 84 ASSERT_TRUE(base::DirectoryExists(version_3)); |
85 | 85 |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
132 install_dir, Manifest::UNPACKED, Extension::NO_FLAGS, &error)); | 132 install_dir, Manifest::UNPACKED, Extension::NO_FLAGS, &error)); |
133 ASSERT_FALSE(extension.get() == NULL); | 133 ASSERT_FALSE(extension.get() == NULL); |
134 EXPECT_TRUE(error.empty()); | 134 EXPECT_TRUE(error.empty()); |
135 } | 135 } |
136 | 136 |
137 TEST_F(ExtensionFileUtilTest, CheckIllegalFilenamesNoUnderscores) { | 137 TEST_F(ExtensionFileUtilTest, CheckIllegalFilenamesNoUnderscores) { |
138 base::ScopedTempDir temp; | 138 base::ScopedTempDir temp; |
139 ASSERT_TRUE(temp.CreateUniqueTempDir()); | 139 ASSERT_TRUE(temp.CreateUniqueTempDir()); |
140 | 140 |
141 base::FilePath src_path = temp.path().AppendASCII("some_dir"); | 141 base::FilePath src_path = temp.path().AppendASCII("some_dir"); |
142 ASSERT_TRUE(file_util::CreateDirectory(src_path)); | 142 ASSERT_TRUE(base::CreateDirectory(src_path)); |
143 | 143 |
144 std::string data = "{ \"name\": { \"message\": \"foobar\" } }"; | 144 std::string data = "{ \"name\": { \"message\": \"foobar\" } }"; |
145 ASSERT_TRUE(file_util::WriteFile(src_path.AppendASCII("some_file.txt"), | 145 ASSERT_TRUE(file_util::WriteFile(src_path.AppendASCII("some_file.txt"), |
146 data.c_str(), data.length())); | 146 data.c_str(), data.length())); |
147 std::string error; | 147 std::string error; |
148 EXPECT_TRUE(extension_file_util::CheckForIllegalFilenames(temp.path(), | 148 EXPECT_TRUE(extension_file_util::CheckForIllegalFilenames(temp.path(), |
149 &error)); | 149 &error)); |
150 } | 150 } |
151 | 151 |
152 TEST_F(ExtensionFileUtilTest, CheckIllegalFilenamesOnlyReserved) { | 152 TEST_F(ExtensionFileUtilTest, CheckIllegalFilenamesOnlyReserved) { |
153 base::ScopedTempDir temp; | 153 base::ScopedTempDir temp; |
154 ASSERT_TRUE(temp.CreateUniqueTempDir()); | 154 ASSERT_TRUE(temp.CreateUniqueTempDir()); |
155 | 155 |
156 const base::FilePath::CharType* folders[] = | 156 const base::FilePath::CharType* folders[] = |
157 { extensions::kLocaleFolder, extensions::kPlatformSpecificFolder }; | 157 { extensions::kLocaleFolder, extensions::kPlatformSpecificFolder }; |
158 | 158 |
159 for (size_t i = 0; i < arraysize(folders); i++) { | 159 for (size_t i = 0; i < arraysize(folders); i++) { |
160 base::FilePath src_path = temp.path().Append(folders[i]); | 160 base::FilePath src_path = temp.path().Append(folders[i]); |
161 ASSERT_TRUE(file_util::CreateDirectory(src_path)); | 161 ASSERT_TRUE(base::CreateDirectory(src_path)); |
162 } | 162 } |
163 | 163 |
164 std::string error; | 164 std::string error; |
165 EXPECT_TRUE(extension_file_util::CheckForIllegalFilenames(temp.path(), | 165 EXPECT_TRUE(extension_file_util::CheckForIllegalFilenames(temp.path(), |
166 &error)); | 166 &error)); |
167 } | 167 } |
168 | 168 |
169 TEST_F(ExtensionFileUtilTest, CheckIllegalFilenamesReservedAndIllegal) { | 169 TEST_F(ExtensionFileUtilTest, CheckIllegalFilenamesReservedAndIllegal) { |
170 base::ScopedTempDir temp; | 170 base::ScopedTempDir temp; |
171 ASSERT_TRUE(temp.CreateUniqueTempDir()); | 171 ASSERT_TRUE(temp.CreateUniqueTempDir()); |
172 | 172 |
173 base::FilePath src_path = temp.path().Append(extensions::kLocaleFolder); | 173 base::FilePath src_path = temp.path().Append(extensions::kLocaleFolder); |
174 ASSERT_TRUE(file_util::CreateDirectory(src_path)); | 174 ASSERT_TRUE(base::CreateDirectory(src_path)); |
175 | 175 |
176 src_path = temp.path().AppendASCII("_some_dir"); | 176 src_path = temp.path().AppendASCII("_some_dir"); |
177 ASSERT_TRUE(file_util::CreateDirectory(src_path)); | 177 ASSERT_TRUE(base::CreateDirectory(src_path)); |
178 | 178 |
179 std::string error; | 179 std::string error; |
180 EXPECT_FALSE(extension_file_util::CheckForIllegalFilenames(temp.path(), | 180 EXPECT_FALSE(extension_file_util::CheckForIllegalFilenames(temp.path(), |
181 &error)); | 181 &error)); |
182 } | 182 } |
183 | 183 |
184 TEST_F(ExtensionFileUtilTest, | 184 TEST_F(ExtensionFileUtilTest, |
185 LoadExtensionGivesHelpfullErrorOnMissingManifest) { | 185 LoadExtensionGivesHelpfullErrorOnMissingManifest) { |
186 base::FilePath install_dir; | 186 base::FilePath install_dir; |
187 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &install_dir)); | 187 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &install_dir)); |
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
350 "uOwrmnQYxaMReFV68Z2w9DcLZn07f7/R9Wn72z89CxwJAFsDoNaDes4h48bX7plct\n" | 350 "uOwrmnQYxaMReFV68Z2w9DcLZn07f7/R9Wn72z89CxwJAFsDoNaDes4h48bX7plct\n" |
351 "s9ACjmTwcCigZjN2K7AGv7ntCLF3DnV5dK0dTHNaAdD3SbY3jl29Rk2CwiURSX6Ee\n" | 351 "s9ACjmTwcCigZjN2K7AGv7ntCLF3DnV5dK0dTHNaAdD3SbY3jl29Rk2CwiURSX6Ee\n" |
352 "g==\n" | 352 "g==\n" |
353 "-----END PRIVATE KEY-----\n"; | 353 "-----END PRIVATE KEY-----\n"; |
354 | 354 |
355 TEST_F(ExtensionFileUtilTest, FindPrivateKeyFiles) { | 355 TEST_F(ExtensionFileUtilTest, FindPrivateKeyFiles) { |
356 base::ScopedTempDir temp; | 356 base::ScopedTempDir temp; |
357 ASSERT_TRUE(temp.CreateUniqueTempDir()); | 357 ASSERT_TRUE(temp.CreateUniqueTempDir()); |
358 | 358 |
359 base::FilePath src_path = temp.path().AppendASCII("some_dir"); | 359 base::FilePath src_path = temp.path().AppendASCII("some_dir"); |
360 ASSERT_TRUE(file_util::CreateDirectory(src_path)); | 360 ASSERT_TRUE(base::CreateDirectory(src_path)); |
361 | 361 |
362 ASSERT_TRUE(file_util::WriteFile(src_path.AppendASCII("a_key.pem"), | 362 ASSERT_TRUE(file_util::WriteFile(src_path.AppendASCII("a_key.pem"), |
363 private_key, arraysize(private_key))); | 363 private_key, arraysize(private_key))); |
364 ASSERT_TRUE(file_util::WriteFile(src_path.AppendASCII("second_key.pem"), | 364 ASSERT_TRUE(file_util::WriteFile(src_path.AppendASCII("second_key.pem"), |
365 private_key, arraysize(private_key))); | 365 private_key, arraysize(private_key))); |
366 // Shouldn't find a key with a different extension. | 366 // Shouldn't find a key with a different extension. |
367 ASSERT_TRUE(file_util::WriteFile(src_path.AppendASCII("key.diff_ext"), | 367 ASSERT_TRUE(file_util::WriteFile(src_path.AppendASCII("key.diff_ext"), |
368 private_key, arraysize(private_key))); | 368 private_key, arraysize(private_key))); |
369 // Shouldn't find a key that isn't parsable. | 369 // Shouldn't find a key that isn't parsable. |
370 ASSERT_TRUE(file_util::WriteFile(src_path.AppendASCII("unparsable_key.pem"), | 370 ASSERT_TRUE(file_util::WriteFile(src_path.AppendASCII("unparsable_key.pem"), |
371 private_key, arraysize(private_key) - 30)); | 371 private_key, arraysize(private_key) - 30)); |
372 std::vector<base::FilePath> private_keys = | 372 std::vector<base::FilePath> private_keys = |
373 extension_file_util::FindPrivateKeyFiles(temp.path()); | 373 extension_file_util::FindPrivateKeyFiles(temp.path()); |
374 EXPECT_EQ(2U, private_keys.size()); | 374 EXPECT_EQ(2U, private_keys.size()); |
375 EXPECT_THAT(private_keys, | 375 EXPECT_THAT(private_keys, |
376 testing::Contains(src_path.AppendASCII("a_key.pem"))); | 376 testing::Contains(src_path.AppendASCII("a_key.pem"))); |
377 EXPECT_THAT(private_keys, | 377 EXPECT_THAT(private_keys, |
378 testing::Contains(src_path.AppendASCII("second_key.pem"))); | 378 testing::Contains(src_path.AppendASCII("second_key.pem"))); |
379 } | 379 } |
380 | 380 |
381 TEST_F(ExtensionFileUtilTest, WarnOnPrivateKey) { | 381 TEST_F(ExtensionFileUtilTest, WarnOnPrivateKey) { |
382 base::ScopedTempDir temp; | 382 base::ScopedTempDir temp; |
383 ASSERT_TRUE(temp.CreateUniqueTempDir()); | 383 ASSERT_TRUE(temp.CreateUniqueTempDir()); |
384 | 384 |
385 base::FilePath ext_path = temp.path().AppendASCII("ext_root"); | 385 base::FilePath ext_path = temp.path().AppendASCII("ext_root"); |
386 ASSERT_TRUE(file_util::CreateDirectory(ext_path)); | 386 ASSERT_TRUE(base::CreateDirectory(ext_path)); |
387 | 387 |
388 const char manifest[] = | 388 const char manifest[] = |
389 "{\n" | 389 "{\n" |
390 " \"name\": \"Test Extension\",\n" | 390 " \"name\": \"Test Extension\",\n" |
391 " \"version\": \"1.0\",\n" | 391 " \"version\": \"1.0\",\n" |
392 " \"manifest_version\": 2,\n" | 392 " \"manifest_version\": 2,\n" |
393 " \"description\": \"The first extension that I made.\"\n" | 393 " \"description\": \"The first extension that I made.\"\n" |
394 "}\n"; | 394 "}\n"; |
395 ASSERT_TRUE(file_util::WriteFile(ext_path.AppendASCII("manifest.json"), | 395 ASSERT_TRUE(file_util::WriteFile(ext_path.AppendASCII("manifest.json"), |
396 manifest, strlen(manifest))); | 396 manifest, strlen(manifest))); |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
458 scoped_refptr<Extension> extension3(extension_file_util::LoadExtension( | 458 scoped_refptr<Extension> extension3(extension_file_util::LoadExtension( |
459 ext_dir, Manifest::UNPACKED, Extension::NO_FLAGS, &error)); | 459 ext_dir, Manifest::UNPACKED, Extension::NO_FLAGS, &error)); |
460 EXPECT_TRUE(extension3.get() == NULL); | 460 EXPECT_TRUE(extension3.get() == NULL); |
461 EXPECT_STREQ("Could not load icon 'icon.png' for page action.", | 461 EXPECT_STREQ("Could not load icon 'icon.png' for page action.", |
462 error.c_str()); | 462 error.c_str()); |
463 } | 463 } |
464 | 464 |
465 // TODO(aa): More tests as motivation allows. Maybe steal some from | 465 // TODO(aa): More tests as motivation allows. Maybe steal some from |
466 // ExtensionService? Many of them could probably be tested here without the | 466 // ExtensionService? Many of them could probably be tested here without the |
467 // MessageLoop shenanigans. | 467 // MessageLoop shenanigans. |
OLD | NEW |