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 import android.util.Base64; 22 23 import java.util.jar.Attributes; 24 25 public class ManifestDigestTest extends AndroidTestCase { 26 private static final byte[] DIGEST_1 = { 27 (byte) 0x00, (byte) 0xAA, (byte) 0x55, (byte) 0xFF 28 }; 29 30 private static final String DIGEST_1_STR = Base64.encodeToString(DIGEST_1, Base64.DEFAULT); 31 32 private static final byte[] DIGEST_2 = { 33 (byte) 0x0A, (byte) 0xA5, (byte) 0xF0, (byte) 0x5A 34 }; 35 36 private static final String DIGEST_2_STR = Base64.encodeToString(DIGEST_2, Base64.DEFAULT); 37 38 private static final Attributes.Name SHA1_DIGEST = new Attributes.Name("SHA1-Digest"); 39 40 private static final Attributes.Name MD5_DIGEST = new Attributes.Name("MD5-Digest"); 41 42 public void testManifestDigest_FromAttributes_Null() { 43 assertNull("Attributes were null, so ManifestDigest.fromAttributes should return null", 44 ManifestDigest.fromAttributes(null)); 45 } 46 47 public void testManifestDigest_FromAttributes_NoAttributes() { 48 Attributes a = new Attributes(); 49 50 assertNull("There were no attributes to extract, so ManifestDigest should be null", 51 ManifestDigest.fromAttributes(a)); 52 } 53 54 public void testManifestDigest_FromAttributes_SHA1PreferredOverMD5() { 55 Attributes a = new Attributes(); 56 a.put(SHA1_DIGEST, DIGEST_1_STR); 57 58 a.put(MD5_DIGEST, DIGEST_2_STR); 59 60 ManifestDigest fromAttributes = ManifestDigest.fromAttributes(a); 61 62 assertNotNull("A valid ManifestDigest should be returned", fromAttributes); 63 64 ManifestDigest created = new ManifestDigest(DIGEST_1); 65 66 assertEquals("SHA-1 should be preferred over MD5: " + created.toString() + " vs. " 67 + fromAttributes.toString(), created, fromAttributes); 68 69 assertEquals("Hash codes should be the same: " + created.toString() + " vs. " 70 + fromAttributes.toString(), created.hashCode(), fromAttributes 71 .hashCode()); 72 } 73 74 public void testManifestDigest_Parcel() { 75 Attributes a = new Attributes(); 76 a.put(SHA1_DIGEST, DIGEST_1_STR); 77 78 ManifestDigest digest = ManifestDigest.fromAttributes(a); 79 80 Parcel p = Parcel.obtain(); 81 digest.writeToParcel(p, 0); 82 p.setDataPosition(0); 83 84 ManifestDigest fromParcel = ManifestDigest.CREATOR.createFromParcel(p); 85 86 assertEquals("ManifestDigest going through parceling should be the same as before: " 87 + digest.toString() + " and " + fromParcel.toString(), digest, 88 fromParcel); 89 } 90 } 91