PackageParserTest.java revision 988149c925b583f5c77f567fa622e39309bc4ae9
1/* 2 * Copyright (C) 2016 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 */ 16package com.android.server.pm; 17 18import android.content.pm.PackageParser; 19import android.support.test.runner.AndroidJUnit4; 20import android.test.suitebuilder.annotation.MediumTest; 21 22import java.io.File; 23import java.nio.charset.StandardCharsets; 24 25import static org.junit.Assert.*; 26import org.junit.Before; 27import org.junit.Test; 28import org.junit.runner.RunWith; 29 30import libcore.io.IoUtils; 31 32@RunWith(AndroidJUnit4.class) 33@MediumTest 34public class PackageParserTest { 35 private File mTmpDir; 36 private static final File FRAMEWORK = new File("/system/framework/framework-res.apk"); 37 38 @Before 39 public void setUp() { 40 // Create a new temporary directory for each of our tests. 41 mTmpDir = IoUtils.createTemporaryDirectory("PackageParserTest"); 42 } 43 44 @Test 45 public void testParse_noCache() throws Exception { 46 PackageParser pp = new CachePackageNameParser(); 47 PackageParser.Package pkg = pp.parsePackage(FRAMEWORK, 0 /* parseFlags */, 48 false /* useCaches */); 49 assertNotNull(pkg); 50 51 pp.setCacheDir(mTmpDir); 52 pkg = pp.parsePackage(FRAMEWORK, 0 /* parseFlags */, 53 false /* useCaches */); 54 assertNotNull(pkg); 55 56 // Make sure that we always write out a cache entry for future reference, 57 // whether or not we're asked to use caches. 58 assertEquals(1, mTmpDir.list().length); 59 } 60 61 @Test 62 public void testParse_withCache() throws Exception { 63 PackageParser pp = new CachePackageNameParser(); 64 65 pp.setCacheDir(mTmpDir); 66 // The first parse will write this package to the cache. 67 pp.parsePackage(FRAMEWORK, 0 /* parseFlags */, true /* useCaches */); 68 69 // Now attempt to parse the package again, should return the 70 // cached result. 71 PackageParser.Package pkg = pp.parsePackage(FRAMEWORK, 0 /* parseFlags */, 72 true /* useCaches */); 73 assertEquals("cache_android", pkg.packageName); 74 75 // Try again, with useCaches == false, shouldn't return the parsed 76 // result. 77 pkg = pp.parsePackage(FRAMEWORK, 0 /* parseFlags */, false /* useCaches */); 78 assertEquals("android", pkg.packageName); 79 80 // We haven't set a cache directory here : the parse should still succeed, 81 // just not using the cached results. 82 pp = new CachePackageNameParser(); 83 pkg = pp.parsePackage(FRAMEWORK, 0 /* parseFlags */, true /* useCaches */); 84 assertEquals("android", pkg.packageName); 85 86 pkg = pp.parsePackage(FRAMEWORK, 0 /* parseFlags */, false /* useCaches */); 87 assertEquals("android", pkg.packageName); 88 } 89 90 /** 91 * A trivial subclass of package parser that only caches the package name, and throws away 92 * all other information. 93 */ 94 public static class CachePackageNameParser extends PackageParser { 95 @Override 96 public byte[] toCacheEntry(Package pkg) { 97 return ("cache_" + pkg.packageName).getBytes(StandardCharsets.UTF_8); 98 } 99 100 @Override 101 public Package fromCacheEntry(byte[] cacheEntry) { 102 return new Package(new String(cacheEntry, StandardCharsets.UTF_8)); 103 } 104 } 105} 106