Skip to content
This repository has been archived by the owner on Sep 21, 2020. It is now read-only.

Commit

Permalink
fix #95: ByteArrayApkFile close input when read zip entry
Browse files Browse the repository at this point in the history
  • Loading branch information
hsiafan committed Jun 6, 2019
1 parent d12c73f commit 302e282
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 20 deletions.
4 changes: 2 additions & 2 deletions src/main/java/net/dongliu/apk/parser/ApkFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ protected List<CertificateFile> getAllCertificateData() throws IOException {
}
String name = ne.getName().toUpperCase();
if (name.endsWith(".RSA") || name.endsWith(".DSA")) {
list.add(new CertificateFile(name, Inputs.readAll(zf.getInputStream(ne))));
list.add(new CertificateFile(name, Inputs.readAllAndClose(zf.getInputStream(ne))));
}
}
return list;
Expand All @@ -61,7 +61,7 @@ public byte[] getFileData(String path) throws IOException {
}

InputStream inputStream = zf.getInputStream(entry);
return Inputs.readAll(inputStream);
return Inputs.readAllAndClose(inputStream);
}

@Override
Expand Down
20 changes: 12 additions & 8 deletions src/main/java/net/dongliu/apk/parser/utils/Inputs.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,19 @@
public class Inputs {

public static byte[] readAll(InputStream in) throws IOException {
try {
byte[] buf = new byte[1024 * 8];
try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
int len;
while ((len = in.read(buf)) != -1) {
bos.write(buf, 0, len);
}
return bos.toByteArray();
byte[] buf = new byte[1024 * 8];
try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
int len;
while ((len = in.read(buf)) != -1) {
bos.write(buf, 0, len);
}
return bos.toByteArray();
}
}

public static byte[] readAllAndClose(InputStream in) throws IOException {
try {
return readAll(in);
} finally {
in.close();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ private String getUrl(String url) throws IOException {
conn.setRequestMethod("GET");
conn.setReadTimeout(10000);
conn.setConnectTimeout(10000);
byte[] bytes = Inputs.readAll(conn.getInputStream());
byte[] bytes = Inputs.readAllAndClose(conn.getInputStream());
return new String(bytes, StandardCharsets.UTF_8);
} finally {
conn.disconnect();
Expand Down
41 changes: 41 additions & 0 deletions src/test/java/net/dongliu/apk/parser/ApkFileTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package net.dongliu.apk.parser;

import net.dongliu.apk.parser.bean.ApkMeta;
import net.dongliu.apk.parser.bean.ApkSigner;
import net.dongliu.apk.parser.bean.CertificateMeta;
import org.junit.Test;

import java.io.IOException;
import java.security.cert.CertificateException;
import java.util.List;
import java.util.Locale;

import static junit.framework.TestCase.assertEquals;

public class ApkFileTest {

@Test
public void testParserMeta() throws IOException {
String path = getClass().getClassLoader().getResource("apks/Twitter_v7.93.2.apk").getPath();
try (ApkFile apkFile = new ApkFile(path)) {
apkFile.setPreferredLocale(Locale.ENGLISH);
ApkMeta apkMeta = apkFile.getApkMeta();
assertEquals("Twitter", apkMeta.getLabel());
}
}

@Test
public void testGetSignature() throws IOException, CertificateException {
String path = getClass().getClassLoader().getResource("apks/Twitter_v7.93.2.apk").getPath();
try (ApkFile apkFile = new ApkFile(path)) {
List<ApkSigner> apkSingers = apkFile.getApkSingers();
assertEquals(1, apkSingers.size());
ApkSigner apkSigner = apkSingers.get(0);
assertEquals("META-INF/CERT.RSA", apkSigner.getPath());
List<CertificateMeta> certificateMetas = apkSigner.getCertificateMetas();
assertEquals(1, certificateMetas.size());
CertificateMeta certificateMeta = certificateMetas.get(0);
assertEquals("69ee076cc84f4d94802d61907b07525f", certificateMeta.getCertMd5());
}
}
}
48 changes: 48 additions & 0 deletions src/test/java/net/dongliu/apk/parser/ByteArrayApkFileTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package net.dongliu.apk.parser;

import net.dongliu.apk.parser.bean.ApkMeta;
import net.dongliu.apk.parser.bean.ApkSigner;
import net.dongliu.apk.parser.bean.ApkV2Signer;
import net.dongliu.apk.parser.bean.CertificateMeta;
import org.junit.Test;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.cert.CertificateException;
import java.util.List;
import java.util.Locale;

import static junit.framework.TestCase.assertEquals;

public class ByteArrayApkFileTest {

@Test
public void testParserMeta() throws IOException {
String path = getClass().getClassLoader().getResource("apks/Twitter_v7.93.2.apk").getPath();
byte[] bytes = Files.readAllBytes(Paths.get(path));
try (ByteArrayApkFile apkFile = new ByteArrayApkFile(bytes)) {
apkFile.setPreferredLocale(Locale.ENGLISH);
ApkMeta apkMeta = apkFile.getApkMeta();
assertEquals("Twitter", apkMeta.getLabel());
}
}

@Test
public void testGetSignature() throws IOException, CertificateException {
String path = getClass().getClassLoader().getResource("apks/Twitter_v7.93.2.apk").getPath();
byte[] bytes = Files.readAllBytes(Paths.get(path));
try (ByteArrayApkFile apkFile = new ByteArrayApkFile(bytes)) {
List<ApkSigner> apkSingers = apkFile.getApkSingers();
assertEquals(1, apkSingers.size());
ApkSigner apkSigner = apkSingers.get(0);
assertEquals("META-INF/CERT.RSA", apkSigner.getPath());
List<CertificateMeta> certificateMetas = apkSigner.getCertificateMetas();
assertEquals(1, certificateMetas.size());
CertificateMeta certificateMeta = certificateMetas.get(0);
assertEquals("69ee076cc84f4d94802d61907b07525f", certificateMeta.getCertMd5());

List<ApkV2Signer> apkV2Singers = apkFile.getApkV2Singers();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,13 @@

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.file.Files;
import java.security.cert.CertificateException;

import static org.junit.Assert.*;

public class ApkSignBlockParserTest {

@Test
public void parse() throws IOException, CertificateException {
byte[] bytes = Inputs.readAll(getClass().getResourceAsStream("/sign/gmail_sign_block"));
byte[] bytes = Inputs.readAllAndClose(getClass().getResourceAsStream("/sign/gmail_sign_block"));
ApkSignBlockParser parser = new ApkSignBlockParser(ByteBuffer.wrap(bytes));
parser.parse();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ public class BCCertificateParserTest {

@Test
public void parse() throws IOException, CertificateException {
byte[] data = Inputs.readAll(getClass().getResourceAsStream("/sign/63_CERT.RSA"));
byte[] data = Inputs.readAllAndClose(getClass().getResourceAsStream("/sign/63_CERT.RSA"));
CertificateParser parser = new BCCertificateParser(data);
List<CertificateMeta> certificateMetas = parser.parse();
assertEquals("SHA1WITHRSA", certificateMetas.get(0).getSignAlgorithm());


data = Inputs.readAll(getClass().getResourceAsStream("/sign/gmail_CERT.RSA"));
data = Inputs.readAllAndClose(getClass().getResourceAsStream("/sign/gmail_CERT.RSA"));
parser = new BCCertificateParser(data);
certificateMetas = parser.parse();
assertEquals(1, certificateMetas.size());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package net.dongliu.apk.parser.parser;

import net.dongliu.apk.parser.bean.CertificateMeta;
import net.dongliu.apk.parser.cert.asn1.Asn1DecodingException;
import net.dongliu.apk.parser.utils.Inputs;
import org.junit.Ignore;
import org.junit.Test;
Expand All @@ -19,15 +18,15 @@ public class JSSECertificateParserTest {
@Ignore
// issue 63
public void parseJDKFailed() throws IOException, CertificateException {
byte[] data = Inputs.readAll(getClass().getResourceAsStream("/sign/63_CERT.RSA"));
byte[] data = Inputs.readAllAndClose(getClass().getResourceAsStream("/sign/63_CERT.RSA"));
CertificateParser parser = new JSSECertificateParser(data);
List<CertificateMeta> certificateMetas = parser.parse();
assertEquals("SHA1WITHRSA", certificateMetas.get(0).getSignAlgorithm());
}

@Test
public void parseJDK() throws IOException, CertificateException {
byte[] data = Inputs.readAll(getClass().getResourceAsStream("/sign/gmail_CERT.RSA"));
byte[] data = Inputs.readAllAndClose(getClass().getResourceAsStream("/sign/gmail_CERT.RSA"));
CertificateParser parser = new JSSECertificateParser(data);
List<CertificateMeta> certificateMetas = parser.parse();
assertEquals(1, certificateMetas.size());
Expand Down
Binary file added src/test/resources/apks/Twitter_v7.93.2.apk
Binary file not shown.

0 comments on commit 302e282

Please sign in to comment.