1 /* 2 * Copyright (C) 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 17 package com.android.tools.build.apkzlib.sign; 18 19 import static org.junit.Assert.assertArrayEquals; 20 import static org.junit.Assert.assertNotNull; 21 22 import com.android.tools.build.apkzlib.utils.ApkZFileTestUtils; 23 import com.android.tools.build.apkzlib.utils.ApkZLibPair; 24 import com.android.tools.build.apkzlib.zip.AlignmentRule; 25 import com.android.tools.build.apkzlib.zip.AlignmentRules; 26 import com.android.tools.build.apkzlib.zip.StoredEntry; 27 import com.android.tools.build.apkzlib.zip.ZFile; 28 import com.android.tools.build.apkzlib.zip.ZFileOptions; 29 import com.android.tools.build.apkzlib.zip.ZFileTestConstants; 30 import java.io.ByteArrayInputStream; 31 import java.io.File; 32 import java.security.PrivateKey; 33 import java.security.cert.X509Certificate; 34 import org.junit.Rule; 35 import org.junit.Test; 36 import org.junit.rules.TemporaryFolder; 37 38 /** 39 * Tests that verify APK Signature Scheme v2 signing using {@link SigningExtension}. 40 */ 41 public class FullApkSignTest { 42 43 /** 44 * Folder used for tests. 45 */ 46 @Rule 47 public TemporaryFolder mTemporaryFolder = new TemporaryFolder(); 48 49 @Test testSignature()50 public void testSignature() throws Exception { 51 File out = new File(mTemporaryFolder.getRoot(), "apk"); 52 53 ApkZLibPair<PrivateKey, X509Certificate> signData = 54 SignatureTestUtils.generateSignaturePre18(); 55 56 // The byte arrays below are larger when compressed, so we end up storing them uncompressed, 57 // which would normally cause them to be 4-aligned. Disable that, to make calculations 58 // easier. 59 ZFileOptions options = new ZFileOptions(); 60 options.setAlignmentRule(AlignmentRules.constant(AlignmentRule.NO_ALIGNMENT)); 61 62 /* 63 * Generate a signed zip. 64 */ 65 ZFile zf = new ZFile(out, options); 66 new SigningExtension(13, signData.v2, signData.v1, false, true) 67 .register(zf); 68 String f1Name = "abc"; 69 byte[] f1Data = new byte[] { 1, 1, 1, 1 }; 70 zf.add(f1Name, new ByteArrayInputStream(f1Data)); 71 String f2Name = "defg"; 72 byte[] f2Data = new byte[] { 2, 2, 2, 2, 3, 3, 3, 3}; 73 zf.add(f2Name, new ByteArrayInputStream(f2Data)); 74 zf.close(); 75 76 /* 77 * We should see the data in place. 78 */ 79 int f1DataStart = ZFileTestConstants.LOCAL_HEADER_SIZE + f1Name.length(); 80 int f1DataEnd = f1DataStart + f1Data.length; 81 int f2DataStart = f1DataEnd + ZFileTestConstants.LOCAL_HEADER_SIZE + f2Name.length(); 82 int f2DataEnd = f2DataStart + f2Data.length; 83 84 byte[] read1 = ApkZFileTestUtils.readSegment(out, f1DataStart, f1Data.length); 85 assertArrayEquals(f1Data, read1); 86 byte[] read2 = ApkZFileTestUtils.readSegment(out, f2DataStart, f2Data.length); 87 assertArrayEquals(f2Data, read2); 88 89 /* 90 * Read the signed zip. 91 */ 92 ZFile zf2 = new ZFile(out); 93 94 StoredEntry se1 = zf2.get(f1Name); 95 assertNotNull(se1); 96 assertArrayEquals(f1Data, se1.read()); 97 98 StoredEntry se2 = zf2.get(f2Name); 99 assertNotNull(se2); 100 assertArrayEquals(f2Data, se2.read()); 101 102 zf2.close(); 103 } 104 } 105