1 /*
2  * Copyright (C) 2022 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 com.android.server.appsearch;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import android.app.appsearch.AppSearchSchema;
22 import android.app.appsearch.GenericDocument;
23 import android.app.appsearch.testutil.FakeAppSearchConfig;
24 import com.android.server.appsearch.external.localstorage.AppSearchImpl;
25 import com.android.server.appsearch.icing.proto.DebugInfoProto;
26 import com.android.server.appsearch.icing.proto.DebugInfoVerbosity;
27 import com.android.server.appsearch.icing.proto.DocumentDebugInfoProto;
28 import com.android.server.appsearch.icing.proto.DocumentStorageInfoProto;
29 import com.android.server.appsearch.icing.proto.NamespaceStorageInfoProto;
30 import com.android.server.appsearch.icing.proto.PropertyConfigProto;
31 import com.android.server.appsearch.icing.proto.SchemaDebugInfoProto;
32 import com.android.server.appsearch.icing.proto.SchemaProto;
33 import com.android.server.appsearch.icing.proto.SchemaTypeConfigProto;
34 import com.android.server.appsearch.util.AdbDumpUtil;
35 import java.util.Collections;
36 import java.util.List;
37 import org.junit.Rule;
38 import org.junit.Test;
39 import org.junit.rules.TemporaryFolder;
40 
41 public class AdbDumpUtilTest {
42     @Rule
43     public TemporaryFolder mTemporaryFolder = new TemporaryFolder();
44 
45     private static final String NAMESPACE_1 = "namespace1";
46     private static final String NAMESPACE_2 = "namespace2";
47     private static final String NAMESPACE_1_MD5 = "612897F0683DD83BE850731B14801182";
48     private static final String NAMESPACE_2_MD5 = "6FDE1BA4470CACA933058264DBFDEA2E";
49 
50     private static final String EMAIL_TYPE = "email";
51     private static final String PERSON_TYPE = "person";
52     private static final String EMAIL_TYPE_MD5 = "0C83F57C786A0B4A39EFAB23731C7EBC";
53     private static final String PERSON_TYPE_MD5 = "8B0A44048F58988B486BDD0D245B22A8";
54 
55     @Test
testDesensitizeDebugInfo()56     public void testDesensitizeDebugInfo() {
57         DocumentDebugInfoProto.Builder originalDocumentInfoBuilder =
58                 DocumentDebugInfoProto.newBuilder().addCorpusInfo(
59                         DocumentDebugInfoProto.CorpusInfo.newBuilder().setNamespace(
60                                 NAMESPACE_1).setSchema(EMAIL_TYPE)).addCorpusInfo(
61                         DocumentDebugInfoProto.CorpusInfo.newBuilder().setNamespace(
62                                 NAMESPACE_1).setSchema(PERSON_TYPE)).addCorpusInfo(
63                         DocumentDebugInfoProto.CorpusInfo.newBuilder().setNamespace(
64                                 NAMESPACE_2).setSchema(EMAIL_TYPE)).addCorpusInfo(
65                         DocumentDebugInfoProto.CorpusInfo.newBuilder().setNamespace(
66                                 NAMESPACE_2).setSchema(PERSON_TYPE)).setDocumentStorageInfo(
67                         DocumentStorageInfoProto.newBuilder().addNamespaceStorageInfo(
68                                 NamespaceStorageInfoProto.newBuilder().setNamespace(
69                                         NAMESPACE_1)).addNamespaceStorageInfo(
70                                 NamespaceStorageInfoProto.newBuilder().setNamespace(NAMESPACE_2)));
71         DocumentDebugInfoProto.Builder desensitizedDocumentInfoBuilder =
72                 DocumentDebugInfoProto.newBuilder().addCorpusInfo(
73                         DocumentDebugInfoProto.CorpusInfo.newBuilder().setNamespace(
74                                 NAMESPACE_1_MD5).setSchema(EMAIL_TYPE_MD5)).addCorpusInfo(
75                         DocumentDebugInfoProto.CorpusInfo.newBuilder().setNamespace(
76                                 NAMESPACE_1_MD5).setSchema(PERSON_TYPE_MD5)).addCorpusInfo(
77                         DocumentDebugInfoProto.CorpusInfo.newBuilder().setNamespace(
78                                 NAMESPACE_2_MD5).setSchema(EMAIL_TYPE_MD5)).addCorpusInfo(
79                         DocumentDebugInfoProto.CorpusInfo.newBuilder().setNamespace(
80                                 NAMESPACE_2_MD5).setSchema(PERSON_TYPE_MD5)).setDocumentStorageInfo(
81                         DocumentStorageInfoProto.newBuilder().addNamespaceStorageInfo(
82                                 NamespaceStorageInfoProto.newBuilder().setNamespace(
83                                         NAMESPACE_1_MD5)).addNamespaceStorageInfo(
84                                 NamespaceStorageInfoProto.newBuilder().setNamespace(
85                                         NAMESPACE_2_MD5)));
86 
87         SchemaDebugInfoProto.Builder originalSchemaInfoBuilder =
88                 SchemaDebugInfoProto.newBuilder().setSchema(SchemaProto.newBuilder().addTypes(
89                         SchemaTypeConfigProto.newBuilder().setSchemaType(EMAIL_TYPE).addProperties(
90                                 PropertyConfigProto.newBuilder().setDataType(
91                                         PropertyConfigProto.DataType.Code.DOCUMENT).setSchemaType(
92                                         PERSON_TYPE).setPropertyName("sender")).addProperties(
93                                 PropertyConfigProto.newBuilder().setDataType(
94                                         PropertyConfigProto.DataType.Code.STRING).setPropertyName(
95                                         "subject"))).addTypes(
96                         SchemaTypeConfigProto.newBuilder().setSchemaType(PERSON_TYPE).addProperties(
97                                 PropertyConfigProto.newBuilder().setDataType(
98                                         PropertyConfigProto.DataType.Code.STRING).setPropertyName(
99                                         "name"))));
100         SchemaDebugInfoProto.Builder desensitizedSchemaInfoBuilder =
101                 SchemaDebugInfoProto.newBuilder().setSchema(SchemaProto.newBuilder().addTypes(
102                         SchemaTypeConfigProto.newBuilder().setSchemaType(
103                                 EMAIL_TYPE_MD5).addProperties(
104                                 PropertyConfigProto.newBuilder().setDataType(
105                                         PropertyConfigProto.DataType.Code.DOCUMENT).setSchemaType(
106                                         PERSON_TYPE_MD5).setPropertyName("sender")).addProperties(
107                                 PropertyConfigProto.newBuilder().setDataType(
108                                         PropertyConfigProto.DataType.Code.STRING).setPropertyName(
109                                         "subject"))).addTypes(
110                         SchemaTypeConfigProto.newBuilder().setSchemaType(
111                                 PERSON_TYPE_MD5).addProperties(
112                                 PropertyConfigProto.newBuilder().setDataType(
113                                         PropertyConfigProto.DataType.Code.STRING).setPropertyName(
114                                         "name"))));
115 
116         DebugInfoProto originalDebugInfoProto = DebugInfoProto.newBuilder().setDocumentInfo(
117                 originalDocumentInfoBuilder).setSchemaInfo(originalSchemaInfoBuilder).build();
118         DebugInfoProto desensitizedDebugInfoProto = DebugInfoProto.newBuilder().setDocumentInfo(
119                 desensitizedDocumentInfoBuilder).setSchemaInfo(
120                 desensitizedSchemaInfoBuilder).build();
121 
122         assertThat(AdbDumpUtil.desensitizeDebugInfo(originalDebugInfoProto)).isEqualTo(
123                 desensitizedDebugInfoProto);
124     }
125 
126     @Test
testDesensitizeRealDebugInfo()127     public void testDesensitizeRealDebugInfo() throws Exception {
128         AppSearchImpl appSearchImpl = AppSearchImpl.create(mTemporaryFolder.newFolder(),
129                 new FakeAppSearchConfig(),
130                 /*initStatsBuilder=*/ null, /*visibilityChecker=*/ null,
131                 optimizeInfo -> true);
132         List<AppSearchSchema> schemas = Collections.singletonList(new AppSearchSchema.Builder(
133                 PERSON_TYPE).addProperty(new AppSearchSchema.StringPropertyConfig.Builder(
134                 "name").setIndexingType(
135                 AppSearchSchema.StringPropertyConfig.INDEXING_TYPE_PREFIXES).setTokenizerType(
136                 AppSearchSchema.StringPropertyConfig.TOKENIZER_TYPE_PLAIN).build()).build());
137         appSearchImpl.setSchema("adbdump_package", "adbdump_database", schemas,
138                 /*visibilityDocuments=*/ Collections.emptyList(),
139                 /*forceOverride=*/ false,
140                 /*version=*/ 0,
141                 /* setSchemaStatsBuilder= */ null);
142         GenericDocument person = new GenericDocument.Builder<>("adbdump_namespace",
143                 "adbdump_doc_id", PERSON_TYPE).setPropertyString("name",
144                 "adbdump test person").build();
145         appSearchImpl.putDocument("adbdump_package", "adbdump_database", person,
146                 /*sendChangeNotifications=*/ false, /*logger=*/ null);
147 
148         DebugInfoProto originalDebugInfoProto = appSearchImpl.getRawDebugInfoProto(
149                 DebugInfoVerbosity.Code.DETAILED);
150         String originalDebugString = originalDebugInfoProto.toString();
151         assertThat(originalDebugString).contains("adbdump_namespace");
152         assertThat(originalDebugString).contains("adbdump_package");
153         assertThat(originalDebugString).contains("adbdump_database");
154         assertThat(originalDebugString).doesNotContain("adbdump_doc_id");
155 
156         DebugInfoProto desensitizedDebugInfoProto = AdbDumpUtil.desensitizeDebugInfo(
157                 originalDebugInfoProto);
158         String desensitizedDebugString = desensitizedDebugInfoProto.toString();
159         assertThat(desensitizedDebugString).doesNotContain("adbdump_namespace");
160         assertThat(desensitizedDebugString).doesNotContain("adbdump_package");
161         assertThat(desensitizedDebugString).doesNotContain("adbdump_database");
162         assertThat(desensitizedDebugString).doesNotContain("adbdump_doc_id");
163     }
164 }
165