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