Song Pan | c56a78d | 2020-01-02 16:06:11 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.server.integrity; |
| 18 | |
Omer Nebil Yaveroglu | 2948cba | 2020-01-10 17:51:35 +0000 | [diff] [blame] | 19 | import static com.google.common.truth.Truth.assertThat; |
Song Pan | c56a78d | 2020-01-02 16:06:11 +0000 | [diff] [blame] | 20 | |
| 21 | import android.content.integrity.AppInstallMetadata; |
| 22 | import android.content.integrity.AtomicFormula; |
| 23 | import android.content.integrity.AtomicFormula.IntAtomicFormula; |
| 24 | import android.content.integrity.AtomicFormula.StringAtomicFormula; |
| 25 | import android.content.integrity.CompoundFormula; |
| 26 | import android.content.integrity.Rule; |
| 27 | import android.util.Slog; |
| 28 | |
Omer Nebil Yaveroglu | 2948cba | 2020-01-10 17:51:35 +0000 | [diff] [blame] | 29 | import com.android.server.integrity.parser.RuleBinaryParser; |
| 30 | import com.android.server.integrity.serializer.RuleBinarySerializer; |
Song Pan | c56a78d | 2020-01-02 16:06:11 +0000 | [diff] [blame] | 31 | |
| 32 | import org.junit.After; |
| 33 | import org.junit.Before; |
| 34 | import org.junit.Test; |
| 35 | import org.junit.runner.RunWith; |
Omer Nebil Yaveroglu | 2948cba | 2020-01-10 17:51:35 +0000 | [diff] [blame] | 36 | import org.junit.runners.JUnit4; |
Song Pan | c56a78d | 2020-01-02 16:06:11 +0000 | [diff] [blame] | 37 | |
| 38 | import java.io.File; |
| 39 | import java.nio.file.Files; |
| 40 | import java.nio.file.Path; |
Omer Nebil Yaveroglu | 2948cba | 2020-01-10 17:51:35 +0000 | [diff] [blame] | 41 | import java.util.ArrayList; |
Song Pan | c56a78d | 2020-01-02 16:06:11 +0000 | [diff] [blame] | 42 | import java.util.Arrays; |
| 43 | import java.util.Collections; |
| 44 | import java.util.Comparator; |
| 45 | import java.util.List; |
| 46 | |
| 47 | /** Unit test for {@link IntegrityFileManager} */ |
Omer Nebil Yaveroglu | 2948cba | 2020-01-10 17:51:35 +0000 | [diff] [blame] | 48 | @RunWith(JUnit4.class) |
Song Pan | c56a78d | 2020-01-02 16:06:11 +0000 | [diff] [blame] | 49 | public class IntegrityFileManagerTest { |
| 50 | private static final String TAG = "IntegrityFileManagerTest"; |
| 51 | |
| 52 | private static final String VERSION = "version"; |
| 53 | private static final String RULE_PROVIDER = "rule_provider"; |
| 54 | |
| 55 | private File mTmpDir; |
| 56 | |
| 57 | // under test |
| 58 | private IntegrityFileManager mIntegrityFileManager; |
| 59 | |
| 60 | @Before |
| 61 | public void setUp() throws Exception { |
| 62 | mTmpDir = Files.createTempDirectory("IntegrityFileManagerTest").toFile(); |
| 63 | Slog.i(TAG, "Using temp directory " + mTmpDir); |
| 64 | |
| 65 | // Use Xml Parser/Serializer to help with debugging since we can just print the file. |
| 66 | mIntegrityFileManager = |
| 67 | new IntegrityFileManager( |
Omer Nebil Yaveroglu | 2948cba | 2020-01-10 17:51:35 +0000 | [diff] [blame] | 68 | new RuleBinaryParser(), new RuleBinarySerializer(), mTmpDir); |
Song Pan | c56a78d | 2020-01-02 16:06:11 +0000 | [diff] [blame] | 69 | Files.walk(mTmpDir.toPath()) |
| 70 | .forEach( |
| 71 | path -> { |
| 72 | Slog.i(TAG, "before " + path); |
| 73 | }); |
| 74 | } |
| 75 | |
| 76 | @After |
| 77 | public void tearDown() throws Exception { |
| 78 | Files.walk(mTmpDir.toPath()) |
| 79 | .forEach( |
| 80 | path -> { |
| 81 | Slog.i(TAG, "after " + path); |
| 82 | }); |
| 83 | // Sorting paths in reverse order guarantees that we delete inside files before deleting |
| 84 | // directory. |
| 85 | Files.walk(mTmpDir.toPath()) |
| 86 | .sorted(Comparator.reverseOrder()) |
| 87 | .map(Path::toFile) |
| 88 | .forEach(File::delete); |
| 89 | } |
| 90 | |
| 91 | @Test |
| 92 | public void testGetMetadata() throws Exception { |
Omer Nebil Yaveroglu | 2948cba | 2020-01-10 17:51:35 +0000 | [diff] [blame] | 93 | assertThat(mIntegrityFileManager.readMetadata()).isNull(); |
Song Pan | c56a78d | 2020-01-02 16:06:11 +0000 | [diff] [blame] | 94 | mIntegrityFileManager.writeRules(VERSION, RULE_PROVIDER, Collections.EMPTY_LIST); |
| 95 | |
Omer Nebil Yaveroglu | 2948cba | 2020-01-10 17:51:35 +0000 | [diff] [blame] | 96 | assertThat(mIntegrityFileManager.readMetadata()).isNotNull(); |
| 97 | assertThat(mIntegrityFileManager.readMetadata().getVersion()).isEqualTo(VERSION); |
| 98 | assertThat(mIntegrityFileManager.readMetadata().getRuleProvider()).isEqualTo(RULE_PROVIDER); |
| 99 | } |
| 100 | |
| 101 | @Test |
| 102 | public void testIsInitialized() throws Exception { |
| 103 | assertThat(mIntegrityFileManager.initialized()).isFalse(); |
| 104 | mIntegrityFileManager.writeRules(VERSION, RULE_PROVIDER, Collections.EMPTY_LIST); |
| 105 | assertThat(mIntegrityFileManager.initialized()).isTrue(); |
Song Pan | c56a78d | 2020-01-02 16:06:11 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | @Test |
| 109 | public void testGetRules() throws Exception { |
| 110 | String packageName = "package"; |
| 111 | String packageCert = "cert"; |
| 112 | int version = 123; |
Omer Nebil Yaveroglu | 2948cba | 2020-01-10 17:51:35 +0000 | [diff] [blame] | 113 | Rule packageNameRule = getPackageNameIndexedRule(packageName); |
| 114 | Rule packageCertRule = getAppCertificateIndexedRule(packageCert); |
Song Pan | c56a78d | 2020-01-02 16:06:11 +0000 | [diff] [blame] | 115 | Rule versionCodeRule = |
| 116 | new Rule( |
| 117 | new IntAtomicFormula(AtomicFormula.VERSION_CODE, AtomicFormula.LE, version), |
| 118 | Rule.DENY); |
| 119 | Rule randomRule = |
| 120 | new Rule( |
| 121 | new CompoundFormula( |
| 122 | CompoundFormula.OR, |
| 123 | Arrays.asList( |
| 124 | new StringAtomicFormula( |
| 125 | AtomicFormula.PACKAGE_NAME, |
| 126 | "abc", |
| 127 | /* isHashedValue= */ false), |
| 128 | new IntAtomicFormula( |
| 129 | AtomicFormula.VERSION_CODE, |
| 130 | AtomicFormula.LE, |
| 131 | version))), |
| 132 | Rule.DENY); |
Omer Nebil Yaveroglu | 2948cba | 2020-01-10 17:51:35 +0000 | [diff] [blame] | 133 | |
Song Pan | c56a78d | 2020-01-02 16:06:11 +0000 | [diff] [blame] | 134 | List<Rule> rules = |
| 135 | Arrays.asList(packageNameRule, packageCertRule, versionCodeRule, randomRule); |
| 136 | mIntegrityFileManager.writeRules(VERSION, RULE_PROVIDER, rules); |
| 137 | |
Song Pan | 95de69d | 2020-01-10 12:29:35 +0000 | [diff] [blame^] | 138 | AppInstallMetadata appInstallMetadata = |
| 139 | new AppInstallMetadata.Builder() |
| 140 | .setPackageName(packageName) |
| 141 | .setAppCertificate(packageCert) |
| 142 | .setVersionCode(version) |
| 143 | .setInstallerName("abc") |
| 144 | .setInstallerCertificate("abc") |
| 145 | .setIsPreInstalled(true) |
| 146 | .build(); |
Song Pan | c56a78d | 2020-01-02 16:06:11 +0000 | [diff] [blame] | 147 | List<Rule> rulesFetched = mIntegrityFileManager.readRules(appInstallMetadata); |
| 148 | |
Omer Nebil Yaveroglu | 2948cba | 2020-01-10 17:51:35 +0000 | [diff] [blame] | 149 | assertThat(rulesFetched) |
| 150 | .containsExactly(packageNameRule, packageCertRule, versionCodeRule, randomRule); |
Song Pan | c56a78d | 2020-01-02 16:06:11 +0000 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | @Test |
Omer Nebil Yaveroglu | 2948cba | 2020-01-10 17:51:35 +0000 | [diff] [blame] | 154 | public void testGetRules_indexedForManyRules() throws Exception { |
| 155 | String packageName = "package"; |
| 156 | String installerName = "installer"; |
| 157 | String appCertificate = "cert"; |
| 158 | |
| 159 | // Create a rule set with 2500 package name indexed, 2500 app certificate indexed and |
| 160 | // 500 unindexed rules. |
| 161 | List<Rule> rules = new ArrayList<>(); |
| 162 | |
| 163 | for (int i = 0; i < 2500; i++) { |
| 164 | rules.add(getPackageNameIndexedRule(String.format("%s%04d", packageName, i))); |
| 165 | rules.add(getAppCertificateIndexedRule(String.format("%s%04d", appCertificate, i))); |
| 166 | } |
| 167 | |
| 168 | for (int i = 0; i < 70; i++) { |
| 169 | rules.add(getInstallerCertificateRule(String.format("%s%04d", installerName, i))); |
| 170 | } |
| 171 | |
| 172 | // Write the rules and get them indexed. |
| 173 | mIntegrityFileManager.writeRules(VERSION, RULE_PROVIDER, rules); |
| 174 | |
| 175 | // Read the rules for a specific rule. |
| 176 | String installedPackageName = String.format("%s%04d", packageName, 264); |
| 177 | String installedAppCertificate = String.format("%s%04d", appCertificate, 1264); |
Song Pan | 95de69d | 2020-01-10 12:29:35 +0000 | [diff] [blame^] | 178 | AppInstallMetadata appInstallMetadata = |
| 179 | new AppInstallMetadata.Builder() |
| 180 | .setPackageName(installedPackageName) |
| 181 | .setAppCertificate(installedAppCertificate) |
| 182 | .setVersionCode(250) |
| 183 | .setInstallerName("abc") |
| 184 | .setInstallerCertificate("abc") |
| 185 | .setIsPreInstalled(true) |
| 186 | .build(); |
Omer Nebil Yaveroglu | 2948cba | 2020-01-10 17:51:35 +0000 | [diff] [blame] | 187 | List<Rule> rulesFetched = mIntegrityFileManager.readRules(appInstallMetadata); |
| 188 | |
| 189 | // Verify that we do not load all the rules and we have the necessary rules to evaluate. |
Omer Nebil Yaveroglu | 8d46f34 | 2020-01-13 15:28:11 +0000 | [diff] [blame] | 190 | assertThat(rulesFetched.size()).isEqualTo(270); |
Omer Nebil Yaveroglu | 2948cba | 2020-01-10 17:51:35 +0000 | [diff] [blame] | 191 | assertThat(rulesFetched) |
| 192 | .containsAllOf( |
| 193 | getPackageNameIndexedRule(installedPackageName), |
| 194 | getAppCertificateIndexedRule(installedAppCertificate)); |
| 195 | } |
| 196 | |
| 197 | private Rule getPackageNameIndexedRule(String packageName) { |
| 198 | return new Rule( |
| 199 | new StringAtomicFormula( |
Song Pan | 95de69d | 2020-01-10 12:29:35 +0000 | [diff] [blame^] | 200 | AtomicFormula.PACKAGE_NAME, packageName, /* isHashedValue= */ false), |
Omer Nebil Yaveroglu | 2948cba | 2020-01-10 17:51:35 +0000 | [diff] [blame] | 201 | Rule.DENY); |
| 202 | } |
| 203 | |
| 204 | private Rule getAppCertificateIndexedRule(String appCertificate) { |
| 205 | return new Rule( |
| 206 | new StringAtomicFormula( |
Song Pan | 95de69d | 2020-01-10 12:29:35 +0000 | [diff] [blame^] | 207 | AtomicFormula.APP_CERTIFICATE, appCertificate, /* isHashedValue= */ false), |
Omer Nebil Yaveroglu | 2948cba | 2020-01-10 17:51:35 +0000 | [diff] [blame] | 208 | Rule.DENY); |
| 209 | } |
| 210 | |
| 211 | private Rule getInstallerCertificateRule(String installerCert) { |
| 212 | return new Rule( |
| 213 | new StringAtomicFormula( |
Song Pan | 95de69d | 2020-01-10 12:29:35 +0000 | [diff] [blame^] | 214 | AtomicFormula.INSTALLER_NAME, installerCert, /* isHashedValue= */ false), |
Omer Nebil Yaveroglu | 2948cba | 2020-01-10 17:51:35 +0000 | [diff] [blame] | 215 | Rule.DENY); |
Song Pan | c56a78d | 2020-01-02 16:06:11 +0000 | [diff] [blame] | 216 | } |
Song Pan | 95de69d | 2020-01-10 12:29:35 +0000 | [diff] [blame^] | 217 | |
| 218 | @Test |
| 219 | public void testStagingDirectoryCleared() throws Exception { |
| 220 | // We must push rules two times to ensure that staging directory is empty because we cleared |
| 221 | // it, rather than because original rules directory is empty. |
| 222 | mIntegrityFileManager.writeRules(VERSION, RULE_PROVIDER, Collections.EMPTY_LIST); |
| 223 | mIntegrityFileManager.writeRules(VERSION, RULE_PROVIDER, Collections.EMPTY_LIST); |
| 224 | |
| 225 | assertStagingDirectoryCleared(); |
| 226 | } |
| 227 | |
| 228 | private void assertStagingDirectoryCleared() { |
| 229 | File stagingDir = new File(mTmpDir, "integrity_staging"); |
| 230 | assertThat(stagingDir.exists()).isTrue(); |
| 231 | assertThat(stagingDir.isDirectory()).isTrue(); |
| 232 | assertThat(stagingDir.listFiles()).isEmpty(); |
| 233 | } |
Song Pan | c56a78d | 2020-01-02 16:06:11 +0000 | [diff] [blame] | 234 | } |