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.cts; 18 19 import java.io.File; 20 import java.io.FileOutputStream; 21 import java.io.IOException; 22 import java.io.InputStream; 23 import java.security.GeneralSecurityException; 24 25 import android.content.Context; 26 import android.content.res.AssetManager; 27 import android.os.RecoverySystem; 28 import android.test.AndroidTestCase; 29 import android.test.suitebuilder.annotation.MediumTest; 30 31 import android.util.Log; 32 33 public class RecoverySystemTest extends AndroidTestCase { 34 private static final String TAG = "RecoverySystemTest"; 35 36 private AssetManager mAssets; 37 38 @Override 39 protected void setUp() throws Exception { 40 Log.v(TAG, "setup"); 41 super.setUp(); 42 mAssets = mContext.getAssets(); 43 } 44 45 /** Write the given asset to a file of the same name and return a File. */ 46 private File getAsset(String name) throws Exception { 47 FileOutputStream fos = mContext.openFileOutput(name, 0); 48 InputStream is = mAssets.open(name); 49 byte[] b = new byte[4096]; 50 int read; 51 while ((read = is.read(b)) != -1) { 52 fos.write(b, 0, read); 53 } 54 is.close(); 55 fos.close(); 56 return mContext.getFileStreamPath(name); 57 } 58 59 @MediumTest 60 public void testVerify() throws Exception { 61 File otacerts = getAsset("otacerts.zip"); 62 File packageFile; 63 64 // This is the only package for which verification should succeed. 65 Log.v(TAG, "testing otasigned.zip"); 66 packageFile = getAsset("otasigned.zip"); 67 RecoverySystem.verifyPackage(packageFile, null, otacerts); 68 packageFile.delete(); 69 70 expectVerifyFail("alter-footer.zip", otacerts); 71 expectVerifyFail("alter-metadata.zip", otacerts); 72 expectVerifyFail("fake-eocd.zip", otacerts); 73 expectVerifyFail("jarsigned.zip", otacerts); 74 expectVerifyFail("random.zip", otacerts); 75 expectVerifyFail("unsigned.zip", otacerts); 76 77 otacerts.delete(); 78 } 79 80 /** 81 * Try verifying the given file against the given otacerts, 82 * expecting verification to fail. 83 */ 84 private void expectVerifyFail(String name, File otacerts) 85 throws Exception { 86 Log.v(TAG, "testing " + name); 87 File packageFile = getAsset(name); 88 try { 89 RecoverySystem.verifyPackage(packageFile, null, otacerts); 90 fail("verification of " + name + " succeeded when it shouldn't have"); 91 } catch (GeneralSecurityException e) { 92 // expected 93 } 94 packageFile.delete(); 95 } 96 } 97