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 com.android.internal.content.PackageHelper;
25import com.android.internal.os.AtomicFile;
26
27import java.io.File;
28import java.io.FileOutputStream;
29import java.io.IOException;
30import java.util.HashSet;
31
32import android.os.Debug;
33import android.os.Environment;
34import android.os.IBinder;
35import android.os.RemoteException;
36import android.os.ServiceManager;
37import android.os.storage.IMountService;
38import android.test.AndroidTestCase;
39import android.util.Log;
40
41public 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();
141        Settings settings = new Settings(getContext(), getContext().getFilesDir());
142        assertEquals(true, settings.readLPw(null));
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();
159        Settings settings = new Settings(getContext(), getContext().getFilesDir());
160        assertEquals(true, settings.readLPw(null));
161
162        // Create Settings again to make it read from the new files
163        settings = new Settings(getContext(), getContext().getFilesDir());
164        assertEquals(true, settings.readLPw(null));
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();
174        Settings settings = new Settings(getContext(), getContext().getFilesDir());
175        assertEquals(true, settings.readLPw(null));
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}
206