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