Suprabh Shukla | 3ac1daa | 2017-07-14 12:15:27 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | |
Svet Ganov | 8455ba2 | 2019-01-02 13:05:56 -0800 | [diff] [blame] | 17 | package com.android.server.appop; |
Suprabh Shukla | 3ac1daa | 2017-07-14 12:15:27 -0700 | [diff] [blame] | 18 | |
| 19 | import static org.junit.Assert.assertEquals; |
| 20 | import static org.junit.Assert.assertTrue; |
| 21 | import static org.junit.Assert.fail; |
| 22 | |
| 23 | import android.app.AppOpsManager; |
| 24 | import android.content.Context; |
| 25 | import android.content.res.AssetManager; |
| 26 | import android.os.Handler; |
| 27 | import android.os.HandlerThread; |
Suprabh Shukla | 3ac1daa | 2017-07-14 12:15:27 -0700 | [diff] [blame] | 28 | import android.util.Log; |
| 29 | import android.util.SparseArray; |
| 30 | import android.util.Xml; |
| 31 | |
Brett Chabot | a26eda9 | 2018-07-23 13:08:30 -0700 | [diff] [blame] | 32 | import androidx.test.InstrumentationRegistry; |
| 33 | import androidx.test.filters.SmallTest; |
| 34 | import androidx.test.runner.AndroidJUnit4; |
| 35 | |
Suprabh Shukla | 3ac1daa | 2017-07-14 12:15:27 -0700 | [diff] [blame] | 36 | import org.junit.Before; |
| 37 | import org.junit.Test; |
| 38 | import org.junit.runner.RunWith; |
| 39 | import org.xmlpull.v1.XmlPullParser; |
| 40 | |
| 41 | import java.io.File; |
| 42 | import java.io.FileInputStream; |
| 43 | import java.io.FileOutputStream; |
| 44 | import java.io.IOException; |
| 45 | import java.io.InputStream; |
| 46 | import java.nio.charset.StandardCharsets; |
| 47 | |
| 48 | /** |
| 49 | * Tests app ops version upgrades |
| 50 | */ |
Suprabh Shukla | 6a565d0 | 2017-08-09 14:55:34 -0700 | [diff] [blame] | 51 | @SmallTest |
Suprabh Shukla | 3ac1daa | 2017-07-14 12:15:27 -0700 | [diff] [blame] | 52 | @RunWith(AndroidJUnit4.class) |
| 53 | public class AppOpsUpgradeTest { |
| 54 | private static final String TAG = AppOpsUpgradeTest.class.getSimpleName(); |
| 55 | private static final String APP_OPS_UNVERSIONED_ASSET_PATH = |
| 56 | "AppOpsUpgradeTest/appops-unversioned.xml"; |
| 57 | private static final String APP_OPS_FILENAME = "appops-test.xml"; |
| 58 | private static final int NON_DEFAULT_OPS_IN_FILE = 4; |
| 59 | private static final int CURRENT_VERSION = 1; |
| 60 | |
| 61 | private File mAppOpsFile; |
| 62 | private Context mContext; |
| 63 | private Handler mHandler; |
| 64 | |
| 65 | private void extractAppOpsFile() { |
| 66 | mAppOpsFile.getParentFile().mkdirs(); |
| 67 | if (mAppOpsFile.exists()) { |
| 68 | mAppOpsFile.delete(); |
| 69 | } |
| 70 | try (FileOutputStream out = new FileOutputStream(mAppOpsFile); |
| 71 | InputStream in = mContext.getAssets().open(APP_OPS_UNVERSIONED_ASSET_PATH, |
| 72 | AssetManager.ACCESS_BUFFER)) { |
| 73 | byte[] buffer = new byte[4096]; |
| 74 | int bytesRead; |
| 75 | while ((bytesRead = in.read(buffer)) >= 0) { |
| 76 | out.write(buffer, 0, bytesRead); |
| 77 | } |
| 78 | out.flush(); |
| 79 | Log.d(TAG, "Successfully copied xml to " + mAppOpsFile.getAbsolutePath()); |
| 80 | } catch (IOException exc) { |
| 81 | Log.e(TAG, "Exception while copying appops xml", exc); |
| 82 | fail(); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | private void assertSameModes(SparseArray<AppOpsService.UidState> uidStates, int op1, int op2) { |
| 87 | int numberOfNonDefaultOps = 0; |
| 88 | final int defaultModeOp1 = AppOpsManager.opToDefaultMode(op1); |
| 89 | final int defaultModeOp2 = AppOpsManager.opToDefaultMode(op2); |
| 90 | for(int i = 0; i < uidStates.size(); i++) { |
| 91 | final AppOpsService.UidState uidState = uidStates.valueAt(i); |
| 92 | if (uidState.opModes != null) { |
| 93 | final int uidMode1 = uidState.opModes.get(op1, defaultModeOp1); |
| 94 | final int uidMode2 = uidState.opModes.get(op2, defaultModeOp2); |
| 95 | assertEquals(uidMode1, uidMode2); |
| 96 | if (uidMode1 != defaultModeOp1) { |
| 97 | numberOfNonDefaultOps++; |
| 98 | } |
| 99 | } |
| 100 | if (uidState.pkgOps == null) { |
| 101 | continue; |
| 102 | } |
| 103 | for (int j = 0; j < uidState.pkgOps.size(); j++) { |
| 104 | final AppOpsService.Ops ops = uidState.pkgOps.valueAt(j); |
| 105 | if (ops == null) { |
| 106 | continue; |
| 107 | } |
| 108 | final AppOpsService.Op _op1 = ops.get(op1); |
| 109 | final AppOpsService.Op _op2 = ops.get(op2); |
| 110 | final int mode1 = (_op1 == null) ? defaultModeOp1 : _op1.mode; |
| 111 | final int mode2 = (_op2 == null) ? defaultModeOp2 : _op2.mode; |
| 112 | assertEquals(mode1, mode2); |
| 113 | if (mode1 != defaultModeOp1) { |
| 114 | numberOfNonDefaultOps++; |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | assertEquals(numberOfNonDefaultOps, NON_DEFAULT_OPS_IN_FILE); |
| 119 | } |
| 120 | |
| 121 | @Before |
| 122 | public void setUp() { |
| 123 | mContext = InstrumentationRegistry.getTargetContext(); |
| 124 | mAppOpsFile = new File(mContext.getFilesDir(), APP_OPS_FILENAME); |
| 125 | extractAppOpsFile(); |
| 126 | HandlerThread handlerThread = new HandlerThread(TAG); |
| 127 | handlerThread.start(); |
| 128 | mHandler = new Handler(handlerThread.getLooper()); |
| 129 | } |
| 130 | |
| 131 | @Test |
| 132 | public void testUpgradeFromNoVersion() throws Exception { |
| 133 | AppOpsDataParser parser = new AppOpsDataParser(mAppOpsFile); |
| 134 | assertTrue(parser.parse()); |
| 135 | assertEquals(AppOpsDataParser.NO_VERSION, parser.mVersion); |
| 136 | AppOpsService testService = new AppOpsService(mAppOpsFile, mHandler); // trigger upgrade |
| 137 | assertSameModes(testService.mUidStates, AppOpsManager.OP_RUN_IN_BACKGROUND, |
| 138 | AppOpsManager.OP_RUN_ANY_IN_BACKGROUND); |
| 139 | testService.mContext = mContext; |
| 140 | mHandler.removeCallbacks(testService.mWriteRunner); |
| 141 | testService.writeState(); |
| 142 | assertTrue(parser.parse()); |
| 143 | assertEquals(CURRENT_VERSION, parser.mVersion); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Class to parse data from the appops xml. Currently only parses and holds the version number. |
| 148 | * Other fields may be added as and when required for testing. |
| 149 | */ |
| 150 | private static final class AppOpsDataParser { |
| 151 | static final int NO_VERSION = -1; |
| 152 | int mVersion; |
| 153 | private File mFile; |
| 154 | |
| 155 | AppOpsDataParser(File file) { |
| 156 | mFile = file; |
| 157 | mVersion = NO_VERSION; |
| 158 | } |
| 159 | |
| 160 | boolean parse() { |
| 161 | try (FileInputStream stream = new FileInputStream(mFile)) { |
| 162 | XmlPullParser parser = Xml.newPullParser(); |
| 163 | parser.setInput(stream, StandardCharsets.UTF_8.name()); |
| 164 | int type; |
| 165 | while ((type = parser.next()) != XmlPullParser.START_TAG |
| 166 | && type != XmlPullParser.END_DOCUMENT) { |
| 167 | ; |
| 168 | } |
| 169 | if (type != XmlPullParser.START_TAG) { |
| 170 | throw new IllegalStateException("no start tag found"); |
| 171 | } |
| 172 | final String versionString = parser.getAttributeValue(null, "v"); |
| 173 | if (versionString != null) { |
| 174 | mVersion = Integer.parseInt(versionString); |
| 175 | } |
| 176 | } catch (Exception e) { |
| 177 | Log.e(TAG, "Failed while parsing test appops xml", e); |
| 178 | return false; |
| 179 | } |
| 180 | return true; |
| 181 | } |
| 182 | } |
| 183 | } |