• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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.compat.testing.app;
18 
19 import static org.junit.Assume.assumeTrue;
20 
21 import android.app.Instrumentation;
22 import android.content.Context;
23 import android.content.pm.SharedLibraryInfo;
24 import android.util.Log;
25 
26 import androidx.test.platform.app.InstrumentationRegistry;
27 import androidx.test.runner.AndroidJUnit4;
28 
29 import com.android.modules.utils.build.SdkLevel;
30 
31 import com.google.common.collect.ImmutableList;
32 
33 import org.junit.After;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 
38 import java.io.File;
39 import java.nio.file.Files;
40 import java.nio.file.Path;
41 import java.util.List;
42 import java.util.Locale;
43 
44 /**
45  * Device-side helper app for obtaining shared libraries.
46  *
47  * <p>It is not technically a test as it simply collects information, but it simplifies the usage
48  * and communication with host-side tests.
49  */
50 @RunWith(AndroidJUnit4.class)
51 public class SharedLibraryInfoDeviceTest {
52 
53     private static final String TAG = "SharedLibraryInfoDeviceTest";
54 
55     private final Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
56     private final Context context = instrumentation.getTargetContext();
57 
58     @Before
before()59     public void before() {
60         assumeTrue(SdkLevel.isAtLeastS());
61         instrumentation.getUiAutomation().adoptShellPermissionIdentity();
62     }
63 
64     @After
after()65     public void after() {
66         instrumentation.getUiAutomation().dropShellPermissionIdentity();
67     }
68 
69     /**
70      * Collects details about all shared libraries on the device and writes them to disk.
71      */
72     @Test
collectSharedLibraryPaths()73     public void collectSharedLibraryPaths() throws Exception {
74         List<SharedLibraryInfo> sharedLibraries =
75                 context.getPackageManager().getSharedLibraries(0);
76 
77         ImmutableList.Builder<String> content = ImmutableList.builder();
78         for (SharedLibraryInfo sharedLibrary : sharedLibraries) {
79             if (!sharedLibrary.isNative()) {
80                 content.add(String.format(Locale.US, "%s %d %d %s",
81                         sharedLibrary.getName(),
82                         sharedLibrary.getType(),
83                         sharedLibrary.getLongVersion(),
84                         String.join(" ", sharedLibrary.getAllCodePaths())));
85             }
86         }
87 
88         Path detailsFilepath = new File("/sdcard/shared-libs.txt").toPath();
89         ImmutableList<String> lines = content.build();
90         Log.i(TAG, String.format("Writing details about %d shared libraries to %s",
91                 lines.size(), detailsFilepath));
92         Files.write(detailsFilepath, lines);
93     }
94 
95 }
96