OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/browser/extensions/test_extension_dir.h" | 5 #include "chrome/browser/extensions/test_extension_dir.h" |
6 | 6 |
7 #include "base/files/file_util.h" | 7 #include "base/files/file_util.h" |
8 #include "base/json/json_writer.h" | 8 #include "base/json/json_writer.h" |
9 #include "base/numerics/safe_conversions.h" | 9 #include "base/numerics/safe_conversions.h" |
10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
11 #include "base/test/values_test_util.h" | 11 #include "base/test/values_test_util.h" |
| 12 #include "base/threading/thread_restrictions.h" |
12 #include "chrome/browser/extensions/extension_creator.h" | 13 #include "chrome/browser/extensions/extension_creator.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
14 | 15 |
15 namespace extensions { | 16 namespace extensions { |
16 | 17 |
17 TestExtensionDir::TestExtensionDir() { | 18 TestExtensionDir::TestExtensionDir() { |
| 19 base::ThreadRestrictions::ScopedAllowIO allow_io; |
18 EXPECT_TRUE(dir_.CreateUniqueTempDir()); | 20 EXPECT_TRUE(dir_.CreateUniqueTempDir()); |
19 EXPECT_TRUE(crx_dir_.CreateUniqueTempDir()); | 21 EXPECT_TRUE(crx_dir_.CreateUniqueTempDir()); |
20 } | 22 } |
21 | 23 |
22 TestExtensionDir::~TestExtensionDir() { | 24 TestExtensionDir::~TestExtensionDir() { |
| 25 base::ThreadRestrictions::ScopedAllowIO allow_io; |
| 26 ignore_result(dir_.Delete()); |
| 27 ignore_result(crx_dir_.Delete()); |
23 } | 28 } |
24 | 29 |
25 void TestExtensionDir::WriteManifest(base::StringPiece manifest) { | 30 void TestExtensionDir::WriteManifest(base::StringPiece manifest) { |
26 WriteFile(FILE_PATH_LITERAL("manifest.json"), manifest); | 31 WriteFile(FILE_PATH_LITERAL("manifest.json"), manifest); |
27 } | 32 } |
28 | 33 |
29 void TestExtensionDir::WriteManifestWithSingleQuotes( | 34 void TestExtensionDir::WriteManifestWithSingleQuotes( |
30 base::StringPiece manifest) { | 35 base::StringPiece manifest) { |
31 std::string double_quotes; | 36 std::string double_quotes; |
32 base::ReplaceChars(manifest.data(), "'", "\"", &double_quotes); | 37 base::ReplaceChars(manifest.data(), "'", "\"", &double_quotes); |
33 WriteManifest(double_quotes); | 38 WriteManifest(double_quotes); |
34 } | 39 } |
35 | 40 |
36 void TestExtensionDir::WriteFile(const base::FilePath::StringType& filename, | 41 void TestExtensionDir::WriteFile(const base::FilePath::StringType& filename, |
37 base::StringPiece contents) { | 42 base::StringPiece contents) { |
| 43 base::ThreadRestrictions::ScopedAllowIO allow_io; |
38 EXPECT_EQ(base::checked_cast<int>(contents.size()), | 44 EXPECT_EQ(base::checked_cast<int>(contents.size()), |
39 base::WriteFile(dir_.GetPath().Append(filename), contents.data(), | 45 base::WriteFile(dir_.GetPath().Append(filename), contents.data(), |
40 contents.size())); | 46 contents.size())); |
41 } | 47 } |
42 | 48 |
43 // This function packs the extension into a .crx, and returns the path to that | 49 // This function packs the extension into a .crx, and returns the path to that |
44 // .crx. Multiple calls to Pack() will produce extensions with the same ID. | 50 // .crx. Multiple calls to Pack() will produce extensions with the same ID. |
45 base::FilePath TestExtensionDir::Pack() { | 51 base::FilePath TestExtensionDir::Pack() { |
| 52 base::ThreadRestrictions::ScopedAllowIO allow_io; |
46 ExtensionCreator creator; | 53 ExtensionCreator creator; |
47 base::FilePath crx_path = | 54 base::FilePath crx_path = |
48 crx_dir_.GetPath().Append(FILE_PATH_LITERAL("ext.crx")); | 55 crx_dir_.GetPath().Append(FILE_PATH_LITERAL("ext.crx")); |
49 base::FilePath pem_path = | 56 base::FilePath pem_path = |
50 crx_dir_.GetPath().Append(FILE_PATH_LITERAL("ext.pem")); | 57 crx_dir_.GetPath().Append(FILE_PATH_LITERAL("ext.pem")); |
51 base::FilePath pem_in_path, pem_out_path; | 58 base::FilePath pem_in_path, pem_out_path; |
52 if (base::PathExists(pem_path)) | 59 if (base::PathExists(pem_path)) |
53 pem_in_path = pem_path; | 60 pem_in_path = pem_path; |
54 else | 61 else |
55 pem_out_path = pem_path; | 62 pem_out_path = pem_path; |
56 if (!creator.Run(dir_.GetPath(), crx_path, pem_in_path, pem_out_path, | 63 if (!creator.Run(dir_.GetPath(), crx_path, pem_in_path, pem_out_path, |
57 ExtensionCreator::kOverwriteCRX)) { | 64 ExtensionCreator::kOverwriteCRX)) { |
58 ADD_FAILURE() | 65 ADD_FAILURE() |
59 << "ExtensionCreator::Run() failed: " << creator.error_message(); | 66 << "ExtensionCreator::Run() failed: " << creator.error_message(); |
60 return base::FilePath(); | 67 return base::FilePath(); |
61 } | 68 } |
62 if (!base::PathExists(crx_path)) { | 69 if (!base::PathExists(crx_path)) { |
63 ADD_FAILURE() << crx_path.value() << " was not created."; | 70 ADD_FAILURE() << crx_path.value() << " was not created."; |
64 return base::FilePath(); | 71 return base::FilePath(); |
65 } | 72 } |
66 return crx_path; | 73 return crx_path; |
67 } | 74 } |
68 | 75 |
69 base::FilePath TestExtensionDir::UnpackedPath() { | 76 base::FilePath TestExtensionDir::UnpackedPath() { |
| 77 base::ThreadRestrictions::ScopedAllowIO allow_io; |
70 // We make this absolute because it's possible that dir_ contains a symlink as | 78 // We make this absolute because it's possible that dir_ contains a symlink as |
71 // part of it's path. When UnpackedInstaller::GetAbsolutePath() runs as part | 79 // part of it's path. When UnpackedInstaller::GetAbsolutePath() runs as part |
72 // of loading the extension, the extension's path is converted to an absolute | 80 // of loading the extension, the extension's path is converted to an absolute |
73 // path, which actually does something like `realpath` as part of its | 81 // path, which actually does something like `realpath` as part of its |
74 // resolution. If the tests are comparing paths to UnpackedPath(), then | 82 // resolution. If the tests are comparing paths to UnpackedPath(), then |
75 // they'll need to compare the same absolute'd path. | 83 // they'll need to compare the same absolute'd path. |
76 return base::MakeAbsoluteFilePath(dir_.GetPath()); | 84 return base::MakeAbsoluteFilePath(dir_.GetPath()); |
77 } | 85 } |
78 | 86 |
79 } // namespace extensions | 87 } // namespace extensions |
OLD | NEW |