LastMileLoggerTest.java revision 3355eb4392aabfae9453e7d6f11d9f0620bf5dae
1/*
2 * Copyright (C) 2017 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.wifi;
18
19import static org.junit.Assert.assertEquals;
20import static org.junit.Assert.assertTrue;
21import static org.mockito.Matchers.anyString;
22import static org.mockito.Matchers.contains;
23import static org.mockito.Mockito.verify;
24import static org.mockito.Mockito.verifyZeroInteractions;
25import static org.mockito.Mockito.when;
26
27import android.os.FileUtils;
28import android.test.suitebuilder.annotation.SmallTest;
29
30import libcore.io.IoUtils;
31
32import org.junit.Before;
33import org.junit.Test;
34import org.mockito.Mock;
35import org.mockito.MockitoAnnotations;
36import org.mockito.Spy;
37
38import java.io.File;
39import java.io.PrintWriter;
40import java.io.StringWriter;
41
42/**
43 * Unit tests for {@link LastMileLogger}.
44 */
45@SmallTest
46public class LastMileLoggerTest {
47    @Mock WifiInjector mWifiInjector;
48    @Spy FakeWifiLog mLog;
49    private static final long FAKE_CONNECTION_ID = 1;
50
51    @Before
52    public void setUp() throws Exception {
53        MockitoAnnotations.initMocks(this);
54        when(mWifiInjector.makeLog(anyString())).thenReturn(mLog);
55        mTraceDataFile = File.createTempFile(TRACE_DATA_PREFIX, null);
56        mTraceEnableFile = File.createTempFile(TRACE_ENABLE_PREFIX, null);
57        mTraceReleaseFile = File.createTempFile(TRACE_RELEASE_PREFIX, null);
58        mTraceDataFile.deleteOnExit();
59        mTraceEnableFile.deleteOnExit();
60        mTraceReleaseFile.deleteOnExit();
61        FileUtils.stringToFile(mTraceEnableFile, "0");
62        mLastMileLogger = new LastMileLogger(mWifiInjector, mTraceDataFile.getPath(),
63                mTraceEnableFile.getPath(),  mTraceReleaseFile.getPath());
64    }
65
66    @Test
67    public void ctorDoesNotCrash() throws Exception {
68        new LastMileLogger(mWifiInjector, mTraceDataFile.getPath(), mTraceEnableFile.getPath(),
69                mTraceReleaseFile.getPath());
70        verifyZeroInteractions(mLog);
71    }
72
73    @Test
74    public void ctorDoesNotCrashEvenIfReleaseFileIsMissing() throws Exception {
75        mTraceReleaseFile.delete();
76        new LastMileLogger(mWifiInjector, mTraceDataFile.getPath(), mTraceEnableFile.getPath(),
77                mTraceReleaseFile.getPath());
78        verify(mLog).warn(contains("Failed"));
79    }
80
81    @Test
82    public void connectionEventStartedEnablesTracing() throws Exception {
83        mLastMileLogger.reportConnectionEvent(
84                FAKE_CONNECTION_ID, BaseWifiDiagnostics.CONNECTION_EVENT_STARTED);
85        assertEquals("1", IoUtils.readFileAsString(mTraceEnableFile.getPath()));
86    }
87
88    @Test
89    public void connectionEventStartedDoesNotEnableTracingForInvalidConnectionId()
90            throws Exception {
91        mLastMileLogger.reportConnectionEvent(
92                -1, BaseWifiDiagnostics.CONNECTION_EVENT_STARTED);
93        assertEquals("0", IoUtils.readFileAsString(mTraceEnableFile.getPath()));
94    }
95
96    @Test
97    public void connectionEventStartedDoesNotCrashIfEnableFileIsMissing() throws Exception {
98        mTraceEnableFile.delete();
99        mLastMileLogger.reportConnectionEvent(
100                FAKE_CONNECTION_ID, BaseWifiDiagnostics.CONNECTION_EVENT_STARTED);
101    }
102
103    @Test
104    public void connectionEventStartedDoesNotCrashOnRepeatedCalls() throws Exception {
105        mLastMileLogger.reportConnectionEvent(
106                FAKE_CONNECTION_ID, BaseWifiDiagnostics.CONNECTION_EVENT_STARTED);
107        mLastMileLogger.reportConnectionEvent(
108                FAKE_CONNECTION_ID, BaseWifiDiagnostics.CONNECTION_EVENT_STARTED);
109    }
110
111    @Test
112    public void connectionEventSucceededDisablesTracing() throws Exception {
113        mLastMileLogger.reportConnectionEvent(
114                FAKE_CONNECTION_ID, BaseWifiDiagnostics.CONNECTION_EVENT_SUCCEEDED);
115        assertEquals("0", IoUtils.readFileAsString(mTraceEnableFile.getPath()));
116    }
117
118    @Test
119    public void connectionEventSucceededDoesNotCrashIfEnableFileIsMissing() throws Exception {
120        mTraceEnableFile.delete();
121        mLastMileLogger.reportConnectionEvent(
122                FAKE_CONNECTION_ID, BaseWifiDiagnostics.CONNECTION_EVENT_SUCCEEDED);
123    }
124
125    @Test
126    public void connectionEventSucceededDoesNotCrashOnRepeatedCalls() throws Exception {
127        mLastMileLogger.reportConnectionEvent(
128                FAKE_CONNECTION_ID, BaseWifiDiagnostics.CONNECTION_EVENT_SUCCEEDED);
129        mLastMileLogger.reportConnectionEvent(
130                FAKE_CONNECTION_ID, BaseWifiDiagnostics.CONNECTION_EVENT_SUCCEEDED);
131    }
132
133    @Test
134    public void connectionEventFailedDisablesTracingWhenPendingFails() throws Exception {
135        mLastMileLogger.reportConnectionEvent(
136                FAKE_CONNECTION_ID, BaseWifiDiagnostics.CONNECTION_EVENT_STARTED);
137        mLastMileLogger.reportConnectionEvent(
138                    FAKE_CONNECTION_ID, BaseWifiDiagnostics.CONNECTION_EVENT_FAILED);
139        assertEquals("0", IoUtils.readFileAsString(mTraceEnableFile.getPath()));
140    }
141
142    @Test
143    public void connectionEventFailedDoesNotDisableTracingOnFailureOfStaleConnection()
144            throws Exception {
145        mLastMileLogger.reportConnectionEvent(
146                FAKE_CONNECTION_ID, BaseWifiDiagnostics.CONNECTION_EVENT_STARTED);
147        mLastMileLogger.reportConnectionEvent(
148                FAKE_CONNECTION_ID + 1, BaseWifiDiagnostics.CONNECTION_EVENT_STARTED);
149        mLastMileLogger.reportConnectionEvent(
150                FAKE_CONNECTION_ID, BaseWifiDiagnostics.CONNECTION_EVENT_FAILED);
151        assertEquals("1", IoUtils.readFileAsString(mTraceEnableFile.getPath()));
152    }
153
154    @Test
155    public void connectionEventFailedDisablesTracingOnFailureOfFutureConnection()
156            throws Exception {
157        mLastMileLogger.reportConnectionEvent(
158                FAKE_CONNECTION_ID, BaseWifiDiagnostics.CONNECTION_EVENT_STARTED);
159        mLastMileLogger.reportConnectionEvent(
160                FAKE_CONNECTION_ID + 1, BaseWifiDiagnostics.CONNECTION_EVENT_FAILED);
161        assertEquals("0", IoUtils.readFileAsString(mTraceEnableFile.getPath()));
162    }
163
164    @Test
165    public void connectionEventFailedDoesNotCrashIfEnableFileIsMissing() throws Exception {
166        mTraceEnableFile.delete();
167        mLastMileLogger.reportConnectionEvent(
168                FAKE_CONNECTION_ID, BaseWifiDiagnostics.CONNECTION_EVENT_FAILED);
169    }
170
171    @Test
172    public void connectionEventFailedDoesNotCrashIfDataFileIsMissing() throws Exception {
173        mTraceDataFile.delete();
174        mLastMileLogger.reportConnectionEvent(
175                FAKE_CONNECTION_ID, BaseWifiDiagnostics.CONNECTION_EVENT_FAILED);
176    }
177
178    @Test
179    public void connectionEventFailedDoesNotCrashOnRepeatedCalls() throws Exception {
180        mLastMileLogger.reportConnectionEvent(
181                FAKE_CONNECTION_ID, BaseWifiDiagnostics.CONNECTION_EVENT_FAILED);
182        mLastMileLogger.reportConnectionEvent(
183                FAKE_CONNECTION_ID, BaseWifiDiagnostics.CONNECTION_EVENT_FAILED);
184    }
185
186    @Test
187    public void dumpShowsFailureTrace() throws Exception {
188        mLastMileLogger.reportConnectionEvent(
189                FAKE_CONNECTION_ID, BaseWifiDiagnostics.CONNECTION_EVENT_STARTED);
190        FileUtils.stringToFile(mTraceDataFile.getPath(), "rdev_connect");
191        mLastMileLogger.reportConnectionEvent(
192                FAKE_CONNECTION_ID, BaseWifiDiagnostics.CONNECTION_EVENT_FAILED);
193        assertTrue(getDumpString().contains("--- Last failed"));
194        assertTrue(getDumpString().contains("rdev_connect"));
195    }
196
197    @Test
198    public void dumpShowsFailureTraceEvenIfConnectionIdIncreases() throws Exception {
199        mLastMileLogger.reportConnectionEvent(
200                FAKE_CONNECTION_ID, BaseWifiDiagnostics.CONNECTION_EVENT_STARTED);
201        FileUtils.stringToFile(mTraceDataFile.getPath(), "rdev_connect");
202        mLastMileLogger.reportConnectionEvent(
203                FAKE_CONNECTION_ID + 1, BaseWifiDiagnostics.CONNECTION_EVENT_FAILED);
204        assertTrue(getDumpString().contains("--- Last failed"));
205        assertTrue(getDumpString().contains("rdev_connect"));
206    }
207
208    @Test
209    public void dumpShowsPendingConnectionTrace() throws Exception {
210        mLastMileLogger.reportConnectionEvent(
211                FAKE_CONNECTION_ID, BaseWifiDiagnostics.CONNECTION_EVENT_STARTED);
212        FileUtils.stringToFile(mTraceDataFile.getPath(), "rdev_connect");
213        assertTrue(getDumpString().contains("No last mile log for \"Last failed"));
214        assertTrue(getDumpString().contains("--- Latest"));
215        assertTrue(getDumpString().contains("rdev_connect"));
216    }
217
218    @Test
219    public void dumpShowsLastFailureTraceAndPendingConnectionTrace() throws Exception {
220        mLastMileLogger.reportConnectionEvent(
221                FAKE_CONNECTION_ID, BaseWifiDiagnostics.CONNECTION_EVENT_STARTED);
222        FileUtils.stringToFile(mTraceDataFile.getPath(), "rdev_connect try #1");
223        mLastMileLogger.reportConnectionEvent(
224                FAKE_CONNECTION_ID, BaseWifiDiagnostics.CONNECTION_EVENT_FAILED);
225        mLastMileLogger.reportConnectionEvent(
226                FAKE_CONNECTION_ID, BaseWifiDiagnostics.CONNECTION_EVENT_STARTED);
227        FileUtils.stringToFile(mTraceDataFile.getPath(), "rdev_connect try #2");
228
229        String dumpString = getDumpString();
230        assertTrue(dumpString.contains("rdev_connect try #1"));
231        assertTrue(dumpString.contains("rdev_connect try #2"));
232    }
233
234    @Test
235    public void dumpShowsLastFailureTraceAndCurrentConnectionTrace() throws Exception {
236        mLastMileLogger.reportConnectionEvent(
237                FAKE_CONNECTION_ID, BaseWifiDiagnostics.CONNECTION_EVENT_STARTED);
238        FileUtils.stringToFile(mTraceDataFile.getPath(), "rdev_connect try #1");
239        mLastMileLogger.reportConnectionEvent(
240                FAKE_CONNECTION_ID, BaseWifiDiagnostics.CONNECTION_EVENT_FAILED);
241        mLastMileLogger.reportConnectionEvent(
242                FAKE_CONNECTION_ID, BaseWifiDiagnostics.CONNECTION_EVENT_STARTED);
243        FileUtils.stringToFile(mTraceDataFile.getPath(), "rdev_connect try #2");
244        mLastMileLogger.reportConnectionEvent(
245                FAKE_CONNECTION_ID, BaseWifiDiagnostics.CONNECTION_EVENT_SUCCEEDED);
246
247        String dumpString = getDumpString();
248        assertTrue(dumpString.contains("rdev_connect try #1"));
249        assertTrue(dumpString.contains("rdev_connect try #2"));
250    }
251
252    @Test
253    public void dumpDoesNotClearLastFailureData() throws Exception {
254        mLastMileLogger.reportConnectionEvent(
255                FAKE_CONNECTION_ID, BaseWifiDiagnostics.CONNECTION_EVENT_STARTED);
256        FileUtils.stringToFile(mTraceDataFile.getPath(), "rdev_connect");
257        mLastMileLogger.reportConnectionEvent(
258                FAKE_CONNECTION_ID, BaseWifiDiagnostics.CONNECTION_EVENT_FAILED);
259
260        getDumpString();
261        String dumpString = getDumpString();
262        assertTrue(dumpString.contains("rdev_connect"));
263    }
264
265    @Test
266    public void dumpDoesNotClearPendingConnectionTrace() throws Exception {
267        mLastMileLogger.reportConnectionEvent(
268                FAKE_CONNECTION_ID, BaseWifiDiagnostics.CONNECTION_EVENT_STARTED);
269        FileUtils.stringToFile(mTraceDataFile.getPath(), "rdev_connect");
270
271        getDumpString();
272        String dumpString = getDumpString();
273        assertTrue(dumpString.contains("rdev_connect"));
274    }
275
276    @Test
277    public void dumpDoesNotCrashIfDataFileIsEmpty() throws Exception {
278        getDumpString();
279    }
280
281    @Test
282    public void dumpDoesNotCrashIfDataFileIsMissing() throws Exception {
283        mTraceDataFile.delete();
284        getDumpString();
285    }
286
287    private static final String TRACE_DATA_PREFIX = "last-mile-logger-trace-data";
288    private static final String TRACE_ENABLE_PREFIX = "last-mile-logger-trace-enable";
289    private static final String TRACE_RELEASE_PREFIX = "last-mile-logger-trace-release";
290    private LastMileLogger mLastMileLogger;
291    private File mTraceDataFile;
292    private File mTraceEnableFile;
293    private File mTraceReleaseFile;
294
295    private String getDumpString() {
296        StringWriter sw = new StringWriter();
297        PrintWriter pw = new PrintWriter(sw);
298        mLastMileLogger.dump(pw);
299        return sw.toString();
300    }
301}
302