1 /* 2 * Copyright (C) 2017 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.compatibility.common.tradefed.presubmit; 17 18 import static org.junit.Assert.assertNotNull; 19 import static org.junit.Assert.assertTrue; 20 import static org.junit.Assert.fail; 21 22 import com.android.tradefed.config.ConfigurationFactory; 23 import com.android.tradefed.config.IConfiguration; 24 import com.android.tradefed.targetprep.ITargetPreparer; 25 import com.android.tradefed.targetprep.TestAppInstallSetup; 26 import com.android.tradefed.util.AaptParser; 27 28 import org.junit.Test; 29 import org.junit.runner.RunWith; 30 import org.junit.runners.JUnit4; 31 32 import java.io.File; 33 import java.io.FilenameFilter; 34 import java.util.ArrayList; 35 import java.util.HashMap; 36 import java.util.HashSet; 37 import java.util.List; 38 import java.util.Map; 39 import java.util.Set; 40 41 /** 42 * Class to validate tests Apks in testcases/ 43 */ 44 @RunWith(JUnit4.class) 45 public class ApkPackageNameCheck { 46 47 private static final Set<String> EXCEPTION_LIST = new HashSet<>(); 48 static { 49 // TODO: Remove exception when their package have been fixed. 50 EXCEPTION_LIST.add("android.app.cts"); 51 EXCEPTION_LIST.add("android.systemui.cts"); 52 } 53 54 /** 55 * We ensure that no apk with same package names may be installed. Otherwise it may results in 56 * conflicts. 57 */ 58 @Test 59 public void testApkPackageNames() throws Exception { 60 String ctsRoot = System.getProperty("CTS_ROOT"); 61 File testcases = new File(ctsRoot, "/android-cts/testcases/"); 62 if (!testcases.exists()) { 63 fail(String.format("%s does not exists", testcases)); 64 return; 65 } 66 File[] listConfig = testcases.listFiles(new FilenameFilter() { 67 @Override 68 public boolean accept(File dir, String name) { 69 if (name.endsWith(".config")) { 70 return true; 71 } 72 return false; 73 } 74 }); 75 assertTrue(listConfig.length > 0); 76 // We check all apk installed by all modules 77 Map<String, String> packageNames = new HashMap<>(); 78 79 for (File config : listConfig) { 80 IConfiguration c = ConfigurationFactory.getInstance() 81 .createConfigurationFromArgs(new String[] {config.getAbsolutePath()}); 82 // For each config, we check all the apk it's going to install 83 List<String> apkNames = new ArrayList<>(); 84 for (ITargetPreparer prep : c.getTargetPreparers()) { 85 if (prep instanceof TestAppInstallSetup) { 86 apkNames.addAll(((TestAppInstallSetup) prep).getTestsFileName()); 87 } 88 } 89 90 for (String apkName : apkNames) { 91 File apkFile = new File(testcases, apkName); 92 if (!apkFile.exists()) { 93 fail(String.format("Module %s is trying to install %s which does not " 94 + "exists in testcases/", config.getName(), apkFile)); 95 } 96 AaptParser res = AaptParser.parse(apkFile); 97 assertNotNull(res); 98 String packageName = res.getPackageName(); 99 String put = packageNames.put(packageName, apkName); 100 // The package already exists and it's a different apk 101 if (put != null && !apkName.equals(put) && !EXCEPTION_LIST.contains(packageName)) { 102 fail(String.format("Module %s: Package name '%s' from apk '%s' was already " 103 + "added by previous apk '%s'.", 104 config.getName(), packageName, apkName, put)); 105 } 106 } 107 } 108 } 109 } 110