1 /* 2 * Copyright (C) 2016 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 com.android.server.cts; 18 19 import android.providers.settings.SettingProto; 20 import android.providers.settings.SettingsOperationProto; 21 import android.providers.settings.SettingsServiceDumpProto; 22 import android.providers.settings.UserSettingsProto; 23 24 import com.android.ddmlib.Log.LogLevel; 25 import com.android.incident.Destination; 26 import com.android.incident.Privacy; 27 import com.android.tradefed.log.LogUtil.CLog; 28 import com.google.protobuf.GeneratedMessage; 29 import com.google.protobuf.Descriptors.FieldDescriptor; 30 31 import java.lang.reflect.Method; 32 import java.lang.reflect.Modifier; 33 import java.util.Arrays; 34 import java.util.ArrayList; 35 import java.util.Collection; 36 import java.util.List; 37 import java.util.Map; 38 import java.util.HashMap; 39 import java.util.stream.Collectors; 40 import java.util.stream.Stream; 41 42 /** 43 * Test to check that the settings service properly outputs its dump state. 44 */ 45 public class SettingsIncidentTest extends ProtoDumpTestCase { 46 47 /** 48 * Test that there are some secure/system settings for the user and there are some global 49 * settings. 50 * 51 * @throws Exception 52 */ 53 public void testBasicStructure() throws Exception { 54 SettingsServiceDumpProto dump = getDump(SettingsServiceDumpProto.parser(), 55 "dumpsys settings --proto"); 56 57 verifySettingsServiceDumpProto(dump, PRIVACY_NONE); 58 } 59 60 static void verifySettingsServiceDumpProto(SettingsServiceDumpProto dump, final int filterLevel) throws Exception { 61 if (dump.getUserSettingsCount() > 0) { 62 UserSettingsProto userSettings = dump.getUserSettings(0); 63 assertEquals(0, userSettings.getUserId()); 64 65 CLog.logAndDisplay(LogLevel.INFO, "#*#*#*#*#*#*#*#*#*#*# SECURE #*#*#*#*#*#*#*#*#*#*#"); 66 verifySettings(userSettings.getSecureSettings(), filterLevel); 67 CLog.logAndDisplay(LogLevel.INFO, "#*#*#*#*#*#*#*#*#*#*# SYSTEM #*#*#*#*#*#*#*#*#*#*#"); 68 verifySettings(userSettings.getSystemSettings(), filterLevel); 69 } 70 71 CLog.logAndDisplay(LogLevel.INFO, "#*#*#*#*#*#*#*#*#*#*# GLOBAL #*#*#*#*#*#*#*#*#*#*#"); 72 verifySettings(dump.getGlobalSettings(), filterLevel); 73 } 74 75 private static void verifySettings(GeneratedMessage settings, final int filterLevel) throws Exception { 76 verifySettings(getSettingProtos(settings), filterLevel); 77 78 final List<SettingsOperationProto> ops = invoke(settings, "getHistoricalOperationsList"); 79 for (SettingsOperationProto op : ops) { 80 assertTrue(op.getTimestamp() >= 0); 81 assertNotNull(op.getOperation()); 82 // setting is optional 83 if (filterLevel == PRIVACY_AUTO) { 84 // SettingOperationProto is EXPLICIT by default. 85 assertTrue(op.getOperation().isEmpty()); 86 assertTrue(op.getSetting().isEmpty()); 87 } 88 } 89 } 90 91 private static Map<SettingProto, Destination> getSettingProtos(GeneratedMessage settingsProto) { 92 CLog.d("Checking out class: " + settingsProto.getClass()); 93 94 Map<SettingProto, Destination> settings = new HashMap<>(); 95 for (FieldDescriptor fd : settingsProto.getDescriptorForType().getFields()) { 96 if (fd.getType() != FieldDescriptor.Type.MESSAGE) { 97 // Only looking for SettingProtos and messages that contain them. Skip any primitive 98 // fields. 99 continue; 100 } 101 List<Object> tmp; 102 if (fd.isRepeated()) { 103 tmp = (List) settingsProto.getField(fd); 104 } else { 105 tmp = new ArrayList<>(); 106 tmp.add(settingsProto.getField(fd)); 107 } 108 Destination dest = fd.getOptions().getExtension(Privacy.privacy).getDest(); 109 for (Object o : tmp) { 110 if ("android.providers.settings.SettingProto".equals(fd.getMessageType().getFullName())) { 111 // The container's default privacy doesn't affect message types. However, 112 // anotations on the field override the message's default annotation. If a 113 // message field doesn't have an annotation, it is treated as EXPLICIT by 114 // default. 115 settings.put((SettingProto) o, dest == Destination.DEST_UNSET ? Destination.DEST_EXPLICIT : dest); 116 } else { 117 // Sub messages don't inherit the container's default privacy. If the field had 118 // an annotation, it would override the sub message's default privacy. 119 settings.putAll(getSettingProtos((GeneratedMessage) o)); 120 } 121 } 122 } 123 124 return settings; 125 } 126 127 private static <T> T invoke(Method method, Object instance, Object... args) { 128 method.setAccessible(true); 129 try { 130 return (T) method.invoke(instance, args); 131 } catch (Exception e) { 132 throw new RuntimeException(e); 133 } 134 } 135 136 private static <T> T invoke(GeneratedMessage instance, String methodName, Object... args) 137 throws Exception { 138 final Class<?>[] inputParamTypes = Arrays.stream(args) 139 .map((arg) -> toPrimitive(arg.getClass())) 140 .toArray(Class[]::new); 141 return invoke( 142 instance.getClass().getDeclaredMethod(methodName, inputParamTypes), 143 instance, args); 144 } 145 146 private static Class<?> toPrimitive(Class<?> c) { 147 return c == Integer.class ? int.class : c; 148 } 149 150 private static void verifySettings(Map<SettingProto, Destination> settings, final int filterLevel) throws Exception { 151 assertFalse(settings.isEmpty()); 152 153 CLog.d("Field count: " + settings.size()); 154 for (Map.Entry<SettingProto, Destination> sDPair : settings.entrySet()) { 155 SettingProto setting = sDPair.getKey(); 156 Destination dest = sDPair.getValue(); 157 try { 158 final String id = setting.getId(); 159 if (!id.isEmpty()) { 160 // _ID has to be a long converted to a String 161 Long.parseLong(id); 162 } 163 assertNotNull(setting.getName()); 164 if (filterLevel < PRIVACY_LOCAL) { 165 if (dest == Destination.DEST_LOCAL) { 166 // Any filter that is not LOCAL should make sure local isn't printed at all. 167 String err = "Setting '" + setting.getName() + "' with LOCAL privacy didn't strip data for filter level '" + privacyToString(filterLevel) + "'"; 168 assertTrue(err, setting.getId().isEmpty()); 169 assertTrue(err, setting.getName().isEmpty()); 170 assertTrue(err, setting.getPkg().isEmpty()); 171 assertTrue(err, setting.getValue().isEmpty()); 172 assertTrue(err, setting.getDefaultValue().isEmpty()); 173 } 174 if (filterLevel < PRIVACY_EXPLICIT) { 175 if (dest == Destination.DEST_EXPLICIT) { 176 String err = "Setting '" + setting.getName() + "' with EXPLICIT privacy didn't strip data for filter level '" + privacyToString(filterLevel) + "'"; 177 assertTrue(err, setting.getId().isEmpty()); 178 assertTrue(err, setting.getName().isEmpty()); 179 assertTrue(err, setting.getPkg().isEmpty()); 180 assertTrue(err, setting.getValue().isEmpty()); 181 assertTrue(err, setting.getDefaultValue().isEmpty()); 182 } 183 } 184 } 185 // pkg is optional 186 // value can be anything 187 // default can be anything 188 // default from system reported only if optional default present 189 } catch (Throwable e) { 190 throw new AssertionError("Failed for setting " + setting, e); 191 } 192 } 193 } 194 } 195 196