Khaled Abdelmohsen | 216109e | 2019-11-04 17:49:18 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.server.integrity.parser; |
| 18 | |
| 19 | import static com.android.server.testutils.TestUtils.assertExpectException; |
| 20 | |
Khaled Abdelmohsen | 12b110e | 2019-11-05 17:24:48 +0000 | [diff] [blame] | 21 | import static com.google.common.truth.Truth.assertThat; |
Khaled Abdelmohsen | 216109e | 2019-11-04 17:49:18 +0000 | [diff] [blame] | 22 | |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 23 | import android.content.integrity.AtomicFormula; |
| 24 | import android.content.integrity.CompoundFormula; |
| 25 | import android.content.integrity.Rule; |
Khaled Abdelmohsen | 216109e | 2019-11-04 17:49:18 +0000 | [diff] [blame] | 26 | |
| 27 | import org.junit.Test; |
| 28 | import org.junit.runner.RunWith; |
| 29 | import org.junit.runners.JUnit4; |
| 30 | |
Khaled Abdelmohsen | 6ecfebc | 2019-12-03 15:00:49 +0000 | [diff] [blame] | 31 | import java.nio.charset.StandardCharsets; |
Khaled Abdelmohsen | 21a9605 | 2019-11-06 12:23:22 +0000 | [diff] [blame] | 32 | import java.util.Arrays; |
Khaled Abdelmohsen | 12b110e | 2019-11-05 17:24:48 +0000 | [diff] [blame] | 33 | import java.util.Collections; |
Khaled Abdelmohsen | b05d706 | 2019-11-19 01:22:23 +0000 | [diff] [blame] | 34 | import java.util.HashMap; |
Khaled Abdelmohsen | 216109e | 2019-11-04 17:49:18 +0000 | [diff] [blame] | 35 | import java.util.List; |
Khaled Abdelmohsen | b05d706 | 2019-11-19 01:22:23 +0000 | [diff] [blame] | 36 | import java.util.Map; |
Khaled Abdelmohsen | 216109e | 2019-11-04 17:49:18 +0000 | [diff] [blame] | 37 | |
| 38 | @RunWith(JUnit4.class) |
| 39 | public class RuleXmlParserTest { |
| 40 | |
Khaled Abdelmohsen | 216109e | 2019-11-04 17:49:18 +0000 | [diff] [blame] | 41 | @Test |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 42 | public void testXmlStream_validCompoundFormula() throws Exception { |
Khaled Abdelmohsen | b05d706 | 2019-11-19 01:22:23 +0000 | [diff] [blame] | 43 | Map<String, String> atomicFormulaAttrs = new HashMap<>(); |
| 44 | atomicFormulaAttrs.put("K", String.valueOf(AtomicFormula.PACKAGE_NAME)); |
Khaled Abdelmohsen | b05d706 | 2019-11-19 01:22:23 +0000 | [diff] [blame] | 45 | atomicFormulaAttrs.put("V", "com.app.test"); |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 46 | String ruleXmlCompoundFormula = |
| 47 | "<RL>" |
| 48 | + generateTagWithAttribute( |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 49 | /* tag= */ "R", |
| 50 | Collections.singletonMap("E", String.valueOf(Rule.DENY)), |
| 51 | /* closed= */ false) |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 52 | + generateTagWithAttribute( |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 53 | /* tag= */ "OF", |
| 54 | Collections.singletonMap("C", String.valueOf(CompoundFormula.NOT)), |
| 55 | /* closed= */ false) |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 56 | + generateTagWithAttribute( |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 57 | /* tag= */ "AF", atomicFormulaAttrs, /* closed= */ true) |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 58 | + "</OF>" |
| 59 | + "</R>" |
| 60 | + "</RL>"; |
Khaled Abdelmohsen | 216109e | 2019-11-04 17:49:18 +0000 | [diff] [blame] | 61 | RuleParser xmlParser = new RuleXmlParser(); |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 62 | 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 Abdelmohsen | 216109e | 2019-11-04 17:49:18 +0000 | [diff] [blame] | 72 | |
Song Pan | 8a0b6fc | 2020-01-14 16:33:50 +0000 | [diff] [blame] | 73 | List<Rule> rules = xmlParser.parse(ruleXmlCompoundFormula.getBytes()); |
Khaled Abdelmohsen | 216109e | 2019-11-04 17:49:18 +0000 | [diff] [blame] | 74 | |
Khaled Abdelmohsen | 21a9605 | 2019-11-06 12:23:22 +0000 | [diff] [blame] | 75 | assertThat(rules).isEqualTo(Collections.singletonList(expectedRule)); |
Khaled Abdelmohsen | 12b110e | 2019-11-05 17:24:48 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | @Test |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 79 | public void testXmlString_validCompoundFormula_notConnector() throws Exception { |
Khaled Abdelmohsen | b05d706 | 2019-11-19 01:22:23 +0000 | [diff] [blame] | 80 | Map<String, String> packageNameAttrs = new HashMap<>(); |
| 81 | packageNameAttrs.put("K", String.valueOf(AtomicFormula.PACKAGE_NAME)); |
Khaled Abdelmohsen | b05d706 | 2019-11-19 01:22:23 +0000 | [diff] [blame] | 82 | packageNameAttrs.put("V", "com.app.test"); |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 83 | String ruleXmlCompoundFormula = |
| 84 | "<RL>" |
| 85 | + generateTagWithAttribute( |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 86 | /* tag= */ "R", |
| 87 | Collections.singletonMap("E", String.valueOf(Rule.DENY)), |
| 88 | /* closed= */ false) |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 89 | + generateTagWithAttribute( |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 90 | /* tag= */ "OF", |
| 91 | Collections.singletonMap("C", String.valueOf(CompoundFormula.NOT)), |
| 92 | /* closed= */ false) |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 93 | + generateTagWithAttribute( |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 94 | /* tag= */ "AF", packageNameAttrs, /* closed= */ true) |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 95 | + "</OF>" |
| 96 | + "</R>" |
| 97 | + "</RL>"; |
Khaled Abdelmohsen | 21a9605 | 2019-11-06 12:23:22 +0000 | [diff] [blame] | 98 | RuleParser xmlParser = new RuleXmlParser(); |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 99 | 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 Abdelmohsen | 21a9605 | 2019-11-06 12:23:22 +0000 | [diff] [blame] | 109 | |
Khaled Abdelmohsen | 6ecfebc | 2019-12-03 15:00:49 +0000 | [diff] [blame] | 110 | List<Rule> rules = xmlParser.parse(ruleXmlCompoundFormula.getBytes(StandardCharsets.UTF_8)); |
Khaled Abdelmohsen | 21a9605 | 2019-11-06 12:23:22 +0000 | [diff] [blame] | 111 | |
| 112 | assertThat(rules).isEqualTo(Collections.singletonList(expectedRule)); |
| 113 | } |
| 114 | |
| 115 | @Test |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 116 | public void testXmlString_validCompoundFormula_andConnector() throws Exception { |
Khaled Abdelmohsen | b05d706 | 2019-11-19 01:22:23 +0000 | [diff] [blame] | 117 | Map<String, String> packageNameAttrs = new HashMap<>(); |
| 118 | packageNameAttrs.put("K", String.valueOf(AtomicFormula.PACKAGE_NAME)); |
Khaled Abdelmohsen | b05d706 | 2019-11-19 01:22:23 +0000 | [diff] [blame] | 119 | packageNameAttrs.put("V", "com.app.test"); |
| 120 | Map<String, String> appCertificateAttrs = new HashMap<>(); |
| 121 | appCertificateAttrs.put("K", String.valueOf(AtomicFormula.APP_CERTIFICATE)); |
Khaled Abdelmohsen | b05d706 | 2019-11-19 01:22:23 +0000 | [diff] [blame] | 122 | appCertificateAttrs.put("V", "test_cert"); |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 123 | String ruleXmlCompoundFormula = |
| 124 | "<RL>" |
| 125 | + generateTagWithAttribute( |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 126 | /* tag= */ "R", |
| 127 | Collections.singletonMap("E", String.valueOf(Rule.DENY)), |
| 128 | /* closed= */ false) |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 129 | + generateTagWithAttribute( |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 130 | /* tag= */ "OF", |
| 131 | Collections.singletonMap("C", String.valueOf(CompoundFormula.AND)), |
| 132 | /* closed= */ false) |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 133 | + generateTagWithAttribute( |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 134 | /* tag= */ "AF", packageNameAttrs, /* closed= */ true) |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 135 | + generateTagWithAttribute( |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 136 | /* tag= */ "AF", appCertificateAttrs, /* closed= */ true) |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 137 | + "</OF>" |
| 138 | + "</R>" |
| 139 | + "</RL>"; |
Khaled Abdelmohsen | 21a9605 | 2019-11-06 12:23:22 +0000 | [diff] [blame] | 140 | RuleParser xmlParser = new RuleXmlParser(); |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 141 | 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 Abdelmohsen | 6ecfebc | 2019-12-03 15:00:49 +0000 | [diff] [blame] | 155 | List<Rule> rules = xmlParser.parse(ruleXmlCompoundFormula.getBytes(StandardCharsets.UTF_8)); |
Khaled Abdelmohsen | 21a9605 | 2019-11-06 12:23:22 +0000 | [diff] [blame] | 156 | |
| 157 | assertThat(rules).isEqualTo(Collections.singletonList(expectedRule)); |
| 158 | } |
| 159 | |
| 160 | @Test |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 161 | public void testXmlString_validCompoundFormula_orConnector() throws Exception { |
Khaled Abdelmohsen | b05d706 | 2019-11-19 01:22:23 +0000 | [diff] [blame] | 162 | Map<String, String> packageNameAttrs = new HashMap<>(); |
| 163 | packageNameAttrs.put("K", String.valueOf(AtomicFormula.PACKAGE_NAME)); |
Khaled Abdelmohsen | b05d706 | 2019-11-19 01:22:23 +0000 | [diff] [blame] | 164 | packageNameAttrs.put("V", "com.app.test"); |
| 165 | Map<String, String> appCertificateAttrs = new HashMap<>(); |
| 166 | appCertificateAttrs.put("K", String.valueOf(AtomicFormula.APP_CERTIFICATE)); |
Khaled Abdelmohsen | b05d706 | 2019-11-19 01:22:23 +0000 | [diff] [blame] | 167 | appCertificateAttrs.put("V", "test_cert"); |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 168 | String ruleXmlCompoundFormula = |
| 169 | "<RL>" |
| 170 | + generateTagWithAttribute( |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 171 | /* tag= */ "R", |
| 172 | Collections.singletonMap("E", String.valueOf(Rule.DENY)), |
| 173 | /* closed= */ false) |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 174 | + generateTagWithAttribute( |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 175 | /* tag= */ "OF", |
| 176 | Collections.singletonMap("C", String.valueOf(CompoundFormula.OR)), |
| 177 | /* closed= */ false) |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 178 | + generateTagWithAttribute( |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 179 | /* tag= */ "AF", packageNameAttrs, /* closed= */ true) |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 180 | + generateTagWithAttribute( |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 181 | /* tag= */ "AF", appCertificateAttrs, /* closed= */ true) |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 182 | + "</OF>" |
| 183 | + "</R>" |
| 184 | + "</RL>"; |
Khaled Abdelmohsen | 21a9605 | 2019-11-06 12:23:22 +0000 | [diff] [blame] | 185 | RuleParser xmlParser = new RuleXmlParser(); |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 186 | 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 Abdelmohsen | 21a9605 | 2019-11-06 12:23:22 +0000 | [diff] [blame] | 200 | |
Khaled Abdelmohsen | 6ecfebc | 2019-12-03 15:00:49 +0000 | [diff] [blame] | 201 | List<Rule> rules = xmlParser.parse(ruleXmlCompoundFormula.getBytes(StandardCharsets.UTF_8)); |
Khaled Abdelmohsen | 21a9605 | 2019-11-06 12:23:22 +0000 | [diff] [blame] | 202 | |
| 203 | assertThat(rules).isEqualTo(Collections.singletonList(expectedRule)); |
| 204 | } |
| 205 | |
| 206 | @Test |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 207 | public void testXmlString_validCompoundFormula_differentTagOrder() throws Exception { |
Khaled Abdelmohsen | b05d706 | 2019-11-19 01:22:23 +0000 | [diff] [blame] | 208 | Map<String, String> packageNameAttrs = new HashMap<>(); |
| 209 | packageNameAttrs.put("K", String.valueOf(AtomicFormula.PACKAGE_NAME)); |
| 210 | packageNameAttrs.put("V", "com.app.test"); |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 211 | String ruleXmlCompoundFormula = |
| 212 | "<RL>" |
| 213 | + generateTagWithAttribute( |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 214 | /* tag= */ "R", |
| 215 | Collections.singletonMap("E", String.valueOf(Rule.DENY)), |
| 216 | /* closed= */ false) |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 217 | + generateTagWithAttribute( |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 218 | /* tag= */ "OF", |
| 219 | Collections.singletonMap("C", String.valueOf(CompoundFormula.NOT)), |
| 220 | /* closed= */ false) |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 221 | + generateTagWithAttribute( |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 222 | /* tag= */ "AF", packageNameAttrs, /* closed= */ true) |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 223 | + "</OF>" |
| 224 | + "</R>" |
| 225 | + "</RL>"; |
Khaled Abdelmohsen | 21a9605 | 2019-11-06 12:23:22 +0000 | [diff] [blame] | 226 | RuleParser xmlParser = new RuleXmlParser(); |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 227 | 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 Abdelmohsen | 21a9605 | 2019-11-06 12:23:22 +0000 | [diff] [blame] | 237 | |
Khaled Abdelmohsen | 6ecfebc | 2019-12-03 15:00:49 +0000 | [diff] [blame] | 238 | List<Rule> rules = xmlParser.parse(ruleXmlCompoundFormula.getBytes(StandardCharsets.UTF_8)); |
Khaled Abdelmohsen | 21a9605 | 2019-11-06 12:23:22 +0000 | [diff] [blame] | 239 | |
| 240 | assertThat(rules).isEqualTo(Collections.singletonList(expectedRule)); |
| 241 | } |
| 242 | |
| 243 | @Test |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 244 | public void testXmlString_invalidCompoundFormula_invalidNumberOfFormulas() throws Exception { |
Khaled Abdelmohsen | b05d706 | 2019-11-19 01:22:23 +0000 | [diff] [blame] | 245 | Map<String, String> packageNameAttrs = new HashMap<>(); |
| 246 | packageNameAttrs.put("K", String.valueOf(AtomicFormula.PACKAGE_NAME)); |
Khaled Abdelmohsen | b05d706 | 2019-11-19 01:22:23 +0000 | [diff] [blame] | 247 | 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 Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 252 | String ruleXmlCompoundFormula = |
| 253 | "<RL>" |
| 254 | + generateTagWithAttribute( |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 255 | /* tag= */ "R", |
| 256 | Collections.singletonMap("E", String.valueOf(Rule.DENY)), |
| 257 | /* closed= */ false) |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 258 | + generateTagWithAttribute( |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 259 | /* tag= */ "OF", |
| 260 | Collections.singletonMap("C", String.valueOf(CompoundFormula.NOT)), |
| 261 | /* closed= */ false) |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 262 | + generateTagWithAttribute( |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 263 | /* tag= */ "AF", packageNameAttrs, /* closed= */ true) |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 264 | + generateTagWithAttribute( |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 265 | /* tag= */ "AF", versionCodeAttrs, /* closed= */ true) |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 266 | + "</OF>" |
| 267 | + "</R>" |
| 268 | + "</RL>"; |
Khaled Abdelmohsen | 21a9605 | 2019-11-06 12:23:22 +0000 | [diff] [blame] | 269 | RuleParser xmlParser = new RuleXmlParser(); |
| 270 | |
| 271 | assertExpectException( |
| 272 | RuleParseException.class, |
| 273 | /* expectedExceptionMessageRegex */ "Connector NOT must have 1 formula only", |
Khaled Abdelmohsen | 6ecfebc | 2019-12-03 15:00:49 +0000 | [diff] [blame] | 274 | () -> xmlParser.parse(ruleXmlCompoundFormula.getBytes(StandardCharsets.UTF_8))); |
Khaled Abdelmohsen | 21a9605 | 2019-11-06 12:23:22 +0000 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | @Test |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 278 | public void testXmlString_invalidCompoundFormula_invalidOperator() throws Exception { |
Khaled Abdelmohsen | b05d706 | 2019-11-19 01:22:23 +0000 | [diff] [blame] | 279 | 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 Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 283 | String ruleXmlCompoundFormula = |
| 284 | "<RL>" |
| 285 | + generateTagWithAttribute( |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 286 | /* tag= */ "R", |
| 287 | Collections.singletonMap("E", String.valueOf(Rule.DENY)), |
| 288 | /* closed= */ false) |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 289 | + generateTagWithAttribute( |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 290 | /* tag= */ "OF", |
| 291 | Collections.singletonMap("C", String.valueOf(CompoundFormula.NOT)), |
| 292 | /* closed= */ false) |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 293 | + generateTagWithAttribute( |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 294 | /* tag= */ "AF", packageNameAttrs, /* closed= */ true) |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 295 | + "</OF>" |
| 296 | + "</R>" |
| 297 | + "</RL>"; |
Khaled Abdelmohsen | 21a9605 | 2019-11-06 12:23:22 +0000 | [diff] [blame] | 298 | RuleParser xmlParser = new RuleXmlParser(); |
| 299 | |
| 300 | assertExpectException( |
| 301 | RuleParseException.class, |
| 302 | /* expectedExceptionMessageRegex */ "For input string: \"INVALID_OPERATOR\"", |
Khaled Abdelmohsen | 6ecfebc | 2019-12-03 15:00:49 +0000 | [diff] [blame] | 303 | () -> xmlParser.parse(ruleXmlCompoundFormula.getBytes(StandardCharsets.UTF_8))); |
Khaled Abdelmohsen | 21a9605 | 2019-11-06 12:23:22 +0000 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | @Test |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 307 | public void testXmlString_invalidCompoundFormula_invalidEffect() throws Exception { |
Khaled Abdelmohsen | b05d706 | 2019-11-19 01:22:23 +0000 | [diff] [blame] | 308 | Map<String, String> packageNameAttrs = new HashMap<>(); |
| 309 | packageNameAttrs.put("K", String.valueOf(AtomicFormula.PACKAGE_NAME)); |
Khaled Abdelmohsen | b05d706 | 2019-11-19 01:22:23 +0000 | [diff] [blame] | 310 | packageNameAttrs.put("V", "com.app.test"); |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 311 | String ruleXmlCompoundFormula = |
| 312 | "<RL>" |
| 313 | + generateTagWithAttribute( |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 314 | /* tag= */ "R", |
| 315 | Collections.singletonMap("E", "INVALID_EFFECT"), |
| 316 | /* closed= */ false) |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 317 | + generateTagWithAttribute( |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 318 | /* tag= */ "OF", |
| 319 | Collections.singletonMap("C", String.valueOf(CompoundFormula.NOT)), |
| 320 | /* closed= */ false) |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 321 | + generateTagWithAttribute( |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 322 | /* tag= */ "AF", packageNameAttrs, /* closed= */ true) |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 323 | + "</OF>" |
| 324 | + "</R>" |
| 325 | + "</RL>"; |
Khaled Abdelmohsen | 21a9605 | 2019-11-06 12:23:22 +0000 | [diff] [blame] | 326 | RuleParser xmlParser = new RuleXmlParser(); |
| 327 | |
| 328 | assertExpectException( |
| 329 | RuleParseException.class, |
| 330 | /* expectedExceptionMessageRegex */ "For input string: \"INVALID_EFFECT\"", |
Khaled Abdelmohsen | 6ecfebc | 2019-12-03 15:00:49 +0000 | [diff] [blame] | 331 | () -> xmlParser.parse(ruleXmlCompoundFormula.getBytes(StandardCharsets.UTF_8))); |
Khaled Abdelmohsen | 21a9605 | 2019-11-06 12:23:22 +0000 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | @Test |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 335 | public void testXmlString_invalidCompoundFormula_invalidTags() throws Exception { |
Khaled Abdelmohsen | b05d706 | 2019-11-19 01:22:23 +0000 | [diff] [blame] | 336 | Map<String, String> packageNameAttrs = new HashMap<>(); |
| 337 | packageNameAttrs.put("K", String.valueOf(AtomicFormula.PACKAGE_NAME)); |
Khaled Abdelmohsen | b05d706 | 2019-11-19 01:22:23 +0000 | [diff] [blame] | 338 | packageNameAttrs.put("V", "com.app.test"); |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 339 | String ruleXmlCompoundFormula = |
| 340 | "<RL>" |
| 341 | + generateTagWithAttribute( |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 342 | /* tag= */ "R", |
| 343 | Collections.singletonMap("E", String.valueOf(Rule.DENY)), |
| 344 | /* closed= */ false) |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 345 | + generateTagWithAttribute( |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 346 | /* tag= */ "OF", |
| 347 | Collections.singletonMap("C", String.valueOf(CompoundFormula.NOT)), |
| 348 | /* closed= */ false) |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 349 | + generateTagWithAttribute( |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 350 | /* tag= */ "InvalidAtomicFormula", |
| 351 | packageNameAttrs, |
| 352 | /* closed= */ true) |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 353 | + "</OF>" |
| 354 | + "</R>" |
| 355 | + "</RL>"; |
Khaled Abdelmohsen | 21a9605 | 2019-11-06 12:23:22 +0000 | [diff] [blame] | 356 | RuleParser xmlParser = new RuleXmlParser(); |
| 357 | |
| 358 | assertExpectException( |
| 359 | RuleParseException.class, |
Khaled Abdelmohsen | b05d706 | 2019-11-19 01:22:23 +0000 | [diff] [blame] | 360 | /* expectedExceptionMessageRegex */ "Found unexpected tag: InvalidAtomicFormula", |
Khaled Abdelmohsen | 6ecfebc | 2019-12-03 15:00:49 +0000 | [diff] [blame] | 361 | () -> xmlParser.parse(ruleXmlCompoundFormula.getBytes(StandardCharsets.UTF_8))); |
Khaled Abdelmohsen | 21a9605 | 2019-11-06 12:23:22 +0000 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | @Test |
| 365 | public void testXmlString_validAtomicFormula_stringValue() throws Exception { |
Khaled Abdelmohsen | b05d706 | 2019-11-19 01:22:23 +0000 | [diff] [blame] | 366 | Map<String, String> packageNameAttrs = new HashMap<>(); |
| 367 | packageNameAttrs.put("K", String.valueOf(AtomicFormula.PACKAGE_NAME)); |
Khaled Abdelmohsen | b05d706 | 2019-11-19 01:22:23 +0000 | [diff] [blame] | 368 | packageNameAttrs.put("V", "com.app.test"); |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 369 | String ruleXmlAtomicFormula = |
| 370 | "<RL>" |
| 371 | + generateTagWithAttribute( |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 372 | /* tag= */ "R", |
| 373 | Collections.singletonMap("E", String.valueOf(Rule.DENY)), |
| 374 | /* closed= */ false) |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 375 | + generateTagWithAttribute( |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 376 | /* tag= */ "AF", packageNameAttrs, /* closed= */ true) |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 377 | + "</R>" |
| 378 | + "</RL>"; |
Khaled Abdelmohsen | 12b110e | 2019-11-05 17:24:48 +0000 | [diff] [blame] | 379 | RuleParser xmlParser = new RuleXmlParser(); |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 380 | Rule expectedRule = |
| 381 | new Rule( |
| 382 | new AtomicFormula.StringAtomicFormula( |
| 383 | AtomicFormula.PACKAGE_NAME, |
| 384 | "com.app.test", |
| 385 | /* isHashedValue= */ false), |
| 386 | Rule.DENY); |
Khaled Abdelmohsen | 12b110e | 2019-11-05 17:24:48 +0000 | [diff] [blame] | 387 | |
Khaled Abdelmohsen | 6ecfebc | 2019-12-03 15:00:49 +0000 | [diff] [blame] | 388 | List<Rule> rules = xmlParser.parse(ruleXmlAtomicFormula.getBytes(StandardCharsets.UTF_8)); |
Khaled Abdelmohsen | 12b110e | 2019-11-05 17:24:48 +0000 | [diff] [blame] | 389 | |
| 390 | assertThat(rules).isEqualTo(Collections.singletonList(expectedRule)); |
| 391 | } |
| 392 | |
| 393 | @Test |
Khaled Abdelmohsen | 21a9605 | 2019-11-06 12:23:22 +0000 | [diff] [blame] | 394 | public void testXmlString_validAtomicFormula_integerValue() throws Exception { |
Khaled Abdelmohsen | b05d706 | 2019-11-19 01:22:23 +0000 | [diff] [blame] | 395 | 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 Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 399 | String ruleXmlAtomicFormula = |
| 400 | "<RL>" |
| 401 | + generateTagWithAttribute( |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 402 | /* tag= */ "R", |
| 403 | Collections.singletonMap("E", String.valueOf(Rule.DENY)), |
| 404 | /* closed= */ false) |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 405 | + generateTagWithAttribute( |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 406 | /* tag= */ "AF", versionCodeAttrs, /* closed= */ true) |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 407 | + "</R>" |
| 408 | + "</RL>"; |
Khaled Abdelmohsen | 12b110e | 2019-11-05 17:24:48 +0000 | [diff] [blame] | 409 | RuleParser xmlParser = new RuleXmlParser(); |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 410 | Rule expectedRule = |
| 411 | new Rule( |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 412 | new AtomicFormula.LongAtomicFormula( |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 413 | AtomicFormula.VERSION_CODE, AtomicFormula.EQ, 1), |
| 414 | Rule.DENY); |
Khaled Abdelmohsen | 12b110e | 2019-11-05 17:24:48 +0000 | [diff] [blame] | 415 | |
Khaled Abdelmohsen | 6ecfebc | 2019-12-03 15:00:49 +0000 | [diff] [blame] | 416 | List<Rule> rules = xmlParser.parse(ruleXmlAtomicFormula.getBytes(StandardCharsets.UTF_8)); |
Khaled Abdelmohsen | 12b110e | 2019-11-05 17:24:48 +0000 | [diff] [blame] | 417 | |
| 418 | assertThat(rules).isEqualTo(Collections.singletonList(expectedRule)); |
| 419 | } |
| 420 | |
| 421 | @Test |
Khaled Abdelmohsen | 21a9605 | 2019-11-06 12:23:22 +0000 | [diff] [blame] | 422 | public void testXmlString_validAtomicFormula_booleanValue() throws Exception { |
Khaled Abdelmohsen | b05d706 | 2019-11-19 01:22:23 +0000 | [diff] [blame] | 423 | Map<String, String> preInstalledAttrs = new HashMap<>(); |
| 424 | preInstalledAttrs.put("K", String.valueOf(AtomicFormula.PRE_INSTALLED)); |
Khaled Abdelmohsen | b05d706 | 2019-11-19 01:22:23 +0000 | [diff] [blame] | 425 | preInstalledAttrs.put("V", "true"); |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 426 | String ruleXmlAtomicFormula = |
| 427 | "<RL>" |
| 428 | + generateTagWithAttribute( |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 429 | /* tag= */ "R", |
| 430 | Collections.singletonMap("E", String.valueOf(Rule.DENY)), |
| 431 | /* closed= */ false) |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 432 | + generateTagWithAttribute( |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 433 | /* tag= */ "AF", preInstalledAttrs, /* closed= */ true) |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 434 | + "</R>" |
| 435 | + "</RL>"; |
Khaled Abdelmohsen | 12b110e | 2019-11-05 17:24:48 +0000 | [diff] [blame] | 436 | RuleParser xmlParser = new RuleXmlParser(); |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 437 | Rule expectedRule = |
| 438 | new Rule( |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 439 | new AtomicFormula.BooleanAtomicFormula( |
| 440 | AtomicFormula.PRE_INSTALLED, true), |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 441 | Rule.DENY); |
Khaled Abdelmohsen | 12b110e | 2019-11-05 17:24:48 +0000 | [diff] [blame] | 442 | |
Khaled Abdelmohsen | 6ecfebc | 2019-12-03 15:00:49 +0000 | [diff] [blame] | 443 | List<Rule> rules = xmlParser.parse(ruleXmlAtomicFormula.getBytes(StandardCharsets.UTF_8)); |
Khaled Abdelmohsen | 12b110e | 2019-11-05 17:24:48 +0000 | [diff] [blame] | 444 | |
| 445 | assertThat(rules).isEqualTo(Collections.singletonList(expectedRule)); |
| 446 | } |
| 447 | |
| 448 | @Test |
Khaled Abdelmohsen | b05d706 | 2019-11-19 01:22:23 +0000 | [diff] [blame] | 449 | 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 Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 453 | String ruleXmlAtomicFormula = |
| 454 | "<RL>" |
| 455 | + generateTagWithAttribute( |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 456 | /* tag= */ "R", |
| 457 | Collections.singletonMap("E", String.valueOf(Rule.DENY)), |
| 458 | /* closed= */ false) |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 459 | + generateTagWithAttribute( |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 460 | /* tag= */ "AF", packageNameAttrs, /* closed= */ true) |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 461 | + "</R>" |
| 462 | + "</RL>"; |
Khaled Abdelmohsen | 12b110e | 2019-11-05 17:24:48 +0000 | [diff] [blame] | 463 | RuleParser xmlParser = new RuleXmlParser(); |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 464 | Rule expectedRule = |
| 465 | new Rule( |
| 466 | new AtomicFormula.StringAtomicFormula( |
| 467 | AtomicFormula.PACKAGE_NAME, |
| 468 | "com.app.test", |
| 469 | /* isHashedValue= */ false), |
| 470 | Rule.DENY); |
Khaled Abdelmohsen | 12b110e | 2019-11-05 17:24:48 +0000 | [diff] [blame] | 471 | |
Khaled Abdelmohsen | 6ecfebc | 2019-12-03 15:00:49 +0000 | [diff] [blame] | 472 | List<Rule> rules = xmlParser.parse(ruleXmlAtomicFormula.getBytes(StandardCharsets.UTF_8)); |
Khaled Abdelmohsen | 12b110e | 2019-11-05 17:24:48 +0000 | [diff] [blame] | 473 | |
| 474 | assertThat(rules).isEqualTo(Collections.singletonList(expectedRule)); |
| 475 | } |
| 476 | |
| 477 | @Test |
Khaled Abdelmohsen | b05d706 | 2019-11-19 01:22:23 +0000 | [diff] [blame] | 478 | public void testXmlString_invalidAtomicFormula_invalidAttribute() throws Exception { |
| 479 | Map<String, String> packageNameAttrs = new HashMap<>(); |
| 480 | packageNameAttrs.put("BadKey", String.valueOf(AtomicFormula.PACKAGE_NAME)); |
Khaled Abdelmohsen | b05d706 | 2019-11-19 01:22:23 +0000 | [diff] [blame] | 481 | packageNameAttrs.put("V", "com.app.test"); |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 482 | String ruleXmlAtomicFormula = |
| 483 | "<RL>" |
| 484 | + generateTagWithAttribute( |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 485 | /* tag= */ "R", |
| 486 | Collections.singletonMap("E", String.valueOf(Rule.DENY)), |
| 487 | /* closed= */ false) |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 488 | + generateTagWithAttribute( |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 489 | /* tag= */ "AF", packageNameAttrs, /* closed= */ true) |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 490 | + "</R>" |
| 491 | + "</RL>"; |
Khaled Abdelmohsen | 12b110e | 2019-11-05 17:24:48 +0000 | [diff] [blame] | 492 | RuleParser xmlParser = new RuleXmlParser(); |
| 493 | |
Khaled Abdelmohsen | 21a9605 | 2019-11-06 12:23:22 +0000 | [diff] [blame] | 494 | assertExpectException( |
| 495 | RuleParseException.class, |
Khaled Abdelmohsen | e39fa0f | 2019-11-25 17:24:22 +0000 | [diff] [blame] | 496 | /* expectedExceptionMessageRegex */ "Found unexpected key: -1", |
Khaled Abdelmohsen | 6ecfebc | 2019-12-03 15:00:49 +0000 | [diff] [blame] | 497 | () -> xmlParser.parse(ruleXmlAtomicFormula.getBytes(StandardCharsets.UTF_8))); |
Khaled Abdelmohsen | 12b110e | 2019-11-05 17:24:48 +0000 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | @Test |
Khaled Abdelmohsen | e39fa0f | 2019-11-25 17:24:22 +0000 | [diff] [blame] | 501 | 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 Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 505 | String ruleXmlAtomicFormula = |
| 506 | "<RL>" |
| 507 | + generateTagWithAttribute( |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 508 | /* tag= */ "R", |
| 509 | Collections.singletonMap("BadEffect", String.valueOf(Rule.DENY)), |
| 510 | /* closed= */ false) |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 511 | + generateTagWithAttribute( |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 512 | /* tag= */ "AF", packageNameAttrs, /* closed= */ true) |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 513 | + "</R>" |
| 514 | + "</RL>"; |
Khaled Abdelmohsen | e39fa0f | 2019-11-25 17:24:22 +0000 | [diff] [blame] | 515 | RuleParser xmlParser = new RuleXmlParser(); |
| 516 | assertExpectException( |
| 517 | RuleParseException.class, |
| 518 | /* expectedExceptionMessageRegex */ "Unknown effect: -1", |
Khaled Abdelmohsen | 6ecfebc | 2019-12-03 15:00:49 +0000 | [diff] [blame] | 519 | () -> xmlParser.parse(ruleXmlAtomicFormula.getBytes(StandardCharsets.UTF_8))); |
Khaled Abdelmohsen | e39fa0f | 2019-11-25 17:24:22 +0000 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | @Test |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 523 | public void testXmlString_invalidCompoundFormula_invalidAttribute() throws Exception { |
Khaled Abdelmohsen | e39fa0f | 2019-11-25 17:24:22 +0000 | [diff] [blame] | 524 | Map<String, String> packageNameAttrs = new HashMap<>(); |
| 525 | packageNameAttrs.put("K", String.valueOf(AtomicFormula.PACKAGE_NAME)); |
| 526 | packageNameAttrs.put("V", "com.app.test"); |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 527 | String ruleXmlCompoundFormula = |
| 528 | "<RL>" |
| 529 | + generateTagWithAttribute( |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 530 | /* tag= */ "R", |
| 531 | Collections.singletonMap("E", String.valueOf(Rule.DENY)), |
| 532 | /* closed= */ false) |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 533 | + generateTagWithAttribute( |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 534 | /* tag= */ "OF", |
| 535 | Collections.singletonMap( |
| 536 | "BadConnector", String.valueOf(CompoundFormula.NOT)), |
| 537 | /* closed= */ false) |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 538 | + generateTagWithAttribute( |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 539 | /* tag= */ "AF", packageNameAttrs, /* closed= */ true) |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 540 | + "</OF>" |
| 541 | + "</R>" |
| 542 | + "</RL>"; |
Khaled Abdelmohsen | e39fa0f | 2019-11-25 17:24:22 +0000 | [diff] [blame] | 543 | RuleParser xmlParser = new RuleXmlParser(); |
| 544 | assertExpectException( |
| 545 | RuleParseException.class, |
| 546 | /* expectedExceptionMessageRegex */ "Unknown connector: -1", |
Khaled Abdelmohsen | 6ecfebc | 2019-12-03 15:00:49 +0000 | [diff] [blame] | 547 | () -> xmlParser.parse(ruleXmlCompoundFormula.getBytes(StandardCharsets.UTF_8))); |
Khaled Abdelmohsen | e39fa0f | 2019-11-25 17:24:22 +0000 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | @Test |
Khaled Abdelmohsen | 21a9605 | 2019-11-06 12:23:22 +0000 | [diff] [blame] | 551 | public void testXmlString_invalidAtomicFormula() throws Exception { |
Khaled Abdelmohsen | b05d706 | 2019-11-19 01:22:23 +0000 | [diff] [blame] | 552 | 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 Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 556 | String ruleXmlAtomicFormula = |
| 557 | "<RL>" |
| 558 | + generateTagWithAttribute( |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 559 | /* tag= */ "R", |
| 560 | Collections.singletonMap("E", String.valueOf(Rule.DENY)), |
| 561 | /* closed= */ false) |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 562 | + generateTagWithAttribute( |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 563 | /* tag= */ "AF", packageNameAttrs, /* closed= */ true) |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 564 | + "</R>" |
| 565 | + "</RL>"; |
Khaled Abdelmohsen | 12b110e | 2019-11-05 17:24:48 +0000 | [diff] [blame] | 566 | RuleParser xmlParser = new RuleXmlParser(); |
| 567 | |
Khaled Abdelmohsen | 21a9605 | 2019-11-06 12:23:22 +0000 | [diff] [blame] | 568 | assertExpectException( |
| 569 | RuleParseException.class, |
| 570 | /* expectedExceptionMessageRegex */ "For input string: \"com.app.test\"", |
Khaled Abdelmohsen | 6ecfebc | 2019-12-03 15:00:49 +0000 | [diff] [blame] | 571 | () -> xmlParser.parse(ruleXmlAtomicFormula.getBytes(StandardCharsets.UTF_8))); |
Khaled Abdelmohsen | 216109e | 2019-11-04 17:49:18 +0000 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | @Test |
| 575 | public void testXmlString_withNoRuleList() { |
Khaled Abdelmohsen | b05d706 | 2019-11-19 01:22:23 +0000 | [diff] [blame] | 576 | Map<String, String> atomicFormulaAttrs = new HashMap<>(); |
| 577 | atomicFormulaAttrs.put("K", String.valueOf(AtomicFormula.PACKAGE_NAME)); |
Khaled Abdelmohsen | b05d706 | 2019-11-19 01:22:23 +0000 | [diff] [blame] | 578 | atomicFormulaAttrs.put("V", "com.app.test"); |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 579 | String ruleXmlWithNoRuleList = |
| 580 | generateTagWithAttribute( |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 581 | /* tag= */ "R", |
| 582 | Collections.singletonMap("E", String.valueOf(Rule.DENY)), |
| 583 | /* closed= */ false) |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 584 | + generateTagWithAttribute( |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 585 | /* tag= */ "OF", |
| 586 | Collections.singletonMap("C", String.valueOf(CompoundFormula.NOT)), |
| 587 | /* closed= */ false) |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 588 | + generateTagWithAttribute( |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 589 | /* tag= */ "AF", atomicFormulaAttrs, /* closed= */ true) |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 590 | + "</OF>" |
| 591 | + "</R>"; |
Khaled Abdelmohsen | 216109e | 2019-11-04 17:49:18 +0000 | [diff] [blame] | 592 | RuleParser xmlParser = new RuleXmlParser(); |
| 593 | |
| 594 | assertExpectException( |
Khaled Abdelmohsen | 21a9605 | 2019-11-06 12:23:22 +0000 | [diff] [blame] | 595 | RuleParseException.class, |
Khaled Abdelmohsen | 6d77f94 | 2019-11-12 16:47:01 +0000 | [diff] [blame] | 596 | /* expectedExceptionMessageRegex */ "Rules must start with RuleList <RL> tag", |
Khaled Abdelmohsen | 6ecfebc | 2019-12-03 15:00:49 +0000 | [diff] [blame] | 597 | () -> xmlParser.parse(ruleXmlWithNoRuleList.getBytes(StandardCharsets.UTF_8))); |
Khaled Abdelmohsen | 216109e | 2019-11-04 17:49:18 +0000 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | @Test |
| 601 | public void testXmlStream_withNoRuleList() { |
Khaled Abdelmohsen | b05d706 | 2019-11-19 01:22:23 +0000 | [diff] [blame] | 602 | Map<String, String> atomicFormulaAttrs = new HashMap<>(); |
| 603 | atomicFormulaAttrs.put("K", String.valueOf(AtomicFormula.PACKAGE_NAME)); |
Khaled Abdelmohsen | b05d706 | 2019-11-19 01:22:23 +0000 | [diff] [blame] | 604 | atomicFormulaAttrs.put("V", "com.app.test"); |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 605 | String ruleXmlWithNoRuleList = |
| 606 | generateTagWithAttribute( |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 607 | /* tag= */ "R", |
| 608 | Collections.singletonMap("E", String.valueOf(Rule.DENY)), |
| 609 | /* closed= */ false) |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 610 | + generateTagWithAttribute( |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 611 | /* tag= */ "OF", |
| 612 | Collections.singletonMap("C", String.valueOf(CompoundFormula.NOT)), |
| 613 | /* closed= */ false) |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 614 | + generateTagWithAttribute( |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 615 | /* tag= */ "AF", atomicFormulaAttrs, /* closed= */ true) |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 616 | + "</OF>" |
| 617 | + "</R>"; |
Khaled Abdelmohsen | 216109e | 2019-11-04 17:49:18 +0000 | [diff] [blame] | 618 | RuleParser xmlParser = new RuleXmlParser(); |
| 619 | |
| 620 | assertExpectException( |
Khaled Abdelmohsen | 21a9605 | 2019-11-06 12:23:22 +0000 | [diff] [blame] | 621 | RuleParseException.class, |
Khaled Abdelmohsen | 6d77f94 | 2019-11-12 16:47:01 +0000 | [diff] [blame] | 622 | /* expectedExceptionMessageRegex */ "Rules must start with RuleList <RL> tag", |
Song Pan | 8a0b6fc | 2020-01-14 16:33:50 +0000 | [diff] [blame] | 623 | () -> xmlParser.parse(ruleXmlWithNoRuleList.getBytes())); |
Khaled Abdelmohsen | 216109e | 2019-11-04 17:49:18 +0000 | [diff] [blame] | 624 | } |
Khaled Abdelmohsen | b05d706 | 2019-11-19 01:22:23 +0000 | [diff] [blame] | 625 | |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 626 | private String generateTagWithAttribute( |
| 627 | String tag, Map<String, String> attributeValues, boolean closed) { |
Khaled Abdelmohsen | b05d706 | 2019-11-19 01:22:23 +0000 | [diff] [blame] | 628 | 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 Abdelmohsen | 216109e | 2019-11-04 17:49:18 +0000 | [diff] [blame] | 640 | } |