1 /*
2  * Copyright (C) 2020 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 android.appsecurity.cts.apkveritytestapp;
18 
19 import static org.junit.Assert.assertNotEquals;
20 import static org.junit.Assert.assertTrue;
21 
22 import android.content.Context;
23 import android.util.Log;
24 
25 import androidx.annotation.NonNull;
26 import androidx.test.InstrumentationRegistry;
27 
28 import org.junit.Test;
29 
30 import java.io.File;
31 import java.nio.file.Path;
32 import java.nio.file.Paths;
33 import java.util.ArrayList;
34 
35 public class InstalledFilesCheck {
36     private static final String TAG = InstalledFilesCheck.class.getSimpleName();
37     private static final String PACKAGE_NAME = InstalledFilesCheck.class.getPackage().getName();
38 
39     static {
40         System.loadLibrary("CtsApkVerityTestAppJni");
41     }
42 
hasFsverityNative(@onNull String path)43     private static native boolean hasFsverityNative(@NonNull String path);
44 
45     @Test
testFilesHaveFsverity()46     public void testFilesHaveFsverity() {
47         for (String path : getInterestedFiles()) {
48             assertTrue("Expect file installed: " + path, new File(path).exists());
49             assertTrue("Expect to have fs-verity: " + path, hasFsverityNative(path));
50         }
51     }
52 
getInterestedFiles()53     private ArrayList<String> getInterestedFiles() {
54         Context context = InstrumentationRegistry.getContext();
55         Path dirname = Paths.get(context.getApplicationInfo().sourceDir).getParent();
56         ArrayList<String> output = new ArrayList<>();
57 
58         android.os.Bundle testArgs = InstrumentationRegistry.getArguments();
59         assertNotEquals(testArgs, null);
60         int number = Integer.valueOf(testArgs.getString("Number"));
61         assertTrue(number > 0);
62 
63         for (int i = 0; i < number; ++i) {
64             String basename = testArgs.getString("File" + Integer.toString(i));
65             String filename = dirname.resolve(basename).toString();
66             Log.d(TAG, "Adding interested file " + filename);
67             output.add(filename);
68         }
69         return output;
70     }
71 }
72