Home | History | Annotate | Download | only in pm
      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 
     17 package android.content.pm;
     18 
     19 import com.android.internal.content.PackageHelper;
     20 
     21 import android.os.IBinder;
     22 import android.os.RemoteException;
     23 import android.os.ServiceManager;
     24 import android.os.storage.IMountService;
     25 import android.test.AndroidTestCase;
     26 import android.util.Log;
     27 
     28 public 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 
     86             Log.d(TAG,PackageHelper.getSdDir(fullId));
     87             PackageHelper.unMountSdDir(fullId);
     88 
     89             Runnable r1 = getMountRunnable();
     90             Runnable r2 = getDestroyRunnable();
     91             Thread thread = new Thread(r1);
     92             Thread thread2 = new Thread(r2);
     93             thread2.start();
     94             thread.start();
     95         } catch (Exception e) {
     96             failStr(e);
     97         }
     98     }
     99 
    100     public Runnable getMountRunnable() {
    101         Runnable r = new Runnable () {
    102             public void run () {
    103                 try {
    104                     Thread.sleep(5);
    105                     String path = PackageHelper.mountSdDir(fullId, "none",
    106                             android.os.Process.myUid());
    107                     Log.e(TAG, "mount done " + path);
    108                 } catch (IllegalArgumentException iae) {
    109                     throw iae;
    110                 } catch (Throwable t) {
    111                     Log.e(TAG, "mount failed", t);
    112                 }
    113             }
    114         };
    115         return r;
    116     }
    117 
    118     public Runnable getDestroyRunnable() {
    119         Runnable r = new Runnable () {
    120             public void run () {
    121                 try {
    122                     PackageHelper.destroySdDir(fullId);
    123                     Log.e(TAG, "destroy done: " + fullId);
    124                 } catch (Throwable t) {
    125                     Log.e(TAG, "destroy failed", t);
    126                 }
    127             }
    128         };
    129         return r;
    130     }
    131 }
    132