1 /* 2 * Copyright 2016, 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 package com.android.managedprovisioning.model; 17 18 import android.os.Bundle; 19 import android.os.Parcel; 20 import android.test.AndroidTestCase; 21 import android.test.MoreAsserts; 22 import android.test.suitebuilder.annotation.SmallTest; 23 24 import com.android.managedprovisioning.model.PackageDownloadInfo; 25 26 import junit.framework.Assert; 27 28 import java.lang.Exception; 29 30 /** Tests for {@link PackageDownloadInfo} */ 31 public class PackageDownloadInfoTest extends AndroidTestCase { 32 private static final String TEST_DOWNLOAD_LOCATION = 33 "http://example/dpc.apk"; 34 private static final String TEST_COOKIE_HEADER = 35 "Set-Cookie: sessionToken=foobar; Expires=Thu, 18 Feb 2016 23:59:59 GMT"; 36 private static final byte[] TEST_PACKAGE_CHECKSUM = new byte[] { '1', '2', '3', '4', '5' }; 37 private static final byte[] TEST_SIGNATURE_CHECKSUM = new byte[] { '5', '4', '3', '2', '1' }; 38 private static final int TEST_MIN_SUPPORT_VERSION = 7689; 39 private static final boolean TEST_CHECKSUM_SUPPORT_SHA1 = true; 40 41 @SmallTest 42 public void testBuilderWriteAndReadBack() { 43 // WHEN a PackageDownloadInfo object is constructed with some test parameters. 44 PackageDownloadInfo downloadInfo = PackageDownloadInfo.Builder.builder() 45 .setLocation(TEST_DOWNLOAD_LOCATION) 46 .setCookieHeader(TEST_COOKIE_HEADER) 47 .setPackageChecksum(TEST_PACKAGE_CHECKSUM) 48 .setSignatureChecksum(TEST_SIGNATURE_CHECKSUM) 49 .setMinVersion(TEST_MIN_SUPPORT_VERSION) 50 .build(); 51 // THEN the same set of values are obtained in the object. 52 assertEquals(TEST_DOWNLOAD_LOCATION, downloadInfo.location); 53 assertEquals(TEST_COOKIE_HEADER, downloadInfo.cookieHeader); 54 assertEquals(TEST_PACKAGE_CHECKSUM, downloadInfo.packageChecksum); 55 assertEquals(TEST_SIGNATURE_CHECKSUM, downloadInfo.signatureChecksum); 56 assertEquals(TEST_MIN_SUPPORT_VERSION, downloadInfo.minVersion); 57 } 58 59 @SmallTest 60 public void testFailToConstructPackageInfoWithDownloadLocationWithoutChecksum() { 61 // WHEN the PackageDownloadInfo is constructed with a download location but without any 62 // checksum. 63 try { 64 PackageDownloadInfo downloadInfo = PackageDownloadInfo.Builder.builder() 65 .setLocation(TEST_DOWNLOAD_LOCATION) 66 .build(); 67 fail("Checksum is mandatory."); 68 } catch (IllegalArgumentException e) { 69 // THEN PackageDownloadInfo is failed to construct due to the missing checksum. 70 } 71 } 72 73 @SmallTest 74 public void testFailToConstructPackageInfoWithoutDownloadLocation() { 75 // WHEN the PackageDownloadInfo is constructed without any download location. 76 try { 77 PackageDownloadInfo downloadInfo = PackageDownloadInfo.Builder.builder() 78 .setPackageChecksum(TEST_PACKAGE_CHECKSUM) 79 .build(); 80 fail("Download location is mandatory."); 81 } catch (IllegalArgumentException e) { 82 // THEN PackageDownloadInfo fails to construct due to the missing download location. 83 } 84 } 85 86 @SmallTest 87 public void testConstructPackageInfoWithDownloadLocationAndPackageChecksum() { 88 // WHEN the PackageDownloadInfo is constructed with a download location and a package 89 // checksum. 90 PackageDownloadInfo downloadInfo = PackageDownloadInfo.Builder.builder() 91 .setLocation(TEST_DOWNLOAD_LOCATION) 92 .setPackageChecksum(TEST_PACKAGE_CHECKSUM) 93 .build(); 94 // THEN the PackageDownloadInfo is constructed with the following values. 95 assertEquals(TEST_DOWNLOAD_LOCATION, downloadInfo.location); 96 assertEquals(TEST_PACKAGE_CHECKSUM, downloadInfo.packageChecksum); 97 } 98 99 @SmallTest 100 public void testConstructPackageInfoWithDownloadLocationAndSignatureChecksum() { 101 // WHEN the PackageDownloadInfo is constructed with a download location and a signature 102 // checksum. 103 PackageDownloadInfo downloadInfo = PackageDownloadInfo.Builder.builder() 104 .setLocation(TEST_DOWNLOAD_LOCATION) 105 .setSignatureChecksum(TEST_SIGNATURE_CHECKSUM) 106 .build(); 107 // THEN the PackageDownloadInfo is constructed with the following values. 108 assertEquals(TEST_DOWNLOAD_LOCATION, downloadInfo.location); 109 assertEquals(TEST_SIGNATURE_CHECKSUM, downloadInfo.signatureChecksum); 110 } 111 112 @SmallTest 113 public void testEquals() { 114 // GIVEN 2 PackageDownloadInfo objects are constructed with the same set of parameters. 115 PackageDownloadInfo downloadInfo1 = PackageDownloadInfo.Builder.builder() 116 .setLocation(TEST_DOWNLOAD_LOCATION) 117 .setCookieHeader(TEST_COOKIE_HEADER) 118 .setPackageChecksum(TEST_PACKAGE_CHECKSUM) 119 .setSignatureChecksum(TEST_SIGNATURE_CHECKSUM) 120 .setMinVersion(TEST_MIN_SUPPORT_VERSION) 121 .build(); 122 PackageDownloadInfo downloadInfo2 = PackageDownloadInfo.Builder.builder() 123 .setLocation(TEST_DOWNLOAD_LOCATION) 124 .setCookieHeader(TEST_COOKIE_HEADER) 125 .setPackageChecksum(TEST_PACKAGE_CHECKSUM) 126 .setSignatureChecksum(TEST_SIGNATURE_CHECKSUM) 127 .setMinVersion(TEST_MIN_SUPPORT_VERSION) 128 .build(); 129 // WHEN comparing these two objects. 130 // THEN they are equal. 131 assertEquals(downloadInfo1, downloadInfo2); 132 } 133 134 @SmallTest 135 public void testNotEquals() { 136 // GIVEN 2 PackageDownloadInfo objects are constructed with the different set of parameters. 137 PackageDownloadInfo downloadInfo1 = PackageDownloadInfo.Builder.builder() 138 .setLocation(TEST_DOWNLOAD_LOCATION) 139 .setCookieHeader(TEST_COOKIE_HEADER) 140 .setPackageChecksum(TEST_PACKAGE_CHECKSUM) 141 .setSignatureChecksum(TEST_SIGNATURE_CHECKSUM) 142 .setMinVersion(TEST_MIN_SUPPORT_VERSION) 143 .build(); 144 PackageDownloadInfo downloadInfo2 = PackageDownloadInfo.Builder.builder() 145 .setLocation("http://a/b/") 146 .setCookieHeader(TEST_COOKIE_HEADER) 147 .setPackageChecksum(TEST_PACKAGE_CHECKSUM) 148 .setSignatureChecksum(TEST_SIGNATURE_CHECKSUM) 149 .setMinVersion(TEST_MIN_SUPPORT_VERSION) 150 .build(); 151 // WHEN comparing these two objects. 152 // THEN they are not equal. 153 MoreAsserts.assertNotEqual(downloadInfo1, downloadInfo2); 154 } 155 156 @SmallTest 157 public void testParceable() { 158 // GIVEN a PackageDownloadInfo object. 159 PackageDownloadInfo expectedDownloadInfo = PackageDownloadInfo.Builder.builder() 160 .setLocation(TEST_DOWNLOAD_LOCATION) 161 .setCookieHeader(TEST_COOKIE_HEADER) 162 .setPackageChecksum(TEST_PACKAGE_CHECKSUM) 163 .setSignatureChecksum(TEST_SIGNATURE_CHECKSUM) 164 .setMinVersion(TEST_MIN_SUPPORT_VERSION) 165 .build(); 166 167 // WHEN the PackageDownloadInfo is written to parcel and then read back. 168 Parcel parcel = Parcel.obtain(); 169 expectedDownloadInfo.writeToParcel(parcel, 0); 170 parcel.setDataPosition(0); 171 PackageDownloadInfo actualDownloadInfo = 172 PackageDownloadInfo.CREATOR.createFromParcel(parcel); 173 174 // THEN the same PackageDownloadInfo is obtained. 175 assertEquals(expectedDownloadInfo, actualDownloadInfo); 176 } 177 } 178