test_extension_dir.cc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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
  3. // found in the LICENSE file.
  4. #include "extensions/test/test_extension_dir.h"
  5. #include <tuple>
  6. #include "base/files/file_util.h"
  7. #include "base/numerics/checked_math.h"
  8. #include "base/strings/string_util.h"
  9. #include "base/threading/thread_restrictions.h"
  10. #include "extensions/browser/extension_creator.h"
  11. #include "testing/gtest/include/gtest/gtest.h"
  12. namespace extensions {
  13. TestExtensionDir::TestExtensionDir() {
  14. base::ScopedAllowBlockingForTesting allow_blocking;
  15. EXPECT_TRUE(dir_.CreateUniqueTempDir());
  16. EXPECT_TRUE(crx_dir_.CreateUniqueTempDir());
  17. }
  18. TestExtensionDir::~TestExtensionDir() {
  19. base::ScopedAllowBlockingForTesting allow_blocking;
  20. std::ignore = dir_.Delete();
  21. std::ignore = crx_dir_.Delete();
  22. }
  23. void TestExtensionDir::WriteManifest(base::StringPiece manifest) {
  24. WriteFile(FILE_PATH_LITERAL("manifest.json"), manifest);
  25. }
  26. void TestExtensionDir::WriteFile(const base::FilePath::StringType& filename,
  27. base::StringPiece contents) {
  28. base::ScopedAllowBlockingForTesting allow_blocking;
  29. EXPECT_EQ(base::checked_cast<int>(contents.size()),
  30. base::WriteFile(dir_.GetPath().Append(filename), contents.data(),
  31. contents.size()));
  32. }
  33. void TestExtensionDir::CopyFileTo(
  34. const base::FilePath& from_path,
  35. const base::FilePath::StringType& local_filename) {
  36. base::ScopedAllowBlockingForTesting allow_blocking;
  37. ASSERT_TRUE(base::PathExists(from_path)) << from_path;
  38. EXPECT_TRUE(base::CopyFile(from_path, dir_.GetPath().Append(local_filename)))
  39. << "Failed to copy file from " << from_path << " to " << local_filename;
  40. }
  41. base::FilePath TestExtensionDir::Pack() {
  42. base::ScopedAllowBlockingForTesting allow_blocking;
  43. ExtensionCreator creator;
  44. base::FilePath crx_path =
  45. crx_dir_.GetPath().Append(FILE_PATH_LITERAL("ext.crx"));
  46. base::FilePath pem_path =
  47. crx_dir_.GetPath().Append(FILE_PATH_LITERAL("ext.pem"));
  48. base::FilePath pem_in_path, pem_out_path;
  49. if (base::PathExists(pem_path))
  50. pem_in_path = pem_path;
  51. else
  52. pem_out_path = pem_path;
  53. if (!creator.Run(dir_.GetPath(), crx_path, pem_in_path, pem_out_path,
  54. ExtensionCreator::kOverwriteCRX)) {
  55. ADD_FAILURE() << "ExtensionCreator::Run() failed: "
  56. << creator.error_message();
  57. return base::FilePath();
  58. }
  59. if (!base::PathExists(crx_path)) {
  60. ADD_FAILURE() << crx_path.value() << " was not created.";
  61. return base::FilePath();
  62. }
  63. return crx_path;
  64. }
  65. base::FilePath TestExtensionDir::UnpackedPath() const {
  66. base::ScopedAllowBlockingForTesting allow_blocking;
  67. // We make this absolute because it's possible that dir_ contains a symlink as
  68. // part of it's path. When UnpackedInstaller::GetAbsolutePath() runs as part
  69. // of loading the extension, the extension's path is converted to an absolute
  70. // path, which actually does something like `realpath` as part of its
  71. // resolution. If the tests are comparing paths to UnpackedPath(), then
  72. // they'll need to compare the same absolute'd path.
  73. return base::MakeAbsoluteFilePath(dir_.GetPath());
  74. }
  75. } // namespace extensions