1 /* 2 * Copyright (C) 2024 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.cts.statsd.atom; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import android.cts.statsd.metric.MetricsUtils; 22 import android.cts.statsdatom.lib.AtomTestUtils; 23 import android.cts.statsdatom.lib.ConfigUtils; 24 import android.cts.statsdatom.lib.DeviceUtils; 25 import android.cts.statsdatom.lib.ReportUtils; 26 27 import com.android.os.AtomsProto.Atom; 28 import com.android.os.AttributionNode; 29 import com.android.os.StatsLog.EventMetricData; 30 import com.android.os.AtomsProto.TestAtomReported; 31 import com.android.os.statsd.StatsdExtensionAtoms; 32 import com.android.os.statsd.StatsdExtensionAtoms.TestExtensionAtomReported; 33 import com.android.tradefed.build.IBuildInfo; 34 import com.android.tradefed.log.LogUtil.CLog; 35 import com.android.tradefed.build.IBuildInfo; 36 import com.android.tradefed.testtype.DeviceTestCase; 37 import com.android.tradefed.testtype.IBuildReceiver; 38 import com.android.tradefed.util.RunUtil; 39 40 import com.google.protobuf.ExtensionRegistry; 41 42 import java.util.List; 43 import java.util.concurrent.Flow.Subscription; 44 45 public class AtomParsingTests extends DeviceTestCase implements IBuildReceiver { 46 47 private IBuildInfo mCtsBuild; 48 49 @Override setUp()50 protected void setUp() throws Exception { 51 super.setUp(); 52 assertThat(mCtsBuild).isNotNull(); 53 ConfigUtils.removeConfig(getDevice()); 54 ReportUtils.clearReports(getDevice()); 55 DeviceUtils.installTestApp(getDevice(), MetricsUtils.DEVICE_SIDE_TEST_APK, 56 MetricsUtils.DEVICE_SIDE_TEST_PACKAGE, mCtsBuild); 57 RunUtil.getDefault().sleep(AtomTestUtils.WAIT_TIME_LONG); 58 } 59 60 @Override tearDown()61 protected void tearDown() throws Exception { 62 super.tearDown(); 63 ConfigUtils.removeConfig(getDevice()); 64 ReportUtils.clearReports(getDevice()); 65 DeviceUtils.uninstallTestApp(getDevice(), MetricsUtils.DEVICE_SIDE_TEST_PACKAGE); 66 } 67 68 @Override setBuild(IBuildInfo buildInfo)69 public void setBuild(IBuildInfo buildInfo) { 70 mCtsBuild = buildInfo; 71 } 72 testWriteExtensionTestAtom()73 public void testWriteExtensionTestAtom() throws Exception { 74 final int atomTag = StatsdExtensionAtoms.TEST_EXTENSION_ATOM_REPORTED_FIELD_NUMBER; 75 ConfigUtils.uploadConfigForPushedAtomWithUid(getDevice(), 76 MetricsUtils.DEVICE_SIDE_TEST_PACKAGE, atomTag, /*useUidAttributionChain=*/true); 77 78 DeviceUtils.runDeviceTests(getDevice(), MetricsUtils.DEVICE_SIDE_TEST_PACKAGE, 79 ".AtomTests", "testWriteExtensionTestAtom"); 80 81 RunUtil.getDefault().sleep(AtomTestUtils.WAIT_TIME_SHORT); 82 // Sorted list of events in order in which they occurred. 83 84 ExtensionRegistry registry = ExtensionRegistry.newInstance(); 85 StatsdExtensionAtoms.registerAllExtensions(registry); 86 87 List<EventMetricData> data = ReportUtils.getEventMetricDataList(getDevice(), registry); 88 assertThat(data).hasSize(4); 89 90 TestExtensionAtomReported atom = data.get(0).getAtom().getExtension( 91 StatsdExtensionAtoms.testExtensionAtomReported); 92 List<AttributionNode> attrChain = atom.getAttributionNodeList(); 93 assertThat(attrChain).hasSize(2); 94 assertThat(attrChain.get(0).getUid()).isEqualTo(1234); 95 assertThat(attrChain.get(0).getTag()).isEqualTo("tag1"); 96 assertThat(attrChain.get(1).getUid()).isEqualTo( 97 DeviceUtils.getAppUid(getDevice(), MetricsUtils.DEVICE_SIDE_TEST_PACKAGE)); 98 assertThat(attrChain.get(1).getTag()).isEqualTo("tag2"); 99 100 assertThat(atom.getIntField()).isEqualTo(42); 101 assertThat(atom.getLongField()).isEqualTo(Long.MAX_VALUE); 102 assertThat(atom.getFloatField()).isEqualTo(3.14f); 103 assertThat(atom.getStringField()).isEqualTo("This is a basic test!"); 104 assertThat(atom.getBooleanField()).isFalse(); 105 assertThat(atom.getState().getNumber()).isEqualTo(TestExtensionAtomReported.State.ON_VALUE); 106 assertThat(atom.getBytesField().getLongFieldList()) 107 .containsExactly(1L, 2L, 3L).inOrder(); 108 109 assertThat(atom.getRepeatedIntFieldList()).containsExactly(3, 6).inOrder(); 110 assertThat(atom.getRepeatedLongFieldList()).containsExactly(1000L, 1002L).inOrder(); 111 assertThat(atom.getRepeatedFloatFieldList()).containsExactly(0.3f, 0.09f).inOrder(); 112 assertThat(atom.getRepeatedStringFieldList()).containsExactly("str1", "str2").inOrder(); 113 assertThat(atom.getRepeatedBooleanFieldList()).containsExactly(true, false).inOrder(); 114 assertThat(atom.getRepeatedEnumFieldList()) 115 .containsExactly(TestExtensionAtomReported.State.OFF, 116 TestExtensionAtomReported.State.ON).inOrder(); 117 118 atom = data.get(1).getAtom().getExtension( 119 StatsdExtensionAtoms.testExtensionAtomReported); 120 attrChain = atom.getAttributionNodeList(); 121 assertThat(attrChain).hasSize(2); 122 assertThat(attrChain.get(0).getUid()).isEqualTo(9999); 123 assertThat(attrChain.get(0).getTag()).isEqualTo("tag9999"); 124 assertThat(attrChain.get(1).getUid()).isEqualTo( 125 DeviceUtils.getAppUid(getDevice(), MetricsUtils.DEVICE_SIDE_TEST_PACKAGE)); 126 assertThat(attrChain.get(1).getTag()).isEmpty(); 127 128 assertThat(atom.getIntField()).isEqualTo(100); 129 assertThat(atom.getLongField()).isEqualTo(Long.MIN_VALUE); 130 assertThat(atom.getFloatField()).isEqualTo(-2.5f); 131 assertThat(atom.getStringField()).isEqualTo("Test null uid"); 132 assertThat(atom.getBooleanField()).isTrue(); 133 assertThat(atom.getState().getNumber()).isEqualTo( 134 TestExtensionAtomReported.State.UNKNOWN_VALUE); 135 assertThat(atom.getBytesField().getLongFieldList()) 136 .containsExactly(1L, 2L, 3L).inOrder(); 137 assertThat(atom.getRepeatedIntFieldList()).containsExactly(3, 6).inOrder(); 138 assertThat(atom.getRepeatedLongFieldList()).containsExactly(1000L, 1002L).inOrder(); 139 assertThat(atom.getRepeatedFloatFieldList()).containsExactly(0.3f, 0.09f).inOrder(); 140 assertThat(atom.getRepeatedStringFieldList()).containsExactly("str1", "str2").inOrder(); 141 assertThat(atom.getRepeatedBooleanFieldList()).containsExactly(true, false).inOrder(); 142 assertThat(atom.getRepeatedEnumFieldList()) 143 .containsExactly(TestExtensionAtomReported.State.OFF, 144 TestExtensionAtomReported.State.ON) 145 .inOrder(); 146 147 atom = data.get(2).getAtom().getExtension( 148 StatsdExtensionAtoms.testExtensionAtomReported); 149 attrChain = atom.getAttributionNodeList(); 150 assertThat(attrChain).hasSize(1); 151 assertThat(attrChain.get(0).getUid()).isEqualTo( 152 DeviceUtils.getAppUid(getDevice(), MetricsUtils.DEVICE_SIDE_TEST_PACKAGE)); 153 assertThat(attrChain.get(0).getTag()).isEqualTo("tag1"); 154 155 assertThat(atom.getIntField()).isEqualTo(-256); 156 assertThat(atom.getLongField()).isEqualTo(-1234567890L); 157 assertThat(atom.getFloatField()).isEqualTo(42.01f); 158 assertThat(atom.getStringField()).isEqualTo("Test non chained"); 159 assertThat(atom.getBooleanField()).isTrue(); 160 assertThat(atom.getState().getNumber()).isEqualTo( 161 TestExtensionAtomReported.State.OFF_VALUE); 162 assertThat(atom.getBytesField().getLongFieldList()) 163 .containsExactly(1L, 2L, 3L).inOrder(); 164 assertThat(atom.getRepeatedIntFieldList()).isEmpty(); 165 assertThat(atom.getRepeatedLongFieldList()).isEmpty(); 166 assertThat(atom.getRepeatedFloatFieldList()).isEmpty(); 167 assertThat(atom.getRepeatedStringFieldList()).isEmpty(); 168 assertThat(atom.getRepeatedBooleanFieldList()).isEmpty(); 169 assertThat(atom.getRepeatedEnumFieldList()).isEmpty(); 170 171 atom = data.get(3).getAtom().getExtension( 172 StatsdExtensionAtoms.testExtensionAtomReported); 173 attrChain = atom.getAttributionNodeList(); 174 assertThat(attrChain).hasSize(1); 175 assertThat(attrChain.get(0).getUid()).isEqualTo( 176 DeviceUtils.getAppUid(getDevice(), MetricsUtils.DEVICE_SIDE_TEST_PACKAGE)); 177 assertThat(attrChain.get(0).getTag()).isEmpty(); 178 179 assertThat(atom.getIntField()).isEqualTo(0); 180 assertThat(atom.getLongField()).isEqualTo(0L); 181 assertThat(atom.getFloatField()).isEqualTo(0f); 182 assertThat(atom.getStringField()).isEmpty(); 183 assertThat(atom.getBooleanField()).isTrue(); 184 assertThat(atom.getState().getNumber()).isEqualTo( 185 TestExtensionAtomReported.State.OFF_VALUE); 186 assertThat(atom.getBytesField().getLongFieldList()).isEmpty(); 187 assertThat(atom.getRepeatedIntFieldList()).isEmpty(); 188 assertThat(atom.getRepeatedLongFieldList()).isEmpty(); 189 assertThat(atom.getRepeatedFloatFieldList()).isEmpty(); 190 assertThat(atom.getRepeatedStringFieldList()).isEmpty(); 191 assertThat(atom.getRepeatedBooleanFieldList()).isEmpty(); 192 assertThat(atom.getRepeatedEnumFieldList()).isEmpty(); 193 } 194 testWriteRawTestAtom()195 public void testWriteRawTestAtom() throws Exception { 196 final int atomTag = Atom.TEST_ATOM_REPORTED_FIELD_NUMBER; 197 ConfigUtils.uploadConfigForPushedAtomWithUid(getDevice(), 198 MetricsUtils.DEVICE_SIDE_TEST_PACKAGE, atomTag, /*useUidAttributionChain=*/true); 199 200 DeviceUtils.runDeviceTests(getDevice(), MetricsUtils.DEVICE_SIDE_TEST_PACKAGE, 201 ".AtomTests", "testWriteRawTestAtom"); 202 203 RunUtil.getDefault().sleep(AtomTestUtils.WAIT_TIME_SHORT); 204 // Sorted list of events in order in which they occurred. 205 List<EventMetricData> data = ReportUtils.getEventMetricDataList(getDevice()); 206 assertThat(data).hasSize(4); 207 208 TestAtomReported atom = data.get(0).getAtom().getTestAtomReported(); 209 List<AttributionNode> attrChain = atom.getAttributionNodeList(); 210 assertThat(attrChain).hasSize(2); 211 assertThat(attrChain.get(0).getUid()).isEqualTo(1234); 212 assertThat(attrChain.get(0).getTag()).isEqualTo("tag1"); 213 assertThat(attrChain.get(1).getUid()).isEqualTo( 214 DeviceUtils.getAppUid(getDevice(), MetricsUtils.DEVICE_SIDE_TEST_PACKAGE)); 215 assertThat(attrChain.get(1).getTag()).isEqualTo("tag2"); 216 217 assertThat(atom.getIntField()).isEqualTo(42); 218 assertThat(atom.getLongField()).isEqualTo(Long.MAX_VALUE); 219 assertThat(atom.getFloatField()).isEqualTo(3.14f); 220 assertThat(atom.getStringField()).isEqualTo("This is a basic test!"); 221 assertThat(atom.getBooleanField()).isFalse(); 222 assertThat(atom.getState().getNumber()).isEqualTo(TestAtomReported.State.ON_VALUE); 223 assertThat(atom.getBytesField().getExperimentIdList()) 224 .containsExactly(1L, 2L, 3L).inOrder(); 225 226 assertThat(atom.getRepeatedIntFieldList()).containsExactly(3, 6).inOrder(); 227 assertThat(atom.getRepeatedLongFieldList()).containsExactly(1000L, 1002L).inOrder(); 228 assertThat(atom.getRepeatedFloatFieldList()).containsExactly(0.3f, 0.09f).inOrder(); 229 assertThat(atom.getRepeatedStringFieldList()).containsExactly("str1", "str2").inOrder(); 230 assertThat(atom.getRepeatedBooleanFieldList()).containsExactly(true, false).inOrder(); 231 assertThat(atom.getRepeatedEnumFieldList()) 232 .containsExactly(TestAtomReported.State.OFF, TestAtomReported.State.ON) 233 .inOrder(); 234 235 atom = data.get(1).getAtom().getTestAtomReported(); 236 attrChain = atom.getAttributionNodeList(); 237 assertThat(attrChain).hasSize(2); 238 assertThat(attrChain.get(0).getUid()).isEqualTo(9999); 239 assertThat(attrChain.get(0).getTag()).isEqualTo("tag9999"); 240 assertThat(attrChain.get(1).getUid()).isEqualTo( 241 DeviceUtils.getAppUid(getDevice(), MetricsUtils.DEVICE_SIDE_TEST_PACKAGE)); 242 assertThat(attrChain.get(1).getTag()).isEmpty(); 243 244 assertThat(atom.getIntField()).isEqualTo(100); 245 assertThat(atom.getLongField()).isEqualTo(Long.MIN_VALUE); 246 assertThat(atom.getFloatField()).isEqualTo(-2.5f); 247 assertThat(atom.getStringField()).isEqualTo("Test null uid"); 248 assertThat(atom.getBooleanField()).isTrue(); 249 assertThat(atom.getState().getNumber()).isEqualTo(TestAtomReported.State.UNKNOWN_VALUE); 250 assertThat(atom.getBytesField().getExperimentIdList()) 251 .containsExactly(1L, 2L, 3L).inOrder(); 252 253 assertThat(atom.getRepeatedIntFieldList()).containsExactly(3, 6).inOrder(); 254 assertThat(atom.getRepeatedLongFieldList()).containsExactly(1000L, 1002L).inOrder(); 255 assertThat(atom.getRepeatedFloatFieldList()).containsExactly(0.3f, 0.09f).inOrder(); 256 assertThat(atom.getRepeatedStringFieldList()).containsExactly("str1", "str2").inOrder(); 257 assertThat(atom.getRepeatedBooleanFieldList()).containsExactly(true, false).inOrder(); 258 assertThat(atom.getRepeatedEnumFieldList()) 259 .containsExactly(TestAtomReported.State.OFF, TestAtomReported.State.ON) 260 .inOrder(); 261 262 atom = data.get(2).getAtom().getTestAtomReported(); 263 attrChain = atom.getAttributionNodeList(); 264 assertThat(attrChain).hasSize(1); 265 assertThat(attrChain.get(0).getUid()).isEqualTo( 266 DeviceUtils.getAppUid(getDevice(), MetricsUtils.DEVICE_SIDE_TEST_PACKAGE)); 267 assertThat(attrChain.get(0).getTag()).isEqualTo("tag1"); 268 269 assertThat(atom.getIntField()).isEqualTo(-256); 270 assertThat(atom.getLongField()).isEqualTo(-1234567890L); 271 assertThat(atom.getFloatField()).isEqualTo(42.01f); 272 assertThat(atom.getStringField()).isEqualTo("Test non chained"); 273 assertThat(atom.getBooleanField()).isTrue(); 274 assertThat(atom.getState().getNumber()).isEqualTo(TestAtomReported.State.OFF_VALUE); 275 assertThat(atom.getBytesField().getExperimentIdList()) 276 .containsExactly(1L, 2L, 3L).inOrder(); 277 278 assertThat(atom.getRepeatedIntFieldList()).isEmpty(); 279 assertThat(atom.getRepeatedLongFieldList()).isEmpty(); 280 assertThat(atom.getRepeatedFloatFieldList()).isEmpty(); 281 assertThat(atom.getRepeatedStringFieldList()).isEmpty(); 282 assertThat(atom.getRepeatedBooleanFieldList()).isEmpty(); 283 assertThat(atom.getRepeatedEnumFieldList()).isEmpty(); 284 285 atom = data.get(3).getAtom().getTestAtomReported(); 286 attrChain = atom.getAttributionNodeList(); 287 assertThat(attrChain).hasSize(1); 288 assertThat(attrChain.get(0).getUid()).isEqualTo( 289 DeviceUtils.getAppUid(getDevice(), MetricsUtils.DEVICE_SIDE_TEST_PACKAGE)); 290 assertThat(attrChain.get(0).getTag()).isEmpty(); 291 292 assertThat(atom.getIntField()).isEqualTo(0); 293 assertThat(atom.getLongField()).isEqualTo(0L); 294 assertThat(atom.getFloatField()).isEqualTo(0f); 295 assertThat(atom.getStringField()).isEmpty(); 296 assertThat(atom.getBooleanField()).isTrue(); 297 assertThat(atom.getState().getNumber()).isEqualTo(TestAtomReported.State.OFF_VALUE); 298 assertThat(atom.getBytesField().getExperimentIdList()).isEmpty(); 299 300 assertThat(atom.getRepeatedIntFieldList()).isEmpty(); 301 assertThat(atom.getRepeatedLongFieldList()).isEmpty(); 302 assertThat(atom.getRepeatedFloatFieldList()).isEmpty(); 303 assertThat(atom.getRepeatedStringFieldList()).isEmpty(); 304 assertThat(atom.getRepeatedBooleanFieldList()).isEmpty(); 305 assertThat(atom.getRepeatedEnumFieldList()).isEmpty(); 306 } 307 } 308