Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2009 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.permission.cts;
     18 
     19 import android.system.ErrnoException;
     20 import android.system.Os;
     21 import android.system.OsConstants;
     22 import android.system.StructStat;
     23 import android.test.AndroidTestCase;
     24 import android.test.suitebuilder.annotation.MediumTest;
     25 import android.util.Log;
     26 
     27 import java.io.BufferedReader;
     28 import java.io.File;
     29 import java.io.IOException;
     30 import java.io.InputStreamReader;
     31 
     32 /**
     33  * Verify the read system log require specific permissions.
     34  */
     35 public class NoReadLogsPermissionTest extends AndroidTestCase {
     36     /**
     37      * Verify that we'll only get our logs without the READ_LOGS permission.
     38      *
     39      * We test this by examining the logs looking for ActivityManager lines.
     40      * Since ActivityManager runs as a different UID, we shouldn't see
     41      * any of those log entries.
     42      *
     43      * @throws IOException
     44      */
     45     @MediumTest
     46     public void testLogcat() throws IOException {
     47         Process logcatProc = null;
     48         BufferedReader reader = null;
     49         try {
     50             logcatProc = Runtime.getRuntime().exec(new String[]
     51                     {"logcat", "-v", "brief", "-d", "ActivityManager:* *:S" });
     52 
     53             reader = new BufferedReader(new InputStreamReader(logcatProc.getInputStream()));
     54 
     55             int lineCt = 0;
     56             String line;
     57             while ((line = reader.readLine()) != null) {
     58                 if (!line.startsWith("--------- beginning of ")) {
     59                     lineCt++;
     60                 }
     61             }
     62 
     63             // no permission get an empty log buffer.
     64             // Logcat returns only one line:
     65             // "--------- beginning of <log device>"
     66 
     67             assertEquals("Unexpected logcat entries. Are you running the "
     68                        + "the latest logger.c from the Android kernel?",
     69                     0, lineCt);
     70 
     71         } finally {
     72             if (reader != null) {
     73                 reader.close();
     74             }
     75         }
     76     }
     77 
     78     public void testEventsLogSane() throws ErrnoException {
     79         testLogIsSane("/dev/log/events");
     80     }
     81 
     82     public void testMainLogSane() throws ErrnoException {
     83         testLogIsSane("/dev/log/main");
     84     }
     85 
     86     public void testRadioLogSane() throws ErrnoException {
     87         testLogIsSane("/dev/log/radio");
     88     }
     89 
     90     public void testSystemLogSane() throws ErrnoException {
     91         testLogIsSane("/dev/log/system");
     92     }
     93 
     94     private static void testLogIsSane(String log) throws ErrnoException {
     95         try {
     96             StructStat stat = Os.stat(log);
     97             assertEquals("not owned by uid=0", 0, stat.st_uid);
     98             assertEquals("not owned by gid=logs", "log", FileUtils.getGroupName(stat.st_gid));
     99         } catch (ErrnoException e) {
    100             if (e.errno != OsConstants.ENOENT && e.errno != OsConstants.EACCES) {
    101                 throw e;
    102             }
    103         }
    104     }
    105 }
    106