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 40 @SmallTest testBuilderWriteAndReadBack()41 public void testBuilderWriteAndReadBack() { 42 // WHEN a PackageDownloadInfo object is constructed with some test parameters. 43 PackageDownloadInfo downloadInfo = PackageDownloadInfo.Builder.builder() 44 .setLocation(TEST_DOWNLOAD_LOCATION) 45 .setCookieHeader(TEST_COOKIE_HEADER) 46 .setPackageChecksum(TEST_PACKAGE_CHECKSUM) 47 .setSignatureChecksum(TEST_SIGNATURE_CHECKSUM) 48 .setMinVersion(TEST_MIN_SUPPORT_VERSION) 49 .build(); 50 // THEN the same set of values are obtained in the object. 51 assertEquals(TEST_DOWNLOAD_LOCATION, downloadInfo.location); 52 assertEquals(TEST_COOKIE_HEADER, downloadInfo.cookieHeader); 53 assertEquals(TEST_PACKAGE_CHECKSUM, downloadInfo.packageChecksum); 54 assertEquals(TEST_SIGNATURE_CHECKSUM, downloadInfo.signatureChecksum); 55 assertEquals(TEST_MIN_SUPPORT_VERSION, downloadInfo.minVersion); 56 } 57 58 @SmallTest testFailToConstructPackageInfoWithDownloadLocationWithoutChecksum()59 public void testFailToConstructPackageInfoWithDownloadLocationWithoutChecksum() { 60 // WHEN the PackageDownloadInfo is constructed with a download location but without any 61 // checksum. 62 try { 63 PackageDownloadInfo downloadInfo = PackageDownloadInfo.Builder.builder() 64 .setLocation(TEST_DOWNLOAD_LOCATION) 65 .build(); 66 fail("Checksum is mandatory."); 67 } catch (IllegalArgumentException e) { 68 // THEN PackageDownloadInfo is failed to construct due to the missing checksum. 69 } 70 } 71 72 @SmallTest testFailToConstructPackageInfoWithoutDownloadLocation()73 public void testFailToConstructPackageInfoWithoutDownloadLocation() { 74 // WHEN the PackageDownloadInfo is constructed without any download location. 75 try { 76 PackageDownloadInfo downloadInfo = PackageDownloadInfo.Builder.builder() 77 .setPackageChecksum(TEST_PACKAGE_CHECKSUM) 78 .build(); 79 fail("Download location is mandatory."); 80 } catch (IllegalArgumentException e) { 81 // THEN PackageDownloadInfo fails to construct due to the missing download location. 82 } 83 } 84 85 @SmallTest testConstructPackageInfoWithDownloadLocationAndPackageChecksum()86 public void testConstructPackageInfoWithDownloadLocationAndPackageChecksum() { 87 // WHEN the PackageDownloadInfo is constructed with a download location and a package 88 // checksum. 89 PackageDownloadInfo downloadInfo = PackageDownloadInfo.Builder.builder() 90 .setLocation(TEST_DOWNLOAD_LOCATION) 91 .setPackageChecksum(TEST_PACKAGE_CHECKSUM) 92 .build(); 93 // THEN the PackageDownloadInfo is constructed with the following values. 94 assertEquals(TEST_DOWNLOAD_LOCATION, downloadInfo.location); 95 assertEquals(TEST_PACKAGE_CHECKSUM, downloadInfo.packageChecksum); 96 } 97 98 @SmallTest testConstructPackageInfoWithDownloadLocationAndSignatureChecksum()99 public void testConstructPackageInfoWithDownloadLocationAndSignatureChecksum() { 100 // WHEN the PackageDownloadInfo is constructed with a download location and a signature 101 // checksum. 102 PackageDownloadInfo downloadInfo = PackageDownloadInfo.Builder.builder() 103 .setLocation(TEST_DOWNLOAD_LOCATION) 104 .setSignatureChecksum(TEST_SIGNATURE_CHECKSUM) 105 .build(); 106 // THEN the PackageDownloadInfo is constructed with the following values. 107 assertEquals(TEST_DOWNLOAD_LOCATION, downloadInfo.location); 108 assertEquals(TEST_SIGNATURE_CHECKSUM, downloadInfo.signatureChecksum); 109 } 110 111 @SmallTest testEquals()112 public void testEquals() { 113 // GIVEN 2 PackageDownloadInfo objects are constructed with the same set of parameters. 114 PackageDownloadInfo downloadInfo1 = PackageDownloadInfo.Builder.builder() 115 .setLocation(TEST_DOWNLOAD_LOCATION) 116 .setCookieHeader(TEST_COOKIE_HEADER) 117 .setPackageChecksum(TEST_PACKAGE_CHECKSUM) 118 .setSignatureChecksum(TEST_SIGNATURE_CHECKSUM) 119 .setMinVersion(TEST_MIN_SUPPORT_VERSION) 120 .build(); 121 PackageDownloadInfo downloadInfo2 = PackageDownloadInfo.Builder.builder() 122 .setLocation(TEST_DOWNLOAD_LOCATION) 123 .setCookieHeader(TEST_COOKIE_HEADER) 124 .setPackageChecksum(TEST_PACKAGE_CHECKSUM) 125 .setSignatureChecksum(TEST_SIGNATURE_CHECKSUM) 126 .setMinVersion(TEST_MIN_SUPPORT_VERSION) 127 .build(); 128 // WHEN comparing these two objects. 129 // THEN they are equal. 130 assertEquals(downloadInfo1, downloadInfo2); 131 } 132 133 @SmallTest testNotEquals()134 public void testNotEquals() { 135 // GIVEN 2 PackageDownloadInfo objects are constructed with the different set of parameters. 136 PackageDownloadInfo downloadInfo1 = PackageDownloadInfo.Builder.builder() 137 .setLocation(TEST_DOWNLOAD_LOCATION) 138 .setCookieHeader(TEST_COOKIE_HEADER) 139 .setPackageChecksum(TEST_PACKAGE_CHECKSUM) 140 .setSignatureChecksum(TEST_SIGNATURE_CHECKSUM) 141 .setMinVersion(TEST_MIN_SUPPORT_VERSION) 142 .build(); 143 PackageDownloadInfo downloadInfo2 = PackageDownloadInfo.Builder.builder() 144 .setLocation("http://a/b/") 145 .setCookieHeader(TEST_COOKIE_HEADER) 146 .setPackageChecksum(TEST_PACKAGE_CHECKSUM) 147 .setSignatureChecksum(TEST_SIGNATURE_CHECKSUM) 148 .setMinVersion(TEST_MIN_SUPPORT_VERSION) 149 .build(); 150 // WHEN comparing these two objects. 151 // THEN they are not equal. 152 MoreAsserts.assertNotEqual(downloadInfo1, downloadInfo2); 153 } 154 155 @SmallTest testParceable()156 public void testParceable() { 157 // GIVEN a PackageDownloadInfo object. 158 PackageDownloadInfo expectedDownloadInfo = PackageDownloadInfo.Builder.builder() 159 .setLocation(TEST_DOWNLOAD_LOCATION) 160 .setCookieHeader(TEST_COOKIE_HEADER) 161 .setPackageChecksum(TEST_PACKAGE_CHECKSUM) 162 .setSignatureChecksum(TEST_SIGNATURE_CHECKSUM) 163 .setMinVersion(TEST_MIN_SUPPORT_VERSION) 164 .build(); 165 166 // WHEN the PackageDownloadInfo is written to parcel and then read back. 167 Parcel parcel = Parcel.obtain(); 168 expectedDownloadInfo.writeToParcel(parcel, 0); 169 parcel.setDataPosition(0); 170 PackageDownloadInfo actualDownloadInfo = 171 PackageDownloadInfo.CREATOR.createFromParcel(parcel); 172 173 // THEN the same PackageDownloadInfo is obtained. 174 assertEquals(expectedDownloadInfo, actualDownloadInfo); 175 } 176 } 177