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