1/*
2 * Copyright (C) 2011 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 android.content.pm;
18
19import static android.net.TrafficStats.MB_IN_BYTES;
20
21import android.os.IBinder;
22import android.os.RemoteException;
23import android.os.ServiceManager;
24import android.os.storage.IMountService;
25import android.test.AndroidTestCase;
26import android.util.Log;
27
28import com.android.internal.content.PackageHelper;
29
30public class PackageHelperTests extends AndroidTestCase {
31    private static final boolean localLOGV = true;
32    public static final String TAG = "PackageHelperTests";
33    protected final String PREFIX = "android.content.pm";
34    private IMountService mMs;
35    private String fullId;
36    private String fullId2;
37
38    private IMountService getMs() {
39        IBinder service = ServiceManager.getService("mount");
40        if (service != null) {
41            return IMountService.Stub.asInterface(service);
42        } else {
43            Log.e(TAG, "Can't get mount service");
44        }
45        return null;
46    }
47
48    private void cleanupContainers() throws RemoteException {
49        Log.d(TAG,"cleanUp");
50        IMountService ms = getMs();
51        String[] containers = ms.getSecureContainerList();
52        for (int i = 0; i < containers.length; i++) {
53            if (containers[i].startsWith(PREFIX)) {
54                Log.d(TAG,"cleaing up "+containers[i]);
55                ms.destroySecureContainer(containers[i], true);
56            }
57        }
58    }
59
60    void failStr(String errMsg) {
61        Log.w(TAG, "errMsg=" + errMsg);
62        fail(errMsg);
63    }
64
65    void failStr(Exception e) {
66        failStr(e.getMessage());
67    }
68
69    @Override
70    protected void setUp() throws Exception {
71        super.setUp();
72        if (localLOGV) Log.i(TAG, "Cleaning out old test containers");
73        cleanupContainers();
74    }
75
76    @Override
77    protected void tearDown() throws Exception {
78        super.tearDown();
79        if (localLOGV) Log.i(TAG, "Cleaning out old test containers");
80        cleanupContainers();
81    }
82
83    public void testMountAndPullSdCard() {
84        try {
85            fullId = PREFIX;
86            fullId2 = PackageHelper.createSdDir(1024 * MB_IN_BYTES, fullId, "none",
87                    android.os.Process.myUid(), true);
88
89            Log.d(TAG,PackageHelper.getSdDir(fullId));
90            PackageHelper.unMountSdDir(fullId);
91
92            Runnable r1 = getMountRunnable();
93            Runnable r2 = getDestroyRunnable();
94            Thread thread = new Thread(r1);
95            Thread thread2 = new Thread(r2);
96            thread2.start();
97            thread.start();
98        } catch (Exception e) {
99            failStr(e);
100        }
101    }
102
103    public Runnable getMountRunnable() {
104        Runnable r = new Runnable () {
105            public void run () {
106                try {
107                    Thread.sleep(5);
108                    String path = PackageHelper.mountSdDir(fullId, "none",
109                            android.os.Process.myUid());
110                    Log.e(TAG, "mount done " + path);
111                } catch (IllegalArgumentException iae) {
112                    throw iae;
113                } catch (Throwable t) {
114                    Log.e(TAG, "mount failed", t);
115                }
116            }
117        };
118        return r;
119    }
120
121    public Runnable getDestroyRunnable() {
122        Runnable r = new Runnable () {
123            public void run () {
124                try {
125                    PackageHelper.destroySdDir(fullId);
126                    Log.e(TAG, "destroy done: " + fullId);
127                } catch (Throwable t) {
128                    Log.e(TAG, "destroy failed", t);
129                }
130            }
131        };
132        return r;
133    }
134}
135