Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2017 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.jni.vendor.cts;
     18 
     19 import java.io.File;
     20 import java.io.IOException;
     21 import java.nio.file.Files;
     22 import java.nio.file.FileSystems;
     23 import java.util.ArrayList;
     24 import java.util.Arrays;
     25 import java.util.List;
     26 
     27 import junit.framework.TestCase;
     28 
     29 import android.os.Process;
     30 
     31 public class VendorJniTest extends TestCase {
     32     private List<String> llndkLibraries;
     33     private List<String> publicLibraries;
     34     private List<String> vndkspLibraries;
     35     private List<String> vndkLibraries;
     36     private List<String> frameworkOnlyLibraries;
     37 
     38     protected void setUp() throws Exception {
     39         llndkLibraries = Files.readAllLines(FileSystems.getDefault().getPath(
     40                 "/system/etc/llndk.libraries.txt"));
     41         publicLibraries = Files.readAllLines(FileSystems.getDefault().getPath(
     42                 "/system/etc/public.libraries.txt"));
     43         vndkspLibraries = Files.readAllLines(FileSystems.getDefault().getPath(
     44                 "/system/etc/vndksp.libraries.txt"));
     45 
     46         String systemLibDir = Process.is64Bit() ? "/system/lib64" : "/system/lib";
     47 
     48         vndkLibraries = new ArrayList<>();
     49         for(File f : (new File(systemLibDir + "/vndk")).listFiles()) {
     50             if (f.isFile()) {
     51                 String name = f.getName();
     52                 if (name.endsWith(".so")) {
     53                     vndkLibraries.add(name);
     54                 }
     55             }
     56         }
     57 
     58         frameworkOnlyLibraries = new ArrayList<>();
     59         for(File f : (new File(systemLibDir)).listFiles()) {
     60             if (f.isFile()) {
     61                 String name = f.getName();
     62                 if (name.endsWith(".so") && !llndkLibraries.contains(name)
     63                         && !vndkspLibraries.contains(name)
     64                         && !publicLibraries.contains(name)) {
     65                     frameworkOnlyLibraries.add(name);
     66                 }
     67             }
     68         }
     69 
     70         System.loadLibrary("vendorjnitest");
     71     }
     72 
     73     public static native String dlopen(String name);
     74 
     75     /* test if llndk libraries are all accessible */
     76     public void test_llndkLibraries() {
     77         for(String lib : llndkLibraries) {
     78             assertEquals("", dlopen(lib));
     79         }
     80     }
     81 
     82     /* test if vndk-sp libs are accessible */
     83     public void test_vndkSpLibraries() {
     84         for(String lib : vndkspLibraries) {
     85             String error = dlopen(lib);
     86             if (lib.equals("android.hidl.memory (at) 1.0-impl.so")) {
     87                 // This won't be accessible to vendor JNI. This lib is only accessible from the
     88                 // 'sphal' namespace which isn't linked to the vendor-classloader-namespace.
     89                 if (error.equals("")) {
     90                     fail(lib + " must not be accessible to vendor apks, but was accessible.");
     91                 }
     92                 continue;
     93             }
     94             assertEquals("", error);
     95         }
     96     }
     97 
     98     /* test if vndk libs are not accessible */
     99     public void test_vndkLibraries() {
    100         for(String lib : vndkLibraries) {
    101             String error = dlopen(lib);
    102             if (error.equals("")) {
    103                 fail(lib + " must not be accessible to vendor apks, but was accessible.");
    104             }
    105         }
    106     }
    107 
    108     /* test if framework-only libs are not accessible */
    109     public void test_frameworkOnlyLibraries() {
    110         for(String lib : frameworkOnlyLibraries) {
    111             String error = dlopen(lib);
    112             if (error.equals("")) {
    113                 fail(lib + " must not be accessible to vendor apks, but was accessible.");
    114             }
    115         }
    116     }
    117 }
    118