Chromium Code Reviews| Index: chrome/android/webapk/libs/client/junit/src/org/chromium/webapk/lib/client/WebApkVerifySignatureTest.java |
| diff --git a/chrome/android/webapk/libs/client/junit/src/org/chromium/webapk/lib/client/WebApkVerifySignatureTest.java b/chrome/android/webapk/libs/client/junit/src/org/chromium/webapk/lib/client/WebApkVerifySignatureTest.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..242ca95e99570f1d26b1eac5d424a65c0a0784f7 |
| --- /dev/null |
| +++ b/chrome/android/webapk/libs/client/junit/src/org/chromium/webapk/lib/client/WebApkVerifySignatureTest.java |
| @@ -0,0 +1,139 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package org.chromium.webapk.lib.client; |
| + |
| +import static org.junit.Assert.assertArrayEquals; |
| +import static org.junit.Assert.assertEquals; |
| +import static org.junit.Assert.assertTrue; |
| + |
| +import org.junit.Test; |
| +import org.junit.runner.RunWith; |
| +import org.robolectric.annotation.Config; |
| + |
| +import org.chromium.testing.local.LocalRobolectricTestRunner; |
| +import org.chromium.testing.local.TestDir; |
| + |
| +import java.io.RandomAccessFile; |
| +import java.nio.MappedByteBuffer; |
| +import java.nio.channels.FileChannel; |
| +import java.nio.file.Files; |
| +import java.nio.file.Paths; |
| +import java.security.KeyFactory; |
| +import java.security.PublicKey; |
| +import java.security.SignatureException; |
| +import java.security.spec.X509EncodedKeySpec; |
| + |
| +/** Unit tests for WebApkVerifySignature for Android. */ |
| +@RunWith(LocalRobolectricTestRunner.class) |
| +@Config(manifest = Config.NONE) |
| +public class WebApkVerifySignatureTest { |
| + /** Elliptical Curves, Digital Signature Algorithm */ |
| + private static final String KEY_FACTORY = "EC"; |
| + |
| + private static final String TEST_DATA_DIR = "/webapks/"; |
| + |
| + static PublicKey readPublicKey(String publicDER) throws Exception { |
| + return createPublicKey(Files.readAllBytes(Paths.get(publicDER))); |
| + } |
| + |
| + private static PublicKey createPublicKey(byte[] bytes) throws Exception { |
| + return KeyFactory.getInstance(KEY_FACTORY).generatePublic(new X509EncodedKeySpec(bytes)); |
| + } |
| + |
| + @Test |
| + public void testHexToBytes() throws Exception { |
| + assertEquals(null, WebApkVerifySignature.hexToBytes("")); |
| + byte[] test = {(byte) 0xFF, (byte) 0xFE, 0x00, 0x01}; |
| + assertArrayEquals(test, WebApkVerifySignature.hexToBytes("fffe0001")); |
| + assertArrayEquals(test, WebApkVerifySignature.hexToBytes("FFFE0001")); |
| + assertEquals(null, WebApkVerifySignature.hexToBytes("f")); // Odd number of nibbles. |
| + } |
| + |
| + @Test |
| + public void testCommentHash() throws Exception { |
| + byte[] bytes = hexStringToByteArray("decafbad"); |
|
pkotwicz
2017/03/31 03:59:24
Can you initialize bytes with the result of hexStr
ScottK
2017/04/03 17:44:18
Done.
|
| + assertEquals(null, WebApkVerifySignature.parseCommentSignature("weapk:decafbad")); |
| + assertEquals(null, WebApkVerifySignature.parseCommentSignature("webapk:")); |
| + assertEquals(null, WebApkVerifySignature.parseCommentSignature("webapk:decafbad")); |
| + assertArrayEquals( |
| + bytes, WebApkVerifySignature.parseCommentSignature("webapk:12345:decafbad")); |
| + assertArrayEquals( |
| + bytes, WebApkVerifySignature.parseCommentSignature("XXXwebapk:0000:decafbadXXX")); |
| + assertArrayEquals( |
| + bytes, WebApkVerifySignature.parseCommentSignature("\n\nwebapk:0000:decafbad\n\n")); |
| + assertArrayEquals(bytes, |
| + WebApkVerifySignature.parseCommentSignature("chrome-webapk:000:decafbad\n\n")); |
| + assertArrayEquals(bytes, |
| + WebApkVerifySignature.parseCommentSignature( |
| + "prefixed: chrome-webapk:000:decafbad :suffixed")); |
| + } |
| + |
| + public static String testFilename(String fname) { |
|
pkotwicz
2017/03/31 03:59:24
testFilename() -> testFilePath()
ScottK
2017/04/03 17:44:18
Done.
|
| + return TestDir.getTestFilePath(TEST_DATA_DIR + fname); |
| + } |
| + |
| + @Test |
| + public void testVerifyFiles() throws Exception { |
|
pkotwicz
2017/03/31 03:59:24
Are the testVerifyFiles() and the testBadVerifyFil
ScottK
2017/04/03 17:44:18
They increase our code coverage by testing things
pkotwicz
2017/04/04 18:19:36
I don't think that it increases code coverage. Wha
ScottK
2017/04/04 21:03:02
Sorry, my mistake, it's the other way around - Web
pkotwicz
2017/04/05 02:54:28
The main concern is preserving the hackability of
ScottK
2017/04/05 20:14:27
Ok, removing this.
|
| + PublicKey pub = readPublicKey(testFilename("public.der")); |
| + String[] filenames = {"example.apk", "java-example.apk"}; |
| + for (String filename : filenames) { |
| + RandomAccessFile file = new RandomAccessFile(testFilename(filename), "r"); |
| + FileChannel inChannel = file.getChannel(); |
| + MappedByteBuffer buf = |
| + inChannel.map(FileChannel.MapMode.READ_ONLY, 0, inChannel.size()); |
| + buf.load(); |
| + WebApkVerifySignature v = new WebApkVerifySignature(buf); |
| + assertEquals("for file " + filename, WebApkVerifySignature.ERROR_OK, v.read()); |
| + assertEquals( |
| + "for file " + filename, WebApkVerifySignature.ERROR_OK, v.verifySignature(pub)); |
| + buf.clear(); |
| + inChannel.close(); |
| + file.close(); |
| + } |
| + } |
| + |
| + @Test |
| + public void testBadVerifyFiles() throws Exception { |
| + PublicKey pub = readPublicKey(testFilename("public.der")); |
| + String[] filenames = { |
| + "bad-sig.apk", "bad-utf8-fname.apk", "empty.apk", "extra-len-too-large.apk", |
| + "fcomment-too-large.apk", "no-cd.apk", "no-comment.apk", "no-eocd.apk", |
| + "no-lfh.apk", "not-an.apk", "too-many-metainf.apk", "truncated.apk", "zeros.apk", |
| + "zeros-at-end.apk", |
| + }; |
| + for (String filename : filenames) { |
| + RandomAccessFile file = new RandomAccessFile(testFilename(filename), "r"); |
| + FileChannel inChannel = file.getChannel(); |
| + MappedByteBuffer buf = |
| + inChannel.map(FileChannel.MapMode.READ_ONLY, 0, inChannel.size()); |
| + buf.load(); |
| + WebApkVerifySignature v = new WebApkVerifySignature(buf); |
| + try { |
| + if (v.read() == WebApkVerifySignature.ERROR_OK) { |
| + assertTrue(v.verifySignature(pub) != WebApkVerifySignature.ERROR_OK); |
| + } |
| + } catch (SignatureException | IllegalArgumentException e) { |
| + // expected |
| + } |
| + buf.clear(); |
| + inChannel.close(); |
| + file.close(); |
| + } |
| + } |
| + |
| + private static byte[] hexStringToByteArray(String s) { |
| + int len = s.length(); |
| + if (len % 2 == 1) { |
| + // Expect an even number of nibbles. |
| + return null; |
| + } |
| + byte[] data = new byte[len / 2]; |
| + for (int i = 0; i < len; i += 2) { |
| + data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) |
| + | Character.digit(s.charAt(i + 1), 16)); |
| + } |
| + return data; |
| + } |
| +} |