1 /* 2 * Copyright (C) 2018 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 com.android.build.tests; 18 19 import org.junit.Assert; 20 import org.junit.Before; 21 import org.junit.Test; 22 import org.junit.runner.RunWith; 23 import org.junit.runners.JUnit4; 24 25 import java.io.ByteArrayInputStream; 26 import java.util.HashMap; 27 import java.util.Map; 28 import java.util.regex.Matcher; 29 import java.util.regex.Pattern; 30 31 /** Unit tests for {@link ImageStats} */ 32 @RunWith(JUnit4.class) 33 public class ImageStatsTest { 34 35 // data obtained from build 4597696, taimen-userdebug_fastbuild_linux 36 private static final String TEST_DATA = 37 " 164424453 /system/app/WallpapersBReel2017/WallpapersBReel2017.apk\n" 38 + " 124082279 /system/app/Chrome/Chrome.apk\n" 39 + " 92966112 /system/priv-app/Velvet/Velvet.apk\n" 40 + " 1790897 /system/framework/ext.jar\n" 41 + " 505436 /system/fonts/NotoSansEgyptianHieroglyphs-Regular.ttf\n" 42 + " 500448 /system/bin/ip6tables\n" 43 + " 500393 /system/usr/share/zoneinfo/tzdata\n" 44 + " 500380 /system/fonts/NotoSansCuneiform-Regular.ttf\n" 45 + " 126391 /system/framework/core-oj.jar\n" 46 + " 122641 /system/framework/com.quicinc.cne.jar\n"; 47 private static final Map<String, Long> PARSED_TEST_DATA = new HashMap<>(); 48 49 static { 50 PARSED_TEST_DATA.put("/system/app/WallpapersBReel2017/WallpapersBReel2017.apk", 164424453L); 51 PARSED_TEST_DATA.put("/system/app/Chrome/Chrome.apk", 124082279L); 52 PARSED_TEST_DATA.put("/system/priv-app/Velvet/Velvet.apk", 92966112L); 53 PARSED_TEST_DATA.put("/system/framework/ext.jar", 1790897L); 54 PARSED_TEST_DATA.put("/system/fonts/NotoSansEgyptianHieroglyphs-Regular.ttf", 505436L); 55 PARSED_TEST_DATA.put("/system/bin/ip6tables", 500448L); 56 PARSED_TEST_DATA.put("/system/usr/share/zoneinfo/tzdata", 500393L); 57 PARSED_TEST_DATA.put("/system/fonts/NotoSansCuneiform-Regular.ttf", 500380L); 58 PARSED_TEST_DATA.put("/system/framework/core-oj.jar", 126391L); 59 PARSED_TEST_DATA.put("/system/framework/com.quicinc.cne.jar", 122641L); 60 } 61 62 private ImageStats mImageStats = null; 63 64 @Before setup()65 public void setup() throws Exception { 66 mImageStats = new ImageStats(); 67 } 68 69 @Test testParseFileSizes()70 public void testParseFileSizes() throws Exception { 71 Map<String, Long> ret = 72 mImageStats.parseFileSizes(new ByteArrayInputStream(TEST_DATA.getBytes())); 73 Assert.assertEquals( 74 "parsed test file sizes mismatches expectations", PARSED_TEST_DATA, ret); 75 } 76 77 /** Verifies that regular matching pattern without capturing group works as expected */ 78 @Test testGetAggregationLabel_regular()79 public void testGetAggregationLabel_regular() throws Exception { 80 String fileName = "/system/app/WallpapersBReel2017/WallpapersBReel2017.apk"; 81 Pattern pattern = Pattern.compile("^.+\\.apk$"); 82 final String label = "foobar"; 83 Assert.assertEquals( 84 "unexpected label transformation output", 85 label, 86 mImageStats.getAggregationLabel(pattern.matcher(fileName), label)); 87 } 88 89 /** Verifies that matching pattern with corresponding capturing groups works as expected */ 90 @Test testGetAggregationLabel_capturingGroups()91 public void testGetAggregationLabel_capturingGroups() throws Exception { 92 String fileName = "/system/app/WallpapersBReel2017/WallpapersBReel2017.apk"; 93 Pattern pattern = Pattern.compile("^/system/(.+?)/.+\\.(.+)$"); 94 final String label = "folder-\\1-ext-\\2"; 95 Matcher m = pattern.matcher(fileName); 96 Assert.assertTrue( 97 "this shouldn't fail unless test case isn't written correctly", m.matches()); 98 Assert.assertEquals( 99 "unexpected label transformation output", 100 "folder-app-ext-apk", 101 mImageStats.getAggregationLabel(m, label)); 102 } 103 104 /** 105 * Verifies that matching pattern with capturing groups but partial back references works as 106 * expected 107 */ 108 @Test testGetAggregationLabel_capturingGroups_partialBackReference()109 public void testGetAggregationLabel_capturingGroups_partialBackReference() throws Exception { 110 String fileName = "/system/app/WallpapersBReel2017/WallpapersBReel2017.apk"; 111 Pattern pattern = Pattern.compile("^/system/(.+?)/.+\\.(.+)$"); 112 final String label = "ext-\\2"; 113 Matcher m = pattern.matcher(fileName); 114 Assert.assertTrue( 115 "this shouldn't fail unless test case isn't written correctly", m.matches()); 116 Assert.assertEquals( 117 "unexpected label transformation output", 118 "ext-apk", 119 mImageStats.getAggregationLabel(m, label)); 120 } 121 122 /** Verifies that aggregating the sample input with patterns works as expected */ 123 @Test testPerformAggregation()124 public void testPerformAggregation() throws Exception { 125 Map<Pattern, String> mapping = new HashMap<>(); 126 mapping.put(Pattern.compile("^.+\\.(.+)"), "ext-\\1"); // aggregate by extension 127 mapping.put(Pattern.compile("^/system/(.+?)/.+$"), "folder-\\1"); // aggregate by folder 128 Map<String, String> ret = mImageStats.performAggregation(PARSED_TEST_DATA, mapping); 129 Assert.assertEquals( 130 "failed to verify aggregated size for category 'ext-apk'", 131 "381472844", 132 ret.get("ext-apk")); 133 Assert.assertEquals( 134 "failed to verify aggregated size for category 'ext-jar'", 135 "2039929", 136 ret.get("ext-jar")); 137 Assert.assertEquals( 138 "failed to verify aggregated size for category 'ext-ttf'", 139 "1005816", 140 ret.get("ext-ttf")); 141 Assert.assertEquals( 142 "failed to verify aggregated size for category 'uncategorized'", 143 "0", 144 ret.get("uncategorized")); 145 Assert.assertEquals( 146 "failed to verify aggregated size for category 'total'", 147 "385519430", 148 ret.get("total")); 149 } 150 } 151