blob: 51bad9af23efb3711acf01a5bce0eb803c3b1bcf [file] [log] [blame]
Mark Chien09ad80f2020-04-07 16:17:49 +00001/*
2 * Copyright (C) 2020 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
markchien503be612020-04-12 21:41:29 +080017package com.android.networkstack.tethering;
Mark Chien09ad80f2020-04-07 16:17:49 +000018
19import static android.net.TetheringManager.TETHERING_WIFI;
20import static android.net.TetheringManager.TETHER_ERROR_NO_ERROR;
21
22import static org.junit.Assert.assertEquals;
23import static org.mockito.ArgumentMatchers.eq;
24import static org.mockito.Mockito.verify;
25import static org.mockito.Mockito.verifyNoMoreInteractions;
26import static org.mockito.Mockito.when;
27
28import android.content.Intent;
29import android.net.IIntResultListener;
30import android.net.ITetheringConnector;
31import android.net.ITetheringEventCallback;
32import android.net.TetheringRequestParcel;
33import android.os.ResultReceiver;
34
35import androidx.test.InstrumentationRegistry;
36import androidx.test.filters.SmallTest;
37import androidx.test.rule.ServiceTestRule;
38import androidx.test.runner.AndroidJUnit4;
39
markchien503be612020-04-12 21:41:29 +080040import com.android.networkstack.tethering.MockTetheringService.MockTetheringConnector;
Mark Chien09ad80f2020-04-07 16:17:49 +000041
42import org.junit.After;
43import org.junit.Before;
44import org.junit.Rule;
45import org.junit.Test;
46import org.junit.runner.RunWith;
47import org.mockito.Mock;
48import org.mockito.MockitoAnnotations;
49
50@RunWith(AndroidJUnit4.class)
51@SmallTest
52public final class TetheringServiceTest {
53 private static final String TEST_IFACE_NAME = "test_wlan0";
54 private static final String TEST_CALLER_PKG = "test_pkg";
55 @Mock private ITetheringEventCallback mITetheringEventCallback;
56 @Rule public ServiceTestRule mServiceTestRule;
57 private Tethering mTethering;
58 private Intent mMockServiceIntent;
59 private ITetheringConnector mTetheringConnector;
60
61 private class TestTetheringResult extends IIntResultListener.Stub {
62 private int mResult = -1; // Default value that does not match any result code.
63 @Override
64 public void onResult(final int resultCode) {
65 mResult = resultCode;
66 }
67
68 public void assertResult(final int expected) {
69 assertEquals(expected, mResult);
70 }
71 }
72
73 @Before
74 public void setUp() throws Exception {
75 MockitoAnnotations.initMocks(this);
76 mServiceTestRule = new ServiceTestRule();
77 mMockServiceIntent = new Intent(
78 InstrumentationRegistry.getTargetContext(),
79 MockTetheringService.class);
80 final MockTetheringConnector mockConnector =
81 (MockTetheringConnector) mServiceTestRule.bindService(mMockServiceIntent);
82 mTetheringConnector = mockConnector.getTetheringConnector();
83 final MockTetheringService service = mockConnector.getService();
84 mTethering = service.getTethering();
85 verify(mTethering).startStateMachineUpdaters();
86 when(mTethering.hasTetherableConfiguration()).thenReturn(true);
87 }
88
89 @After
90 public void tearDown() throws Exception {
91 mServiceTestRule.unbindService();
92 }
93
94 @Test
95 public void testTether() throws Exception {
96 when(mTethering.tether(TEST_IFACE_NAME)).thenReturn(TETHER_ERROR_NO_ERROR);
97 final TestTetheringResult result = new TestTetheringResult();
98 mTetheringConnector.tether(TEST_IFACE_NAME, TEST_CALLER_PKG, result);
99 verify(mTethering).hasTetherableConfiguration();
100 verify(mTethering).tether(TEST_IFACE_NAME);
101 verifyNoMoreInteractions(mTethering);
102 result.assertResult(TETHER_ERROR_NO_ERROR);
103 }
104
105 @Test
106 public void testUntether() throws Exception {
107 when(mTethering.untether(TEST_IFACE_NAME)).thenReturn(TETHER_ERROR_NO_ERROR);
108 final TestTetheringResult result = new TestTetheringResult();
109 mTetheringConnector.untether(TEST_IFACE_NAME, TEST_CALLER_PKG, result);
110 verify(mTethering).hasTetherableConfiguration();
111 verify(mTethering).untether(TEST_IFACE_NAME);
112 verifyNoMoreInteractions(mTethering);
113 result.assertResult(TETHER_ERROR_NO_ERROR);
114 }
115
116 @Test
117 public void testSetUsbTethering() throws Exception {
118 when(mTethering.setUsbTethering(true /* enable */)).thenReturn(TETHER_ERROR_NO_ERROR);
119 final TestTetheringResult result = new TestTetheringResult();
120 mTetheringConnector.setUsbTethering(true /* enable */, TEST_CALLER_PKG, result);
121 verify(mTethering).hasTetherableConfiguration();
122 verify(mTethering).setUsbTethering(true /* enable */);
123 verifyNoMoreInteractions(mTethering);
124 result.assertResult(TETHER_ERROR_NO_ERROR);
125 }
126
127 @Test
128 public void testStartTethering() throws Exception {
129 final TestTetheringResult result = new TestTetheringResult();
130 final TetheringRequestParcel request = new TetheringRequestParcel();
131 request.tetheringType = TETHERING_WIFI;
132 mTetheringConnector.startTethering(request, TEST_CALLER_PKG, result);
133 verify(mTethering).hasTetherableConfiguration();
134 verify(mTethering).startTethering(eq(request), eq(result));
135 verifyNoMoreInteractions(mTethering);
136 }
137
138 @Test
139 public void testStopTethering() throws Exception {
140 final TestTetheringResult result = new TestTetheringResult();
141 mTetheringConnector.stopTethering(TETHERING_WIFI, TEST_CALLER_PKG, result);
142 verify(mTethering).hasTetherableConfiguration();
143 verify(mTethering).stopTethering(TETHERING_WIFI);
144 verifyNoMoreInteractions(mTethering);
145 result.assertResult(TETHER_ERROR_NO_ERROR);
146 }
147
148 @Test
149 public void testRequestLatestTetheringEntitlementResult() throws Exception {
150 final ResultReceiver result = new ResultReceiver(null);
151 mTetheringConnector.requestLatestTetheringEntitlementResult(TETHERING_WIFI, result,
152 true /* showEntitlementUi */, TEST_CALLER_PKG);
153 verify(mTethering).hasTetherableConfiguration();
154 verify(mTethering).requestLatestTetheringEntitlementResult(eq(TETHERING_WIFI),
155 eq(result), eq(true) /* showEntitlementUi */);
156 verifyNoMoreInteractions(mTethering);
157 }
158
159 @Test
160 public void testRegisterTetheringEventCallback() throws Exception {
161 mTetheringConnector.registerTetheringEventCallback(mITetheringEventCallback,
162 TEST_CALLER_PKG);
163 verify(mTethering).registerTetheringEventCallback(eq(mITetheringEventCallback));
164 verifyNoMoreInteractions(mTethering);
165 }
166
167 @Test
168 public void testUnregisterTetheringEventCallback() throws Exception {
169 mTetheringConnector.unregisterTetheringEventCallback(mITetheringEventCallback,
170 TEST_CALLER_PKG);
171 verify(mTethering).unregisterTetheringEventCallback(
172 eq(mITetheringEventCallback));
173 verifyNoMoreInteractions(mTethering);
174 }
175
176 @Test
177 public void testStopAllTethering() throws Exception {
178 final TestTetheringResult result = new TestTetheringResult();
179 mTetheringConnector.stopAllTethering(TEST_CALLER_PKG, result);
180 verify(mTethering).hasTetherableConfiguration();
181 verify(mTethering).untetherAll();
182 verifyNoMoreInteractions(mTethering);
183 result.assertResult(TETHER_ERROR_NO_ERROR);
184 }
185
186 @Test
187 public void testIsTetheringSupported() throws Exception {
188 final TestTetheringResult result = new TestTetheringResult();
189 mTetheringConnector.isTetheringSupported(TEST_CALLER_PKG, result);
190 verify(mTethering).hasTetherableConfiguration();
191 verifyNoMoreInteractions(mTethering);
192 result.assertResult(TETHER_ERROR_NO_ERROR);
193 }
194}