1 /* 2 * Copyright (C) 2010 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.os.storage; 18 19 import android.content.Context; 20 import android.os.Environment; 21 import android.test.InstrumentationTestCase; 22 import android.test.suitebuilder.annotation.LargeTest; 23 import android.util.Log; 24 25 import com.android.frameworks.coretests.R; 26 27 import java.io.DataInputStream; 28 import java.io.IOException; 29 import java.io.File; 30 import java.io.FileInputStream; 31 32 import junit.framework.AssertionFailedError; 33 34 public class StorageManagerIntegrationTest extends StorageManagerBaseTest { 35 36 private static String LOG_TAG = "StorageManagerBaseTest.StorageManagerIntegrationTest"; 37 protected File mFile = null; 38 39 /** 40 * {@inheritDoc} 41 */ 42 @Override 43 public void setUp() throws Exception { 44 super.setUp(); 45 mContext = getInstrumentation().getContext(); 46 mFile = null; 47 } 48 49 /** 50 * {@inheritDoc} 51 */ 52 @Override 53 protected void tearDown() throws Exception { 54 if (mFile != null) { 55 mFile.delete(); 56 mFile = null; 57 } 58 super.tearDown(); 59 } 60 61 /** 62 * Tests mounting a single OBB file and verifies its contents. 63 */ 64 @LargeTest 65 public void testMountSingleObb() { 66 mFile = createObbFile(OBB_FILE_1, R.raw.obb_file1); 67 String filePath = mFile.getAbsolutePath(); 68 mountObb(filePath); 69 verifyObb1Contents(filePath); 70 unmountObb(filePath, DONT_FORCE); 71 } 72 73 /** 74 * Tests mounting several OBB files and verifies its contents. 75 */ 76 @LargeTest 77 public void testMountMultipleObb() { 78 File file1 = null; 79 File file2 = null; 80 File file3 = null; 81 try { 82 file1 = createObbFile(OBB_FILE_1, R.raw.obb_file1); 83 String filePath1 = file1.getAbsolutePath(); 84 mountObb(filePath1); 85 verifyObb1Contents(filePath1); 86 87 file2 = createObbFile(OBB_FILE_2, R.raw.obb_file2); 88 String filePath2 = file2.getAbsolutePath(); 89 mountObb(filePath2); 90 verifyObb2Contents(filePath2); 91 92 file3 = createObbFile(OBB_FILE_3, R.raw.obb_file3); 93 String filePath3 = file3.getAbsolutePath(); 94 mountObb(filePath3); 95 verifyObb3Contents(filePath3); 96 97 unmountObb(filePath1, DONT_FORCE); 98 unmountObb(filePath2, DONT_FORCE); 99 unmountObb(filePath3, DONT_FORCE); 100 } finally { 101 if (file1 != null) { 102 file1.delete(); 103 } 104 if (file2 != null) { 105 file2.delete(); 106 } 107 if (file3 != null) { 108 file3.delete(); 109 } 110 } 111 } 112 113 /** 114 * Tests mounting a single encrypted OBB file and verifies its contents. 115 */ 116 @LargeTest 117 public void testMountSingleEncryptedObb() { 118 mFile = createObbFile(OBB_FILE_3_ENCRYPTED, R.raw.obb_enc_file100_orig3); 119 String filePath = mFile.getAbsolutePath(); 120 mountObb(filePath, OBB_FILE_3_PASSWORD, OnObbStateChangeListener.MOUNTED); 121 verifyObb3Contents(filePath); 122 unmountObb(filePath, DONT_FORCE); 123 } 124 125 /** 126 * Tests mounting a single encrypted OBB file using an invalid password. 127 */ 128 @LargeTest 129 public void testMountSingleEncryptedObbInvalidPassword() { 130 mFile = createObbFile("bad password@$%#@^*(!&)", R.raw.obb_enc_file100_orig3); 131 String filePath = mFile.getAbsolutePath(); 132 mountObb(filePath, OBB_FILE_3_PASSWORD, OnObbStateChangeListener.ERROR_COULD_NOT_MOUNT); 133 unmountObb(filePath, DONT_FORCE); 134 } 135 136 /** 137 * Tests simultaneously mounting 2 encrypted OBBs with different keys and verifies contents. 138 */ 139 @LargeTest 140 public void testMountTwoEncryptedObb() { 141 File file3 = null; 142 File file1 = null; 143 try { 144 file3 = createObbFile(OBB_FILE_3_ENCRYPTED, R.raw.obb_enc_file100_orig3); 145 String filePath3 = file3.getAbsolutePath(); 146 mountObb(filePath3, OBB_FILE_3_PASSWORD, OnObbStateChangeListener.MOUNTED); 147 verifyObb3Contents(filePath3); 148 149 file1 = createObbFile(OBB_FILE_1_ENCRYPTED, R.raw.obb_enc_file100_orig1); 150 String filePath1 = file1.getAbsolutePath(); 151 mountObb(filePath1, OBB_FILE_1_PASSWORD, OnObbStateChangeListener.MOUNTED); 152 verifyObb1Contents(filePath1); 153 154 unmountObb(filePath3, DONT_FORCE); 155 unmountObb(filePath1, DONT_FORCE); 156 } finally { 157 if (file3 != null) { 158 file3.delete(); 159 } 160 if (file1 != null) { 161 file1.delete(); 162 } 163 } 164 } 165 166 /** 167 * Tests that we can not force unmount when a file is currently open on the OBB. 168 */ 169 @LargeTest 170 public void testUnmount_DontForce() { 171 mFile = createObbFile(OBB_FILE_1, R.raw.obb_file1); 172 String obbFilePath = mFile.getAbsolutePath(); 173 174 MountingObbThread mountingThread = new MountingObbThread(obbFilePath, 175 OBB_FILE_1_CONTENTS_1); 176 177 try { 178 mountingThread.start(); 179 180 long waitTime = 0; 181 while (!mountingThread.isFileOpenOnObb()) { 182 synchronized (mountingThread) { 183 Log.i(LOG_TAG, "Waiting for file to be opened on OBB..."); 184 mountingThread.wait(WAIT_TIME_INCR); 185 waitTime += WAIT_TIME_INCR; 186 if (waitTime > MAX_WAIT_TIME) { 187 fail("Timed out waiting for file file to be opened on OBB!"); 188 } 189 } 190 } 191 192 unmountObb(obbFilePath, DONT_FORCE); 193 194 // verify still mounted 195 assertTrue("mounted path should not be null!", obbFilePath != null); 196 assertTrue("mounted path should still be mounted!", mSm.isObbMounted(obbFilePath)); 197 198 // close the opened file 199 mountingThread.doStop(); 200 201 // try unmounting again (should succeed this time) 202 unmountObb(obbFilePath, DONT_FORCE); 203 assertFalse("mounted path should no longer be mounted!", 204 mSm.isObbMounted(obbFilePath)); 205 } catch (InterruptedException e) { 206 fail("Timed out waiting for file on OBB to be opened..."); 207 } 208 } 209 210 /** 211 * Tests mounting a single OBB that isn't signed. 212 */ 213 @LargeTest 214 public void testMountUnsignedObb() { 215 mFile = createObbFile(OBB_FILE_2_UNSIGNED, R.raw.obb_file2_nosign); 216 String filePath = mFile.getAbsolutePath(); 217 mountObb(filePath, OBB_FILE_2_UNSIGNED, OnObbStateChangeListener.ERROR_INTERNAL); 218 } 219 220 /** 221 * Tests mounting a single OBB that is signed with a different package. 222 */ 223 @LargeTest 224 public void testMountBadPackageNameObb() { 225 mFile = createObbFile(OBB_FILE_3_BAD_PACKAGENAME, R.raw.obb_file3_bad_packagename); 226 String filePath = mFile.getAbsolutePath(); 227 mountObb(filePath, OBB_FILE_3_BAD_PACKAGENAME, 228 OnObbStateChangeListener.ERROR_PERMISSION_DENIED); 229 } 230 231 /** 232 * Tests remounting a single OBB that has already been mounted. 233 */ 234 @LargeTest 235 public void testRemountObb() { 236 mFile = createObbFile(OBB_FILE_1, R.raw.obb_file1); 237 String filePath = mFile.getAbsolutePath(); 238 mountObb(filePath); 239 verifyObb1Contents(filePath); 240 mountObb(filePath, null, OnObbStateChangeListener.ERROR_ALREADY_MOUNTED); 241 verifyObb1Contents(filePath); 242 unmountObb(filePath, DONT_FORCE); 243 } 244 }