summaryrefslogtreecommitdiff
path: root/tests/wifitests/src/com/android/server/wifi/MemoryStoreImplTest.java
blob: 307ef647eee790e14f34f45b7b3037a6fb714d91 (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
/*
 * Copyright 2019 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;

import static org.junit.Assert.*;
import static org.mockito.Mockito.*;

import android.content.Context;
import android.net.IpMemoryStore;

import androidx.test.filters.SmallTest;

import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

//import java.util.HashMap;
//import java.util.Map;

/**
 * Unit tests for {@link com.android.server.wifi.MemoryStoreImpl}.
 */
@SmallTest
public class MemoryStoreImplTest extends WifiBaseTest {
    @Mock Context mContext;
    @Mock WifiScoreCard mWifiScoreCard;
    @Mock WifiHealthMonitor mWifiHealthMonitor;
    @Mock WifiScoreCard.BlobListener mBlobListener;
    @Mock WifiInjector mWifiInjector;
    @Mock IpMemoryStore mIpMemoryStore;
    private MemoryStoreImpl mMemoryStoreImpl;
    private static final String DATA_NAME = "test";

    @Before
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);
        mMemoryStoreImpl = new MemoryStoreImpl(mContext, mWifiInjector, mWifiScoreCard,
                mWifiHealthMonitor);
    }

    /**
     * Attempt to start before ip memory service is ready should not call install.
     */
    @Test
    public void attemptToStartBeforeIpMemoryServiceIsReadyShouldNotCallInstall() throws Exception {
        mMemoryStoreImpl.start();
        verify(mWifiScoreCard, never()).installMemoryStore(any());
    }

    /**
     * Test start installs itself.
     */
    @Test
    public void testStartInstallsItself() throws Exception {
        when(mWifiInjector.getIpMemoryStore()).thenReturn(mIpMemoryStore);
        mMemoryStoreImpl.start();
        verify(mWifiScoreCard).installMemoryStore(eq(mMemoryStoreImpl));
    }

    /**
     * Test that stop does pending writes.
     *
     * Also check that stop is harmless if done before start or if done twice.
     */
    @Test
    public void testThatStopDoesPendingWrites() throws Exception {
        when(mWifiInjector.getIpMemoryStore()).thenReturn(mIpMemoryStore);

        // stop without start is a NOP
        mMemoryStoreImpl.stop();
        verifyNoMoreInteractions(mWifiScoreCard);

        // start / stop calls for pending writes
        mMemoryStoreImpl.start();
        mMemoryStoreImpl.stop();
        verify(mWifiScoreCard).installMemoryStore(eq(mMemoryStoreImpl));
        verify(mWifiScoreCard).doWrites();
        verifyNoMoreInteractions(mWifiScoreCard);

        // stopping again should do nothing more
        mMemoryStoreImpl.stop();
        verifyNoMoreInteractions(mWifiScoreCard);
    }

    /**
     * WifiScoreCard read should call ip memory store retrieveBlob.
     */
    @Test
    public void wifiScoreCardReadShouldCallIpMemoryStoreRetrieveBlob() throws Exception {
        final byte[] myBlob = new byte[]{0x0, 0x3};
        final String myL2Key = "L2Key:" + myBlob;
        final android.net.ipmemorystore.Status statusSuccess =
                new android.net.ipmemorystore.Status(android.net.ipmemorystore.Status.SUCCESS);

        when(mWifiInjector.getIpMemoryStore()).thenReturn(mIpMemoryStore);
        mMemoryStoreImpl.start();
        mMemoryStoreImpl.read(myL2Key, DATA_NAME, mBlobListener);
        verify(mIpMemoryStore).retrieveBlob(
                eq(myL2Key),                                                  // String l2Key
                eq(MemoryStoreImpl.WIFI_FRAMEWORK_IP_MEMORY_STORE_CLIENT_ID), // String clientId
                eq(DATA_NAME),
                mOnBlobRetrievedListenerCaptor.capture());
        verifyNoMoreInteractions(mIpMemoryStore, mBlobListener);
        // Now simulate the completion of the read request
        final android.net.ipmemorystore.Blob wrappedBlob = new android.net.ipmemorystore.Blob();
        wrappedBlob.data = myBlob;
        mOnBlobRetrievedListenerCaptor.getValue()
                .onBlobRetrieved(
                        statusSuccess,
                        myL2Key,
                        DATA_NAME,
                        wrappedBlob);
        verify(mBlobListener).onBlobRetrieved(mBytesCaptor.capture());
        assertArrayEquals(myBlob, mBytesCaptor.getValue());
    }
    final ArgumentCaptor<android.net.ipmemorystore.OnBlobRetrievedListener>
            mOnBlobRetrievedListenerCaptor =
            ArgumentCaptor.forClass(android.net.ipmemorystore.OnBlobRetrievedListener.class);
    final ArgumentCaptor<byte[]>
            mBytesCaptor =
            ArgumentCaptor.forClass(byte[].class);

    /**
     * WifiScoreCard write should call ip memory store storeBlob.
     */
    @Test
    public void wifiScoreCardWriteShouldCallIpMemoryStoreStoreBlob() throws Exception {
        final byte[] myBlob = new byte[]{0x0, 0x3, 0x1};
        final String myL2Key = "L2Key:" + myBlob;
        when(mWifiInjector.getIpMemoryStore()).thenReturn(mIpMemoryStore);
        mMemoryStoreImpl.start();
        mMemoryStoreImpl.write(myL2Key, DATA_NAME, myBlob);
        verify(mIpMemoryStore).storeBlob(
                eq(myL2Key),
                eq(MemoryStoreImpl.WIFI_FRAMEWORK_IP_MEMORY_STORE_CLIENT_ID),
                eq(DATA_NAME),
                mIpMemoryStoreBlobCaptor.capture(),
                eq(null));
        android.net.ipmemorystore.Blob blob = mIpMemoryStoreBlobCaptor.getValue();
        assertArrayEquals(myBlob, blob.data);
        verifyNoMoreInteractions(mIpMemoryStore);
    }
    final ArgumentCaptor<android.net.ipmemorystore.Blob> mIpMemoryStoreBlobCaptor =
            ArgumentCaptor.forClass(android.net.ipmemorystore.Blob.class);

    /**
     * An exception should disable further operations.
     */
    @Test
    public void exceptionDisablesFurtherOperations() throws Exception {
        final byte[] myBlob = new byte[]{0x0, 0x3, 0x1};
        final String myL2Key = "L2Key:" + myBlob;
        when(mWifiInjector.getIpMemoryStore()).thenReturn(mIpMemoryStore);
        doThrow(new RuntimeException("Just a test"))
                .when(mIpMemoryStore).storeBlob(any(), any(), any(), any(), any());
        mMemoryStoreImpl.start();
        mMemoryStoreImpl.write(myL2Key, DATA_NAME, myBlob);
        // After the failed write, the read should do nothing.
        mMemoryStoreImpl.read(myL2Key, DATA_NAME, mBlobListener);
        verify(mIpMemoryStore, never())
                .retrieveBlob(any(), any(), any(), any());
    }
}