1 /*
2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.app.backup;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import static org.mockito.Mockito.when;
22 
23 import android.app.backup.BackupAgent.IncludeExcludeRules;
24 import android.app.backup.BackupManager.OperationType;
25 import android.app.backup.FullBackup.BackupScheme.PathWithRequiredFlags;
26 import android.os.ParcelFileDescriptor;
27 import android.os.UserHandle;
28 import android.platform.test.annotations.Presubmit;
29 import android.util.ArraySet;
30 
31 import androidx.test.runner.AndroidJUnit4;
32 
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.mockito.Mock;
37 import org.mockito.MockitoAnnotations;
38 
39 import java.io.IOException;
40 import java.util.Collections;
41 import java.util.Map;
42 import java.util.Set;
43 
44 @Presubmit
45 @RunWith(AndroidJUnit4.class)
46 public class BackupAgentTest {
47     // An arbitrary user.
48     private static final UserHandle USER_HANDLE = new UserHandle(15);
49 
50     @Mock FullBackup.BackupScheme mBackupScheme;
51 
52     private BackupAgent mBackupAgent;
53 
54     @Before
setUp()55     public void setUp() {
56         MockitoAnnotations.initMocks(this);
57     }
58 
59     @Test
testGetIncludeExcludeRules_isNotMigration_returnsRules()60     public void testGetIncludeExcludeRules_isNotMigration_returnsRules() throws Exception {
61         PathWithRequiredFlags path = new PathWithRequiredFlags("path", /* requiredFlags */ 0);
62         Map<String, Set<PathWithRequiredFlags>> includePaths = Collections.singletonMap("test",
63                 Collections.singleton(path));
64         ArraySet<PathWithRequiredFlags> excludePaths = new ArraySet<>();
65         excludePaths.add(path);
66         IncludeExcludeRules expectedRules = new IncludeExcludeRules(includePaths, excludePaths);
67 
68         mBackupAgent = getAgentForOperationType(OperationType.BACKUP);
69         when(mBackupScheme.maybeParseAndGetCanonicalExcludePaths()).thenReturn(excludePaths);
70         when(mBackupScheme.maybeParseAndGetCanonicalIncludePaths()).thenReturn(includePaths);
71 
72         IncludeExcludeRules rules = mBackupAgent.getIncludeExcludeRules(mBackupScheme);
73         assertThat(rules).isEqualTo(expectedRules);
74     }
75 
getAgentForOperationType(@perationType int operationType)76     private BackupAgent getAgentForOperationType(@OperationType int operationType) {
77         BackupAgent agent = new TestFullBackupAgent();
78         agent.onCreate(USER_HANDLE, operationType);
79         return agent;
80     }
81 
82     private static class TestFullBackupAgent extends BackupAgent {
83 
84         @Override
onBackup(ParcelFileDescriptor oldState, BackupDataOutput data, ParcelFileDescriptor newState)85         public void onBackup(ParcelFileDescriptor oldState, BackupDataOutput data,
86                 ParcelFileDescriptor newState) throws IOException {
87             // Left empty as this is a full backup agent.
88         }
89 
90         @Override
onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState)91         public void onRestore(BackupDataInput data, int appVersionCode,
92                 ParcelFileDescriptor newState) throws IOException {
93             // Left empty as this is a full backup agent.
94         }
95     }
96 }
97