Home | History | Annotate | Download | only in devicepolicy
      1 /*
      2  * Copyright (C) 2015 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 com.android.server.devicepolicy;
     18 
     19 import android.content.Context;
     20 import android.os.Bundle;
     21 import android.os.FileUtils;
     22 import android.os.Parcel;
     23 import android.os.Parcelable;
     24 import android.test.AndroidTestCase;
     25 import android.util.Log;
     26 import android.util.Printer;
     27 
     28 import libcore.io.Streams;
     29 
     30 import com.google.android.collect.Lists;
     31 
     32 import junit.framework.AssertionFailedError;
     33 
     34 import org.junit.Assert;
     35 
     36 import java.io.BufferedReader;
     37 import java.io.File;
     38 import java.io.FileNotFoundException;
     39 import java.io.FileOutputStream;
     40 import java.io.FileWriter;
     41 import java.io.IOException;
     42 import java.io.InputStream;
     43 import java.io.InputStreamReader;
     44 import java.util.ArrayList;
     45 import java.util.Collections;
     46 import java.util.List;
     47 
     48 public class DpmTestUtils extends AndroidTestCase {
     49     public static void clearDir(File dir) {
     50         if (dir.exists()) {
     51             Assert.assertTrue("failed to delete dir", FileUtils.deleteContents(dir));
     52         }
     53         dir.mkdirs();
     54         Log.i(DpmTestBase.TAG, "Created " + dir);
     55     }
     56 
     57     public static int getListSizeAllowingNull(List<?> list) {
     58         return list == null ? 0 : list.size();
     59     }
     60 
     61     public static Bundle newRestrictions(String... restrictions) {
     62         final Bundle ret = new Bundle();
     63         for (String restriction : restrictions) {
     64             ret.putBoolean(restriction, true);
     65         }
     66         return ret;
     67     }
     68 
     69     public static void assertRestrictions(Bundle expected, Bundle actual) {
     70         final ArrayList<String> elist;
     71         if (expected == null) {
     72             elist = null;
     73         } else {
     74             elist = Lists.newArrayList();
     75             for (String key : expected.keySet()) {
     76                 if (expected.getBoolean(key)) {
     77                     elist.add(key);
     78                 }
     79             }
     80             Collections.sort(elist);
     81         }
     82 
     83         final ArrayList<String> alist;
     84         if (actual == null) {
     85             alist = null;
     86         } else {
     87             alist = Lists.newArrayList();
     88             for (String key : actual.keySet()) {
     89                 if (actual.getBoolean(key)) {
     90                     alist.add(key);
     91                 }
     92             }
     93             Collections.sort(alist);
     94         }
     95 
     96         assertEquals(elist, alist);
     97     }
     98 
     99     public static <T extends Parcelable> T cloneParcelable(T source) {
    100         Parcel p = Parcel.obtain();
    101         p.writeParcelable(source, 0);
    102         p.setDataPosition(0);
    103         final T clone = p.readParcelable(DpmTestUtils.class.getClassLoader());
    104         p.recycle();
    105         return clone;
    106     }
    107 
    108     public static Printer LOG_PRINTER = new Printer() {
    109         @Override
    110         public void println(String x) {
    111             Log.i(DpmTestBase.TAG, x);
    112         }
    113     };
    114 
    115     public static String readAsset(Context context, String assetPath) throws IOException {
    116         final StringBuilder sb = new StringBuilder();
    117         try (BufferedReader br = new BufferedReader(
    118                 new InputStreamReader(
    119                         context.getResources().getAssets().open(assetPath)))) {
    120             String line;
    121             while ((line = br.readLine()) != null) {
    122                 sb.append(line);
    123                 sb.append(System.lineSeparator());
    124             }
    125         }
    126         return sb.toString();
    127     }
    128 
    129     public static void writeToFile(File path, String content)
    130             throws IOException {
    131         path.getParentFile().mkdirs();
    132 
    133         try (FileWriter writer = new FileWriter(path)) {
    134             Log.i(DpmTestBase.TAG, "Writing to " + path);
    135             Log.i(DpmTestBase.TAG, content);
    136             writer.write(content);
    137         }
    138     }
    139 
    140     public static void writeInputStreamToFile(InputStream stream, File file)
    141             throws IOException {
    142         Streams.copy(stream, new FileOutputStream(file));
    143     }
    144 
    145     private static boolean checkAssertRestrictions(Bundle a, Bundle b) {
    146         try {
    147             assertRestrictions(a, b);
    148             return true;
    149         } catch (AssertionFailedError e) {
    150             return false;
    151         }
    152     }
    153 
    154     public void testAssertRestrictions() {
    155         final Bundle a = newRestrictions();
    156         final Bundle b = newRestrictions("a");
    157         final Bundle c = newRestrictions("a");
    158         final Bundle d = newRestrictions("b", "c");
    159         final Bundle e = newRestrictions("b", "c");
    160 
    161         assertTrue(checkAssertRestrictions(null, null));
    162         assertFalse(checkAssertRestrictions(null, a));
    163         assertFalse(checkAssertRestrictions(a, null));
    164         assertTrue(checkAssertRestrictions(a, a));
    165 
    166         assertFalse(checkAssertRestrictions(a, b));
    167         assertTrue(checkAssertRestrictions(b, c));
    168 
    169         assertFalse(checkAssertRestrictions(c, d));
    170         assertTrue(checkAssertRestrictions(d, e));
    171     }
    172 }
    173