Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(709)

Side by Side Diff: chrome/android/webapk/libs/client/junit/src/org/chromium/webapk/lib/client/WebApkVerifySignatureTest.java

Issue 2772483002: Commment signed webapks working and verified. (Closed)
Patch Set: Resolve review comments. Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 package org.chromium.webapk.lib.client;
6
7 import static org.junit.Assert.assertArrayEquals;
8 import static org.junit.Assert.assertEquals;
9
10 import org.junit.Test;
11 import org.junit.runner.RunWith;
12 import org.robolectric.annotation.Config;
13
14 import org.chromium.testing.local.LocalRobolectricTestRunner;
15
16 import java.nio.file.Files;
17 import java.nio.file.Paths;
18 import java.security.KeyFactory;
19 import java.security.PublicKey;
20 import java.security.spec.X509EncodedKeySpec;
21
22 /** Unit tests for WebApkVerifySignature for Android. */
23 @RunWith(LocalRobolectricTestRunner.class)
24 @Config(manifest = Config.NONE)
25 public class WebApkVerifySignatureTest {
26 /** Elliptical Curves, Digital Signature Algorithm */
27 private static final String KEY_FACTORY = "EC";
28
29 private static final String TEST_DATA_DIR = "/webapks/";
30
31 static PublicKey readPublicKey(String publicDER) throws Exception {
pkotwicz 2017/04/06 17:58:29 This function is now unused?
ScottK 2017/04/07 21:57:08 Good catch.
32 return createPublicKey(Files.readAllBytes(Paths.get(publicDER)));
33 }
34
35 private static PublicKey createPublicKey(byte[] bytes) throws Exception {
36 return KeyFactory.getInstance(KEY_FACTORY).generatePublic(new X509Encode dKeySpec(bytes));
37 }
38
39 @Test
40 public void testHexToBytes() throws Exception {
41 byte[] empty = {};
42 assertArrayEquals(empty, WebApkVerifySignature.hexToBytes(""));
43 byte[] test = {(byte) 0xFF, (byte) 0xFE, 0x00, 0x01};
44 assertArrayEquals(test, WebApkVerifySignature.hexToBytes("fffe0001"));
45 assertArrayEquals(test, WebApkVerifySignature.hexToBytes("FFFE0001"));
46 assertEquals(null, WebApkVerifySignature.hexToBytes("f")); // Odd number of nibbles.
47 }
48
49 @Test
50 public void testCommentHash() throws Exception {
51 byte[] bytes = {(byte) 0xde, (byte) 0xca, (byte) 0xfb, (byte) 0xad};
52 assertEquals(null, WebApkVerifySignature.parseCommentSignature("weapk:de cafbad"));
53 assertEquals(null, WebApkVerifySignature.parseCommentSignature("webapk:" ));
54 assertEquals(null, WebApkVerifySignature.parseCommentSignature("webapk:d ecafbad"));
55 assertArrayEquals(
56 bytes, WebApkVerifySignature.parseCommentSignature("webapk:12345 :decafbad"));
57 assertArrayEquals(
58 bytes, WebApkVerifySignature.parseCommentSignature("XXXwebapk:00 00:decafbadXXX"));
59 assertArrayEquals(
60 bytes, WebApkVerifySignature.parseCommentSignature("\n\nwebapk:0 000:decafbad\n\n"));
61 assertArrayEquals(bytes,
62 WebApkVerifySignature.parseCommentSignature("chrome-webapk:000:d ecafbad\n\n"));
63 assertArrayEquals(bytes,
64 WebApkVerifySignature.parseCommentSignature(
65 "prefixed: chrome-webapk:000:decafbad :suffixed"));
66 }
67 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698