| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.android.updatablesystemfont; |
| 18 |
|
| 19 |
import static android.graphics.fonts.FontStyle.FONT_SLANT_UPRIGHT; |
| 20 |
import static android.graphics.fonts.FontStyle.FONT_WEIGHT_BOLD; |
| 21 |
import static android.graphics.fonts.FontStyle.FONT_WEIGHT_NORMAL; |
| 22 |
import static android.os.ParcelFileDescriptor.MODE_READ_ONLY; |
| 23 |
|
| 24 |
import static com.google.common.truth.Truth.assertThat; |
| 25 |
|
| 26 |
import static org.junit.Assert.assertThrows; |
| 27 |
import static org.junit.Assume.assumeTrue; |
| 28 |
|
| 29 |
import static java.util.concurrent.TimeUnit.SECONDS; |
| 30 |
|
| 31 |
import android.app.UiAutomation; |
| 32 |
import android.content.Context; |
| 33 |
import android.graphics.fonts.FontFamilyUpdateRequest; |
| 34 |
import android.graphics.fonts.FontFileUpdateRequest; |
| 35 |
import android.graphics.fonts.FontManager; |
| 36 |
import android.graphics.fonts.FontStyle; |
| 37 |
import android.os.ParcelFileDescriptor; |
| 38 |
import android.platform.test.annotations.RootPermissionTest; |
| 39 |
import android.security.FileIntegrityManager; |
| 40 |
import android.text.FontConfig; |
| 41 |
import android.util.Log; |
| 42 |
import android.util.Pair; |
| 43 |
|
| 44 |
import androidx.annotation.Nullable; |
| 45 |
import androidx.test.ext.junit.runners.AndroidJUnit4; |
| 46 |
import androidx.test.platform.app.InstrumentationRegistry; |
| 47 |
import androidx.test.uiautomator.By; |
| 48 |
import androidx.test.uiautomator.UiDevice; |
| 49 |
import androidx.test.uiautomator.Until; |
| 50 |
|
| 51 |
import com.android.compatibility.common.util.StreamUtil; |
| 52 |
import com.android.compatibility.common.util.SystemUtil; |
| 53 |
|
| 54 |
import org.junit.After; |
| 55 |
import org.junit.Before; |
| 56 |
import org.junit.Test; |
| 57 |
import org.junit.runner.RunWith; |
| 58 |
|
| 59 |
import java.io.File; |
| 60 |
import java.io.FileInputStream; |
| 61 |
import java.io.FileOutputStream; |
| 62 |
import java.io.IOException; |
| 63 |
import java.io.InputStream; |
| 64 |
import java.io.OutputStream; |
| 65 |
import java.nio.file.Files; |
| 66 |
import java.nio.file.Paths; |
| 67 |
import java.util.ArrayList; |
| 68 |
import java.util.Arrays; |
| 69 |
import java.util.Collections; |
| 70 |
import java.util.List; |
| 71 |
import java.util.regex.Pattern; |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
@RootPermissionTest |
| 77 |
@RunWith(AndroidJUnit4.class) |
| 78 |
public class UpdatableSystemFontTest { |
| 79 |
|
| 80 |
private static final String TAG = "UpdatableSystemFontTest"; |
| 81 |
private static final String SYSTEM_FONTS_DIR = "/system/fonts/"; |
| 82 |
private static final String DATA_FONTS_DIR = "/data/fonts/files/"; |
| 83 |
private static final String CERT_PATH = "/data/local/tmp/UpdatableSystemFontTestCert.der"; |
| 84 |
|
| 85 |
private static final String NOTO_COLOR_EMOJI_POSTSCRIPT_NAME = "NotoColorEmoji"; |
| 86 |
private static final String NOTO_COLOR_EMOJI_TTF = |
| 87 |
"/data/local/tmp/UpdatableSystemFontTest_NotoColorEmoji.ttf"; |
| 88 |
private static final String NOTO_COLOR_EMOJI_SIG = |
| 89 |
"/data/local/tmp/UpdatableSystemFontTest_NotoColorEmoji.sig"; |
| 90 |
|
| 91 |
private static final String TEST_NOTO_COLOR_EMOJI_V0_TTF = |
| 92 |
"/data/local/tmp/UpdatableSystemFontTest_NotoColorEmojiV0.ttf"; |
| 93 |
private static final String TEST_NOTO_COLOR_EMOJI_V0_SIG = |
| 94 |
"/data/local/tmp/UpdatableSystemFontTest_NotoColorEmojiV0.sig"; |
| 95 |
|
| 96 |
private static final String TEST_NOTO_COLOR_EMOJI_VPLUS1_TTF = |
| 97 |
"/data/local/tmp/UpdatableSystemFontTest_NotoColorEmojiVPlus1.ttf"; |
| 98 |
private static final String TEST_NOTO_COLOR_EMOJI_VPLUS1_SIG = |
| 99 |
"/data/local/tmp/UpdatableSystemFontTest_NotoColorEmojiVPlus1.sig"; |
| 100 |
|
| 101 |
private static final String TEST_NOTO_COLOR_EMOJI_VPLUS2_TTF = |
| 102 |
"/data/local/tmp/UpdatableSystemFontTest_NotoColorEmojiVPlus2.ttf"; |
| 103 |
private static final String TEST_NOTO_COLOR_EMOJI_VPLUS2_SIG = |
| 104 |
"/data/local/tmp/UpdatableSystemFontTest_NotoColorEmojiVPlus2.sig"; |
| 105 |
|
| 106 |
private static final String NOTO_SERIF_REGULAR_POSTSCRIPT_NAME = "NotoSerif"; |
| 107 |
private static final String NOTO_SERIF_REGULAR_TTF = |
| 108 |
"/data/local/tmp/UpdatableSystemFontTest_NotoSerif-Regular.ttf"; |
| 109 |
private static final String NOTO_SERIF_REGULAR_SIG = |
| 110 |
"/data/local/tmp/UpdatableSystemFontTest_NotoSerif-Regular.sig"; |
| 111 |
|
| 112 |
private static final String NOTO_SERIF_BOLD_POSTSCRIPT_NAME = "NotoSerif-Bold"; |
| 113 |
private static final String NOTO_SERIF_BOLD_TTF = |
| 114 |
"/data/local/tmp/UpdatableSystemFontTest_NotoSerif-Bold.ttf"; |
| 115 |
private static final String NOTO_SERIF_BOLD_SIG = |
| 116 |
"/data/local/tmp/UpdatableSystemFontTest_NotoSerif-Bold.sig"; |
| 117 |
|
| 118 |
private static final String EMOJI_RENDERING_TEST_APP_ID = "com.android.emojirenderingtestapp"; |
| 119 |
private static final String EMOJI_RENDERING_TEST_ACTIVITY = |
| 120 |
EMOJI_RENDERING_TEST_APP_ID + "/.EmojiRenderingTestActivity"; |
| 121 |
|
| 122 |
private static final String TEST_NOTO_SERIF = "test-noto-serif"; |
| 123 |
private static final long ACTIVITY_TIMEOUT_MILLIS = SECONDS.toMillis(10); |
| 124 |
|
| 125 |
private static final String GET_AVAILABLE_FONTS_TEST_ACTIVITY = |
| 126 |
EMOJI_RENDERING_TEST_APP_ID + "/.GetAvailableFontsTestActivity"; |
| 127 |
|
| 128 |
private static final Pattern PATTERN_FONT_FILES = Pattern.compile("\\.(ttf|otf|ttc|otc)$"); |
| 129 |
private static final Pattern PATTERN_TMP_FILES = Pattern.compile("^/data/local/tmp/"); |
| 130 |
private static final Pattern PATTERN_DATA_FONT_FILES = Pattern.compile("^/data/fonts/files/"); |
| 131 |
private static final Pattern PATTERN_SYSTEM_FONT_FILES = |
| 132 |
Pattern.compile("^/(system|product)/fonts/"); |
| 133 |
|
| 134 |
private String mKeyId; |
| 135 |
private FontManager mFontManager; |
| 136 |
private UiDevice mUiDevice; |
| 137 |
|
| 138 |
@Before |
| 139 |
public void setUp() throws Exception { |
| 140 |
Context context = InstrumentationRegistry.getInstrumentation().getTargetContext(); |
| 141 |
|
| 142 |
FileIntegrityManager fim = context.getSystemService(FileIntegrityManager.class); |
| 143 |
assumeTrue(fim != null); |
| 144 |
assumeTrue(fim.isApkVeritySupported()); |
| 145 |
mKeyId = insertCert(CERT_PATH); |
| 146 |
mFontManager = context.getSystemService(FontManager.class); |
| 147 |
expectCommandToSucceed("cmd font clear"); |
| 148 |
mUiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()); |
| 149 |
} |
| 150 |
|
| 151 |
@After |
| 152 |
public void tearDown() throws Exception { |
| 153 |
|
| 154 |
runShellCommand("cmd font clear", null); |
| 155 |
if (mKeyId != null) { |
| 156 |
expectCommandToSucceed("mini-keyctl unlink " + mKeyId + " .fs-verity"); |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
@Test |
| 161 |
public void updateFont() throws Exception { |
| 162 |
FontConfig oldFontConfig = |
| 163 |
SystemUtil.callWithShellPermissionIdentity(mFontManager::getFontConfig); |
| 164 |
assertThat(updateFontFile( |
| 165 |
TEST_NOTO_COLOR_EMOJI_VPLUS1_TTF, TEST_NOTO_COLOR_EMOJI_VPLUS1_SIG)) |
| 166 |
.isEqualTo(FontManager.RESULT_SUCCESS); |
| 167 |
|
| 168 |
String fontPath = getFontPath(NOTO_COLOR_EMOJI_POSTSCRIPT_NAME); |
| 169 |
assertThat(fontPath).startsWith(DATA_FONTS_DIR); |
| 170 |
FontConfig newFontConfig = |
| 171 |
SystemUtil.callWithShellPermissionIdentity(mFontManager::getFontConfig); |
| 172 |
assertThat(newFontConfig.getConfigVersion()) |
| 173 |
.isGreaterThan(oldFontConfig.getConfigVersion()); |
| 174 |
assertThat(newFontConfig.getLastModifiedTimeMillis()) |
| 175 |
.isGreaterThan(oldFontConfig.getLastModifiedTimeMillis()); |
| 176 |
|
| 177 |
expectCommandToSucceed("dd status=none if=" + fontPath + " of=/dev/null"); |
| 178 |
expectCommandToFail("dd status=none if=" + CERT_PATH + " of=" + fontPath); |
| 179 |
} |
| 180 |
|
| 181 |
@Test |
| 182 |
public void updateFont_twice() throws Exception { |
| 183 |
assertThat(updateFontFile( |
| 184 |
TEST_NOTO_COLOR_EMOJI_VPLUS1_TTF, TEST_NOTO_COLOR_EMOJI_VPLUS1_SIG)) |
| 185 |
.isEqualTo(FontManager.RESULT_SUCCESS); |
| 186 |
String fontPath = getFontPath(NOTO_COLOR_EMOJI_POSTSCRIPT_NAME); |
| 187 |
assertThat(updateFontFile( |
| 188 |
TEST_NOTO_COLOR_EMOJI_VPLUS2_TTF, TEST_NOTO_COLOR_EMOJI_VPLUS2_SIG)) |
| 189 |
.isEqualTo(FontManager.RESULT_SUCCESS); |
| 190 |
String fontPath2 = getFontPath(NOTO_COLOR_EMOJI_POSTSCRIPT_NAME); |
| 191 |
assertThat(fontPath2).startsWith(DATA_FONTS_DIR); |
| 192 |
assertThat(fontPath2).isNotEqualTo(fontPath); |
| 193 |
|
| 194 |
expectCommandToSucceed("dd status=none if=" + fontPath2 + " of=/dev/null"); |
| 195 |
|
| 196 |
expectCommandToSucceed("dd status=none if=" + fontPath + " of=/dev/null"); |
| 197 |
} |
| 198 |
|
| 199 |
@Test |
| 200 |
public void updateFont_allowSameVersion() throws Exception { |
| 201 |
|
| 202 |
assertThat(updateFontFile( |
| 203 |
NOTO_COLOR_EMOJI_TTF, NOTO_COLOR_EMOJI_SIG)) |
| 204 |
.isEqualTo(FontManager.RESULT_SUCCESS); |
| 205 |
String fontPath = getFontPath(NOTO_COLOR_EMOJI_POSTSCRIPT_NAME); |
| 206 |
assertThat(updateFontFile( |
| 207 |
TEST_NOTO_COLOR_EMOJI_VPLUS1_TTF, TEST_NOTO_COLOR_EMOJI_VPLUS1_SIG)) |
| 208 |
.isEqualTo(FontManager.RESULT_SUCCESS); |
| 209 |
String fontPath2 = getFontPath(NOTO_COLOR_EMOJI_POSTSCRIPT_NAME); |
| 210 |
|
| 211 |
assertThat(updateFontFile( |
| 212 |
TEST_NOTO_COLOR_EMOJI_VPLUS1_TTF, TEST_NOTO_COLOR_EMOJI_VPLUS1_SIG)) |
| 213 |
.isEqualTo(FontManager.RESULT_SUCCESS); |
| 214 |
String fontPath3 = getFontPath(NOTO_COLOR_EMOJI_POSTSCRIPT_NAME); |
| 215 |
assertThat(fontPath).startsWith(DATA_FONTS_DIR); |
| 216 |
assertThat(fontPath2).isNotEqualTo(fontPath); |
| 217 |
assertThat(fontPath2).startsWith(DATA_FONTS_DIR); |
| 218 |
assertThat(fontPath3).startsWith(DATA_FONTS_DIR); |
| 219 |
assertThat(fontPath3).isNotEqualTo(fontPath); |
| 220 |
} |
| 221 |
|
| 222 |
@Test |
| 223 |
public void updateFont_invalidCert() throws Exception { |
| 224 |
assertThat(updateFontFile( |
| 225 |
TEST_NOTO_COLOR_EMOJI_VPLUS1_TTF, TEST_NOTO_COLOR_EMOJI_VPLUS2_SIG)) |
| 226 |
.isEqualTo(FontManager.RESULT_ERROR_VERIFICATION_FAILURE); |
| 227 |
} |
| 228 |
|
| 229 |
@Test |
| 230 |
public void updateFont_downgradeFromSystem() throws Exception { |
| 231 |
assertThat(updateFontFile( |
| 232 |
TEST_NOTO_COLOR_EMOJI_V0_TTF, TEST_NOTO_COLOR_EMOJI_V0_SIG)) |
| 233 |
.isEqualTo(FontManager.RESULT_ERROR_DOWNGRADING); |
| 234 |
} |
| 235 |
|
| 236 |
@Test |
| 237 |
public void updateFont_downgradeFromData() throws Exception { |
| 238 |
assertThat(updateFontFile( |
| 239 |
TEST_NOTO_COLOR_EMOJI_VPLUS2_TTF, TEST_NOTO_COLOR_EMOJI_VPLUS2_SIG)) |
| 240 |
.isEqualTo(FontManager.RESULT_SUCCESS); |
| 241 |
assertThat(updateFontFile( |
| 242 |
TEST_NOTO_COLOR_EMOJI_VPLUS1_TTF, TEST_NOTO_COLOR_EMOJI_VPLUS1_SIG)) |
| 243 |
.isEqualTo(FontManager.RESULT_ERROR_DOWNGRADING); |
| 244 |
} |
| 245 |
|
| 246 |
@Test |
| 247 |
public void updateFontFamily() throws Exception { |
| 248 |
assertThat(updateNotoSerifAs("serif")).isEqualTo(FontManager.RESULT_SUCCESS); |
| 249 |
FontConfig.FontFamily family = findFontFamilyOrThrow("serif"); |
| 250 |
assertThat(family.getFontList()).hasSize(2); |
| 251 |
assertThat(family.getFontList().get(0).getPostScriptName()) |
| 252 |
.isEqualTo(NOTO_SERIF_REGULAR_POSTSCRIPT_NAME); |
| 253 |
assertThat(family.getFontList().get(0).getFile().getAbsolutePath()) |
| 254 |
.startsWith(DATA_FONTS_DIR); |
| 255 |
assertThat(family.getFontList().get(0).getStyle().getWeight()) |
| 256 |
.isEqualTo(FONT_WEIGHT_NORMAL); |
| 257 |
assertThat(family.getFontList().get(1).getPostScriptName()) |
| 258 |
.isEqualTo(NOTO_SERIF_BOLD_POSTSCRIPT_NAME); |
| 259 |
assertThat(family.getFontList().get(1).getFile().getAbsolutePath()) |
| 260 |
.startsWith(DATA_FONTS_DIR); |
| 261 |
assertThat(family.getFontList().get(1).getStyle().getWeight()).isEqualTo(FONT_WEIGHT_BOLD); |
| 262 |
} |
| 263 |
|
| 264 |
@Test |
| 265 |
public void updateFontFamily_asNewFont() throws Exception { |
| 266 |
assertThat(updateNotoSerifAs("UpdatableSystemFontTest-serif")) |
| 267 |
.isEqualTo(FontManager.RESULT_SUCCESS); |
| 268 |
FontConfig.FontFamily family = findFontFamilyOrThrow("UpdatableSystemFontTest-serif"); |
| 269 |
assertThat(family.getFontList()).hasSize(2); |
| 270 |
assertThat(family.getFontList().get(0).getPostScriptName()) |
| 271 |
.isEqualTo(NOTO_SERIF_REGULAR_POSTSCRIPT_NAME); |
| 272 |
assertThat(family.getFontList().get(1).getPostScriptName()) |
| 273 |
.isEqualTo(NOTO_SERIF_BOLD_POSTSCRIPT_NAME); |
| 274 |
} |
| 275 |
|
| 276 |
@Test |
| 277 |
public void launchApp() throws Exception { |
| 278 |
String fontPath = getFontPath(NOTO_COLOR_EMOJI_POSTSCRIPT_NAME); |
| 279 |
assertThat(fontPath).startsWith(SYSTEM_FONTS_DIR); |
| 280 |
startActivity(EMOJI_RENDERING_TEST_APP_ID, EMOJI_RENDERING_TEST_ACTIVITY); |
| 281 |
SystemUtil.eventually( |
| 282 |
() -> assertThat(isFileOpenedBy(fontPath, EMOJI_RENDERING_TEST_APP_ID)).isTrue(), |
| 283 |
ACTIVITY_TIMEOUT_MILLIS); |
| 284 |
} |
| 285 |
|
| 286 |
@Test |
| 287 |
public void launchApp_afterUpdateFont() throws Exception { |
| 288 |
String originalFontPath = getFontPath(NOTO_COLOR_EMOJI_POSTSCRIPT_NAME); |
| 289 |
assertThat(originalFontPath).startsWith(SYSTEM_FONTS_DIR); |
| 290 |
assertThat(updateFontFile( |
| 291 |
TEST_NOTO_COLOR_EMOJI_VPLUS1_TTF, TEST_NOTO_COLOR_EMOJI_VPLUS1_SIG)) |
| 292 |
.isEqualTo(FontManager.RESULT_SUCCESS); |
| 293 |
String updatedFontPath = getFontPath(NOTO_COLOR_EMOJI_POSTSCRIPT_NAME); |
| 294 |
assertThat(updatedFontPath).startsWith(DATA_FONTS_DIR); |
| 295 |
updateNotoSerifAs(TEST_NOTO_SERIF); |
| 296 |
String notoSerifPath = getFontPath(NOTO_SERIF_REGULAR_POSTSCRIPT_NAME); |
| 297 |
startActivity(EMOJI_RENDERING_TEST_APP_ID, EMOJI_RENDERING_TEST_ACTIVITY); |
| 298 |
|
| 299 |
SystemUtil.eventually(() -> { |
| 300 |
assertThat(isFileOpenedBy(updatedFontPath, EMOJI_RENDERING_TEST_APP_ID)).isTrue(); |
| 301 |
assertThat(isFileOpenedBy(originalFontPath, EMOJI_RENDERING_TEST_APP_ID)).isFalse(); |
| 302 |
assertThat(isFileOpenedBy(notoSerifPath, EMOJI_RENDERING_TEST_APP_ID)).isTrue(); |
| 303 |
}, ACTIVITY_TIMEOUT_MILLIS); |
| 304 |
} |
| 305 |
|
| 306 |
@Test |
| 307 |
public void reboot() throws Exception { |
| 308 |
expectCommandToSucceed(String.format("cmd font update %s %s", |
| 309 |
TEST_NOTO_COLOR_EMOJI_VPLUS1_TTF, TEST_NOTO_COLOR_EMOJI_VPLUS1_SIG)); |
| 310 |
String fontPath = getFontPath(NOTO_COLOR_EMOJI_POSTSCRIPT_NAME); |
| 311 |
assertThat(fontPath).startsWith(DATA_FONTS_DIR); |
| 312 |
|
| 313 |
|
| 314 |
expectCommandToSucceed("cmd font restart"); |
| 315 |
String fontPathAfterReboot = getFontPath(NOTO_COLOR_EMOJI_POSTSCRIPT_NAME); |
| 316 |
assertThat(fontPathAfterReboot).isEqualTo(fontPath); |
| 317 |
} |
| 318 |
|
| 319 |
@Test |
| 320 |
public void fdLeakTest() throws Exception { |
| 321 |
long originalOpenFontCount = |
| 322 |
countMatch(getOpenFiles("system_server"), PATTERN_FONT_FILES); |
| 323 |
Pattern patternEmojiVPlus1 = |
| 324 |
Pattern.compile(Pattern.quote(TEST_NOTO_COLOR_EMOJI_VPLUS1_TTF)); |
| 325 |
for (int i = 0; i < 10; i++) { |
| 326 |
assertThat(updateFontFile( |
| 327 |
TEST_NOTO_COLOR_EMOJI_VPLUS1_TTF, TEST_NOTO_COLOR_EMOJI_VPLUS1_SIG)) |
| 328 |
.isEqualTo(FontManager.RESULT_SUCCESS); |
| 329 |
List<String> openFiles = getOpenFiles("system_server"); |
| 330 |
for (Pattern p : Arrays.asList(PATTERN_FONT_FILES, PATTERN_SYSTEM_FONT_FILES, |
| 331 |
PATTERN_DATA_FONT_FILES, PATTERN_TMP_FILES)) { |
| 332 |
Log.i(TAG, String.format("num of %s: %d", p, countMatch(openFiles, p))); |
| 333 |
} |
| 334 |
|
| 335 |
assertThat(countMatch(openFiles, PATTERN_DATA_FONT_FILES)).isEqualTo(0); |
| 336 |
|
| 337 |
assertThat(countMatch(openFiles, patternEmojiVPlus1)).isEqualTo(0); |
| 338 |
|
| 339 |
assertThat(countMatch(openFiles, PATTERN_FONT_FILES)) |
| 340 |
.isAtMost(originalOpenFontCount); |
| 341 |
} |
| 342 |
} |
| 343 |
|
| 344 |
@Test |
| 345 |
public void fdLeakTest_withoutPermission() throws Exception { |
| 346 |
Pattern patternEmojiVPlus1 = |
| 347 |
Pattern.compile(Pattern.quote(TEST_NOTO_COLOR_EMOJI_VPLUS1_TTF)); |
| 348 |
byte[] signature = Files.readAllBytes(Paths.get(TEST_NOTO_COLOR_EMOJI_VPLUS1_SIG)); |
| 349 |
try (ParcelFileDescriptor fd = ParcelFileDescriptor.open( |
| 350 |
new File(TEST_NOTO_COLOR_EMOJI_VPLUS1_TTF), MODE_READ_ONLY)) { |
| 351 |
assertThrows(SecurityException.class, |
| 352 |
() -> updateFontFileWithoutPermission(fd, signature, 0)); |
| 353 |
} |
| 354 |
List<String> openFiles = getOpenFiles("system_server"); |
| 355 |
assertThat(countMatch(openFiles, patternEmojiVPlus1)).isEqualTo(0); |
| 356 |
} |
| 357 |
|
| 358 |
@Test |
| 359 |
public void getAvailableFonts() throws Exception { |
| 360 |
String fontPath = getFontPath(NOTO_COLOR_EMOJI_POSTSCRIPT_NAME); |
| 361 |
startActivity(EMOJI_RENDERING_TEST_APP_ID, GET_AVAILABLE_FONTS_TEST_ACTIVITY); |
| 362 |
|
| 363 |
mUiDevice.wait( |
| 364 |
Until.findObject(By.pkg(EMOJI_RENDERING_TEST_APP_ID).text(fontPath)), |
| 365 |
ACTIVITY_TIMEOUT_MILLIS); |
| 366 |
|
| 367 |
|
| 368 |
assertThat(isFileOpenedBy(fontPath, EMOJI_RENDERING_TEST_APP_ID)).isFalse(); |
| 369 |
} |
| 370 |
|
| 371 |
private static String insertCert(String certPath) throws Exception { |
| 372 |
Pair<String, String> result; |
| 373 |
try (InputStream is = new FileInputStream(certPath)) { |
| 374 |
result = runShellCommand("mini-keyctl padd asymmetric fsv_test .fs-verity", is); |
| 375 |
} |
| 376 |
|
| 377 |
final String copiedCert = "/data/fonts/debug_cert.der"; |
| 378 |
runShellCommand("cp " + certPath + " " + copiedCert, null); |
| 379 |
runShellCommand("cmd font install-debug-cert " + copiedCert, null); |
| 380 |
|
| 381 |
assertThat(result.second).isEmpty(); |
| 382 |
String keyId = result.first.trim(); |
| 383 |
assertThat(keyId).matches("^\\d+$"); |
| 384 |
return keyId; |
| 385 |
} |
| 386 |
|
| 387 |
private int updateFontFile(String fontPath, String signaturePath) throws IOException { |
| 388 |
byte[] signature = Files.readAllBytes(Paths.get(signaturePath)); |
| 389 |
try (ParcelFileDescriptor fd = |
| 390 |
ParcelFileDescriptor.open(new File(fontPath), MODE_READ_ONLY)) { |
| 391 |
return SystemUtil.runWithShellPermissionIdentity(() -> { |
| 392 |
int configVersion = mFontManager.getFontConfig().getConfigVersion(); |
| 393 |
return updateFontFileWithoutPermission(fd, signature, configVersion); |
| 394 |
}); |
| 395 |
} |
| 396 |
} |
| 397 |
|
| 398 |
private int updateFontFileWithoutPermission(ParcelFileDescriptor fd, byte[] signature, |
| 399 |
int configVersion) { |
| 400 |
return mFontManager.updateFontFamily( |
| 401 |
new FontFamilyUpdateRequest.Builder() |
| 402 |
.addFontFileUpdateRequest(new FontFileUpdateRequest(fd, signature)) |
| 403 |
.build(), |
| 404 |
configVersion); |
| 405 |
} |
| 406 |
|
| 407 |
private int updateNotoSerifAs(String familyName) throws IOException { |
| 408 |
List<FontFamilyUpdateRequest.Font> fonts = Arrays.asList( |
| 409 |
new FontFamilyUpdateRequest.Font.Builder(NOTO_SERIF_REGULAR_POSTSCRIPT_NAME, |
| 410 |
new FontStyle(FONT_WEIGHT_NORMAL, FONT_SLANT_UPRIGHT)).build(), |
| 411 |
new FontFamilyUpdateRequest.Font.Builder(NOTO_SERIF_BOLD_POSTSCRIPT_NAME, |
| 412 |
new FontStyle(FONT_WEIGHT_BOLD, FONT_SLANT_UPRIGHT)).build()); |
| 413 |
FontFamilyUpdateRequest.FontFamily fontFamily = |
| 414 |
new FontFamilyUpdateRequest.FontFamily.Builder(familyName, fonts).build(); |
| 415 |
byte[] regularSig = Files.readAllBytes(Paths.get(NOTO_SERIF_REGULAR_SIG)); |
| 416 |
byte[] boldSig = Files.readAllBytes(Paths.get(NOTO_SERIF_BOLD_SIG)); |
| 417 |
try (ParcelFileDescriptor regularFd = ParcelFileDescriptor.open( |
| 418 |
new File(NOTO_SERIF_REGULAR_TTF), MODE_READ_ONLY); |
| 419 |
ParcelFileDescriptor boldFd = ParcelFileDescriptor.open( |
| 420 |
new File(NOTO_SERIF_BOLD_TTF), MODE_READ_ONLY)) { |
| 421 |
return SystemUtil.runWithShellPermissionIdentity(() -> { |
| 422 |
FontConfig fontConfig = mFontManager.getFontConfig(); |
| 423 |
return mFontManager.updateFontFamily(new FontFamilyUpdateRequest.Builder() |
| 424 |
.addFontFileUpdateRequest( |
| 425 |
new FontFileUpdateRequest(regularFd, regularSig)) |
| 426 |
.addFontFileUpdateRequest( |
| 427 |
new FontFileUpdateRequest(boldFd, boldSig)) |
| 428 |
.addFontFamily(fontFamily) |
| 429 |
.build(), fontConfig.getConfigVersion()); |
| 430 |
}); |
| 431 |
} |
| 432 |
} |
| 433 |
|
| 434 |
private String getFontPath(String psName) { |
| 435 |
FontConfig fontConfig = |
| 436 |
SystemUtil.runWithShellPermissionIdentity(mFontManager::getFontConfig); |
| 437 |
return fontConfig.getFontFamilies().stream() |
| 438 |
.flatMap(family -> family.getFontList().stream()) |
| 439 |
.filter(font -> psName.equals(font.getPostScriptName())) |
| 440 |
|
| 441 |
|
| 442 |
.reduce((first, second) -> second) |
| 443 |
.orElseThrow(() -> new AssertionError("Font not found: " + psName)) |
| 444 |
.getFile() |
| 445 |
.getAbsolutePath(); |
| 446 |
} |
| 447 |
|
| 448 |
private FontConfig.FontFamily findFontFamilyOrThrow(String familyName) { |
| 449 |
FontConfig fontConfig = |
| 450 |
SystemUtil.runWithShellPermissionIdentity(mFontManager::getFontConfig); |
| 451 |
return fontConfig.getFontFamilies().stream() |
| 452 |
.filter(family -> familyName.equals(family.getName())) |
| 453 |
|
| 454 |
|
| 455 |
.reduce((first, second) -> second) |
| 456 |
.orElseThrow(() -> new AssertionError("Family not found: " + familyName)); |
| 457 |
} |
| 458 |
|
| 459 |
private static void startActivity(String appId, String activityId) throws Exception { |
| 460 |
expectCommandToSucceed("am force-stop " + appId); |
| 461 |
expectCommandToSucceed("am start-activity -n " + activityId); |
| 462 |
} |
| 463 |
|
| 464 |
private static String expectCommandToSucceed(String cmd) throws IOException { |
| 465 |
Pair<String, String> result = runShellCommand(cmd, null); |
| 466 |
|
| 467 |
|
| 468 |
assertThat(result.second.trim()).isEmpty(); |
| 469 |
return result.first; |
| 470 |
} |
| 471 |
|
| 472 |
private static void expectCommandToFail(String cmd) throws IOException { |
| 473 |
Pair<String, String> result = runShellCommand(cmd, null); |
| 474 |
|
| 475 |
|
| 476 |
assertThat(result.second.trim()).isNotEmpty(); |
| 477 |
} |
| 478 |
|
| 479 |
|
| 480 |
private static Pair<String, String> runShellCommand(String cmd, @Nullable InputStream input) |
| 481 |
throws IOException { |
| 482 |
Log.i(TAG, "runShellCommand: " + cmd); |
| 483 |
UiAutomation automation = InstrumentationRegistry.getInstrumentation().getUiAutomation(); |
| 484 |
ParcelFileDescriptor[] rwe = automation.executeShellCommandRwe(cmd); |
| 485 |
|
| 486 |
try (ParcelFileDescriptor outFd = rwe[0]; |
| 487 |
ParcelFileDescriptor inFd = rwe[1]; |
| 488 |
ParcelFileDescriptor errFd = rwe[2]) { |
| 489 |
if (input != null) { |
| 490 |
try (OutputStream os = new FileOutputStream(inFd.getFileDescriptor())) { |
| 491 |
StreamUtil.copyStreams(input, os); |
| 492 |
} |
| 493 |
} |
| 494 |
|
| 495 |
|
| 496 |
inFd.close(); |
| 497 |
String stdout; |
| 498 |
try (InputStream is = new FileInputStream(outFd.getFileDescriptor())) { |
| 499 |
stdout = StreamUtil.readInputStream(is); |
| 500 |
} |
| 501 |
Log.i(TAG, "stdout = " + stdout); |
| 502 |
String stderr; |
| 503 |
try (InputStream is = new FileInputStream(errFd.getFileDescriptor())) { |
| 504 |
stderr = StreamUtil.readInputStream(is); |
| 505 |
} |
| 506 |
Log.i(TAG, "stderr = " + stderr); |
| 507 |
return new Pair<>(stdout, stderr); |
| 508 |
} |
| 509 |
} |
| 510 |
|
| 511 |
private static boolean isFileOpenedBy(String path, String appId) throws Exception { |
| 512 |
String pid = pidOf(appId); |
| 513 |
if (pid.isEmpty()) { |
| 514 |
return false; |
| 515 |
} |
| 516 |
String cmd = String.format("lsof -t -p %s %s", pid, path); |
| 517 |
return !expectCommandToSucceed(cmd).trim().isEmpty(); |
| 518 |
} |
| 519 |
|
| 520 |
private static List<String> getOpenFiles(String appId) throws Exception { |
| 521 |
String pid = pidOf(appId); |
| 522 |
if (pid.isEmpty()) { |
| 523 |
return Collections.emptyList(); |
| 524 |
} |
| 525 |
String cmd = String.format("lsof -p %s", pid); |
| 526 |
String out = expectCommandToSucceed(cmd); |
| 527 |
List<String> paths = new ArrayList<>(); |
| 528 |
boolean first = true; |
| 529 |
for (String line : out.split("\n")) { |
| 530 |
|
| 531 |
if (first) { |
| 532 |
first = false; |
| 533 |
continue; |
| 534 |
} |
| 535 |
String[] records = line.split(" "); |
| 536 |
if (records.length > 0) { |
| 537 |
paths.add(records[records.length - 1]); |
| 538 |
} |
| 539 |
} |
| 540 |
return paths; |
| 541 |
} |
| 542 |
|
| 543 |
private static String pidOf(String appId) throws Exception { |
| 544 |
return expectCommandToSucceed("pidof " + appId).trim(); |
| 545 |
} |
| 546 |
|
| 547 |
private static long countMatch(List<String> paths, Pattern pattern) { |
| 548 |
|
| 549 |
return paths.stream() |
| 550 |
.filter(pattern.asPredicate()) |
| 551 |
.count(); |
| 552 |
} |
| 553 |
} |