1/*
2 * Copyright (C) 2012 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
17package android.content.pm;
18
19import android.content.pm.ManifestDigest;
20import android.content.pm.VerificationParams;
21import android.net.Uri;
22import android.os.Parcel;
23import android.test.AndroidTestCase;
24
25/**
26 * Tests the android.content.pm.VerificationParams class
27 *
28 * To test run:
29 * ./development/testrunner/runtest.py frameworks-core -c android.content.pm.VerificationParamsTest
30 */
31public class VerificationParamsTest extends AndroidTestCase {
32
33    private final static String VERIFICATION_URI_STRING = "http://verification.uri/path";
34    private final static String ORIGINATING_URI_STRING = "http://originating.uri/path";
35    private final static String REFERRER_STRING = "http://referrer.uri/path";
36    private final static byte[] DIGEST_BYTES = "fake digest".getBytes();
37    private final static int INSTALLER_UID = 42;
38
39    private final static Uri VERIFICATION_URI = Uri.parse(VERIFICATION_URI_STRING);
40    private final static Uri ORIGINATING_URI = Uri.parse(ORIGINATING_URI_STRING);
41    private final static Uri REFERRER = Uri.parse(REFERRER_STRING);
42
43    private final static int ORIGINATING_UID = 10042;
44
45    private final static ManifestDigest MANIFEST_DIGEST = new ManifestDigest(DIGEST_BYTES);
46
47    public void testParcel() throws Exception {
48        VerificationParams expected = new VerificationParams(VERIFICATION_URI, ORIGINATING_URI,
49                REFERRER, ORIGINATING_UID, MANIFEST_DIGEST);
50
51        Parcel parcel = Parcel.obtain();
52        expected.writeToParcel(parcel, 0);
53        parcel.setDataPosition(0);
54
55        VerificationParams actual = VerificationParams.CREATOR.createFromParcel(parcel);
56
57        assertEquals(VERIFICATION_URI, actual.getVerificationURI());
58
59        assertEquals(ORIGINATING_URI, actual.getOriginatingURI());
60
61        assertEquals(REFERRER, actual.getReferrer());
62
63        assertEquals(ORIGINATING_UID, actual.getOriginatingUid());
64
65        assertEquals(MANIFEST_DIGEST, actual.getManifestDigest());
66    }
67
68    public void testEquals_Success() throws Exception {
69        VerificationParams params1 = new VerificationParams(VERIFICATION_URI, ORIGINATING_URI,
70                REFERRER, ORIGINATING_UID, MANIFEST_DIGEST);
71
72        VerificationParams params2 = new VerificationParams(
73                Uri.parse(VERIFICATION_URI_STRING), Uri.parse(ORIGINATING_URI_STRING),
74                Uri.parse(REFERRER_STRING), ORIGINATING_UID, new ManifestDigest(DIGEST_BYTES));
75
76        assertEquals(params1, params2);
77    }
78
79    public void testEquals_VerificationUri_Failure() throws Exception {
80        VerificationParams params1 = new VerificationParams(VERIFICATION_URI, ORIGINATING_URI,
81                REFERRER, ORIGINATING_UID, MANIFEST_DIGEST);
82
83        VerificationParams params2 = new VerificationParams(
84                Uri.parse("http://a.different.uri/"), Uri.parse(ORIGINATING_URI_STRING),
85                Uri.parse(REFERRER_STRING), ORIGINATING_UID, new ManifestDigest(DIGEST_BYTES));
86
87        assertFalse(params1.equals(params2));
88    }
89
90    public void testEquals_OriginatingUri_Failure() throws Exception {
91        VerificationParams params1 = new VerificationParams(VERIFICATION_URI, ORIGINATING_URI,
92                REFERRER, ORIGINATING_UID, MANIFEST_DIGEST);
93
94        VerificationParams params2 = new VerificationParams(
95                Uri.parse(VERIFICATION_URI_STRING), Uri.parse("http://a.different.uri/"),
96                Uri.parse(REFERRER_STRING), ORIGINATING_UID, new ManifestDigest(DIGEST_BYTES));
97
98        assertFalse(params1.equals(params2));
99    }
100
101    public void testEquals_Referrer_Failure() throws Exception {
102        VerificationParams params1 = new VerificationParams(VERIFICATION_URI, ORIGINATING_URI,
103                REFERRER, ORIGINATING_UID, MANIFEST_DIGEST);
104
105        VerificationParams params2 = new VerificationParams(
106                Uri.parse(VERIFICATION_URI_STRING), Uri.parse(ORIGINATING_URI_STRING),
107                Uri.parse("http://a.different.uri/"), ORIGINATING_UID,
108                new ManifestDigest(DIGEST_BYTES));
109
110        assertFalse(params1.equals(params2));
111    }
112
113    public void testEquals_Originating_Uid_Failure() throws Exception {
114        VerificationParams params1 = new VerificationParams(VERIFICATION_URI, ORIGINATING_URI,
115                REFERRER, ORIGINATING_UID, MANIFEST_DIGEST);
116
117        VerificationParams params2 = new VerificationParams(
118                Uri.parse(VERIFICATION_URI_STRING), Uri.parse(ORIGINATING_URI_STRING),
119                Uri.parse(REFERRER_STRING), 12345, new ManifestDigest(DIGEST_BYTES));
120
121        assertFalse(params1.equals(params2));
122    }
123
124    public void testEquals_ManifestDigest_Failure() throws Exception {
125        VerificationParams params1 = new VerificationParams(VERIFICATION_URI, ORIGINATING_URI,
126                REFERRER, ORIGINATING_UID, MANIFEST_DIGEST);
127
128        VerificationParams params2 = new VerificationParams(
129                Uri.parse(VERIFICATION_URI_STRING), Uri.parse(ORIGINATING_URI_STRING),
130                Uri.parse(REFERRER_STRING), ORIGINATING_UID,
131                new ManifestDigest("a different digest".getBytes()));
132
133        assertFalse(params1.equals(params2));
134    }
135
136    public void testEquals_InstallerUid_Failure() throws Exception {
137        VerificationParams params1 = new VerificationParams(VERIFICATION_URI, ORIGINATING_URI,
138                REFERRER, ORIGINATING_UID, MANIFEST_DIGEST);
139
140        VerificationParams params2 = new VerificationParams(
141                Uri.parse(VERIFICATION_URI_STRING), Uri.parse(ORIGINATING_URI_STRING),
142                Uri.parse(REFERRER_STRING), ORIGINATING_UID, new ManifestDigest(DIGEST_BYTES));
143        params2.setInstallerUid(INSTALLER_UID);
144
145        assertFalse(params1.equals(params2));
146    }
147
148    public void testHashCode_Success() throws Exception {
149        VerificationParams params1 = new VerificationParams(VERIFICATION_URI, ORIGINATING_URI,
150                REFERRER, ORIGINATING_UID, MANIFEST_DIGEST);
151
152        VerificationParams params2 = new VerificationParams(
153                Uri.parse(VERIFICATION_URI_STRING), Uri.parse(ORIGINATING_URI_STRING),
154                Uri.parse(REFERRER_STRING), ORIGINATING_UID, new ManifestDigest(DIGEST_BYTES));
155
156        assertEquals(params1.hashCode(), params2.hashCode());
157    }
158
159    public void testHashCode_VerificationUri_Failure() throws Exception {
160        VerificationParams params1 = new VerificationParams(VERIFICATION_URI, ORIGINATING_URI,
161                REFERRER, ORIGINATING_UID, MANIFEST_DIGEST);
162
163        VerificationParams params2 = new VerificationParams(null, Uri.parse(ORIGINATING_URI_STRING),
164                Uri.parse(REFERRER_STRING), ORIGINATING_UID, new ManifestDigest(DIGEST_BYTES));
165
166        assertFalse(params1.hashCode() == params2.hashCode());
167    }
168
169    public void testHashCode_OriginatingUri_Failure() throws Exception {
170        VerificationParams params1 = new VerificationParams(VERIFICATION_URI, ORIGINATING_URI,
171                REFERRER, ORIGINATING_UID, MANIFEST_DIGEST);
172
173        VerificationParams params2 = new VerificationParams(
174                Uri.parse(VERIFICATION_URI_STRING), Uri.parse("http://a.different.uri/"),
175                Uri.parse(REFERRER_STRING), ORIGINATING_UID, new ManifestDigest(DIGEST_BYTES));
176
177        assertFalse(params1.hashCode() == params2.hashCode());
178    }
179
180    public void testHashCode_Referrer_Failure() throws Exception {
181        VerificationParams params1 = new VerificationParams(VERIFICATION_URI, ORIGINATING_URI,
182                REFERRER, ORIGINATING_UID, MANIFEST_DIGEST);
183
184        VerificationParams params2 = new VerificationParams(
185                Uri.parse(VERIFICATION_URI_STRING), Uri.parse(ORIGINATING_URI_STRING), null,
186                ORIGINATING_UID, new ManifestDigest(DIGEST_BYTES));
187
188        assertFalse(params1.hashCode() == params2.hashCode());
189    }
190
191    public void testHashCode_Originating_Uid_Failure() throws Exception {
192        VerificationParams params1 = new VerificationParams(VERIFICATION_URI, ORIGINATING_URI,
193                REFERRER, ORIGINATING_UID, MANIFEST_DIGEST);
194
195        VerificationParams params2 = new VerificationParams(
196                Uri.parse(VERIFICATION_URI_STRING), Uri.parse(ORIGINATING_URI_STRING),
197                Uri.parse(REFERRER_STRING), 12345, new ManifestDigest(DIGEST_BYTES));
198
199        assertFalse(params1.hashCode() == params2.hashCode());
200    }
201
202    public void testHashCode_ManifestDigest_Failure() throws Exception {
203        VerificationParams params1 = new VerificationParams(VERIFICATION_URI, ORIGINATING_URI,
204                REFERRER, ORIGINATING_UID, MANIFEST_DIGEST);
205
206        VerificationParams params2 = new VerificationParams(
207                Uri.parse(VERIFICATION_URI_STRING), Uri.parse(ORIGINATING_URI_STRING),
208                Uri.parse(REFERRER_STRING), ORIGINATING_UID,
209                new ManifestDigest("a different digest".getBytes()));
210
211        assertFalse(params1.hashCode() == params2.hashCode());
212    }
213
214    public void testHashCode_InstallerUid_Failure() throws Exception {
215        VerificationParams params1 = new VerificationParams(VERIFICATION_URI, ORIGINATING_URI,
216                REFERRER, ORIGINATING_UID, MANIFEST_DIGEST);
217
218        VerificationParams params2 = new VerificationParams(
219                Uri.parse(VERIFICATION_URI_STRING), Uri.parse(ORIGINATING_URI_STRING),
220                Uri.parse(REFERRER_STRING), ORIGINATING_UID,
221                new ManifestDigest("a different digest".getBytes()));
222        params2.setInstallerUid(INSTALLER_UID);
223
224        assertFalse(params1.hashCode() == params2.hashCode());
225    }
226}
227