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 android.car.cts.utils; 18 19 import android.app.UiAutomation; 20 import android.os.ParcelFileDescriptor; 21 22 import androidx.test.platform.app.InstrumentationRegistry; 23 24 import java.io.ByteArrayOutputStream; 25 import java.io.FileInputStream; 26 import java.io.IOException; 27 28 /** 29 * Class contains static methods to dump proto for car services 30 */ 31 public final class ProtoDumpUtils { ProtoDumpUtils()32 private ProtoDumpUtils() { 33 } 34 35 /** 36 * Dump proto by shell command 37 * 38 * @param serviceName Name of service to be dumped. 39 * @return byte array for dumped proto 40 */ executeProtoDumpShellCommand(String serviceName)41 public static byte[] executeProtoDumpShellCommand(String serviceName) { 42 UiAutomation uiAutomation = InstrumentationRegistry.getInstrumentation() 43 .getUiAutomation(); 44 ParcelFileDescriptor pfd = uiAutomation.executeShellCommand( 45 "dumpsys car_service --services " + serviceName + " --proto"); 46 try (FileInputStream fis = new ParcelFileDescriptor.AutoCloseInputStream(pfd)) { 47 byte[] buf = new byte[512]; 48 int bytesRead; 49 ByteArrayOutputStream stdout = new ByteArrayOutputStream(); 50 while ((bytesRead = fis.read(buf)) != -1) { 51 stdout.write(buf, 0, bytesRead); 52 } 53 return stdout.toByteArray(); 54 } catch (IOException e) { 55 throw new RuntimeException(e); 56 } 57 } 58 } 59