1 /*
2  * Copyright (C) 2011 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.signature.cts.api;
18 
19 import android.signature.cts.ApiComplianceChecker;
20 import android.signature.cts.ApiDocumentParser;
21 import android.signature.cts.VirtualPath;
22 import android.signature.cts.VirtualPath.LocalFilePath;
23 import java.io.IOException;
24 import java.util.Arrays;
25 import java.util.stream.Stream;
26 
27 import static com.android.compatibility.common.util.SystemUtil.runShellCommand;
28 
29 /**
30  * Performs the signature multi-libs check via a JUnit test.
31  */
32 public class SignatureMultiLibsTest extends SignatureTest {
33 
34     private static final String TAG = SignatureMultiLibsTest.class.getSimpleName();
35 
36     /**
37      * Tests that the device's API matches the expected set defined in xml.
38      * <p/>
39      * Will check the entire API, and then report the complete list of failures
40      */
testSignature()41     public void testSignature() {
42         runWithTestResultObserver(mResultObserver -> {
43 
44             ApiComplianceChecker complianceChecker =
45                     new ApiComplianceChecker(mResultObserver, mClassProvider);
46 
47             ApiDocumentParser apiDocumentParser = new ApiDocumentParser(TAG);
48 
49             parseApiResourcesAsStream(apiDocumentParser,
50                     Stream.concat(Arrays.stream(systemApiFiles), Arrays.stream(previousApiFiles))
51                     .toArray(String[]::new))
52                     .forEach(complianceChecker::checkSignatureCompliance);
53 
54             // After done parsing all expected API files, perform any deferred checks.
55             complianceChecker.checkDeferred();
56         });
57     }
58 
getLibraries()59     private Stream<String> getLibraries() {
60         try {
61             String result = runShellCommand(getInstrumentation(), "cmd package list libraries");
62             return Arrays.stream(result.split("\n")).map(line -> line.split(":")[1]);
63         } catch (IOException e) {
64             throw new RuntimeException(e);
65         }
66     }
67 
checkLibrary(String name)68     private boolean checkLibrary (String name) {
69         String libraryName = name.substring(name.lastIndexOf('/') + 1).split("-")[0];
70         return getLibraries().anyMatch(libraryName::equals);
71     }
72 
73     @Override
getZipEntryFiles(LocalFilePath path)74     protected Stream<VirtualPath> getZipEntryFiles(LocalFilePath path) throws IOException {
75         // Only return entries corresponding to shared libraries.
76         return super.getZipEntryFiles(path).filter(p -> checkLibrary(p.toString()));
77     }
78 }
79