1 /*
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 
17 package com.android.server;
18 
19 import static org.mockito.Mockito.doNothing;
20 import static org.mockito.Mockito.doReturn;
21 import static org.mockito.Mockito.mock;
22 import static org.mockito.Mockito.reset;
23 import static org.mockito.Mockito.timeout;
24 import static org.mockito.Mockito.verify;
25 import static org.mockito.Mockito.verifyNoMoreInteractions;
26 
27 import android.annotation.NonNull;
28 import android.content.Context;
29 import android.net.INetd;
30 import android.net.INetdUnsolicitedEventListener;
31 import android.net.LinkAddress;
32 import android.os.BatteryStats;
33 import android.os.Binder;
34 import android.os.IBinder;
35 import android.test.suitebuilder.annotation.SmallTest;
36 
37 import androidx.test.runner.AndroidJUnit4;
38 
39 import com.android.internal.app.IBatteryStats;
40 import com.android.server.NetworkManagementService.SystemServices;
41 import com.android.server.net.BaseNetworkObserver;
42 
43 import org.junit.After;
44 import org.junit.Before;
45 import org.junit.Test;
46 import org.junit.runner.RunWith;
47 import org.mockito.ArgumentCaptor;
48 import org.mockito.Captor;
49 import org.mockito.Mock;
50 import org.mockito.MockitoAnnotations;
51 
52 /**
53  * Tests for {@link NetworkManagementService}.
54  */
55 @RunWith(AndroidJUnit4.class)
56 @SmallTest
57 public class NetworkManagementServiceTest {
58 
59     private NetworkManagementService mNMService;
60 
61     @Mock private Context mContext;
62     @Mock private IBatteryStats.Stub mBatteryStatsService;
63     @Mock private INetd.Stub mNetdService;
64 
65     @NonNull
66     @Captor
67     private ArgumentCaptor<INetdUnsolicitedEventListener> mUnsolListenerCaptor;
68 
69     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
setUp()89     public void setUp() throws Exception {
90         MockitoAnnotations.initMocks(this);
91         doNothing().when(mNetdService)
92                 .registerUnsolicitedEventListener(mUnsolListenerCaptor.capture());
93         // Start the service and wait until it connects to our socket.
94         mNMService = NetworkManagementService.create(mContext, mServices);
95     }
96 
97     @After
tearDown()98     public void tearDown() throws Exception {
99         mNMService.shutdown();
100     }
101 
expectSoon(T mock)102     private static <T> T expectSoon(T mock) {
103         return verify(mock, timeout(200));
104     }
105 
106     /**
107      * Tests that network observers work properly.
108      */
109     @Test
testNetworkObservers()110     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.
117 
118         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
122         // invalid messages don't cause any callbacks, we call verifyNoMoreInteractions at the end.
123 
124         /**
125          * Interface changes.
126          */
127         unsolListener.onInterfaceAdded("rmnet12");
128         expectSoon(observer).interfaceAdded("rmnet12");
129 
130         unsolListener.onInterfaceRemoved("eth1");
131         expectSoon(observer).interfaceRemoved("eth1");
132 
133         unsolListener.onInterfaceChanged("clat4", true);
134         expectSoon(observer).interfaceStatusChanged("clat4", true);
135 
136         unsolListener.onInterfaceLinkStateChanged("rmnet0", false);
137         expectSoon(observer).interfaceLinkStateChanged("rmnet0", false);
138 
139         /**
140          * Bandwidth control events.
141          */
142         unsolListener.onQuotaLimitReached("data", "rmnet_usb0");
143         expectSoon(observer).limitReached("data", "rmnet_usb0");
144 
145         /**
146          * Interface class activity.
147          */
148         unsolListener.onInterfaceClassActivityChanged(true, 1, 1234, 0);
149         expectSoon(observer).interfaceClassDataActivityChanged("1", true, 1234);
150 
151         unsolListener.onInterfaceClassActivityChanged(false, 9, 5678, 0);
152         expectSoon(observer).interfaceClassDataActivityChanged("9", false, 5678);
153 
154         unsolListener.onInterfaceClassActivityChanged(false, 9, 4321, 0);
155         expectSoon(observer).interfaceClassDataActivityChanged("9", false, 4321);
156 
157         /**
158          * IP address changes.
159          */
160         unsolListener.onInterfaceAddressUpdated("fe80::1/64", "wlan0", 128, 253);
161         expectSoon(observer).addressUpdated("wlan0", new LinkAddress("fe80::1/64", 128, 253));
162 
163         unsolListener.onInterfaceAddressRemoved("fe80::1/64", "wlan0", 128, 253);
164         expectSoon(observer).addressRemoved("wlan0", new LinkAddress("fe80::1/64", 128, 253));
165 
166         unsolListener.onInterfaceAddressRemoved("2001:db8::1/64", "wlan0", 1, 0);
167         expectSoon(observer).addressRemoved("wlan0", new LinkAddress("2001:db8::1/64", 1, 0));
168 
169         /**
170          * DNS information broadcasts.
171          */
172         unsolListener.onInterfaceDnsServerInfo("rmnet_usb0", 3600, new String[]{"2001:db8::1"});
173         expectSoon(observer).interfaceDnsServerInfo("rmnet_usb0", 3600,
174                 new String[]{"2001:db8::1"});
175 
176         unsolListener.onInterfaceDnsServerInfo("wlan0", 14400,
177                 new String[]{"2001:db8::1", "2001:db8::2"});
178         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.
182         unsolListener.onInterfaceDnsServerInfo("wlan0", -3600, new String[]{"::1"});
183         expectSoon(observer).interfaceDnsServerInfo("wlan0", -3600,
184                 new String[]{"::1"});
185 
186         // No syntax checking on the addresses.
187         unsolListener.onInterfaceDnsServerInfo("wlan0", 600,
188                 new String[]{"", "::", "", "foo", "::1"});
189         expectSoon(observer).interfaceDnsServerInfo("wlan0", 600,
190                 new String[]{"", "::", "", "foo", "::1"});
191 
192         // Make sure nothing else was called.
193         verifyNoMoreInteractions(observer);
194     }
195 }
196