blob: 0f72229d38e672a225535e5821eeb89920db3842 [file] [log] [blame]
markchienb6eb2c22018-07-18 14:29:20 +08001/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.connectivity.tethering;
18
19import static org.junit.Assert.assertFalse;
20import static org.junit.Assert.assertTrue;
21import static org.mockito.Matchers.anyBoolean;
22import static org.mockito.Matchers.anyString;
23import static org.mockito.Matchers.eq;
24import static org.mockito.Mockito.when;
25
26import android.content.ContentResolver;
27import android.content.Context;
28import android.content.res.Resources;
29import android.net.util.SharedLog;
30import android.os.PersistableBundle;
31import android.support.test.filters.SmallTest;
32import android.support.test.runner.AndroidJUnit4;
33import android.telephony.CarrierConfigManager;
34
35import com.android.internal.R;
36import com.android.server.connectivity.MockableSystemProperties;
37
38import org.junit.After;
39import org.junit.Before;
40import org.junit.Test;
41import org.junit.runner.RunWith;
42import org.mockito.Mock;
43import org.mockito.MockitoAnnotations;
44
45@RunWith(AndroidJUnit4.class)
46@SmallTest
47public final class EntitlementManagerTest {
48
49 private static final int EVENT_EM_UPDATE = 1;
50 private static final String[] PROVISIONING_APP_NAME = {"some", "app"};
51
52 @Mock private CarrierConfigManager mCarrierConfigManager;
53 @Mock private Context mContext;
54 @Mock private ContentResolver mContent;
55 @Mock private MockableSystemProperties mSystemProperties;
56 @Mock private Resources mResources;
57 @Mock private SharedLog mLog;
58
59 // Like so many Android system APIs, these cannot be mocked because it is marked final.
60 // We have to use the real versions.
61 private final PersistableBundle mCarrierConfig = new PersistableBundle();
62
63 private EntitlementManager mEnMgr;
64
65 @Before
66 public void setUp() {
67 MockitoAnnotations.initMocks(this);
68
69 when(mContext.getResources()).thenReturn(mResources);
70 when(mContext.getContentResolver()).thenReturn(mContent);
71 when(mResources.getStringArray(R.array.config_tether_dhcp_range))
72 .thenReturn(new String[0]);
73 when(mResources.getStringArray(R.array.config_tether_usb_regexs))
74 .thenReturn(new String[0]);
75 when(mResources.getStringArray(R.array.config_tether_wifi_regexs))
76 .thenReturn(new String[0]);
77 when(mResources.getStringArray(R.array.config_tether_bluetooth_regexs))
78 .thenReturn(new String[0]);
79 when(mResources.getIntArray(R.array.config_tether_upstream_types))
80 .thenReturn(new int[0]);
81 when(mLog.forSubComponent(anyString())).thenReturn(mLog);
82
83 mEnMgr = new EntitlementManager(mContext, mLog, mSystemProperties);
84 mEnMgr.updateConfiguration(new TetheringConfiguration(mContext, mLog));
85 }
86
87 @After
88 public void tearDown() throws Exception {}
89
90 private void setupForRequiredProvisioning() {
91 // Produce some acceptable looking provision app setting if requested.
92 when(mResources.getStringArray(R.array.config_mobile_hotspot_provision_app))
93 .thenReturn(PROVISIONING_APP_NAME);
94 // Don't disable tethering provisioning unless requested.
95 when(mSystemProperties.getBoolean(eq(EntitlementManager.DISABLE_PROVISIONING_SYSPROP_KEY),
96 anyBoolean())).thenReturn(false);
97 // Act like the CarrierConfigManager is present and ready unless told otherwise.
98 when(mContext.getSystemService(Context.CARRIER_CONFIG_SERVICE))
99 .thenReturn(mCarrierConfigManager);
100 when(mCarrierConfigManager.getConfig()).thenReturn(mCarrierConfig);
101 mCarrierConfig.putBoolean(CarrierConfigManager.KEY_REQUIRE_ENTITLEMENT_CHECKS_BOOL, true);
102 }
103
104 @Test
105 public void canRequireProvisioning() {
106 setupForRequiredProvisioning();
107 mEnMgr.updateConfiguration(new TetheringConfiguration(mContext, mLog));
108 assertTrue(mEnMgr.isTetherProvisioningRequired());
109 }
110
111 @Test
112 public void toleratesCarrierConfigManagerMissing() {
113 setupForRequiredProvisioning();
114 when(mContext.getSystemService(Context.CARRIER_CONFIG_SERVICE))
115 .thenReturn(null);
116 mEnMgr.updateConfiguration(new TetheringConfiguration(mContext, mLog));
117 // Couldn't get the CarrierConfigManager, but still had a declared provisioning app.
118 // Therefore provisioning still be required.
119 assertTrue(mEnMgr.isTetherProvisioningRequired());
120 }
121
122 @Test
123 public void toleratesCarrierConfigMissing() {
124 setupForRequiredProvisioning();
125 when(mCarrierConfigManager.getConfig()).thenReturn(null);
126 mEnMgr.updateConfiguration(new TetheringConfiguration(mContext, mLog));
127 // We still have a provisioning app configured, so still require provisioning.
128 assertTrue(mEnMgr.isTetherProvisioningRequired());
129 }
130
131 @Test
132 public void provisioningNotRequiredWhenAppNotFound() {
133 setupForRequiredProvisioning();
134 when(mResources.getStringArray(R.array.config_mobile_hotspot_provision_app))
135 .thenReturn(null);
136 mEnMgr.updateConfiguration(new TetheringConfiguration(mContext, mLog));
137 assertFalse(mEnMgr.isTetherProvisioningRequired());
138 when(mResources.getStringArray(R.array.config_mobile_hotspot_provision_app))
139 .thenReturn(new String[] {"malformedApp"});
140 mEnMgr.updateConfiguration(new TetheringConfiguration(mContext, mLog));
141 assertFalse(mEnMgr.isTetherProvisioningRequired());
142 }
143
144}