1 /* 2 * Copyright (C) 2023 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 com.android.phone; 18 19 20 import static org.junit.Assert.assertEquals; 21 import static org.junit.Assert.assertFalse; 22 import static org.mockito.ArgumentMatchers.eq; 23 import static org.mockito.Mockito.spy; 24 import static org.mockito.Mockito.times; 25 import static org.mockito.Mockito.verify; 26 27 import android.os.DropBoxManager; 28 import android.os.SystemClock; 29 import android.telephony.TelephonyManager; 30 31 import org.junit.After; 32 import org.junit.Before; 33 import org.junit.Test; 34 import org.junit.runner.RunWith; 35 import org.junit.runners.JUnit4; 36 import org.mockito.ArgumentCaptor; 37 import org.mockito.Mock; 38 import org.mockito.MockitoAnnotations; 39 40 import java.io.IOException; 41 import java.util.Arrays; 42 43 /** 44 * Unit Tests for DiagnosticDataCollector. 45 */ 46 @RunWith(JUnit4.class) 47 public class DiagnosticDataCollectorTest { 48 49 private static final String[] TELECOM_DUMPSYS_COMMAND = 50 {"/system/bin/dumpsys", "telecom", "EmergencyDiagnostics"}; 51 private static final String[] TELEPHONY_DUMPSYS_COMMAND = 52 {"/system/bin/dumpsys", "telephony.registry", "EmergencyDiagnostics"}; 53 private static final String[] LOGCAT_BINARY = {"/system/bin/logcat"}; 54 55 56 @Mock 57 DataCollectorConfig.Adapter mConfig; 58 private Runtime mRuntime; 59 60 @Mock 61 private DropBoxManager mDropBoxManager; 62 63 private DiagnosticDataCollector mDiagnosticDataCollector; 64 65 @Before setUp()66 public void setUp() throws Exception { 67 MockitoAnnotations.initMocks(this); 68 mRuntime = spy(Runtime.getRuntime()); 69 mDiagnosticDataCollector = new DiagnosticDataCollector(mRuntime, Runnable::run, 70 mDropBoxManager, false); 71 } 72 73 @After tearDown()74 public void tearDown() throws Exception { 75 } 76 verifyCmdAndDropboxTag(String[] cmd, String tag, boolean startsWithMatch)77 private void verifyCmdAndDropboxTag(String[] cmd, String tag, boolean startsWithMatch) 78 throws InterruptedException, IOException { 79 ArgumentCaptor<String[]> textArrayCaptor = ArgumentCaptor.forClass(String[].class); 80 81 //verify cmd passed to runtime 82 verify(mRuntime).exec(textArrayCaptor.capture()); 83 String[] argList = textArrayCaptor.getValue(); 84 if (startsWithMatch) { 85 assertEquals(cmd[0], argList[0]); 86 } else { 87 assertEquals(Arrays.toString(cmd), Arrays.toString(argList)); 88 } 89 ArgumentCaptor<String> textCaptor = ArgumentCaptor.forClass(String.class); 90 91 //make sure logcat output does not have errors 92 verify(mDropBoxManager, times(1)).addText(eq(tag), textCaptor.capture()); 93 assertFalse(textCaptor.getValue().contains(DiagnosticDataCollector.ERROR_MSG)); 94 } 95 96 @Test testPersistForTelecomDumpsys()97 public void testPersistForTelecomDumpsys() throws IOException, InterruptedException { 98 TelephonyManager.EmergencyCallDiagnosticData.Builder callDiagnosticBuilder = 99 new TelephonyManager.EmergencyCallDiagnosticData.Builder(); 100 TelephonyManager.EmergencyCallDiagnosticData ecdData = 101 callDiagnosticBuilder.setTelecomDumpsysCollectionEnabled(true).build(); 102 mDiagnosticDataCollector.persistEmergencyDianosticData( 103 mConfig, ecdData, "test_tag_telecom"); 104 105 verifyCmdAndDropboxTag(TELECOM_DUMPSYS_COMMAND, "test_tag_telecom", false); 106 } 107 108 @Test testPersistForTelephonyDumpsys()109 public void testPersistForTelephonyDumpsys() throws IOException, InterruptedException { 110 TelephonyManager.EmergencyCallDiagnosticData.Builder callDiagnosticBuilder = 111 new TelephonyManager.EmergencyCallDiagnosticData.Builder(); 112 TelephonyManager.EmergencyCallDiagnosticData ecdData = 113 callDiagnosticBuilder.setTelephonyDumpsysCollectionEnabled(true).build(); 114 mDiagnosticDataCollector.persistEmergencyDianosticData( 115 mConfig, ecdData, "test_tag_telephony"); 116 117 verifyCmdAndDropboxTag(TELEPHONY_DUMPSYS_COMMAND, "test_tag_telephony", false); 118 } 119 120 @Test testPersistForLogcat()121 public void testPersistForLogcat() throws IOException, InterruptedException { 122 TelephonyManager.EmergencyCallDiagnosticData.Builder callDiagnosticBuilder = 123 new TelephonyManager.EmergencyCallDiagnosticData.Builder(); 124 TelephonyManager.EmergencyCallDiagnosticData ecdData = 125 callDiagnosticBuilder.setLogcatCollectionStartTimeMillis( 126 SystemClock.elapsedRealtime()).build(); 127 mDiagnosticDataCollector.persistEmergencyDianosticData( 128 mConfig, ecdData, "test_tag_logcat"); 129 130 verifyCmdAndDropboxTag(LOGCAT_BINARY, "test_tag_logcat", true); 131 } 132 133 } 134