ManifestDigestTest.java revision bcc954d772e8cd5ef640060cbc0be50e7e4778f2
1package android.content.pm;
2
3import android.os.Parcel;
4import android.test.AndroidTestCase;
5import android.util.Base64;
6
7import java.util.jar.Attributes;
8
9public class ManifestDigestTest extends AndroidTestCase {
10    private static final byte[] DIGEST_1 = {
11            (byte) 0x00, (byte) 0xAA, (byte) 0x55, (byte) 0xFF
12    };
13
14    private static final String DIGEST_1_STR = Base64.encodeToString(DIGEST_1, Base64.DEFAULT);
15
16    private static final byte[] DIGEST_2 = {
17            (byte) 0x0A, (byte) 0xA5, (byte) 0xF0, (byte) 0x5A
18    };
19
20    private static final String DIGEST_2_STR = Base64.encodeToString(DIGEST_2, Base64.DEFAULT);
21
22    private static final Attributes.Name SHA1_DIGEST = new Attributes.Name("SHA1-Digest");
23
24    private static final Attributes.Name MD5_DIGEST = new Attributes.Name("MD5-Digest");
25
26    public void testManifestDigest_FromAttributes_Null() {
27        assertNull("Attributes were null, so ManifestDigest.fromAttributes should return null",
28                ManifestDigest.fromAttributes(null));
29    }
30
31    public void testManifestDigest_FromAttributes_NoAttributes() {
32        Attributes a = new Attributes();
33
34        assertNull("There were no attributes to extract, so ManifestDigest should be null",
35                ManifestDigest.fromAttributes(a));
36    }
37
38    public void testManifestDigest_FromAttributes_SHA1PreferredOverMD5() {
39        Attributes a = new Attributes();
40        a.put(SHA1_DIGEST, DIGEST_1_STR);
41
42        a.put(MD5_DIGEST, DIGEST_2_STR);
43
44        ManifestDigest fromAttributes = ManifestDigest.fromAttributes(a);
45
46        assertNotNull("A valid ManifestDigest should be returned", fromAttributes);
47
48        ManifestDigest created = new ManifestDigest(DIGEST_1);
49
50        assertEquals("SHA-1 should be preferred over MD5: " + created.toString() + " vs. "
51                + fromAttributes.toString(), created, fromAttributes);
52
53        assertEquals("Hash codes should be the same: " + created.toString() + " vs. "
54                + fromAttributes.toString(), created.hashCode(), fromAttributes
55                .hashCode());
56    }
57
58    public void testManifestDigest_Parcel() {
59        Attributes a = new Attributes();
60        a.put(SHA1_DIGEST, DIGEST_1_STR);
61
62        ManifestDigest digest = ManifestDigest.fromAttributes(a);
63
64        Parcel p = Parcel.obtain();
65        digest.writeToParcel(p, 0);
66        p.setDataPosition(0);
67
68        ManifestDigest fromParcel = ManifestDigest.CREATOR.createFromParcel(p);
69
70        assertEquals("ManifestDigest going through parceling should be the same as before: "
71                + digest.toString() + " and " + fromParcel.toString(), digest,
72                fromParcel);
73    }
74}
75