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 java.io.File; 22 import java.io.FileInputStream; 23 import java.io.IOException; 24 import java.io.InputStream; 25 import java.util.Arrays; 26 import java.util.stream.Stream; 27 import java.util.zip.ZipFile; 28 29 import static com.android.compatibility.common.util.SystemUtil.runShellCommand; 30 31 /** 32 * Performs the signature multi-libs check via a JUnit test. 33 */ 34 public class SignatureMultiLibsTest extends SignatureTest { 35 36 private static final String TAG = SignatureMultiLibsTest.class.getSimpleName(); 37 38 /** 39 * Tests that the device's API matches the expected set defined in xml. 40 * <p/> 41 * Will check the entire API, and then report the complete list of failures 42 */ testSignature()43 public void testSignature() { 44 runWithTestResultObserver(mResultObserver -> { 45 46 ApiComplianceChecker complianceChecker = 47 new ApiComplianceChecker(mResultObserver, classProvider); 48 49 ApiDocumentParser apiDocumentParser = new ApiDocumentParser(TAG); 50 51 parseApiFilesAsStream(apiDocumentParser, expectedApiFiles) 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 readFile(File file)73 protected Stream<InputStream> readFile(File file) { 74 try { 75 if (file.getName().endsWith(".zip")) { 76 ZipFile zip = new ZipFile(file); 77 return zip.stream().filter(entry -> checkLibrary(entry.getName())).map(entry -> { 78 try { 79 return zip.getInputStream(entry); 80 } catch (IOException e) { 81 throw new RuntimeException(e); 82 }}); 83 } else { 84 return Stream.of(new FileInputStream(file)); 85 } 86 } catch (IOException e) { 87 throw new RuntimeException(e); 88 } 89 } 90 } 91