Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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.pm; |
| 18 | |
| 19 | import static android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_DEFAULT; |
| 20 | import static android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_DISABLED; |
| 21 | import static android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_DISABLED_USER; |
| 22 | import static android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_ENABLED; |
| 23 | |
| 24 | import com.android.internal.content.PackageHelper; |
| 25 | import com.android.internal.os.AtomicFile; |
| 26 | |
| 27 | import java.io.File; |
| 28 | import java.io.FileOutputStream; |
| 29 | import java.io.IOException; |
| 30 | import java.util.HashSet; |
| 31 | |
| 32 | import android.os.Debug; |
| 33 | import android.os.Environment; |
| 34 | import android.os.IBinder; |
| 35 | import android.os.RemoteException; |
| 36 | import android.os.ServiceManager; |
| 37 | import android.os.storage.IMountService; |
| 38 | import android.test.AndroidTestCase; |
| 39 | import android.util.Log; |
| 40 | |
| 41 | public class PackageManagerSettingsTests extends AndroidTestCase { |
| 42 | |
| 43 | private static final String PACKAGE_NAME_2 = "com.google.app2"; |
| 44 | private static final String PACKAGE_NAME_3 = "com.android.app3"; |
| 45 | private static final String PACKAGE_NAME_1 = "com.google.app1"; |
| 46 | private static final boolean localLOGV = true; |
| 47 | public static final String TAG = "PackageManagerSettingsTests"; |
| 48 | protected final String PREFIX = "android.content.pm"; |
| 49 | |
| 50 | private void writeFile(File file, byte[] data) { |
| 51 | file.mkdirs(); |
| 52 | try { |
| 53 | AtomicFile aFile = new AtomicFile(file); |
| 54 | FileOutputStream fos = aFile.startWrite(); |
| 55 | fos.write(data); |
| 56 | aFile.finishWrite(fos); |
| 57 | } catch (IOException ioe) { |
| 58 | Log.e(TAG, "Cannot write file " + file.getPath()); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | private void writePackagesXml() { |
| 63 | writeFile(new File(getContext().getFilesDir(), "system/packages.xml"), |
| 64 | ("<?xml version='1.0' encoding='utf-8' standalone='yes' ?>" |
| 65 | + "<packages>" |
| 66 | + "<last-platform-version internal=\"15\" external=\"0\" />" |
| 67 | + "<permission-trees>" |
| 68 | + "<item name=\"com.google.android.permtree\" package=\"com.google.android.permpackage\" />" |
| 69 | + "</permission-trees>" |
| 70 | + "<permissions>" |
| 71 | + "<item name=\"android.permission.WRITE_CALL_LOG\" package=\"android\" protection=\"1\" />" |
| 72 | + "<item name=\"android.permission.ASEC_ACCESS\" package=\"android\" protection=\"2\" />" |
| 73 | + "<item name=\"android.permission.ACCESS_WIMAX_STATE\" package=\"android\" />" |
| 74 | + "<item name=\"android.permission.REBOOT\" package=\"android\" protection=\"18\" />" |
| 75 | + "</permissions>" |
| 76 | + "<package name=\"com.google.app1\" codePath=\"/system/app/app1.apk\" nativeLibraryPath=\"/data/data/com.google.app1/lib\" flags=\"1\" ft=\"1360e2caa70\" it=\"135f2f80d08\" ut=\"1360e2caa70\" version=\"1109\" sharedUserId=\"11000\">" |
| 77 | + "<sigs count=\"1\">" |
| 78 | + "<cert index=\"0\" key=\"308886\" />" |
| 79 | + "</sigs>" |
| 80 | + "</package>" |
| 81 | + "<package name=\"com.google.app2\" codePath=\"/system/app/app2.apk\" nativeLibraryPath=\"/data/data/com.google.app2/lib\" flags=\"1\" ft=\"1360e578718\" it=\"135f2f80d08\" ut=\"1360e578718\" version=\"15\" enabled=\"3\" userId=\"11001\">" |
| 82 | + "<sigs count=\"1\">" |
| 83 | + "<cert index=\"0\" />" |
| 84 | + "</sigs>" |
| 85 | + "</package>" |
| 86 | + "<package name=\"com.android.app3\" codePath=\"/system/app/app3.apk\" nativeLibraryPath=\"/data/data/com.android.app3/lib\" flags=\"1\" ft=\"1360e577b60\" it=\"135f2f80d08\" ut=\"1360e577b60\" version=\"15\" userId=\"11030\">" |
| 87 | + "<sigs count=\"1\">" |
| 88 | + "<cert index=\"1\" key=\"308366\" />" |
| 89 | + "</sigs>" |
| 90 | + "</package>" |
| 91 | + "<shared-user name=\"com.android.shared1\" userId=\"11000\">" |
| 92 | + "<sigs count=\"1\">" |
| 93 | + "<cert index=\"1\" />" |
| 94 | + "</sigs>" |
| 95 | + "<perms>" |
| 96 | + "<item name=\"android.permission.REBOOT\" />" |
| 97 | + "</perms>" |
| 98 | + "</shared-user>" |
| 99 | + "</packages>").getBytes()); |
| 100 | } |
| 101 | |
| 102 | private void writeStoppedPackagesXml() { |
| 103 | writeFile(new File(getContext().getFilesDir(), "system/packages-stopped.xml"), |
| 104 | ( "<?xml version='1.0' encoding='utf-8' standalone='yes' ?>" |
| 105 | + "<stopped-packages>" |
| 106 | + "<pkg name=\"com.google.app1\" nl=\"1\" />" |
| 107 | + "<pkg name=\"com.android.app3\" nl=\"1\" />" |
| 108 | + "</stopped-packages>") |
| 109 | .getBytes()); |
| 110 | } |
| 111 | |
| 112 | private void writePackagesList() { |
| 113 | writeFile(new File(getContext().getFilesDir(), "system/packages.list"), |
| 114 | ( "com.google.app1 11000 0 /data/data/com.google.app1" |
| 115 | + "com.google.app2 11001 0 /data/data/com.google.app2" |
| 116 | + "com.android.app3 11030 0 /data/data/com.android.app3") |
| 117 | .getBytes()); |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | protected void setUp() throws Exception { |
| 122 | super.setUp(); |
| 123 | } |
| 124 | |
| 125 | private void writeOldFiles() { |
| 126 | writePackagesXml(); |
| 127 | writeStoppedPackagesXml(); |
| 128 | writePackagesList(); |
| 129 | } |
| 130 | |
| 131 | @Override |
| 132 | protected void tearDown() throws Exception { |
| 133 | super.tearDown(); |
| 134 | } |
| 135 | |
| 136 | public void testSettingsReadOld() { |
| 137 | // Debug.waitForDebugger(); |
| 138 | |
| 139 | // Write the package files and make sure they're parsed properly the first time |
| 140 | writeOldFiles(); |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 141 | Settings settings = new Settings(getContext(), getContext().getFilesDir()); |
Dianne Hackborn | 2c41c4a | 2012-11-28 18:46:45 -0800 | [diff] [blame] | 142 | assertEquals(true, settings.readLPw(null, 0, false)); |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 143 | assertNotNull(settings.peekPackageLPr(PACKAGE_NAME_3)); |
| 144 | assertNotNull(settings.peekPackageLPr(PACKAGE_NAME_1)); |
| 145 | |
| 146 | PackageSetting ps = settings.peekPackageLPr(PACKAGE_NAME_1); |
| 147 | assertEquals(COMPONENT_ENABLED_STATE_DEFAULT, ps.getEnabled(0)); |
| 148 | assertEquals(true, ps.getNotLaunched(0)); |
| 149 | |
| 150 | ps = settings.peekPackageLPr(PACKAGE_NAME_2); |
| 151 | assertEquals(false, ps.getStopped(0)); |
| 152 | assertEquals(COMPONENT_ENABLED_STATE_DISABLED_USER, ps.getEnabled(0)); |
| 153 | assertEquals(COMPONENT_ENABLED_STATE_DEFAULT, ps.getEnabled(1)); |
| 154 | } |
| 155 | |
| 156 | public void testNewPackageRestrictionsFile() { |
| 157 | // Write the package files and make sure they're parsed properly the first time |
| 158 | writeOldFiles(); |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 159 | Settings settings = new Settings(getContext(), getContext().getFilesDir()); |
Dianne Hackborn | 2c41c4a | 2012-11-28 18:46:45 -0800 | [diff] [blame] | 160 | assertEquals(true, settings.readLPw(null, 0, false)); |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 161 | |
| 162 | // Create Settings again to make it read from the new files |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 163 | settings = new Settings(getContext(), getContext().getFilesDir()); |
Dianne Hackborn | 2c41c4a | 2012-11-28 18:46:45 -0800 | [diff] [blame] | 164 | assertEquals(true, settings.readLPw(null, 0, false)); |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 165 | |
| 166 | PackageSetting ps = settings.peekPackageLPr(PACKAGE_NAME_2); |
| 167 | assertEquals(COMPONENT_ENABLED_STATE_DISABLED_USER, ps.getEnabled(0)); |
| 168 | assertEquals(COMPONENT_ENABLED_STATE_DEFAULT, ps.getEnabled(1)); |
| 169 | } |
| 170 | |
| 171 | public void testEnableDisable() { |
| 172 | // Write the package files and make sure they're parsed properly the first time |
| 173 | writeOldFiles(); |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 174 | Settings settings = new Settings(getContext(), getContext().getFilesDir()); |
Dianne Hackborn | 2c41c4a | 2012-11-28 18:46:45 -0800 | [diff] [blame] | 175 | assertEquals(true, settings.readLPw(null, 0, false)); |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 176 | |
| 177 | // Enable/Disable a package |
| 178 | PackageSetting ps = settings.peekPackageLPr(PACKAGE_NAME_1); |
| 179 | ps.setEnabled(COMPONENT_ENABLED_STATE_DISABLED, 0); |
| 180 | ps.setEnabled(COMPONENT_ENABLED_STATE_ENABLED, 1); |
| 181 | assertEquals(COMPONENT_ENABLED_STATE_DISABLED, ps.getEnabled(0)); |
| 182 | assertEquals(COMPONENT_ENABLED_STATE_ENABLED, ps.getEnabled(1)); |
| 183 | |
| 184 | // Enable/Disable a component |
| 185 | HashSet<String> components = new HashSet<String>(); |
| 186 | String component1 = PACKAGE_NAME_1 + "/.Component1"; |
| 187 | components.add(component1); |
| 188 | ps.setDisabledComponents(components, 0); |
| 189 | HashSet<String> componentsDisabled = ps.getDisabledComponents(0); |
| 190 | assertEquals(1, componentsDisabled.size()); |
| 191 | assertEquals(component1, componentsDisabled.toArray()[0]); |
| 192 | boolean hasEnabled = |
| 193 | ps.getEnabledComponents(0) != null && ps.getEnabledComponents(1).size() > 0; |
| 194 | assertEquals(false, hasEnabled); |
| 195 | |
| 196 | // User 1 should not have any disabled components |
| 197 | boolean hasDisabled = |
| 198 | ps.getDisabledComponents(1) != null && ps.getDisabledComponents(1).size() > 0; |
| 199 | assertEquals(false, hasDisabled); |
| 200 | ps.setEnabledComponents(components, 1); |
| 201 | assertEquals(1, ps.getEnabledComponents(1).size()); |
| 202 | hasEnabled = ps.getEnabledComponents(0) != null && ps.getEnabledComponents(0).size() > 0; |
| 203 | assertEquals(false, hasEnabled); |
| 204 | } |
| 205 | } |