summaryrefslogtreecommitdiff
path: root/PermissionController/tests/mocking/src/com/android/permissioncontroller/tests/mocking/safetylabel/AppsSafetyLabelHistoryTest.kt
blob: 74eba4e4c8cc23ce39d272518d63e0ab6d703229 (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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/*
 * Copyright (C) 2023 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 com.android.permissioncontroller.tests.mocking.safetylabel

import android.os.Build
import android.os.PersistableBundle
import androidx.test.filters.SdkSuppress
import com.android.permission.safetylabel.SafetyLabel as AppMetadataSafetyLabel
import com.android.permission.safetylabel.SafetyLabel.KEY_VERSION
import com.android.permissioncontroller.safetylabel.AppsSafetyLabelHistory
import com.android.permissioncontroller.safetylabel.AppsSafetyLabelHistory.AppInfo
import com.android.permissioncontroller.safetylabel.AppsSafetyLabelHistory.AppSafetyLabelHistory
import com.android.permissioncontroller.safetylabel.AppsSafetyLabelHistory.DataCategory
import com.android.permissioncontroller.safetylabel.AppsSafetyLabelHistory.DataLabel
import com.android.permissioncontroller.safetylabel.AppsSafetyLabelHistory.SafetyLabel
import com.android.permissioncontroller.tests.mocking.safetylabel.TestSafetyLabels.DATE_2022_09_01
import com.android.permissioncontroller.tests.mocking.safetylabel.TestSafetyLabels.DATE_2022_10_10
import com.android.permissioncontroller.tests.mocking.safetylabel.TestSafetyLabels.LOCATION_CATEGORY
import com.android.permissioncontroller.tests.mocking.safetylabel.TestSafetyLabels.PACKAGE_NAME_1
import com.android.permissioncontroller.tests.mocking.safetylabel.TestSafetyLabels.PACKAGE_NAME_2
import com.android.permissioncontroller.tests.mocking.safetylabel.TestSafetyLabels.SAFETY_LABEL_PKG_1_V1
import com.android.permissioncontroller.tests.mocking.safetylabel.TestSafetyLabels.SAFETY_LABEL_PKG_1_V2
import com.android.permissioncontroller.tests.mocking.safetylabel.TestSafetyLabels.SAFETY_LABEL_PKG_1_V3
import com.android.permissioncontroller.tests.mocking.safetylabel.TestSafetyLabels.SAFETY_LABEL_PKG_2_V1
import com.google.common.truth.Truth.assertThat
import java.time.ZonedDateTime
import org.junit.Assert
import org.junit.Test

/** Tests for [AppsSafetyLabelHistory]. */
@SdkSuppress(minSdkVersion = Build.VERSION_CODES.UPSIDE_DOWN_CAKE, codeName = "UpsideDownCake")
class AppsSafetyLabelHistoryTest {

    @Test
    fun createAppSafetyLabelHistory_requiresAllSafetyLabelsHaveSameApp() {
        Assert.assertThrows(IllegalArgumentException::class.java) {
            AppSafetyLabelHistory(
                AppInfo(PACKAGE_NAME_1),
                listOf(SAFETY_LABEL_PKG_1_V1, SAFETY_LABEL_PKG_2_V1)
            )
        }
    }

    @Test
    fun createAppSafetyLabelHistory_requiresOrderedByReceivedAt() {
        Assert.assertThrows(IllegalArgumentException::class.java) {
            AppSafetyLabelHistory(
                AppInfo(PACKAGE_NAME_1),
                listOf(SAFETY_LABEL_PKG_1_V2, SAFETY_LABEL_PKG_1_V1)
            )
        }
    }

    @Test
    fun withSafetyLabel_forDifferentApp_throwsIllegalArgumentException() {
        val appSafetyLabelHistory =
            AppSafetyLabelHistory(
                AppInfo(PACKAGE_NAME_1),
                listOf(SAFETY_LABEL_PKG_1_V1, SAFETY_LABEL_PKG_1_V3)
            )

        Assert.assertThrows(IllegalArgumentException::class.java) {
            appSafetyLabelHistory.withSafetyLabel(SAFETY_LABEL_PKG_2_V1, 20)
        }
    }

    @Test
    fun withSafetyLabel_returnsOrderdSafetyLabels() {
        val appSafetyLabelHistory =
            AppSafetyLabelHistory(
                AppInfo(PACKAGE_NAME_1),
                listOf(SAFETY_LABEL_PKG_1_V1, SAFETY_LABEL_PKG_1_V3)
            )

        assertThat(appSafetyLabelHistory.withSafetyLabel(SAFETY_LABEL_PKG_1_V2, 20))
            .isEqualTo(
                AppSafetyLabelHistory(
                    AppInfo(PACKAGE_NAME_1),
                    listOf(SAFETY_LABEL_PKG_1_V1, SAFETY_LABEL_PKG_1_V2, SAFETY_LABEL_PKG_1_V3)
                )
            )
    }

    @Test
    fun withSafetyLabel_dropsOldLabelsWhenMaxPersisted() {
        val appSafetyLabelHistory =
            AppSafetyLabelHistory(
                AppInfo(PACKAGE_NAME_1),
                listOf(SAFETY_LABEL_PKG_1_V1, SAFETY_LABEL_PKG_1_V3)
            )

        assertThat(appSafetyLabelHistory.withSafetyLabel(SAFETY_LABEL_PKG_1_V2, 2))
            .isEqualTo(
                AppSafetyLabelHistory(
                    AppInfo(PACKAGE_NAME_1),
                    listOf(SAFETY_LABEL_PKG_1_V2, SAFETY_LABEL_PKG_1_V3)
                )
            )
    }

    @Test
    fun extractLocationSharingSafetyLabel_noSharing_returnsSafetyLabelForPersistence() {
        val metadataBundle = createMetadataWithDataShared(createDataSharedNoSharing())
        val appMetadataSafetyLabel =
            AppMetadataSafetyLabel.getSafetyLabelFromMetadata(metadataBundle)!!

        val safetyLabelForPersistence =
            SafetyLabel.extractLocationSharingSafetyLabel(
                PACKAGE_NAME_1,
                DATE_2022_09_01,
                appMetadataSafetyLabel
            )

        assertThat(safetyLabelForPersistence)
            .isEqualTo(SafetyLabel(AppInfo(PACKAGE_NAME_1), DATE_2022_09_01, DataLabel(mapOf())))
    }

    @Test
    fun extractLocationSharingSafetyLabel_locationSharingNoAds_returnsSafetyLabelForPersistence() {
        val metadataBundle = createMetadataWithDataShared(createDataSharedWithLocationNoAds())
        val appMetadataSafetyLabel =
            AppMetadataSafetyLabel.getSafetyLabelFromMetadata(metadataBundle)!!

        val safetyLabelForPersistence =
            SafetyLabel.extractLocationSharingSafetyLabel(
                PACKAGE_NAME_1,
                DATE_2022_10_10,
                appMetadataSafetyLabel
            )

        assertThat(safetyLabelForPersistence)
            .isEqualTo(
                SafetyLabel(
                    AppInfo(PACKAGE_NAME_1),
                    DATE_2022_10_10,
                    DataLabel(mapOf(LOCATION_CATEGORY to DataCategory(false)))
                )
            )
    }

    @Test
    fun extractLocationSharingSafetyLabel_locationSharingAds_returnsSafetyLabelForPersistence() {
        val metadataBundle = createMetadataWithDataShared(createDataSharedWithLocationWithAds())
        val appMetadataSafetyLabel =
            AppMetadataSafetyLabel.getSafetyLabelFromMetadata(metadataBundle)!!

        val safetyLabelForPersistence =
            SafetyLabel.extractLocationSharingSafetyLabel(
                PACKAGE_NAME_2,
                DATE_2022_10_10,
                appMetadataSafetyLabel
            )

        assertThat(safetyLabelForPersistence)
            .isEqualTo(
                SafetyLabel(
                    AppInfo(PACKAGE_NAME_2),
                    DATE_2022_10_10,
                    DataLabel(mapOf(LOCATION_CATEGORY to DataCategory(true)))
                )
            )
    }

    @Test
    fun extractLocationSharingSafetyLabel_financeCategory_returnsEmptySafetyLabel() {
        val metadataBundle = createMetadataWithDataShared(createDataSharedWithFinanceWithAds())
        val appMetadataSafetyLabel =
            AppMetadataSafetyLabel.getSafetyLabelFromMetadata(metadataBundle)!!

        val safetyLabelForPersistence =
            SafetyLabel.extractLocationSharingSafetyLabel(
                PACKAGE_NAME_2,
                DATE_2022_10_10,
                appMetadataSafetyLabel
            )

        assertThat(safetyLabelForPersistence)
            .isEqualTo(SafetyLabel(AppInfo(PACKAGE_NAME_2), DATE_2022_10_10, DataLabel(mapOf())))
    }

    @Test
    fun extractLocationSharingSafetyLabel_locationFinance_returnsLocationSafetyLabel() {
        val metadataBundle =
            createMetadataWithDataShared(createDataSharedWithLocationAndFinanceWithAds())
        val appMetadataSafetyLabel =
            AppMetadataSafetyLabel.getSafetyLabelFromMetadata(metadataBundle)!!

        val safetyLabelForPersistence =
            SafetyLabel.extractLocationSharingSafetyLabel(
                PACKAGE_NAME_2,
                DATE_2022_10_10,
                appMetadataSafetyLabel
            )

        assertThat(safetyLabelForPersistence)
            .isEqualTo(
                SafetyLabel(
                    AppInfo(PACKAGE_NAME_2),
                    DATE_2022_10_10,
                    DataLabel(mapOf(LOCATION_CATEGORY to DataCategory(true)))
                )
            )
    }

    /** Companion object for [AppsSafetyLabelHistoryTest]. */
    companion object {
        private const val PACKAGE_NAME_1 = "package_name_1"
        private const val PACKAGE_NAME_2 = "package_name_2"
        private const val FINANCE_CATEGORY = "finance"
        private const val FINANCIAL_PURCHASE_HISTORY = "purchase_history"
        private const val LOCATION_CATEGORY = "location"
        private const val APPROX_LOCATION = "approx_location"
        private const val PURPOSE_FRAUD_PREVENTION_SECURITY = 4
        private const val PURPOSE_ADVERTISING = 5
        private const val SAFETY_LABEL_KEY = "safety_labels"
        private const val DATA_SHARED_KEY = "data_shared"
        private const val DATA_LABEL_KEY = "data_labels"
        private const val PURPOSES_KEY = "purposes"
        private const val TOP_LEVEL_VERSION = 1L
        private const val SAFETY_LABELS_VERSION = 1L
        private val DATE_2022_09_01 = ZonedDateTime.parse("2022-09-01T00:00:00.000Z").toInstant()
        private val DATE_2022_10_10 = ZonedDateTime.parse("2022-10-10T00:00:00.000Z").toInstant()

        fun createDataSharedNoSharing(): PersistableBundle {
            return PersistableBundle()
        }

        fun createDataSharedWithLocationNoAds(): PersistableBundle {
            val locationBundle =
                PersistableBundle().apply {
                    putPersistableBundle(
                        APPROX_LOCATION,
                        PersistableBundle().apply {
                            putIntArray(
                                PURPOSES_KEY,
                                listOf(PURPOSE_FRAUD_PREVENTION_SECURITY).toIntArray()
                            )
                        }
                    )
                }

            return PersistableBundle().apply {
                putPersistableBundle(LOCATION_CATEGORY, locationBundle)
            }
        }

        fun createDataSharedWithLocationWithAds(): PersistableBundle {
            val locationBundle =
                PersistableBundle().apply {
                    putPersistableBundle(
                        APPROX_LOCATION,
                        PersistableBundle().apply {
                            putIntArray(PURPOSES_KEY, listOf(PURPOSE_ADVERTISING).toIntArray())
                        }
                    )
                }

            return PersistableBundle().apply {
                putPersistableBundle(LOCATION_CATEGORY, locationBundle)
            }
        }

        fun createDataSharedWithFinanceWithAds(): PersistableBundle {
            val financeBundle =
                PersistableBundle().apply {
                    putPersistableBundle(
                        FINANCIAL_PURCHASE_HISTORY,
                        PersistableBundle().apply {
                            putIntArray(PURPOSES_KEY, listOf(PURPOSE_ADVERTISING).toIntArray())
                        }
                    )
                }

            return PersistableBundle().apply {
                putPersistableBundle(FINANCE_CATEGORY, financeBundle)
            }
        }

        fun createDataSharedWithLocationAndFinanceWithAds(): PersistableBundle {
            val locationBundle =
                PersistableBundle().apply {
                    putPersistableBundle(
                        APPROX_LOCATION,
                        PersistableBundle().apply {
                            putIntArray(PURPOSES_KEY, listOf(PURPOSE_ADVERTISING).toIntArray())
                        }
                    )
                }
            val financeBundle =
                PersistableBundle().apply {
                    putPersistableBundle(
                        FINANCIAL_PURCHASE_HISTORY,
                        PersistableBundle().apply {
                            putIntArray(PURPOSES_KEY, listOf(PURPOSE_ADVERTISING).toIntArray())
                        }
                    )
                }

            return PersistableBundle().apply {
                putPersistableBundle(FINANCE_CATEGORY, financeBundle)
                putPersistableBundle(LOCATION_CATEGORY, locationBundle)
            }
        }

        fun createMetadataWithDataShared(dataSharedBundle: PersistableBundle): PersistableBundle {
            val dataLabelBundle =
                PersistableBundle().apply {
                    putPersistableBundle(DATA_SHARED_KEY, dataSharedBundle)
                }

            val safetyLabelBundle =
                PersistableBundle().apply {
                    putLong(KEY_VERSION, SAFETY_LABELS_VERSION)
                    putPersistableBundle(DATA_LABEL_KEY, dataLabelBundle)
                }

            return PersistableBundle().apply {
                putLong(KEY_VERSION, TOP_LEVEL_VERSION)
                putPersistableBundle(SAFETY_LABEL_KEY, safetyLabelBundle)
            }
        }
    }
}