Makoto Onuki | 0b575a3 | 2018-04-16 14:33:59 -0700 | [diff] [blame] | 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 | package com.android.internal.util; |
| 17 | |
Vishnu Nair | c69cd44 | 2018-07-02 14:29:31 -0700 | [diff] [blame] | 18 | import static com.android.internal.util.DumpUtils.CRITICAL_SECTION_COMPONENTS; |
Makoto Onuki | 0b575a3 | 2018-04-16 14:33:59 -0700 | [diff] [blame] | 19 | import static com.android.internal.util.DumpUtils.filterRecord; |
| 20 | import static com.android.internal.util.DumpUtils.isNonPlatformPackage; |
Vishnu Nair | c69cd44 | 2018-07-02 14:29:31 -0700 | [diff] [blame] | 21 | import static com.android.internal.util.DumpUtils.isPlatformCriticalPackage; |
| 22 | import static com.android.internal.util.DumpUtils.isPlatformNonCriticalPackage; |
Makoto Onuki | 0b575a3 | 2018-04-16 14:33:59 -0700 | [diff] [blame] | 23 | import static com.android.internal.util.DumpUtils.isPlatformPackage; |
| 24 | |
| 25 | import android.content.ComponentName; |
| 26 | |
| 27 | import junit.framework.TestCase; |
| 28 | |
| 29 | /** |
| 30 | * Run with: |
Vishnu Nair | c69cd44 | 2018-07-02 14:29:31 -0700 | [diff] [blame] | 31 | atest FrameworksCoreTests:DumpUtilsTest |
Makoto Onuki | 0b575a3 | 2018-04-16 14:33:59 -0700 | [diff] [blame] | 32 | */ |
| 33 | public class DumpUtilsTest extends TestCase { |
| 34 | |
| 35 | private static ComponentName cn(String componentName) { |
| 36 | if (componentName == null) { |
| 37 | return null; |
| 38 | } |
| 39 | return ComponentName.unflattenFromString(componentName); |
| 40 | } |
| 41 | |
| 42 | private static ComponentName.WithComponentName wcn(String componentName) { |
| 43 | if (componentName == null) { |
| 44 | return null; |
| 45 | } |
| 46 | return () -> cn(componentName); |
| 47 | } |
| 48 | |
| 49 | public void testIsPlatformPackage() { |
| 50 | assertTrue(isPlatformPackage("android")); |
| 51 | assertTrue(isPlatformPackage("android.abc")); |
| 52 | assertTrue(isPlatformPackage("com.android.abc")); |
| 53 | |
| 54 | assertFalse(isPlatformPackage((String) null)); |
| 55 | assertFalse(isPlatformPackage("com.google")); |
| 56 | |
| 57 | assertTrue(isPlatformPackage(cn("android/abc"))); |
| 58 | assertTrue(isPlatformPackage(cn("android.abc/abc"))); |
| 59 | assertTrue(isPlatformPackage(cn("com.android.def/abc"))); |
| 60 | |
| 61 | assertFalse(isPlatformPackage(cn(null))); |
| 62 | assertFalse(isPlatformPackage(cn("com.google.def/abc"))); |
| 63 | |
| 64 | assertTrue(isPlatformPackage(wcn("android/abc"))); |
| 65 | assertTrue(isPlatformPackage(wcn("android.abc/abc"))); |
| 66 | assertTrue(isPlatformPackage(wcn("com.android.def/abc"))); |
| 67 | |
| 68 | assertFalse(isPlatformPackage(wcn(null))); |
| 69 | assertFalse(isPlatformPackage(wcn("com.google.def/abc"))); |
| 70 | } |
| 71 | |
| 72 | public void testIsNonPlatformPackage() { |
| 73 | assertFalse(isNonPlatformPackage("android")); |
| 74 | assertFalse(isNonPlatformPackage("android.abc")); |
| 75 | assertFalse(isNonPlatformPackage("com.android.abc")); |
| 76 | |
| 77 | assertFalse(isNonPlatformPackage((String) null)); |
| 78 | assertTrue(isNonPlatformPackage("com.google")); |
| 79 | |
| 80 | assertFalse(isNonPlatformPackage(cn("android/abc"))); |
| 81 | assertFalse(isNonPlatformPackage(cn("android.abc/abc"))); |
| 82 | assertFalse(isNonPlatformPackage(cn("com.android.def/abc"))); |
| 83 | |
| 84 | assertFalse(isNonPlatformPackage(cn(null))); |
| 85 | assertTrue(isNonPlatformPackage(cn("com.google.def/abc"))); |
| 86 | |
| 87 | assertFalse(isNonPlatformPackage(wcn("android/abc"))); |
| 88 | assertFalse(isNonPlatformPackage(wcn("android.abc/abc"))); |
| 89 | assertFalse(isNonPlatformPackage(wcn("com.android.def/abc"))); |
| 90 | |
| 91 | assertFalse(isNonPlatformPackage(wcn(null))); |
| 92 | assertTrue(isNonPlatformPackage(wcn("com.google.def/abc"))); |
| 93 | } |
| 94 | |
Vishnu Nair | c69cd44 | 2018-07-02 14:29:31 -0700 | [diff] [blame] | 95 | public void testIsPlatformCriticalPackage() { |
| 96 | for (final ComponentName componentName : CRITICAL_SECTION_COMPONENTS) { |
| 97 | assertTrue(isPlatformCriticalPackage(() -> componentName)); |
| 98 | assertTrue(isPlatformPackage(componentName)); |
| 99 | } |
| 100 | assertFalse(isPlatformCriticalPackage(wcn("com.google.p/abc"))); |
| 101 | assertFalse(isPlatformCriticalPackage(wcn("com.android.def/abc"))); |
| 102 | assertFalse(isPlatformCriticalPackage(wcn("com.android.abc"))); |
| 103 | assertFalse(isPlatformCriticalPackage(wcn("com.android"))); |
| 104 | assertFalse(isPlatformCriticalPackage(wcn(null))); |
| 105 | assertFalse(isPlatformCriticalPackage(null)); |
| 106 | } |
| 107 | |
| 108 | public void testIsPlatformNonCriticalPackage() { |
| 109 | for (final ComponentName componentName : CRITICAL_SECTION_COMPONENTS) { |
| 110 | assertFalse(isPlatformNonCriticalPackage(() -> componentName)); |
| 111 | } |
| 112 | assertTrue(isPlatformNonCriticalPackage(wcn("android/abc"))); |
| 113 | assertTrue(isPlatformNonCriticalPackage(wcn("android.abc/abc"))); |
| 114 | assertTrue(isPlatformNonCriticalPackage(wcn("com.android.def/abc"))); |
| 115 | |
| 116 | assertFalse(isPlatformNonCriticalPackage(wcn("com.google.def/abc"))); |
| 117 | assertFalse(isPlatformNonCriticalPackage(wcn(null))); |
| 118 | assertFalse(isPlatformNonCriticalPackage(null)); |
| 119 | } |
| 120 | |
Makoto Onuki | 0b575a3 | 2018-04-16 14:33:59 -0700 | [diff] [blame] | 121 | public void testFilterRecord() { |
| 122 | assertFalse(filterRecord(null).test(wcn("com.google.p/abc"))); |
| 123 | assertFalse(filterRecord(null).test(wcn("com.android.p/abc"))); |
| 124 | |
| 125 | assertTrue(filterRecord("all").test(wcn("com.google.p/abc"))); |
| 126 | assertTrue(filterRecord("all").test(wcn("com.android.p/abc"))); |
| 127 | assertFalse(filterRecord("all").test(wcn(null))); |
| 128 | |
| 129 | assertFalse(filterRecord("all-platform").test(wcn("com.google.p/abc"))); |
| 130 | assertTrue(filterRecord("all-platform").test(wcn("com.android.p/abc"))); |
| 131 | assertFalse(filterRecord("all-platform").test(wcn(null))); |
| 132 | |
| 133 | assertTrue(filterRecord("all-non-platform").test(wcn("com.google.p/abc"))); |
| 134 | assertFalse(filterRecord("all-non-platform").test(wcn("com.android.p/abc"))); |
| 135 | assertFalse(filterRecord("all-non-platform").test(wcn(null))); |
| 136 | |
Vishnu Nair | c69cd44 | 2018-07-02 14:29:31 -0700 | [diff] [blame] | 137 | for (final ComponentName componentName : CRITICAL_SECTION_COMPONENTS) { |
| 138 | assertTrue(filterRecord("all-platform-critical").test((() -> componentName))); |
| 139 | assertFalse(filterRecord("all-platform-non-critical").test((() -> componentName))); |
| 140 | assertTrue(filterRecord("all-platform").test((() -> componentName))); |
| 141 | } |
| 142 | assertFalse(filterRecord("all-platform-critical").test(wcn("com.google.p/abc"))); |
| 143 | assertFalse(filterRecord("all-platform-critical").test(wcn("com.android.p/abc"))); |
| 144 | assertFalse(filterRecord("all-platform-critical").test(wcn(null))); |
| 145 | |
| 146 | assertTrue(filterRecord("all-platform-non-critical").test(wcn("com.android.p/abc"))); |
| 147 | assertFalse(filterRecord("all-platform-non-critical").test(wcn("com.google.p/abc"))); |
| 148 | assertFalse(filterRecord("all-platform-non-critical").test(wcn(null))); |
| 149 | |
Makoto Onuki | 0b575a3 | 2018-04-16 14:33:59 -0700 | [diff] [blame] | 150 | // Partial string match. |
| 151 | assertTrue(filterRecord("abc").test(wcn("com.google.p/.abc"))); |
| 152 | assertFalse(filterRecord("abc").test(wcn("com.google.p/.def"))); |
| 153 | assertTrue(filterRecord("com").test(wcn("com.google.p/.xyz"))); |
| 154 | |
| 155 | // Full component name match. |
| 156 | assertTrue(filterRecord("com.google/com.google.abc").test(wcn("com.google/.abc"))); |
| 157 | assertFalse(filterRecord("com.google/com.google.abc").test(wcn("com.google/.abc.def"))); |
| 158 | |
| 159 | |
| 160 | // Hex ID match |
| 161 | ComponentName.WithComponentName component = wcn("com.google/.abc"); |
| 162 | |
| 163 | assertTrue(filterRecord( |
| 164 | Integer.toHexString(System.identityHashCode(component))).test(component)); |
| 165 | // Same component name, but different ID, no match. |
| 166 | assertFalse(filterRecord( |
| 167 | Integer.toHexString(System.identityHashCode(component))).test( |
| 168 | wcn("com.google/.abc"))); |
| 169 | } |
| 170 | } |