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