1 /* 2 * Copyright (C) 2011 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.content.pm; 18 19 import android.os.Parcel; 20 import android.test.AndroidTestCase; 21 22 import java.io.ByteArrayInputStream; 23 import java.io.IOException; 24 import java.io.InputStream; 25 import java.security.MessageDigest; 26 27 public class ManifestDigestTest extends AndroidTestCase { 28 private static final byte[] MESSAGE_1 = { 29 (byte) 0x00, (byte) 0xAA, (byte) 0x55, (byte) 0xFF 30 }; 31 32 public void testManifestDigest_FromInputStream_Null() { 33 assertNull("Attributes were null, so ManifestDigest.fromAttributes should return null", 34 ManifestDigest.fromInputStream(null)); 35 } 36 37 public void testManifestDigest_FromInputStream_ThrowsIoException() { 38 InputStream is = new InputStream() { 39 @Override 40 public int read() throws IOException { 41 throw new IOException(); 42 } 43 }; 44 45 assertNull("InputStream threw exception, so ManifestDigest should be null", 46 ManifestDigest.fromInputStream(is)); 47 } 48 49 public void testManifestDigest_Equals() throws Exception { 50 InputStream is = new ByteArrayInputStream(MESSAGE_1); 51 52 ManifestDigest expected = 53 new ManifestDigest(MessageDigest.getInstance("SHA-256").digest(MESSAGE_1)); 54 55 ManifestDigest actual = ManifestDigest.fromInputStream(is); 56 assertEquals(expected, actual); 57 58 ManifestDigest unexpected = new ManifestDigest(new byte[0]); 59 assertFalse(unexpected.equals(actual)); 60 } 61 62 public void testManifestDigest_Parcel() throws Exception { 63 InputStream is = new ByteArrayInputStream(MESSAGE_1); 64 65 ManifestDigest digest = ManifestDigest.fromInputStream(is); 66 67 Parcel p = Parcel.obtain(); 68 digest.writeToParcel(p, 0); 69 p.setDataPosition(0); 70 71 ManifestDigest fromParcel = ManifestDigest.CREATOR.createFromParcel(p); 72 73 assertEquals("ManifestDigest going through parceling should be the same as before: " 74 + digest.toString() + " and " + fromParcel.toString(), digest, 75 fromParcel); 76 } 77 } 78