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