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_TRUE(WriteFile(excluded_file_path, 34 base::byte_span_from_cstring(placeholder_data))); 35 // Initial state should be non-excluded. 36 EXPECT_FALSE(GetBackupExclusion(excluded_file_path)); 37 // Exclude the file. 38 ASSERT_TRUE(SetBackupExclusion(excluded_file_path)); 39 EXPECT_TRUE(GetBackupExclusion(excluded_file_path)); 40} 41 42TEST_F(BackupUtilTest, TestExcludeFileFromBackups_NotByPath) { 43 ScopedTempDir temp_dir_; 44 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 45 FilePath excluded_file_path = temp_dir_.GetPath().Append("excluded"); 46 ScopedCFTypeRef<CFURLRef> excluded_url = 47 apple::FilePathToCFURL(excluded_file_path); 48 49 constexpr char placeholder_data[] = "All your base are belong to us!"; 50 ASSERT_TRUE(WriteFile(excluded_file_path, 51 base::byte_span_from_cstring(placeholder_data))); 52 53 ASSERT_TRUE(SetBackupExclusion(excluded_file_path)); 54 EXPECT_TRUE(GetBackupExclusion(excluded_file_path)) 55 << "Backup exclusion persists as long as the file exists"; 56 57 // Re-create the file. 58 ASSERT_TRUE(DeleteFile(excluded_file_path)); 59 ASSERT_TRUE(WriteFile(excluded_file_path, 60 base::byte_span_from_cstring(placeholder_data))); 61 EXPECT_FALSE(GetBackupExclusion(excluded_file_path)) 62 << "Re-created file should not be excluded from backup"; 63} 64 65} // namespace 66 67} // namespace base::apple 68