summaryrefslogtreecommitdiff
path: root/hostsidetests/statsdatom/src/android/cts/statsdatom/appops/AppOpsTests.java
blob: 0ccc450f66577a180c8e33f1d7743017bf8746c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.cts.statsdatom.appops;

import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage;

import android.cts.statsdatom.lib.AtomTestUtils;
import android.cts.statsdatom.lib.ConfigUtils;
import android.cts.statsdatom.lib.DeviceUtils;
import android.cts.statsdatom.lib.ReportUtils;

import com.android.os.AtomsProto;
import com.android.tradefed.build.IBuildInfo;
import com.android.tradefed.testtype.DeviceTestCase;
import com.android.tradefed.testtype.IBuildReceiver;

import com.google.protobuf.Descriptors;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class AppOpsTests extends DeviceTestCase implements IBuildReceiver {
    private static final int NUM_APP_OPS = AtomsProto.AttributedAppOps.getDefaultInstance().getOp().
            getDescriptorForType().getValues().size() - 1;

    /**
     * Some ops are only available to specific dynamic uids and are otherwise transformed to less
     * privileged ops. For example, RECORD_AUDIO_HOTWORD is downgraded to RECORD_AUDIO. This stores
     * a mapping from an op to the op it can be transformed from.
     */
    private static final Map<Integer, Integer> TRANSFORMED_FROM_OP = new HashMap<>();

    static {
        final int APP_OP_RECORD_AUDIO = 27;
        final int APP_OP_RECORD_AUDIO_HOTWORD = 102;

        TRANSFORMED_FROM_OP.put(APP_OP_RECORD_AUDIO, APP_OP_RECORD_AUDIO_HOTWORD);
    }

    private IBuildInfo mCtsBuild;

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        assertThat(mCtsBuild).isNotNull();
        ConfigUtils.removeConfig(getDevice());
        ReportUtils.clearReports(getDevice());
        DeviceUtils.installStatsdTestApp(getDevice(), mCtsBuild);
        Thread.sleep(AtomTestUtils.WAIT_TIME_LONG);
    }

    @Override
    protected void tearDown() throws Exception {
        ConfigUtils.removeConfig(getDevice());
        ReportUtils.clearReports(getDevice());
        DeviceUtils.uninstallStatsdTestApp(getDevice());
        super.tearDown();
    }

    @Override
    public void setBuild(IBuildInfo buildInfo) {
        mCtsBuild = buildInfo;
    }

    public void testAppOps() throws Exception {
        // Set up what to collect
        ConfigUtils.uploadConfigForPulledAtom(getDevice(), DeviceUtils.STATSD_ATOM_TEST_PKG,
                AtomsProto.Atom.APP_OPS_FIELD_NUMBER);

        DeviceUtils.runDeviceTestsOnStatsdApp(getDevice(), ".AtomTests", "testAppOps");
        Thread.sleep(AtomTestUtils.WAIT_TIME_SHORT);

        // Pull a report
        AtomTestUtils.sendAppBreadcrumbReportedAtom(getDevice());
        Thread.sleep(AtomTestUtils.WAIT_TIME_SHORT);

        ArrayList<Integer> expectedOps = new ArrayList<>();
        Set<Integer> transformedOps = new HashSet<>(TRANSFORMED_FROM_OP.values());
        for (int i = 0; i < NUM_APP_OPS; i++) {
            if (!transformedOps.contains(i)) {
                expectedOps.add(i);
            }
        }

        for (Descriptors.EnumValueDescriptor valueDescriptor :
                AtomsProto.AttributedAppOps.getDefaultInstance().getOp().getDescriptorForType()
                        .getValues()) {
            if (valueDescriptor.getOptions().hasDeprecated()) {
                // Deprecated app op, remove from list of expected ones.
                expectedOps.remove(expectedOps.indexOf(valueDescriptor.getNumber()));
            }
        }
        for (AtomsProto.Atom atom : ReportUtils.getGaugeMetricAtoms(getDevice())) {

            AtomsProto.AppOps appOps = atom.getAppOps();
            if (appOps.getPackageName().equals(DeviceUtils.STATSD_ATOM_TEST_PKG)) {
                if (appOps.getOpId().getNumber() == -1) {
                    continue;
                }
                long totalNoted = appOps.getTrustedForegroundGrantedCount()
                        + appOps.getTrustedBackgroundGrantedCount()
                        + appOps.getTrustedForegroundRejectedCount()
                        + appOps.getTrustedBackgroundRejectedCount();
                int expectedNoted =
                        appOps.getOpId().getNumber() + 1
                                + computeExpectedTransformedNoted(appOps.getOpId().getNumber());
                assertWithMessage("Operation in APP_OPS_ENUM_MAP: " + appOps.getOpId().getNumber())
                        .that(totalNoted).isEqualTo(expectedNoted);
                assertWithMessage("Unexpected Op reported").that(expectedOps).contains(
                        appOps.getOpId().getNumber());
                expectedOps.remove(expectedOps.indexOf(appOps.getOpId().getNumber()));
            }
        }
        assertWithMessage("Logging app op ids are missing in report.").that(expectedOps).isEmpty();
    }

    private static int computeExpectedTransformedNoted(int op) {
        if (!TRANSFORMED_FROM_OP.containsKey(op)) {
            return 0;
        }
        return TRANSFORMED_FROM_OP.get(op) + 1;
    }
}