summaryrefslogtreecommitdiff
path: root/tests/wifitests/src/com/android/server/wifi/aware/WifiAwareNativeManagerTest.java
blob: d97ae06044afe16e9e6bdaf61bedce56543a294c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.server.wifi.aware;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;

import android.hardware.wifi.V1_0.IWifiNanIface;
import android.hardware.wifi.V1_0.IfaceType;
import android.hardware.wifi.V1_0.WifiStatus;
import android.hardware.wifi.V1_0.WifiStatusCode;
import android.os.Handler;

import androidx.test.filters.SmallTest;

import com.android.server.wifi.HalDeviceManager;
import com.android.server.wifi.WifiBaseTest;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ErrorCollector;
import org.mockito.ArgumentCaptor;
import org.mockito.InOrder;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

/**
 * Unit test harness for WifiAwareNativeManager.
 */
@SmallTest
public class WifiAwareNativeManagerTest extends WifiBaseTest {
    private WifiAwareNativeManager mDut;
    @Mock private WifiAwareStateManager mWifiAwareStateManagerMock;
    @Mock private HalDeviceManager mHalDeviceManager;
    @Mock private WifiAwareNativeCallback mWifiAwareNativeCallback;
    @Mock private IWifiNanIface mWifiNanIfaceMock;
    @Mock android.hardware.wifi.V1_2.IWifiNanIface mIWifiNanIface12Mock;
    @Mock private Handler mHandlerMock;
    private ArgumentCaptor<HalDeviceManager.ManagerStatusListener> mManagerStatusListenerCaptor =
            ArgumentCaptor.forClass(HalDeviceManager.ManagerStatusListener.class);
    private ArgumentCaptor<HalDeviceManager.InterfaceDestroyedListener>
            mDestroyedListenerCaptor = ArgumentCaptor.forClass(
            HalDeviceManager.InterfaceDestroyedListener.class);
    private ArgumentCaptor<HalDeviceManager.InterfaceAvailableForRequestListener>
            mAvailListenerCaptor = ArgumentCaptor.forClass(
            HalDeviceManager.InterfaceAvailableForRequestListener.class);
    private InOrder mInOrder;
    @Rule public ErrorCollector collector = new ErrorCollector();

    private WifiStatus mStatusOk;

    private class MockableWifiAwareNativeManager extends WifiAwareNativeManager {
        MockableWifiAwareNativeManager(WifiAwareStateManager awareStateManager,
                HalDeviceManager halDeviceManager,
                WifiAwareNativeCallback wifiAwareNativeCallback) {
            super(awareStateManager, halDeviceManager, wifiAwareNativeCallback);
        }

        @Override
        public android.hardware.wifi.V1_2.IWifiNanIface mockableCastTo_1_2(IWifiNanIface iface) {
            return (iface == mIWifiNanIface12Mock) ? mIWifiNanIface12Mock : null;
        }
    }

