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