blob: 968b3071bf1d06add1894bdbd4cc96134944f136 [file] [log] [blame]
Lorenzo Colitti7421a012013-08-20 22:51:24 +09001/*
2 * Copyright (C) 2012 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;
18
Luke Huang909b31a2019-03-16 21:21:16 +080019import static org.mockito.Mockito.doNothing;
Lorenzo Colitti7421a012013-08-20 22:51:24 +090020import static org.mockito.Mockito.doReturn;
21import static org.mockito.Mockito.mock;
22import static org.mockito.Mockito.reset;
23import static org.mockito.Mockito.timeout;
24import static org.mockito.Mockito.verify;
25import static org.mockito.Mockito.verifyNoMoreInteractions;
26
Luke Huang909b31a2019-03-16 21:21:16 +080027import android.annotation.NonNull;
Lorenzo Colittia0868002017-07-11 02:29:28 +090028import android.content.Context;
29import android.net.INetd;
Luke Huang909b31a2019-03-16 21:21:16 +080030import android.net.INetdUnsolicitedEventListener;
Lorenzo Colittia0868002017-07-11 02:29:28 +090031import android.net.LinkAddress;
Lorenzo Colittia0868002017-07-11 02:29:28 +090032import android.os.BatteryStats;
33import android.os.Binder;
34import android.os.IBinder;
Lorenzo Colittia0868002017-07-11 02:29:28 +090035import android.test.suitebuilder.annotation.SmallTest;
36
Brett Chabot1ae2aa62019-03-04 14:14:56 -080037import androidx.test.runner.AndroidJUnit4;
38
Lorenzo Colittia0868002017-07-11 02:29:28 +090039import com.android.internal.app.IBatteryStats;
40import com.android.server.NetworkManagementService.SystemServices;
41import com.android.server.net.BaseNetworkObserver;
42
Lorenzo Colittia0868002017-07-11 02:29:28 +090043import org.junit.After;
44import org.junit.Before;
45import org.junit.Test;
46import org.junit.runner.RunWith;
Luke Huang909b31a2019-03-16 21:21:16 +080047import org.mockito.ArgumentCaptor;
48import org.mockito.Captor;
Lorenzo Colittia0868002017-07-11 02:29:28 +090049import org.mockito.Mock;
50import org.mockito.MockitoAnnotations;
51
Lorenzo Colitti7421a012013-08-20 22:51:24 +090052/**
53 * Tests for {@link NetworkManagementService}.
54 */
Lorenzo Colittia0868002017-07-11 02:29:28 +090055@RunWith(AndroidJUnit4.class)
56@SmallTest
57public class NetworkManagementServiceTest {
Lorenzo Colitti7421a012013-08-20 22:51:24 +090058
Lorenzo Colitti7421a012013-08-20 22:51:24 +090059 private NetworkManagementService mNMService;
Lorenzo Colitti7421a012013-08-20 22:51:24 +090060
Lorenzo Colittia0868002017-07-11 02:29:28 +090061 @Mock private Context mContext;
62 @Mock private IBatteryStats.Stub mBatteryStatsService;
63 @Mock private INetd.Stub mNetdService;
64
Luke Huang909b31a2019-03-16 21:21:16 +080065 @NonNull
66 @Captor
67 private ArgumentCaptor<INetdUnsolicitedEventListener> mUnsolListenerCaptor;
68
Lorenzo Colittia0868002017-07-11 02:29:28 +090069 private final SystemServices mServices = new SystemServices() {
70 @Override
71 public IBinder getService(String name) {
72 switch (name) {
73 case BatteryStats.SERVICE_NAME:
74 return mBatteryStatsService;
75 default:
76 throw new UnsupportedOperationException("Unknown service " + name);
77 }
78 }
79 @Override
80 public void registerLocalService(NetworkManagementInternal nmi) {
81 }
82 @Override
83 public INetd getNetd() {
84 return mNetdService;
85 }
86 };
87
88 @Before
Lorenzo Colitti7421a012013-08-20 22:51:24 +090089 public void setUp() throws Exception {
Lorenzo Colittia0868002017-07-11 02:29:28 +090090 MockitoAnnotations.initMocks(this);
Luke Huang909b31a2019-03-16 21:21:16 +080091 doNothing().when(mNetdService)
92 .registerUnsolicitedEventListener(mUnsolListenerCaptor.capture());
Lorenzo Colitti7421a012013-08-20 22:51:24 +090093 // Start the service and wait until it connects to our socket.
Luke Huang909b31a2019-03-16 21:21:16 +080094 mNMService = NetworkManagementService.create(mContext, mServices);
Lorenzo Colitti7421a012013-08-20 22:51:24 +090095 }
96
Lorenzo Colittia0868002017-07-11 02:29:28 +090097 @After
Lorenzo Colitti7421a012013-08-20 22:51:24 +090098 public void tearDown() throws Exception {
Hugo Benichie93c5692018-01-29 14:00:44 +090099 mNMService.shutdown();
Lorenzo Colitti7421a012013-08-20 22:51:24 +0900100 }
101
102 private static <T> T expectSoon(T mock) {
Lorenzo Colittia0868002017-07-11 02:29:28 +0900103 return verify(mock, timeout(200));
Lorenzo Colitti7421a012013-08-20 22:51:24 +0900104 }
105
106 /**
107 * Tests that network observers work properly.
108 */
Lorenzo Colittia0868002017-07-11 02:29:28 +0900109 @Test
Lorenzo Colitti7421a012013-08-20 22:51:24 +0900110 public void testNetworkObservers() throws Exception {
111 BaseNetworkObserver observer = mock(BaseNetworkObserver.class);
112 doReturn(new Binder()).when(observer).asBinder(); // Used by registerObserver.
113 mNMService.registerObserver(observer);
114
115 // Forget everything that happened to the mock so far, so we can explicitly verify
116 // everything that happens and does not happen to it from now on.
Lorenzo Colitti7421a012013-08-20 22:51:24 +0900117
Luke Huang909b31a2019-03-16 21:21:16 +0800118 INetdUnsolicitedEventListener unsolListener = mUnsolListenerCaptor.getValue();
119 reset(observer);
120 // Now call unsolListener methods and ensure that the observer methods are
121 // called. After every method we expect a callback soon after; to ensure that
Lorenzo Colitti7421a012013-08-20 22:51:24 +0900122 // invalid messages don't cause any callbacks, we call verifyNoMoreInteractions at the end.
123
124 /**
125 * Interface changes.
126 */
Luke Huang909b31a2019-03-16 21:21:16 +0800127 unsolListener.onInterfaceAdded("rmnet12");
Lorenzo Colitti7421a012013-08-20 22:51:24 +0900128 expectSoon(observer).interfaceAdded("rmnet12");
129
Luke Huang909b31a2019-03-16 21:21:16 +0800130 unsolListener.onInterfaceRemoved("eth1");
Lorenzo Colitti7421a012013-08-20 22:51:24 +0900131 expectSoon(observer).interfaceRemoved("eth1");
132
Luke Huang909b31a2019-03-16 21:21:16 +0800133 unsolListener.onInterfaceChanged("clat4", true);
Lorenzo Colitti7421a012013-08-20 22:51:24 +0900134 expectSoon(observer).interfaceStatusChanged("clat4", true);
135
Luke Huang909b31a2019-03-16 21:21:16 +0800136 unsolListener.onInterfaceLinkStateChanged("rmnet0", false);
Lorenzo Colitti7421a012013-08-20 22:51:24 +0900137 expectSoon(observer).interfaceLinkStateChanged("rmnet0", false);
138
Lorenzo Colitti7421a012013-08-20 22:51:24 +0900139 /**
140 * Bandwidth control events.
141 */
Luke Huang909b31a2019-03-16 21:21:16 +0800142 unsolListener.onQuotaLimitReached("data", "rmnet_usb0");
Lorenzo Colitti7421a012013-08-20 22:51:24 +0900143 expectSoon(observer).limitReached("data", "rmnet_usb0");
144
Lorenzo Colitti7421a012013-08-20 22:51:24 +0900145 /**
146 * Interface class activity.
147 */
Luke Huang909b31a2019-03-16 21:21:16 +0800148 unsolListener.onInterfaceClassActivityChanged(true, 1, 1234, 0);
Lorenzo Colittia0868002017-07-11 02:29:28 +0900149 expectSoon(observer).interfaceClassDataActivityChanged("1", true, 1234);
Ashish Sharmaa3f828b2014-03-18 16:38:58 -0700150
Luke Huang909b31a2019-03-16 21:21:16 +0800151 unsolListener.onInterfaceClassActivityChanged(false, 9, 5678, 0);
Lorenzo Colittia0868002017-07-11 02:29:28 +0900152 expectSoon(observer).interfaceClassDataActivityChanged("9", false, 5678);
Lorenzo Colitti7421a012013-08-20 22:51:24 +0900153
Luke Huang909b31a2019-03-16 21:21:16 +0800154 unsolListener.onInterfaceClassActivityChanged(false, 9, 4321, 0);
Lorenzo Colittia0868002017-07-11 02:29:28 +0900155 expectSoon(observer).interfaceClassDataActivityChanged("9", false, 4321);
Lorenzo Colitti7421a012013-08-20 22:51:24 +0900156
Lorenzo Colitti7421a012013-08-20 22:51:24 +0900157 /**
158 * IP address changes.
159 */
Luke Huang909b31a2019-03-16 21:21:16 +0800160 unsolListener.onInterfaceAddressUpdated("fe80::1/64", "wlan0", 128, 253);
Lorenzo Colitti64483942013-11-15 18:43:52 +0900161 expectSoon(observer).addressUpdated("wlan0", new LinkAddress("fe80::1/64", 128, 253));
Lorenzo Colitti7421a012013-08-20 22:51:24 +0900162
Luke Huang909b31a2019-03-16 21:21:16 +0800163 unsolListener.onInterfaceAddressRemoved("fe80::1/64", "wlan0", 128, 253);
Lorenzo Colitti64483942013-11-15 18:43:52 +0900164 expectSoon(observer).addressRemoved("wlan0", new LinkAddress("fe80::1/64", 128, 253));
Lorenzo Colitti7421a012013-08-20 22:51:24 +0900165
Luke Huang909b31a2019-03-16 21:21:16 +0800166 unsolListener.onInterfaceAddressRemoved("2001:db8::1/64", "wlan0", 1, 0);
Lorenzo Colitti64483942013-11-15 18:43:52 +0900167 expectSoon(observer).addressRemoved("wlan0", new LinkAddress("2001:db8::1/64", 1, 0));
Lorenzo Colitti7421a012013-08-20 22:51:24 +0900168
Lorenzo Colitti5ae4a532013-10-31 11:59:46 +0900169 /**
170 * DNS information broadcasts.
171 */
Luke Huang909b31a2019-03-16 21:21:16 +0800172 unsolListener.onInterfaceDnsServerInfo("rmnet_usb0", 3600, new String[]{"2001:db8::1"});
Lorenzo Colitti5ae4a532013-10-31 11:59:46 +0900173 expectSoon(observer).interfaceDnsServerInfo("rmnet_usb0", 3600,
174 new String[]{"2001:db8::1"});
175
Luke Huang909b31a2019-03-16 21:21:16 +0800176 unsolListener.onInterfaceDnsServerInfo("wlan0", 14400,
177 new String[]{"2001:db8::1", "2001:db8::2"});
Lorenzo Colitti5ae4a532013-10-31 11:59:46 +0900178 expectSoon(observer).interfaceDnsServerInfo("wlan0", 14400,
179 new String[]{"2001:db8::1", "2001:db8::2"});
180
181 // We don't check for negative lifetimes, only for parse errors.
Luke Huang909b31a2019-03-16 21:21:16 +0800182 unsolListener.onInterfaceDnsServerInfo("wlan0", -3600, new String[]{"::1"});
Lorenzo Colitti5ae4a532013-10-31 11:59:46 +0900183 expectSoon(observer).interfaceDnsServerInfo("wlan0", -3600,
184 new String[]{"::1"});
185
Lorenzo Colitti5ae4a532013-10-31 11:59:46 +0900186 // No syntax checking on the addresses.
Luke Huang909b31a2019-03-16 21:21:16 +0800187 unsolListener.onInterfaceDnsServerInfo("wlan0", 600,
188 new String[]{"", "::", "", "foo", "::1"});
Lorenzo Colitti5ae4a532013-10-31 11:59:46 +0900189 expectSoon(observer).interfaceDnsServerInfo("wlan0", 600,
190 new String[]{"", "::", "", "foo", "::1"});
191
Lorenzo Colitti7421a012013-08-20 22:51:24 +0900192 // Make sure nothing else was called.
193 verifyNoMoreInteractions(observer);
194 }
195}