Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2013 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.os.cts;
     18 
     19 import android.system.OsConstants;
     20 import android.system.ErrnoException;
     21 import android.util.ArraySet;
     22 
     23 import com.android.compatibility.common.util.ReadElf;
     24 
     25 import java.io.File;
     26 import java.io.IOException;
     27 import java.util.Arrays;
     28 
     29 import junit.framework.TestCase;
     30 
     31 public class AbiTest extends TestCase {
     32     public void testNo64() throws Exception {
     33         ArraySet<String> abiDirs = new ArraySet(Arrays.asList(
     34             "/sbin",
     35             "/system",
     36             "/vendor"));
     37         String pathVar = System.getenv("PATH");
     38         if (pathVar != null) {
     39             abiDirs.addAll(Arrays.asList(pathVar.split(":")));
     40         }
     41         for (String dir : abiDirs) {
     42             boolean skip_dir = false;
     43             for (String dirOther : abiDirs) {
     44                 if (dir.equals(dirOther)) {
     45                     continue;
     46                 } else if (dir.startsWith(dirOther + "/")) {
     47                     skip_dir = true;
     48                     break;
     49                 }
     50             }
     51             if (!skip_dir) {
     52                 checkElfFilesInDirectory(new File(dir));
     53             }
     54         }
     55     }
     56 
     57     private void checkElfFilesInDirectory(File dir) throws Exception {
     58         if (!dir.isDirectory()) {
     59             return;
     60         }
     61 
     62         if (isSymbolicLink(dir)) {
     63             return;
     64         }
     65 
     66         File[] files = dir.listFiles();
     67         if (files == null) {
     68             return;
     69         }
     70 
     71         for (File f : files) {
     72             if (f.isDirectory()) {
     73                 checkElfFilesInDirectory(f);
     74             } else if (f.getName().endsWith(".so") || f.canExecute()) {
     75                 ReadElf elf = null;
     76                 try { // TODO: switch to try-with-resources.
     77                     elf = ReadElf.read(f);
     78                 } catch (IllegalArgumentException ignored) {
     79                     // If it's not actually an ELF file, we don't care.
     80                 } catch (IOException ex) {
     81                     Throwable cause = ex.getCause();
     82                     if (cause instanceof ErrnoException) {
     83                         // if we are denied access to the file, ignore.
     84                         if (((ErrnoException) cause).errno != OsConstants.EACCES) {
     85                             throw ex;
     86                         }
     87                     }
     88                 } finally {
     89                     if (elf != null) {
     90                         elf.close();
     91                     }
     92                 }
     93             }
     94         }
     95     }
     96 
     97     private static boolean isSymbolicLink(File f) throws Exception {
     98         return !f.getAbsolutePath().equals(f.getCanonicalPath());
     99     }
    100 }
    101