1 /* 2 * Copyright (C) 2020 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 android.net.wifi.mts; 18 19 import static com.google.common.truth.Truth.assertThat; 20 import static com.google.common.truth.Truth.assertWithMessage; 21 22 import static org.junit.Assume.assumeTrue; 23 24 import android.content.Context; 25 import android.util.Base64; 26 27 import androidx.test.ext.junit.runners.AndroidJUnit4; 28 import androidx.test.platform.app.InstrumentationRegistry; 29 30 import com.android.server.wifi.proto.nano.WifiMetricsProto.WifiLog; 31 32 import org.junit.Before; 33 import org.junit.Test; 34 import org.junit.runner.RunWith; 35 36 @RunWith(AndroidJUnit4.class) 37 public class WifiDumpsysMetricsTest { 38 39 private static final String WIFI_DUMP_PROTO_CMD = "/system/bin/dumpsys wifi wifiMetricsProto"; 40 41 private static final String START_TAG = "WifiMetrics:\n"; 42 private static final String END_TAG = "\nEndWifiMetrics"; 43 44 @Before setUp()45 public void setUp() throws Exception { 46 Context context = InstrumentationRegistry.getInstrumentation().getTargetContext(); 47 assumeTrue(WifiFeature.isWifiSupported(context)); 48 } 49 50 /** 51 * Test that Wifi dumps metrics in the expected format. 52 * 53 * Assumption: 54 * - Test device should have at least 1 saved network 55 */ 56 @Test testWifiDumpMetrics()57 public void testWifiDumpMetrics() throws Exception { 58 // DO NOT run under shell identity. Shell has a lot more permissions. 59 // `dumpsys wifi wifiMetricsProto` should ONLY need `android.permission.DUMP` 60 String rawDumpOutput = StreamReader.runProcessCommand(WIFI_DUMP_PROTO_CMD); 61 62 assertThat(rawDumpOutput).isNotNull(); 63 64 int protoStart = rawDumpOutput.indexOf(START_TAG); 65 int protoEnd = rawDumpOutput.indexOf(END_TAG); 66 67 assertWithMessage("Expected to find \"WifiMetrics:\", but instead found: " + rawDumpOutput) 68 .that(protoStart).isAtLeast(0); 69 assertWithMessage( 70 "Expected to find \"EndWifiMetrics\", but instead found: " + rawDumpOutput) 71 .that(protoEnd).isAtLeast(protoStart); 72 73 String protoString = rawDumpOutput.substring(protoStart + START_TAG.length(), protoEnd); 74 byte[] protoBytes = Base64.decode(protoString, Base64.DEFAULT); 75 76 WifiLog wifiLog = WifiLog.parseFrom(protoBytes); 77 78 assertThat(wifiLog.numSavedNetworks).isAtLeast(1); 79 assertThat(wifiLog.numOpenNetworks).isAtLeast(0); 80 assertThat(wifiLog.numHiddenNetworks).isAtLeast(0); 81 assertThat(wifiLog.numWificondCrashes).isAtLeast(0); 82 assertThat(wifiLog.numSupplicantCrashes).isAtLeast(0); 83 } 84 } 85