    @Before
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);

        mStatusOk = new WifiStatus();
        mStatusOk.code = WifiStatusCode.SUCCESS;

        when(mWifiNanIfaceMock.registerEventCallback(any())).thenReturn(mStatusOk);
        when(mIWifiNanIface12Mock.registerEventCallback_1_2(any())).thenReturn(mStatusOk);

        mDut = new MockableWifiAwareNativeManager(mWifiAwareStateManagerMock,
                mHalDeviceManager, mWifiAwareNativeCallback);
        mDut.start(mHandlerMock);

        mInOrder = inOrder(mWifiAwareStateManagerMock, mHalDeviceManager, mWifiNanIfaceMock,
                mIWifiNanIface12Mock);

        // validate (and capture) that register manage status callback
        mInOrder.verify(mHalDeviceManager).initialize();
        mInOrder.verify(mHalDeviceManager).registerStatusListener(
                mManagerStatusListenerCaptor.capture(), any());
    }

    /**
     * Test the control flow of the manager when Aware isn't being actively used:
     *
     * 1. onStatusChange (ready/started)
     * 2. on available -> enableUsage
     * 3. onStatusChange (!started) -> disableUsage
     * 4. onStatusChange (ready/started) + available -> enableUsage
     * 5. on not available -> disableUsage
     *
     * --> no interface creation at any point!
     */
    @Test
    public void testControlFlowWithoutInterface() {
        // configure HalDeviceManager as ready/wifi started (and to return an interface if
        // requested)
        when(mHalDeviceManager.isStarted()).thenReturn(true);
        when(mHalDeviceManager.createNanIface(any(), any())).thenReturn(mWifiNanIfaceMock);

        // 1. onStatusChange (ready/started)
        mManagerStatusListenerCaptor.getValue().onStatusChanged();
        mInOrder.verify(mHalDeviceManager).registerInterfaceAvailableForRequestListener(
                eq(IfaceType.NAN), mAvailListenerCaptor.capture(), any(Handler.class));

        // 2. NAN is available -> enableUsage
        mAvailListenerCaptor.getValue().onAvailabilityChanged(true);
        mInOrder.verify(mWifiAwareStateManagerMock).enableUsage();

        // 3. onStatusChange (not ready) -> disableUsage
        when(mHalDeviceManager.isStarted()).thenReturn(false);
        mManagerStatusListenerCaptor.getValue().onStatusChanged();

        mInOrder.verify(mWifiAwareStateManagerMock).disableUsage();

        // 4. onStatusChange (ready/started) + available -> enableUsage
        when(mHalDeviceManager.isStarted()).thenReturn(true);
        mManagerStatusListenerCaptor.getValue().onStatusChanged();

        mManagerStatusListenerCaptor.getValue().onStatusChanged();
        mInOrder.verify(mHalDeviceManager).registerInterfaceAvailableForRequestListener(
                eq(IfaceType.NAN), mAvailListenerCaptor.capture(), any(Handler.class));
        mAvailListenerCaptor.getValue().onAvailabilityChanged(true);

        mInOrder.verify(mWifiAwareStateManagerMock).enableUsage();

        // 5. not available -> disableUsage
        mAvailListenerCaptor.getValue().onAvailabilityChanged(false);

        mInOrder.verify(mWifiAwareStateManagerMock).disableUsage();

        mInOrder.verify(mHalDeviceManager, never()).createNanIface(any(), any());
        verifyNoMoreInteractions(mWifiAwareStateManagerMock, mWifiNanIfaceMock,
                mIWifiNanIface12Mock);
        assertNull("Interface non-null!", mDut.getWifiNanIface());
    }

    /**
     * Test the control flow (and reference counting) of the manager when Aware is actively used and
     * reference counted (i.e. irregular requests/releases).
     */
    @Test
    public void testReferenceCounting() throws Exception {
        // configure HalDeviceManager as ready/wifi started (and to return an interface if
        // requested)
        when(mHalDeviceManager.isStarted()).thenReturn(true);
        when(mHalDeviceManager.createNanIface(any(), any())).thenReturn(mWifiNanIfaceMock);

        // 1. onStatusChange (ready/started)
        mManagerStatusListenerCaptor.getValue().onStatusChanged();
        mInOrder.verify(mHalDeviceManager).registerInterfaceAvailableForRequestListener(
                eq(IfaceType.NAN), mAvailListenerCaptor.capture(), any(Handler.class));
        assertNull("Interface non-null!", mDut.getWifiNanIface());

        mAvailListenerCaptor.getValue().onAvailabilityChanged(true);
        mInOrder.verify(mWifiAwareStateManagerMock).enableUsage();

        // 2. request (interface obtained)
        mDut.tryToGetAware();
        mInOrder.verify(mHalDeviceManager).createNanIface(mDestroyedListenerCaptor.capture(),
                any());
        mInOrder.verify(mWifiNanIfaceMock).registerEventCallback(any());
        assertEquals("Interface mismatch", mWifiNanIfaceMock, mDut.getWifiNanIface());

        // 3. release (interface released)
        mDut.releaseAware();
        mInOrder.verify(mHalDeviceManager).removeIface(mWifiNanIfaceMock);
        assertNull("Interface non-null!", mDut.getWifiNanIface());

        mDestroyedListenerCaptor.getValue().onDestroyed("nan0");

        // 4. request (interface obtained)
        mDut.tryToGetAware();
        mInOrder.verify(mHalDeviceManager).createNanIface(mDestroyedListenerCaptor.capture(),
                any());
        mInOrder.verify(mWifiNanIfaceMock).registerEventCallback(any());
        assertEquals("Interface mismatch", mWifiNanIfaceMock, mDut.getWifiNanIface());

        // 5. request (nop - already have interface)
        mDut.tryToGetAware();
        assertEquals("Interface mismatch", mWifiNanIfaceMock, mDut.getWifiNanIface());

        // 6. release (nop - reference counting requests)
        mDut.releaseAware();
        assertEquals("Interface mismatch", mWifiNanIfaceMock, mDut.getWifiNanIface());

        // 7. release (interface released)
        mDut.releaseAware();
        mInOrder.verify(mHalDeviceManager).removeIface(mWifiNanIfaceMock);
        assertNull("Interface non-null!", mDut.getWifiNanIface());

        mDestroyedListenerCaptor.getValue().onDestroyed("nan0");

        mInOrder.verify(mHalDeviceManager, never()).createNanIface(any(), any());
        mInOrder.verify(mHalDeviceManager, never()).removeIface(any());
        verifyNoMoreInteractions(mWifiAwareStateManagerMock, mWifiNanIfaceMock,
                mIWifiNanIface12Mock);
    }

    /**
     * Test the control flow when the interface gets deleted due to external
     */
    @Test
    public void testRequestFlowWithAsyncDeletes() throws Exception {
        // configure HalDeviceManager as ready/wifi started (and to return an interface if
        // requested)
        when(mHalDeviceManager.isStarted()).thenReturn(true);
        when(mHalDeviceManager.createNanIface(any(), any())).thenReturn(mWifiNanIfaceMock);

        // 1. onStatusChange (ready/started)
        mManagerStatusListenerCaptor.getValue().onStatusChanged();
        mInOrder.verify(mHalDeviceManager).registerInterfaceAvailableForRequestListener(
                eq(IfaceType.NAN), mAvailListenerCaptor.capture(), any(Handler.class));
        assertNull("Interface non-null!", mDut.getWifiNanIface());

        mAvailListenerCaptor.getValue().onAvailabilityChanged(true);
        mInOrder.verify(mWifiAwareStateManagerMock).enableUsage();

        // 2. request (interface obtained)
        mDut.tryToGetAware();
        mInOrder.verify(mHalDeviceManager).createNanIface(mDestroyedListenerCaptor.capture(),
                any());
        mInOrder.verify(mWifiNanIfaceMock).registerEventCallback(any());
        assertEquals("Interface mismatch", mWifiNanIfaceMock, mDut.getWifiNanIface());

        // 3. interface gets destroyed
        mDestroyedListenerCaptor.getValue().onDestroyed("nan0");

        mInOrder.verify(mWifiAwareStateManagerMock).disableUsage();
        assertNull("Interface non-null!", mDut.getWifiNanIface());

        // 4. a release doesn't do much
        mDut.releaseAware();

        mInOrder.verify(mHalDeviceManager, never()).createNanIface(any(), any());
        mInOrder.verify(mHalDeviceManager, never()).removeIface(any());
        verifyNoMoreInteractions(mWifiAwareStateManagerMock, mWifiNanIfaceMock,
                mIWifiNanIface12Mock);
    }

    /**
     * Test the basic control flow for HAL 1.2 - validate that the correct event registration
     * occurs.
     */
    @Test
    public void testBasicFlowHal12() throws Exception {
        // configure HalDeviceManager as ready/wifi started (and to return an interface if
        // requested)
        when(mHalDeviceManager.isStarted()).thenReturn(true);
        when(mHalDeviceManager.createNanIface(any(), any())).thenReturn(mIWifiNanIface12Mock);

        // 1. onStatusChange (ready/started)
        mManagerStatusListenerCaptor.getValue().onStatusChanged();
        mInOrder.verify(mHalDeviceManager).registerInterfaceAvailableForRequestListener(
                eq(IfaceType.NAN), mAvailListenerCaptor.capture(), any(Handler.class));
        assertNull("Interface non-null!", mDut.getWifiNanIface());

        mAvailListenerCaptor.getValue().onAvailabilityChanged(true);
        mInOrder.verify(mWifiAwareStateManagerMock).enableUsage();

        // 2. request (interface obtained)
        mDut.tryToGetAware();
        mInOrder.verify(mHalDeviceManager).createNanIface(mDestroyedListenerCaptor.capture(),
                any());
        mInOrder.verify(mIWifiNanIface12Mock).registerEventCallback_1_2(any());
        assertEquals("Interface mismatch", mIWifiNanIface12Mock, mDut.getWifiNanIface());

        // 3. receive Availability Change, has interface, should ignore
        mAvailListenerCaptor.getValue().onAvailabilityChanged(false);
        assertTrue("AwareNativeAvailable mismatch ", mDut.isAwareNativeAvailable());
    }
}