1 /* 2 * Copyright (C) 2019 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 17 package com.android.fastdeploy; 18 19 import static org.junit.Assert.assertEquals; 20 import static org.junit.Assert.assertNotEquals; 21 22 import androidx.test.filters.SmallTest; 23 import androidx.test.runner.AndroidJUnit4; 24 25 import org.junit.Assert; 26 import org.junit.Test; 27 import org.junit.runner.RunWith; 28 29 import com.android.fastdeploy.ApkArchive; 30 31 import java.io.File; 32 import java.io.IOException; 33 34 @SmallTest 35 @RunWith(AndroidJUnit4.class) 36 public class ApkArchiveTest { 37 private static final File SAMPLE_APK = new File("/data/local/tmp/FastDeployTests/sample.apk"); 38 private static final File WRONG_APK = new File("/data/local/tmp/FastDeployTests/sample.cd"); 39 40 @Test testApkArchiveSizes()41 public void testApkArchiveSizes() throws IOException { 42 ApkArchive archive = new ApkArchive(SAMPLE_APK); 43 44 ApkArchive.Location cdLoc = archive.getCDLocation(); 45 assertNotEquals(cdLoc, null); 46 assertEquals(cdLoc.offset, 2044145); 47 assertEquals(cdLoc.size, 49390); 48 49 // Check that block can be retrieved 50 ApkArchive.Location sigLoc = archive.getSignatureLocation(cdLoc.offset); 51 assertNotEquals(sigLoc, null); 52 assertEquals(sigLoc.offset, 2040049); 53 assertEquals(sigLoc.size, 4088); 54 } 55 56 @Test testApkArchiveDump()57 public void testApkArchiveDump() throws IOException { 58 ApkArchive archive = new ApkArchive(SAMPLE_APK); 59 60 ApkArchive.Dump dump = archive.extractMetadata(); 61 assertNotEquals(dump, null); 62 assertNotEquals(dump.cd, null); 63 assertNotEquals(dump.signature, null); 64 assertEquals(dump.cd.length, 49390); 65 assertEquals(dump.signature.length, 4088); 66 } 67 68 @Test(expected = IllegalArgumentException.class) testApkArchiveDumpWrongApk()69 public void testApkArchiveDumpWrongApk() throws IOException { 70 ApkArchive archive = new ApkArchive(WRONG_APK); 71 72 archive.extractMetadata(); 73 } 74 } 75