1// Copyright 2021 The Chromium Authors 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5#include "base/apple/backup_util.h" 6 7#include <stddef.h> 8#include <stdint.h> 9 10#include "base/apple/foundation_util.h" 11#include "base/apple/scoped_cftyperef.h" 12#include "base/files/file_path.h" 13#include "base/files/file_util.h" 14#include "base/files/scoped_temp_dir.h" 15#include "base/numerics/safe_conversions.h" 16#include "build/build_config.h" 17#include "testing/gtest/include/gtest/gtest.h" 18#include "testing/platform_test.h" 19 20namespace base::apple { 21 22namespace { 23 24using BackupUtilTest = PlatformTest; 25 26TEST_F(BackupUtilTest, TestExcludeFileFromBackups_Persists) { 27 // The file must already exist in order to set its exclusion property. 28 ScopedTempDir temp_dir_; 29 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 30 FilePath excluded_file_path = temp_dir_.GetPath().Append("excluded"); 31 constexpr char placeholder_data[] = "All your base are belong to us!"; 32 // Dump something real into the file. 33 ASSERT_EQ(checked_cast<int>(std::size(placeholder_data)), 34 WriteFile(excluded_file_path, placeholder_data, 35 std::size(placeholder_data))); 36 // Initial state should be non-excluded. 37 EXPECT_FALSE(GetBackupExclusion(excluded_file_path)); 38 // Exclude the file. 39 ASSERT_TRUE(SetBackupExclusion(excluded_file_path)); 40 EXPECT_TRUE(GetBackupExclusion(excluded_file_path)); 41} 42 43TEST_F(BackupUtilTest, TestExcludeFileFromBackups_NotByPath) { 44 ScopedTempDir temp_dir_; 45 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 46 FilePath excluded_file_path = temp_dir_.GetPath().Append("excluded"); 47 ScopedCFTypeRef<CFURLRef> excluded_url = 48 apple::FilePathToCFURL(excluded_file_path); 49 50 constexpr char placeholder_data[] = "All your base are belong to us!"; 51 ASSERT_EQ(checked_cast<int>(std::size(placeholder_data)), 52 WriteFile(excluded_file_path, placeholder_data, 53 std::size(placeholder_data))); 54 55 ASSERT_TRUE(SetBackupExclusion(excluded_file_path)); 56 EXPECT_TRUE(GetBackupExclusion(excluded_file_path)) 57 << "Backup exclusion persists as long as the file exists"; 58 59 // Re-create the file. 60 ASSERT_TRUE(DeleteFile(excluded_file_path)); 61 ASSERT_EQ(checked_cast<int>(std::size(placeholder_data)), 62 WriteFile(excluded_file_path, placeholder_data, 63 std::size(placeholder_data))); 64 EXPECT_FALSE(GetBackupExclusion(excluded_file_path)) 65 << "Re-created file should not be excluded from backup"; 66} 67 68} // namespace 69 70} // namespace base::apple 71