VerificationParamsTest.java revision 706e8ba26bf0de19ad5f736516dae40c4c88c2d7
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 38 private final static Uri VERIFICATION_URI = Uri.parse(VERIFICATION_URI_STRING); 39 private final static Uri ORIGINATING_URI = Uri.parse(ORIGINATING_URI_STRING); 40 private final static Uri REFERRER = Uri.parse(REFERRER_STRING); 41 42 private final static ManifestDigest MANIFEST_DIGEST = new ManifestDigest(DIGEST_BYTES); 43 44 public void testParcel() throws Exception { 45 VerificationParams expected = new VerificationParams(VERIFICATION_URI, ORIGINATING_URI, 46 REFERRER, MANIFEST_DIGEST); 47 48 Parcel parcel = Parcel.obtain(); 49 expected.writeToParcel(parcel, 0); 50 parcel.setDataPosition(0); 51 52 VerificationParams actual = VerificationParams.CREATOR.createFromParcel(parcel); 53 54 assertEquals(VERIFICATION_URI, actual.getVerificationURI()); 55 56 assertEquals(ORIGINATING_URI, actual.getOriginatingURI()); 57 58 assertEquals(REFERRER, actual.getReferrer()); 59 60 assertEquals(MANIFEST_DIGEST, actual.getManifestDigest()); 61 } 62 63 public void testEquals_Success() throws Exception { 64 VerificationParams params1 = new VerificationParams(VERIFICATION_URI, ORIGINATING_URI, 65 REFERRER, MANIFEST_DIGEST); 66 67 VerificationParams params2 = new VerificationParams( 68 Uri.parse(VERIFICATION_URI_STRING), Uri.parse(ORIGINATING_URI_STRING), 69 Uri.parse(REFERRER_STRING), new ManifestDigest(DIGEST_BYTES)); 70 71 assertEquals(params1, params2); 72 } 73 74 public void testEquals_VerificationUri_Failure() throws Exception { 75 VerificationParams params1 = new VerificationParams(VERIFICATION_URI, ORIGINATING_URI, 76 REFERRER, MANIFEST_DIGEST); 77 78 VerificationParams params2 = new VerificationParams( 79 Uri.parse("http://a.different.uri/"), Uri.parse(ORIGINATING_URI_STRING), 80 Uri.parse(REFERRER_STRING), new ManifestDigest(DIGEST_BYTES)); 81 82 assertFalse(params1.equals(params2)); 83 } 84 85 public void testEquals_OriginatingUri_Failure() throws Exception { 86 VerificationParams params1 = new VerificationParams(VERIFICATION_URI, ORIGINATING_URI, 87 REFERRER, MANIFEST_DIGEST); 88 89 VerificationParams params2 = new VerificationParams( 90 Uri.parse(VERIFICATION_URI_STRING), Uri.parse("http://a.different.uri/"), 91 Uri.parse(REFERRER_STRING), new ManifestDigest(DIGEST_BYTES)); 92 93 assertFalse(params1.equals(params2)); 94 } 95 96 public void testEquals_Referrer_Failure() throws Exception { 97 VerificationParams params1 = new VerificationParams(VERIFICATION_URI, ORIGINATING_URI, 98 REFERRER, MANIFEST_DIGEST); 99 100 VerificationParams params2 = new VerificationParams( 101 Uri.parse(VERIFICATION_URI_STRING), Uri.parse(ORIGINATING_URI_STRING), 102 Uri.parse("http://a.different.uri/"), new ManifestDigest(DIGEST_BYTES)); 103 104 assertFalse(params1.equals(params2)); 105 } 106 107 public void testEquals_ManifestDigest_Failure() throws Exception { 108 VerificationParams params1 = new VerificationParams(VERIFICATION_URI, ORIGINATING_URI, 109 REFERRER, MANIFEST_DIGEST); 110 111 VerificationParams params2 = new VerificationParams( 112 Uri.parse(VERIFICATION_URI_STRING), Uri.parse(ORIGINATING_URI_STRING), 113 Uri.parse(REFERRER_STRING), new ManifestDigest("a different digest".getBytes())); 114 115 assertFalse(params1.equals(params2)); 116 } 117 118 public void testHashCode_Success() throws Exception { 119 VerificationParams params1 = new VerificationParams(VERIFICATION_URI, ORIGINATING_URI, 120 REFERRER, MANIFEST_DIGEST); 121 122 VerificationParams params2 = new VerificationParams( 123 Uri.parse(VERIFICATION_URI_STRING), Uri.parse(ORIGINATING_URI_STRING), 124 Uri.parse(REFERRER_STRING), new ManifestDigest(DIGEST_BYTES)); 125 126 assertEquals(params1.hashCode(), params2.hashCode()); 127 } 128 129 public void testHashCode_VerificationUri_Failure() throws Exception { 130 VerificationParams params1 = new VerificationParams(VERIFICATION_URI, ORIGINATING_URI, 131 REFERRER, MANIFEST_DIGEST); 132 133 VerificationParams params2 = new VerificationParams(null, Uri.parse(ORIGINATING_URI_STRING), 134 Uri.parse(REFERRER_STRING), new ManifestDigest(DIGEST_BYTES)); 135 136 assertFalse(params1.hashCode() == params2.hashCode()); 137 } 138 139 public void testHashCode_OriginatingUri_Failure() throws Exception { 140 VerificationParams params1 = new VerificationParams(VERIFICATION_URI, ORIGINATING_URI, 141 REFERRER, MANIFEST_DIGEST); 142 143 VerificationParams params2 = new VerificationParams( 144 Uri.parse(VERIFICATION_URI_STRING), Uri.parse("http://a.different.uri/"), 145 Uri.parse(REFERRER_STRING), new ManifestDigest(DIGEST_BYTES)); 146 147 assertFalse(params1.hashCode() == params2.hashCode()); 148 } 149 150 public void testHashCode_Referrer_Failure() throws Exception { 151 VerificationParams params1 = new VerificationParams(VERIFICATION_URI, ORIGINATING_URI, 152 REFERRER, MANIFEST_DIGEST); 153 154 VerificationParams params2 = new VerificationParams( 155 Uri.parse(VERIFICATION_URI_STRING), Uri.parse(ORIGINATING_URI_STRING), null, 156 new ManifestDigest(DIGEST_BYTES)); 157 158 assertFalse(params1.hashCode() == params2.hashCode()); 159 } 160 161 public void testHashCode_ManifestDigest_Failure() throws Exception { 162 VerificationParams params1 = new VerificationParams(VERIFICATION_URI, ORIGINATING_URI, 163 REFERRER, MANIFEST_DIGEST); 164 165 VerificationParams params2 = new VerificationParams( 166 Uri.parse(VERIFICATION_URI_STRING), Uri.parse(ORIGINATING_URI_STRING), 167 Uri.parse(REFERRER_STRING), new ManifestDigest("a different digest".getBytes())); 168 169 assertFalse(params1.hashCode() == params2.hashCode()); 170 } 171} 172