blob: c57136c1acaba5872b8163a03a460ffe41ab2c54 [file] [log] [blame]
Khaled Abdelmohsen216109e2019-11-04 17:49:18 +00001/*
2 * Copyright (C) 2019 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
17package com.android.server.integrity.parser;
18
19import static com.android.server.testutils.TestUtils.assertExpectException;
20
Khaled Abdelmohsen12b110e2019-11-05 17:24:48 +000021import static com.google.common.truth.Truth.assertThat;
Khaled Abdelmohsen216109e2019-11-04 17:49:18 +000022
Song Pan75147d52019-11-19 00:57:46 +000023import android.content.integrity.AtomicFormula;
24import android.content.integrity.CompoundFormula;
25import android.content.integrity.Rule;
Khaled Abdelmohsen216109e2019-11-04 17:49:18 +000026
27import org.junit.Test;
28import org.junit.runner.RunWith;
29import org.junit.runners.JUnit4;
30
Khaled Abdelmohsen6ecfebc2019-12-03 15:00:49 +000031import java.nio.charset.StandardCharsets;
Khaled Abdelmohsen21a96052019-11-06 12:23:22 +000032import java.util.Arrays;
Khaled Abdelmohsen12b110e2019-11-05 17:24:48 +000033import java.util.Collections;
Khaled Abdelmohsenb05d7062019-11-19 01:22:23 +000034import java.util.HashMap;
Khaled Abdelmohsen216109e2019-11-04 17:49:18 +000035import java.util.List;
Khaled Abdelmohsenb05d7062019-11-19 01:22:23 +000036import java.util.Map;
Khaled Abdelmohsen216109e2019-11-04 17:49:18 +000037
38@RunWith(JUnit4.class)
39public class RuleXmlParserTest {
40
Khaled Abdelmohsen216109e2019-11-04 17:49:18 +000041 @Test
Song Pan75147d52019-11-19 00:57:46 +000042 public void testXmlStream_validCompoundFormula() throws Exception {
Khaled Abdelmohsenb05d7062019-11-19 01:22:23 +000043 Map<String, String> atomicFormulaAttrs = new HashMap<>();
44 atomicFormulaAttrs.put("K", String.valueOf(AtomicFormula.PACKAGE_NAME));
Khaled Abdelmohsenb05d7062019-11-19 01:22:23 +000045 atomicFormulaAttrs.put("V", "com.app.test");
Song Pan75147d52019-11-19 00:57:46 +000046 String ruleXmlCompoundFormula =
47 "<RL>"
48 + generateTagWithAttribute(
Omer Nebil Yaveroglu15395f52020-01-22 12:14:44 +000049 /* tag= */ "R",
50 Collections.singletonMap("E", String.valueOf(Rule.DENY)),
51 /* closed= */ false)
Song Pan75147d52019-11-19 00:57:46 +000052 + generateTagWithAttribute(
Omer Nebil Yaveroglu15395f52020-01-22 12:14:44 +000053 /* tag= */ "OF",
54 Collections.singletonMap("C", String.valueOf(CompoundFormula.NOT)),
55 /* closed= */ false)
Song Pan75147d52019-11-19 00:57:46 +000056 + generateTagWithAttribute(
Omer Nebil Yaveroglu15395f52020-01-22 12:14:44 +000057 /* tag= */ "AF", atomicFormulaAttrs, /* closed= */ true)
Song Pan75147d52019-11-19 00:57:46 +000058 + "</OF>"
59 + "</R>"
60 + "</RL>";
Khaled Abdelmohsen216109e2019-11-04 17:49:18 +000061 RuleParser xmlParser = new RuleXmlParser();
Song Pan75147d52019-11-19 00:57:46 +000062 Rule expectedRule =
63 new Rule(
64 new CompoundFormula(
65 CompoundFormula.NOT,
66 Collections.singletonList(
67 new AtomicFormula.StringAtomicFormula(
68 AtomicFormula.PACKAGE_NAME,
69 "com.app.test",
70 /* isHashedValue= */ false))),
71 Rule.DENY);
Khaled Abdelmohsen216109e2019-11-04 17:49:18 +000072
Song Pan8a0b6fc2020-01-14 16:33:50 +000073 List<Rule> rules = xmlParser.parse(ruleXmlCompoundFormula.getBytes());
Khaled Abdelmohsen216109e2019-11-04 17:49:18 +000074
Khaled Abdelmohsen21a96052019-11-06 12:23:22 +000075 assertThat(rules).isEqualTo(Collections.singletonList(expectedRule));
Khaled Abdelmohsen12b110e2019-11-05 17:24:48 +000076 }
77
78 @Test
Song Pan75147d52019-11-19 00:57:46 +000079 public void testXmlString_validCompoundFormula_notConnector() throws Exception {
Khaled Abdelmohsenb05d7062019-11-19 01:22:23 +000080 Map<String, String> packageNameAttrs = new HashMap<>();
81 packageNameAttrs.put("K", String.valueOf(AtomicFormula.PACKAGE_NAME));
Khaled Abdelmohsenb05d7062019-11-19 01:22:23 +000082 packageNameAttrs.put("V", "com.app.test");
Song Pan75147d52019-11-19 00:57:46 +000083 String ruleXmlCompoundFormula =
84 "<RL>"
85 + generateTagWithAttribute(
Omer Nebil Yaveroglu15395f52020-01-22 12:14:44 +000086 /* tag= */ "R",
87 Collections.singletonMap("E", String.valueOf(Rule.DENY)),
88 /* closed= */ false)
Song Pan75147d52019-11-19 00:57:46 +000089 + generateTagWithAttribute(
Omer Nebil Yaveroglu15395f52020-01-22 12:14:44 +000090 /* tag= */ "OF",
91 Collections.singletonMap("C", String.valueOf(CompoundFormula.NOT)),
92 /* closed= */ false)
Song Pan75147d52019-11-19 00:57:46 +000093 + generateTagWithAttribute(
Omer Nebil Yaveroglu15395f52020-01-22 12:14:44 +000094 /* tag= */ "AF", packageNameAttrs, /* closed= */ true)
Song Pan75147d52019-11-19 00:57:46 +000095 + "</OF>"
96 + "</R>"
97 + "</RL>";
Khaled Abdelmohsen21a96052019-11-06 12:23:22 +000098 RuleParser xmlParser = new RuleXmlParser();
Song Pan75147d52019-11-19 00:57:46 +000099 Rule expectedRule =
100 new Rule(
101 new CompoundFormula(
102 CompoundFormula.NOT,
103 Collections.singletonList(
104 new AtomicFormula.StringAtomicFormula(
105 AtomicFormula.PACKAGE_NAME,
106 "com.app.test",
107 /* isHashedValue= */ false))),
108 Rule.DENY);
Khaled Abdelmohsen21a96052019-11-06 12:23:22 +0000109
Khaled Abdelmohsen6ecfebc2019-12-03 15:00:49 +0000110 List<Rule> rules = xmlParser.parse(ruleXmlCompoundFormula.getBytes(StandardCharsets.UTF_8));
Khaled Abdelmohsen21a96052019-11-06 12:23:22 +0000111
112 assertThat(rules).isEqualTo(Collections.singletonList(expectedRule));
113 }
114
115 @Test
Song Pan75147d52019-11-19 00:57:46 +0000116 public void testXmlString_validCompoundFormula_andConnector() throws Exception {
Khaled Abdelmohsenb05d7062019-11-19 01:22:23 +0000117 Map<String, String> packageNameAttrs = new HashMap<>();
118 packageNameAttrs.put("K", String.valueOf(AtomicFormula.PACKAGE_NAME));
Khaled Abdelmohsenb05d7062019-11-19 01:22:23 +0000119 packageNameAttrs.put("V", "com.app.test");
120 Map<String, String> appCertificateAttrs = new HashMap<>();
121 appCertificateAttrs.put("K", String.valueOf(AtomicFormula.APP_CERTIFICATE));
Khaled Abdelmohsenb05d7062019-11-19 01:22:23 +0000122 appCertificateAttrs.put("V", "test_cert");
Song Pan75147d52019-11-19 00:57:46 +0000123 String ruleXmlCompoundFormula =
124 "<RL>"
125 + generateTagWithAttribute(
Omer Nebil Yaveroglu15395f52020-01-22 12:14:44 +0000126 /* tag= */ "R",
127 Collections.singletonMap("E", String.valueOf(Rule.DENY)),
128 /* closed= */ false)
Song Pan75147d52019-11-19 00:57:46 +0000129 + generateTagWithAttribute(
Omer Nebil Yaveroglu15395f52020-01-22 12:14:44 +0000130 /* tag= */ "OF",
131 Collections.singletonMap("C", String.valueOf(CompoundFormula.AND)),
132 /* closed= */ false)
Song Pan75147d52019-11-19 00:57:46 +0000133 + generateTagWithAttribute(
Omer Nebil Yaveroglu15395f52020-01-22 12:14:44 +0000134 /* tag= */ "AF", packageNameAttrs, /* closed= */ true)
Song Pan75147d52019-11-19 00:57:46 +0000135 + generateTagWithAttribute(
Omer Nebil Yaveroglu15395f52020-01-22 12:14:44 +0000136 /* tag= */ "AF", appCertificateAttrs, /* closed= */ true)
Song Pan75147d52019-11-19 00:57:46 +0000137 + "</OF>"
138 + "</R>"
139 + "</RL>";
Khaled Abdelmohsen21a96052019-11-06 12:23:22 +0000140 RuleParser xmlParser = new RuleXmlParser();
Song Pan75147d52019-11-19 00:57:46 +0000141 Rule expectedRule =
142 new Rule(
143 new CompoundFormula(
144 CompoundFormula.AND,
145 Arrays.asList(
146 new AtomicFormula.StringAtomicFormula(
147 AtomicFormula.PACKAGE_NAME,
148 "com.app.test",
149 /* isHashedValue= */ false),
150 new AtomicFormula.StringAtomicFormula(
151 AtomicFormula.APP_CERTIFICATE,
152 "test_cert",
153 /* isHashedValue= */ false))),
154 Rule.DENY);
Khaled Abdelmohsen6ecfebc2019-12-03 15:00:49 +0000155 List<Rule> rules = xmlParser.parse(ruleXmlCompoundFormula.getBytes(StandardCharsets.UTF_8));
Khaled Abdelmohsen21a96052019-11-06 12:23:22 +0000156
157 assertThat(rules).isEqualTo(Collections.singletonList(expectedRule));
158 }
159
160 @Test
Song Pan75147d52019-11-19 00:57:46 +0000161 public void testXmlString_validCompoundFormula_orConnector() throws Exception {
Khaled Abdelmohsenb05d7062019-11-19 01:22:23 +0000162 Map<String, String> packageNameAttrs = new HashMap<>();
163 packageNameAttrs.put("K", String.valueOf(AtomicFormula.PACKAGE_NAME));
Khaled Abdelmohsenb05d7062019-11-19 01:22:23 +0000164 packageNameAttrs.put("V", "com.app.test");
165 Map<String, String> appCertificateAttrs = new HashMap<>();
166 appCertificateAttrs.put("K", String.valueOf(AtomicFormula.APP_CERTIFICATE));
Khaled Abdelmohsenb05d7062019-11-19 01:22:23 +0000167 appCertificateAttrs.put("V", "test_cert");
Song Pan75147d52019-11-19 00:57:46 +0000168 String ruleXmlCompoundFormula =
169 "<RL>"
170 + generateTagWithAttribute(
Omer Nebil Yaveroglu15395f52020-01-22 12:14:44 +0000171 /* tag= */ "R",
172 Collections.singletonMap("E", String.valueOf(Rule.DENY)),
173 /* closed= */ false)
Song Pan75147d52019-11-19 00:57:46 +0000174 + generateTagWithAttribute(
Omer Nebil Yaveroglu15395f52020-01-22 12:14:44 +0000175 /* tag= */ "OF",
176 Collections.singletonMap("C", String.valueOf(CompoundFormula.OR)),
177 /* closed= */ false)
Song Pan75147d52019-11-19 00:57:46 +0000178 + generateTagWithAttribute(
Omer Nebil Yaveroglu15395f52020-01-22 12:14:44 +0000179 /* tag= */ "AF", packageNameAttrs, /* closed= */ true)
Song Pan75147d52019-11-19 00:57:46 +0000180 + generateTagWithAttribute(
Omer Nebil Yaveroglu15395f52020-01-22 12:14:44 +0000181 /* tag= */ "AF", appCertificateAttrs, /* closed= */ true)
Song Pan75147d52019-11-19 00:57:46 +0000182 + "</OF>"
183 + "</R>"
184 + "</RL>";
Khaled Abdelmohsen21a96052019-11-06 12:23:22 +0000185 RuleParser xmlParser = new RuleXmlParser();
Song Pan75147d52019-11-19 00:57:46 +0000186 Rule expectedRule =
187 new Rule(
188 new CompoundFormula(
189 CompoundFormula.OR,
190 Arrays.asList(
191 new AtomicFormula.StringAtomicFormula(
192 AtomicFormula.PACKAGE_NAME,
193 "com.app.test",
194 /* isHashedValue= */ false),
195 new AtomicFormula.StringAtomicFormula(
196 AtomicFormula.APP_CERTIFICATE,
197 "test_cert",
198 /* isHashedValue= */ false))),
199 Rule.DENY);
Khaled Abdelmohsen21a96052019-11-06 12:23:22 +0000200
Khaled Abdelmohsen6ecfebc2019-12-03 15:00:49 +0000201 List<Rule> rules = xmlParser.parse(ruleXmlCompoundFormula.getBytes(StandardCharsets.UTF_8));
Khaled Abdelmohsen21a96052019-11-06 12:23:22 +0000202
203 assertThat(rules).isEqualTo(Collections.singletonList(expectedRule));
204 }
205
206 @Test
Song Pan75147d52019-11-19 00:57:46 +0000207 public void testXmlString_validCompoundFormula_differentTagOrder() throws Exception {
Khaled Abdelmohsenb05d7062019-11-19 01:22:23 +0000208 Map<String, String> packageNameAttrs = new HashMap<>();
209 packageNameAttrs.put("K", String.valueOf(AtomicFormula.PACKAGE_NAME));
210 packageNameAttrs.put("V", "com.app.test");
Song Pan75147d52019-11-19 00:57:46 +0000211 String ruleXmlCompoundFormula =
212 "<RL>"
213 + generateTagWithAttribute(
Omer Nebil Yaveroglu15395f52020-01-22 12:14:44 +0000214 /* tag= */ "R",
215 Collections.singletonMap("E", String.valueOf(Rule.DENY)),
216 /* closed= */ false)
Song Pan75147d52019-11-19 00:57:46 +0000217 + generateTagWithAttribute(
Omer Nebil Yaveroglu15395f52020-01-22 12:14:44 +0000218 /* tag= */ "OF",
219 Collections.singletonMap("C", String.valueOf(CompoundFormula.NOT)),
220 /* closed= */ false)
Song Pan75147d52019-11-19 00:57:46 +0000221 + generateTagWithAttribute(
Omer Nebil Yaveroglu15395f52020-01-22 12:14:44 +0000222 /* tag= */ "AF", packageNameAttrs, /* closed= */ true)
Song Pan75147d52019-11-19 00:57:46 +0000223 + "</OF>"
224 + "</R>"
225 + "</RL>";
Khaled Abdelmohsen21a96052019-11-06 12:23:22 +0000226 RuleParser xmlParser = new RuleXmlParser();
Song Pan75147d52019-11-19 00:57:46 +0000227 Rule expectedRule =
228 new Rule(
229 new CompoundFormula(
230 CompoundFormula.NOT,
231 Collections.singletonList(
232 new AtomicFormula.StringAtomicFormula(
233 AtomicFormula.PACKAGE_NAME,
234 "com.app.test",
235 /* isHashedValue= */ false))),
236 Rule.DENY);
Khaled Abdelmohsen21a96052019-11-06 12:23:22 +0000237
Khaled Abdelmohsen6ecfebc2019-12-03 15:00:49 +0000238 List<Rule> rules = xmlParser.parse(ruleXmlCompoundFormula.getBytes(StandardCharsets.UTF_8));
Khaled Abdelmohsen21a96052019-11-06 12:23:22 +0000239
240 assertThat(rules).isEqualTo(Collections.singletonList(expectedRule));
241 }
242
243 @Test
Song Pan75147d52019-11-19 00:57:46 +0000244 public void testXmlString_invalidCompoundFormula_invalidNumberOfFormulas() throws Exception {
Khaled Abdelmohsenb05d7062019-11-19 01:22:23 +0000245 Map<String, String> packageNameAttrs = new HashMap<>();
246 packageNameAttrs.put("K", String.valueOf(AtomicFormula.PACKAGE_NAME));
Khaled Abdelmohsenb05d7062019-11-19 01:22:23 +0000247 packageNameAttrs.put("V", "com.app.test");
248 Map<String, String> versionCodeAttrs = new HashMap<>();
249 versionCodeAttrs.put("K", String.valueOf(AtomicFormula.VERSION_CODE));
250 versionCodeAttrs.put("O", String.valueOf(AtomicFormula.EQ));
251 versionCodeAttrs.put("V", "1");
Song Pan75147d52019-11-19 00:57:46 +0000252 String ruleXmlCompoundFormula =
253 "<RL>"
254 + generateTagWithAttribute(
Omer Nebil Yaveroglu15395f52020-01-22 12:14:44 +0000255 /* tag= */ "R",
256 Collections.singletonMap("E", String.valueOf(Rule.DENY)),
257 /* closed= */ false)
Song Pan75147d52019-11-19 00:57:46 +0000258 + generateTagWithAttribute(
Omer Nebil Yaveroglu15395f52020-01-22 12:14:44 +0000259 /* tag= */ "OF",
260 Collections.singletonMap("C", String.valueOf(CompoundFormula.NOT)),
261 /* closed= */ false)
Song Pan75147d52019-11-19 00:57:46 +0000262 + generateTagWithAttribute(
Omer Nebil Yaveroglu15395f52020-01-22 12:14:44 +0000263 /* tag= */ "AF", packageNameAttrs, /* closed= */ true)
Song Pan75147d52019-11-19 00:57:46 +0000264 + generateTagWithAttribute(
Omer Nebil Yaveroglu15395f52020-01-22 12:14:44 +0000265 /* tag= */ "AF", versionCodeAttrs, /* closed= */ true)
Song Pan75147d52019-11-19 00:57:46 +0000266 + "</OF>"
267 + "</R>"
268 + "</RL>";
Khaled Abdelmohsen21a96052019-11-06 12:23:22 +0000269 RuleParser xmlParser = new RuleXmlParser();
270
271 assertExpectException(
272 RuleParseException.class,
273 /* expectedExceptionMessageRegex */ "Connector NOT must have 1 formula only",
Khaled Abdelmohsen6ecfebc2019-12-03 15:00:49 +0000274 () -> xmlParser.parse(ruleXmlCompoundFormula.getBytes(StandardCharsets.UTF_8)));
Khaled Abdelmohsen21a96052019-11-06 12:23:22 +0000275 }
276
277 @Test
Song Pan75147d52019-11-19 00:57:46 +0000278 public void testXmlString_invalidCompoundFormula_invalidOperator() throws Exception {
Khaled Abdelmohsenb05d7062019-11-19 01:22:23 +0000279 Map<String, String> packageNameAttrs = new HashMap<>();
280 packageNameAttrs.put("K", String.valueOf(AtomicFormula.PACKAGE_NAME));
281 packageNameAttrs.put("O", "INVALID_OPERATOR");
282 packageNameAttrs.put("V", "com.app.test");
Song Pan75147d52019-11-19 00:57:46 +0000283 String ruleXmlCompoundFormula =
284 "<RL>"
285 + generateTagWithAttribute(
Omer Nebil Yaveroglu15395f52020-01-22 12:14:44 +0000286 /* tag= */ "R",
287 Collections.singletonMap("E", String.valueOf(Rule.DENY)),
288 /* closed= */ false)
Song Pan75147d52019-11-19 00:57:46 +0000289 + generateTagWithAttribute(
Omer Nebil Yaveroglu15395f52020-01-22 12:14:44 +0000290 /* tag= */ "OF",
291 Collections.singletonMap("C", String.valueOf(CompoundFormula.NOT)),
292 /* closed= */ false)
Song Pan75147d52019-11-19 00:57:46 +0000293 + generateTagWithAttribute(
Omer Nebil Yaveroglu15395f52020-01-22 12:14:44 +0000294 /* tag= */ "AF", packageNameAttrs, /* closed= */ true)
Song Pan75147d52019-11-19 00:57:46 +0000295 + "</OF>"
296 + "</R>"
297 + "</RL>";
Khaled Abdelmohsen21a96052019-11-06 12:23:22 +0000298 RuleParser xmlParser = new RuleXmlParser();
299
300 assertExpectException(
301 RuleParseException.class,
302 /* expectedExceptionMessageRegex */ "For input string: \"INVALID_OPERATOR\"",
Khaled Abdelmohsen6ecfebc2019-12-03 15:00:49 +0000303 () -> xmlParser.parse(ruleXmlCompoundFormula.getBytes(StandardCharsets.UTF_8)));
Khaled Abdelmohsen21a96052019-11-06 12:23:22 +0000304 }
305
306 @Test
Song Pan75147d52019-11-19 00:57:46 +0000307 public void testXmlString_invalidCompoundFormula_invalidEffect() throws Exception {
Khaled Abdelmohsenb05d7062019-11-19 01:22:23 +0000308 Map<String, String> packageNameAttrs = new HashMap<>();
309 packageNameAttrs.put("K", String.valueOf(AtomicFormula.PACKAGE_NAME));
Khaled Abdelmohsenb05d7062019-11-19 01:22:23 +0000310 packageNameAttrs.put("V", "com.app.test");
Song Pan75147d52019-11-19 00:57:46 +0000311 String ruleXmlCompoundFormula =
312 "<RL>"
313 + generateTagWithAttribute(
Omer Nebil Yaveroglu15395f52020-01-22 12:14:44 +0000314 /* tag= */ "R",
315 Collections.singletonMap("E", "INVALID_EFFECT"),
316 /* closed= */ false)
Song Pan75147d52019-11-19 00:57:46 +0000317 + generateTagWithAttribute(
Omer Nebil Yaveroglu15395f52020-01-22 12:14:44 +0000318 /* tag= */ "OF",
319 Collections.singletonMap("C", String.valueOf(CompoundFormula.NOT)),
320 /* closed= */ false)
Song Pan75147d52019-11-19 00:57:46 +0000321 + generateTagWithAttribute(
Omer Nebil Yaveroglu15395f52020-01-22 12:14:44 +0000322 /* tag= */ "AF", packageNameAttrs, /* closed= */ true)
Song Pan75147d52019-11-19 00:57:46 +0000323 + "</OF>"
324 + "</R>"
325 + "</RL>";
Khaled Abdelmohsen21a96052019-11-06 12:23:22 +0000326 RuleParser xmlParser = new RuleXmlParser();
327
328 assertExpectException(
329 RuleParseException.class,
330 /* expectedExceptionMessageRegex */ "For input string: \"INVALID_EFFECT\"",
Khaled Abdelmohsen6ecfebc2019-12-03 15:00:49 +0000331 () -> xmlParser.parse(ruleXmlCompoundFormula.getBytes(StandardCharsets.UTF_8)));
Khaled Abdelmohsen21a96052019-11-06 12:23:22 +0000332 }
333
334 @Test
Song Pan75147d52019-11-19 00:57:46 +0000335 public void testXmlString_invalidCompoundFormula_invalidTags() throws Exception {
Khaled Abdelmohsenb05d7062019-11-19 01:22:23 +0000336 Map<String, String> packageNameAttrs = new HashMap<>();
337 packageNameAttrs.put("K", String.valueOf(AtomicFormula.PACKAGE_NAME));
Khaled Abdelmohsenb05d7062019-11-19 01:22:23 +0000338 packageNameAttrs.put("V", "com.app.test");
Song Pan75147d52019-11-19 00:57:46 +0000339 String ruleXmlCompoundFormula =
340 "<RL>"
341 + generateTagWithAttribute(
Omer Nebil Yaveroglu15395f52020-01-22 12:14:44 +0000342 /* tag= */ "R",
343 Collections.singletonMap("E", String.valueOf(Rule.DENY)),
344 /* closed= */ false)
Song Pan75147d52019-11-19 00:57:46 +0000345 + generateTagWithAttribute(
Omer Nebil Yaveroglu15395f52020-01-22 12:14:44 +0000346 /* tag= */ "OF",
347 Collections.singletonMap("C", String.valueOf(CompoundFormula.NOT)),
348 /* closed= */ false)
Song Pan75147d52019-11-19 00:57:46 +0000349 + generateTagWithAttribute(
Omer Nebil Yaveroglu15395f52020-01-22 12:14:44 +0000350 /* tag= */ "InvalidAtomicFormula",
351 packageNameAttrs,
352 /* closed= */ true)
Song Pan75147d52019-11-19 00:57:46 +0000353 + "</OF>"
354 + "</R>"
355 + "</RL>";
Khaled Abdelmohsen21a96052019-11-06 12:23:22 +0000356 RuleParser xmlParser = new RuleXmlParser();
357
358 assertExpectException(
359 RuleParseException.class,
Khaled Abdelmohsenb05d7062019-11-19 01:22:23 +0000360 /* expectedExceptionMessageRegex */ "Found unexpected tag: InvalidAtomicFormula",
Khaled Abdelmohsen6ecfebc2019-12-03 15:00:49 +0000361 () -> xmlParser.parse(ruleXmlCompoundFormula.getBytes(StandardCharsets.UTF_8)));
Khaled Abdelmohsen21a96052019-11-06 12:23:22 +0000362 }
363
364 @Test
365 public void testXmlString_validAtomicFormula_stringValue() throws Exception {
Khaled Abdelmohsenb05d7062019-11-19 01:22:23 +0000366 Map<String, String> packageNameAttrs = new HashMap<>();
367 packageNameAttrs.put("K", String.valueOf(AtomicFormula.PACKAGE_NAME));
Khaled Abdelmohsenb05d7062019-11-19 01:22:23 +0000368 packageNameAttrs.put("V", "com.app.test");
Song Pan75147d52019-11-19 00:57:46 +0000369 String ruleXmlAtomicFormula =
370 "<RL>"
371 + generateTagWithAttribute(
Omer Nebil Yaveroglu15395f52020-01-22 12:14:44 +0000372 /* tag= */ "R",
373 Collections.singletonMap("E", String.valueOf(Rule.DENY)),
374 /* closed= */ false)
Song Pan75147d52019-11-19 00:57:46 +0000375 + generateTagWithAttribute(
Omer Nebil Yaveroglu15395f52020-01-22 12:14:44 +0000376 /* tag= */ "AF", packageNameAttrs, /* closed= */ true)
Song Pan75147d52019-11-19 00:57:46 +0000377 + "</R>"
378 + "</RL>";
Khaled Abdelmohsen12b110e2019-11-05 17:24:48 +0000379 RuleParser xmlParser = new RuleXmlParser();
Song Pan75147d52019-11-19 00:57:46 +0000380 Rule expectedRule =
381 new Rule(
382 new AtomicFormula.StringAtomicFormula(
383 AtomicFormula.PACKAGE_NAME,
384 "com.app.test",
385 /* isHashedValue= */ false),
386 Rule.DENY);
Khaled Abdelmohsen12b110e2019-11-05 17:24:48 +0000387
Khaled Abdelmohsen6ecfebc2019-12-03 15:00:49 +0000388 List<Rule> rules = xmlParser.parse(ruleXmlAtomicFormula.getBytes(StandardCharsets.UTF_8));
Khaled Abdelmohsen12b110e2019-11-05 17:24:48 +0000389
390 assertThat(rules).isEqualTo(Collections.singletonList(expectedRule));
391 }
392
393 @Test
Khaled Abdelmohsen21a96052019-11-06 12:23:22 +0000394 public void testXmlString_validAtomicFormula_integerValue() throws Exception {
Khaled Abdelmohsenb05d7062019-11-19 01:22:23 +0000395 Map<String, String> versionCodeAttrs = new HashMap<>();
396 versionCodeAttrs.put("K", String.valueOf(AtomicFormula.VERSION_CODE));
397 versionCodeAttrs.put("O", String.valueOf(AtomicFormula.EQ));
398 versionCodeAttrs.put("V", "1");
Song Pan75147d52019-11-19 00:57:46 +0000399 String ruleXmlAtomicFormula =
400 "<RL>"
401 + generateTagWithAttribute(
Omer Nebil Yaveroglu15395f52020-01-22 12:14:44 +0000402 /* tag= */ "R",
403 Collections.singletonMap("E", String.valueOf(Rule.DENY)),
404 /* closed= */ false)
Song Pan75147d52019-11-19 00:57:46 +0000405 + generateTagWithAttribute(
Omer Nebil Yaveroglu15395f52020-01-22 12:14:44 +0000406 /* tag= */ "AF", versionCodeAttrs, /* closed= */ true)
Song Pan75147d52019-11-19 00:57:46 +0000407 + "</R>"
408 + "</RL>";
Khaled Abdelmohsen12b110e2019-11-05 17:24:48 +0000409 RuleParser xmlParser = new RuleXmlParser();
Song Pan75147d52019-11-19 00:57:46 +0000410 Rule expectedRule =
411 new Rule(
Omer Nebil Yaveroglu15395f52020-01-22 12:14:44 +0000412 new AtomicFormula.LongAtomicFormula(
Song Pan75147d52019-11-19 00:57:46 +0000413 AtomicFormula.VERSION_CODE, AtomicFormula.EQ, 1),
414 Rule.DENY);
Khaled Abdelmohsen12b110e2019-11-05 17:24:48 +0000415
Khaled Abdelmohsen6ecfebc2019-12-03 15:00:49 +0000416 List<Rule> rules = xmlParser.parse(ruleXmlAtomicFormula.getBytes(StandardCharsets.UTF_8));
Khaled Abdelmohsen12b110e2019-11-05 17:24:48 +0000417
418 assertThat(rules).isEqualTo(Collections.singletonList(expectedRule));
419 }
420
421 @Test
Khaled Abdelmohsen21a96052019-11-06 12:23:22 +0000422 public void testXmlString_validAtomicFormula_booleanValue() throws Exception {
Khaled Abdelmohsenb05d7062019-11-19 01:22:23 +0000423 Map<String, String> preInstalledAttrs = new HashMap<>();
424 preInstalledAttrs.put("K", String.valueOf(AtomicFormula.PRE_INSTALLED));
Khaled Abdelmohsenb05d7062019-11-19 01:22:23 +0000425 preInstalledAttrs.put("V", "true");
Song Pan75147d52019-11-19 00:57:46 +0000426 String ruleXmlAtomicFormula =
427 "<RL>"
428 + generateTagWithAttribute(
Omer Nebil Yaveroglu15395f52020-01-22 12:14:44 +0000429 /* tag= */ "R",
430 Collections.singletonMap("E", String.valueOf(Rule.DENY)),
431 /* closed= */ false)
Song Pan75147d52019-11-19 00:57:46 +0000432 + generateTagWithAttribute(
Omer Nebil Yaveroglu15395f52020-01-22 12:14:44 +0000433 /* tag= */ "AF", preInstalledAttrs, /* closed= */ true)
Song Pan75147d52019-11-19 00:57:46 +0000434 + "</R>"
435 + "</RL>";
Khaled Abdelmohsen12b110e2019-11-05 17:24:48 +0000436 RuleParser xmlParser = new RuleXmlParser();
Song Pan75147d52019-11-19 00:57:46 +0000437 Rule expectedRule =
438 new Rule(
Omer Nebil Yaveroglu15395f52020-01-22 12:14:44 +0000439 new AtomicFormula.BooleanAtomicFormula(
440 AtomicFormula.PRE_INSTALLED, true),
Song Pan75147d52019-11-19 00:57:46 +0000441 Rule.DENY);
Khaled Abdelmohsen12b110e2019-11-05 17:24:48 +0000442
Khaled Abdelmohsen6ecfebc2019-12-03 15:00:49 +0000443 List<Rule> rules = xmlParser.parse(ruleXmlAtomicFormula.getBytes(StandardCharsets.UTF_8));
Khaled Abdelmohsen12b110e2019-11-05 17:24:48 +0000444
445 assertThat(rules).isEqualTo(Collections.singletonList(expectedRule));
446 }
447
448 @Test
Khaled Abdelmohsenb05d7062019-11-19 01:22:23 +0000449 public void testXmlString_validAtomicFormula_differentAttributeOrder() throws Exception {
450 Map<String, String> packageNameAttrs = new HashMap<>();
451 packageNameAttrs.put("K", String.valueOf(AtomicFormula.PACKAGE_NAME));
452 packageNameAttrs.put("V", "com.app.test");
Song Pan75147d52019-11-19 00:57:46 +0000453 String ruleXmlAtomicFormula =
454 "<RL>"
455 + generateTagWithAttribute(
Omer Nebil Yaveroglu15395f52020-01-22 12:14:44 +0000456 /* tag= */ "R",
457 Collections.singletonMap("E", String.valueOf(Rule.DENY)),
458 /* closed= */ false)
Song Pan75147d52019-11-19 00:57:46 +0000459 + generateTagWithAttribute(
Omer Nebil Yaveroglu15395f52020-01-22 12:14:44 +0000460 /* tag= */ "AF", packageNameAttrs, /* closed= */ true)
Song Pan75147d52019-11-19 00:57:46 +0000461 + "</R>"
462 + "</RL>";
Khaled Abdelmohsen12b110e2019-11-05 17:24:48 +0000463 RuleParser xmlParser = new RuleXmlParser();
Song Pan75147d52019-11-19 00:57:46 +0000464 Rule expectedRule =
465 new Rule(
466 new AtomicFormula.StringAtomicFormula(
467 AtomicFormula.PACKAGE_NAME,
468 "com.app.test",
469 /* isHashedValue= */ false),
470 Rule.DENY);
Khaled Abdelmohsen12b110e2019-11-05 17:24:48 +0000471
Khaled Abdelmohsen6ecfebc2019-12-03 15:00:49 +0000472 List<Rule> rules = xmlParser.parse(ruleXmlAtomicFormula.getBytes(StandardCharsets.UTF_8));
Khaled Abdelmohsen12b110e2019-11-05 17:24:48 +0000473
474 assertThat(rules).isEqualTo(Collections.singletonList(expectedRule));
475 }
476
477 @Test
Khaled Abdelmohsenb05d7062019-11-19 01:22:23 +0000478 public void testXmlString_invalidAtomicFormula_invalidAttribute() throws Exception {
479 Map<String, String> packageNameAttrs = new HashMap<>();
480 packageNameAttrs.put("BadKey", String.valueOf(AtomicFormula.PACKAGE_NAME));
Khaled Abdelmohsenb05d7062019-11-19 01:22:23 +0000481 packageNameAttrs.put("V", "com.app.test");
Song Pan75147d52019-11-19 00:57:46 +0000482 String ruleXmlAtomicFormula =
483 "<RL>"
484 + generateTagWithAttribute(
Omer Nebil Yaveroglu15395f52020-01-22 12:14:44 +0000485 /* tag= */ "R",
486 Collections.singletonMap("E", String.valueOf(Rule.DENY)),
487 /* closed= */ false)
Song Pan75147d52019-11-19 00:57:46 +0000488 + generateTagWithAttribute(
Omer Nebil Yaveroglu15395f52020-01-22 12:14:44 +0000489 /* tag= */ "AF", packageNameAttrs, /* closed= */ true)
Song Pan75147d52019-11-19 00:57:46 +0000490 + "</R>"
491 + "</RL>";
Khaled Abdelmohsen12b110e2019-11-05 17:24:48 +0000492 RuleParser xmlParser = new RuleXmlParser();
493
Khaled Abdelmohsen21a96052019-11-06 12:23:22 +0000494 assertExpectException(
495 RuleParseException.class,
Khaled Abdelmohsene39fa0f2019-11-25 17:24:22 +0000496 /* expectedExceptionMessageRegex */ "Found unexpected key: -1",
Khaled Abdelmohsen6ecfebc2019-12-03 15:00:49 +0000497 () -> xmlParser.parse(ruleXmlAtomicFormula.getBytes(StandardCharsets.UTF_8)));
Khaled Abdelmohsen12b110e2019-11-05 17:24:48 +0000498 }
499
500 @Test
Khaled Abdelmohsene39fa0f2019-11-25 17:24:22 +0000501 public void testXmlString_invalidRule_invalidAttribute() throws Exception {
502 Map<String, String> packageNameAttrs = new HashMap<>();
503 packageNameAttrs.put("K", String.valueOf(AtomicFormula.PACKAGE_NAME));
504 packageNameAttrs.put("V", "com.app.test");
Song Pan75147d52019-11-19 00:57:46 +0000505 String ruleXmlAtomicFormula =
506 "<RL>"
507 + generateTagWithAttribute(
Omer Nebil Yaveroglu15395f52020-01-22 12:14:44 +0000508 /* tag= */ "R",
509 Collections.singletonMap("BadEffect", String.valueOf(Rule.DENY)),
510 /* closed= */ false)
Song Pan75147d52019-11-19 00:57:46 +0000511 + generateTagWithAttribute(
Omer Nebil Yaveroglu15395f52020-01-22 12:14:44 +0000512 /* tag= */ "AF", packageNameAttrs, /* closed= */ true)
Song Pan75147d52019-11-19 00:57:46 +0000513 + "</R>"
514 + "</RL>";
Khaled Abdelmohsene39fa0f2019-11-25 17:24:22 +0000515 RuleParser xmlParser = new RuleXmlParser();
516 assertExpectException(
517 RuleParseException.class,
518 /* expectedExceptionMessageRegex */ "Unknown effect: -1",
Khaled Abdelmohsen6ecfebc2019-12-03 15:00:49 +0000519 () -> xmlParser.parse(ruleXmlAtomicFormula.getBytes(StandardCharsets.UTF_8)));
Khaled Abdelmohsene39fa0f2019-11-25 17:24:22 +0000520 }
521
522 @Test
Song Pan75147d52019-11-19 00:57:46 +0000523 public void testXmlString_invalidCompoundFormula_invalidAttribute() throws Exception {
Khaled Abdelmohsene39fa0f2019-11-25 17:24:22 +0000524 Map<String, String> packageNameAttrs = new HashMap<>();
525 packageNameAttrs.put("K", String.valueOf(AtomicFormula.PACKAGE_NAME));
526 packageNameAttrs.put("V", "com.app.test");
Song Pan75147d52019-11-19 00:57:46 +0000527 String ruleXmlCompoundFormula =
528 "<RL>"
529 + generateTagWithAttribute(
Omer Nebil Yaveroglu15395f52020-01-22 12:14:44 +0000530 /* tag= */ "R",
531 Collections.singletonMap("E", String.valueOf(Rule.DENY)),
532 /* closed= */ false)
Song Pan75147d52019-11-19 00:57:46 +0000533 + generateTagWithAttribute(
Omer Nebil Yaveroglu15395f52020-01-22 12:14:44 +0000534 /* tag= */ "OF",
535 Collections.singletonMap(
536 "BadConnector", String.valueOf(CompoundFormula.NOT)),
537 /* closed= */ false)
Song Pan75147d52019-11-19 00:57:46 +0000538 + generateTagWithAttribute(
Omer Nebil Yaveroglu15395f52020-01-22 12:14:44 +0000539 /* tag= */ "AF", packageNameAttrs, /* closed= */ true)
Song Pan75147d52019-11-19 00:57:46 +0000540 + "</OF>"
541 + "</R>"
542 + "</RL>";
Khaled Abdelmohsene39fa0f2019-11-25 17:24:22 +0000543 RuleParser xmlParser = new RuleXmlParser();
544 assertExpectException(
545 RuleParseException.class,
546 /* expectedExceptionMessageRegex */ "Unknown connector: -1",
Khaled Abdelmohsen6ecfebc2019-12-03 15:00:49 +0000547 () -> xmlParser.parse(ruleXmlCompoundFormula.getBytes(StandardCharsets.UTF_8)));
Khaled Abdelmohsene39fa0f2019-11-25 17:24:22 +0000548 }
549
550 @Test
Khaled Abdelmohsen21a96052019-11-06 12:23:22 +0000551 public void testXmlString_invalidAtomicFormula() throws Exception {
Khaled Abdelmohsenb05d7062019-11-19 01:22:23 +0000552 Map<String, String> packageNameAttrs = new HashMap<>();
553 packageNameAttrs.put("K", String.valueOf(AtomicFormula.VERSION_CODE));
554 packageNameAttrs.put("O", String.valueOf(AtomicFormula.EQ));
555 packageNameAttrs.put("V", "com.app.test");
Song Pan75147d52019-11-19 00:57:46 +0000556 String ruleXmlAtomicFormula =
557 "<RL>"
558 + generateTagWithAttribute(
Omer Nebil Yaveroglu15395f52020-01-22 12:14:44 +0000559 /* tag= */ "R",
560 Collections.singletonMap("E", String.valueOf(Rule.DENY)),
561 /* closed= */ false)
Song Pan75147d52019-11-19 00:57:46 +0000562 + generateTagWithAttribute(
Omer Nebil Yaveroglu15395f52020-01-22 12:14:44 +0000563 /* tag= */ "AF", packageNameAttrs, /* closed= */ true)
Song Pan75147d52019-11-19 00:57:46 +0000564 + "</R>"
565 + "</RL>";
Khaled Abdelmohsen12b110e2019-11-05 17:24:48 +0000566 RuleParser xmlParser = new RuleXmlParser();
567
Khaled Abdelmohsen21a96052019-11-06 12:23:22 +0000568 assertExpectException(
569 RuleParseException.class,
570 /* expectedExceptionMessageRegex */ "For input string: \"com.app.test\"",
Khaled Abdelmohsen6ecfebc2019-12-03 15:00:49 +0000571 () -> xmlParser.parse(ruleXmlAtomicFormula.getBytes(StandardCharsets.UTF_8)));
Khaled Abdelmohsen216109e2019-11-04 17:49:18 +0000572 }
573
574 @Test
575 public void testXmlString_withNoRuleList() {
Khaled Abdelmohsenb05d7062019-11-19 01:22:23 +0000576 Map<String, String> atomicFormulaAttrs = new HashMap<>();
577 atomicFormulaAttrs.put("K", String.valueOf(AtomicFormula.PACKAGE_NAME));
Khaled Abdelmohsenb05d7062019-11-19 01:22:23 +0000578 atomicFormulaAttrs.put("V", "com.app.test");
Song Pan75147d52019-11-19 00:57:46 +0000579 String ruleXmlWithNoRuleList =
580 generateTagWithAttribute(
Omer Nebil Yaveroglu15395f52020-01-22 12:14:44 +0000581 /* tag= */ "R",
582 Collections.singletonMap("E", String.valueOf(Rule.DENY)),
583 /* closed= */ false)
Song Pan75147d52019-11-19 00:57:46 +0000584 + generateTagWithAttribute(
Omer Nebil Yaveroglu15395f52020-01-22 12:14:44 +0000585 /* tag= */ "OF",
586 Collections.singletonMap("C", String.valueOf(CompoundFormula.NOT)),
587 /* closed= */ false)
Song Pan75147d52019-11-19 00:57:46 +0000588 + generateTagWithAttribute(
Omer Nebil Yaveroglu15395f52020-01-22 12:14:44 +0000589 /* tag= */ "AF", atomicFormulaAttrs, /* closed= */ true)
Song Pan75147d52019-11-19 00:57:46 +0000590 + "</OF>"
591 + "</R>";
Khaled Abdelmohsen216109e2019-11-04 17:49:18 +0000592 RuleParser xmlParser = new RuleXmlParser();
593
594 assertExpectException(
Khaled Abdelmohsen21a96052019-11-06 12:23:22 +0000595 RuleParseException.class,
Khaled Abdelmohsen6d77f942019-11-12 16:47:01 +0000596 /* expectedExceptionMessageRegex */ "Rules must start with RuleList <RL> tag",
Khaled Abdelmohsen6ecfebc2019-12-03 15:00:49 +0000597 () -> xmlParser.parse(ruleXmlWithNoRuleList.getBytes(StandardCharsets.UTF_8)));
Khaled Abdelmohsen216109e2019-11-04 17:49:18 +0000598 }
599
600 @Test
601 public void testXmlStream_withNoRuleList() {
Khaled Abdelmohsenb05d7062019-11-19 01:22:23 +0000602 Map<String, String> atomicFormulaAttrs = new HashMap<>();
603 atomicFormulaAttrs.put("K", String.valueOf(AtomicFormula.PACKAGE_NAME));
Khaled Abdelmohsenb05d7062019-11-19 01:22:23 +0000604 atomicFormulaAttrs.put("V", "com.app.test");
Song Pan75147d52019-11-19 00:57:46 +0000605 String ruleXmlWithNoRuleList =
606 generateTagWithAttribute(
Omer Nebil Yaveroglu15395f52020-01-22 12:14:44 +0000607 /* tag= */ "R",
608 Collections.singletonMap("E", String.valueOf(Rule.DENY)),
609 /* closed= */ false)
Song Pan75147d52019-11-19 00:57:46 +0000610 + generateTagWithAttribute(
Omer Nebil Yaveroglu15395f52020-01-22 12:14:44 +0000611 /* tag= */ "OF",
612 Collections.singletonMap("C", String.valueOf(CompoundFormula.NOT)),
613 /* closed= */ false)
Song Pan75147d52019-11-19 00:57:46 +0000614 + generateTagWithAttribute(
Omer Nebil Yaveroglu15395f52020-01-22 12:14:44 +0000615 /* tag= */ "AF", atomicFormulaAttrs, /* closed= */ true)
Song Pan75147d52019-11-19 00:57:46 +0000616 + "</OF>"
617 + "</R>";
Khaled Abdelmohsen216109e2019-11-04 17:49:18 +0000618 RuleParser xmlParser = new RuleXmlParser();
619
620 assertExpectException(
Khaled Abdelmohsen21a96052019-11-06 12:23:22 +0000621 RuleParseException.class,
Khaled Abdelmohsen6d77f942019-11-12 16:47:01 +0000622 /* expectedExceptionMessageRegex */ "Rules must start with RuleList <RL> tag",
Song Pan8a0b6fc2020-01-14 16:33:50 +0000623 () -> xmlParser.parse(ruleXmlWithNoRuleList.getBytes()));
Khaled Abdelmohsen216109e2019-11-04 17:49:18 +0000624 }
Khaled Abdelmohsenb05d7062019-11-19 01:22:23 +0000625
Song Pan75147d52019-11-19 00:57:46 +0000626 private String generateTagWithAttribute(
627 String tag, Map<String, String> attributeValues, boolean closed) {
Khaled Abdelmohsenb05d7062019-11-19 01:22:23 +0000628 StringBuilder res = new StringBuilder("<");
629 res.append(tag);
630 for (String attribute : attributeValues.keySet()) {
631 res.append(" ");
632 res.append(attribute);
633 res.append("=\"");
634 res.append(attributeValues.get(attribute));
635 res.append("\"");
636 }
637 res.append(closed ? " />" : ">");
638 return res.toString();
639 }
Khaled Abdelmohsen216109e2019-11-04 17:49:18 +0000640}