OLD | NEW |
| (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.runtime_library; | |
6 | |
7 import static org.junit.Assert.assertFalse; | |
8 import static org.junit.Assert.assertTrue; | |
9 import static org.mockito.Mockito.mock; | |
10 import static org.mockito.Mockito.when; | |
11 | |
12 import android.content.pm.PackageManager; | |
13 import android.os.Bundle; | |
14 import android.os.RemoteException; | |
15 | |
16 import org.chromium.testing.local.LocalRobolectricTestRunner; | |
17 import org.junit.Assert; | |
18 import org.junit.Before; | |
19 import org.junit.Test; | |
20 import org.junit.runner.RunWith; | |
21 import org.robolectric.Robolectric; | |
22 import org.robolectric.annotation.Config; | |
23 | |
24 /** | |
25 * Unit tests for {@link org.chromium.webapk.WebApkServiceImpl}. | |
26 */ | |
27 @RunWith(LocalRobolectricTestRunner.class) | |
28 @Config(manifest = Config.NONE) | |
29 public class WebApkServiceImplTest { | |
30 | |
31 private static final String ALLOWED_PACKAGE = "com.allowed.package"; | |
32 private static final int ALLOWED_UID = 0; | |
33 | |
34 private PackageManager mPackageManager; | |
35 | |
36 @Before | |
37 public void setUp() { | |
38 mPackageManager = mock(PackageManager.class); | |
39 when(mPackageManager.getPackagesForUid(ALLOWED_UID)).thenReturn( | |
40 new String[] { ALLOWED_PACKAGE }); | |
41 } | |
42 | |
43 @Test | |
44 public void testAllowedCaller() { | |
45 WebApkServiceImpl service = | |
46 new WebApkServiceImpl(Robolectric.application, createBundle(ALLO
WED_PACKAGE)); | |
47 assertTrue(service.checkHasAccess(ALLOWED_UID, mPackageManager)); | |
48 } | |
49 | |
50 @Test | |
51 public void testProhibitedCaller() { | |
52 WebApkServiceImpl service = | |
53 new WebApkServiceImpl(Robolectric.application, createBundle(ALLO
WED_PACKAGE)); | |
54 assertFalse(service.checkHasAccess(ALLOWED_UID + 1, mPackageManager)); | |
55 } | |
56 | |
57 @Test | |
58 public void testOnTransactThrowsIfNoPermission() { | |
59 WebApkServiceImpl service = new WebApkServiceImpl( | |
60 Robolectric.application, createBundle(ALLOWED_PACKAGE)) { | |
61 @Override | |
62 boolean checkHasAccess(int uid, PackageManager packageManager) { | |
63 return false; | |
64 } | |
65 }; | |
66 try { | |
67 service.onTransact(0, null, null, 0); | |
68 Assert.fail("Should have thrown an exception for no access."); | |
69 } catch (RemoteException e) { | |
70 } | |
71 } | |
72 | |
73 @Test | |
74 public void testOnTransactRunsIfHasPermission() { | |
75 WebApkServiceImpl service = new WebApkServiceImpl( | |
76 Robolectric.application, createBundle(ALLOWED_PACKAGE)) { | |
77 @Override | |
78 boolean checkHasAccess(int uid, PackageManager packageManager) { | |
79 return true; | |
80 } | |
81 }; | |
82 try { | |
83 service.onTransact(0, null, null, 0); | |
84 } catch (RemoteException e) { | |
85 e.printStackTrace(); | |
86 Assert.fail("Should not have thrown an exception when permission is
granted."); | |
87 } | |
88 } | |
89 | |
90 /** | |
91 * Creates Bundle to pass to WebApkServiceImpl constructor. | |
92 * @param expectedHostBrowser | |
93 * @return The bundle | |
94 */ | |
95 private static Bundle createBundle(String expectedHostBrowser) { | |
96 Bundle bundle = new Bundle(); | |
97 bundle.putString(WebApkServiceImpl.KEY_EXPECTED_HOST_BROWSER, expectedHo
stBrowser); | |
98 return bundle; | |
99 } | |
100 } | |
OLD | NEW |