Bookatz | 04d7ae5 | 2019-08-05 14:07:12 -0700 | [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 | |
Winson | d9d1736 | 2019-10-02 12:41:29 -0700 | [diff] [blame] | 17 | package com.android.server.systemconfig; |
Bookatz | 04d7ae5 | 2019-08-05 14:07:12 -0700 | [diff] [blame] | 18 | |
| 19 | import static org.junit.Assert.assertEquals; |
| 20 | |
| 21 | import android.util.ArrayMap; |
| 22 | import android.util.ArraySet; |
| 23 | import android.util.Log; |
| 24 | |
| 25 | import androidx.test.filters.SmallTest; |
| 26 | import androidx.test.runner.AndroidJUnit4; |
| 27 | |
Winson | d9d1736 | 2019-10-02 12:41:29 -0700 | [diff] [blame] | 28 | import com.android.server.SystemConfig; |
| 29 | |
Bookatz | 04d7ae5 | 2019-08-05 14:07:12 -0700 | [diff] [blame] | 30 | import org.junit.Before; |
| 31 | import org.junit.Rule; |
| 32 | import org.junit.Test; |
| 33 | import org.junit.rules.TemporaryFolder; |
| 34 | import org.junit.runner.RunWith; |
| 35 | |
| 36 | import java.io.BufferedWriter; |
| 37 | import java.io.File; |
| 38 | import java.io.FileWriter; |
| 39 | import java.io.IOException; |
| 40 | import java.util.Arrays; |
| 41 | import java.util.Map; |
| 42 | import java.util.Scanner; |
| 43 | import java.util.Set; |
| 44 | |
| 45 | /** |
| 46 | * Tests for {@link SystemConfig}. |
| 47 | * |
| 48 | * Build/Install/Run: |
| 49 | * atest FrameworksServicesTests:SystemConfigTest |
| 50 | */ |
| 51 | @RunWith(AndroidJUnit4.class) |
| 52 | @SmallTest |
| 53 | public class SystemConfigTest { |
| 54 | private static final String LOG_TAG = "SystemConfigTest"; |
| 55 | |
| 56 | private SystemConfig mSysConfig; |
| 57 | |
| 58 | @Rule public TemporaryFolder mTemporaryFolder = new TemporaryFolder(); |
| 59 | |
| 60 | @Before |
| 61 | public void setUp() throws Exception { |
| 62 | mSysConfig = new SystemConfigTestClass(); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Subclass of SystemConfig without running the constructor. |
| 67 | */ |
| 68 | private class SystemConfigTestClass extends SystemConfig { |
| 69 | SystemConfigTestClass() { |
| 70 | super(false); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Tests that readPermissions works correctly for the tag: install-in-user-type |
| 76 | */ |
| 77 | @Test |
| 78 | public void testInstallInUserType() throws Exception { |
| 79 | final String contents1 = |
| 80 | "<permissions>\n" |
| 81 | + " <install-in-user-type package=\"com.android.package1\">\n" |
| 82 | + " <install-in user-type=\"FULL\" />\n" |
| 83 | + " <install-in user-type=\"PROFILE\" />\n" |
| 84 | + " </install-in-user-type>\n" |
| 85 | + " <install-in-user-type package=\"com.android.package2\">\n" |
| 86 | + " <install-in user-type=\"FULL\" />\n" |
| 87 | + " <install-in user-type=\"PROFILE\" />\n" |
| 88 | + " <do-not-install-in user-type=\"GUEST\" />\n" |
| 89 | + " </install-in-user-type>\n" |
| 90 | + "</permissions>"; |
| 91 | |
| 92 | final String contents2 = |
| 93 | "<permissions>\n" |
| 94 | + " <install-in-user-type package=\"com.android.package2\">\n" |
| 95 | + " <install-in user-type=\"SYSTEM\" />\n" |
| 96 | + " <do-not-install-in user-type=\"PROFILE\" />\n" |
| 97 | + " </install-in-user-type>\n" |
| 98 | + "</permissions>"; |
| 99 | |
| 100 | final String contents3 = |
| 101 | "<permissions>\n" |
| 102 | + " <install-in-user-type package=\"com.android.package2\">\n" |
| 103 | + " <install-in invalid-attribute=\"ADMIN\" />\n" // Ignore invalid attribute |
| 104 | + " </install-in-user-type>\n" |
| 105 | + " <install-in-user-type package=\"com.android.package2\">\n" |
| 106 | + " <install-in user-type=\"RESTRICTED\" />\n" // Valid |
| 107 | + " </install-in-user-type>\n" |
| 108 | + " <install-in-user-type>\n" // Ignored since missing package name |
| 109 | + " <install-in user-type=\"ADMIN\" />\n" |
| 110 | + " </install-in-user-type>\n" |
| 111 | + "</permissions>"; |
| 112 | |
| 113 | Map<String, Set<String>> expectedWhite = new ArrayMap<>(); |
| 114 | expectedWhite.put("com.android.package1", |
| 115 | new ArraySet<>(Arrays.asList("FULL", "PROFILE"))); |
| 116 | expectedWhite.put("com.android.package2", |
| 117 | new ArraySet<>(Arrays.asList("FULL", "PROFILE", "RESTRICTED", "SYSTEM"))); |
| 118 | |
| 119 | Map<String, Set<String>> expectedBlack = new ArrayMap<>(); |
| 120 | expectedBlack.put("com.android.package2", |
| 121 | new ArraySet<>(Arrays.asList("GUEST", "PROFILE"))); |
| 122 | |
| 123 | final File folder1 = createTempSubfolder("folder1"); |
| 124 | createTempFile(folder1, "permFile1.xml", contents1); |
| 125 | |
| 126 | final File folder2 = createTempSubfolder("folder2"); |
| 127 | createTempFile(folder2, "permFile2.xml", contents2); |
| 128 | |
| 129 | // Also, make a third file, but with the name folder1/permFile2.xml, to prove no conflicts. |
| 130 | createTempFile(folder1, "permFile2.xml", contents3); |
| 131 | |
| 132 | mSysConfig.readPermissions(folder1, /* No permission needed anyway */ 0); |
| 133 | mSysConfig.readPermissions(folder2, /* No permission needed anyway */ 0); |
| 134 | |
| 135 | Map<String, Set<String>> actualWhite = mSysConfig.getAndClearPackageToUserTypeWhitelist(); |
| 136 | Map<String, Set<String>> actualBlack = mSysConfig.getAndClearPackageToUserTypeBlacklist(); |
| 137 | |
| 138 | assertEquals("Whitelist was not cleared", 0, |
| 139 | mSysConfig.getAndClearPackageToUserTypeWhitelist().size()); |
| 140 | assertEquals("Blacklist was not cleared", 0, |
| 141 | mSysConfig.getAndClearPackageToUserTypeBlacklist().size()); |
| 142 | |
| 143 | assertEquals("Incorrect whitelist.", expectedWhite, actualWhite); |
| 144 | assertEquals("Incorrect blacklist", expectedBlack, actualBlack); |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Creates folderName/fileName in the mTemporaryFolder and fills it with the contents. |
| 149 | * @param folderName subdirectory of mTemporaryFolder to put the file, creating if needed |
| 150 | * @return the folder |
| 151 | */ |
| 152 | private File createTempSubfolder(String folderName) |
| 153 | throws IOException { |
| 154 | File folder = new File(mTemporaryFolder.getRoot(), folderName); |
| 155 | folder.mkdir(); |
| 156 | return folder; |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Creates folderName/fileName in the mTemporaryFolder and fills it with the contents. |
| 161 | * @param folder pre-existing subdirectory of mTemporaryFolder to put the file |
| 162 | * @param fileName name of the file (e.g. filename.xml) to create |
| 163 | * @param contents contents to write to the file |
| 164 | * @return the folder containing the newly created file (not the file itself!) |
| 165 | */ |
| 166 | private File createTempFile(File folder, String fileName, String contents) |
| 167 | throws IOException { |
| 168 | File file = new File(folder, fileName); |
| 169 | BufferedWriter bw = new BufferedWriter(new FileWriter(file)); |
| 170 | bw.write(contents); |
| 171 | bw.close(); |
| 172 | |
| 173 | // Print to logcat for test debugging. |
| 174 | Log.d(LOG_TAG, "Contents of file " + file.getAbsolutePath()); |
| 175 | Scanner input = new Scanner(file); |
| 176 | while (input.hasNextLine()) { |
| 177 | Log.d(LOG_TAG, input.nextLine()); |
| 178 | } |
| 179 | |
| 180 | return folder; |
| 181 | } |
| 182 | } |