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