1 /* 2 * Copyright (C) 2021 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.tests.odsign; 18 19 import static com.google.common.truth.Truth.assertThat; 20 import static com.google.common.truth.Truth.assertWithMessage; 21 import static org.junit.Assume.assumeTrue; 22 23 import androidx.annotation.NonNull; 24 25 import org.junit.Test; 26 27 import java.io.File; 28 import java.io.IOException; 29 import java.nio.file.Files; 30 import java.nio.file.Path; 31 import java.nio.file.Paths; 32 import java.util.stream.Collectors; 33 import java.util.stream.Stream; 34 35 public class ArtifactsSignedTest { 36 private static final String TAG = "VerifyArtArtifactsSignedTest"; 37 private static final String ARTIFACTS_DIR = "/data/misc/apexdata/com.android.art/dalvik-cache"; 38 private static final String FS_VERITY_PROC_PATH = "/proc/sys/fs/verity"; 39 40 // Note that some of these files may exist multiple times - for different architectures 41 // Verifying that they are generated for the correct architectures is currently out of 42 // scope for this test. 43 private static final String[] REQUIRED_ARTIFACT_NAMES = { 44 "boot.art", 45 "boot.oat", 46 "boot.vdex", 47 "system@[email protected]@classes.vdex", 48 "system@[email protected]@classes.odex", 49 "system@[email protected]@classes.art", 50 }; 51 52 static { 53 System.loadLibrary("OdsignTestAppJni"); 54 } 55 hasFsverityNative(@onNull String path)56 private static native boolean hasFsverityNative(@NonNull String path); 57 isFsVeritySupported()58 public boolean isFsVeritySupported() { 59 return new File(FS_VERITY_PROC_PATH).exists(); 60 } 61 62 @Test testArtArtifactsHaveFsverity()63 public void testArtArtifactsHaveFsverity() throws Exception { 64 assumeTrue("fs-verity is not supported on this device.", isFsVeritySupported()); 65 assertWithMessage("Found artifacts not in fs-verity") 66 .that(getArtifacts().map(File::getPath).filter((path) -> !hasFsverityNative(path)) 67 .collect(Collectors.toList())) 68 .isEmpty(); 69 } 70 71 @Test testGeneratesRequiredArtArtifacts()72 public void testGeneratesRequiredArtArtifacts() throws Exception { 73 assertThat(getArtifacts().map(File::getName).collect(Collectors.toList())) 74 .containsAtLeastElementsIn(REQUIRED_ARTIFACT_NAMES); 75 } 76 77 @Test testGeneratesAnyArtArtifacts()78 public void testGeneratesAnyArtArtifacts() throws Exception { 79 assertThat(getArtifacts().collect(Collectors.toList())).isNotEmpty(); 80 } 81 getArtifacts()82 private Stream<File> getArtifacts() throws Exception { 83 return Files.walk(Paths.get(ARTIFACTS_DIR), Integer.MAX_VALUE) 84 .map(Path::toFile) 85 .filter(File::isFile); 86 } 87 } 88