backup_util_unittest.mm 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // Copyright 2021 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 "base/mac/backup_util.h"
  5. #include <stddef.h>
  6. #include <stdint.h>
  7. #include "base/files/file_path.h"
  8. #include "base/files/file_util.h"
  9. #include "base/files/scoped_temp_dir.h"
  10. #include "base/mac/foundation_util.h"
  11. #include "base/mac/scoped_cftyperef.h"
  12. #include "base/numerics/safe_conversions.h"
  13. #include "build/build_config.h"
  14. #include "testing/gtest/include/gtest/gtest.h"
  15. #include "testing/platform_test.h"
  16. namespace base::mac {
  17. namespace {
  18. using BackupUtilTest = PlatformTest;
  19. TEST_F(BackupUtilTest, TestExcludeFileFromBackups_Persists) {
  20. // The file must already exist in order to set its exclusion property.
  21. ScopedTempDir temp_dir_;
  22. ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
  23. FilePath excluded_file_path = temp_dir_.GetPath().Append("excluded");
  24. constexpr char placeholder_data[] = "All your base are belong to us!";
  25. // Dump something real into the file.
  26. ASSERT_EQ(checked_cast<int>(std::size(placeholder_data)),
  27. WriteFile(excluded_file_path, placeholder_data,
  28. std::size(placeholder_data)));
  29. // Initial state should be non-excluded.
  30. EXPECT_FALSE(GetBackupExclusion(excluded_file_path));
  31. // Exclude the file.
  32. ASSERT_TRUE(SetBackupExclusion(excluded_file_path));
  33. EXPECT_TRUE(GetBackupExclusion(excluded_file_path));
  34. }
  35. TEST_F(BackupUtilTest, TestExcludeFileFromBackups_NotByPath) {
  36. ScopedTempDir temp_dir_;
  37. ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
  38. FilePath excluded_file_path = temp_dir_.GetPath().Append("excluded");
  39. base::ScopedCFTypeRef<CFURLRef> excluded_url =
  40. base::mac::FilePathToCFURL(excluded_file_path);
  41. constexpr char placeholder_data[] = "All your base are belong to us!";
  42. ASSERT_EQ(checked_cast<int>(std::size(placeholder_data)),
  43. WriteFile(excluded_file_path, placeholder_data,
  44. std::size(placeholder_data)));
  45. ASSERT_TRUE(SetBackupExclusion(excluded_file_path));
  46. EXPECT_TRUE(GetBackupExclusion(excluded_file_path))
  47. << "Backup exclusion persists as long as the file exists";
  48. // Re-create the file.
  49. ASSERT_TRUE(DeleteFile(excluded_file_path));
  50. ASSERT_EQ(checked_cast<int>(std::size(placeholder_data)),
  51. WriteFile(excluded_file_path, placeholder_data,
  52. std::size(placeholder_data)));
  53. EXPECT_FALSE(GetBackupExclusion(excluded_file_path))
  54. << "Re-created file should not be excluded from backup";
  55. }
  56. } // namespace
  57. } // namespace base::mac