summaryrefslogtreecommitdiffstats
path: root/tests/wifitests/src/com/android/server/wifi/LastMileLoggerTest.java
blob: 0180d6a04dc5f3178d5dd96683adb209ecbb2655 (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
/*
 * 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;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.contains;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when;

import android.os.FileUtils;

import androidx.test.filters.SmallTest;

import libcore.io.IoUtils;

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

import java.io.File;
import java.io.PrintWriter;
import java.io.StringWriter;

/**
 * Unit tests for {@link LastMileLogger}.
 */
@SmallTest
public class LastMileLoggerTest {
    @Mock WifiInjector mWifiInjector;
    @Spy FakeWifiLog mLog;

    @Before
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);
        when(mWifiInjector.makeLog(anyString())).thenReturn(mLog);
        mTraceDataFile = File.createTempFile(TRACE_DATA_PREFIX, null);
        mTraceEnableFile = File.createTempFile(TRACE_ENABLE_PREFIX, null);
        mTraceReleaseFile = File.createTempFile(TRACE_RELEASE_PREFIX, null);
        mTraceDataFile.deleteOnExit();
        mTraceEnableFile.deleteOnExit();
        mTraceReleaseFile.deleteOnExit();
        FileUtils.stringToFile(mTraceEnableFile, "0");
        mLastMileLogger = new LastMileLogger(mWifiInjector, mTraceDataFile.getPath(),
                mTraceEnableFile.getPath(),  mTraceReleaseFile.getPath());
    }

    @Test
    public void ctorDoesNotCrash() throws Exception {
        new LastMileLogger(mWifiInjector, mTraceDataFile.getPath(), mTraceEnableFile.getPath(),
                mTraceReleaseFile.getPath());
        verifyZeroInteractions(mLog);
    }

    @Test
    public void connectionEventStartedEnablesTracing() throws Exception {
        mLastMileLogger.reportConnectionEvent(BaseWifiDiagnostics.CONNECTION_EVENT_STARTED);
        assertEquals("1", IoUtils.readFileAsString(mTraceEnableFile.getPath()));
    }

    @Test
    public void connectionEventStartedDoesNotCrashIfReleaseFileIsMissing() throws Exception {
        mTraceReleaseFile.delete();
        mLastMileLogger.reportConnectionEvent(BaseWifiDiagnostics.CONNECTION_EVENT_STARTED);
        verify(mLog).warn(contains("Failed to open free_buffer"));
    }

    @Test
    public void connectionEventStartedDoesNotEnableTracingIfReleaseFileIsMissing()
            throws Exception {
        mTraceReleaseFile.delete();
        mLastMileLogger.reportConnectionEvent(BaseWifiDiagnostics.CONNECTION_EVENT_STARTED);
        assertEquals("0", IoUtils.readFileAsString(mTraceEnableFile.getPath()));
    }

    @Test
    public void connectionEventStartedDoesNotAttemptToReopenReleaseFile() throws Exception {
        mLastMileLogger.reportConnectionEvent(BaseWifiDiagnostics.CONNECTION_EVENT_STARTED);

        // This is a rather round-about way of verifying that we don't attempt to re-open
        // the file. Namely: if we delete the |release| file, and CONNECTION_EVENT_STARTED
        // _did_ re-open the file, then we'd log an error message. Since the test is deleting the
        // |release| file, the absence of a warning message means that we didn't try to open the
        // file again.
        //
        // A more direct test would require the use of a factory for the creation of the
        // FileInputStream.
        mTraceReleaseFile.delete();
        mLastMileLogger.reportConnectionEvent(BaseWifiDiagnostics.CONNECTION_EVENT_STARTED);
        verifyZeroInteractions(mLog);
    }

    @Test
    public void connectionEventStartedDoesNotCrashIfEnableFileIsMissing() throws Exception {
        mTraceEnableFile.delete();
        mLastMileLogger.reportConnectionEvent(BaseWifiDiagnostics.CONNECTION_EVENT_STARTED);
    }

    @Test
    public void connectionEventStartedDoesNotCrashOnRepeatedCalls() throws Exception {
        mLastMileLogger.reportConnectionEvent(BaseWifiDiagnostics.CONNECTION_EVENT_STARTED);
        mLastMileLogger.reportConnectionEvent(BaseWifiDiagnostics.CONNECTION_EVENT_STARTED);
    }

    @Test
    public void connectionEventSucceededDisablesTracing() throws Exception {
        mLastMileLogger.reportConnectionEvent(BaseWifiDiagnostics.CONNECTION_EVENT_SUCCEEDED);
        assertEquals("0", IoUtils.readFileAsString(mTraceEnableFile.getPath()));
    }

    @Test
    public void connectionEventSucceededDoesNotCrashIfEnableFileIsMissing() throws Exception {
        mTraceEnableFile.delete();
        mLastMileLogger.reportConnectionEvent(BaseWifiDiagnostics.CONNECTION_EVENT_SUCCEEDED);
    }

    @Test
    public void connectionEventSucceededDoesNotCrashOnRepeatedCalls() throws Exception {
        mLastMileLogger.reportConnectionEvent(BaseWifiDiagnostics.CONNECTION_EVENT_SUCCEEDED);
        mLastMileLogger.reportConnectionEvent(BaseWifiDiagnostics.CONNECTION_EVENT_SUCCEEDED);
    }

    @Test
    public void connectionEventFailedDisablesTracingWhenPendingFails() throws Exception {
        mLastMileLogger.reportConnectionEvent(BaseWifiDiagnostics.CONNECTION_EVENT_STARTED);
        mLastMileLogger.reportConnectionEvent(BaseWifiDiagnostics.CONNECTION_EVENT_FAILED);
        assertEquals("0", IoUtils.readFileAsString(mTraceEnableFile.getPath()));
    }

    @Test
    public void connectionEventTimeoutDisablesTracing()
            throws Exception {
        mLastMileLogger.reportConnectionEvent(BaseWifiDiagnostics.CONNECTION_EVENT_STARTED);
        mLastMileLogger.reportConnectionEvent(BaseWifiDiagnostics.CONNECTION_EVENT_TIMEOUT);
        assertEquals("0", IoUtils.readFileAsString(mTraceEnableFile.getPath()));
    }

    @Test
    public void connectionEventFailedDoesNotCrashIfEnableFileIsMissing() throws Exception {
        mTraceEnableFile.delete();
        mLastMileLogger.reportConnectionEvent(BaseWifiDiagnostics.CONNECTION_EVENT_FAILED);
    }

    @Test
    public void connectionEventFailedDoesNotCrashIfDataFileIsMissing() throws Exception {
        mTraceDataFile.delete();
        mLastMileLogger.reportConnectionEvent(BaseWifiDiagnostics.CONNECTION_EVENT_FAILED);
    }

    @Test
    public void connectionEventFailedDoesNotCrashOnRepeatedCalls() throws Exception {
        mLastMileLogger.reportConnectionEvent(BaseWifiDiagnostics.CONNECTION_EVENT_FAILED);
        mLastMileLogger.reportConnectionEvent(BaseWifiDiagnostics.CONNECTION_EVENT_FAILED);
    }

    @Test
    public void dumpShowsFailureTrace() throws Exception {
        mLastMileLogger.reportConnectionEvent(BaseWifiDiagnostics.CONNECTION_EVENT_STARTED);
        FileUtils.stringToFile(mTraceDataFile.getPath(), "rdev_connect");
        mLastMileLogger.reportConnectionEvent(BaseWifiDiagnostics.CONNECTION_EVENT_FAILED);
        assertTrue(getDumpString().contains("--- Last failed"));
        assertTrue(getDumpString().contains("rdev_connect"));
    }


    @Test
    public void dumpShowsPendingConnectionTrace() throws Exception {
        mLastMileLogger.reportConnectionEvent(BaseWifiDiagnostics.CONNECTION_EVENT_STARTED);
        FileUtils.stringToFile(mTraceDataFile.getPath(), "rdev_connect");
        assertTrue(getDumpString().contains("No last mile log for \"Last failed"));
        assertTrue(getDumpString().contains("--- Latest"));
        assertTrue(getDumpString().contains("rdev_connect"));
    }

    @Test
    public void dumpShowsLastFailureTraceAndPendingConnectionTrace() throws Exception {
        mLastMileLogger.reportConnectionEvent(BaseWifiDiagnostics.CONNECTION_EVENT_STARTED);
        FileUtils.stringToFile(mTraceDataFile.getPath(), "rdev_connect try #1");
        mLastMileLogger.reportConnectionEvent(BaseWifiDiagnostics.CONNECTION_EVENT_FAILED);
        mLastMileLogger.reportConnectionEvent(BaseWifiDiagnostics.CONNECTION_EVENT_STARTED);
        FileUtils.stringToFile(mTraceDataFile.getPath(), "rdev_connect try #2");

        String dumpString = getDumpString();
        assertTrue(dumpString.contains("rdev_connect try #1"));
        assertTrue(dumpString.contains("rdev_connect try #2"));
    }

    @Test
    public void dumpShowsLastFailureTraceAndCurrentConnectionTrace() throws Exception {
        mLastMileLogger.reportConnectionEvent(BaseWifiDiagnostics.CONNECTION_EVENT_STARTED);
        FileUtils.stringToFile(mTraceDataFile.getPath(), "rdev_connect try #1");
        mLastMileLogger.reportConnectionEvent(BaseWifiDiagnostics.CONNECTION_EVENT_FAILED);
        mLastMileLogger.reportConnectionEvent(BaseWifiDiagnostics.CONNECTION_EVENT_STARTED);
        FileUtils.stringToFile(mTraceDataFile.getPath(), "rdev_connect try #2");
        mLastMileLogger.reportConnectionEvent(BaseWifiDiagnostics.CONNECTION_EVENT_SUCCEEDED);

        String dumpString = getDumpString();
        assertTrue(dumpString.contains("rdev_connect try #1"));
        assertTrue(dumpString.contains("rdev_connect try #2"));
    }

    @Test
    public void dumpDoesNotClearLastFailureData() throws Exception {
        mLastMileLogger.reportConnectionEvent(BaseWifiDiagnostics.CONNECTION_EVENT_STARTED);
        FileUtils.stringToFile(mTraceDataFile.getPath(), "rdev_connect");
        mLastMileLogger.reportConnectionEvent(BaseWifiDiagnostics.CONNECTION_EVENT_FAILED);

        getDumpString();
        String dumpString = getDumpString();
        assertTrue(dumpString.contains("rdev_connect"));
    }

    @Test
    public void dumpDoesNotClearPendingConnectionTrace() throws Exception {
        mLastMileLogger.reportConnectionEvent(BaseWifiDiagnostics.CONNECTION_EVENT_STARTED);
        FileUtils.stringToFile(mTraceDataFile.getPath(), "rdev_connect");

        getDumpString();
        String dumpString = getDumpString();
        assertTrue(dumpString.contains("rdev_connect"));
    }

    @Test
    public void dumpDoesNotCrashIfDataFileIsEmpty() throws Exception {
        getDumpString();
    }

    @Test
    public void dumpDoesNotCrashIfDataFileIsMissing() throws Exception {
        mTraceDataFile.delete();
        getDumpString();
    }

    private static final String TRACE_DATA_PREFIX = "last-mile-logger-trace-data";
    private static final String TRACE_ENABLE_PREFIX = "last-mile-logger-trace-enable";
    private static final String TRACE_RELEASE_PREFIX = "last-mile-logger-trace-release";
    private LastMileLogger mLastMileLogger;
    private File mTraceDataFile;
    private File mTraceEnableFile;
    private File mTraceReleaseFile;

    private String getDumpString() {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        mLastMileLogger.dump(pw);
        return sw.toString();
    }
}