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 package com.android.trout.dumpstate.tests;
17 
18 import android.hardware.dumpstate.V1_1.DumpstateMode;
19 import android.hardware.dumpstate.V1_1.DumpstateStatus;
20 import android.hardware.dumpstate.V1_1.IDumpstateDevice;
21 import android.os.NativeHandle;
22 import android.util.Log;
23 import androidx.test.filters.MediumTest;
24 import java.io.File;
25 import java.io.FileDescriptor;
26 import java.io.FileInputStream;
27 import java.io.FileOutputStream;
28 import org.apache.commons.compress.archivers.ArchiveStreamFactory;
29 import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
30 import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
31 import org.junit.Assert;
32 import org.junit.Before;
33 import org.junit.Rule;
34 import org.junit.Test;
35 import org.junit.rules.TemporaryFolder;
36 import org.junit.runner.RunWith;
37 import org.junit.runners.JUnit4;
38 
39 /**
40  * Tests used to validate Trout dumpstate E2E functionality.
41  */
42 @RunWith(JUnit4.class)
43 public class DumpstateE2eTests {
44     private static final String TAG = DumpstateE2eTests.class.getSimpleName();
45     private IDumpstateDevice mDevice;
46 
47     @Rule public TemporaryFolder mTempFolder = new TemporaryFolder();
48 
49     @Before
setUp()50     public void setUp() throws Exception {
51         mDevice = android.hardware.dumpstate.V1_1.IDumpstateDevice.getService(true /* retry */);
52     }
53 
54     @Test
55     @MediumTest
testDumpstateBoard()56     public void testDumpstateBoard() throws Exception {
57         File dumpstate_board_txt_file = mTempFolder.newFile();
58         File dumpstate_board_bin_file = mTempFolder.newFile();
59 
60         try (FileOutputStream dumpstate_board_txt_ostream =
61                         new FileOutputStream(dumpstate_board_txt_file.getPath());
62                 FileOutputStream dumpstate_board_bin_ostream =
63                         new FileOutputStream(dumpstate_board_bin_file.getPath());) {
64             NativeHandle handle = new NativeHandle(
65                     new FileDescriptor[] {
66                             dumpstate_board_txt_ostream.getFD(),
67                             dumpstate_board_bin_ostream.getFD(),
68                     },
69                     new int[0], false);
70             int dumping_status = mDevice.dumpstateBoard_1_1(
71                     handle, DumpstateMode.DEFAULT, 10 * 1000 /* milliseconds */);
72             Assert.assertEquals(DumpstateStatus.OK, dumping_status);
73         }
74 
75         Assert.assertNotEquals(0, dumpstate_board_txt_file.length());
76         Assert.assertNotEquals(0, dumpstate_board_bin_file.length());
77 
78         try (FileInputStream dumpstate_board_tar_istream =
79                         new FileInputStream(dumpstate_board_bin_file.getPath());
80                 TarArchiveInputStream tar_stream =
81                         (TarArchiveInputStream) new ArchiveStreamFactory().createArchiveInputStream(
82                                 "tar", dumpstate_board_tar_istream);) {
83             // Traversing the tar package to make sure it is valid
84             TarArchiveEntry tar_entry = null;
85             while ((tar_entry = (TarArchiveEntry) tar_stream.getNextEntry()) != null) {
86                 Log.d(TAG, tar_entry.getName());
87             }
88         }
89     }
90 }
91