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

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

Issue 1971773002: Upstream: Add WebAPK's client library. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: yfriedman@'s comments. Created 4 years, 7 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 2016 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.assertEquals;
8 import static org.junit.Assert.assertFalse;
9 import static org.junit.Assert.assertNull;
10 import static org.junit.Assert.assertTrue;
11 import static org.mockito.Mockito.mock;
12 import static org.mockito.Mockito.when;
13
14 import android.content.Context;
15 import android.content.Intent;
16 import android.content.pm.ActivityInfo;
17 import android.content.pm.PackageInfo;
18 import android.content.pm.PackageManager;
19 import android.content.pm.PackageManager.NameNotFoundException;
20 import android.content.pm.ResolveInfo;
21 import android.content.pm.Signature;
22
23 import org.chromium.testing.local.LocalRobolectricTestRunner;
24 import org.junit.Assert;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 import org.robolectric.Robolectric;
29 import org.robolectric.annotation.Config;
30 import org.robolectric.res.builder.RobolectricPackageManager;
31
32 import java.net.URISyntaxException;
33 import java.util.ArrayList;
34 import java.util.List;
35
36 /**
37 * Unit tests for {@link org.chromium.webapk.lib.client.WebApkValidator}.
38 */
39 @RunWith(LocalRobolectricTestRunner.class)
40 @Config(manifest = Config.NONE)
41 public class WebApkValidatorTest {
42 private static final String WEBAPK_PACKAGE_NAME = "org.chromium.webapk.foo";
43 private static final String URL_OF_WEBAPK = "https://www.foo.com";
44 private static final String URL_WITHOUT_WEBAPK = "https://www.other.com";
45
46 private static final byte[] EXPECTED_SIGNATURE = new byte[] {
47 48, -126, 3, -121, 48, -126, 2, 111, -96, 3, 2, 1, 2, 2, 4, 20, -104, -6 6, -126, 48, 13,
48 6, 9, 42, -122, 72, -122, -9, 13, 1, 1, 11, 5, 0, 48, 116, 49, 11, 48, 9 , 6, 3, 85, 4,
49 6, 19, 2, 67, 65, 49, 16, 48, 14, 6, 3, 85, 4, 8, 19, 7, 79, 110, 116, 9 7, 114, 105,
50 111, 49, 17, 48, 15, 6, 3, 85, 4, 7, 19, 8, 87, 97, 116, 101, 114, 108, 111, 111, 49,
51 17, 48, 15, 6, 3, 85, 4, 10, 19, 8, 67, 104, 114, 111, 109, 105, 117, 10 9, 49, 17, 48};
52
53 private static final byte[] SIGNATURE_1 = new byte[] {
54 13, 52, 51, 48, 51, 48, 51, 49, 53, 49, 54, 52, 52, 90, 48, 116, 49, 11, 48, 9, 6, 3,
55 85, 4, 6, 19, 2, 67, 65, 49, 16, 48, 14, 6, 3, 85, 4, 8, 19, 7, 79, 110, 116, 97, 114};
56
57 private static final byte[] SIGNATURE_2 = new byte[] {
58 49, 17, 48, 15, 6, 3, 85, 4, 10, 19, 8, 67, 104, 114, 111, 109, 105, 117 , 109, 49, 17,
59 48, 15, 6, 3, 85, 4, 11, 19, 8, 67, 104, 114, 111, 109, 105, 117, 109, 4 9, 26, 48, 24};
60
61 private Context mContext;
62 private PackageManager mPackageManager;
63 private PackageInfo mWebApkPackageInfo;
64 private PackageInfo mInvalidWebApkPackageInfo;
65
66 @Before
67 public void setUp() {
68 mContext = mock(Context.class);
69 mPackageManager = mock(PackageManager.class);
70 when(mContext.getPackageManager()).thenReturn(mPackageManager);
71 WebApkValidator.initWithBrowserHostSignature(EXPECTED_SIGNATURE);
72 }
73
74 /**
75 * Tests {@link WebApkValidator.queryWebApkPackage()} returns a WebAPK's pac kage name if the
76 * WebAPK can handle the given URL.
77 */
78 @Test
79 public void testQueryWebApkPackageReturnsWebApkPackageNameIfTheURLCanBeHandl ed() {
80 try {
81 Intent intent = Intent.parseUri(URL_OF_WEBAPK, Intent.URI_INTENT_SCH EME);
82 intent.addCategory(Intent.CATEGORY_BROWSABLE);
83
84 ResolveInfo info = newResolveInfo(WEBAPK_PACKAGE_NAME);
85 RobolectricPackageManager packageManager =
86 (RobolectricPackageManager) Robolectric.application.getPacka geManager();
87 packageManager.addResolveInfoForIntent(intent, info);
88
89 assertEquals(WEBAPK_PACKAGE_NAME,
90 WebApkValidator.queryWebApkPackage(Robolectric.application, URL_OF_WEBAPK));
91 } catch (URISyntaxException e) {
92 Assert.fail("URI is invalid.");
93 }
94 }
95
96 /**
97 * Tests {@link WebApkValidator.queryWebApkPackage()} returns null for a non -browsable Intent.
98 */
99 @Test
100 public void testQueryWebApkPackageReturnsNullForNonBrowsableIntent() {
101 try {
102 Intent intent = Intent.parseUri(URL_OF_WEBAPK, Intent.URI_INTENT_SCH EME);
103
104 ResolveInfo info = newResolveInfo(WEBAPK_PACKAGE_NAME);
105 RobolectricPackageManager packageManager =
106 (RobolectricPackageManager) Robolectric.application.getPacka geManager();
107 packageManager.addResolveInfoForIntent(intent, info);
108
109 assertNull(WebApkValidator.queryWebApkPackage(
110 Robolectric.application, URL_OF_WEBAPK));
111 } catch (URISyntaxException e) {
112 Assert.fail("URI is invalid.");
113 }
114 }
115
116 /**
117 * Tests {@link WebApkValidator.queryWebApkPackage()} returns null if no Web APK handles
118 * the given URL.
119 */
120 @Test
121 public void testQueryWebApkPackageReturnsNullWhenNoWebApkHandlesTheURL() {
122 try {
123 Intent intent = Intent.parseUri(URL_OF_WEBAPK, Intent.URI_INTENT_SCH EME);
124 intent.addCategory(Intent.CATEGORY_BROWSABLE);
125
126 ResolveInfo info = newResolveInfo(WEBAPK_PACKAGE_NAME);
127 RobolectricPackageManager packageManager =
128 (RobolectricPackageManager) Robolectric.application.getPacka geManager();
129 packageManager.addResolveInfoForIntent(intent, info);
130
131 assertNull(WebApkValidator.queryWebApkPackage(
132 Robolectric.application, URL_WITHOUT_WEBAPK));
133 } catch (URISyntaxException e) {
134 Assert.fail("URI is invalid.");
135 }
136 }
137
138 /**
139 * Tests {@link WebApkValidator.findWebApkPackage()} returns a WebAPK's pack age name when there
140 * are ResolveInfos corresponds to a WebAPK.
141 */
142 @Test
143 public void testFindWebApkPackageReturnsWebApkPackageName() {
144 List<ResolveInfo> infos = new ArrayList<ResolveInfo>();
145 infos.add(newResolveInfo(WEBAPK_PACKAGE_NAME));
146 assertEquals(WEBAPK_PACKAGE_NAME, WebApkValidator.findWebApkPackage(info s));
147 }
148
149 /**
150 * Tests {@link WebApkValidator.findWebApkPackage()} returns null when there isn't any
151 * ResolveInfos corresponds to a WebAPK.
152 */
153 @Test
154 public void testFindWebApkPackageReturnsNullWhenNoResolveInfosCorrespondingT oWebApk() {
155 List<ResolveInfo> infos = new ArrayList<ResolveInfo>();
156 infos.add(newResolveInfo("com.google.android"));
157 assertNull(WebApkValidator.findWebApkPackage(infos));
158 }
159
160 /**
161 * Tests {@link WebApkValidator.IsValidWebApk} returns true if the WebAPK ha s the expected
162 * signature.
163 */
164 @Test
165 public void testIsValidWebApkReturnsTrueForValidWebApk() throws NameNotFound Exception {
166 mWebApkPackageInfo = mock(PackageInfo.class);
167 when(mPackageManager.getPackageInfo(WEBAPK_PACKAGE_NAME, PackageManager. GET_SIGNATURES))
168 .thenReturn(mWebApkPackageInfo);
169 mWebApkPackageInfo.signatures = new Signature[] {new Signature(EXPECTED_ SIGNATURE)};
170
171 assertTrue(WebApkValidator.isValidWebApk(mContext, WEBAPK_PACKAGE_NAME)) ;
172 }
173
174 /**
175 * Tests {@link WebApkValidator.IsValidWebApk} returns true if a WebAPK has multiple
176 * signatures and one matches the expected signature.
177 */
178 @Test
179 public void testIsValidWebApkReturnsTrueForWebApkWithMultipleSignaturesAndOn eMatched()
180 throws NameNotFoundException {
181 mWebApkPackageInfo = mock(PackageInfo.class);
182 when(mPackageManager.getPackageInfo(WEBAPK_PACKAGE_NAME, PackageManager. GET_SIGNATURES))
183 .thenReturn(mWebApkPackageInfo);
184 mWebApkPackageInfo.signatures = new Signature[] {new Signature(SIGNATURE _1),
185 new Signature(SIGNATURE_2), new Signature(EXPECTED_SIGNATURE)};
186
187 assertTrue(WebApkValidator.isValidWebApk(mContext, WEBAPK_PACKAGE_NAME)) ;
188 }
189
190 /**
191 * Tests {@link WebApkValidator.IsValidWebApk} returns false if a WebAPK has multiple
192 * signatures but none of them matches the expected signature.
193 */
194 @Test
195 public void testIsValidWebApkReturnsTrueForWebApkWithMultipleSignaturesWitho utAnyMatched()
Yaron 2016/05/13 15:50:13 ReturnsFalse
Xi Han 2016/05/13 15:57:47 :(
196 throws NameNotFoundException {
197 mWebApkPackageInfo = mock(PackageInfo.class);
198 when(mPackageManager.getPackageInfo(WEBAPK_PACKAGE_NAME, PackageManager. GET_SIGNATURES))
199 .thenReturn(mWebApkPackageInfo);
200 mWebApkPackageInfo.signatures = new Signature[] {new Signature(SIGNATURE _1),
201 new Signature(SIGNATURE_2)};
202
203 assertFalse(WebApkValidator.isValidWebApk(mContext, WEBAPK_PACKAGE_NAME) );
204 }
205
206 private static ResolveInfo newResolveInfo(String packageName) {
207 ActivityInfo activityInfo = new ActivityInfo();
208 activityInfo.packageName = packageName;
209 ResolveInfo resolveInfo = new ResolveInfo();
210 resolveInfo.activityInfo = activityInfo;
211 return resolveInfo;
212 }
213 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698